PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/core/root/gallery/comment.php

https://github.com/phpbbgallery/phpbb-gallery
PHP | 448 lines | 366 code | 58 blank | 24 comment | 97 complexity | 50abbc69f7d2e3aaa6fc79bd54fe4777 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package phpBB Gallery
  5. * @version $Id$
  6. * @copyright (c) 2007 nickvergessen nickvergessen@gmx.de http://www.flying-bits.org
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. define('IN_PHPBB', true);
  14. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  15. include('common.' . $phpEx);
  16. include($phpbb_root_path . 'common.' . $phpEx);
  17. $phpbb_ext_gallery = new phpbb_ext_gallery_core($auth, $cache, $config, $db, $template, $user, $phpEx, $phpbb_root_path);
  18. $phpbb_ext_gallery->setup('posting');
  19. $phpbb_ext_gallery->url->_include(array('functions_display', 'functions_posting', 'functions_user'), 'phpbb');
  20. $phpbb_ext_gallery->url->_include(array('bbcode', 'message_parser'), 'phpbb');
  21. $user->add_lang_ext('gallery/core', 'gallery');
  22. add_form_key('gallery');
  23. $submit = (isset($_POST['submit'])) ? true : false;
  24. $mode = request_var('mode', '');
  25. $album_id = request_var('album_id', 0);
  26. $image_id = request_var('image_id', 0);
  27. $comment_id = request_var('comment_id', 0);
  28. $error = $message = '';
  29. // Check for permissions cheaters!
  30. if ($comment_id)
  31. {
  32. $sql = 'SELECT *
  33. FROM ' . GALLERY_COMMENTS_TABLE . '
  34. WHERE comment_id = ' . $comment_id;
  35. $result = $db->sql_query($sql);
  36. $comment_data = $db->sql_fetchrow($result);
  37. $db->sql_freeresult($result);
  38. $image_id = (int) $comment_data['comment_image_id'];
  39. }
  40. if ($image_id)
  41. {
  42. $image_data = phpbb_ext_gallery_core_image::get_info($image_id);
  43. $album_id = (int) $image_data['image_album_id'];
  44. }
  45. $album_data = phpbb_ext_gallery_core_album::get_info($album_id);
  46. phpbb_ext_gallery_core_album_display::generate_nav($album_data);
  47. $image_backlink = $phpbb_ext_gallery->url->append_sid('image_page', "album_id=$album_id&amp;image_id=$image_id");
  48. $album_backlink = $phpbb_ext_gallery->url->append_sid('album', "album_id=$album_id");
  49. $image_loginlink = $phpbb_ext_gallery->url->append_sid('relative', 'image_page', "album_id=$album_id&amp;image_id=$image_id");
  50. // Send some cheaters back
  51. if ($user->data['is_bot'])
  52. {
  53. redirect($image_backlink);
  54. }
  55. if ($album_data['album_type'] == phpbb_ext_gallery_core_album::TYPE_CAT)
  56. {
  57. // If we get here, the database is corrupted,
  58. // but at least we dont let them comment any more.
  59. meta_refresh(3, $album_backlink);
  60. trigger_error('ALBUM_IS_CATEGORY');
  61. }
  62. if (!in_array($mode, array('rate', 'add', 'edit', 'delete')))
  63. {
  64. phpbb_ext_gallery_core_misc::not_authorised($image_backlink, $image_loginlink);
  65. }
  66. if (($mode != 'rate') && !phpbb_ext_gallery_core_comment::is_able($album_data, $image_data))
  67. {
  68. // The user is unable to comment.
  69. phpbb_ext_gallery_core_misc::not_authorised($image_backlink, $image_loginlink);
  70. }
  71. $rating = new phpbb_ext_gallery_core_rating($image_id, $image_data, $album_data);
  72. if (!($phpbb_ext_gallery->config->get('allow_rates') && $rating->is_able()) && ($mode == 'rate'))
  73. {
  74. // The user is unable to rate.
  75. phpbb_ext_gallery_core_misc::not_authorised($image_backlink, $image_loginlink);
  76. }
  77. switch ($mode)
  78. {
  79. case 'add':
  80. if (!$phpbb_ext_gallery->auth->acl_check('c_post', $album_id, $album_data['album_user_id']))
  81. {
  82. phpbb_ext_gallery_core_misc::not_authorised($image_backlink, $image_loginlink);
  83. }
  84. break;
  85. case 'edit':
  86. if (!$phpbb_ext_gallery->auth->acl_check('c_edit', $album_id, $album_data['album_user_id']))
  87. {
  88. if (!$phpbb_ext_gallery->auth->acl_check('m_comments', $album_id, $album_data['album_user_id']))
  89. {
  90. phpbb_ext_gallery_core_misc::not_authorised($image_backlink, $image_loginlink);
  91. }
  92. }
  93. else if (($comment_data['comment_user_id'] != $user->data['user_id']) && !$phpbb_ext_gallery->auth->acl_check('m_comments', $album_id, $album_data['album_user_id']))
  94. {
  95. phpbb_ext_gallery_core_misc::not_authorised($image_backlink, $image_loginlink);
  96. }
  97. break;
  98. case 'delete':
  99. if (!$phpbb_ext_gallery->auth->acl_check('c_delete', $album_id, $album_data['album_user_id']))
  100. {
  101. if (!$phpbb_ext_gallery->auth->acl_check('m_comments', $album_id, $album_data['album_user_id']))
  102. {
  103. phpbb_ext_gallery_core_misc::not_authorised($image_backlink, $image_loginlink);
  104. }
  105. }
  106. else if (($comment_data['comment_user_id'] != $user->data['user_id']) && !$phpbb_ext_gallery->auth->acl_check('m_comments', $album_id, $album_data['album_user_id']))
  107. {
  108. phpbb_ext_gallery_core_misc::not_authorised($image_backlink, $image_loginlink);
  109. }
  110. break;
  111. }
  112. $bbcode_status = ($config['allow_bbcode']) ? true : false;
  113. $smilies_status = ($config['allow_smilies']) ? true : false;
  114. $img_status = ($bbcode_status) ? true : false;
  115. $url_status = ($config['allow_post_links']) ? true : false;
  116. $flash_status = false;
  117. $quote_status = true;
  118. $template->assign_vars(array(
  119. 'BBCODE_STATUS' => ($bbcode_status) ? sprintf($user->lang['BBCODE_IS_ON'], '<a href="' . $phpbb_ext_gallery->url->append_sid('phpbb', 'faq', 'mode=bbcode') . '">', '</a>') : sprintf($user->lang['BBCODE_IS_OFF'], '<a href="' . $phpbb_ext_gallery->url->append_sid('phpbb', 'faq', 'mode=bbcode') . '">', '</a>'),
  120. 'IMG_STATUS' => ($img_status) ? $user->lang['IMAGES_ARE_ON'] : $user->lang['IMAGES_ARE_OFF'],
  121. 'FLASH_STATUS' => ($flash_status) ? $user->lang['FLASH_IS_ON'] : $user->lang['FLASH_IS_OFF'],
  122. 'SMILIES_STATUS' => ($smilies_status) ? $user->lang['SMILIES_ARE_ON'] : $user->lang['SMILIES_ARE_OFF'],
  123. 'URL_STATUS' => ($bbcode_status && $url_status) ? $user->lang['URL_IS_ON'] : $user->lang['URL_IS_OFF'],
  124. 'S_BBCODE_ALLOWED' => $bbcode_status,
  125. 'S_SMILIES_ALLOWED' => $smilies_status,
  126. 'S_LINKS_ALLOWED' => $url_status,
  127. 'S_BBCODE_IMG' => $img_status,
  128. 'S_BBCODE_URL' => $url_status,
  129. 'S_BBCODE_FLASH' => $flash_status,
  130. 'S_BBCODE_QUOTE' => $quote_status,
  131. ));
  132. // Build custom bbcodes array
  133. display_custom_bbcodes();
  134. // Build smilies array
  135. generate_smilies('inline', 0);
  136. /**
  137. * Rating-System: now you can comment and rate in one form
  138. */
  139. $s_user_rated = false;
  140. if ($phpbb_ext_gallery->config->get('allow_rates') && ($mode != 'edit'))
  141. {
  142. $user_rating = $rating->get_user_rating($user->data['user_id']);
  143. // Check: User didn't rate yet, has permissions, it's not the users own image and the user is logged in
  144. if (!$user_rating && $rating->is_allowed())
  145. {
  146. $rating->display_box();
  147. // User just rated the image, so we store it
  148. $rate_point = request_var('rating', 0);
  149. if ($rating->rating_enabled && $rate_point > 0)
  150. {
  151. $rating->submit_rating();
  152. $s_user_rated = true;
  153. $message .= $user->lang['RATING_SUCCESSFUL'] . '<br />';
  154. }
  155. $template->assign_vars(array(
  156. 'S_ALLOWED_TO_RATE' => $rating->is_allowed(),
  157. ));
  158. }
  159. if ($mode == 'rate')
  160. {
  161. $s_album_action = '';
  162. }
  163. }
  164. if ($mode == 'add')
  165. {
  166. if (phpbb_ext_gallery_core_misc::display_captcha('comment'))
  167. {
  168. $phpbb_ext_gallery->url->_include('captcha/captcha_factory', 'phpbb');
  169. $captcha =& phpbb_captcha_factory::get_instance($config['captcha_plugin']);
  170. $captcha->init(CONFIRM_POST);
  171. }
  172. $s_captcha_hidden_fields = '';
  173. $comment_username_req = ($user->data['user_id'] == ANONYMOUS);
  174. if ($submit)
  175. {
  176. if (!check_form_key('gallery'))
  177. {
  178. trigger_error('FORM_INVALID');
  179. }
  180. if (phpbb_ext_gallery_core_misc::display_captcha('comment'))
  181. {
  182. $captcha_error = $captcha->validate();
  183. if ($captcha_error)
  184. {
  185. $error .= (($error) ? '<br />' : '') . $captcha_error;
  186. }
  187. }
  188. $comment_plain = request_var('message', '', true);
  189. $comment_username = request_var('username', '', true);
  190. if ($comment_username_req)
  191. {
  192. if ($comment_username == '')
  193. {
  194. $error .= (($error) ? '<br />' : '') . $user->lang['MISSING_USERNAME'];
  195. }
  196. if ($result = validate_username($comment_username))
  197. {
  198. $user->add_lang('ucp');
  199. $error .= (($error) ? '<br />' : '') . $user->lang[$result . '_USERNAME'];
  200. $submit = false;
  201. }
  202. }
  203. if (($comment_plain == '') && !$s_user_rated)
  204. {
  205. $error .= (($error) ? '<br />' : '') . $user->lang['MISSING_COMMENT'];
  206. }
  207. if (utf8_strlen($comment_plain) > $phpbb_ext_gallery->config->get('comment_length'))
  208. {
  209. $error .= (($error) ? '<br />' : '') . $user->lang['COMMENT_TOO_LONG'];
  210. }
  211. $message_parser = new parse_message();
  212. $message_parser->message = utf8_normalize_nfc($comment_plain);
  213. if ($message_parser->message)
  214. {
  215. $message_parser->parse(true, true, true, true, false, true, true, true);
  216. }
  217. $sql_ary = array(
  218. 'comment_image_id' => $image_id,
  219. 'comment' => $message_parser->message,
  220. 'comment_uid' => $message_parser->bbcode_uid,
  221. 'comment_bitfield' => $message_parser->bbcode_bitfield,
  222. 'comment_signature' => ($auth->acl_get('u_sig') && isset($_POST['attach_sig'])),
  223. );
  224. if ((!$error) && ($sql_ary['comment'] != ''))
  225. {
  226. if (phpbb_ext_gallery_core_misc::display_captcha('comment'))
  227. {
  228. $captcha->reset();
  229. }
  230. phpbb_ext_gallery_core_comment::add($sql_ary, $comment_username);
  231. if ($phpbb_ext_gallery->user->get_data('watch_com') && !$image_data['watch_id'])
  232. {
  233. phpbb_ext_gallery_core_notification::add($image_id);
  234. }
  235. phpbb_ext_gallery_core_notification::send_notification('image', $image_id, $image_data['image_name']);
  236. $message .= $user->lang['COMMENT_STORED'] . '<br />';
  237. }
  238. else if (phpbb_ext_gallery_core_misc::display_captcha('comment'))
  239. {
  240. $s_captcha_hidden_fields = ($captcha->is_solved()) ? build_hidden_fields($captcha->get_hidden_fields()) : '';
  241. }
  242. $sig_checked = ($auth->acl_get('u_sig') && isset($_POST['attach_sig']));
  243. }
  244. else
  245. {
  246. if ($comment_id)
  247. {
  248. $comment_ary = generate_text_for_edit($comment_data['comment'], $comment_data['comment_uid'], $comment_data['comment_bitfield'], 7);
  249. $comment_plain = '[quote="' . $comment_data['comment_username'] . '"]' . $comment_ary['text'] . '[/quote]';
  250. }
  251. $sig_checked = $user->optionget('attachsig');
  252. }
  253. if (phpbb_ext_gallery_core_misc::display_captcha('comment'))
  254. {
  255. if (!$submit || !$captcha->is_solved())
  256. {
  257. $template->assign_vars(array(
  258. 'S_CONFIRM_CODE' => true,
  259. 'CAPTCHA_TEMPLATE' => $captcha->get_template(),
  260. ));
  261. }
  262. $template->assign_vars(array(
  263. 'S_CAPTCHA_HIDDEN_FIELDS' => $s_captcha_hidden_fields,
  264. ));
  265. }
  266. }
  267. else if ($mode == 'edit')
  268. {
  269. $comment_username_req = ($comment_data['comment_user_id'] == ANONYMOUS) ? true : false;
  270. if ($submit)
  271. {
  272. if (!check_form_key('gallery'))
  273. {
  274. trigger_error('FORM_INVALID');
  275. }
  276. $sql_ary = array();
  277. $comment_plain = request_var('message', '', true);
  278. if ($comment_username_req)
  279. {
  280. $comment_username = request_var('username', '');
  281. if ($comment_username == '')
  282. {
  283. $error .= (($error) ? '<br />' : '') . $user->lang['MISSING_USERNAME'];
  284. }
  285. if (validate_username($comment_username))
  286. {
  287. $error .= (($error) ? '<br />' : '') . $user->lang['INVALID_USERNAME'];
  288. $comment_username = '';
  289. }
  290. $sql_ary = array(
  291. 'comment_username' => $comment_username,
  292. );
  293. }
  294. if ($comment_plain == '')
  295. {
  296. $error .= (($error) ? '<br />' : '') . $user->lang['MISSING_COMMENT'];
  297. }
  298. if (utf8_strlen($comment_plain) > $phpbb_ext_gallery->config->get('comment_length'))
  299. {
  300. $error .= (($error) ? '<br />' : '') . $user->lang['COMMENT_TOO_LONG'];
  301. }
  302. $message_parser = new parse_message();
  303. $message_parser->message = utf8_normalize_nfc($comment_plain);
  304. if ($message_parser->message)
  305. {
  306. $message_parser->parse(true, true, true, true, false, true, true, true);
  307. }
  308. $sql_ary = array_merge($sql_ary, array(
  309. 'comment' => $message_parser->message,
  310. 'comment_uid' => $message_parser->bbcode_uid,
  311. 'comment_bitfield' => $message_parser->bbcode_bitfield,
  312. 'comment_edit_count' => $comment_data['comment_edit_count'] + 1,
  313. 'comment_signature' => ($auth->acl_get('u_sig') && isset($_POST['attach_sig'])),
  314. ));
  315. if (!$error)
  316. {
  317. phpbb_ext_gallery_core_comment::edit($comment_id, $sql_ary);
  318. $message .= $user->lang['COMMENT_STORED'] . '<br />';
  319. if ($user->data['user_id'] != $comment_data['comment_user_id'])
  320. {
  321. add_log('gallery', $image_data['image_album_id'], $image_data['image_id'], 'LOG_GALLERY_COMMENT_EDITED', $image_data['image_name']);
  322. }
  323. }
  324. }
  325. else
  326. {
  327. $sig_checked = (bool) $comment_data['comment_signature'];
  328. $comment_ary = generate_text_for_edit($comment_data['comment'], $comment_data['comment_uid'], $comment_data['comment_bitfield'], 7);
  329. $comment_plain = $comment_ary['text'];
  330. $comment_username = $comment_data['comment_username'];
  331. }
  332. }
  333. else if ($mode == 'delete')
  334. {
  335. $s_hidden_fields = build_hidden_fields(array(
  336. 'album_id' => $album_id,
  337. 'image_id' => $image_id,
  338. 'comment_id' => $comment_id,
  339. 'mode' => 'delete',
  340. ));
  341. if (confirm_box(true))
  342. {
  343. phpbb_ext_gallery_core_comment::delete_comments($comment_id);
  344. if ($user->data['user_id'] != $comment_data['comment_user_id'])
  345. {
  346. add_log('gallery', $image_data['image_album_id'], $image_data['image_id'], 'LOG_GALLERY_COMMENT_DELETED', $image_data['image_name']);
  347. }
  348. $message = $user->lang['DELETED_COMMENT'] . '<br />';
  349. $submit = true;
  350. }
  351. else
  352. {
  353. if (isset($_POST['cancel']))
  354. {
  355. $message = $user->lang['DELETED_COMMENT_NOT'] . '<br />';
  356. $submit = true;
  357. }
  358. else
  359. {
  360. confirm_box(false, 'DELETE_COMMENT2', $s_hidden_fields);
  361. }
  362. }
  363. }
  364. $template->assign_vars(array(
  365. 'ERROR' => $error,
  366. 'MESSAGE' => (isset($comment_plain)) ? $comment_plain : '',
  367. 'USERNAME' => (isset($comment_username)) ? $comment_username : '',
  368. 'REQ_USERNAME' => (!empty($comment_username_req)) ? true : false,
  369. 'L_COMMENT_LENGTH' => sprintf($user->lang['COMMENT_LENGTH'], $phpbb_ext_gallery->config->get('comment_length')),
  370. 'IMAGE_RSZ_WIDTH' => $phpbb_ext_gallery->config->get('medium_width'),
  371. 'IMAGE_RSZ_HEIGHT' => $phpbb_ext_gallery->config->get('medium_height'),
  372. 'U_IMAGE' => $phpbb_ext_gallery->url->append_sid('image', "album_id=$album_id&amp;image_id=$image_id"),
  373. 'U_VIEW_IMAGE' => $phpbb_ext_gallery->url->append_sid('image_page', "album_id=$album_id&amp;image_id=$image_id"),
  374. 'IMAGE_NAME' => $image_data['image_name'],
  375. 'S_SIGNATURE_CHECKED' => (isset($sig_checked) && $sig_checked) ? ' checked="checked"' : '',
  376. 'S_ALBUM_ACTION' => $phpbb_ext_gallery->url->append_sid('comment', "mode=$mode&amp;album_id=$album_id&amp;image_id=$image_id" . (($comment_id) ? "&amp;comment_id=$comment_id" : '')),
  377. ));
  378. if ($submit && !$error)
  379. {
  380. $message .= '<br />' . sprintf($user->lang['CLICK_RETURN_IMAGE'], '<a href="' . $image_backlink . '">', '</a>');
  381. $message .= '<br />' . sprintf($user->lang['CLICK_RETURN_ALBUM'], '<a href="' . $album_backlink . '">', '</a>');
  382. meta_refresh(3, $image_backlink);
  383. trigger_error($message);
  384. }
  385. page_header((($mode == 'add') ? $user->lang['POST_COMMENT'] : $user->lang['EDIT_COMMENT']), false);
  386. $template->set_filenames(array(
  387. 'body' => 'gallery/comment_body.html',
  388. ));
  389. page_footer();
  390. ?>