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

Revision 9624, 8.4 KB checked in by mistic100, 9 years ago (diff)

[plugins] Comments on Albums

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