source: trunk/comments.php @ 18490

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

feature 2754: Add "Email" field for user comments + mandatory "Author"

  • Property svn:eol-style set to LF
File size: 16.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24// +-----------------------------------------------------------------------+
25// |                           initialization                              |
26// +-----------------------------------------------------------------------+
27define('PHPWG_ROOT_PATH','./');
28include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
29include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php');
30
31if (!$conf['activate_comments'])
32{
33  page_not_found(null);
34}
35
36// +-----------------------------------------------------------------------+
37// | Check Access and exit when user status is not ok                      |
38// +-----------------------------------------------------------------------+
39check_status(ACCESS_GUEST);
40
41$sort_order = array(
42  'DESC' => l10n('descending'),
43  'ASC'  => l10n('ascending')
44  );
45
46// sort_by : database fields proposed for sorting comments list
47$sort_by = array(
48  'date' => l10n('comment date'),
49  'image_id' => l10n('photo')
50  );
51
52// items_number : list of number of items to display per page
53$items_number = array(5,10,20,50,'all');
54
55// since when display comments ?
56//
57$since_options = array(
58  1 => array('label' => l10n('today'),
59             'clause' => 'date > '.pwg_db_get_recent_period_expression(1)),
60  2 => array('label' => sprintf(l10n('last %d days'), 7),
61             'clause' => 'date > '.pwg_db_get_recent_period_expression(7)),
62  3 => array('label' => sprintf(l10n('last %d days'), 30),
63             'clause' => 'date > '.pwg_db_get_recent_period_expression(30)),
64  4 => array('label' => l10n('the beginning'),
65             'clause' => '1=1') // stupid but generic
66  );
67 
68trigger_action('loc_begin_comments');
69
70if (!empty($_GET['since']) && is_numeric($_GET['since']))
71{
72  $page['since'] = $_GET['since'];
73}
74else
75{
76  $page['since'] = 4;
77}
78
79// on which field sorting
80//
81$page['sort_by'] = 'date';
82// if the form was submitted, it overloads default behaviour
83if (isset($_GET['sort_by']) and isset($sort_by[$_GET['sort_by']]) )
84{
85  $page['sort_by'] = $_GET['sort_by'];
86}
87
88// order to sort
89//
90$page['sort_order'] = 'DESC';
91// if the form was submitted, it overloads default behaviour
92if (isset($_GET['sort_order']) and isset($sort_order[$_GET['sort_order']]))
93{
94  $page['sort_order'] = $_GET['sort_order'];
95}
96
97// number of items to display
98//
99$page['items_number'] = 10;
100if (isset($_GET['items_number']))
101{
102  $page['items_number'] = $_GET['items_number'];
103}
104if ( !is_numeric($page['items_number']) and $page['items_number']!='all' )
105{
106  $page['items_number'] = 10;
107}
108
109$page['where_clauses'] = array();
110
111// which category to filter on ?
112if (isset($_GET['cat']) and 0 != $_GET['cat'])
113{
114  check_input_parameter('cat', $_GET, false, PATTERN_ID);
115
116  $category_ids = get_subcat_ids(array($_GET['cat']));
117  if (empty($category_ids))
118  {
119    $category_ids = array(-1);
120  }
121
122  $page['where_clauses'][] =
123    'category_id IN ('.implode(',', $category_ids).')';
124}
125
126// search a particular author
127if (!empty($_GET['author']))
128{
129  $page['where_clauses'][] =
130    'u.'.$conf['user_fields']['username'].' = \''.$_GET['author'].'\'
131     OR author = \''.$_GET['author'].'\'';
132}
133
134// search a specific comment (if you're coming directly from an admin
135// notification email)
136if (!empty($_GET['comment_id']))
137{
138  check_input_parameter('comment_id', $_GET, false, PATTERN_ID);
139
140  // currently, the $_GET['comment_id'] is only used by admins from email
141  // for management purpose (validate/delete)
142  if (!is_admin())
143  {
144    $login_url =
145      get_root_url().'identification.php?redirect='
146      .urlencode(urlencode($_SERVER['REQUEST_URI']))
147      ;
148    redirect($login_url);
149  }
150
151  $page['where_clauses'][] = 'com.id = '.$_GET['comment_id'];
152}
153
154// search a substring among comments content
155if (!empty($_GET['keyword']))
156{
157  $page['where_clauses'][] =
158    '('.
159    implode(' AND ',
160            array_map(
161              create_function(
162                '$s',
163                'return "content LIKE \'%$s%\'";'
164                ),
165              preg_split('/[\s,;]+/', $_GET['keyword'] )
166              )
167      ).
168    ')';
169}
170
171$page['where_clauses'][] = $since_options[$page['since']]['clause'];
172
173// which status to filter on ?
174if ( !is_admin() )
175{
176  $page['where_clauses'][] = 'validated=\'true\'';
177}
178
179$page['where_clauses'][] = get_sql_condition_FandF
180  (
181    array
182      (
183        'forbidden_categories' => 'category_id',
184        'visible_categories' => 'category_id',
185        'visible_images' => 'ic.image_id'
186      ),
187    '', true
188  );
189
190// +-----------------------------------------------------------------------+
191// |                         comments management                           |
192// +-----------------------------------------------------------------------+
193
194$comment_id = null;
195$action = null;
196
197$actions = array('delete', 'validate', 'edit');
198foreach ($actions as $loop_action)
199{
200  if (isset($_GET[$loop_action]))
201  {
202    $action = $loop_action;
203    check_input_parameter($action, $_GET, false, PATTERN_ID);
204    $comment_id = $_GET[$action];
205    break;
206  }
207}
208
209if (isset($action))
210{
211  $comment_author_id = get_comment_author_id($comment_id);
212
213  if (can_manage_comment($action, $comment_author_id))
214  {
215    $perform_redirect = false;
216
217    if ('delete' == $action)
218    {
219      check_pwg_token();
220      delete_user_comment($comment_id);
221      $perform_redirect = true;
222    }
223
224    if ('validate' == $action)
225    {
226      check_pwg_token();
227      validate_user_comment($comment_id);
228      $perform_redirect = true;
229    }
230
231    if ('edit' == $action)
232    {
233      if (!empty($_POST['content']))
234      {
235        check_pwg_token();
236        update_user_comment(
237          array(
238            'comment_id' => $_GET['edit'],
239            'image_id' => $_POST['image_id'],
240            'content' => $_POST['content']
241            ),
242          $_POST['key']
243          );
244
245        $perform_redirect = true;
246      }
247      else
248      {
249        $edit_comment = $_GET['edit'];
250      }
251    }
252
253    if ($perform_redirect)
254    {
255      $redirect_url =
256        PHPWG_ROOT_PATH
257        .'comments.php'
258        .get_query_string_diff(array('delete','edit','validate','pwg_token'));
259
260      redirect($redirect_url);
261    }
262  }
263}
264
265// +-----------------------------------------------------------------------+
266// |                       page header and options                         |
267// +-----------------------------------------------------------------------+
268
269$title= l10n('User comments');
270$page['body_id'] = 'theCommentsPage';
271
272$template->set_filenames(array('comments'=>'comments.tpl'));
273$template->assign(
274  array(
275    'F_ACTION'=>PHPWG_ROOT_PATH.'comments.php',
276    'F_KEYWORD'=> @htmlspecialchars(stripslashes($_GET['keyword'], ENT_QUOTES, 'utf-8')),
277    'F_AUTHOR'=> @htmlspecialchars(stripslashes($_GET['author'], ENT_QUOTES, 'utf-8')),
278    )
279  );
280
281// +-----------------------------------------------------------------------+
282// |                          form construction                            |
283// +-----------------------------------------------------------------------+
284
285// Search in a particular category
286$blockname = 'categories';
287
288$query = '
289SELECT id, name, uppercats, global_rank
290  FROM '.CATEGORIES_TABLE.'
291'.get_sql_condition_FandF
292  (
293    array
294      (
295        'forbidden_categories' => 'id',
296        'visible_categories' => 'id'
297      ),
298    'WHERE'
299  ).'
300;';
301display_select_cat_wrapper($query, array(@$_GET['cat']), $blockname, true);
302
303// Filter on recent comments...
304$tpl_var=array();
305foreach ($since_options as $id => $option)
306{
307  $tpl_var[ $id ] = $option['label'];
308}
309$template->assign( 'since_options', $tpl_var);
310$template->assign( 'since_options_selected', $page['since']);
311
312// Sort by
313$template->assign( 'sort_by_options', $sort_by);
314$template->assign( 'sort_by_options_selected', $page['sort_by']);
315
316// Sorting order
317$template->assign( 'sort_order_options', $sort_order);
318$template->assign( 'sort_order_options_selected', $page['sort_order']);
319
320
321// Number of items
322$blockname = 'items_number_option';
323$tpl_var=array();
324foreach ($items_number as $option)
325{
326  $tpl_var[ $option ] = is_numeric($option) ? $option : l10n($option);
327}
328$template->assign( 'item_number_options', $tpl_var);
329$template->assign( 'item_number_options_selected', $page['items_number']);
330
331
332// +-----------------------------------------------------------------------+
333// |                            navigation bar                             |
334// +-----------------------------------------------------------------------+
335
336if (isset($_GET['start']) and is_numeric($_GET['start']))
337{
338  $start = $_GET['start'];
339}
340else
341{
342  $start = 0;
343}
344
345$query = '
346SELECT COUNT(DISTINCT(com.id))
347  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
348    INNER JOIN '.COMMENTS_TABLE.' AS com
349    ON ic.image_id = com.image_id
350    LEFT JOIN '.USERS_TABLE.' As u
351    ON u.'.$conf['user_fields']['id'].' = com.author_id
352  WHERE '.implode('
353    AND ', $page['where_clauses']).'
354;';
355list($counter) = pwg_db_fetch_row(pwg_query($query));
356
357$url = PHPWG_ROOT_PATH
358    .'comments.php'
359  .get_query_string_diff(array('start','delete','validate','pwg_token'));
360
361$navbar = create_navigation_bar($url,
362                                $counter,
363                                $start,
364                                $page['items_number'],
365                                '');
366
367$template->assign('navbar', $navbar);
368
369$url_self = PHPWG_ROOT_PATH
370    .'comments.php'
371  .get_query_string_diff(array('edit','delete','validate','pwg_token'));
372
373// +-----------------------------------------------------------------------+
374// |                        last comments display                          |
375// +-----------------------------------------------------------------------+
376
377$comments = array();
378$element_ids = array();
379$category_ids = array();
380
381$query = '
382SELECT com.id AS comment_id,
383       com.image_id,
384       com.author,
385       com.author_id,
386       u.'.$conf['user_fields']['email'].' AS user_email,
387       com.email,
388       com.date,
389       com.website_url,
390       com.content,
391       com.validated
392  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
393    INNER JOIN '.COMMENTS_TABLE.' AS com
394    ON ic.image_id = com.image_id
395    LEFT JOIN '.USERS_TABLE.' As u
396    ON u.'.$conf['user_fields']['id'].' = com.author_id
397  WHERE '.implode('
398    AND ', $page['where_clauses']).'
399  GROUP BY comment_id,
400       com.image_id,
401       com.author,
402       com.author_id,
403       com.date,
404       com.content,
405       com.validated
406  ORDER BY '.$page['sort_by'].' '.$page['sort_order'];
407if ('all' != $page['items_number'])
408{
409  $query.= '
410  LIMIT '.$page['items_number'].' OFFSET '.$start;
411}
412$query.= '
413;';
414$result = pwg_query($query);
415while ($row = pwg_db_fetch_assoc($result))
416{
417  array_push($comments, $row);
418  array_push($element_ids, $row['image_id']);
419}
420
421if (count($comments) > 0)
422{
423  // retrieving element informations
424  $elements = array();
425  $query = '
426SELECT *
427  FROM '.IMAGES_TABLE.'
428  WHERE id IN ('.implode(',', $element_ids).')
429;';
430  $result = pwg_query($query);
431  while ($row = pwg_db_fetch_assoc($result))
432  {
433    $elements[$row['id']] = $row;
434  }
435
436  // retrieving category informations
437  $query = '
438SELECT c.id, name, permalink, uppercats, com.id as comment_id
439  FROM '.CATEGORIES_TABLE.' AS c
440  LEFT JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
441  ON c.id=ic.category_id
442  LEFT JOIN '.COMMENTS_TABLE.' AS com
443  ON ic.image_id=com.image_id
444  '.get_sql_condition_FandF
445    (
446      array
447      (
448        'forbidden_categories' => 'c.id',
449        'visible_categories' => 'c.id'
450       ),
451      'WHERE'
452     ).'
453;';
454  $categories = hash_from_query($query, 'comment_id');
455
456  foreach ($comments as $comment)
457  {
458    if (!empty($elements[$comment['image_id']]['name']))
459    {
460      $name=$elements[$comment['image_id']]['name'];
461    }
462    else
463    {
464      $name=get_name_from_file($elements[$comment['image_id']]['file']);
465    }
466
467    // source of the thumbnail picture
468    $src_image = new SrcImage($elements[$comment['image_id']]);
469
470    // link to the full size picture
471    $url = make_picture_url(
472      array(
473        'category' => $categories[ $comment['comment_id'] ],
474        'image_id' => $comment['image_id'],
475        'image_file' => $elements[$comment['image_id']]['file'],
476        )
477      );
478     
479    $email = null;
480    if (!empty($comment['user_email']))
481    {
482      $email = $comment['user_email'];
483    }
484    else if (!empty($comment['email']))
485    {
486      $email = $comment['email'];
487    }
488
489    $tpl_comment = array(
490      'ID' => $comment['comment_id'],
491      'U_PICTURE' => $url,
492      'src_image' => $src_image,
493      'ALT' => $name,
494      'AUTHOR' => trigger_event('render_comment_author', $comment['author']),
495      'WEBSITE_URL' => $comment['website_url'],
496      'DATE'=>format_date($comment['date'], true),
497      'CONTENT'=>trigger_event('render_comment_content',$comment['content']),
498      );
499     
500    if (is_admin())
501    {
502      $tpl_comment['EMAIL'] = $email;
503    }
504
505    if (can_manage_comment('delete', $comment['author_id']))
506    {
507      $url =
508        get_root_url()
509        .'comments.php'
510        .get_query_string_diff(array('delete','validate','edit', 'pwg_token'));
511
512      $tpl_comment['U_DELETE'] = add_url_params(
513        $url,
514        array(
515          'delete' => $comment['comment_id'],
516          'pwg_token' => get_pwg_token(),
517          )
518        );
519    }
520
521    if (can_manage_comment('edit', $comment['author_id']))
522    {
523      $url =
524        get_root_url()
525        .'comments.php'
526        .get_query_string_diff(array('edit', 'delete','validate', 'pwg_token'));
527
528      $tpl_comment['U_EDIT'] = add_url_params(
529        $url,
530        array(
531          'edit' => $comment['comment_id']
532          )
533        );
534
535      if (isset($edit_comment) and ($comment['comment_id'] == $edit_comment))
536      {
537        $tpl_comment['IN_EDIT'] = true;
538        $key = get_ephemeral_key(2, $comment['image_id']);
539        $tpl_comment['KEY'] = $key;
540        $tpl_comment['IMAGE_ID'] = $comment['image_id'];
541        $tpl_comment['CONTENT'] = $comment['content'];
542        $tpl_comment['PWG_TOKEN'] = get_pwg_token();
543        $tpl_comment['U_CANCEL'] = $url_self;
544      }
545    }
546
547    if (can_manage_comment('validate', $comment['author_id']))
548    {
549      if ('true' != $comment['validated'])
550      {
551        $tpl_comment['U_VALIDATE'] = add_url_params(
552          $url,
553          array(
554            'validate'=> $comment['comment_id'],
555            'pwg_token' => get_pwg_token(),
556            )
557          );
558      }
559    }
560    $template->append('comments', $tpl_comment);
561  }
562}
563
564$derivative_params = trigger_event('get_comments_derivative_params', ImageStdParams::get_by_type(IMG_THUMB) );
565$template->assign( 'derivative_params', $derivative_params );
566
567// include menubar
568$themeconf = $template->get_template_vars('themeconf');
569if (!isset($themeconf['hide_menu_on']) OR !in_array('theCommentsPage', $themeconf['hide_menu_on']))
570{
571  include( PHPWG_ROOT_PATH.'include/menubar.inc.php');
572}
573
574// +-----------------------------------------------------------------------+
575// |                           html code display                           |
576// +-----------------------------------------------------------------------+
577include(PHPWG_ROOT_PATH.'include/page_header.php');
578trigger_action('loc_end_comments');
579include(PHPWG_ROOT_PATH.'include/page_messages.php');
580$template->pparse('comments');
581include(PHPWG_ROOT_PATH.'include/page_tail.php');
582?>
Note: See TracBrowser for help on using the repository browser.