source: branches/1.6/comments.php @ 6974

Last change on this file since 6974 was 1695, checked in by rub, 18 years ago

Fixed: HTML vulnerability (Cross Site Scripting).
Fixed: All comments are displayed on comments.php

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.6 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: 2007-01-03 23:27:32 +0000 (Wed, 03 Jan 2007) $
10// | last modifier : $Author: rub $
11// | revision      : $Revision: 1695 $
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// Only validated on 1.6.x
148// on 1.7, admin can see all because he can be validated or rejected comments
149$page['status_clause'] = 'validated="true"';
150
151// +-----------------------------------------------------------------------+
152// |                         comments management                           |
153// +-----------------------------------------------------------------------+
154// comments deletion
155if (isset($_POST['delete']) and count($_POST['comment_id']) > 0 and is_admin())
156{
157  $_POST['comment_id'] = array_map('intval', $_POST['comment_id']);
158  $query = '
159DELETE FROM '.COMMENTS_TABLE.'
160  WHERE id IN ('.implode(',', $_POST['comment_id']).')
161;';
162  pwg_query($query);
163}
164// comments validation
165if (isset($_POST['validate']) and count($_POST['comment_id']) > 0
166   and is_admin())
167{
168  $_POST['comment_id'] = array_map('intval', $_POST['comment_id']);
169  $query = '
170UPDATE '.COMMENTS_TABLE.'
171  SET validated = \'true\'
172    , validation_date = NOW()
173  WHERE id IN ('.implode(',', $_POST['comment_id']).')
174;';
175  pwg_query($query);
176}
177// +-----------------------------------------------------------------------+
178// |                       page header and options                         |
179// +-----------------------------------------------------------------------+
180
181$title= l10n('title_comments');
182$page['body_id'] = 'theCommentsPage';
183include(PHPWG_ROOT_PATH.'include/page_header.php');
184
185$template->set_filenames(array('comments'=>'comments.tpl'));
186$template->assign_vars(
187  array(
188    'L_COMMENT_TITLE' => $title,
189
190    'F_ACTION'=>PHPWG_ROOT_PATH.'comments.php',
191    'F_KEYWORD'=>@htmlentities($_GET['keyword']),
192    'F_AUTHOR'=>@htmlentities($_GET['author']),
193
194    'U_HOME' => make_index_url(),
195    )
196  );
197
198// +-----------------------------------------------------------------------+
199// |                          form construction                            |
200// +-----------------------------------------------------------------------+
201
202// Search in a particular category
203$blockname = 'category';
204
205$template->assign_block_vars(
206  $blockname,
207  array('SELECTED' => '',
208        'VALUE'=> 0,
209        'OPTION' => '------------'
210    ));
211
212$query = '
213SELECT id,name,uppercats,global_rank
214  FROM '.CATEGORIES_TABLE;
215if ($user['forbidden_categories'] != '')
216{
217  $query.= '
218    WHERE id NOT IN ('.$user['forbidden_categories'].')';
219}
220$query.= '
221;';
222display_select_cat_wrapper($query, array(@$_GET['cat']), $blockname, true);
223
224// Filter on recent comments...
225$blockname = 'since_option';
226
227foreach ($since_options as $id => $option)
228{
229  $selected = ($id == $page['since']) ? 'selected="selected"' : '';
230
231  $template->assign_block_vars(
232    $blockname,
233    array('SELECTED' => $selected,
234          'VALUE'=> $id,
235          'CONTENT' => $option['label']
236      ));
237}
238
239// Sort by
240$blockname = 'sort_by_option';
241
242foreach ($sort_by as $key => $value)
243{
244  $selected = ($key == $page['sort_by']) ? 'selected="selected"' : '';
245
246  $template->assign_block_vars(
247    $blockname,
248    array('SELECTED' => $selected,
249          'VALUE'=> $key,
250          'CONTENT' => l10n($value)
251      ));
252}
253
254// Sorting order
255$blockname = 'sort_order_option';
256
257foreach (array_keys($sort_order) as $option)
258{
259  $selected = ($option == $page['sort_order']) ? 'selected="selected"' : '';
260
261  $template->assign_block_vars(
262    $blockname,
263    array('SELECTED' => $selected,
264          'VALUE'=> $option,
265          'CONTENT' => l10n($option)
266      ));
267}
268
269// Number of items
270$blockname = 'items_number_option';
271
272foreach ($items_number as $option)
273{
274  $selected = ($option == $page['items_number']) ? 'selected="selected"' : '';
275
276  $template->assign_block_vars(
277    $blockname,
278    array('SELECTED' => $selected,
279          'VALUE'=> $option,
280          'CONTENT' => is_numeric($option) ? $option : l10n($option)
281      ));
282}
283
284// +-----------------------------------------------------------------------+
285// |                            navigation bar                             |
286// +-----------------------------------------------------------------------+
287
288if (isset($_GET['start']) and is_numeric($_GET['start']))
289{
290  $start = $_GET['start'];
291}
292else
293{
294  $start = 0;
295}
296
297$query = '
298SELECT COUNT(DISTINCT(id))
299  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
300    INNER JOIN '.COMMENTS_TABLE.' AS com
301    ON ic.image_id = com.image_id
302  WHERE '.$since_options[$page['since']]['clause'].'
303    AND '.$page['cat_clause'].'
304    AND '.$page['author_clause'].'
305    AND '.$page['keyword_clause'].'
306    AND '.$page['status_clause'];
307if ($user['forbidden_categories'] != '')
308{
309  $query.= '
310    AND category_id NOT IN ('.$user['forbidden_categories'].')';
311}
312$query.= '
313;';
314list($counter) = mysql_fetch_row(pwg_query($query));
315
316$url = PHPWG_ROOT_PATH.'comments.php'.get_query_string_diff(array('start'));
317
318$navbar = create_navigation_bar($url,
319                                $counter,
320                                $start,
321                                $page['items_number'],
322                                '');
323
324$template->assign_vars(array('NAVBAR' => $navbar));
325
326// +-----------------------------------------------------------------------+
327// |                        last comments display                          |
328// +-----------------------------------------------------------------------+
329
330$comments = array();
331$element_ids = array();
332$category_ids = array();
333
334$query = '
335SELECT com.id AS comment_id
336     , com.image_id
337     , ic.category_id
338     , com.author
339     , com.date
340     , com.content
341     , com.id AS comment_id
342  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
343    INNER JOIN '.COMMENTS_TABLE.' AS com
344    ON ic.image_id = com.image_id
345  WHERE '.$since_options[$page['since']]['clause'].'
346    AND '.$page['cat_clause'].'
347    AND '.$page['author_clause'].'
348    AND '.$page['keyword_clause'].'
349    AND '.$page['status_clause'];
350if ($user['forbidden_categories'] != '')
351{
352  $query.= '
353    AND category_id NOT IN ('.$user['forbidden_categories'].')';
354}
355$query.= '
356  GROUP BY comment_id
357  ORDER BY '.$page['sort_by'].' '.$page['sort_order'];
358if ('all' != $page['items_number'])
359{
360  $query.= '
361  LIMIT '.$start.','.$page['items_number'];
362}
363$query.= '
364;';
365$result = pwg_query($query);
366while ($row = mysql_fetch_array($result))
367{
368  array_push($comments, $row);
369  array_push($element_ids, $row['image_id']);
370  array_push($category_ids, $row['category_id']);
371}
372
373if (count($comments) > 0)
374{
375  // retrieving element informations
376  $elements = array();
377  $query = '
378SELECT id, name, file, path, tn_ext
379  FROM '.IMAGES_TABLE.'
380  WHERE id IN ('.implode(',', $element_ids).')
381;';
382  $result = pwg_query($query);
383  while ($row = mysql_fetch_array($result))
384  {
385    $elements[$row['id']] = $row;
386  }
387
388  // retrieving category informations
389  $categories = array();
390  $query = '
391SELECT id, name, uppercats
392  FROM '.CATEGORIES_TABLE.'
393  WHERE id IN ('.implode(',', $category_ids).')
394;';
395  $result = pwg_query($query);
396  while ($row = mysql_fetch_array($result))
397  {
398    $categories[$row['id']] = $row;
399  }
400
401  foreach ($comments as $comment)
402  {
403    // name of the picture
404    $name = get_cat_display_name_cache(
405      $categories[$comment['category_id']]['uppercats'], null, false);
406    $name.= $conf['level_separator'];
407    if (!empty($elements[$comment['image_id']]['name']))
408    {
409      $name.= $elements[$comment['image_id']]['name'];
410    }
411    else
412    {
413      $name.= get_name_from_file($elements[$comment['image_id']]['file']);
414    }
415
416    // source of the thumbnail picture
417    $thumbnail_src = get_thumbnail_src(
418      $elements[$comment['image_id']]['path'],
419      @$elements[$comment['image_id']]['tn_ext']
420      );
421
422    // link to the full size picture
423    $url = make_picture_url(
424            array(
425              'category' => $comment['category_id'],
426              'cat_name' => $categories[ $comment['category_id']] ['name'],
427              'image_id' => $comment['image_id'],
428              'image_file' => $elements[$comment['image_id']]['file'],
429            )
430          );
431
432    $template->assign_block_vars(
433      'picture',
434      array(
435        'TITLE_IMG'=>$name,
436        'I_THUMB'=>$thumbnail_src,
437        'U_THUMB'=>$url
438        ));
439
440    $author = $comment['author'];
441    if (empty($comment['author']))
442    {
443      $author = l10n('guest');
444    }
445
446    $template->assign_block_vars(
447      'comment',
448      array(
449        'U_PICTURE' => $url,
450        'TN_SRC' => $thumbnail_src,
451        'AUTHOR' => $author,
452        'DATE'=>format_date($comment['date'],'mysql_datetime',true),
453        'CONTENT'=>parse_comment_content($comment['content']),
454        ));
455  }
456}
457// +-----------------------------------------------------------------------+
458// |                           html code display                           |
459// +-----------------------------------------------------------------------+
460if (defined('IN_ADMIN'))
461{
462  $template->assign_var_from_handle('ADMIN_CONTENT', 'comments');
463}
464else
465{
466  $template->assign_block_vars('title',array());
467  $template->parse('comments');
468  include(PHPWG_ROOT_PATH.'include/page_tail.php');
469}
470?>
Note: See TracBrowser for help on using the repository browser.