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

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

[plugins] Comments on Albums

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