source: trunk/comments.php @ 1536

Last change on this file since 1536 was 1536, checked in by nikrou, 18 years ago

fix bug 518: anyone can delete or validate a comment
comment_id must be int: use of intval function to use it in the query.

svn merge -r1534:1535 from branch 1.6 into trunk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2006-08-14 20:57:50 +0000 (Mon, 14 Aug 2006) $
10// | last modifier : $Author: nikrou $
11// | revision      : $Revision: 1536 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28// +-----------------------------------------------------------------------+
29// |                           initialization                              |
30// +-----------------------------------------------------------------------+
31if (!defined('IN_ADMIN'))
32{
33  define('PHPWG_ROOT_PATH','./');
34  include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
35}
36
37// +-----------------------------------------------------------------------+
38// | Check Access and exit when user status is not ok                      |
39// +-----------------------------------------------------------------------+
40check_status(ACCESS_GUEST);
41
42$sort_order = array(
43  'descending' => 'DESC',
44  'ascending' => 'ASC'
45  );
46
47// sort_by : database fields proposed for sorting comments list
48$sort_by = array(
49  'date' => 'comment date',
50  'image_id' => 'picture'
51  );
52
53// items_number : list of number of items to display per page
54$items_number = array(5,10,20,50,'all');
55
56// since when display comments ?
57//
58$since_options = array(
59  1 => array('label' => l10n('today'),
60             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 1 DAY)'),
61  2 => array('label' => sprintf(l10n('last %d days'), 7),
62             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 7 DAY)'),
63  3 => array('label' => sprintf(l10n('last %d days'), 30),
64             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 30 DAY)'),
65  4 => array('label' => l10n('the beginning'),
66             'clause' => '1=1') // stupid but generic
67  );
68
69$page['since'] = isset($_GET['since']) ? $_GET['since'] : 3;
70
71// on which field sorting
72//
73$page['sort_by'] = 'date';
74// if the form was submitted, it overloads default behaviour
75if (isset($_GET['sort_by']))
76{
77  $page['sort_by'] = $_GET['sort_by'];
78}
79
80// order to sort
81//
82$page['sort_order'] = $sort_order['descending'];
83// if the form was submitted, it overloads default behaviour
84if (isset($_GET['sort_order']))
85{
86  $page['sort_order'] = $sort_order[$_GET['sort_order']];
87}
88
89// number of items to display
90//
91$page['items_number'] = 5;
92if (isset($_GET['items_number']))
93{
94  $page['items_number'] = $_GET['items_number'];
95}
96
97// which category to filter on ?
98$page['cat_clause'] = '1=1';
99if (isset($_GET['cat']) and 0 != $_GET['cat'])
100{
101  $page['cat_clause'] =
102    'category_id IN ('.implode(',', get_subcat_ids(array($_GET['cat']))).')';
103}
104
105// search a particular author
106$page['author_clause'] = '1=1';
107if (isset($_GET['author']) and !empty($_GET['author']))
108{
109  if (function_exists('mysql_real_escape_string'))
110  {
111    $author = mysql_real_escape_string($_GET['author']);
112  }
113  else
114  {
115    $author = mysql_escape_string($_GET['author']);
116  }
117
118  $page['author_clause'] = 'author = \''.$author.'\'';
119}
120
121// search a substring among comments content
122$page['keyword_clause'] = '1=1';
123if (isset($_GET['keyword']) and !empty($_GET['keyword']))
124{
125  if (function_exists('mysql_real_escape_string'))
126  {
127    $keyword = mysql_real_escape_string($_GET['keyword']);
128  }
129  else
130  {
131    $keyword = mysql_escape_string($_GET['keyword']);
132  }
133  $page['keyword_clause'] =
134    '('.
135    implode(' AND ',
136            array_map(
137              create_function(
138                '$s',
139                'return "content LIKE \'%$s%\'";'
140                ),
141              preg_split('/[\s,;]+/', $keyword)
142              )
143      ).
144    ')';
145}
146
147// +-----------------------------------------------------------------------+
148// |                         comments management                           |
149// +-----------------------------------------------------------------------+
150// comments deletion
151if (isset($_POST['delete']) and count($_POST['comment_id']) > 0 and is_admin())
152{
153  $_POST['comment_id'] = array_map('intval', $_POST['comment_id']);
154  $query = '
155DELETE FROM '.COMMENTS_TABLE.'
156  WHERE id IN ('.implode(',', $_POST['comment_id']).')
157;';
158  pwg_query($query);
159}
160// comments validation
161if (isset($_POST['validate']) and count($_POST['comment_id']) > 0 
162   and is_admin())
163{
164  $_POST['comment_id'] = array_map('intval', $_POST['comment_id']);
165  $query = '
166UPDATE '.COMMENTS_TABLE.'
167  SET validated = \'true\'
168    , validation_date = NOW()
169  WHERE id IN ('.implode(',', $_POST['comment_id']).')
170;';
171  pwg_query($query);
172}
173// +-----------------------------------------------------------------------+
174// |                       page header and options                         |
175// +-----------------------------------------------------------------------+
176
177$title= l10n('title_comments');
178$page['body_id'] = 'theCommentsPage';
179include(PHPWG_ROOT_PATH.'include/page_header.php');
180
181$template->set_filenames(array('comments'=>'comments.tpl'));
182$template->assign_vars(
183  array(
184    'L_COMMENT_TITLE' => $title,
185
186    'F_ACTION'=>PHPWG_ROOT_PATH.'comments.php',
187    'F_KEYWORD'=>@$_GET['keyword'],
188    'F_AUTHOR'=>@$_GET['author'],
189
190    'U_HOME' => make_index_url(),
191    )
192  );
193
194// +-----------------------------------------------------------------------+
195// |                          form construction                            |
196// +-----------------------------------------------------------------------+
197
198// Search in a particular category
199$blockname = 'category';
200
201$template->assign_block_vars(
202  $blockname,
203  array('SELECTED' => '',
204        'VALUE'=> 0,
205        'OPTION' => '------------'
206    ));
207
208$query = '
209SELECT id,name,uppercats,global_rank
210  FROM '.CATEGORIES_TABLE;
211if ($user['forbidden_categories'] != '')
212{
213  $query.= '
214    WHERE id NOT IN ('.$user['forbidden_categories'].')';
215}
216$query.= '
217;';
218display_select_cat_wrapper($query, array(@$_GET['cat']), $blockname, true);
219
220// Filter on recent comments...
221$blockname = 'since_option';
222
223foreach ($since_options as $id => $option)
224{
225  $selected = ($id == $page['since']) ? 'selected="selected"' : '';
226
227  $template->assign_block_vars(
228    $blockname,
229    array('SELECTED' => $selected,
230          'VALUE'=> $id,
231          'CONTENT' => $option['label']
232      ));
233}
234
235// Sort by
236$blockname = 'sort_by_option';
237
238foreach ($sort_by as $key => $value)
239{
240  $selected = ($key == $page['sort_by']) ? 'selected="selected"' : '';
241
242  $template->assign_block_vars(
243    $blockname,
244    array('SELECTED' => $selected,
245          'VALUE'=> $key,
246          'CONTENT' => l10n($value)
247      ));
248}
249
250// Sorting order
251$blockname = 'sort_order_option';
252
253foreach (array_keys($sort_order) as $option)
254{
255  $selected = ($option == $page['sort_order']) ? 'selected="selected"' : '';
256
257  $template->assign_block_vars(
258    $blockname,
259    array('SELECTED' => $selected,
260          'VALUE'=> $option,
261          'CONTENT' => l10n($option)
262      ));
263}
264
265// Number of items
266$blockname = 'items_number_option';
267
268foreach ($items_number as $option)
269{
270  $selected = ($option == $page['items_number']) ? 'selected="selected"' : '';
271
272  $template->assign_block_vars(
273    $blockname,
274    array('SELECTED' => $selected,
275          'VALUE'=> $option,
276          'CONTENT' => is_numeric($option) ? $option : l10n($option)
277      ));
278}
279
280// +-----------------------------------------------------------------------+
281// |                            navigation bar                             |
282// +-----------------------------------------------------------------------+
283
284if (isset($_GET['start']) and is_numeric($_GET['start']))
285{
286  $start = $_GET['start'];
287}
288else
289{
290  $start = 0;
291}
292
293$query = '
294SELECT COUNT(DISTINCT(id))
295  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
296    INNER JOIN '.COMMENTS_TABLE.' AS com
297    ON ic.image_id = com.image_id
298  WHERE '.$since_options[$page['since']]['clause'].'
299    AND '.$page['cat_clause'].'
300    AND '.$page['author_clause'].'
301    AND '.$page['keyword_clause'];
302if ($user['forbidden_categories'] != '')
303{
304  $query.= '
305    AND category_id NOT IN ('.$user['forbidden_categories'].')';
306}
307$query.= '
308;';
309list($counter) = mysql_fetch_row(pwg_query($query));
310
311$url = PHPWG_ROOT_PATH.'comments.php?t=1'.get_query_string_diff(array('start'));
312
313$navbar = create_navigation_bar($url,
314                                $counter,
315                                $start,
316                                $page['items_number'],
317                                '');
318
319$template->assign_vars(array('NAVBAR' => $navbar));
320
321// +-----------------------------------------------------------------------+
322// |                        last comments display                          |
323// +-----------------------------------------------------------------------+
324
325$comments = array();
326$element_ids = array();
327$category_ids = array();
328
329$query = '
330SELECT com.id AS comment_id
331     , com.image_id
332     , ic.category_id
333     , com.author
334     , com.date
335     , com.content
336     , com.id AS comment_id
337  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
338    INNER JOIN '.COMMENTS_TABLE.' AS com
339    ON ic.image_id = com.image_id
340  WHERE '.$since_options[$page['since']]['clause'].'
341    AND '.$page['cat_clause'].'
342    AND '.$page['author_clause'].'
343    AND '.$page['keyword_clause'];
344if ($user['forbidden_categories'] != '')
345{
346  $query.= '
347    AND category_id NOT IN ('.$user['forbidden_categories'].')';
348}
349$query.= '
350  GROUP BY comment_id
351  ORDER BY '.$page['sort_by'].' '.$page['sort_order'];
352if ('all' != $page['items_number'])
353{
354  $query.= '
355  LIMIT '.$start.','.$page['items_number'];
356}
357$query.= '
358;';
359$result = pwg_query($query);
360while ($row = mysql_fetch_array($result))
361{
362  array_push($comments, $row);
363  array_push($element_ids, $row['image_id']);
364  array_push($category_ids, $row['category_id']);
365}
366
367if (count($comments) > 0)
368{
369  // retrieving element informations
370  $elements = array();
371  $query = '
372SELECT id, name, file, path, tn_ext
373  FROM '.IMAGES_TABLE.'
374  WHERE id IN ('.implode(',', $element_ids).')
375;';
376  $result = pwg_query($query);
377  while ($row = mysql_fetch_array($result))
378  {
379    $elements[$row['id']] = $row;
380  }
381
382  // retrieving category informations
383  $categories = array();
384  $query = '
385SELECT id, uppercats
386  FROM '.CATEGORIES_TABLE.'
387  WHERE id IN ('.implode(',', $category_ids).')
388;';
389  $result = pwg_query($query);
390  while ($row = mysql_fetch_array($result))
391  {
392    $categories[$row['id']] = $row;
393  }
394
395  foreach ($comments as $comment)
396  {
397    // name of the picture
398    $name = get_cat_display_name_cache(
399      $categories[$comment['category_id']]['uppercats'], null, false);
400    $name.= $conf['level_separator'];
401    if (!empty($elements[$comment['image_id']]['name']))
402    {
403      $name.= $elements[$comment['image_id']]['name'];
404    }
405    else
406    {
407      $name.= get_name_from_file($elements[$comment['image_id']]['file']);
408    }
409
410    // source of the thumbnail picture
411    $thumbnail_src = get_thumbnail_src(
412      $elements[$comment['image_id']]['path'],
413      @$elements[$comment['image_id']]['tn_ext']
414      );
415
416    // link to the full size picture
417    $url = make_picture_url(
418            array(
419              'category' => $comment['category_id'],
420              'image_id' => $comment['image_id'],
421              'image_file' => $elements[$comment['image_id']]['file'],
422            )
423          );
424
425    $template->assign_block_vars(
426      'picture',
427      array(
428        'TITLE_IMG'=>$name,
429        'I_THUMB'=>$thumbnail_src,
430        'U_THUMB'=>$url
431        ));
432
433    $author = $comment['author'];
434    if (empty($comment['author']))
435    {
436      $author = l10n('guest');
437    }
438
439    $template->assign_block_vars(
440      'comment',
441      array(
442        'U_PICTURE' => $url,
443        'TN_SRC' => $thumbnail_src,
444        'AUTHOR' => $author,
445        'DATE'=>format_date($comment['date'],'mysql_datetime',true),
446        'CONTENT'=>parse_comment_content($comment['content']),
447        ));
448  }
449}
450// +-----------------------------------------------------------------------+
451// |                           html code display                           |
452// +-----------------------------------------------------------------------+
453if (defined('IN_ADMIN'))
454{
455  $template->assign_var_from_handle('ADMIN_CONTENT', 'comments');
456}
457else
458{
459  $template->assign_block_vars('title',array());
460  $template->parse('comments');
461  include(PHPWG_ROOT_PATH.'include/page_tail.php');
462}
463?>
Note: See TracBrowser for help on using the repository browser.