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

Revision 15983, 12.0 KB checked in by mistic100, 7 years ago (diff)

bug 2660: check guest IP on insert_user_comment (same system as rate_picture)

  • Property svn:eol-style set to LF
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      $comm['author'] = 'guest';
95    }
96    $comm['author_id'] = $conf['guest_id'];
97    // if a guest try to use the name of an already existing user, he must be
98    // rejected
99    if ( $comm['author'] != 'guest' )
100    {
101      $query = '
102SELECT COUNT(*) AS user_exists
103  FROM '.USERS_TABLE.'
104  WHERE '.$conf['user_fields']['username']." = '".addslashes($comm['author'])."'";
105      $row = pwg_db_fetch_assoc( pwg_query( $query ) );
106      if ( $row['user_exists'] == 1 )
107      {
108        array_push($infos, l10n('This login is already used by another user') );
109        $comment_action='reject';
110      }
111    }
112  }
113  else
114  {
115    $comm['author'] = addslashes($user['username']);
116    $comm['author_id'] = $user['id'];
117  }
118
119  if ( empty($comm['content']) )
120  { // empty comment content
121    $comment_action='reject';
122  }
123
124  if ( !verify_ephemeral_key(@$key, $comm['image_id']) )
125  {
126    $comment_action='reject';
127    $_POST['cr'][] = 'key'; // rvelices: I use this outside to see how spam robots work
128  }
129 
130  // anonymous id = ip address
131  $ip_components = explode('.', $comm['ip']);
132  if (count($ip_components) > 3)
133  {
134    array_pop($ip_components);
135  }
136  $comm['anonymous_id'] = implode('.', $ip_components);
137
138  if ($comment_action!='reject' and $conf['anti-flood_time']>0 and !is_admin())
139  { // anti-flood system
140    $reference_date = pwg_db_get_flood_period_expression($conf['anti-flood_time']);
141
142    $query = '
143SELECT count(1) FROM '.COMMENTS_TABLE.'
144  WHERE date > '.$reference_date.'
145    AND author_id = '.$comm['author_id'];
146    if (!is_classic_user())
147    {
148      $query.= '
149      AND anonymous_id = "'.$comm['anonymous_id'].'"';
150    }
151    $query.= '
152;';
153
154    list($counter) = pwg_db_fetch_row(pwg_query($query));
155    if ( $counter > 0 )
156    {
157      array_push( $infos, l10n('Anti-flood system : please wait for a moment before trying to post another comment') );
158      $comment_action='reject';
159    }
160  }
161
162  // perform more spam check
163  $comment_action = trigger_event('user_comment_check',
164      $comment_action, $comm
165    );
166
167  if ( $comment_action!='reject' )
168  {
169    $query = '
170INSERT INTO '.COMMENTS_TABLE.'
171  (author, author_id, anonymous_id, content, date, validated, validation_date, image_id)
172  VALUES (
173    \''.$comm['author'].'\',
174    '.$comm['author_id'].',
175    \''.$comm['anonymous_id'].'\',
176    \''.$comm['content'].'\',
177    NOW(),
178    \''.($comment_action=='validate' ? 'true':'false').'\',
179    '.($comment_action=='validate' ? 'NOW()':'NULL').',
180    '.$comm['image_id'].'
181  )
182';
183
184    pwg_query($query);
185
186    $comm['id'] = pwg_db_insert_id(COMMENTS_TABLE);
187
188    if ( ($conf['email_admin_on_comment'] && 'validate' == $comment_action)
189        or ($conf['email_admin_on_comment_validation'] and 'moderate' == $comment_action))
190    {
191      include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
192
193      $comment_url = get_absolute_root_url().'comments.php?comment_id='.$comm['id'];
194
195      $keyargs_content = array
196      (
197        get_l10n_args('Author: %s', stripslashes($comm['author']) ),
198        get_l10n_args('Comment: %s', stripslashes($comm['content']) ),
199        get_l10n_args('', ''),
200        get_l10n_args('Manage this user comment: %s', $comment_url)
201      );
202
203      if ('moderate' == $comment_action)
204      {
205        $keyargs_content[] = get_l10n_args('', '');
206        $keyargs_content[] = get_l10n_args('(!) This comment requires validation', '');
207      }
208
209      pwg_mail_notification_admins
210      (
211        get_l10n_args('Comment by %s', stripslashes($comm['author']) ),
212        $keyargs_content
213      );
214    }
215  }
216  return $comment_action;
217}
218
219/**
220 * Tries to delete a user comment in the database
221 * only admin can delete all comments
222 * other users can delete their own comments
223 * so to avoid a new sql request we add author in where clause
224 *
225 * @param int or array of int comment_id
226 */
227function delete_user_comment($comment_id)
228{
229  $user_where_clause = '';
230  if (!is_admin())
231  {
232    $user_where_clause = '   AND author_id = \''.$GLOBALS['user']['id'].'\'';
233  }
234 
235  if (is_array($comment_id))
236    $where_clause = 'id IN('.implode(',', $comment_id).')';
237  else
238    $where_clause = 'id = '.$comment_id;
239   
240  $query = '
241DELETE FROM '.COMMENTS_TABLE.'
242  WHERE '.$where_clause.
243$user_where_clause.'
244;';
245  $result = pwg_query($query);
246 
247  if ($result) 
248  {
249    email_admin('delete', 
250                array('author' => $GLOBALS['user']['username'],
251                      'comment_id' => $comment_id
252                  ));
253  }
254 
255  trigger_action('user_comment_deletion', $comment_id);
256}
257
258/**
259 * Tries to update a user comment in the database
260 * only admin can update all comments
261 * users can edit their own comments if admin allow them
262 * so to avoid a new sql request we add author in where clause
263 *
264 * @param comment_id
265 * @param post_key
266 * @param content
267 */
268
269function update_user_comment($comment, $post_key)
270{
271  global $conf;
272
273  $comment_action = 'validate';
274
275  if ( !verify_ephemeral_key($post_key, $comment['image_id']) )
276  {
277    $comment_action='reject';
278  }
279  elseif (!$conf['comments_validation'] or is_admin()) // should the updated comment must be validated
280  {
281    $comment_action='validate'; //one of validate, moderate, reject
282  }
283  else
284  {
285    $comment_action='moderate'; //one of validate, moderate, reject
286  }
287
288  // perform more spam check
289  $comment_action =
290    trigger_event('user_comment_check',
291                  $comment_action,
292                  array_merge($comment,
293                              array('author' => $GLOBALS['user']['username'])
294                              )
295                  );
296
297  if ( $comment_action!='reject' )
298  {
299    $user_where_clause = '';
300    if (!is_admin())
301    {
302      $user_where_clause = '   AND author_id = \''.
303        $GLOBALS['user']['id'].'\'';
304    }
305
306    $query = '
307UPDATE '.COMMENTS_TABLE.'
308  SET content = \''.$comment['content'].'\',
309      validated = \''.($comment_action=='validate' ? 'true':'false').'\',
310      validation_date = '.($comment_action=='validate' ? 'NOW()':'NULL').'
311  WHERE id = '.$comment['comment_id'].
312$user_where_clause.'
313;';
314    $result = pwg_query($query);
315   
316    // mail admin and ask to validate the comment
317    if ($result and $conf['email_admin_on_comment_validation'] and 'moderate' == $comment_action) 
318    {
319      include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
320
321      $comment_url = get_absolute_root_url().'comments.php?comment_id='.$comment['comment_id'];
322
323      $keyargs_content = array
324      (
325        get_l10n_args('Author: %s', stripslashes($GLOBALS['user']['username']) ),
326        get_l10n_args('Comment: %s', stripslashes($comment['content']) ),
327        get_l10n_args('', ''),
328        get_l10n_args('Manage this user comment: %s', $comment_url),
329        get_l10n_args('', ''),
330        get_l10n_args('(!) This comment requires validation', ''),
331      );
332
333      pwg_mail_notification_admins
334      (
335        get_l10n_args('Comment by %s', stripslashes($GLOBALS['user']['username']) ),
336        $keyargs_content
337      );
338    }
339    // just mail admin
340    else if ($result)
341    {
342      email_admin('edit', array('author' => $GLOBALS['user']['username'],
343                                'content' => stripslashes($comment['content'])) );
344    }
345  }
346 
347  return $comment_action;
348}
349
350function email_admin($action, $comment)
351{
352  global $conf;
353
354  if (!in_array($action, array('edit', 'delete'))
355      or (($action=='edit') and !$conf['email_admin_on_comment_edition'])
356      or (($action=='delete') and !$conf['email_admin_on_comment_deletion']))
357  {
358    return;
359  }
360
361  include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
362
363  $keyargs_content = array();
364  $keyargs_content[] = get_l10n_args('Author: %s', $comment['author']);
365  if ($action=='delete')
366  {
367    $keyargs_content[] = get_l10n_args('This author removed the comment with id %d',
368                                       $comment['comment_id']
369                                       );
370  }
371  else
372  {
373    $keyargs_content[] = get_l10n_args('This author modified following comment:', '');
374    $keyargs_content[] = get_l10n_args('Comment: %s', $comment['content']);
375  }
376
377  pwg_mail_notification_admins(get_l10n_args('Comment by %s',
378                                             $comment['author']),
379                               $keyargs_content
380                               );
381}
382
383function get_comment_author_id($comment_id, $die_on_error=true)
384{
385  $query = '
386SELECT
387    author_id
388  FROM '.COMMENTS_TABLE.'
389  WHERE id = '.$comment_id.'
390;';
391  $result = pwg_query($query);
392  if (pwg_db_num_rows($result) == 0)
393  {
394    if ($die_on_error)
395    {
396      fatal_error('Unknown comment identifier');
397    }
398    else
399    {
400      return false;
401    }
402  }
403 
404  list($author_id) = pwg_db_fetch_row($result);
405
406  return $author_id;
407}
408
409/**
410 * Tries to validate a user comment in the database
411 * @param int or array of int comment_id
412 */
413function validate_user_comment($comment_id)
414{
415  if (is_array($comment_id))
416    $where_clause = 'id IN('.implode(',', $comment_id).')';
417  else
418    $where_clause = 'id = '.$comment_id;
419   
420  $query = '
421UPDATE '.COMMENTS_TABLE.'
422  SET validated = \'true\'
423    , validation_date = NOW()
424  WHERE '.$where_clause.'
425;';
426  pwg_query($query);
427 
428  trigger_action('user_comment_validation', $comment_id);
429}
430?>
Note: See TracBrowser for help on using the repository browser.