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

Last change on this file since 2155 was 2155, checked in by rvelices, 16 years ago
  • send status code 403 when attempt to enter a user comment, but comments are disabled
  • don't increase hit count when a comment is posted
  • remove the check of user ip agains spamhaus.org when a comment is entered (my conclusion is that is useless)
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: functions_comment.inc.php 2155 2007-10-29 23:39:41Z rvelices $
8// | last update   : $Date: 2007-10-29 23:39:41 +0000 (Mon, 29 Oct 2007) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2155 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27/**
28 * returns a "secret key" that is to be sent back when a user enters a comment
29 */
30function get_comment_post_key($image_id)
31{
32  global $conf;
33
34  $time = time();
35
36  return sprintf(
37    '%s:%s',
38    $time,
39    hash_hmac(
40      'md5',
41      $time.':'.$image_id,
42      $conf['secret_key']
43      )
44    );
45}
46
47//returns string action to perform on a new comment: validate, moderate, reject
48function user_comment_check($action, $comment)
49{
50  global $conf,$user;
51
52  if ($action=='reject')
53    return $action;
54
55  $my_action = $conf['comment_spam_reject'] ? 'reject':'moderate';
56
57  if ($action==$my_action)
58    return $action;
59
60  // we do here only BASIC spam check (plugins can do more)
61  if ( !is_a_guest() )
62    return $action;
63
64  $link_count = preg_match_all( '/https?:\/\//',
65    $comment['content'], $matches);
66
67  if ( strpos($comment['author'], 'http://')!==false )
68  {
69    $link_count++;
70  }
71
72  if ( $link_count>$conf['comment_spam_max_links'] )
73    return $my_action;
74
75  return $action;
76}
77
78
79add_event_handler('user_comment_check', 'user_comment_check',
80  EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
81
82/**
83 * Tries to insert a user comment in the database and returns one of :
84 * validate, moderate, reject
85 * @param array comm contains author, content, image_id
86 * @param string key secret key sent back to the browser
87 * @param array infos out array of messages
88 */
89function insert_user_comment( &$comm, $key, &$infos )
90{
91  global $conf, $user;
92
93  $comm = array_merge( $comm,
94    array(
95      'ip' => $_SERVER['REMOTE_ADDR'],
96      'agent' => $_SERVER['HTTP_USER_AGENT']
97    )
98   );
99
100  $infos = array();
101  if (!$conf['comments_validation'] or is_admin())
102  {
103    $comment_action='validate'; //one of validate, moderate, reject
104  }
105  else
106  {
107    $comment_action='moderate'; //one of validate, moderate, reject
108  }
109
110  // display author field if the user status is guest or generic
111  if (!is_classic_user())
112  {
113    if ( empty($comm['author']) )
114    {
115      $comm['author'] = 'guest';
116    }
117    // if a guest try to use the name of an already existing user, he must be
118    // rejected
119    if ( $comm['author'] != 'guest' )
120    {
121      $query = '
122SELECT COUNT(*) AS user_exists
123  FROM '.USERS_TABLE.'
124  WHERE '.$conf['user_fields']['username']." = '".addslashes($comm['author'])."'";
125      $row = mysql_fetch_assoc( pwg_query( $query ) );
126      if ( $row['user_exists'] == 1 )
127      {
128        array_push($infos, l10n('comment_user_exists') );
129        $comment_action='reject';
130      }
131    }
132  }
133  else
134  {
135    $comm['author'] = $user['username'];
136  }
137  if ( empty($comm['content']) )
138  { // empty comment content
139    $comment_action='reject';
140  }
141
142  $key = explode( ':', @$key );
143  if ( count($key)!=2
144        or $key[0]>time()-2 // page must have been retrieved more than 2 sec ago
145        or $key[0]<time()-3600 // 60 minutes expiration
146        or hash_hmac(
147              'md5', $key[0].':'.$comm['image_id'], $conf['secret_key']
148            ) != $key[1]
149      )
150  {
151    $comment_action='reject';
152  }
153
154  if ($comment_action!='reject' and $conf['anti-flood_time']>0 )
155  { // anti-flood system
156    $reference_date = time() - $conf['anti-flood_time'];
157    $query = '
158SELECT id FROM '.COMMENTS_TABLE.'
159  WHERE date > FROM_UNIXTIME('.$reference_date.')
160    AND author = "'.addslashes($comm['author']).'"';
161    if ( mysql_num_rows( pwg_query( $query ) ) > 0 )
162    {
163      array_push( $infos, l10n('comment_anti-flood') );
164      $comment_action='reject';
165    }
166  }
167
168  // perform more spam check
169  $comment_action = trigger_event('user_comment_check',
170      $comment_action, $comm
171    );
172
173  if ( $comment_action!='reject' )
174  {
175    $query = '
176INSERT INTO '.COMMENTS_TABLE.'
177  (author, content, date, validated, validation_date, image_id)
178  VALUES (
179    "'.addslashes($comm['author']).'",
180    "'.addslashes($comm['content']).'",
181    NOW(),
182    "'.($comment_action=='validate' ? 'true':'false').'",
183    '.($comment_action=='validate' ? 'NOW()':'NULL').',
184    '.$comm['image_id'].'
185  )
186';
187
188    pwg_query($query);
189
190    $comm['id'] = mysql_insert_id();
191
192    if ( ($comment_action=='validate' and $conf['email_admin_on_comment'])
193      or $conf['email_admin_on_comment_validation'] )
194    {
195      include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
196
197      $del_url =
198          get_absolute_root_url().'comments.php?delete='.$comm['id'];
199
200      $keyargs_content = array
201      (
202        get_l10n_args('Author: %s', $comm['author']),
203        get_l10n_args('Comment: %s', $comm['content']),
204        get_l10n_args('', ''),
205        get_l10n_args('Delete: %s', $del_url)
206      );
207
208      if ($comment_action!='validate')
209      {
210        $keyargs_content[] =
211          get_l10n_args('', '');
212        $keyargs_content[] =
213          get_l10n_args('Validate: %s',
214            get_absolute_root_url().'comments.php?validate='.$comm['id']);
215      }
216
217      pwg_mail_notification_admins
218      (
219        get_l10n_args('Comment by %s', $comm['author']),
220        $keyargs_content
221      );
222    }
223  }
224  return $comment_action;
225}
226
227?>
Note: See TracBrowser for help on using the repository browser.