source: tags/release-1_5_1/comments.php @ 27569

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