| 1 | <?php |
|---|
| 2 | /* Code adapted from include/picture_comment.inc.php and picture.php */ |
|---|
| 3 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
|---|
| 4 | |
|---|
| 5 | // +-----------------------------------------------------------------------+ |
|---|
| 6 | // | category infos | |
|---|
| 7 | // +-----------------------------------------------------------------------+ |
|---|
| 8 | $category = $page['category']; |
|---|
| 9 | |
|---|
| 10 | $url_self = duplicate_index_url(array( |
|---|
| 11 | 'category' => array( |
|---|
| 12 | 'id' => $category['id'], |
|---|
| 13 | 'name' => $category['name'], |
|---|
| 14 | 'permalink' => $category['permalink'] |
|---|
| 15 | ), |
|---|
| 16 | array('start') |
|---|
| 17 | )); |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | // +-----------------------------------------------------------------------+ |
|---|
| 21 | // | actions | |
|---|
| 22 | // +-----------------------------------------------------------------------+ |
|---|
| 23 | if (isset($_GET['action'])) |
|---|
| 24 | { |
|---|
| 25 | switch ($_GET['action']) |
|---|
| 26 | { |
|---|
| 27 | case 'edit_comment' : |
|---|
| 28 | { |
|---|
| 29 | include_once(COA_PATH.'include/functions_comment.inc.php'); // custom fonctions |
|---|
| 30 | check_input_parameter('comment_to_edit', $_GET, false, PATTERN_ID); |
|---|
| 31 | $author_id = get_comment_author_id_albums($_GET['comment_to_edit']); |
|---|
| 32 | |
|---|
| 33 | if (can_manage_comment('edit', $author_id)) |
|---|
| 34 | { |
|---|
| 35 | if (!empty($_POST['content'])) |
|---|
| 36 | { |
|---|
| 37 | check_pwg_token(); |
|---|
| 38 | $comment_action = update_user_comment_albums( |
|---|
| 39 | array( |
|---|
| 40 | 'comment_id' => $_GET['comment_to_edit'], |
|---|
| 41 | 'category_id' => $category['id'], |
|---|
| 42 | 'content' => $_POST['content'] |
|---|
| 43 | ), |
|---|
| 44 | $_POST['key'] |
|---|
| 45 | ); |
|---|
| 46 | |
|---|
| 47 | $perform_redirect = false; |
|---|
| 48 | switch ($comment_action) |
|---|
| 49 | { |
|---|
| 50 | case 'moderate': |
|---|
| 51 | $_SESSION['page_infos'][] = l10n('An administrator must authorize your comment before it is visible.'); |
|---|
| 52 | case 'validate': |
|---|
| 53 | $_SESSION['page_infos'][] = l10n('Your comment has been registered'); |
|---|
| 54 | $perform_redirect = true; |
|---|
| 55 | break; |
|---|
| 56 | case 'reject': |
|---|
| 57 | $_SESSION['page_errors'][] = l10n('Your comment has NOT been registered because it did not pass the validation rules'); |
|---|
| 58 | $perform_redirect = true; |
|---|
| 59 | break; |
|---|
| 60 | default: |
|---|
| 61 | trigger_error('Invalid comment action '.$comment_action, E_USER_WARNING); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | if ($perform_redirect) |
|---|
| 65 | { |
|---|
| 66 | redirect($url_self); |
|---|
| 67 | } |
|---|
| 68 | unset($_POST['content']); |
|---|
| 69 | } |
|---|
| 70 | else |
|---|
| 71 | { |
|---|
| 72 | $edit_comment = $_GET['comment_to_edit']; |
|---|
| 73 | } |
|---|
| 74 | break; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | case 'delete_comment' : |
|---|
| 78 | { |
|---|
| 79 | check_pwg_token(); |
|---|
| 80 | |
|---|
| 81 | include_once(COA_PATH.'include/functions_comment.inc.php'); // custom fonctions |
|---|
| 82 | |
|---|
| 83 | check_input_parameter('comment_to_delete', $_GET, false, PATTERN_ID); |
|---|
| 84 | |
|---|
| 85 | $author_id = get_comment_author_id_albums($_GET['comment_to_delete']); |
|---|
| 86 | |
|---|
| 87 | if (can_manage_comment('delete', $author_id)) |
|---|
| 88 | { |
|---|
| 89 | delete_user_comment_albums($_GET['comment_to_delete']); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | redirect($url_self); |
|---|
| 93 | } |
|---|
| 94 | case 'validate_comment' : |
|---|
| 95 | { |
|---|
| 96 | check_pwg_token(); |
|---|
| 97 | |
|---|
| 98 | include_once(COA_PATH.'include/functions_comment.inc.php'); // custom fonctions |
|---|
| 99 | |
|---|
| 100 | check_input_parameter('comment_to_validate', $_GET, false, PATTERN_ID); |
|---|
| 101 | |
|---|
| 102 | $author_id = get_comment_author_id_albums($_GET['comment_to_validate']); |
|---|
| 103 | |
|---|
| 104 | if (can_manage_comment('validate', $author_id)) |
|---|
| 105 | { |
|---|
| 106 | validate_user_comment_albums($_GET['comment_to_validate']); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | redirect($url_self); |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | // +-----------------------------------------------------------------------+ |
|---|
| 116 | // | insert comment | |
|---|
| 117 | // +-----------------------------------------------------------------------+ |
|---|
| 118 | if ($category['commentable'] and isset($_POST['content'])) |
|---|
| 119 | { |
|---|
| 120 | if (is_a_guest() and !$conf['comments_forall']) |
|---|
| 121 | { |
|---|
| 122 | die('Session expired'); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | $comm = array( |
|---|
| 126 | 'author' => trim( @$_POST['author'] ), |
|---|
| 127 | 'content' => trim( $_POST['content'] ), |
|---|
| 128 | 'category_id' => $category['id'], |
|---|
| 129 | ); |
|---|
| 130 | |
|---|
| 131 | include_once(COA_PATH.'include/functions_comment.inc.php'); // custom fonctions |
|---|
| 132 | |
|---|
| 133 | $comment_action = insert_user_comment_albums($comm, @$_POST['key'], $page['infos']); |
|---|
| 134 | |
|---|
| 135 | switch ($comment_action) |
|---|
| 136 | { |
|---|
| 137 | case 'moderate': |
|---|
| 138 | array_push($page['infos'], l10n('An administrator must authorize your comment before it is visible.')); |
|---|
| 139 | case 'validate': |
|---|
| 140 | array_push($page['infos'], l10n('Your comment has been registered')); |
|---|
| 141 | break; |
|---|
| 142 | case 'reject': |
|---|
| 143 | set_status_header(403); |
|---|
| 144 | array_push($page['errors'], l10n('Your comment has NOT been registered because it did not pass the validation rules')); |
|---|
| 145 | break; |
|---|
| 146 | default: |
|---|
| 147 | trigger_error('Invalid comment action '.$comment_action, E_USER_WARNING); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | // allow plugins to notify what's going on |
|---|
| 151 | trigger_action( 'user_comment_insertion', |
|---|
| 152 | array_merge($comm, array('action'=>$comment_action) ) |
|---|
| 153 | ); |
|---|
| 154 | |
|---|
| 155 | $template->assign('DISPLAY_COMMENTS_BLOCK', true); |
|---|
| 156 | } |
|---|
| 157 | else if (isset($_POST['content'])) |
|---|
| 158 | { |
|---|
| 159 | set_status_header(403); |
|---|
| 160 | die('ugly spammer'); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | // +-----------------------------------------------------------------------+ |
|---|
| 165 | // | display comments | |
|---|
| 166 | // +-----------------------------------------------------------------------+ |
|---|
| 167 | if ($category['commentable']) |
|---|
| 168 | { |
|---|
| 169 | if (!is_admin()) |
|---|
| 170 | { |
|---|
| 171 | $validated_clause = " AND validated = 'true'"; |
|---|
| 172 | } |
|---|
| 173 | else |
|---|
| 174 | { |
|---|
| 175 | $validated_clause = null; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | // number of comments for this category |
|---|
| 179 | $query = ' |
|---|
| 180 | SELECT |
|---|
| 181 | COUNT(*) AS nb_comments |
|---|
| 182 | FROM '.COA_TABLE.' |
|---|
| 183 | WHERE category_id = '.$category['id'] |
|---|
| 184 | .$validated_clause.' |
|---|
| 185 | ;'; |
|---|
| 186 | $row = pwg_db_fetch_assoc(pwg_query($query)); |
|---|
| 187 | |
|---|
| 188 | // navigation bar creation, can't use $_GET['start'] because used by thumbnails navigation bar |
|---|
| 189 | if (isset($_GET['start_comments'])) |
|---|
| 190 | { |
|---|
| 191 | $page['start_comments'] = $_GET['start_comments']; |
|---|
| 192 | } |
|---|
| 193 | else |
|---|
| 194 | { |
|---|
| 195 | $page['start_comments'] = 0; |
|---|
| 196 | } |
|---|
| 197 | include_once(COA_PATH.'include/functions.inc.php'); // custom fonctions |
|---|
| 198 | |
|---|
| 199 | $navigation_bar = create_comment_navigation_bar( |
|---|
| 200 | duplicate_index_url(array(), array('start')), |
|---|
| 201 | $row['nb_comments'], |
|---|
| 202 | $page['start_comments'], |
|---|
| 203 | $conf['nb_comment_page'] |
|---|
| 204 | ); |
|---|
| 205 | |
|---|
| 206 | $template->assign( |
|---|
| 207 | array( |
|---|
| 208 | 'COMMENT_COUNT' => $row['nb_comments'], |
|---|
| 209 | 'comment_navbar' => $navigation_bar, |
|---|
| 210 | ) |
|---|
| 211 | ); |
|---|
| 212 | |
|---|
| 213 | if ($row['nb_comments'] > 0) |
|---|
| 214 | { |
|---|
| 215 | // comments order (get, session, conf) |
|---|
| 216 | if (!empty($_GET['comments_order']) && in_array(strtoupper($_GET['comments_order']), array('ASC', 'DESC'))) |
|---|
| 217 | { |
|---|
| 218 | pwg_set_session_var('comments_order', $_GET['comments_order']); |
|---|
| 219 | } |
|---|
| 220 | $comments_order = pwg_get_session_var('comments_order', $conf['comments_order']); |
|---|
| 221 | |
|---|
| 222 | $template->assign(array( |
|---|
| 223 | 'COMMENTS_ORDER_URL' => add_url_params( duplicate_index_url(), array('comments_order'=> ($comments_order == 'ASC' ? 'DESC' : 'ASC') ) ), |
|---|
| 224 | 'COMMENTS_ORDER_TITLE' => $comments_order == 'ASC' ? l10n('Show latest comments first') : l10n('Show oldest comments first'), |
|---|
| 225 | )); |
|---|
| 226 | |
|---|
| 227 | // get comments |
|---|
| 228 | $query = ' |
|---|
| 229 | SELECT |
|---|
| 230 | com.id, |
|---|
| 231 | com.author, |
|---|
| 232 | com.author_id, |
|---|
| 233 | '.$conf['user_fields']['username'].' AS username, |
|---|
| 234 | com.date, |
|---|
| 235 | com.category_id, |
|---|
| 236 | com.content, |
|---|
| 237 | com.validated |
|---|
| 238 | FROM '.COA_TABLE.' AS com |
|---|
| 239 | LEFT JOIN '.USERS_TABLE.' AS u |
|---|
| 240 | ON u.'.$conf['user_fields']['id'].' = author_id |
|---|
| 241 | WHERE category_id = '.$category['id'].' |
|---|
| 242 | '.$validated_clause.' |
|---|
| 243 | ORDER BY date '.$comments_order.' |
|---|
| 244 | LIMIT '.$conf['nb_comment_page'].' OFFSET '.$page['start_comments'].' |
|---|
| 245 | ;'; |
|---|
| 246 | $result = pwg_query($query); |
|---|
| 247 | |
|---|
| 248 | while ($row = pwg_db_fetch_assoc($result)) |
|---|
| 249 | { |
|---|
| 250 | // author |
|---|
| 251 | if (!empty($row['author'])) |
|---|
| 252 | { |
|---|
| 253 | $author = $row['author']; |
|---|
| 254 | if ($author == 'guest') |
|---|
| 255 | { |
|---|
| 256 | $author = l10n('guest'); |
|---|
| 257 | } |
|---|
| 258 | } |
|---|
| 259 | else |
|---|
| 260 | { |
|---|
| 261 | $author = stripslashes($row['username']); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | // comment content |
|---|
| 265 | $tpl_comment = array( |
|---|
| 266 | 'ID' => $row['id'], |
|---|
| 267 | 'AUTHOR' => trigger_event('render_comment_author', $author), |
|---|
| 268 | 'DATE' => format_date($row['date'], true), |
|---|
| 269 | 'CONTENT' => trigger_event('render_comment_content', $row['content'], 'album'), |
|---|
| 270 | ); |
|---|
| 271 | |
|---|
| 272 | // rights |
|---|
| 273 | if (can_manage_comment('delete', $row['author_id'])) |
|---|
| 274 | { |
|---|
| 275 | $tpl_comment['U_DELETE'] = add_url_params( |
|---|
| 276 | $url_self, |
|---|
| 277 | array( |
|---|
| 278 | 'action' => 'delete_comment', |
|---|
| 279 | 'comment_to_delete' => $row['id'], |
|---|
| 280 | 'pwg_token' => get_pwg_token(), |
|---|
| 281 | ) |
|---|
| 282 | ); |
|---|
| 283 | } |
|---|
| 284 | if (can_manage_comment('edit', $row['author_id'])) |
|---|
| 285 | { |
|---|
| 286 | $tpl_comment['U_EDIT'] = add_url_params( |
|---|
| 287 | $url_self, |
|---|
| 288 | array( |
|---|
| 289 | 'action' => 'edit_comment', |
|---|
| 290 | 'comment_to_edit' => $row['id'], |
|---|
| 291 | ) |
|---|
| 292 | ); |
|---|
| 293 | if (isset($edit_comment) and ($row['id'] == $edit_comment)) |
|---|
| 294 | { |
|---|
| 295 | $tpl_comment['IN_EDIT'] = true; |
|---|
| 296 | $key = get_ephemeral_key(2, $category['id']); |
|---|
| 297 | $tpl_comment['KEY'] = $key; |
|---|
| 298 | $tpl_comment['CONTENT'] = $row['content']; |
|---|
| 299 | $tpl_comment['PWG_TOKEN'] = get_pwg_token(); |
|---|
| 300 | } |
|---|
| 301 | } |
|---|
| 302 | if (is_admin() AND $row['validated'] != 'true') |
|---|
| 303 | { |
|---|
| 304 | $tpl_comment['U_VALIDATE'] = add_url_params( |
|---|
| 305 | $url_self, |
|---|
| 306 | array( |
|---|
| 307 | 'action' => 'validate_comment', |
|---|
| 308 | 'comment_to_validate' => $row['id'], |
|---|
| 309 | 'pwg_token' => get_pwg_token(), |
|---|
| 310 | ) |
|---|
| 311 | ); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | $template->append('comments', $tpl_comment); |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | // comment form |
|---|
| 319 | $show_add_comment_form = true; |
|---|
| 320 | if (isset($edit_comment)) |
|---|
| 321 | { |
|---|
| 322 | $show_add_comment_form = false; |
|---|
| 323 | } |
|---|
| 324 | if (is_a_guest() and !$conf['comments_forall']) |
|---|
| 325 | { |
|---|
| 326 | $show_add_comment_form = false; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | if ($show_add_comment_form) |
|---|
| 330 | { |
|---|
| 331 | $key = get_ephemeral_key(3, $category['id']); |
|---|
| 332 | $content = null; |
|---|
| 333 | if ('reject'===@$comment_action) |
|---|
| 334 | { |
|---|
| 335 | $content = htmlspecialchars(stripslashes($comm['content'])); |
|---|
| 336 | } |
|---|
| 337 | $template->assign('comment_add', |
|---|
| 338 | array( |
|---|
| 339 | 'F_ACTION' => $url_self, |
|---|
| 340 | 'KEY' => $key, |
|---|
| 341 | 'CONTENT' => $content, |
|---|
| 342 | 'SHOW_AUTHOR' => !is_classic_user(), |
|---|
| 343 | ) |
|---|
| 344 | ); |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | // template |
|---|
| 348 | $template->assign(array( |
|---|
| 349 | 'COA_PATH' => COA_PATH, // for css |
|---|
| 350 | 'COA_ABSOLUTE_PATH' => dirname(__FILE__) .'/../', // for template |
|---|
| 351 | )); |
|---|
| 352 | |
|---|
| 353 | $template->set_filename('comments_on_albums', dirname(__FILE__) .'/../template/albums.tpl'); |
|---|
| 354 | if (isset($pwg_loaded_plugins['rv_tscroller']) AND count($page['navigation_bar']) != 0) |
|---|
| 355 | { |
|---|
| 356 | $template->assign('COMMENTS_ON_TOP', true); |
|---|
| 357 | $template->concat('PLUGIN_INDEX_CONTENT_BEGIN', $template->parse('comments_on_albums', true)); |
|---|
| 358 | } |
|---|
| 359 | else |
|---|
| 360 | { |
|---|
| 361 | $template->concat('PLUGIN_INDEX_CONTENT_END', $template->parse('comments_on_albums', true)); |
|---|
| 362 | } |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | ?> |
|---|