login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Gastenboeken > Gastenboek class V1.0

Gastenboek class V1.0

Auteur: marten - 10 december 2007 - 14:46 - Gekeurd door: Stijn - Hits: 7490 - Aantal punten: (0 stemmen)




Dit is een gastenboek script die ik gemaakt heb. Dit is erg basic allemaal. Ik ga nu verder met de childs van deze class zoals:

* admin systeem
* Captcha beveiliging
* Woord Blokkage Systeem

Het systeem maakt gebruik van PDO. Dit moet je dus geinstalleerd hebben staan. Opmerkingen zijn altijd welkom. Wel graag goed onderbouwt aub.

Code:
  1. CREATE TABLE `messages` (
  2. `message_id` int(10) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(50) NOT NULL,
  4. `email` varchar(50) DEFAULT NULL,
  5. `message` longtext NOT NULL,
  6. `posted` datetime DEFAULT NULL,
  7. PRIMARY KEY (`message_id`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;


  1. <?php
  2. /**
  3.  * CG class (ComSi Guestbook)
  4.  *
  5.  * @version 1.0 alpha
  6.  * @author Marten van Urk .: ComSi :. m.van.urk@comsi.nl .: www.comsi.nl :.
  7.  *
  8.  *
  9.  */
  10. class Gastenboek {
  11. /**
  12. * Settings of the guestbook
  13. *
  14. * guestbook_name The name of the guestbook
  15. * background_color The background color of the guestbook
  16. *
  17. * font_face The default font of the guestbook
  18. * font_color The color of the default font of the guestbook
  19. * font_size The size of the default font of the guestbook
  20. *
  21. * link_color The default color of a link
  22. * hover_link_color The default color of a hover link
  23. * visited_link_color The default color of a visited link
  24. * active_link_color The default color of a active link
  25. *
  26. * title_font_face The default font of a title
  27. * title_font_color The default font color of the title
  28. * title_font_size The default font size of the title
  29. *
  30. * Add_font_face The default font of the Add Message button
  31. * Add_font_color The default font color of the Add message button
  32. * Add_font_size The default font size of the Add Message button
  33. *
  34. * cell_background_color1 The background color of the first cell (name)
  35. * cell_background_color2 The background color of the second cell (email)
  36. * cell_background_color3 The background color of the third cell (Date / Time)
  37. * cell_background_color4 The background color of the fourth cell (Message)
  38. *
  39. * Border size The border size of the guestbook table
  40. * Border color The border color of the guestbook table
  41. * Table spacing The spacing in the guestbook table
  42. * Table width The width of the guestbook table
  43. *
  44. * Messages per page The number of messages on a single page.
  45. *
  46. * @var Array
  47. */
  48. public $settings = array(
  49. 'guestbook_name' => '',
  50. 'background_color' => '',
  51. 'font_face' => 'verdana',
  52. 'font_color' => '',
  53. 'font_size' => '',
  54. 'link_color' => '',
  55. 'hover_link_color' => '',
  56. 'visited_link_color' => '',
  57. 'active_link_color' => '',
  58. 'title_font_color' => '',
  59. 'title_font_face' => '',
  60. 'title_font_size' => '',
  61. 'add_font_face' => 'verdana',
  62. 'add_font_size' => '',
  63. 'add_font_color' => '',
  64. 'cell_background_color1' => '',
  65. 'cell_background_color2' => '',
  66. 'cell_background_color3' => '',
  67. 'cell_background_color4' => '',
  68. 'border_size' => '',
  69. 'border_color' => '',
  70. 'table_spacing' => '',
  71. 'table_width' => '',
  72. 'messages_per_page' => 10);
  73.  
  74. /**
  75. * Database connection
  76. *
  77. * @var Object
  78. */
  79. private $db;
  80.  
  81. /**
  82. * The last stored message id
  83. *
  84. * @var unknown_type
  85. */
  86. public $message_id;
  87.  
  88. /**
  89. * The message
  90. *
  91. * @var String
  92. */
  93. public $message;
  94.  
  95. /**
  96. * The email
  97. *
  98. * @var string
  99. */
  100. public $email;
  101.  
  102. /**
  103. * The poster's name
  104. *
  105. * @var string
  106. */
  107. public $name;
  108.  
  109. /**
  110. * Stores the last error message
  111. *
  112. * @var String
  113. */
  114. public $error;
  115.  
  116. /**
  117. * Constructor, Set the database connection and set the class variables to an empty string
  118. *
  119. * @param Object $db
  120. */
  121. public function __construct($db) {
  122. $this->db = $db;
  123. $this->message_id = '';
  124. $this->message = '';
  125. $this->email = '';
  126. $this->name = '';
  127. $this->error = '';
  128. }
  129.  
  130. /**
  131. * Set the message
  132. *
  133. * @param unknown_type $message
  134. */
  135. public function setMessage($message) {
  136. $this->message = $message;
  137. }
  138.  
  139. /**
  140. * Set an email address
  141. *
  142. * @param String $email
  143. */
  144. public function setEmail($email) {
  145. $this->email = $email;
  146. }
  147.  
  148. /**
  149. * Set the poster's name
  150. *
  151. * @param String $name
  152. */
  153. public function setName($name) {
  154. $this->name = $name;
  155. }
  156.  
  157. /**
  158. * Get the stored message
  159. *
  160. * @return String
  161. */
  162. public function getMessage() {
  163. if (isset($this->message)) {
  164. return $this->message;
  165. }
  166. }
  167.  
  168. /**
  169. * Get the stored email address
  170. *
  171. * @return String
  172. */
  173. public function getEmail() {
  174. if (isset($this->email)) {
  175. return $this->email;
  176. }
  177. }
  178.  
  179. /**
  180. * Get the stored poster's name
  181. *
  182. * @return String
  183. */
  184. public function getName() {
  185. if (isset($this->name)) {
  186. return $this->name;
  187. }
  188. }
  189.  
  190. /**
  191. * Return a setting
  192. * If you want return all settings you need $setting = all
  193. *
  194. * @author Marten van Urk .: ComSi :. m.van.urk@comsi.nl .: www.comsi.nl :.
  195. * @param $setting The desired setting | $setting = all for the complete array
  196. */
  197. public function getSetting($setting) {
  198. if ($setting == 'all') {
  199. return $this->settings;
  200. } else {
  201. if (array_key_exists($setting)) {
  202. return $this->settings[$setting];
  203. } else {
  204. return false;
  205. }
  206. }
  207. }
  208.  
  209. /**
  210. * Set a setting
  211. *
  212. * @author Marten van Urk .: ComSi :. m.van.urk@comsi.nl .: www.comsi.nl :.
  213. * @param String $setting The setting key
  214. * @param String $value The new value
  215. * @return Boolean True when succesfull, False when failed
  216. */
  217. public function setSetting($setting, $value) {
  218. if (array_key_exists($setting)) {
  219. $this->settings[$setting] = $value;
  220. return true;
  221. } else {
  222. return false;
  223. }
  224. }
  225.  
  226. /**
  227. * Add message in the guestbook. Message, Name and email should be filled
  228. *
  229. * @author Marten van Urk .: ComSi :. m.van.urk@comsi.nl .: www.comsi.nl :.
  230. * @param String $message
  231. * @param String $name
  232. * @param String $email
  233. * @return Boolean True when successfully inserted the message in the database otherwise false
  234. */
  235. public function addMessage() {
  236. if ($this->checkEmail($this->email) === true) {
  237. if (strlen($this->message) != 0) {
  238. if (strlen($this->name) != 0) {
  239. /**
  240. * Filter the user-supplied text from (bad) html code
  241. */
  242. $this->message = htmlspecialchars($this->message, ENT_QUOTES);
  243. $this->email = htmlspecialchars($this->email, ENT_QUOTES);
  244. $this->name = htmlspecialchars($this->name, ENT_QUOTES);
  245.  
  246. $sQuery = "INSERT INTO messages (name, email, message, posted) VALEUS ('" .$this->name. "', '" .$this->email. "', '" .$this->message. "', NOW())";
  247.  
  248. /**
  249. * Try to execute the query. When a error occurs the error message will be stored in $this->error
  250. */
  251. try {
  252. $this->db->query($sQuery);
  253. return true;
  254. } catch (PDOException $e) {
  255. $this->error = 'Can\'t insert the message:' . $e->getMessage();
  256. return false;
  257. }
  258.  
  259. return true;
  260. } else {
  261. return false;
  262. }
  263. } else {
  264. return false;
  265. }
  266. } else {
  267. return false;
  268. }
  269. }
  270.  
  271. /**
  272. * Check a email address
  273. *
  274. * @author Marten van Urk .: ComSi :. m.van.urk@comsi.nl .: www.comsi.nl :.
  275. * @param String $email
  276. * @return Boolean True when the email is correct otherwise false
  277. */
  278. private function checkEmail($email) {
  279. if(!eregi("^[a-z0-9_-]+@[a-z0-9._-]+\.[a-z]{2,4}$", $email)) {
  280. return false;
  281. } else {
  282. return true;
  283. }
  284. }
  285.  
  286. /**
  287. * Moderate a message.
  288. *
  289. * @author Marten van Urk .: ComSi :. m.van.urk@comsi.nl .: www.comsi.nl :.
  290. * @return Boolean True when the message is moderated succesfully, false when the message can't be moderated.
  291. */
  292. public function modMessage() {
  293. if (is_int($this->message_id)) {
  294. $sQuery = "UPDATE messages SET message = '" .htmlspecialchars($this->message, ENT_QUOTES). "',
  295. name = '" .htmlspecialchars($this->name, ENT_QUOTES). "',
  296. email = '" .htmlspecialchars($this->email, ENT_QUOTES). "'
  297. WHERE
  298. message_id = " . $this->message_id;
  299.  
  300. /**
  301. * Try to execute the query. When a error occurs, the error message will be stored in $this->error
  302. */
  303. try {
  304. $rResult = $this->db->query($sQuery);
  305. return true;
  306. } catch(PDOException $e) {
  307. $this->error = 'Can\'t update the message:' . $e->getMessage();
  308. return false;
  309. }
  310. } else {
  311. $this->error = 'Can\'t update the message: Message_id is not set or not set properly';
  312. return false;
  313. }
  314. }
  315.  
  316. /**
  317. * Delete a message
  318. *
  319. * @author Marten van Urk .: ComSi :. m.van.urk@comsi.nl .: www.comsi.nl :.
  320. * @return Boolean True when the message is succesfull deleted, false when an error occurs.
  321. */
  322. public function delMessage() {
  323. if (is_int($this->message_id)) {
  324. $sQuery = "DELETE FROM messages WHERE message_id = " . $this->message_id;
  325. try {
  326. $rResult = $this->db->query($sQuery);
  327. return true;
  328. } catch (PDOException $e) {
  329. $this->error = 'Can\'t delete the message: ' . $e->getMessage();
  330. return false;
  331. }
  332. } else {
  333. $this->error = 'Can\'t delete the message: Message_id is not set or not set properly';
  334. return false;
  335. }
  336. }
  337. }
  338. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (7)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.038s