source: trunk/include/functions_comment.inc.php @ 18634

Last change on this file since 18634 was 18164, checked in by mistic100, 12 years ago

feature 2754: Add "Email" field for user comments + mandatory "Author"

  • Property svn:eol-style set to LF
File size: 13.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 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
25function 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  {
51    $_POST['cr'][] = 'links';
52    return $my_action;
53  }
54  return $action;
55}
56
57
58add_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 */
68function insert_user_comment( &$comm, $key, &$infos )
69{
70  global $conf, $user;
71
72  $comm = array_merge( $comm,
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
89  // display author field if the user status is guest or generic
90  if (!is_classic_user())
91  {
92    if ( empty($comm['author']) )
93    {
94      if ($conf['comments_author_mandatory'])
95      {
96        array_push($infos, l10n('Username is mandatory') );
97        $comment_action='reject';
98      }
99      $comm['author'] = 'guest';
100    }
101    $comm['author_id'] = $conf['guest_id'];
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 = '
107SELECT COUNT(*) AS user_exists
108  FROM '.USERS_TABLE.'
109  WHERE '.$conf['user_fields']['username']." = '".addslashes($comm['author'])."'";
110      $row = pwg_db_fetch_assoc( pwg_query( $query ) );
111      if ( $row['user_exists'] == 1 )
112      {
113        array_push($infos, l10n('This login is already used by another user') );
114        $comment_action='reject';
115      }
116    }
117  }
118  else
119  {
120    $comm['author'] = addslashes($user['username']);
121    $comm['author_id'] = $user['id'];
122  }
123
124  if ( empty($comm['content']) )
125  { // empty comment content
126    $comment_action='reject';
127  }
128
129  if ( !verify_ephemeral_key(@$key, $comm['image_id']) )
130  {
131    $comment_action='reject';
132    $_POST['cr'][] = 'key'; // rvelices: I use this outside to see how spam robots work
133  }
134 
135  // website
136  if (!empty($comm['website_url']))
137  {
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    }
147  }
148 
149  // email
150  if (empty($comm['email']))
151  {
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)'));
165    $comment_action='reject';
166  }
167 
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);
175
176  if ($comment_action!='reject' and $conf['anti-flood_time']>0 and !is_admin())
177  { // anti-flood system
178    $reference_date = pwg_db_get_flood_period_expression($conf['anti-flood_time']);
179
180    $query = '
181SELECT count(1) FROM '.COMMENTS_TABLE.'
182  WHERE date > '.$reference_date.'
183    AND author_id = '.$comm['author_id'];
184    if (!is_classic_user())
185    {
186      $query.= '
187      AND anonymous_id = "'.$comm['anonymous_id'].'"';
188    }
189    $query.= '
190;';
191
192    list($counter) = pwg_db_fetch_row(pwg_query($query));
193    if ( $counter > 0 )
194    {
195      array_push( $infos, l10n('Anti-flood system : please wait for a moment before trying to post another comment') );
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 = '
208INSERT INTO '.COMMENTS_TABLE.'
209  (author, author_id, anonymous_id, content, date, validated, validation_date, image_id, website_url, email)
210  VALUES (
211    \''.$comm['author'].'\',
212    '.$comm['author_id'].',
213    \''.$comm['anonymous_id'].'\',
214    \''.$comm['content'].'\',
215    NOW(),
216    \''.($comment_action=='validate' ? 'true':'false').'\',
217    '.($comment_action=='validate' ? 'NOW()':'NULL').',
218    '.$comm['image_id'].',
219    '.(!empty($comm['website_url']) ? '\''.$comm['website_url'].'\'' : 'NULL').',
220    '.(!empty($comm['email']) ? '\''.$comm['email'].'\'' : 'NULL').'
221  )
222';
223
224    pwg_query($query);
225
226    $comm['id'] = pwg_db_insert_id(COMMENTS_TABLE);
227
228    if ( ($conf['email_admin_on_comment'] && 'validate' == $comment_action)
229        or ($conf['email_admin_on_comment_validation'] and 'moderate' == $comment_action))
230    {
231      include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
232
233      $comment_url = get_absolute_root_url().'comments.php?comment_id='.$comm['id'];
234
235      $keyargs_content = array
236      (
237        get_l10n_args('Author: %s', stripslashes($comm['author']) ),
238        get_l10n_args('Email: %s', stripslashes($comm['email']) ),
239        get_l10n_args('Comment: %s', stripslashes($comm['content']) ),
240        get_l10n_args('', ''),
241        get_l10n_args('Manage this user comment: %s', $comment_url)
242      );
243
244      if ('moderate' == $comment_action)
245      {
246        $keyargs_content[] = get_l10n_args('', '');
247        $keyargs_content[] = get_l10n_args('(!) This comment requires validation', '');
248      }
249
250      pwg_mail_notification_admins
251      (
252        get_l10n_args('Comment by %s', stripslashes($comm['author']) ),
253        $keyargs_content
254      );
255    }
256  }
257  return $comment_action;
258}
259
260/**
261 * Tries to delete a user comment in the database
262 * only admin can delete all comments
263 * other users can delete their own comments
264 * so to avoid a new sql request we add author in where clause
265 *
266 * @param int or array of int comment_id
267 */
268function delete_user_comment($comment_id)
269{
270  $user_where_clause = '';
271  if (!is_admin())
272  {
273    $user_where_clause = '   AND author_id = \''.$GLOBALS['user']['id'].'\'';
274  }
275 
276  if (is_array($comment_id))
277    $where_clause = 'id IN('.implode(',', $comment_id).')';
278  else
279    $where_clause = 'id = '.$comment_id;
280   
281  $query = '
282DELETE FROM '.COMMENTS_TABLE.'
283  WHERE '.$where_clause.
284$user_where_clause.'
285;';
286  $result = pwg_query($query);
287 
288  if ($result) 
289  {
290    email_admin('delete', 
291                array('author' => $GLOBALS['user']['username'],
292                      'comment_id' => $comment_id
293                  ));
294  }
295 
296  trigger_action('user_comment_deletion', $comment_id);
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 *
305 * @param comment_id
306 * @param post_key
307 * @param content
308 */
309
310function update_user_comment($comment, $post_key)
311{
312  global $conf;
313
314  $comment_action = 'validate';
315
316  if ( !verify_ephemeral_key($post_key, $comment['image_id']) )
317  {
318    $comment_action='reject';
319  }
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  }
328
329  // perform more spam check
330  $comment_action =
331    trigger_event('user_comment_check',
332                  $comment_action,
333                  array_merge($comment,
334                              array('author' => $GLOBALS['user']['username'])
335                              )
336                  );
337
338  if ( $comment_action!='reject' )
339  {
340    $user_where_clause = '';
341    if (!is_admin())
342    {
343      $user_where_clause = '   AND author_id = \''.
344        $GLOBALS['user']['id'].'\'';
345    }
346
347    $query = '
348UPDATE '.COMMENTS_TABLE.'
349  SET content = \''.$comment['content'].'\',
350      validated = \''.($comment_action=='validate' ? 'true':'false').'\',
351      validation_date = '.($comment_action=='validate' ? 'NOW()':'NULL').'
352  WHERE id = '.$comment['comment_id'].
353$user_where_clause.'
354;';
355    $result = pwg_query($query);
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    {
383      email_admin('edit', array('author' => $GLOBALS['user']['username'],
384                                'content' => stripslashes($comment['content'])) );
385    }
386  }
387 
388  return $comment_action;
389}
390
391function email_admin($action, $comment)
392{
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');
403
404  $keyargs_content = array();
405  $keyargs_content[] = get_l10n_args('Author: %s', $comment['author']);
406  if ($action=='delete')
407  {
408    $keyargs_content[] = get_l10n_args('This author removed the comment with id %d',
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  }
417
418  pwg_mail_notification_admins(get_l10n_args('Comment by %s',
419                                             $comment['author']),
420                               $keyargs_content
421                               );
422}
423
424function get_comment_author_id($comment_id, $die_on_error=true)
425{
426  $query = '
427SELECT
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
450/**
451 * Tries to validate a user comment in the database
452 * @param int or array of int comment_id
453 */
454function validate_user_comment($comment_id)
455{
456  if (is_array($comment_id))
457    $where_clause = 'id IN('.implode(',', $comment_id).')';
458  else
459    $where_clause = 'id = '.$comment_id;
460   
461  $query = '
462UPDATE '.COMMENTS_TABLE.'
463  SET validated = \'true\'
464    , validation_date = NOW()
465  WHERE '.$where_clause.'
466;';
467  pwg_query($query);
468 
469  trigger_action('user_comment_validation', $comment_id);
470}
471?>
Note: See TracBrowser for help on using the repository browser.