diff options
Diffstat (limited to 'include/functions_comment.inc.php')
-rw-r--r-- | include/functions_comment.inc.php | 173 |
1 files changed, 108 insertions, 65 deletions
diff --git a/include/functions_comment.inc.php b/include/functions_comment.inc.php index dc218a2ff..f14431cf7 100644 --- a/include/functions_comment.inc.php +++ b/include/functions_comment.inc.php @@ -2,7 +2,7 @@ // +-----------------------------------------------------------------------+ // | Piwigo - a PHP based photo gallery | // +-----------------------------------------------------------------------+ -// | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org | +// | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | // +-----------------------------------------------------------------------+ @@ -21,7 +21,22 @@ // | USA. | // +-----------------------------------------------------------------------+ -//returns string action to perform on a new comment: validate, moderate, reject +/** + * @package functions\comment + */ + + +add_event_handler('user_comment_check', 'user_comment_check', + EVENT_HANDLER_PRIORITY_NEUTRAL, 2); + +/** + * Does basic check on comment and returns action to perform. + * This method is called by a trigger_event() + * + * @param string $action before check + * @param array $comment + * @return string validate, moderate, reject + */ function user_comment_check($action, $comment) { global $conf,$user; @@ -54,18 +69,15 @@ function user_comment_check($action, $comment) return $action; } - -add_event_handler('user_comment_check', 'user_comment_check', - EVENT_HANDLER_PRIORITY_NEUTRAL, 2); - /** - * Tries to insert a user comment in the database and returns one of : - * validate, moderate, reject - * @param array comm contains author, content, image_id - * @param string key secret key sent back to the browser - * @param array infos out array of messages + * Tries to insert a user comment and returns action to perform. + * + * @param array &$comm + * @param string $key secret key sent back to the browser + * @param array &$infos output array of error messages + * @return string validate, moderate, reject */ -function insert_user_comment( &$comm, $key, &$infos ) +function insert_user_comment(&$comm, $key, &$infos) { global $conf, $user; @@ -93,7 +105,7 @@ function insert_user_comment( &$comm, $key, &$infos ) { if ($conf['comments_author_mandatory']) { - array_push($infos, l10n('Username is mandatory') ); + $infos[] = l10n('Username is mandatory'); $comment_action='reject'; } $comm['author'] = 'guest'; @@ -110,7 +122,7 @@ SELECT COUNT(*) AS user_exists $row = pwg_db_fetch_assoc( pwg_query( $query ) ); if ( $row['user_exists'] == 1 ) { - array_push($infos, l10n('This login is already used by another user') ); + $infos[] = l10n('This login is already used by another user'); $comment_action='reject'; } } @@ -141,7 +153,7 @@ SELECT COUNT(*) AS user_exists } if (!url_check_format($comm['website_url'])) { - array_push($infos, l10n('Your website URL is invalid')); + $infos[] = l10n('Your website URL is invalid'); $comment_action='reject'; } } @@ -155,13 +167,13 @@ SELECT COUNT(*) AS user_exists } else if ($conf['comments_email_mandatory']) { - array_push($infos, l10n('Email address is missing. Please specify an email address.') ); + $infos[] = l10n('Email address is missing. Please specify an email address.'); $comment_action='reject'; } } else if (!email_check_format($comm['email'])) { - array_push($infos, l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)')); + $infos[] = l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)'); $comment_action='reject'; } @@ -192,7 +204,7 @@ SELECT count(1) FROM '.COMMENTS_TABLE.' list($counter) = pwg_db_fetch_row(pwg_query($query)); if ( $counter > 0 ) { - array_push( $infos, l10n('Anti-flood system : please wait for a moment before trying to post another comment') ); + $infos[] = l10n('Anti-flood system : please wait for a moment before trying to post another comment'); $comment_action='reject'; } } @@ -220,11 +232,11 @@ INSERT INTO '.COMMENTS_TABLE.' '.(!empty($comm['email']) ? '\''.$comm['email'].'\'' : 'NULL').' ) '; - pwg_query($query); - $comm['id'] = pwg_db_insert_id(COMMENTS_TABLE); + invalidate_user_cache_nb_comments(); + if ( ($conf['email_admin_on_comment'] && 'validate' == $comment_action) or ($conf['email_admin_on_comment_validation'] and 'moderate' == $comment_action)) { @@ -232,38 +244,36 @@ INSERT INTO '.COMMENTS_TABLE.' $comment_url = get_absolute_root_url().'comments.php?comment_id='.$comm['id']; - $keyargs_content = array - ( + $keyargs_content = array( get_l10n_args('Author: %s', stripslashes($comm['author']) ), get_l10n_args('Email: %s', stripslashes($comm['email']) ), get_l10n_args('Comment: %s', stripslashes($comm['content']) ), - get_l10n_args('', ''), - get_l10n_args('Manage this user comment: %s', $comment_url) + get_l10n_args(''), + get_l10n_args('Manage this user comment: %s', $comment_url), ); if ('moderate' == $comment_action) { - $keyargs_content[] = get_l10n_args('', ''); - $keyargs_content[] = get_l10n_args('(!) This comment requires validation', ''); + $keyargs_content[] = get_l10n_args('(!) This comment requires validation'); } - pwg_mail_notification_admins - ( + pwg_mail_notification_admins( get_l10n_args('Comment by %s', stripslashes($comm['author']) ), $keyargs_content ); } } + return $comment_action; } /** - * Tries to delete a user comment in the database - * only admin can delete all comments - * other users can delete their own comments - * so to avoid a new sql request we add author in where clause + * Tries to delete a (or more) user comment. + * only admin can delete all comments + * other users can delete their own comments * - * @param int or array of int comment_id + * @param int|int[] $comment_id + * @return bool false if nothing deleted */ function delete_user_comment($comment_id) { @@ -283,28 +293,31 @@ DELETE FROM '.COMMENTS_TABLE.' WHERE '.$where_clause. $user_where_clause.' ;'; - $result = pwg_query($query); - if ($result) + if ( pwg_db_changes(pwg_query($query)) ) { + invalidate_user_cache_nb_comments(); + email_admin('delete', array('author' => $GLOBALS['user']['username'], 'comment_id' => $comment_id )); + trigger_action('user_comment_deletion', $comment_id); + + return true; } - - trigger_action('user_comment_deletion', $comment_id); + + return false; } /** - * Tries to update a user comment in the database - * only admin can update all comments - * users can edit their own comments if admin allow them - * so to avoid a new sql request we add author in where clause + * Tries to update a user comment + * only admin can update all comments + * users can edit their own comments if admin allow them * - * @param comment_id - * @param post_key - * @param content + * @param array $comment + * @param string $post_key secret key sent back to the browser + * @return string validate, moderate, reject */ function update_user_comment($comment, $post_key) @@ -344,7 +357,7 @@ function update_user_comment($comment, $post_key) } if (!url_check_format($comment['website_url'])) { - array_push($page['errors'], l10n('Your website URL is invalid')); + $page['errors'][] = l10n('Your website URL is invalid'); $comment_action='reject'; } } @@ -376,24 +389,21 @@ $user_where_clause.' $comment_url = get_absolute_root_url().'comments.php?comment_id='.$comment['comment_id']; - $keyargs_content = array - ( + $keyargs_content = array( get_l10n_args('Author: %s', stripslashes($GLOBALS['user']['username']) ), get_l10n_args('Comment: %s', stripslashes($comment['content']) ), - get_l10n_args('', ''), + get_l10n_args(''), get_l10n_args('Manage this user comment: %s', $comment_url), - get_l10n_args('', ''), - get_l10n_args('(!) This comment requires validation', ''), + get_l10n_args('(!) This comment requires validation'), ); - pwg_mail_notification_admins - ( + pwg_mail_notification_admins( get_l10n_args('Comment by %s', stripslashes($GLOBALS['user']['username']) ), $keyargs_content ); } // just mail admin - else if ($result) + elseif ($result) { email_admin('edit', array('author' => $GLOBALS['user']['username'], 'content' => stripslashes($comment['content'])) ); @@ -403,6 +413,13 @@ $user_where_clause.' return $comment_action; } +/** + * Notifies admins about updated or deleted comment. + * Only used when no validation is needed, otherwise pwg_mail_notification_admins() is used. + * + * @param string $action edit, delete + * @param array $comment + */ function email_admin($action, $comment) { global $conf; @@ -416,26 +433,33 @@ function email_admin($action, $comment) include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php'); - $keyargs_content = array(); - $keyargs_content[] = get_l10n_args('Author: %s', $comment['author']); + $keyargs_content = array( + get_l10n_args('Author: %s', $comment['author']), + ); + if ($action=='delete') { - $keyargs_content[] = get_l10n_args('This author removed the comment with id %d', - $comment['comment_id'] - ); + $keyargs_content[] = get_l10n_args('This author removed the comment with id %d', $comment['comment_id']); } else { - $keyargs_content[] = get_l10n_args('This author modified following comment:', ''); + $keyargs_content[] = get_l10n_args('This author modified following comment:'); $keyargs_content[] = get_l10n_args('Comment: %s', $comment['content']); } - pwg_mail_notification_admins(get_l10n_args('Comment by %s', - $comment['author']), - $keyargs_content - ); + pwg_mail_notification_admins( + get_l10n_args('Comment by %s', $comment['author']), + $keyargs_content + ); } +/** + * Returns the author id of a comment + * + * @param int $comment_id + * @param bool $die_on_error + * @return int + */ function get_comment_author_id($comment_id, $die_on_error=true) { $query = ' @@ -463,8 +487,9 @@ SELECT } /** - * Tries to validate a user comment in the database - * @param int or array of int comment_id + * Tries to validate a user comment. + * + * @param int|int[] $comment_id */ function validate_user_comment($comment_id) { @@ -481,6 +506,24 @@ UPDATE '.COMMENTS_TABLE.' ;'; pwg_query($query); + invalidate_user_cache_nb_comments(); trigger_action('user_comment_validation', $comment_id); } + +/** + * Clears cache of nb comments for all users + */ +function invalidate_user_cache_nb_comments() +{ + global $user; + + unset($user['nb_available_comments']); + + $query = ' +UPDATE '.USER_CACHE_TABLE.' + SET nb_available_comments = NULL +;'; + pwg_query($query); +} + ?>
\ No newline at end of file |