source: trunk/comments.php @ 17351

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

feature 2380: add URL for user comment

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