1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2011 Piwigo Team http://piwigo.org | |
---|
6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
8 | // +-----------------------------------------------------------------------+ |
---|
9 | // | This program is free software; you can redistribute it and/or modify | |
---|
10 | // | it under the terms of the GNU General Public License as published by | |
---|
11 | // | the Free Software Foundation | |
---|
12 | // | | |
---|
13 | // | This program is distributed in the hope that it will be useful, but | |
---|
14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
16 | // | General Public License for more details. | |
---|
17 | // | | |
---|
18 | // | You should have received a copy of the GNU General Public License | |
---|
19 | // | along with this program; if not, write to the Free Software | |
---|
20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
21 | // | USA. | |
---|
22 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | //returns string action to perform on a new comment: validate, moderate, reject |
---|
25 | function user_comment_check($action, $comment) |
---|
26 | { |
---|
27 | global $conf,$user; |
---|
28 | |
---|
29 | if ($action=='reject') |
---|
30 | return $action; |
---|
31 | |
---|
32 | $my_action = $conf['comment_spam_reject'] ? 'reject':'moderate'; |
---|
33 | |
---|
34 | if ($action==$my_action) |
---|
35 | return $action; |
---|
36 | |
---|
37 | // we do here only BASIC spam check (plugins can do more) |
---|
38 | if ( !is_a_guest() ) |
---|
39 | return $action; |
---|
40 | |
---|
41 | $link_count = preg_match_all( '/https?:\/\//', |
---|
42 | $comment['content'], $matches); |
---|
43 | |
---|
44 | if ( strpos($comment['author'], 'http://')!==false ) |
---|
45 | { |
---|
46 | $link_count++; |
---|
47 | } |
---|
48 | |
---|
49 | if ( $link_count>$conf['comment_spam_max_links'] ) |
---|
50 | return $my_action; |
---|
51 | |
---|
52 | return $action; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | add_event_handler('user_comment_check', 'user_comment_check', |
---|
57 | EVENT_HANDLER_PRIORITY_NEUTRAL, 2); |
---|
58 | |
---|
59 | /** |
---|
60 | * Tries to insert a user comment in the database and returns one of : |
---|
61 | * validate, moderate, reject |
---|
62 | * @param array comm contains author, content, image_id |
---|
63 | * @param string key secret key sent back to the browser |
---|
64 | * @param array infos out array of messages |
---|
65 | */ |
---|
66 | function insert_user_comment( &$comm, $key, &$infos ) |
---|
67 | { |
---|
68 | global $conf, $user; |
---|
69 | |
---|
70 | $comm = array_merge( $comm, |
---|
71 | array( |
---|
72 | 'ip' => $_SERVER['REMOTE_ADDR'], |
---|
73 | 'agent' => $_SERVER['HTTP_USER_AGENT'] |
---|
74 | ) |
---|
75 | ); |
---|
76 | |
---|
77 | $infos = array(); |
---|
78 | if (!$conf['comments_validation'] or is_admin()) |
---|
79 | { |
---|
80 | $comment_action='validate'; //one of validate, moderate, reject |
---|
81 | } |
---|
82 | else |
---|
83 | { |
---|
84 | $comment_action='moderate'; //one of validate, moderate, reject |
---|
85 | } |
---|
86 | |
---|
87 | // display author field if the user status is guest or generic |
---|
88 | if (!is_classic_user()) |
---|
89 | { |
---|
90 | if ( empty($comm['author']) ) |
---|
91 | { |
---|
92 | $comm['author'] = 'guest'; |
---|
93 | } |
---|
94 | $comm['author_id'] = $conf['guest_id']; |
---|
95 | // if a guest try to use the name of an already existing user, he must be |
---|
96 | // rejected |
---|
97 | if ( $comm['author'] != 'guest' ) |
---|
98 | { |
---|
99 | $query = ' |
---|
100 | SELECT COUNT(*) AS user_exists |
---|
101 | FROM '.USERS_TABLE.' |
---|
102 | WHERE '.$conf['user_fields']['username']." = '".addslashes($comm['author'])."'"; |
---|
103 | $row = pwg_db_fetch_assoc( pwg_query( $query ) ); |
---|
104 | if ( $row['user_exists'] == 1 ) |
---|
105 | { |
---|
106 | array_push($infos, l10n('This login is already used by another user') ); |
---|
107 | $comment_action='reject'; |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | else |
---|
112 | { |
---|
113 | $comm['author'] = addslashes($user['username']); |
---|
114 | $comm['author_id'] = $user['id']; |
---|
115 | } |
---|
116 | |
---|
117 | if ( empty($comm['content']) ) |
---|
118 | { // empty comment content |
---|
119 | $comment_action='reject'; |
---|
120 | } |
---|
121 | |
---|
122 | if ( !verify_ephemeral_key(@$key, $comm['image_id']) ) |
---|
123 | { |
---|
124 | $comment_action='reject'; |
---|
125 | } |
---|
126 | |
---|
127 | if ($comment_action!='reject' and $conf['anti-flood_time']>0 and !is_admin()) |
---|
128 | { // anti-flood system |
---|
129 | $reference_date = pwg_db_get_flood_period_expression($conf['anti-flood_time']); |
---|
130 | |
---|
131 | $query = ' |
---|
132 | SELECT count(1) FROM '.COMMENTS_TABLE.' |
---|
133 | WHERE date > '.$reference_date.' |
---|
134 | AND author_id = '.$comm['author_id']; |
---|
135 | list($counter) = pwg_db_fetch_row(pwg_query($query)); |
---|
136 | if ( $counter > 0 ) |
---|
137 | { |
---|
138 | array_push( $infos, l10n('Anti-flood system : please wait for a moment before trying to post another comment') ); |
---|
139 | $comment_action='reject'; |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | // perform more spam check |
---|
144 | $comment_action = trigger_event('user_comment_check', |
---|
145 | $comment_action, $comm |
---|
146 | ); |
---|
147 | |
---|
148 | if ( $comment_action!='reject' ) |
---|
149 | { |
---|
150 | $query = ' |
---|
151 | INSERT INTO '.COMMENTS_TABLE.' |
---|
152 | (author, author_id, content, date, validated, validation_date, image_id) |
---|
153 | VALUES ( |
---|
154 | \''.$comm['author'].'\', |
---|
155 | '.$comm['author_id'].', |
---|
156 | \''.$comm['content'].'\', |
---|
157 | NOW(), |
---|
158 | \''.($comment_action=='validate' ? 'true':'false').'\', |
---|
159 | '.($comment_action=='validate' ? 'NOW()':'NULL').', |
---|
160 | '.$comm['image_id'].' |
---|
161 | ) |
---|
162 | '; |
---|
163 | |
---|
164 | pwg_query($query); |
---|
165 | |
---|
166 | $comm['id'] = pwg_db_insert_id(COMMENTS_TABLE); |
---|
167 | |
---|
168 | if ($conf['email_admin_on_comment'] |
---|
169 | or ($conf['email_admin_on_comment_validation'] and 'moderate' == $comment_action)) |
---|
170 | { |
---|
171 | include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php'); |
---|
172 | |
---|
173 | $comment_url = get_absolute_root_url().'comments.php?comment_id='.$comm['id']; |
---|
174 | |
---|
175 | $keyargs_content = array |
---|
176 | ( |
---|
177 | get_l10n_args('Author: %s', stripslashes($comm['author']) ), |
---|
178 | get_l10n_args('Comment: %s', stripslashes($comm['content']) ), |
---|
179 | get_l10n_args('', ''), |
---|
180 | get_l10n_args('Manage this user comment: %s', $comment_url) |
---|
181 | ); |
---|
182 | |
---|
183 | if ('moderate' == $comment_action) |
---|
184 | { |
---|
185 | $keyargs_content[] = get_l10n_args('', ''); |
---|
186 | $keyargs_content[] = get_l10n_args('(!) This comment requires validation', ''); |
---|
187 | } |
---|
188 | |
---|
189 | pwg_mail_notification_admins |
---|
190 | ( |
---|
191 | get_l10n_args('Comment by %s', stripslashes($comm['author']) ), |
---|
192 | $keyargs_content |
---|
193 | ); |
---|
194 | } |
---|
195 | } |
---|
196 | return $comment_action; |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * Tries to delete a user comment in the database |
---|
201 | * only admin can delete all comments |
---|
202 | * other users can delete their own comments |
---|
203 | * so to avoid a new sql request we add author in where clause |
---|
204 | * |
---|
205 | * @param comment_id |
---|
206 | */ |
---|
207 | function delete_user_comment($comment_id) { |
---|
208 | $user_where_clause = ''; |
---|
209 | if (!is_admin()) |
---|
210 | { |
---|
211 | $user_where_clause = ' AND author_id = \''.$GLOBALS['user']['id'].'\''; |
---|
212 | } |
---|
213 | $query = ' |
---|
214 | DELETE FROM '.COMMENTS_TABLE.' |
---|
215 | WHERE id = '.$comment_id. |
---|
216 | $user_where_clause.' |
---|
217 | ;'; |
---|
218 | $result = pwg_query($query); |
---|
219 | if ($result) { |
---|
220 | email_admin('delete', |
---|
221 | array('author' => $GLOBALS['user']['username'], |
---|
222 | 'comment_id' => $comment_id |
---|
223 | )); |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | * Tries to update a user comment in the database |
---|
229 | * only admin can update all comments |
---|
230 | * users can edit their own comments if admin allow them |
---|
231 | * so to avoid a new sql request we add author in where clause |
---|
232 | * |
---|
233 | * @param comment_id |
---|
234 | * @param post_key |
---|
235 | * @param content |
---|
236 | */ |
---|
237 | |
---|
238 | function update_user_comment($comment, $post_key) |
---|
239 | { |
---|
240 | global $conf; |
---|
241 | |
---|
242 | $comment_action = 'validate'; |
---|
243 | |
---|
244 | if ( !verify_ephemeral_key($post_key, $comment['image_id']) ) |
---|
245 | { |
---|
246 | $comment_action='reject'; |
---|
247 | } |
---|
248 | elseif (!$conf['comments_validation'] or is_admin()) // should the updated comment must be validated |
---|
249 | { |
---|
250 | $comment_action='validate'; //one of validate, moderate, reject |
---|
251 | } |
---|
252 | else |
---|
253 | { |
---|
254 | $comment_action='moderate'; //one of validate, moderate, reject |
---|
255 | } |
---|
256 | |
---|
257 | // perform more spam check |
---|
258 | $comment_action = |
---|
259 | trigger_event('user_comment_check', |
---|
260 | $comment_action, |
---|
261 | array_merge($comment, |
---|
262 | array('author' => $GLOBALS['user']['username']) |
---|
263 | ) |
---|
264 | ); |
---|
265 | |
---|
266 | if ( $comment_action!='reject' ) |
---|
267 | { |
---|
268 | $user_where_clause = ''; |
---|
269 | if (!is_admin()) |
---|
270 | { |
---|
271 | $user_where_clause = ' AND author_id = \''. |
---|
272 | $GLOBALS['user']['id'].'\''; |
---|
273 | } |
---|
274 | |
---|
275 | $query = ' |
---|
276 | UPDATE '.COMMENTS_TABLE.' |
---|
277 | SET content = \''.$comment['content'].'\', |
---|
278 | validated = \''.($comment_action=='validate' ? 'true':'false').'\', |
---|
279 | validation_date = '.($comment_action=='validate' ? 'NOW()':'NULL').' |
---|
280 | WHERE id = '.$comment['comment_id']. |
---|
281 | $user_where_clause.' |
---|
282 | ;'; |
---|
283 | $result = pwg_query($query); |
---|
284 | |
---|
285 | // mail admin and ask to validate the comment |
---|
286 | if ($result and $conf['email_admin_on_comment_validation'] and 'moderate' == $comment_action) |
---|
287 | { |
---|
288 | include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php'); |
---|
289 | |
---|
290 | $comment_url = get_absolute_root_url().'comments.php?comment_id='.$comment['comment_id']; |
---|
291 | |
---|
292 | $keyargs_content = array |
---|
293 | ( |
---|
294 | get_l10n_args('Author: %s', stripslashes($GLOBALS['user']['username']) ), |
---|
295 | get_l10n_args('Comment: %s', stripslashes($comment['content']) ), |
---|
296 | get_l10n_args('', ''), |
---|
297 | get_l10n_args('Manage this user comment: %s', $comment_url), |
---|
298 | get_l10n_args('', ''), |
---|
299 | get_l10n_args('(!) This comment requires validation', ''), |
---|
300 | ); |
---|
301 | |
---|
302 | pwg_mail_notification_admins |
---|
303 | ( |
---|
304 | get_l10n_args('Comment by %s', stripslashes($GLOBALS['user']['username']) ), |
---|
305 | $keyargs_content |
---|
306 | ); |
---|
307 | } |
---|
308 | // just mail admin |
---|
309 | else if ($result) |
---|
310 | { |
---|
311 | email_admin('edit', array('author' => $GLOBALS['user']['username'], |
---|
312 | 'content' => stripslashes($comment['content'])) ); |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | return $comment_action; |
---|
317 | } |
---|
318 | |
---|
319 | function email_admin($action, $comment) |
---|
320 | { |
---|
321 | global $conf; |
---|
322 | |
---|
323 | if (!in_array($action, array('edit', 'delete')) |
---|
324 | or (($action=='edit') and !$conf['email_admin_on_comment_edition']) |
---|
325 | or (($action=='delete') and !$conf['email_admin_on_comment_deletion'])) |
---|
326 | { |
---|
327 | return; |
---|
328 | } |
---|
329 | |
---|
330 | include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php'); |
---|
331 | |
---|
332 | $keyargs_content = array(); |
---|
333 | $keyargs_content[] = get_l10n_args('Author: %s', $comment['author']); |
---|
334 | if ($action=='delete') |
---|
335 | { |
---|
336 | $keyargs_content[] = get_l10n_args('This author removed the comment with id %d', |
---|
337 | $comment['comment_id'] |
---|
338 | ); |
---|
339 | } |
---|
340 | else |
---|
341 | { |
---|
342 | $keyargs_content[] = get_l10n_args('This author modified following comment:', ''); |
---|
343 | $keyargs_content[] = get_l10n_args('Comment: %s', $comment['content']); |
---|
344 | } |
---|
345 | |
---|
346 | pwg_mail_notification_admins(get_l10n_args('Comment by %s', |
---|
347 | $comment['author']), |
---|
348 | $keyargs_content |
---|
349 | ); |
---|
350 | } |
---|
351 | |
---|
352 | function get_comment_author_id($comment_id, $die_on_error=true) |
---|
353 | { |
---|
354 | $query = ' |
---|
355 | SELECT |
---|
356 | author_id |
---|
357 | FROM '.COMMENTS_TABLE.' |
---|
358 | WHERE id = '.$comment_id.' |
---|
359 | ;'; |
---|
360 | $result = pwg_query($query); |
---|
361 | if (pwg_db_num_rows($result) == 0) |
---|
362 | { |
---|
363 | if ($die_on_error) |
---|
364 | { |
---|
365 | fatal_error('Unknown comment identifier'); |
---|
366 | } |
---|
367 | else |
---|
368 | { |
---|
369 | return false; |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | list($author_id) = pwg_db_fetch_row($result); |
---|
374 | |
---|
375 | return $author_id; |
---|
376 | } |
---|
377 | |
---|
378 | function validate_user_comment($comment_id) |
---|
379 | { |
---|
380 | $query = ' |
---|
381 | UPDATE '.COMMENTS_TABLE.' |
---|
382 | SET validated = \'true\' |
---|
383 | , validation_date = NOW() |
---|
384 | WHERE id = '.$comment_id.' |
---|
385 | ;'; |
---|
386 | pwg_query($query); |
---|
387 | } |
---|
388 | ?> |
---|