-
Notifications
You must be signed in to change notification settings - Fork 4
/
commentable.php
369 lines (337 loc) · 13.2 KB
/
commentable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**
* Commentable Model Behavior
*
* Allows you to attach a comment to any model in your application
* Moderates/Validates comments to check for spam.
* Validates comments based on a point system.High points is an automatic approval,
* where as low points is marked as spam or deleted.
* Based on Jonathan Snooks outline.
*
* @filesource
* @copyright Stoop Dev
* @link http://github.com/josegonzalez/cakephp-commentable-behavior
* @package commentable
* @subpackage commentable.models.behaviors
* @version 0.3
* @modifiedby Jose Diaz-Gonzalez
* @lastmodified October 4th, 2009
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
**/
class CommentableBehavior extends ModelBehavior {
/**
* Settings initialized with the behavior
* @access public
* @var array
*/
var $defaults = array(
'class' => 'Comment', // name of Comment model
'foreign_key' => 'foreign_id',// foreign key of Comment model
'counter_cache' => true,
'dependent' => true, // model dependency
'conditions' => array(), // conditions for find method on Comment model
'auto_bind' => true, // automatically bind the model to the User model (default true),
'sanitize' => true, // whether to sanitize incoming comments
'column_author' => 'name', // Column name for the authors name
'column_content' => 'body', // Column name for the comments body
'column_class' => 'class', // Column name for the foreign model
'column_email' => 'email', // Column name for the authors email
'column_website' => 'website', // Column name for the authors website
'column_foreign_id' => 'foreign_id',// Column name of the foreign id that links to the article/entry/etc
'column_status' => 'status', // Column name for automatic rating
'column_points' => 'points', // Column name for accrued points
'blacklist_keywords'=> array('levitra', 'viagra', 'casino', 'sex', 'loan', 'finance',
'slots', 'debt', 'free', 'interesting', 'sorry', 'cool'),
// List of blacklisted words within text blocks
'blacklist_words' => array('.html', '.info', '?', '&', '.de', '.pl', '.cn'),
// List of blacklisted words within URLs
'deletion' => -10 // How many points till the comment is deleted (negative)
);
/**
* Contain settings indexed by model name.
*
* @var array
* @access private
*/
var $__settings = array();
/**
* Initiate behaviour for the model using settings.
*
* @param object $Model Model using the behaviour
* @param array $settings Settings to override for model.
* @access public
*/
function setup(&$model, $settings = array()) {
$default = $this->defaults;
$default['conditions'] = array('Comment.class' => $model->alias);
if (!isset($this->__settings[$model->alias])) {
$this->__settings[$model->alias] = $default;
}
$this->__settings[$model->alias] = array_merge($this->__settings[$model->alias], ife(is_array($settings), $settings, array()));
//handles model binding to the model
//according to the auto_bind settings (default true)
if ($this->__settings[$model->alias]['auto_bind']) {
$hasManyComment = array(
'Comment' => array(
'className' => $this->__settings[$model->alias]['class'],
'foreignKey' => $this->__settings[$model->alias]['foreign_key'],
'dependent' => $this->__settings[$model->alias]['dependent'],
'conditions' =>$this->__settings[$model->alias]['conditions']));
$commentBelongsTo = array(
$model->alias => array(
'className' => $model->alias,
'foreignKey' => $this->__settings[$model->alias]['foreign_key'],
'counterCache' => $this->__settings[$model->alias]['counter_cache']
)
);
$model->bindModel(array('hasMany' => $hasManyComment), false);
$model->Comment->bindModel(array('belongsTo' => $commentBelongsTo), false);
}
}
function createComment(&$model, $id, $data = array()){
if (!empty($data[$this->__settings[$model->alias]['class']]) ) {
unset($data[$model->alias]);
$model->Comment->validate = array(
$this->__settings[$model->alias]['column_author'] => array(
'notempty' => array(
'rule' => array('notempty')
)
),
$this->__settings[$model->alias]['column_content'] => array(
'notempty' => array(
'rule' => array('notempty')
)
),
$this->__settings[$model->alias]['column_email'] => array(
'notempty' => array(
'rule' => array('notempty')
),
'email' => array(
'rule' => array('email'),
'message' => 'Please enter a valid email address'
)
),
$this->__settings[$model->alias]['column_class'] => array(
'notempty' => array(
'rule' => array('notempty')
)
),
$this->__settings[$model->alias]['column_foreign_id'] => array(
'notempty' => array(
'rule' => array('notempty')
)
),
$this->__settings[$model->alias]['column_status'] => array(
'notempty' => array(
'rule' => array('notempty')
)
),
$this->__settings[$model->alias]['column_points'] => array(
'notempty' => array(
'rule' => array('notempty')
),
'numeric' => array(
'rule' => array('numeric'),
)
)
);
$data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_class']] = $model->alias;
$data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_foreign_id']] = $id;
$data[$this->__settings[$model->alias]['class']] = $this->_rateComment($model, $data['Comment']);
if ($this->__settings[$model->alias]['sanitize']) {
App::import('Sanitize');
$data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_author']] =
Sanitize::clean($data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_author']]);
$data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_email']] =
Sanitize::clean($data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_email']]);
$data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_content']] =
Sanitize::clean($data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_content']]);
} else {
$data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_author']] =
$data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_author']];
$data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_email']] =
$data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_email']];
$data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_content']] =
$data[$this->__settings[$model->alias]['class']][$this->__settings[$model->alias]['column_content']];
}
if ($this->_checkForEmptyVal($data[$this->__settings[$model->alias]['class']]) == false) {
$model->Comment->create();
if ($model->Comment->save($data)) {
return true;
}
}
}
return false;
}
function deleteComment(&$model, $id){
if(is_null($id) || $id == "")
return false;
$model->Comment->id = $id;
if($model->Comment->saveField('status', 'deleted')) {
return true;
}
return false;
}
function getComments(&$model, $options = array()){
$options = array_merge(array('id' => $model->id, 'options' => array()), $options);
$parameters = array();
if (isset($options['id']) && is_numeric($options['id'])) {
$settings = $this->__settings[$model->alias];
$parameters = array_merge_recursive(
array('conditions' =>
array(
$settings['class'] .'.'. $settings['column_class'] => $model->alias,
$settings['class'] .'.'. $settings['foreign_key'] => $options['id'],
$settings['class'] .'.'. $settings['column_status'] => 'approved')),
$options['options']
);
}
$parameters = (isset($parameters) && !$this->_checkForEmptyVal($parameters)) ? $parameters : array();
return $model->Comment->find('all', $parameters);
}
function _rateComment($model, $data) {
if (!empty($data)) {
$points = $this->_rateLinks($model, $data);
$points += $this->_rateLength($model, $data);
$points += $this->_rateEmail($model, $data);
$points += $this->_rateKeywords($model, $data);
$points += $this->_rateStartingWord($model, $data);
$points += $this->_rateAuthorName($model, $data);
$points += $this->_rateByPreviousComment($model, $data);
$points += $this->_rateBody($model, $data);
$data[$this->__settings[$model->alias]['column_points']] = $points;
if ($points >= 1) {
$data[$this->__settings[$model->alias]['column_status']] = 'approved';
} else if ($points == 0) {
$data[$this->__settings[$model->alias]['column_status']] = 'pending';
} else if ($points <= $this->__settings[$model->alias]['deletion']) {
$data[$this->__settings[$model->alias]['column_status']] = 'delete';
} else {
$data[$this->__settings[$model->alias]['column_status']] = 'spam';
}
} else {
$data[$this->__settings[$model->alias]['column_points']] = "-100";
$data[$this->__settings[$model->alias]['column_status']] = 'delete';
}
return $data;
}
function _rateLinks($model, $data) {
$links = preg_match_all(
"#(^|[\n ])(?:(?:http|ftp|irc)s?:\/\/|www.)(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,4}(?:[-a-zA-Z0-9._\/&=+%?;\#]+)#is",
$data[$this->__settings[$model->alias]['column_content']], $matches);
$links = $matches[0];
$totalLinks = count($links);
$length = mb_strlen($data[$this->__settings[$model->alias]['column_content']]);
// How many links are in the body
// -1 per link if over 2, otherwise +2 if less than 2
$points = ($totalLinks > 2) ? $totalLinks * -1 : 2;
// URLs that have certain words or characters in them
// -1 per blacklisted word
// URL length
// -1 if more then 30 chars
foreach ($links as $link) {
foreach ($this->__settings[$model->alias]['blacklist_words'] as $word) {
$points = (stripos($link, $word) !== false) ? $points - 1 : $points;
}
foreach ($this->__settings[$model->alias]['blacklist_keywords'] as $keyword) {
$points = (stripos($link, $keyword) !== false) ? $points - 1 : $points;
}
$points = (strlen($link) >= 30) ? $points - 1 : $points;
}
return $points;
}
function _rateLength($model, $data) {
// How long is the body
// +2 if more then 20 chars and no links, -1 if less then 20
$length = mb_strlen($data[$this->__settings[$model->alias]['column_content']]);
if ($length >= 20 && $totalLinks <= 0) {
return 2;
} elseif ($length >= 20 && $totalLinks == 1) {
return 1;
} elseif ($length < 20) {
return -1;
}
}
function _rateEmail($model, $data) {
$points = 0;
// Number of previous comments from email
// +1 per approved, -1 per spam
$comments = $model->Comment->find('all', array(
'fields' => array('Comment.id', 'Comment.status'),
'conditions' => array(
'Comment.' . $this->__settings[$model->alias]['column_email'] => $data[$this->__settings[$model->alias]['column_email']]),
'recursive' => -1,
'contain' => false
));
if (!empty($comments)) {
foreach ($comments as $comment) {
if ($comment['Comment']['status'] == 'spam') {
--$points;
} elseif ($comment['Comment']['status'] == 'approved') {
++$points;
}
}
}
return $points;
}
function _rateKeywords($model, $data) {
$points = 0;
// Keyword search
// -1 per blacklisted keyword
foreach ($this->__settings[$model->alias]['blacklist_keywords'] as $keyword) {
if (stripos($data[$this->__settings[$model->alias]['column_content']], $keyword) !== false) {
--$points;
}
}
return $points;
}
function _rateStartingWord($model, $data) {
// Body starts with...
// -10 points
$firstWord = mb_substr(
$data[$this->__settings[$model->alias]['column_content']],
0,
stripos($data[$this->__settings[$model->alias]['column_content']], ' '));
return (in_array(mb_strtolower($firstWord), $this->__settings[$model->alias]['blacklist_keywords'])) ? -10 : 0;
}
function _rateAuthorName($model, $data) {
// Author name has http:// in it
// -2 points
if (stripos($data[$this->__settings[$model->alias]['column_author']], 'http://') !== false) {
return -2;
}
return 0;
}
function _rateByPreviousComment($model, $data) {
// Body used in previous comment
// -1 per exact comment
$previousComments = $model->Comment->find('count', array(
'conditions' => array(
'Comment.' . $this->__settings[$model->alias]['column_content'] => $data[$this->__settings[$model->alias]['column_content']]),
'recursive' => -1,
'contain' => false
));
return ($previousComments > 0) ? -$previousComments : 0;
}
function _rateBody($model, $data) {
// Random character match
// -1 point per 5 consecutive consonants
$consonants = preg_match_all(
'/[^aAeEiIoOuU\s]{5,}+/i',
$data[$this->__settings[$model->alias]['column_content']],
$matches);
$totalConsonants = count($matches[0]);
return ($totalConsonants > 0) ? -$totalConsonants : 0;
}
function _checkForEmptyVal($array) {
$isEmpty = 0;
foreach ($array as $key => $item) {
if (is_numeric($item)) {
} elseif (empty($item)) {
$isEmpty++;
}
}
return ($isEmpty > 0) ? true : false;
}
}
?>