/source/data_layer/xml/comments.class.php

https://github.com/dotted/JibberBook · PHP · 222 lines · 115 code · 15 blank · 92 comment · 13 complexity · 51e7dcc1edab7f23d034223faa84c574 MD5 · raw file

  1. <?php
  2. /*
  3. Class: Comments
  4. Used to interface with the storage mechanism.
  5. This instance supports XML.
  6. */
  7. class Comments extends DataLayer {
  8. private $filename;
  9. private $handle;
  10. /*
  11. Function: __construct
  12. Sets the filename, gets a file resource handle, and locks the XML file.
  13. */
  14. public function __construct() {
  15. $this->filename = realpath(dirname(__FILE__) . '/' . JB_XML_FILENAME);
  16. $this->handle = @fopen($this->filename, 'r+') or die('<p><strong>Error:</strong> Could not open XML file. Make sure your server has correct permissions.</p>');
  17. flock($this->handle, LOCK_EX);
  18. }
  19. public function __destruct() {
  20. @flock($this->handle, LOCK_UN);
  21. @fclose($this->handle);
  22. }
  23. /*
  24. Function: getContents
  25. Gets contents from the XML file.
  26. Returns:
  27. String
  28. */
  29. private function getContents() {
  30. $contents = fread($this->handle, filesize($this->filename)) or die('Can\'t read file.');
  31. fseek($this->handle, 0);
  32. return (string) $contents;
  33. }
  34. /*
  35. Function: putContents
  36. Writes to the XML file.
  37. Parameters:
  38. $contents - string to write to the XML file.
  39. */
  40. private function putContents($contents) {
  41. if (strlen($contents) > 25) {
  42. ftruncate($this->handle, 0);
  43. fwrite($this->handle, $contents) or die('Could not write to file.');
  44. }
  45. }
  46. /*
  47. Function: deleteComment
  48. Deletes a comment given the comment ID.
  49. Parameters:
  50. id - comment ID
  51. */
  52. public function deleteComment($id) {
  53. $xml = new DOMDocument();
  54. $xml->validateOnParse=true;
  55. $xml->loadXML($this->getContents(true));
  56. $message = $xml->getElementById($id);
  57. $parentNode = $message->parentNode;
  58. $parentNode->removeChild($message);
  59. $this->putContents($xml->saveXML());
  60. }
  61. /*
  62. Function: deleteSpam
  63. Deletes all comments classified as spam.
  64. */
  65. public function deleteSpam() {
  66. $xml = new DOMDocument();
  67. $xml->validateOnParse=true;
  68. $xml->loadXML($this->getContents(true));
  69. $xpath = new DOMXPath($xml);
  70. $spams = $xpath->query("/messages/message[spam=1]");
  71. foreach ($spams as $spam){
  72. $parentNode = $spam->parentNode;
  73. $parentNode->removeChild($spam);
  74. }
  75. $this->putContents($xml->saveXML());
  76. }
  77. /*
  78. Function: addComment
  79. Adds a comment.
  80. Parameters:
  81. $data - associative array of data, must contain 'name', 'website', 'comment', 'date', 'user_ip', 'user_agent', and 'spam'.
  82. Returns:
  83. ID of the new comment.
  84. */
  85. public function addComment($data) {
  86. $xml = new SimpleXMLElement($this->getContents(true));
  87. $message = $xml->addChild('message');
  88. $id = $this->generateID();
  89. $message->addAttribute('mID', $id);
  90. foreach ($data as $key => $value) {
  91. $message->addChild($key, htmlspecialchars($value, ENT_QUOTES));
  92. }
  93. $this->putContents($xml->asXML());
  94. return $id;
  95. }
  96. /*
  97. Function: reclassifyComment
  98. Marks comment as spam or not spam given its current classification.
  99. Parameters:
  100. $id - comment ID
  101. */
  102. public function reclassifyComment($id) {
  103. $xml = new SimpleXMLElement($this->getContents(true));
  104. $message = $xml->xpath("/messages/message[@mID='$id']");
  105. $message = $message[0];
  106. if ((int) $message->spam) {
  107. $new_type = 'ham';
  108. $new_value = 0;
  109. } else {
  110. $new_type = 'spam';
  111. $new_value = 1;
  112. }
  113. $message->spam = $new_value;
  114. $this->notifyAkismet($message, $new_type);
  115. $this->putContents($xml->asXML());
  116. }
  117. /*
  118. Function: getCount
  119. Gets the number of the specified type of comments.
  120. Parameters:
  121. $filter - type of comments, 0 for ham, 1 for spam
  122. Returns:
  123. Number
  124. */
  125. public function getCount($filter) {
  126. $xml = new SimpleXMLElement($this->getContents());
  127. return count($xml->xpath("/messages/message[spam=$filter]"));
  128. }
  129. /*
  130. Function: getComments
  131. Gets all comments of specified type.
  132. Parameters:
  133. $filter - type of comment, 1 for spam, 0 for ham
  134. $limit - single value will get last [value] comments, array['upper'] and array['lower'] will get a range
  135. Returns:
  136. Multi-dimensional array
  137. */
  138. public function getComments($filter, $limit = null) {
  139. $xml = new SimpleXMLElement($this->getContents());
  140. $comments = $xml->xpath("//message[spam=$filter]");
  141. if (isset($limit)) {
  142. if (is_array($limit)) {
  143. if ($limit['lower'] < 0) $limit['lower'] = 0;
  144. $show = abs($limit['lower'] - $limit['upper']);
  145. $comments = array_slice($comments, $limit['lower'], $show);
  146. } else {
  147. $end = count($comments) - $limit;
  148. $end = ($end < 0) ? 0 : $end;
  149. $comments = array_slice($comments, $end);
  150. }
  151. }
  152. $data = array();
  153. foreach ($comments as $key => $message) {
  154. $data[$key]['name'] = (string)$message->name;
  155. $data[$key]['website'] = (string)$message->website;
  156. $data[$key]['comment'] = (string)$message->comment;
  157. $data[$key]['date'] = (string)$message->date;
  158. $data[$key]['id'] = (string)$message['mID'];
  159. }
  160. return array_reverse($data);
  161. }
  162. /*
  163. Function: notifyAkismet
  164. Called when a comment is reclassified.
  165. Parameters:
  166. $obj - comment array
  167. $type - new type, 'spam' or 'ham'
  168. */
  169. protected function notifyAkismet($obj, $type) {
  170. if (JB_AKISMET_KEY != '') {
  171. if ((string) $obj->user_ip != '' && (string) $obj->user_agent != '') {
  172. $vars = array();
  173. $vars['user_ip'] = (string) $obj->user_ip;
  174. $vars['user_agent'] = (string) $obj->user_agent;
  175. $vars['comment_content'] = (string) $obj->comment;
  176. $vars['comment_author'] = (string) $obj->name;
  177. $vars['comment_author_url'] = (string) $obj->website;
  178. $akismet = new MicroAkismet(JB_AKISMET_KEY, JB_GUESTBOOK_URL, 'JibberBook/' . JB_VERSION . ' | microakismet/' . JB_MA_VERSION);
  179. $akismet->$type($vars);
  180. }
  181. }
  182. }
  183. }
  184. ?>