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

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

some more modifications for Subscribe_to_comments

File size: 10.6 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, category_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['category_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['category_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?display_mode=albums&amp;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 
198  if (is_array($comment_id))
199    $where_clause = 'id IN('.implode(',', $comment_id).')';
200  else
201    $where_clause = 'id = '.$comment_id;
202 
203  $query = '
204DELETE FROM '.COA_TABLE.'
205  WHERE '.$where_clause.
206$user_where_clause.'
207;';
208  $result = pwg_query($query);
209 
210  if ($result) 
211  {
212    email_admin('delete', 
213                array('author' => $GLOBALS['user']['username'],
214                      'comment_id' => $comment_id
215                  ));
216  }
217 
218  trigger_action('user_comment_deletion', $comment_id, 'category');
219}
220
221/**
222 * Tries to update a user comment in the database
223 * only admin can update all comments
224 * users can edit their own comments if admin allow them
225 * so to avoid a new sql request we add author in where clause
226 *
227 * @param comment_id
228 * @param post_key
229 * @param content
230 */
231function update_user_comment_albums($comment, $post_key)
232{
233  global $conf;
234
235  $comment_action = 'validate';
236
237  if ( !verify_ephemeral_key($post_key, $comment['category_id']) )
238  {
239    $comment_action='reject';
240  }
241
242  // perform more spam check
243  $comment_action =
244    trigger_event('user_comment_check',
245      $comment_action,
246      array_merge($comment,
247            array('author' => $GLOBALS['user']['username'])
248            )
249      );
250
251  if ( $comment_action!='reject' )
252  {
253    $user_where_clause = '';
254    if (!is_admin())
255    {
256      $user_where_clause = '   AND author_id = \''.
257  $GLOBALS['user']['id'].'\'';
258    }
259   
260    // should the updated comment must be validated
261    if (!$conf['comments_validation'] or is_admin())
262    {
263      $comment_action='validate'; //one of validate, moderate, reject
264    }
265    else
266    {
267      $comment_action='moderate'; //one of validate, moderate, reject
268    }
269
270    $query = '
271UPDATE '.COA_TABLE.'
272  SET content = \''.$comment['content'].'\',
273      validated = \''.($comment_action=='validate' ? 'true':'false').'\',
274      validation_date = '.($comment_action=='validate' ? 'NOW()':'NULL').'
275  WHERE id = '.$comment['comment_id'].
276$user_where_clause.'
277;';
278    $result = pwg_query($query);
279   
280    // mail admin and ask to validate the comment
281    if ($result and $conf['email_admin_on_comment_validation'] and 'moderate' == $comment_action) 
282    {
283      include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
284
285      $comment_url = get_absolute_root_url().'comments.php?display_mode=albums&amp;comment_id='.$comment['comment_id'];
286
287      $keyargs_content = array
288      (
289        get_l10n_args('Author: %s', stripslashes($GLOBALS['user']['username']) ),
290        get_l10n_args('Comment: %s', stripslashes($comment['content']) ),
291        get_l10n_args('', ''),
292        get_l10n_args('Manage this user comment: %s', $comment_url),
293        get_l10n_args('', ''),
294        get_l10n_args('(!) This comment requires validation', ''),
295      );
296
297      pwg_mail_notification_admins
298      (
299        get_l10n_args('Comment by %s', stripslashes($GLOBALS['user']['username']) ),
300        $keyargs_content
301      );
302    }
303    // just mail admin
304    else if ($result)
305    {
306      email_admin('edit', array('author' => $GLOBALS['user']['username'],
307        'content' => stripslashes($comment['content'])) );
308    }
309  }
310 
311  return $comment_action;
312}
313
314if (!function_exists('email_admin')) 
315{
316  function email_admin($action, $comment)
317  {
318    global $conf;
319
320    if (!in_array($action, array('edit', 'delete'))
321      or (($action=='edit') and !$conf['email_admin_on_comment_edition'])
322      or (($action=='delete') and !$conf['email_admin_on_comment_deletion']))
323    {
324    return;
325    }
326
327    include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
328
329    $keyargs_content = array();
330    $keyargs_content[] = get_l10n_args('Author: %s', $comment['author']);
331    if ($action=='delete')
332    {
333    $keyargs_content[] = get_l10n_args('This author removed the comment with id %d',
334               $comment['comment_id']
335               );
336    }
337    else
338    {
339    $keyargs_content[] = get_l10n_args('This author modified following comment:', '');
340    $keyargs_content[] = get_l10n_args('Comment: %s', $comment['content']);
341    }
342
343    pwg_mail_notification_admins(get_l10n_args('Comment by %s',
344               $comment['author']),
345             $keyargs_content
346             );
347  }
348}
349
350function get_comment_author_id_albums($comment_id, $die_on_error=true)
351{
352  $query = '
353SELECT
354    author_id
355  FROM '.COA_TABLE.'
356  WHERE id = '.$comment_id.'
357;';
358  $result = pwg_query($query);
359  if (pwg_db_num_rows($result) == 0)
360  {
361    if ($die_on_error)
362    {
363      fatal_error('Unknown comment identifier');
364    }
365    else
366    {
367      return false;
368    }
369  }
370 
371  list($author_id) = pwg_db_fetch_row($result);
372
373  return $author_id;
374}
375
376/**
377 * Tries to validate a user comment in the database
378 * @param int or array of int comment_id
379 */
380function validate_user_comment_albums($comment_id)
381{
382  if (is_array($comment_id))
383    $where_clause = 'id IN('.implode(',', $comment_id).')';
384  else
385    $where_clause = 'id = '.$comment_id;
386   
387  $query = '
388UPDATE '.COA_TABLE.'
389  SET validated = \'true\'
390    , validation_date = NOW()
391  WHERE '.$where_clause.'
392;';
393  pwg_query($query);
394 
395  trigger_action('user_comment_validation', $comment_id, 'category');
396}
397?>
Note: See TracBrowser for help on using the repository browser.