/wildflower/models/comment.php

https://github.com/purushoth85/wildflower · PHP · 115 lines · 80 code · 17 blank · 18 comment · 10 complexity · 4e1a8364403b744eba317e0709c50dfe MD5 · raw file

  1. <?php
  2. class Comment extends AppModel {
  3. public $actsAs = array('Containable');
  4. public $belongsTo = array(
  5. 'Post' => array(
  6. 'className' => 'Post',
  7. 'foreignKey' => 'post_id',
  8. 'counterCache' => true
  9. )
  10. );
  11. /** @var bool Do a spam check before each save? **/
  12. public $spamCheck = false;
  13. public $validate = array(
  14. 'name' => VALID_NOT_EMPTY,
  15. 'email' => array('rule' => 'email', 'message' => 'Please enter a valid email address'),
  16. 'url' => array('rule' => 'url', 'message' => 'Please enter a valid URL', 'allowEmpty' => true),
  17. 'content' => VALID_NOT_EMPTY
  18. );
  19. function beforeSave() {
  20. parent::beforeSave();
  21. if (!isset($this->data[$this->name]['spam'])) {
  22. $this->data[$this->name]['spam'] = 0;
  23. }
  24. if ($this->spamCheck) {
  25. // Reset spamCheck for another save
  26. $this->spamCheck = false;
  27. if ($this->isSpam($this->data)) {
  28. $this->data[$this->name]['spam'] = 1;
  29. }
  30. }
  31. return true;
  32. }
  33. function beforeValidate() {
  34. // Some tiny name and content fields sanitization
  35. if (isset($this->data[$this->name]['name'])) {
  36. $this->data[$this->name]['name'] = trim(strip_tags($this->data[$this->name]['name']));
  37. }
  38. if (isset($this->data[$this->name]['content'])) {
  39. $this->data[$this->name]['content'] = trim($this->data[$this->name]['content']);
  40. }
  41. // Generate full url with http:// prefix
  42. if (isset($this->data[$this->name]['url']) && !empty($this->data[$this->name]['url'])) {
  43. $this->data[$this->name]['url'] = trim($this->data[$this->name]['url']);
  44. $httpPrefix = 'http://';
  45. if (strpos($this->data[$this->name]['url'], $httpPrefix) !== 0) {
  46. $this->data[$this->name]['url'] = $httpPrefix . $this->data[$this->name]['url'];
  47. }
  48. }
  49. }
  50. /**
  51. * Use Akismet to check comment data for spam
  52. *
  53. * @param array $data
  54. * @return bool
  55. */
  56. function isSpam(&$data) {
  57. $apiKey = Configure::read('Wildflower.settings.wordpress_api_key');
  58. if (empty($apiKey)) {
  59. return false;
  60. }
  61. try {
  62. App::import('Vendor', 'akismet');
  63. $siteUrl = Configure::read('Wildflower.fullSiteUrl');
  64. $akismet = new Akismet($siteUrl, $apiKey);
  65. $akismet->setCommentAuthor($data[$this->name]['name']);
  66. $akismet->setCommentAuthorEmail($data[$this->name]['email']);
  67. $akismet->setCommentAuthorURL($data[$this->name]['url']);
  68. $akismet->setCommentContent($data[$this->name]['content']);
  69. $akismet->setPermalink($data['Post']['permalink']);
  70. if ($akismet->isCommentSpam()) {
  71. return true;
  72. }
  73. } catch(Exception $e) {
  74. trigger_error('Akismet not reachable: ' . $e->message);
  75. }
  76. return false;
  77. }
  78. function approve() {
  79. return $this->saveField('approved', 1);
  80. }
  81. function unapprove() {
  82. return $this->saveField('approved', 0);
  83. }
  84. /**
  85. * Mark current comment as spam
  86. *
  87. */
  88. function spam() {
  89. return $this->saveField('spam', 1);
  90. }
  91. /**
  92. * Mark current comment as not spam
  93. *
  94. */
  95. function unspam() {
  96. return $this->saveField('spam', 0);
  97. }
  98. }