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

Last change on this file since 3450 was 3450, checked in by nikrou, 15 years ago

Feature 1026 step 2 :
add author_id column so that guest cannot modify old users comments

  • Property svn:eol-style set to LF
File size: 9.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 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    return $my_action;
51
52  return $action;
53}
54
55
56add_event_handler('user_comment_check', 'user_comment_check',
57  EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
58
59/**
60 * Tries to insert a user comment in the database and returns one of :
61 * validate, moderate, reject
62 * @param array comm contains author, content, image_id
63 * @param string key secret key sent back to the browser
64 * @param array infos out array of messages
65 */
66function insert_user_comment( &$comm, $key, &$infos )
67{
68  global $conf, $user;
69
70  $comm = array_merge( $comm,
71    array(
72      'ip' => $_SERVER['REMOTE_ADDR'],
73      'agent' => $_SERVER['HTTP_USER_AGENT']
74    )
75   );
76
77  $infos = array();
78  if (!$conf['comments_validation'] or is_admin())
79  {
80    $comment_action='validate'; //one of validate, moderate, reject
81  }
82  else
83  {
84    $comment_action='moderate'; //one of validate, moderate, reject
85  }
86
87  // display author field if the user status is guest or generic
88  if (!is_classic_user())
89  {
90    if ( empty($comm['author']) )
91    {
92      $comm['author'] = 'guest';
93    }
94    $comm['author_id'] = $conf['guest_id'];
95    // if a guest try to use the name of an already existing user, he must be
96    // rejected
97    if ( $comm['author'] != 'guest' )
98    {
99      $query = '
100SELECT COUNT(*) AS user_exists
101  FROM '.USERS_TABLE.'
102  WHERE '.$conf['user_fields']['username']." = '".addslashes($comm['author'])."'";
103      $row = mysql_fetch_assoc( pwg_query( $query ) );
104      if ( $row['user_exists'] == 1 )
105      {
106        array_push($infos, l10n('comment_user_exists') );
107        $comment_action='reject';
108      }
109    }
110  }
111  else
112  {
113    $comm['author'] = '';
114    $comm['author_id'] = $user['id'];
115  }
116
117  if ( empty($comm['content']) )
118  { // empty comment content
119    $comment_action='reject';
120  }
121
122  $key = explode( ':', @$key );
123  if ( count($key)!=2
124        or $key[0]>time()-2 // page must have been retrieved more than 2 sec ago
125        or $key[0]<time()-3600 // 60 minutes expiration
126        or hash_hmac(
127              'md5', $key[0].':'.$comm['image_id'], $conf['secret_key']
128            ) != $key[1]
129      )
130  {
131    $comment_action='reject';
132  }
133
134  if ($comment_action!='reject' and $conf['anti-flood_time']>0 )
135  { // anti-flood system
136    $reference_date = time() - $conf['anti-flood_time'];
137    $query = '
138SELECT id FROM '.COMMENTS_TABLE.'
139  WHERE date > FROM_UNIXTIME('.$reference_date.')
140    AND author_id = '.$comm['author_id'];
141    if ( mysql_num_rows( pwg_query( $query ) ) > 0 )
142    {
143      array_push( $infos, l10n('comment_anti-flood') );
144      $comment_action='reject';
145    }
146  }
147
148  // perform more spam check
149  $comment_action = trigger_event('user_comment_check',
150      $comment_action, $comm
151    );
152
153  if ( $comment_action!='reject' )
154  {
155    $query = '
156INSERT INTO '.COMMENTS_TABLE.'
157  (author, author_id, content, date, validated, validation_date, image_id)
158  VALUES (
159    "'.addslashes($comm['author']).'",
160    '.$comm['author_id'].',
161    "'.addslashes($comm['content']).'",
162    NOW(),
163    "'.($comment_action=='validate' ? 'true':'false').'",
164    '.($comment_action=='validate' ? 'NOW()':'NULL').',
165    '.$comm['image_id'].'
166  )
167';
168
169    pwg_query($query);
170
171    $comm['id'] = mysql_insert_id();
172
173    if (($comment_action=='validate' and $conf['email_admin_on_comment']) or
174        ($comment_action!='validate' 
175         and $conf['email_admin_on_comment_validation']))
176    {
177      include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
178
179      $del_url = get_absolute_root_url().'comments.php?delete='.$comm['id'];
180
181      if (empty($comm['author'])) 
182      {
183        $author_name = $user['username'];
184      }
185      else
186      {
187        $author_name = $comm['author'];
188      }
189      $keyargs_content = array
190      (
191        get_l10n_args('Author: %s', $author_name),
192        get_l10n_args('Comment: %s', $comm['content']),
193        get_l10n_args('', ''),
194        get_l10n_args('Delete: %s', $del_url)
195      );
196
197      if ($comment_action!='validate')
198      {
199        $keyargs_content[] =
200          get_l10n_args('', '');
201        $keyargs_content[] =
202          get_l10n_args('Validate: %s',
203            get_absolute_root_url().'comments.php?validate='.$comm['id']);
204      }
205
206      pwg_mail_notification_admins
207      (
208        get_l10n_args('Comment by %s', $author_name),
209        $keyargs_content
210      );
211    }
212  }
213  return $comment_action;
214}
215
216/**
217 * Tries to delete a user comment in the database
218 * only admin can delete all comments
219 * other users can delete their own comments
220 * so to avoid a new sql request we add author in where clause
221 *
222 * @param comment_id
223 */
224
225function delete_user_comment($comment_id) {
226  $user_where_clause = '';
227  if (!is_admin())
228  {
229    $user_where_clause = '   AND author_id = \''.$GLOBALS['user']['id'].'\'';
230  }
231  $query = '
232DELETE FROM '.COMMENTS_TABLE.'
233  WHERE id = '.$comment_id.
234$user_where_clause.'
235;';
236  $result = pwg_query($query);
237  if ($result) {
238    email_admin('delete', array('author' => $GLOBALS['user']['username']));
239  }
240}
241
242/**
243 * Tries to update a user comment in the database
244 * only admin can update all comments
245 * users can edit their own comments if admin allow them
246 * so to avoid a new sql request we add author in where clause
247 *
248 * @param comment_id
249 * @param post_key
250 * @param content
251 */
252
253function update_user_comment($comment, $post_key) {
254  global $conf;
255
256  $comment_action = 'validate';
257
258  $key = explode( ':', $post_key );
259  if ( count($key)!=2
260       or $key[0]>time()-2 // page must have been retrieved more than 2 sec ago
261       or $key[0]<time()-3600 // 60 minutes expiration
262       or hash_hmac('md5', $key[0].':'.$comment['image_id'], $conf['secret_key']
263                    ) != $key[1]
264       )
265  {
266    $comment_action='reject';
267  }
268
269  if ($comment_action!='reject' and $conf['anti-flood_time']>0 )
270  { // anti-flood system
271    $reference_date = time() - $conf['anti-flood_time'];
272    $query = '
273SELECT id FROM '.COMMENTS_TABLE.'
274  WHERE date > FROM_UNIXTIME('.$reference_date.')
275    AND author_id = '.$comm['author_id'];
276    if ( mysql_num_rows( pwg_query( $query ) ) > 0 )
277    {
278      array_push( $infos, l10n('comment_anti-flood') );
279      $comment_action='reject';
280    }
281  }
282
283  // perform more spam check
284  $comment_action = 
285    trigger_event('user_comment_check',
286                  $comment_action, 
287                  array_merge($comment, 
288                              array('author' => $GLOBALS['user']['username'])
289                              )
290                  );
291
292  if ( $comment_action!='reject' )
293  {
294    $user_where_clause = '';
295    if (!is_admin())
296    {
297      $user_where_clause = '   AND author_id = \''.
298        $GLOBALS['user']['id'].'\'';
299    }
300    $query = '
301UPDATE '.COMMENTS_TABLE.'
302  SET content = \''.$comment['content'].'\',
303      validation_date = now()
304  WHERE id = '.$comment['comment_id'].
305$user_where_clause.'
306;';
307    $result = pwg_query($query);
308    if ($result) {
309      email_admin('edit', array('author' => $GLOBALS['user']['username'],
310                                'content' => $comment['content']));
311    }
312  }
313}
314
315function email_admin($action, $comment) {
316  global $conf;
317
318  if (!in_array($action, array('edit', 'delete'))
319      or (($action=='edit') and !$conf['email_admin_on_comment_edition'])
320      or (($action=='delete') and !$conf['email_admin_on_comment_deletion']))
321  {
322    return;
323  }
324
325  include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
326 
327  $keyargs_content = array();
328  $keyargs_content[] = get_l10n_args('Author: %s', $comment['author']);
329  if ($action=='delete') 
330  {
331    $keyargs_content[] = get_l10n_args('This author remove comment with id %d',
332                                       $comment['comment_id']
333                                       );
334  }
335  else
336  {
337    $keyargs_content[] = get_l10n_args('This author modified following comment:', '');
338    $keyargs_content[] = get_l10n_args('Comment: %s', $comment['content']);
339  }
340 
341  pwg_mail_notification_admins(get_l10n_args('Comment by %s', 
342                                             $comment['author']),
343                               $keyargs_content
344                               );
345}
346?>
Note: See TracBrowser for help on using the repository browser.