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

Last change on this file since 12562 was 12562, checked in by mistic100, 9 years ago

minor changes for Piwigo 2.3.1 and Subscribe To Comments plugin

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