source: trunk/comments.php @ 18117

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

feature 2747: Add triggers on all main pages

  • 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 
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       com.date,
387       com.website_url,
388       com.content,
389       com.validated
390  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
391    INNER JOIN '.COMMENTS_TABLE.' AS com
392    ON ic.image_id = com.image_id
393    LEFT JOIN '.USERS_TABLE.' As u
394    ON u.'.$conf['user_fields']['id'].' = com.author_id
395  WHERE '.implode('
396    AND ', $page['where_clauses']).'
397  GROUP BY comment_id,
398       com.image_id,
399       com.author,
400       com.author_id,
401       com.date,
402       com.content,
403       com.validated
404  ORDER BY '.$page['sort_by'].' '.$page['sort_order'];
405if ('all' != $page['items_number'])
406{
407  $query.= '
408  LIMIT '.$page['items_number'].' OFFSET '.$start;
409}
410$query.= '
411;';
412$result = pwg_query($query);
413while ($row = pwg_db_fetch_assoc($result))
414{
415  array_push($comments, $row);
416  array_push($element_ids, $row['image_id']);
417}
418
419if (count($comments) > 0)
420{
421  // retrieving element informations
422  $elements = array();
423  $query = '
424SELECT *
425  FROM '.IMAGES_TABLE.'
426  WHERE id IN ('.implode(',', $element_ids).')
427;';
428  $result = pwg_query($query);
429  while ($row = pwg_db_fetch_assoc($result))
430  {
431    $elements[$row['id']] = $row;
432  }
433
434  // retrieving category informations
435  $query = '
436SELECT c.id, name, permalink, uppercats, com.id as comment_id
437  FROM '.CATEGORIES_TABLE.' AS c
438  LEFT JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
439  ON c.id=ic.category_id
440  LEFT JOIN '.COMMENTS_TABLE.' AS com
441  ON ic.image_id=com.image_id
442  '.get_sql_condition_FandF
443    (
444      array
445      (
446        'forbidden_categories' => 'c.id',
447        'visible_categories' => 'c.id'
448       ),
449      'WHERE'
450     ).'
451;';
452  $categories = hash_from_query($query, 'comment_id');
453
454  foreach ($comments as $comment)
455  {
456    if (!empty($elements[$comment['image_id']]['name']))
457    {
458      $name=$elements[$comment['image_id']]['name'];
459    }
460    else
461    {
462      $name=get_name_from_file($elements[$comment['image_id']]['file']);
463    }
464
465    // source of the thumbnail picture
466    $src_image = new SrcImage($elements[$comment['image_id']]);
467
468    // link to the full size picture
469    $url = make_picture_url(
470      array(
471        'category' => $categories[ $comment['comment_id'] ],
472        'image_id' => $comment['image_id'],
473        'image_file' => $elements[$comment['image_id']]['file'],
474        )
475      );
476
477    $tpl_comment = array(
478      'ID' => $comment['comment_id'],
479      'U_PICTURE' => $url,
480      'src_image' => $src_image,
481      'ALT' => $name,
482      'AUTHOR' => trigger_event('render_comment_author', $comment['author']),
483      'WEBSITE_URL' => $comment['website_url'],
484      'DATE'=>format_date($comment['date'], true),
485      'CONTENT'=>trigger_event('render_comment_content',$comment['content']),
486      );
487
488    if (can_manage_comment('delete', $comment['author_id']))
489    {
490      $url =
491        get_root_url()
492        .'comments.php'
493        .get_query_string_diff(array('delete','validate','edit', 'pwg_token'));
494
495      $tpl_comment['U_DELETE'] = add_url_params(
496        $url,
497        array(
498          'delete' => $comment['comment_id'],
499          'pwg_token' => get_pwg_token(),
500          )
501        );
502    }
503
504    if (can_manage_comment('edit', $comment['author_id']))
505    {
506      $url =
507        get_root_url()
508        .'comments.php'
509        .get_query_string_diff(array('edit', 'delete','validate', 'pwg_token'));
510
511      $tpl_comment['U_EDIT'] = add_url_params(
512        $url,
513        array(
514          'edit' => $comment['comment_id']
515          )
516        );
517
518      if (isset($edit_comment) and ($comment['comment_id'] == $edit_comment))
519      {
520        $tpl_comment['IN_EDIT'] = true;
521        $key = get_ephemeral_key(2, $comment['image_id']);
522        $tpl_comment['KEY'] = $key;
523        $tpl_comment['IMAGE_ID'] = $comment['image_id'];
524        $tpl_comment['CONTENT'] = $comment['content'];
525        $tpl_comment['PWG_TOKEN'] = get_pwg_token();
526        $tpl_comment['U_CANCEL'] = $url_self;
527      }
528    }
529
530    if (can_manage_comment('validate', $comment['author_id']))
531    {
532      if ('true' != $comment['validated'])
533      {
534        $tpl_comment['U_VALIDATE'] = add_url_params(
535          $url,
536          array(
537            'validate'=> $comment['comment_id'],
538            'pwg_token' => get_pwg_token(),
539            )
540          );
541      }
542    }
543    $template->append('comments', $tpl_comment);
544  }
545}
546
547$derivative_params = trigger_event('get_comments_derivative_params', ImageStdParams::get_by_type(IMG_THUMB) );
548$template->assign( 'derivative_params', $derivative_params );
549
550// include menubar
551$themeconf = $template->get_template_vars('themeconf');
552if (!isset($themeconf['hide_menu_on']) OR !in_array('theCommentsPage', $themeconf['hide_menu_on']))
553{
554  include( PHPWG_ROOT_PATH.'include/menubar.inc.php');
555}
556
557// +-----------------------------------------------------------------------+
558// |                           html code display                           |
559// +-----------------------------------------------------------------------+
560include(PHPWG_ROOT_PATH.'include/page_header.php');
561trigger_action('loc_end_comments');
562include(PHPWG_ROOT_PATH.'include/page_messages.php');
563$template->pparse('comments');
564include(PHPWG_ROOT_PATH.'include/page_tail.php');
565?>
Note: See TracBrowser for help on using the repository browser.