source: trunk/include/picture_comment.inc.php @ 10097

Last change on this file since 10097 was 10097, checked in by mistic100, 13 years ago

bug:2152 Comments revalidation when modified

  • Property svn:eol-style set to LF
File size: 7.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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 * This file is included by the picture page to manage user comments
26 *
27 */
28
29// the picture is commentable if it belongs at least to one category which
30// is commentable
31$page['show_comments'] = false;
32foreach ($related_categories as $category)
33{
34  if ($category['commentable'] == 'true')
35  {
36    $page['show_comments'] = true;
37    break;
38  }
39}
40
41if (!isset($comment_action))
42{
43  if ( $page['show_comments'] and isset( $_POST['content'] ) )
44  {
45    if ( is_a_guest() and !$conf['comments_forall'] )
46    {
47      die ('Session expired');
48    }
49
50    $comm = array(
51      'author' => trim( @$_POST['author'] ),
52      'content' => trim( $_POST['content'] ),
53      'image_id' => $page['image_id'],
54     );
55
56    include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php');
57
58    $comment_action = insert_user_comment($comm, @$_POST['key'], $infos );
59
60    switch ($comment_action)
61    {
62      case 'moderate':
63        array_push( $infos, l10n('An administrator must authorize your comment before it is visible.') );
64      case 'validate':
65        array_push( $infos, l10n('Your comment has been registered'));
66        break;
67      case 'reject':
68        set_status_header(403);
69        array_push($infos, l10n('Your comment has NOT been registered because it did not pass the validation rules') );
70        break;
71      default:
72        trigger_error('Invalid comment action '.$comment_action, E_USER_WARNING);
73    }
74
75    $template->assign(
76        ($comment_action=='reject') ? 'errors' : 'infos',
77        $infos
78      );
79
80    // allow plugins to notify what's going on
81    trigger_action( 'user_comment_insertion',
82        array_merge($comm, array('action'=>$comment_action) )
83      );
84  }
85  elseif ( isset($_POST['content']) )
86  {
87    set_status_header(403);
88    die('ugly spammer');
89  }
90}
91
92if ($page['show_comments'])
93{
94  if ( !is_admin() )
95  {
96    $validated_clause = '  AND validated = \'true\'';
97  }
98  else
99  {
100    $validated_clause = '';
101  }
102
103  // number of comments for this picture
104  $query = '
105SELECT
106    COUNT(*) AS nb_comments
107  FROM '.COMMENTS_TABLE.'
108  WHERE image_id = '.$page['image_id']
109  .$validated_clause.'
110;';
111  $row = pwg_db_fetch_assoc( pwg_query( $query ) );
112
113  // navigation bar creation
114  if (!isset($page['start']))
115  {
116    $page['start'] = 0;
117  }
118
119  $navigation_bar = create_navigation_bar(
120    duplicate_picture_url(array(), array('start')),
121    $row['nb_comments'],
122    $page['start'],
123    $conf['nb_comment_page'],
124    true // We want a clean URL
125    );
126
127  $template->assign(
128    array(
129      'COMMENT_COUNT' => $row['nb_comments'],
130      'navbar' => $navigation_bar,
131      )
132    );
133
134  if ($row['nb_comments'] > 0)
135  {
136    $query = '
137SELECT
138    com.id,
139    author,
140    author_id,
141    '.$conf['user_fields']['username'].' AS username,
142    date,
143    image_id,
144    content,
145    validated
146  FROM '.COMMENTS_TABLE.' AS com
147  LEFT JOIN '.USERS_TABLE.' AS u
148    ON u.'.$conf['user_fields']['id'].' = author_id
149  WHERE image_id = '.$page['image_id'].'
150    '.$validated_clause.'
151  ORDER BY date ASC
152  LIMIT '.$conf['nb_comment_page'].' OFFSET '.$page['start'].'
153;';
154    $result = pwg_query( $query );
155
156    while ($row = pwg_db_fetch_assoc($result))
157    {
158      if (!empty($row['author']))
159      {
160        $author = $row['author'];
161        if ($author == 'guest')
162        {
163          $author = l10n('guest');
164        }
165      }
166      else
167      {
168        $author = stripslashes($row['username']);
169      }
170
171      $tpl_comment =
172        array(
173          'AUTHOR' => trigger_event('render_comment_author', $author),
174
175          'DATE' => format_date( $row['date'], true),
176
177          'CONTENT' => trigger_event('render_comment_content',$row['content']),
178        );
179
180      if (can_manage_comment('delete', $row['author_id']))
181      {
182        $tpl_comment['U_DELETE'] = add_url_params(
183          $url_self,
184          array(
185            'action'=>'delete_comment',
186            'comment_to_delete'=>$row['id'],
187            'pwg_token' => get_pwg_token(),
188            )
189          );
190      }
191      if (can_manage_comment('edit', $row['author_id']))
192      {
193        $tpl_comment['U_EDIT'] = add_url_params(
194          $url_self,
195          array(
196            'action'=>'edit_comment',
197            'comment_to_edit'=>$row['id'],
198            'pwg_token' => get_pwg_token(),
199            )
200          );
201          if (isset($edit_comment) and ($row['id'] == $edit_comment))
202          {
203            $tpl_comment['IN_EDIT'] = true;
204            $key = get_ephemeral_key(2, $page['image_id']);
205            $tpl_comment['KEY'] = $key;
206            $tpl_comment['CONTENT'] = $row['content'];
207          }
208      }
209      if (is_admin())
210      {
211        if ($row['validated'] != 'true')
212        {
213          $tpl_comment['U_VALIDATE'] = add_url_params(
214                  $url_self,
215                  array(
216                    'action' => 'validate_comment',
217                    'comment_to_validate' => $row['id'],
218                    'pwg_token' => get_pwg_token(),
219                    )
220                  );
221        }
222      }
223      $template->append('comments', $tpl_comment);
224    }
225  }
226
227  $show_add_comment_form = true;
228  if (isset($edit_comment))
229  {
230    $show_add_comment_form = false;
231  }
232  if (is_a_guest() and !$conf['comments_forall'])
233  {
234    $show_add_comment_form = false;
235  }
236
237  if ($show_add_comment_form)
238  {
239    $key = get_ephemeral_key(3, $page['image_id']);
240    $content = '';
241    if ('reject'===@$comment_action)
242    {
243      $content = htmlspecialchars( stripslashes($comm['content']) );
244    }
245    $template->assign('comment_add',
246        array(
247          'F_ACTION' => $url_self,
248          'KEY' => $key,
249          'CONTENT' => $content,
250          'SHOW_AUTHOR' => !is_classic_user()
251        ));
252  }
253}
254
255?>
Note: See TracBrowser for help on using the repository browser.