source: branches/2.0/comments.php @ 4183

Last change on this file since 4183 was 4183, checked in by nikrou, 15 years ago

bug 1220 : merge from trunk
fix regression when search by author or keyword contains quote

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 KB
RevLine 
[166]1<?php
[354]2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[3046]5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[166]23
[579]24// +-----------------------------------------------------------------------+
25// |                           initialization                              |
26// +-----------------------------------------------------------------------+
[1598]27define('PHPWG_ROOT_PATH','./');
28include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
[345]29
[1072]30// +-----------------------------------------------------------------------+
31// | Check Access and exit when user status is not ok                      |
32// +-----------------------------------------------------------------------+
33check_status(ACCESS_GUEST);
34
[796]35$sort_order = array(
[2223]36  'DESC' => l10n('descending'),
37  'ASC'  => l10n('ascending')
[796]38  );
39
40// sort_by : database fields proposed for sorting comments list
41$sort_by = array(
[2223]42  'date' => l10n('comment date'),
43  'image_id' => l10n('picture')
[796]44  );
45
46// items_number : list of number of items to display per page
47$items_number = array(5,10,20,50,'all');
48
49// since when display comments ?
50//
51$since_options = array(
52  1 => array('label' => l10n('today'),
53             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 1 DAY)'),
54  2 => array('label' => sprintf(l10n('last %d days'), 7),
55             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 7 DAY)'),
56  3 => array('label' => sprintf(l10n('last %d days'), 30),
57             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 30 DAY)'),
58  4 => array('label' => l10n('the beginning'),
59             'clause' => '1=1') // stupid but generic
60  );
61
[4140]62if (!empty($_GET['since']) && is_numeric($_GET['since']))
63{
64  $page['since'] = $_GET['since'];
65}
66else
67{
68  $page['since'] = 4;
69}
[796]70
71// on which field sorting
72//
73$page['sort_by'] = 'date';
74// if the form was submitted, it overloads default behaviour
[2755]75if (isset($_GET['sort_by']) and isset($sort_by[$_GET['sort_by']]) )
[393]76{
[796]77  $page['sort_by'] = $_GET['sort_by'];
[393]78}
[796]79
80// order to sort
81//
[2223]82$page['sort_order'] = 'DESC';
[796]83// if the form was submitted, it overloads default behaviour
[2755]84if (isset($_GET['sort_order']) and isset($sort_order[$_GET['sort_order']]))
[393]85{
[2223]86  $page['sort_order'] = $_GET['sort_order'];
[393]87}
[796]88
89// number of items to display
90//
[1814]91$page['items_number'] = 10;
[796]92if (isset($_GET['items_number']))
93{
94  $page['items_number'] = $_GET['items_number'];
95}
[3519]96if ( !is_numeric($page['items_number']) and $page['items_number']!='all' ) 
97{
98  $page['items_number'] = 10;
99}
[796]100
[1716]101$page['where_clauses'] = array();
102
[796]103// which category to filter on ?
104if (isset($_GET['cat']) and 0 != $_GET['cat'])
105{
[1716]106  $page['where_clauses'][] =
[796]107    'category_id IN ('.implode(',', get_subcat_ids(array($_GET['cat']))).')';
108}
109
110// search a particular author
[4140]111if (!empty($_GET['author']))
[796]112{
[1716]113  $page['where_clauses'][] = 'com.author = \''.$_GET['author'].'\'';
[796]114}
115
116// search a substring among comments content
[4140]117if (!empty($_GET['keyword']))
[796]118{
[1716]119  $page['where_clauses'][] =
[796]120    '('.
121    implode(' AND ',
122            array_map(
123              create_function(
124                '$s',
125                'return "content LIKE \'%$s%\'";'
126                ),
[2012]127              preg_split('/[\s,;]+/', $_GET['keyword'] )
[796]128              )
129      ).
130    ')';
131}
132
[1716]133$page['where_clauses'][] = $since_options[$page['since']]['clause'];
134
[1598]135// which status to filter on ?
[1716]136if ( !is_admin() )
[1598]137{
[1716]138  $page['where_clauses'][] = 'validated="true"';
[1598]139}
140
[1716]141$page['where_clauses'][] = get_sql_condition_FandF
142  (
143    array
144      (
145        'forbidden_categories' => 'category_id',
146        'visible_categories' => 'category_id',
147        'visible_images' => 'ic.image_id'
148      ),
149    '', true
150  );
[1598]151
[579]152// +-----------------------------------------------------------------------+
153// |                         comments management                           |
154// +-----------------------------------------------------------------------+
[1617]155if (isset($_GET['delete']) and is_numeric($_GET['delete'])
156      and !is_adviser() )
157{// comments deletion
158  check_status(ACCESS_ADMINISTRATOR);
159  $query = '
[579]160DELETE FROM '.COMMENTS_TABLE.'
[1598]161  WHERE id='.$_GET['delete'].'
[579]162;';
[1617]163  pwg_query($query);
164}
[1598]165
[1617]166if (isset($_GET['validate']) and is_numeric($_GET['validate'])
167      and !is_adviser() )
168{  // comments validation
169  check_status(ACCESS_ADMINISTRATOR);
170  $query = '
[579]171UPDATE '.COMMENTS_TABLE.'
172  SET validated = \'true\'
[1617]173  , validation_date = NOW()
[1598]174  WHERE id='.$_GET['validate'].'
[579]175;';
[1617]176  pwg_query($query);
[579]177}
[1617]178
[579]179// +-----------------------------------------------------------------------+
180// |                       page header and options                         |
181// +-----------------------------------------------------------------------+
[355]182
[2268]183$title= l10n('User comments');
[850]184$page['body_id'] = 'theCommentsPage';
185
[579]186$template->set_filenames(array('comments'=>'comments.tpl'));
[2223]187$template->assign(
[579]188  array(
[796]189    'F_ACTION'=>PHPWG_ROOT_PATH.'comments.php',
[4183]190    'F_KEYWORD'=> @htmlspecialchars(stripslashes($_GET['keyword'], ENT_QUOTES, 'utf-8')),
191    'F_AUTHOR'=> @htmlspecialchars(stripslashes($_GET['author'], ENT_QUOTES, 'utf-8')),
[579]192    )
193  );
[355]194
[796]195// +-----------------------------------------------------------------------+
196// |                          form construction                            |
197// +-----------------------------------------------------------------------+
198
199// Search in a particular category
[2223]200$blockname = 'categories';
[796]201
202$query = '
[1861]203SELECT id, name, uppercats, global_rank
[1677]204  FROM '.CATEGORIES_TABLE.'
205'.get_sql_condition_FandF
206  (
207    array
208      (
209        'forbidden_categories' => 'id',
210        'visible_categories' => 'id'
211      ),
212    'WHERE'
213  ).'
[796]214;';
215display_select_cat_wrapper($query, array(@$_GET['cat']), $blockname, true);
216
217// Filter on recent comments...
[2223]218$tpl_var=array();
[796]219foreach ($since_options as $id => $option)
220{
[2223]221  $tpl_var[ $id ] = $option['label'];
[355]222}
[2223]223$template->assign( 'since_options', $tpl_var);
224$template->assign( 'since_options_selected', $page['since']);
[796]225
226// Sort by
[2223]227$template->assign( 'sort_by_options', $sort_by);
228$template->assign( 'sort_by_options_selected', $page['sort_by']);
[796]229
230// Sorting order
[2223]231$template->assign( 'sort_order_options', $sort_order);
232$template->assign( 'sort_order_options_selected', $page['sort_order']);
[796]233
234
235// Number of items
236$blockname = 'items_number_option';
[2223]237$tpl_var=array();
[796]238foreach ($items_number as $option)
239{
[2223]240  $tpl_var[ $option ] = is_numeric($option) ? $option : l10n($option);
[796]241}
[2223]242$template->assign( 'item_number_options', $tpl_var);
243$template->assign( 'item_number_options_selected', $page['items_number']);
[796]244
[2223]245
[579]246// +-----------------------------------------------------------------------+
[796]247// |                            navigation bar                             |
248// +-----------------------------------------------------------------------+
249
250if (isset($_GET['start']) and is_numeric($_GET['start']))
251{
252  $start = $_GET['start'];
253}
254else
255{
256  $start = 0;
257}
258
259$query = '
[4183]260SELECT COUNT(DISTINCT(com.id))
[796]261  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
[4140]262    INNER JOIN '.COMMENTS_TABLE.' AS com   
[796]263    ON ic.image_id = com.image_id
[4140]264    LEFT JOIN '.USERS_TABLE.' As u
265    ON u.'.$conf['user_fields']['id'].' = com.author_id
[1716]266  WHERE '.implode('
267    AND ', $page['where_clauses']).'
[796]268;';
269list($counter) = mysql_fetch_row(pwg_query($query));
270
[1598]271$url = PHPWG_ROOT_PATH
272    .'comments.php'
273    .get_query_string_diff(array('start','delete','validate'));
[796]274
275$navbar = create_navigation_bar($url,
276                                $counter,
277                                $start,
278                                $page['items_number'],
279                                '');
280
[2223]281$template->assign('NAVBAR', $navbar);
[796]282
283// +-----------------------------------------------------------------------+
[579]284// |                        last comments display                          |
285// +-----------------------------------------------------------------------+
[355]286
[796]287$comments = array();
288$element_ids = array();
289$category_ids = array();
290
[579]291$query = '
[796]292SELECT com.id AS comment_id
293     , com.image_id
294     , ic.category_id
295     , com.author
296     , com.date
297     , com.content
[1598]298     , com.validated
[796]299  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
300    INNER JOIN '.COMMENTS_TABLE.' AS com
301    ON ic.image_id = com.image_id
[4140]302    LEFT JOIN '.USERS_TABLE.' As u
303    ON u.'.$conf['user_fields']['id'].' = com.author_id
[1716]304  WHERE '.implode('
305    AND ', $page['where_clauses']).'
[796]306  GROUP BY comment_id
307  ORDER BY '.$page['sort_by'].' '.$page['sort_order'];
308if ('all' != $page['items_number'])
309{
310  $query.= '
311  LIMIT '.$start.','.$page['items_number'];
312}
313$query.= '
[579]314;';
[587]315$result = pwg_query($query);
[1598]316while ($row = mysql_fetch_assoc($result))
[393]317{
[796]318  array_push($comments, $row);
319  array_push($element_ids, $row['image_id']);
320  array_push($category_ids, $row['category_id']);
[393]321}
[796]322
323if (count($comments) > 0)
[579]324{
[796]325  // retrieving element informations
326  $elements = array();
[579]327  $query = '
[796]328SELECT id, name, file, path, tn_ext
[579]329  FROM '.IMAGES_TABLE.'
[796]330  WHERE id IN ('.implode(',', $element_ids).')
[579]331;';
[796]332  $result = pwg_query($query);
[1598]333  while ($row = mysql_fetch_assoc($result))
[579]334  {
[796]335    $elements[$row['id']] = $row;
[579]336  }
[721]337
[796]338  // retrieving category informations
[579]339  $query = '
[1866]340SELECT id, name, permalink, uppercats
[796]341  FROM '.CATEGORIES_TABLE.'
342  WHERE id IN ('.implode(',', $category_ids).')
343;';
[1866]344  $categories = hash_from_query($query, 'id');
[796]345
346  foreach ($comments as $comment)
[579]347  {
[796]348    if (!empty($elements[$comment['image_id']]['name']))
[166]349    {
[1598]350      $name=$elements[$comment['image_id']]['name'];
[166]351    }
[796]352    else
353    {
[1598]354      $name=get_name_from_file($elements[$comment['image_id']]['file']);
[796]355    }
[1090]356
[796]357    // source of the thumbnail picture
[1598]358    $thumbnail_src = get_thumbnail_url( $elements[$comment['image_id']] );
[1090]359
[796]360    // link to the full size picture
[1090]361    $url = make_picture_url(
362            array(
[1861]363              'category' => $categories[ $comment['category_id'] ],
[1090]364              'image_id' => $comment['image_id'],
365              'image_file' => $elements[$comment['image_id']]['file'],
366            )
367          );
368
[796]369    $author = $comment['author'];
370    if (empty($comment['author']))
[393]371    {
[796]372      $author = l10n('guest');
[166]373    }
[1090]374
[2223]375    $tpl_comment =
[796]376      array(
[1004]377        'U_PICTURE' => $url,
[848]378        'TN_SRC' => $thumbnail_src,
[1598]379        'ALT' => $name,
[2030]380        'AUTHOR' => trigger_event('render_comment_author', $author),
[3123]381        'DATE'=>format_date($comment['date'], true),
[1598]382        'CONTENT'=>trigger_event('render_comment_content',$comment['content']),
[2223]383        );
[1598]384
385    if ( is_admin() )
386    {
387      $url = get_root_url().'comments.php'.get_query_string_diff(array('delete','validate'));
[2223]388      $tpl_comment['U_DELETE'] = add_url_params($url,
[1598]389                          array('delete'=>$comment['comment_id'])
[2223]390                         );
391
[1598]392      if ($comment['validated'] != 'true')
393      {
[2223]394        $tpl_comment['U_VALIDATE'] = add_url_params($url,
[1598]395                            array('validate'=>$comment['comment_id'])
[2223]396                           );
[1598]397      }
398    }
[2223]399    $template->append('comments', $tpl_comment);
[166]400  }
[579]401}
402// +-----------------------------------------------------------------------+
403// |                           html code display                           |
404// +-----------------------------------------------------------------------+
[2107]405include(PHPWG_ROOT_PATH.'include/page_header.php');
[2223]406$template->pparse('comments');
[1598]407include(PHPWG_ROOT_PATH.'include/page_tail.php');
[2107]408?>
Note: See TracBrowser for help on using the repository browser.