[1849] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[12922] | 5 | // | Copyright(C) 2008-2012 Piwigo Team http://piwigo.org | |
---|
[2297] | 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 | // +-----------------------------------------------------------------------+ |
---|
[1849] | 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) |
---|
[2029] | 38 | if ( !is_a_guest() ) |
---|
[1849] | 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 | } |
---|
[2155] | 48 | |
---|
[1849] | 49 | if ( $link_count>$conf['comment_spam_max_links'] ) |
---|
[13800] | 50 | { |
---|
| 51 | $_POST['cr'][] = 'links'; |
---|
[1849] | 52 | return $my_action; |
---|
[13800] | 53 | } |
---|
[1849] | 54 | return $action; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | add_event_handler('user_comment_check', 'user_comment_check', |
---|
| 59 | EVENT_HANDLER_PRIORITY_NEUTRAL, 2); |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * Tries to insert a user comment in the database and returns one of : |
---|
| 63 | * validate, moderate, reject |
---|
| 64 | * @param array comm contains author, content, image_id |
---|
| 65 | * @param string key secret key sent back to the browser |
---|
| 66 | * @param array infos out array of messages |
---|
| 67 | */ |
---|
| 68 | function insert_user_comment( &$comm, $key, &$infos ) |
---|
| 69 | { |
---|
| 70 | global $conf, $user; |
---|
[2155] | 71 | |
---|
| 72 | $comm = array_merge( $comm, |
---|
[1849] | 73 | array( |
---|
| 74 | 'ip' => $_SERVER['REMOTE_ADDR'], |
---|
| 75 | 'agent' => $_SERVER['HTTP_USER_AGENT'] |
---|
| 76 | ) |
---|
| 77 | ); |
---|
| 78 | |
---|
| 79 | $infos = array(); |
---|
| 80 | if (!$conf['comments_validation'] or is_admin()) |
---|
| 81 | { |
---|
| 82 | $comment_action='validate'; //one of validate, moderate, reject |
---|
| 83 | } |
---|
| 84 | else |
---|
| 85 | { |
---|
| 86 | $comment_action='moderate'; //one of validate, moderate, reject |
---|
| 87 | } |
---|
| 88 | |
---|
[2029] | 89 | // display author field if the user status is guest or generic |
---|
| 90 | if (!is_classic_user()) |
---|
[1849] | 91 | { |
---|
| 92 | if ( empty($comm['author']) ) |
---|
| 93 | { |
---|
[18164] | 94 | if ($conf['comments_author_mandatory']) |
---|
| 95 | { |
---|
| 96 | array_push($infos, l10n('Username is mandatory') ); |
---|
| 97 | $comment_action='reject'; |
---|
| 98 | } |
---|
[1849] | 99 | $comm['author'] = 'guest'; |
---|
| 100 | } |
---|
[3450] | 101 | $comm['author_id'] = $conf['guest_id']; |
---|
[1849] | 102 | // if a guest try to use the name of an already existing user, he must be |
---|
| 103 | // rejected |
---|
| 104 | if ( $comm['author'] != 'guest' ) |
---|
| 105 | { |
---|
| 106 | $query = ' |
---|
| 107 | SELECT COUNT(*) AS user_exists |
---|
| 108 | FROM '.USERS_TABLE.' |
---|
[4304] | 109 | WHERE '.$conf['user_fields']['username']." = '".addslashes($comm['author'])."'"; |
---|
[4325] | 110 | $row = pwg_db_fetch_assoc( pwg_query( $query ) ); |
---|
[1849] | 111 | if ( $row['user_exists'] == 1 ) |
---|
| 112 | { |
---|
[5021] | 113 | array_push($infos, l10n('This login is already used by another user') ); |
---|
[1849] | 114 | $comment_action='reject'; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | else |
---|
| 119 | { |
---|
[3600] | 120 | $comm['author'] = addslashes($user['username']); |
---|
[3450] | 121 | $comm['author_id'] = $user['id']; |
---|
[1849] | 122 | } |
---|
[3450] | 123 | |
---|
[1849] | 124 | if ( empty($comm['content']) ) |
---|
| 125 | { // empty comment content |
---|
| 126 | $comment_action='reject'; |
---|
| 127 | } |
---|
| 128 | |
---|
[7495] | 129 | if ( !verify_ephemeral_key(@$key, $comm['image_id']) ) |
---|
[1849] | 130 | { |
---|
| 131 | $comment_action='reject'; |
---|
[13800] | 132 | $_POST['cr'][] = 'key'; // rvelices: I use this outside to see how spam robots work |
---|
[1849] | 133 | } |
---|
[15983] | 134 | |
---|
[17351] | 135 | // website |
---|
[18164] | 136 | if (!empty($comm['website_url'])) |
---|
[17351] | 137 | { |
---|
[18164] | 138 | if (!preg_match('/^https?/i', $comm['website_url'])) |
---|
| 139 | { |
---|
| 140 | $comm['website_url'] = 'http://'.$comm['website_url']; |
---|
| 141 | } |
---|
| 142 | if (!url_check_format($comm['website_url'])) |
---|
| 143 | { |
---|
| 144 | array_push($infos, l10n('Your website URL is invalid')); |
---|
| 145 | $comment_action='reject'; |
---|
| 146 | } |
---|
[17351] | 147 | } |
---|
[18164] | 148 | |
---|
| 149 | // email |
---|
| 150 | if (empty($comm['email'])) |
---|
[17351] | 151 | { |
---|
[18164] | 152 | if (!empty($user['email'])) |
---|
| 153 | { |
---|
| 154 | $comm['email'] = $user['email']; |
---|
| 155 | } |
---|
| 156 | else if ($conf['comments_email_mandatory']) |
---|
| 157 | { |
---|
| 158 | array_push($infos, l10n('Email address is missing. Please specify an email address.') ); |
---|
| 159 | $comment_action='reject'; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | else if (!email_check_format($comm['email'])) |
---|
| 163 | { |
---|
| 164 | array_push($infos, l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)')); |
---|
[17351] | 165 | $comment_action='reject'; |
---|
| 166 | } |
---|
| 167 | |
---|
[15983] | 168 | // anonymous id = ip address |
---|
| 169 | $ip_components = explode('.', $comm['ip']); |
---|
| 170 | if (count($ip_components) > 3) |
---|
| 171 | { |
---|
| 172 | array_pop($ip_components); |
---|
| 173 | } |
---|
| 174 | $comm['anonymous_id'] = implode('.', $ip_components); |
---|
[2155] | 175 | |
---|
[7784] | 176 | if ($comment_action!='reject' and $conf['anti-flood_time']>0 and !is_admin()) |
---|
[1849] | 177 | { // anti-flood system |
---|
[6604] | 178 | $reference_date = pwg_db_get_flood_period_expression($conf['anti-flood_time']); |
---|
| 179 | |
---|
[1849] | 180 | $query = ' |
---|
[6604] | 181 | SELECT count(1) FROM '.COMMENTS_TABLE.' |
---|
| 182 | WHERE date > '.$reference_date.' |
---|
[3450] | 183 | AND author_id = '.$comm['author_id']; |
---|
[15983] | 184 | if (!is_classic_user()) |
---|
| 185 | { |
---|
| 186 | $query.= ' |
---|
| 187 | AND anonymous_id = "'.$comm['anonymous_id'].'"'; |
---|
| 188 | } |
---|
| 189 | $query.= ' |
---|
| 190 | ;'; |
---|
| 191 | |
---|
[6604] | 192 | list($counter) = pwg_db_fetch_row(pwg_query($query)); |
---|
| 193 | if ( $counter > 0 ) |
---|
[1849] | 194 | { |
---|
[5021] | 195 | array_push( $infos, l10n('Anti-flood system : please wait for a moment before trying to post another comment') ); |
---|
[1849] | 196 | $comment_action='reject'; |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | // perform more spam check |
---|
| 201 | $comment_action = trigger_event('user_comment_check', |
---|
| 202 | $comment_action, $comm |
---|
| 203 | ); |
---|
| 204 | |
---|
| 205 | if ( $comment_action!='reject' ) |
---|
| 206 | { |
---|
| 207 | $query = ' |
---|
| 208 | INSERT INTO '.COMMENTS_TABLE.' |
---|
[18164] | 209 | (author, author_id, anonymous_id, content, date, validated, validation_date, image_id, website_url, email) |
---|
[1849] | 210 | VALUES ( |
---|
[6589] | 211 | \''.$comm['author'].'\', |
---|
[3450] | 212 | '.$comm['author_id'].', |
---|
[15983] | 213 | \''.$comm['anonymous_id'].'\', |
---|
[6589] | 214 | \''.$comm['content'].'\', |
---|
[1849] | 215 | NOW(), |
---|
[6589] | 216 | \''.($comment_action=='validate' ? 'true':'false').'\', |
---|
[1849] | 217 | '.($comment_action=='validate' ? 'NOW()':'NULL').', |
---|
[17351] | 218 | '.$comm['image_id'].', |
---|
[18164] | 219 | '.(!empty($comm['website_url']) ? '\''.$comm['website_url'].'\'' : 'NULL').', |
---|
| 220 | '.(!empty($comm['email']) ? '\''.$comm['email'].'\'' : 'NULL').' |
---|
[1849] | 221 | ) |
---|
| 222 | '; |
---|
| 223 | |
---|
| 224 | pwg_query($query); |
---|
| 225 | |
---|
[4892] | 226 | $comm['id'] = pwg_db_insert_id(COMMENTS_TABLE); |
---|
[1849] | 227 | |
---|
[13800] | 228 | if ( ($conf['email_admin_on_comment'] && 'validate' == $comment_action) |
---|
[5195] | 229 | or ($conf['email_admin_on_comment_validation'] and 'moderate' == $comment_action)) |
---|
[1849] | 230 | { |
---|
| 231 | include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php'); |
---|
| 232 | |
---|
[5195] | 233 | $comment_url = get_absolute_root_url().'comments.php?comment_id='.$comm['id']; |
---|
[1849] | 234 | |
---|
[1908] | 235 | $keyargs_content = array |
---|
| 236 | ( |
---|
[3600] | 237 | get_l10n_args('Author: %s', stripslashes($comm['author']) ), |
---|
[18164] | 238 | get_l10n_args('Email: %s', stripslashes($comm['email']) ), |
---|
[3488] | 239 | get_l10n_args('Comment: %s', stripslashes($comm['content']) ), |
---|
[1908] | 240 | get_l10n_args('', ''), |
---|
[5195] | 241 | get_l10n_args('Manage this user comment: %s', $comment_url) |
---|
[1908] | 242 | ); |
---|
[1849] | 243 | |
---|
[5195] | 244 | if ('moderate' == $comment_action) |
---|
[1849] | 245 | { |
---|
[5195] | 246 | $keyargs_content[] = get_l10n_args('', ''); |
---|
| 247 | $keyargs_content[] = get_l10n_args('(!) This comment requires validation', ''); |
---|
[1849] | 248 | } |
---|
| 249 | |
---|
[1908] | 250 | pwg_mail_notification_admins |
---|
[1849] | 251 | ( |
---|
[3600] | 252 | get_l10n_args('Comment by %s', stripslashes($comm['author']) ), |
---|
[1908] | 253 | $keyargs_content |
---|
[1849] | 254 | ); |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | return $comment_action; |
---|
| 258 | } |
---|
| 259 | |
---|
[3445] | 260 | /** |
---|
| 261 | * Tries to delete a user comment in the database |
---|
| 262 | * only admin can delete all comments |
---|
[3488] | 263 | * other users can delete their own comments |
---|
[3445] | 264 | * so to avoid a new sql request we add author in where clause |
---|
| 265 | * |
---|
[12596] | 266 | * @param int or array of int comment_id |
---|
[3445] | 267 | */ |
---|
[12596] | 268 | function delete_user_comment($comment_id) |
---|
| 269 | { |
---|
[3445] | 270 | $user_where_clause = ''; |
---|
| 271 | if (!is_admin()) |
---|
| 272 | { |
---|
[3450] | 273 | $user_where_clause = ' AND author_id = \''.$GLOBALS['user']['id'].'\''; |
---|
[3445] | 274 | } |
---|
[12596] | 275 | |
---|
| 276 | if (is_array($comment_id)) |
---|
| 277 | $where_clause = 'id IN('.implode(',', $comment_id).')'; |
---|
| 278 | else |
---|
| 279 | $where_clause = 'id = '.$comment_id; |
---|
| 280 | |
---|
[3445] | 281 | $query = ' |
---|
| 282 | DELETE FROM '.COMMENTS_TABLE.' |
---|
[12596] | 283 | WHERE '.$where_clause. |
---|
[3445] | 284 | $user_where_clause.' |
---|
| 285 | ;'; |
---|
| 286 | $result = pwg_query($query); |
---|
[12596] | 287 | |
---|
| 288 | if ($result) |
---|
| 289 | { |
---|
[5707] | 290 | email_admin('delete', |
---|
| 291 | array('author' => $GLOBALS['user']['username'], |
---|
| 292 | 'comment_id' => $comment_id |
---|
| 293 | )); |
---|
[3445] | 294 | } |
---|
[12556] | 295 | |
---|
| 296 | trigger_action('user_comment_deletion', $comment_id); |
---|
[3445] | 297 | } |
---|
| 298 | |
---|
| 299 | /** |
---|
| 300 | * Tries to update a user comment in the database |
---|
| 301 | * only admin can update all comments |
---|
| 302 | * users can edit their own comments if admin allow them |
---|
| 303 | * so to avoid a new sql request we add author in where clause |
---|
| 304 | * |
---|
[3488] | 305 | * @param comment_id |
---|
[3445] | 306 | * @param post_key |
---|
| 307 | * @param content |
---|
| 308 | */ |
---|
| 309 | |
---|
[3488] | 310 | function update_user_comment($comment, $post_key) |
---|
| 311 | { |
---|
[3445] | 312 | global $conf; |
---|
| 313 | |
---|
| 314 | $comment_action = 'validate'; |
---|
| 315 | |
---|
[7495] | 316 | if ( !verify_ephemeral_key($post_key, $comment['image_id']) ) |
---|
[3445] | 317 | { |
---|
| 318 | $comment_action='reject'; |
---|
| 319 | } |
---|
[11253] | 320 | elseif (!$conf['comments_validation'] or is_admin()) // should the updated comment must be validated |
---|
| 321 | { |
---|
| 322 | $comment_action='validate'; //one of validate, moderate, reject |
---|
| 323 | } |
---|
| 324 | else |
---|
| 325 | { |
---|
| 326 | $comment_action='moderate'; //one of validate, moderate, reject |
---|
| 327 | } |
---|
[3445] | 328 | |
---|
| 329 | // perform more spam check |
---|
[3488] | 330 | $comment_action = |
---|
[3445] | 331 | trigger_event('user_comment_check', |
---|
[3488] | 332 | $comment_action, |
---|
| 333 | array_merge($comment, |
---|
[3445] | 334 | array('author' => $GLOBALS['user']['username']) |
---|
| 335 | ) |
---|
| 336 | ); |
---|
| 337 | |
---|
| 338 | if ( $comment_action!='reject' ) |
---|
| 339 | { |
---|
| 340 | $user_where_clause = ''; |
---|
| 341 | if (!is_admin()) |
---|
| 342 | { |
---|
[3450] | 343 | $user_where_clause = ' AND author_id = \''. |
---|
| 344 | $GLOBALS['user']['id'].'\''; |
---|
[3445] | 345 | } |
---|
[10097] | 346 | |
---|
[3445] | 347 | $query = ' |
---|
| 348 | UPDATE '.COMMENTS_TABLE.' |
---|
| 349 | SET content = \''.$comment['content'].'\', |
---|
[10097] | 350 | validated = \''.($comment_action=='validate' ? 'true':'false').'\', |
---|
| 351 | validation_date = '.($comment_action=='validate' ? 'NOW()':'NULL').' |
---|
[3445] | 352 | WHERE id = '.$comment['comment_id']. |
---|
| 353 | $user_where_clause.' |
---|
| 354 | ;'; |
---|
| 355 | $result = pwg_query($query); |
---|
[10097] | 356 | |
---|
| 357 | // mail admin and ask to validate the comment |
---|
| 358 | if ($result and $conf['email_admin_on_comment_validation'] and 'moderate' == $comment_action) |
---|
| 359 | { |
---|
| 360 | include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php'); |
---|
| 361 | |
---|
| 362 | $comment_url = get_absolute_root_url().'comments.php?comment_id='.$comment['comment_id']; |
---|
| 363 | |
---|
| 364 | $keyargs_content = array |
---|
| 365 | ( |
---|
| 366 | get_l10n_args('Author: %s', stripslashes($GLOBALS['user']['username']) ), |
---|
| 367 | get_l10n_args('Comment: %s', stripslashes($comment['content']) ), |
---|
| 368 | get_l10n_args('', ''), |
---|
| 369 | get_l10n_args('Manage this user comment: %s', $comment_url), |
---|
| 370 | get_l10n_args('', ''), |
---|
| 371 | get_l10n_args('(!) This comment requires validation', ''), |
---|
| 372 | ); |
---|
| 373 | |
---|
| 374 | pwg_mail_notification_admins |
---|
| 375 | ( |
---|
| 376 | get_l10n_args('Comment by %s', stripslashes($GLOBALS['user']['username']) ), |
---|
| 377 | $keyargs_content |
---|
| 378 | ); |
---|
| 379 | } |
---|
| 380 | // just mail admin |
---|
| 381 | else if ($result) |
---|
| 382 | { |
---|
[3445] | 383 | email_admin('edit', array('author' => $GLOBALS['user']['username'], |
---|
[3488] | 384 | 'content' => stripslashes($comment['content'])) ); |
---|
[3445] | 385 | } |
---|
| 386 | } |
---|
[10097] | 387 | |
---|
| 388 | return $comment_action; |
---|
[3445] | 389 | } |
---|
| 390 | |
---|
[3488] | 391 | function email_admin($action, $comment) |
---|
| 392 | { |
---|
[3445] | 393 | global $conf; |
---|
| 394 | |
---|
| 395 | if (!in_array($action, array('edit', 'delete')) |
---|
| 396 | or (($action=='edit') and !$conf['email_admin_on_comment_edition']) |
---|
| 397 | or (($action=='delete') and !$conf['email_admin_on_comment_deletion'])) |
---|
| 398 | { |
---|
| 399 | return; |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php'); |
---|
[3488] | 403 | |
---|
[3445] | 404 | $keyargs_content = array(); |
---|
| 405 | $keyargs_content[] = get_l10n_args('Author: %s', $comment['author']); |
---|
[3488] | 406 | if ($action=='delete') |
---|
[3445] | 407 | { |
---|
[3488] | 408 | $keyargs_content[] = get_l10n_args('This author removed the comment with id %d', |
---|
[3445] | 409 | $comment['comment_id'] |
---|
| 410 | ); |
---|
| 411 | } |
---|
| 412 | else |
---|
| 413 | { |
---|
| 414 | $keyargs_content[] = get_l10n_args('This author modified following comment:', ''); |
---|
| 415 | $keyargs_content[] = get_l10n_args('Comment: %s', $comment['content']); |
---|
| 416 | } |
---|
[3488] | 417 | |
---|
| 418 | pwg_mail_notification_admins(get_l10n_args('Comment by %s', |
---|
[3445] | 419 | $comment['author']), |
---|
| 420 | $keyargs_content |
---|
| 421 | ); |
---|
| 422 | } |
---|
[5195] | 423 | |
---|
| 424 | function get_comment_author_id($comment_id, $die_on_error=true) |
---|
| 425 | { |
---|
| 426 | $query = ' |
---|
| 427 | SELECT |
---|
| 428 | author_id |
---|
| 429 | FROM '.COMMENTS_TABLE.' |
---|
| 430 | WHERE id = '.$comment_id.' |
---|
| 431 | ;'; |
---|
| 432 | $result = pwg_query($query); |
---|
| 433 | if (pwg_db_num_rows($result) == 0) |
---|
| 434 | { |
---|
| 435 | if ($die_on_error) |
---|
| 436 | { |
---|
| 437 | fatal_error('Unknown comment identifier'); |
---|
| 438 | } |
---|
| 439 | else |
---|
| 440 | { |
---|
| 441 | return false; |
---|
| 442 | } |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | list($author_id) = pwg_db_fetch_row($result); |
---|
| 446 | |
---|
| 447 | return $author_id; |
---|
| 448 | } |
---|
| 449 | |
---|
[12596] | 450 | /** |
---|
| 451 | * Tries to validate a user comment in the database |
---|
| 452 | * @param int or array of int comment_id |
---|
| 453 | */ |
---|
[5195] | 454 | function validate_user_comment($comment_id) |
---|
| 455 | { |
---|
[12596] | 456 | if (is_array($comment_id)) |
---|
| 457 | $where_clause = 'id IN('.implode(',', $comment_id).')'; |
---|
| 458 | else |
---|
| 459 | $where_clause = 'id = '.$comment_id; |
---|
| 460 | |
---|
[5195] | 461 | $query = ' |
---|
| 462 | UPDATE '.COMMENTS_TABLE.' |
---|
[6589] | 463 | SET validated = \'true\' |
---|
[5195] | 464 | , validation_date = NOW() |
---|
[12596] | 465 | WHERE '.$where_clause.' |
---|
[5195] | 466 | ;'; |
---|
| 467 | pwg_query($query); |
---|
[12556] | 468 | |
---|
| 469 | trigger_action('user_comment_validation', $comment_id); |
---|
[5195] | 470 | } |
---|
[1849] | 471 | ?> |
---|