source: extensions/Comments_on_Albums/include/functions_comment.inc.php @ 11267

Last change on this file since 11267 was 11267, checked in by mistic100, 13 years ago

big code cleaning

File size: 10.1 KB
Line 
1<?php
2/* This is a copy of include/functions_comment.inc.php but adapted for Comments On Albums */
3
4//returns string action to perform on a new comment: validate, moderate, reject
5if (!function_exists('user_comment_check')) 
6{
7  function user_comment_check($action, $comment)
8  {
9    global $conf,$user;
10
11    if ($action=='reject')
12    return $action;
13
14    $my_action = $conf['comment_spam_reject'] ? 'reject':'moderate';
15
16    if ($action==$my_action)
17    return $action;
18
19    // we do here only BASIC spam check (plugins can do more)
20    if ( !is_a_guest() )
21    return $action;
22
23    $link_count = preg_match_all( '/https?:\/\//',
24    $comment['content'], $matches);
25
26    if ( strpos($comment['author'], 'http://')!==false )
27    {
28    $link_count++;
29    }
30
31    if ( $link_count>$conf['comment_spam_max_links'] )
32    return $my_action;
33
34    return $action;
35  }
36}
37
38
39add_event_handler('user_comment_check', 'user_comment_check',
40  EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
41
42/**
43 * Tries to insert a user comment in the database and returns one of :
44 * validate, moderate, reject
45 * @param array comm contains author, content, image_id
46 * @param string key secret key sent back to the browser
47 * @param array infos out array of messages
48 */
49function insert_user_comment_albums( &$comm, $key, &$infos )
50{
51  global $conf, $user;
52
53  $comm = array_merge( $comm,
54    array(
55      'ip' => $_SERVER['REMOTE_ADDR'],
56      'agent' => $_SERVER['HTTP_USER_AGENT']
57    )
58   );
59
60  $infos = array();
61  if (!$conf['comments_validation'] or is_admin())
62  {
63    $comment_action='validate'; //one of validate, moderate, reject
64  }
65  else
66  {
67    $comment_action='moderate'; //one of validate, moderate, reject
68  }
69
70  // display author field if the user status is guest or generic
71  if (!is_classic_user())
72  {
73    if ( empty($comm['author']) )
74    {
75      $comm['author'] = 'guest';
76    }
77    $comm['author_id'] = $conf['guest_id'];
78    // if a guest try to use the name of an already existing user, he must be
79    // rejected
80    if ( $comm['author'] != 'guest' )
81    {
82      $query = '
83SELECT COUNT(*) AS user_exists
84  FROM '.USERS_TABLE.'
85  WHERE '.$conf['user_fields']['username']." = '".addslashes($comm['author'])."'";
86      $row = pwg_db_fetch_assoc( pwg_query( $query ) );
87      if ( $row['user_exists'] == 1 )
88      {
89        array_push($infos, l10n('This login is already used by another user') );
90        $comment_action='reject';
91      }
92    }
93  }
94  else
95  {
96    $comm['author'] = addslashes($user['username']);
97    $comm['author_id'] = $user['id'];
98  }
99
100  if ( empty($comm['content']) )
101  { // empty comment content
102    $comment_action='reject';
103  }
104
105  if ( !verify_ephemeral_key(@$key, $comm['image_id']) )
106  {
107    $comment_action='reject';
108  }
109
110  if ($comment_action!='reject' and $conf['anti-flood_time']>0 and !is_admin())
111  { // anti-flood system
112    $reference_date = pwg_db_get_flood_period_expression($conf['anti-flood_time']);
113
114    $query = '
115SELECT count(1) FROM '.COA_TABLE.'
116  WHERE date > '.$reference_date.'
117    AND author_id = '.$comm['author_id'];
118    list($counter) = pwg_db_fetch_row(pwg_query($query));
119    if ( $counter > 0 )
120    {
121      array_push( $infos, l10n('Anti-flood system : please wait for a moment before trying to post another comment') );
122      $comment_action='reject';
123    }
124  }
125
126  // perform more spam check
127  $comment_action = trigger_event('user_comment_check',
128      $comment_action, $comm
129    );
130
131  if ( $comment_action!='reject' )
132  {
133    $query = '
134INSERT INTO '.COA_TABLE.'
135  (author, author_id, content, date, validated, validation_date, category_id)
136  VALUES (
137    \''.$comm['author'].'\',
138    '.$comm['author_id'].',
139    \''.$comm['content'].'\',
140    NOW(),
141    \''.($comment_action=='validate' ? 'true':'false').'\',
142    '.($comment_action=='validate' ? 'NOW()':'NULL').',
143    '.$comm['image_id'].'
144  )
145';
146
147    pwg_query($query);
148
149    $comm['id'] = pwg_db_insert_id(COA_TABLE);
150
151    if ($conf['email_admin_on_comment']
152        or ($conf['email_admin_on_comment_validation'] and 'moderate' == $comment_action))
153    {
154      include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
155
156      $comment_url = get_absolute_root_url().'comments.php?comment_id='.$comm['id'];
157
158      $keyargs_content = array
159      (
160        get_l10n_args('Author: %s', stripslashes($comm['author']) ),
161        get_l10n_args('Comment: %s', stripslashes($comm['content']) ),
162        get_l10n_args('', ''),
163        get_l10n_args('Manage this user comment: %s', $comment_url)
164      );
165
166      if ('moderate' == $comment_action)
167      {
168        $keyargs_content[] = get_l10n_args('', '');
169        $keyargs_content[] = get_l10n_args('(!) This comment requires validation', '');
170      }
171
172      pwg_mail_notification_admins
173      (
174        get_l10n_args('Comment by %s', stripslashes($comm['author']) ),
175        $keyargs_content
176      );
177    }
178  }
179  return $comment_action;
180}
181
182/**
183 * Tries to delete a user comment in the database
184 * only admin can delete all comments
185 * other users can delete their own comments
186 * so to avoid a new sql request we add author in where clause
187 *
188 * @param comment_id
189 */
190function delete_user_comment_albums($comment_id) 
191{
192  $user_where_clause = '';
193  if (!is_admin())
194  {
195    $user_where_clause = '   AND author_id = \''.$GLOBALS['user']['id'].'\'';
196  }
197  $query = '
198DELETE FROM '.COA_TABLE.'
199  WHERE id = '.$comment_id.
200$user_where_clause.'
201;';
202  $result = pwg_query($query);
203  if ($result) {
204    email_admin('delete', 
205                array('author' => $GLOBALS['user']['username'],
206                      'comment_id' => $comment_id
207                  ));
208  }
209}
210
211/**
212 * Tries to update a user comment in the database
213 * only admin can update all comments
214 * users can edit their own comments if admin allow them
215 * so to avoid a new sql request we add author in where clause
216 *
217 * @param comment_id
218 * @param post_key
219 * @param content
220 */
221function update_user_comment_albums($comment, $post_key)
222{
223  global $conf;
224
225  $comment_action = 'validate';
226
227  if ( !verify_ephemeral_key($post_key, $comment['image_id']) )
228  {
229    $comment_action='reject';
230  }
231
232  // perform more spam check
233  $comment_action =
234    trigger_event('user_comment_check',
235      $comment_action,
236      array_merge($comment,
237            array('author' => $GLOBALS['user']['username'])
238            )
239      );
240
241  if ( $comment_action!='reject' )
242  {
243    $user_where_clause = '';
244    if (!is_admin())
245    {
246      $user_where_clause = '   AND author_id = \''.
247  $GLOBALS['user']['id'].'\'';
248    }
249   
250    // should the updated comment must be validated
251    if (!$conf['comments_validation'] or is_admin())
252    {
253      $comment_action='validate'; //one of validate, moderate, reject
254    }
255    else
256    {
257      $comment_action='moderate'; //one of validate, moderate, reject
258    }
259
260    $query = '
261UPDATE '.COA_TABLE.'
262  SET content = \''.$comment['content'].'\',
263      validated = \''.($comment_action=='validate' ? 'true':'false').'\',
264      validation_date = '.($comment_action=='validate' ? 'NOW()':'NULL').'
265  WHERE id = '.$comment['comment_id'].
266$user_where_clause.'
267;';
268    $result = pwg_query($query);
269   
270    // mail admin and ask to validate the comment
271    if ($result and $conf['email_admin_on_comment_validation'] and 'moderate' == $comment_action) 
272    {
273      include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
274
275      $comment_url = get_absolute_root_url().'comments.php?display_mode=albums&comment_id='.$comment['comment_id'];
276
277      $keyargs_content = array
278      (
279        get_l10n_args('Author: %s', stripslashes($GLOBALS['user']['username']) ),
280        get_l10n_args('Comment: %s', stripslashes($comment['content']) ),
281        get_l10n_args('', ''),
282        get_l10n_args('Manage this user comment: %s', $comment_url),
283        get_l10n_args('', ''),
284        get_l10n_args('(!) This comment requires validation', ''),
285      );
286
287      pwg_mail_notification_admins
288      (
289        get_l10n_args('Comment by %s', stripslashes($GLOBALS['user']['username']) ),
290        $keyargs_content
291      );
292    }
293    // just mail admin
294    else if ($result)
295    {
296      email_admin('edit', array('author' => $GLOBALS['user']['username'],
297        'content' => stripslashes($comment['content'])) );
298    }
299  }
300 
301  return $comment_action;
302}
303
304if (!function_exists('email_admin')) 
305{
306  function email_admin($action, $comment)
307  {
308    global $conf;
309
310    if (!in_array($action, array('edit', 'delete'))
311      or (($action=='edit') and !$conf['email_admin_on_comment_edition'])
312      or (($action=='delete') and !$conf['email_admin_on_comment_deletion']))
313    {
314    return;
315    }
316
317    include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
318
319    $keyargs_content = array();
320    $keyargs_content[] = get_l10n_args('Author: %s', $comment['author']);
321    if ($action=='delete')
322    {
323    $keyargs_content[] = get_l10n_args('This author removed the comment with id %d',
324               $comment['comment_id']
325               );
326    }
327    else
328    {
329    $keyargs_content[] = get_l10n_args('This author modified following comment:', '');
330    $keyargs_content[] = get_l10n_args('Comment: %s', $comment['content']);
331    }
332
333    pwg_mail_notification_admins(get_l10n_args('Comment by %s',
334               $comment['author']),
335             $keyargs_content
336             );
337  }
338}
339
340function get_comment_author_id_albums($comment_id, $die_on_error=true)
341{
342  $query = '
343SELECT
344    author_id
345  FROM '.COA_TABLE.'
346  WHERE id = '.$comment_id.'
347;';
348  $result = pwg_query($query);
349  if (pwg_db_num_rows($result) == 0)
350  {
351    if ($die_on_error)
352    {
353      fatal_error('Unknown comment identifier');
354    }
355    else
356    {
357      return false;
358    }
359  }
360 
361  list($author_id) = pwg_db_fetch_row($result);
362
363  return $author_id;
364}
365
366function validate_user_comment_albums($comment_id)
367{
368  $query = '
369UPDATE '.COA_TABLE.'
370  SET validated = \'true\'
371    , validation_date = NOW()
372  WHERE id = '.$comment_id.'
373;';
374  pwg_query($query);
375}
376?>
Note: See TracBrowser for help on using the repository browser.