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