PageRenderTime 54ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/public/forums/posting.php

https://github.com/karthikprashanth/mayalabs
PHP | 1418 lines | 1083 code | 227 blank | 108 comment | 479 complexity | aabb47d9b75847205cff2e7d1fc6a3a1 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package phpBB3
  5. * @version $Id$
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. define('IN_PHPBB', true);
  14. $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
  15. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  16. include($phpbb_root_path . 'common.' . $phpEx);
  17. include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
  18. include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
  19. include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
  20. // Start session management
  21. $user->session_begin();
  22. $auth->acl($user->data);
  23. // Grab only parameters needed here
  24. $post_id = request_var('p', 0);
  25. $topic_id = request_var('t', 0);
  26. $forum_id = request_var('f', 0);
  27. $draft_id = request_var('d', 0);
  28. $lastclick = request_var('lastclick', 0);
  29. $submit = (isset($_POST['post'])) ? true : false;
  30. $preview = (isset($_POST['preview'])) ? true : false;
  31. $save = (isset($_POST['save'])) ? true : false;
  32. $load = (isset($_POST['load'])) ? true : false;
  33. $delete = (isset($_POST['delete'])) ? true : false;
  34. $cancel = (isset($_POST['cancel']) && !isset($_POST['save'])) ? true : false;
  35. $refresh = (isset($_POST['add_file']) || isset($_POST['delete_file']) || isset($_POST['full_editor']) || isset($_POST['cancel_unglobalise']) || $save || $load) ? true : false;
  36. $mode = ($delete && !$preview && !$refresh && $submit) ? 'delete' : request_var('mode', '');
  37. $error = $post_data = array();
  38. $current_time = time();
  39. // Was cancel pressed? If so then redirect to the appropriate page
  40. if ($cancel || ($current_time - $lastclick < 2 && $submit))
  41. {
  42. $f = ($forum_id) ? 'f=' . $forum_id . '&amp;' : '';
  43. $redirect = ($post_id) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", $f . 'p=' . $post_id) . '#p' . $post_id : (($topic_id) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", $f . 't=' . $topic_id) : (($forum_id) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id) : append_sid("{$phpbb_root_path}index.$phpEx")));
  44. redirect($redirect);
  45. }
  46. if (in_array($mode, array('post', 'reply', 'quote', 'edit', 'delete')) && !$forum_id)
  47. {
  48. trigger_error('NO_FORUM');
  49. }
  50. // We need to know some basic information in all cases before we do anything.
  51. switch ($mode)
  52. {
  53. case 'post':
  54. $sql = 'SELECT *
  55. FROM ' . FORUMS_TABLE . "
  56. WHERE forum_id = $forum_id";
  57. break;
  58. case 'bump':
  59. case 'reply':
  60. if (!$topic_id)
  61. {
  62. trigger_error('NO_TOPIC');
  63. }
  64. // Force forum id
  65. $sql = 'SELECT forum_id
  66. FROM ' . TOPICS_TABLE . '
  67. WHERE topic_id = ' . $topic_id;
  68. $result = $db->sql_query($sql);
  69. $f_id = (int) $db->sql_fetchfield('forum_id');
  70. $db->sql_freeresult($result);
  71. $forum_id = (!$f_id) ? $forum_id : $f_id;
  72. $sql = 'SELECT f.*, t.*
  73. FROM ' . TOPICS_TABLE . ' t, ' . FORUMS_TABLE . " f
  74. WHERE t.topic_id = $topic_id
  75. AND (f.forum_id = t.forum_id
  76. OR f.forum_id = $forum_id)" .
  77. (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND t.topic_approved = 1');
  78. break;
  79. case 'quote':
  80. case 'edit':
  81. case 'delete':
  82. if (!$post_id)
  83. {
  84. $user->setup('posting');
  85. trigger_error('NO_POST');
  86. }
  87. // Force forum id
  88. $sql = 'SELECT forum_id
  89. FROM ' . POSTS_TABLE . '
  90. WHERE post_id = ' . $post_id;
  91. $result = $db->sql_query($sql);
  92. $f_id = (int) $db->sql_fetchfield('forum_id');
  93. $db->sql_freeresult($result);
  94. $forum_id = (!$f_id) ? $forum_id : $f_id;
  95. $sql = 'SELECT f.*, t.*, p.*, u.username, u.username_clean, u.user_sig, u.user_sig_bbcode_uid, u.user_sig_bbcode_bitfield
  96. FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . ' t, ' . FORUMS_TABLE . ' f, ' . USERS_TABLE . " u
  97. WHERE p.post_id = $post_id
  98. AND t.topic_id = p.topic_id
  99. AND u.user_id = p.poster_id
  100. AND (f.forum_id = t.forum_id
  101. OR f.forum_id = $forum_id)" .
  102. (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND p.post_approved = 1');
  103. break;
  104. case 'smilies':
  105. $sql = '';
  106. generate_smilies('window', $forum_id);
  107. break;
  108. case 'popup':
  109. if ($forum_id)
  110. {
  111. $sql = 'SELECT forum_style
  112. FROM ' . FORUMS_TABLE . '
  113. WHERE forum_id = ' . $forum_id;
  114. }
  115. else
  116. {
  117. upload_popup();
  118. return;
  119. }
  120. break;
  121. default:
  122. $sql = '';
  123. break;
  124. }
  125. if (!$sql)
  126. {
  127. $user->setup('posting');
  128. trigger_error('NO_POST_MODE');
  129. }
  130. $result = $db->sql_query($sql);
  131. $post_data = $db->sql_fetchrow($result);
  132. $db->sql_freeresult($result);
  133. if (!$post_data)
  134. {
  135. if (!($mode == 'post' || $mode == 'bump' || $mode == 'reply'))
  136. {
  137. $user->setup('posting');
  138. }
  139. trigger_error(($mode == 'post' || $mode == 'bump' || $mode == 'reply') ? 'NO_TOPIC' : 'NO_POST');
  140. }
  141. // Not able to reply to unapproved posts/topics
  142. // TODO: add more descriptive language key
  143. if ($auth->acl_get('m_approve', $forum_id) && ((($mode == 'reply' || $mode == 'bump') && !$post_data['topic_approved']) || ($mode == 'quote' && !$post_data['post_approved'])))
  144. {
  145. trigger_error(($mode == 'reply' || $mode == 'bump') ? 'TOPIC_UNAPPROVED' : 'POST_UNAPPROVED');
  146. }
  147. if ($mode == 'popup')
  148. {
  149. upload_popup($post_data['forum_style']);
  150. return;
  151. }
  152. $user->setup(array('posting', 'mcp', 'viewtopic'), $post_data['forum_style']);
  153. if ($config['enable_post_confirm'] && !$user->data['is_registered'])
  154. {
  155. include($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx);
  156. $captcha =& phpbb_captcha_factory::get_instance($config['captcha_plugin']);
  157. $captcha->init(CONFIRM_POST);
  158. }
  159. // Use post_row values in favor of submitted ones...
  160. $forum_id = (!empty($post_data['forum_id'])) ? (int) $post_data['forum_id'] : (int) $forum_id;
  161. $topic_id = (!empty($post_data['topic_id'])) ? (int) $post_data['topic_id'] : (int) $topic_id;
  162. $post_id = (!empty($post_data['post_id'])) ? (int) $post_data['post_id'] : (int) $post_id;
  163. // Need to login to passworded forum first?
  164. if ($post_data['forum_password'])
  165. {
  166. login_forum_box(array(
  167. 'forum_id' => $forum_id,
  168. 'forum_password' => $post_data['forum_password'])
  169. );
  170. }
  171. // Check permissions
  172. if ($user->data['is_bot'])
  173. {
  174. redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
  175. }
  176. // Is the user able to read within this forum?
  177. if (!$auth->acl_get('f_read', $forum_id))
  178. {
  179. if ($user->data['user_id'] != ANONYMOUS)
  180. {
  181. trigger_error('USER_CANNOT_READ');
  182. }
  183. login_box('', $user->lang['LOGIN_EXPLAIN_POST']);
  184. }
  185. // Permission to do the action asked?
  186. $is_authed = false;
  187. switch ($mode)
  188. {
  189. case 'post':
  190. if ($auth->acl_get('f_post', $forum_id))
  191. {
  192. $is_authed = true;
  193. }
  194. break;
  195. case 'bump':
  196. if ($auth->acl_get('f_bump', $forum_id))
  197. {
  198. $is_authed = true;
  199. }
  200. break;
  201. case 'quote':
  202. $post_data['post_edit_locked'] = 0;
  203. // no break;
  204. case 'reply':
  205. if ($auth->acl_get('f_reply', $forum_id))
  206. {
  207. $is_authed = true;
  208. }
  209. break;
  210. case 'edit':
  211. if ($user->data['is_registered'] && $auth->acl_gets('f_edit', 'm_edit', $forum_id))
  212. {
  213. $is_authed = true;
  214. }
  215. break;
  216. case 'delete':
  217. if ($user->data['is_registered'] && $auth->acl_gets('f_delete', 'm_delete', $forum_id))
  218. {
  219. $is_authed = true;
  220. }
  221. break;
  222. }
  223. if (!$is_authed)
  224. {
  225. $check_auth = ($mode == 'quote') ? 'reply' : $mode;
  226. if ($user->data['is_registered'])
  227. {
  228. trigger_error('USER_CANNOT_' . strtoupper($check_auth));
  229. }
  230. login_box('', $user->lang['LOGIN_EXPLAIN_' . strtoupper($mode)]);
  231. }
  232. // Is the user able to post within this forum?
  233. if ($post_data['forum_type'] != FORUM_POST && in_array($mode, array('post', 'bump', 'quote', 'reply')))
  234. {
  235. trigger_error('USER_CANNOT_FORUM_POST');
  236. }
  237. // Forum/Topic locked?
  238. if (($post_data['forum_status'] == ITEM_LOCKED || (isset($post_data['topic_status']) && $post_data['topic_status'] == ITEM_LOCKED)) && !$auth->acl_get('m_edit', $forum_id))
  239. {
  240. trigger_error(($post_data['forum_status'] == ITEM_LOCKED) ? 'FORUM_LOCKED' : 'TOPIC_LOCKED');
  241. }
  242. // Can we edit this post ... if we're a moderator with rights then always yes
  243. // else it depends on editing times, lock status and if we're the correct user
  244. if ($mode == 'edit' && !$auth->acl_get('m_edit', $forum_id))
  245. {
  246. if ($user->data['user_id'] != $post_data['poster_id'])
  247. {
  248. trigger_error('USER_CANNOT_EDIT');
  249. }
  250. if (!($post_data['post_time'] > time() - ($config['edit_time'] * 60) || !$config['edit_time']))
  251. {
  252. trigger_error('CANNOT_EDIT_TIME');
  253. }
  254. if ($post_data['post_edit_locked'])
  255. {
  256. trigger_error('CANNOT_EDIT_POST_LOCKED');
  257. }
  258. }
  259. // Handle delete mode...
  260. if ($mode == 'delete')
  261. {
  262. handle_post_delete($forum_id, $topic_id, $post_id, $post_data);
  263. return;
  264. }
  265. // Handle bump mode...
  266. if ($mode == 'bump')
  267. {
  268. if ($bump_time = bump_topic_allowed($forum_id, $post_data['topic_bumped'], $post_data['topic_last_post_time'], $post_data['topic_poster'], $post_data['topic_last_poster_id'])
  269. && check_link_hash(request_var('hash', ''), "topic_{$post_data['topic_id']}"))
  270. {
  271. $db->sql_transaction('begin');
  272. $sql = 'UPDATE ' . POSTS_TABLE . "
  273. SET post_time = $current_time
  274. WHERE post_id = {$post_data['topic_last_post_id']}
  275. AND topic_id = $topic_id";
  276. $db->sql_query($sql);
  277. $sql = 'UPDATE ' . TOPICS_TABLE . "
  278. SET topic_last_post_time = $current_time,
  279. topic_bumped = 1,
  280. topic_bumper = " . $user->data['user_id'] . "
  281. WHERE topic_id = $topic_id";
  282. $db->sql_query($sql);
  283. update_post_information('forum', $forum_id);
  284. $sql = 'UPDATE ' . USERS_TABLE . "
  285. SET user_lastpost_time = $current_time
  286. WHERE user_id = " . $user->data['user_id'];
  287. $db->sql_query($sql);
  288. $db->sql_transaction('commit');
  289. markread('post', $forum_id, $topic_id, $current_time);
  290. add_log('mod', $forum_id, $topic_id, 'LOG_BUMP_TOPIC', $post_data['topic_title']);
  291. $meta_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;p={$post_data['topic_last_post_id']}") . "#p{$post_data['topic_last_post_id']}";
  292. meta_refresh(3, $meta_url);
  293. $message = $user->lang['TOPIC_BUMPED'] . '<br /><br />' . sprintf($user->lang['VIEW_MESSAGE'], '<a href="' . $meta_url . '">', '</a>');
  294. $message .= '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id) . '">', '</a>');
  295. trigger_error($message);
  296. }
  297. trigger_error('BUMP_ERROR');
  298. }
  299. // Subject length limiting to 60 characters if first post...
  300. if ($mode == 'post' || ($mode == 'edit' && $post_data['topic_first_post_id'] == $post_data['post_id']))
  301. {
  302. $template->assign_var('S_NEW_MESSAGE', true);
  303. }
  304. // Determine some vars
  305. if (isset($post_data['poster_id']) && $post_data['poster_id'] == ANONYMOUS)
  306. {
  307. $post_data['quote_username'] = (!empty($post_data['post_username'])) ? $post_data['post_username'] : $user->lang['GUEST'];
  308. }
  309. else
  310. {
  311. $post_data['quote_username'] = isset($post_data['username']) ? $post_data['username'] : '';
  312. }
  313. $post_data['post_edit_locked'] = (isset($post_data['post_edit_locked'])) ? (int) $post_data['post_edit_locked'] : 0;
  314. $post_data['post_subject_md5'] = (isset($post_data['post_subject']) && $mode == 'edit') ? md5($post_data['post_subject']) : '';
  315. $post_data['post_subject'] = (in_array($mode, array('quote', 'edit'))) ? $post_data['post_subject'] : ((isset($post_data['topic_title'])) ? $post_data['topic_title'] : '');
  316. $post_data['topic_time_limit'] = (isset($post_data['topic_time_limit'])) ? (($post_data['topic_time_limit']) ? (int) $post_data['topic_time_limit'] / 86400 : (int) $post_data['topic_time_limit']) : 0;
  317. $post_data['poll_length'] = (!empty($post_data['poll_length'])) ? (int) $post_data['poll_length'] / 86400 : 0;
  318. $post_data['poll_start'] = (!empty($post_data['poll_start'])) ? (int) $post_data['poll_start'] : 0;
  319. $post_data['icon_id'] = (!isset($post_data['icon_id']) || in_array($mode, array('quote', 'reply'))) ? 0 : (int) $post_data['icon_id'];
  320. $post_data['poll_options'] = array();
  321. // Get Poll Data
  322. if ($post_data['poll_start'])
  323. {
  324. $sql = 'SELECT poll_option_text
  325. FROM ' . POLL_OPTIONS_TABLE . "
  326. WHERE topic_id = $topic_id
  327. ORDER BY poll_option_id";
  328. $result = $db->sql_query($sql);
  329. while ($row = $db->sql_fetchrow($result))
  330. {
  331. $post_data['poll_options'][] = trim($row['poll_option_text']);
  332. }
  333. $db->sql_freeresult($result);
  334. }
  335. if ($mode == 'edit')
  336. {
  337. $original_poll_data = array(
  338. 'poll_title' => $post_data['poll_title'],
  339. 'poll_length' => $post_data['poll_length'],
  340. 'poll_max_options' => $post_data['poll_max_options'],
  341. 'poll_option_text' => implode("\n", $post_data['poll_options']),
  342. 'poll_start' => $post_data['poll_start'],
  343. 'poll_last_vote' => $post_data['poll_last_vote'],
  344. 'poll_vote_change' => $post_data['poll_vote_change'],
  345. );
  346. }
  347. $orig_poll_options_size = sizeof($post_data['poll_options']);
  348. $message_parser = new parse_message();
  349. if (isset($post_data['post_text']))
  350. {
  351. $message_parser->message = &$post_data['post_text'];
  352. unset($post_data['post_text']);
  353. }
  354. // Set some default variables
  355. $uninit = array('post_attachment' => 0, 'poster_id' => $user->data['user_id'], 'enable_magic_url' => 0, 'topic_status' => 0, 'topic_type' => POST_NORMAL, 'post_subject' => '', 'topic_title' => '', 'post_time' => 0, 'post_edit_reason' => '', 'notify_set' => 0);
  356. foreach ($uninit as $var_name => $default_value)
  357. {
  358. if (!isset($post_data[$var_name]))
  359. {
  360. $post_data[$var_name] = $default_value;
  361. }
  362. }
  363. unset($uninit);
  364. // Always check if the submitted attachment data is valid and belongs to the user.
  365. // Further down (especially in submit_post()) we do not check this again.
  366. $message_parser->get_submitted_attachment_data($post_data['poster_id']);
  367. if ($post_data['post_attachment'] && !$submit && !$refresh && !$preview && $mode == 'edit')
  368. {
  369. // Do not change to SELECT *
  370. $sql = 'SELECT attach_id, is_orphan, attach_comment, real_filename
  371. FROM ' . ATTACHMENTS_TABLE . "
  372. WHERE post_msg_id = $post_id
  373. AND in_message = 0
  374. AND is_orphan = 0
  375. ORDER BY filetime DESC";
  376. $result = $db->sql_query($sql);
  377. $message_parser->attachment_data = array_merge($message_parser->attachment_data, $db->sql_fetchrowset($result));
  378. $db->sql_freeresult($result);
  379. }
  380. if ($post_data['poster_id'] == ANONYMOUS)
  381. {
  382. $post_data['username'] = ($mode == 'quote' || $mode == 'edit') ? trim($post_data['post_username']) : '';
  383. }
  384. else
  385. {
  386. $post_data['username'] = ($mode == 'quote' || $mode == 'edit') ? trim($post_data['username']) : '';
  387. }
  388. $post_data['enable_urls'] = $post_data['enable_magic_url'];
  389. if ($mode != 'edit')
  390. {
  391. $post_data['enable_sig'] = ($config['allow_sig'] && $user->optionget('attachsig')) ? true: false;
  392. $post_data['enable_smilies'] = ($config['allow_smilies'] && $user->optionget('smilies')) ? true : false;
  393. $post_data['enable_bbcode'] = ($config['allow_bbcode'] && $user->optionget('bbcode')) ? true : false;
  394. $post_data['enable_urls'] = true;
  395. }
  396. $post_data['enable_magic_url'] = $post_data['drafts'] = false;
  397. // User own some drafts?
  398. if ($user->data['is_registered'] && $auth->acl_get('u_savedrafts') && ($mode == 'reply' || $mode == 'post' || $mode == 'quote'))
  399. {
  400. $sql = 'SELECT draft_id
  401. FROM ' . DRAFTS_TABLE . '
  402. WHERE user_id = ' . $user->data['user_id'] .
  403. (($forum_id) ? ' AND forum_id = ' . (int) $forum_id : '') .
  404. (($topic_id) ? ' AND topic_id = ' . (int) $topic_id : '') .
  405. (($draft_id) ? " AND draft_id <> $draft_id" : '');
  406. $result = $db->sql_query_limit($sql, 1);
  407. if ($db->sql_fetchrow($result))
  408. {
  409. $post_data['drafts'] = true;
  410. }
  411. $db->sql_freeresult($result);
  412. }
  413. $check_value = (($post_data['enable_bbcode']+1) << 8) + (($post_data['enable_smilies']+1) << 4) + (($post_data['enable_urls']+1) << 2) + (($post_data['enable_sig']+1) << 1);
  414. // Check if user is watching this topic
  415. if ($mode != 'post' && $config['allow_topic_notify'] && $user->data['is_registered'])
  416. {
  417. $sql = 'SELECT topic_id
  418. FROM ' . TOPICS_WATCH_TABLE . '
  419. WHERE topic_id = ' . $topic_id . '
  420. AND user_id = ' . $user->data['user_id'];
  421. $result = $db->sql_query($sql);
  422. $post_data['notify_set'] = (int) $db->sql_fetchfield('topic_id');
  423. $db->sql_freeresult($result);
  424. }
  425. // Do we want to edit our post ?
  426. if ($mode == 'edit' && $post_data['bbcode_uid'])
  427. {
  428. $message_parser->bbcode_uid = $post_data['bbcode_uid'];
  429. }
  430. // HTML, BBCode, Smilies, Images and Flash status
  431. $bbcode_status = ($config['allow_bbcode'] && $auth->acl_get('f_bbcode', $forum_id)) ? true : false;
  432. $smilies_status = ($config['allow_smilies'] && $auth->acl_get('f_smilies', $forum_id)) ? true : false;
  433. $img_status = ($bbcode_status && $auth->acl_get('f_img', $forum_id)) ? true : false;
  434. $url_status = ($config['allow_post_links']) ? true : false;
  435. $flash_status = ($bbcode_status && $auth->acl_get('f_flash', $forum_id) && $config['allow_post_flash']) ? true : false;
  436. $quote_status = true;
  437. // Save Draft
  438. if ($save && $user->data['is_registered'] && $auth->acl_get('u_savedrafts') && ($mode == 'reply' || $mode == 'post' || $mode == 'quote'))
  439. {
  440. $subject = utf8_normalize_nfc(request_var('subject', '', true));
  441. $subject = (!$subject && $mode != 'post') ? $post_data['topic_title'] : $subject;
  442. $message = utf8_normalize_nfc(request_var('message', '', true));
  443. if ($subject && $message)
  444. {
  445. if (confirm_box(true))
  446. {
  447. $sql = 'INSERT INTO ' . DRAFTS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  448. 'user_id' => (int) $user->data['user_id'],
  449. 'topic_id' => (int) $topic_id,
  450. 'forum_id' => (int) $forum_id,
  451. 'save_time' => (int) $current_time,
  452. 'draft_subject' => (string) $subject,
  453. 'draft_message' => (string) $message)
  454. );
  455. $db->sql_query($sql);
  456. $meta_info = ($mode == 'post') ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id) : append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id");
  457. meta_refresh(3, $meta_info);
  458. $message = $user->lang['DRAFT_SAVED'] . '<br /><br />';
  459. $message .= ($mode != 'post') ? sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $meta_info . '">', '</a>') . '<br /><br />' : '';
  460. $message .= sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id) . '">', '</a>');
  461. trigger_error($message);
  462. }
  463. else
  464. {
  465. $s_hidden_fields = build_hidden_fields(array(
  466. 'mode' => $mode,
  467. 'save' => true,
  468. 'f' => $forum_id,
  469. 't' => $topic_id,
  470. 'subject' => $subject,
  471. 'message' => $message,
  472. 'attachment_data' => $message_parser->attachment_data,
  473. )
  474. );
  475. $hidden_fields = array(
  476. 'icon_id' => 0,
  477. 'disable_bbcode' => false,
  478. 'disable_smilies' => false,
  479. 'disable_magic_url' => false,
  480. 'attach_sig' => true,
  481. 'lock_topic' => false,
  482. 'topic_type' => POST_NORMAL,
  483. 'topic_time_limit' => 0,
  484. 'poll_title' => '',
  485. 'poll_option_text' => '',
  486. 'poll_max_options' => 1,
  487. 'poll_length' => 0,
  488. 'poll_vote_change' => false,
  489. );
  490. foreach ($hidden_fields as $name => $default)
  491. {
  492. if (!isset($_POST[$name]))
  493. {
  494. // Don't include it, if its not available
  495. unset($hidden_fields[$name]);
  496. continue;
  497. }
  498. if (is_bool($default))
  499. {
  500. // Use the string representation
  501. $hidden_fields[$name] = request_var($name, '');
  502. }
  503. else
  504. {
  505. $hidden_fields[$name] = request_var($name, $default);
  506. }
  507. }
  508. $s_hidden_fields .= build_hidden_fields($hidden_fields);
  509. confirm_box(false, 'SAVE_DRAFT', $s_hidden_fields);
  510. }
  511. }
  512. else
  513. {
  514. if (utf8_clean_string($subject) === '')
  515. {
  516. $error[] = $user->lang['EMPTY_SUBJECT'];
  517. }
  518. if (utf8_clean_string($message) === '')
  519. {
  520. $error[] = $user->lang['TOO_FEW_CHARS'];
  521. }
  522. }
  523. unset($subject, $message);
  524. }
  525. // Load requested Draft
  526. if ($draft_id && ($mode == 'reply' || $mode == 'quote' || $mode == 'post') && $user->data['is_registered'] && $auth->acl_get('u_savedrafts'))
  527. {
  528. $sql = 'SELECT draft_subject, draft_message
  529. FROM ' . DRAFTS_TABLE . "
  530. WHERE draft_id = $draft_id
  531. AND user_id = " . $user->data['user_id'];
  532. $result = $db->sql_query_limit($sql, 1);
  533. $row = $db->sql_fetchrow($result);
  534. $db->sql_freeresult($result);
  535. if ($row)
  536. {
  537. $post_data['post_subject'] = $row['draft_subject'];
  538. $message_parser->message = $row['draft_message'];
  539. $template->assign_var('S_DRAFT_LOADED', true);
  540. }
  541. else
  542. {
  543. $draft_id = 0;
  544. }
  545. }
  546. // Load draft overview
  547. if ($load && ($mode == 'reply' || $mode == 'quote' || $mode == 'post') && $post_data['drafts'])
  548. {
  549. load_drafts($topic_id, $forum_id);
  550. }
  551. if ($submit || $preview || $refresh)
  552. {
  553. $post_data['topic_cur_post_id'] = request_var('topic_cur_post_id', 0);
  554. $post_data['post_subject'] = utf8_normalize_nfc(request_var('subject', '', true));
  555. $message_parser->message = utf8_normalize_nfc(request_var('message', '', true));
  556. $post_data['username'] = utf8_normalize_nfc(request_var('username', $post_data['username'], true));
  557. $post_data['post_edit_reason'] = (!empty($_POST['edit_reason']) && $mode == 'edit' && $auth->acl_get('m_edit', $forum_id)) ? utf8_normalize_nfc(request_var('edit_reason', '', true)) : '';
  558. $post_data['orig_topic_type'] = $post_data['topic_type'];
  559. $post_data['topic_type'] = request_var('topic_type', (($mode != 'post') ? (int) $post_data['topic_type'] : POST_NORMAL));
  560. $post_data['topic_time_limit'] = request_var('topic_time_limit', (($mode != 'post') ? (int) $post_data['topic_time_limit'] : 0));
  561. if ($post_data['enable_icons'] && $auth->acl_get('f_icons', $forum_id))
  562. {
  563. $post_data['icon_id'] = request_var('icon', (int) $post_data['icon_id']);
  564. }
  565. $post_data['enable_bbcode'] = (!$bbcode_status || isset($_POST['disable_bbcode'])) ? false : true;
  566. $post_data['enable_smilies'] = (!$smilies_status || isset($_POST['disable_smilies'])) ? false : true;
  567. $post_data['enable_urls'] = (isset($_POST['disable_magic_url'])) ? 0 : 1;
  568. $post_data['enable_sig'] = (!$config['allow_sig'] || !$auth->acl_get('f_sigs', $forum_id) || !$auth->acl_get('u_sig')) ? false : ((isset($_POST['attach_sig']) && $user->data['is_registered']) ? true : false);
  569. if ($config['allow_topic_notify'] && $user->data['is_registered'])
  570. {
  571. $notify = (isset($_POST['notify'])) ? true : false;
  572. }
  573. else
  574. {
  575. $notify = false;
  576. }
  577. $topic_lock = (isset($_POST['lock_topic'])) ? true : false;
  578. $post_lock = (isset($_POST['lock_post'])) ? true : false;
  579. $poll_delete = (isset($_POST['poll_delete'])) ? true : false;
  580. if ($submit)
  581. {
  582. $status_switch = (($post_data['enable_bbcode']+1) << 8) + (($post_data['enable_smilies']+1) << 4) + (($post_data['enable_urls']+1) << 2) + (($post_data['enable_sig']+1) << 1);
  583. $status_switch = ($status_switch != $check_value);
  584. }
  585. else
  586. {
  587. $status_switch = 1;
  588. }
  589. // Delete Poll
  590. if ($poll_delete && $mode == 'edit' && sizeof($post_data['poll_options']) &&
  591. ((!$post_data['poll_last_vote'] && $post_data['poster_id'] == $user->data['user_id'] && $auth->acl_get('f_delete', $forum_id)) || $auth->acl_get('m_delete', $forum_id)))
  592. {
  593. if ($submit && check_form_key('posting'))
  594. {
  595. $sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . "
  596. WHERE topic_id = $topic_id";
  597. $db->sql_query($sql);
  598. $sql = 'DELETE FROM ' . POLL_VOTES_TABLE . "
  599. WHERE topic_id = $topic_id";
  600. $db->sql_query($sql);
  601. $topic_sql = array(
  602. 'poll_title' => '',
  603. 'poll_start' => 0,
  604. 'poll_length' => 0,
  605. 'poll_last_vote' => 0,
  606. 'poll_max_options' => 0,
  607. 'poll_vote_change' => 0
  608. );
  609. $sql = 'UPDATE ' . TOPICS_TABLE . '
  610. SET ' . $db->sql_build_array('UPDATE', $topic_sql) . "
  611. WHERE topic_id = $topic_id";
  612. $db->sql_query($sql);
  613. }
  614. $post_data['poll_title'] = $post_data['poll_option_text'] = '';
  615. $post_data['poll_vote_change'] = $post_data['poll_max_options'] = $post_data['poll_length'] = 0;
  616. }
  617. else
  618. {
  619. $post_data['poll_title'] = utf8_normalize_nfc(request_var('poll_title', '', true));
  620. $post_data['poll_length'] = request_var('poll_length', 0);
  621. $post_data['poll_option_text'] = utf8_normalize_nfc(request_var('poll_option_text', '', true));
  622. $post_data['poll_max_options'] = request_var('poll_max_options', 1);
  623. $post_data['poll_vote_change'] = ($auth->acl_get('f_votechg', $forum_id) && $auth->acl_get('f_vote', $forum_id) && isset($_POST['poll_vote_change'])) ? 1 : 0;
  624. }
  625. // If replying/quoting and last post id has changed
  626. // give user option to continue submit or return to post
  627. // notify and show user the post made between his request and the final submit
  628. if (($mode == 'reply' || $mode == 'quote') && $post_data['topic_cur_post_id'] && $post_data['topic_cur_post_id'] != $post_data['topic_last_post_id'])
  629. {
  630. // Only do so if it is allowed forum-wide
  631. if ($post_data['forum_flags'] & FORUM_FLAG_POST_REVIEW)
  632. {
  633. if (topic_review($topic_id, $forum_id, 'post_review', $post_data['topic_cur_post_id']))
  634. {
  635. $template->assign_var('S_POST_REVIEW', true);
  636. }
  637. $submit = false;
  638. $refresh = true;
  639. }
  640. }
  641. // Parse Attachments - before checksum is calculated
  642. $message_parser->parse_attachments('fileupload', $mode, $forum_id, $submit, $preview, $refresh);
  643. // Grab md5 'checksum' of new message
  644. $message_md5 = md5($message_parser->message);
  645. // If editing and checksum has changed we know the post was edited while we're editing
  646. // Notify and show user the changed post
  647. if ($mode == 'edit' && $post_data['forum_flags'] & FORUM_FLAG_POST_REVIEW)
  648. {
  649. $edit_post_message_checksum = request_var('edit_post_message_checksum', '');
  650. $edit_post_subject_checksum = request_var('edit_post_subject_checksum', '');
  651. // $post_data['post_checksum'] is the checksum of the post submitted in the meantime
  652. // $message_md5 is the checksum of the post we're about to submit
  653. // $edit_post_message_checksum is the checksum of the post we're editing
  654. // ...
  655. // We make sure nobody else made exactly the same change
  656. // we're about to submit by also checking $message_md5 != $post_data['post_checksum']
  657. if (($edit_post_message_checksum !== '' && $edit_post_message_checksum != $post_data['post_checksum'] && $message_md5 != $post_data['post_checksum'])
  658. || ($edit_post_subject_checksum !== '' && $edit_post_subject_checksum != $post_data['post_subject_md5'] && md5($post_data['post_subject']) != $post_data['post_subject_md5']))
  659. {
  660. if (topic_review($topic_id, $forum_id, 'post_review_edit', $post_id))
  661. {
  662. $template->assign_vars(array(
  663. 'S_POST_REVIEW' => true,
  664. 'L_POST_REVIEW' => $user->lang['POST_REVIEW_EDIT'],
  665. 'L_POST_REVIEW_EXPLAIN' => $user->lang['POST_REVIEW_EDIT_EXPLAIN'],
  666. ));
  667. }
  668. $submit = false;
  669. $refresh = true;
  670. }
  671. }
  672. // Check checksum ... don't re-parse message if the same
  673. $update_message = ($mode != 'edit' || $message_md5 != $post_data['post_checksum'] || $status_switch || strlen($post_data['bbcode_uid']) < BBCODE_UID_LEN) ? true : false;
  674. // Also check if subject got updated...
  675. $update_subject = $mode != 'edit' || ($post_data['post_subject_md5'] && $post_data['post_subject_md5'] != md5($post_data['post_subject']));
  676. // Parse message
  677. if ($update_message)
  678. {
  679. if (sizeof($message_parser->warn_msg))
  680. {
  681. $error[] = implode('<br />', $message_parser->warn_msg);
  682. $message_parser->warn_msg = array();
  683. }
  684. $message_parser->parse($post_data['enable_bbcode'], ($config['allow_post_links']) ? $post_data['enable_urls'] : false, $post_data['enable_smilies'], $img_status, $flash_status, $quote_status, $config['allow_post_links']);
  685. // On a refresh we do not care about message parsing errors
  686. if (sizeof($message_parser->warn_msg) && $refresh)
  687. {
  688. $message_parser->warn_msg = array();
  689. }
  690. }
  691. else
  692. {
  693. $message_parser->bbcode_bitfield = $post_data['bbcode_bitfield'];
  694. }
  695. if ($mode != 'edit' && !$preview && !$refresh && $config['flood_interval'] && !$auth->acl_get('f_ignoreflood', $forum_id))
  696. {
  697. // Flood check
  698. $last_post_time = 0;
  699. if ($user->data['is_registered'])
  700. {
  701. $last_post_time = $user->data['user_lastpost_time'];
  702. }
  703. else
  704. {
  705. $sql = 'SELECT post_time AS last_post_time
  706. FROM ' . POSTS_TABLE . "
  707. WHERE poster_ip = '" . $user->ip . "'
  708. AND post_time > " . ($current_time - $config['flood_interval']);
  709. $result = $db->sql_query_limit($sql, 1);
  710. if ($row = $db->sql_fetchrow($result))
  711. {
  712. $last_post_time = $row['last_post_time'];
  713. }
  714. $db->sql_freeresult($result);
  715. }
  716. if ($last_post_time && ($current_time - $last_post_time) < intval($config['flood_interval']))
  717. {
  718. $error[] = $user->lang['FLOOD_ERROR'];
  719. }
  720. }
  721. // Validate username
  722. if (($post_data['username'] && !$user->data['is_registered']) || ($mode == 'edit' && $post_data['poster_id'] == ANONYMOUS && $post_data['username'] && $post_data['post_username'] && $post_data['post_username'] != $post_data['username']))
  723. {
  724. include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  725. if (($result = validate_username($post_data['username'], (!empty($post_data['post_username'])) ? $post_data['post_username'] : '')) !== false)
  726. {
  727. $user->add_lang('ucp');
  728. $error[] = $user->lang[$result . '_USERNAME'];
  729. }
  730. }
  731. if ($config['enable_post_confirm'] && !$user->data['is_registered'] && in_array($mode, array('quote', 'post', 'reply')))
  732. {
  733. $captcha_data = array(
  734. 'message' => utf8_normalize_nfc(request_var('message', '', true)),
  735. 'subject' => utf8_normalize_nfc(request_var('subject', '', true)),
  736. 'username' => utf8_normalize_nfc(request_var('username', '', true)),
  737. );
  738. $vc_response = $captcha->validate($captcha_data);
  739. if ($vc_response)
  740. {
  741. $error[] = $vc_response;
  742. }
  743. }
  744. // check form
  745. if (($submit || $preview) && !check_form_key('posting'))
  746. {
  747. $error[] = $user->lang['FORM_INVALID'];
  748. }
  749. // Parse subject
  750. if (!$preview && !$refresh && utf8_clean_string($post_data['post_subject']) === '' && ($mode == 'post' || ($mode == 'edit' && $post_data['topic_first_post_id'] == $post_id)))
  751. {
  752. $error[] = $user->lang['EMPTY_SUBJECT'];
  753. }
  754. $post_data['poll_last_vote'] = (isset($post_data['poll_last_vote'])) ? $post_data['poll_last_vote'] : 0;
  755. if ($post_data['poll_option_text'] &&
  756. ($mode == 'post' || ($mode == 'edit' && $post_id == $post_data['topic_first_post_id']/* && (!$post_data['poll_last_vote'] || $auth->acl_get('m_edit', $forum_id))*/))
  757. && $auth->acl_get('f_poll', $forum_id))
  758. {
  759. $poll = array(
  760. 'poll_title' => $post_data['poll_title'],
  761. 'poll_length' => $post_data['poll_length'],
  762. 'poll_max_options' => $post_data['poll_max_options'],
  763. 'poll_option_text' => $post_data['poll_option_text'],
  764. 'poll_start' => $post_data['poll_start'],
  765. 'poll_last_vote' => $post_data['poll_last_vote'],
  766. 'poll_vote_change' => $post_data['poll_vote_change'],
  767. 'enable_bbcode' => $post_data['enable_bbcode'],
  768. 'enable_urls' => $post_data['enable_urls'],
  769. 'enable_smilies' => $post_data['enable_smilies'],
  770. 'img_status' => $img_status
  771. );
  772. $message_parser->parse_poll($poll);
  773. $post_data['poll_options'] = (isset($poll['poll_options'])) ? $poll['poll_options'] : '';
  774. $post_data['poll_title'] = (isset($poll['poll_title'])) ? $poll['poll_title'] : '';
  775. /* We reset votes, therefore also allow removing options
  776. if ($post_data['poll_last_vote'] && ($poll['poll_options_size'] < $orig_poll_options_size))
  777. {
  778. $message_parser->warn_msg[] = $user->lang['NO_DELETE_POLL_OPTIONS'];
  779. }*/
  780. }
  781. else if (!$auth->acl_get('f_poll', $forum_id) && ($mode == 'edit') && ($post_id == $post_data['topic_first_post_id']) && ($original_poll_data['poll_title'] != ''))
  782. {
  783. // We have a poll but the editing user is not permitted to create/edit it.
  784. // So we just keep the original poll-data.
  785. $poll = array_merge($original_poll_data, array(
  786. 'enable_bbcode' => $post_data['enable_bbcode'],
  787. 'enable_urls' => $post_data['enable_urls'],
  788. 'enable_smilies' => $post_data['enable_smilies'],
  789. 'img_status' => $img_status,
  790. ));
  791. $message_parser->parse_poll($poll);
  792. $post_data['poll_options'] = (isset($poll['poll_options'])) ? $poll['poll_options'] : '';
  793. $post_data['poll_title'] = (isset($poll['poll_title'])) ? $poll['poll_title'] : '';
  794. }
  795. else
  796. {
  797. $poll = array();
  798. }
  799. // Check topic type
  800. if ($post_data['topic_type'] != POST_NORMAL && ($mode == 'post' || ($mode == 'edit' && $post_data['topic_first_post_id'] == $post_id)))
  801. {
  802. switch ($post_data['topic_type'])
  803. {
  804. case POST_GLOBAL:
  805. case POST_ANNOUNCE:
  806. $auth_option = 'f_announce';
  807. break;
  808. case POST_STICKY:
  809. $auth_option = 'f_sticky';
  810. break;
  811. default:
  812. $auth_option = '';
  813. break;
  814. }
  815. if (!$auth->acl_get($auth_option, $forum_id))
  816. {
  817. // There is a special case where a user edits his post whereby the topic type got changed by an admin/mod.
  818. // Another case would be a mod not having sticky permissions for example but edit permissions.
  819. if ($mode == 'edit')
  820. {
  821. // To prevent non-authed users messing around with the topic type we reset it to the original one.
  822. $post_data['topic_type'] = $post_data['orig_topic_type'];
  823. }
  824. else
  825. {
  826. $error[] = $user->lang['CANNOT_POST_' . str_replace('F_', '', strtoupper($auth_option))];
  827. }
  828. }
  829. }
  830. if (sizeof($message_parser->warn_msg))
  831. {
  832. $error[] = implode('<br />', $message_parser->warn_msg);
  833. }
  834. // DNSBL check
  835. if ($config['check_dnsbl'] && !$refresh)
  836. {
  837. if (($dnsbl = $user->check_dnsbl('post')) !== false)
  838. {
  839. $error[] = sprintf($user->lang['IP_BLACKLISTED'], $user->ip, $dnsbl[1]);
  840. }
  841. }
  842. // Store message, sync counters
  843. if (!sizeof($error) && $submit)
  844. {
  845. // Check if we want to de-globalize the topic... and ask for new forum
  846. if ($post_data['topic_type'] != POST_GLOBAL)
  847. {
  848. $sql = 'SELECT topic_type, forum_id
  849. FROM ' . TOPICS_TABLE . "
  850. WHERE topic_id = $topic_id";
  851. $result = $db->sql_query($sql);
  852. $row = $db->sql_fetchrow($result);
  853. $db->sql_freeresult($result);
  854. if ($row && !$row['forum_id'] && $row['topic_type'] == POST_GLOBAL)
  855. {
  856. $to_forum_id = request_var('to_forum_id', 0);
  857. if ($to_forum_id)
  858. {
  859. $sql = 'SELECT forum_type
  860. FROM ' . FORUMS_TABLE . '
  861. WHERE forum_id = ' . $to_forum_id;
  862. $result = $db->sql_query($sql);
  863. $forum_type = (int) $db->sql_fetchfield('forum_type');
  864. $db->sql_freeresult($result);
  865. if ($forum_type != FORUM_POST || !$auth->acl_get('f_post', $to_forum_id) || (!$auth->acl_get('m_approve', $to_forum_id) && !$auth->acl_get('f_noapprove', $to_forum_id)))
  866. {
  867. $to_forum_id = 0;
  868. }
  869. }
  870. if (!$to_forum_id)
  871. {
  872. include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  873. $template->assign_vars(array(
  874. 'S_FORUM_SELECT' => make_forum_select(false, false, false, true, true, true),
  875. 'S_UNGLOBALISE' => true)
  876. );
  877. $submit = false;
  878. $refresh = true;
  879. }
  880. else
  881. {
  882. if (!$auth->acl_get('f_post', $to_forum_id))
  883. {
  884. // This will only be triggered if the user tried to trick the forum.
  885. trigger_error('NOT_AUTHORISED');
  886. }
  887. $forum_id = $to_forum_id;
  888. }
  889. }
  890. }
  891. if ($submit)
  892. {
  893. // Lock/Unlock Topic
  894. $change_topic_status = $post_data['topic_status'];
  895. $perm_lock_unlock = ($auth->acl_get('m_lock', $forum_id) || ($auth->acl_get('f_user_lock', $forum_id) && $user->data['is_registered'] && !empty($post_data['topic_poster']) && $user->data['user_id'] == $post_data['topic_poster'] && $post_data['topic_status'] == ITEM_UNLOCKED)) ? true : false;
  896. if ($post_data['topic_status'] == ITEM_LOCKED && !$topic_lock && $perm_lock_unlock)
  897. {
  898. $change_topic_status = ITEM_UNLOCKED;
  899. }
  900. else if ($post_data['topic_status'] == ITEM_UNLOCKED && $topic_lock && $perm_lock_unlock)
  901. {
  902. $change_topic_status = ITEM_LOCKED;
  903. }
  904. if ($change_topic_status != $post_data['topic_status'])
  905. {
  906. $sql = 'UPDATE ' . TOPICS_TABLE . "
  907. SET topic_status = $change_topic_status
  908. WHERE topic_id = $topic_id
  909. AND topic_moved_id = 0";
  910. $db->sql_query($sql);
  911. $user_lock = ($auth->acl_get('f_user_lock', $forum_id) && $user->data['is_registered'] && $user->data['user_id'] == $post_data['topic_poster']) ? 'USER_' : '';
  912. add_log('mod', $forum_id, $topic_id, 'LOG_' . $user_lock . (($change_topic_status == ITEM_LOCKED) ? 'LOCK' : 'UNLOCK'), $post_data['topic_title']);
  913. }
  914. // Lock/Unlock Post Edit
  915. if ($mode == 'edit' && $post_data['post_edit_locked'] == ITEM_LOCKED && !$post_lock && $auth->acl_get('m_edit', $forum_id))
  916. {
  917. $post_data['post_edit_locked'] = ITEM_UNLOCKED;
  918. }
  919. else if ($mode == 'edit' && $post_data['post_edit_locked'] == ITEM_UNLOCKED && $post_lock && $auth->acl_get('m_edit', $forum_id))
  920. {
  921. $post_data['post_edit_locked'] = ITEM_LOCKED;
  922. }
  923. $data = array(
  924. 'topic_title' => (empty($post_data['topic_title'])) ? $post_data['post_subject'] : $post_data['topic_title'],
  925. 'topic_first_post_id' => (isset($post_data['topic_first_post_id'])) ? (int) $post_data['topic_first_post_id'] : 0,
  926. 'topic_last_post_id' => (isset($post_data['topic_last_post_id'])) ? (int) $post_data['topic_last_post_id'] : 0,
  927. 'topic_time_limit' => (int) $post_data['topic_time_limit'],
  928. 'topic_attachment' => (isset($post_data['topic_attachment'])) ? (int) $post_data['topic_attachment'] : 0,
  929. 'post_id' => (int) $post_id,
  930. 'topic_id' => (int) $topic_id,
  931. 'forum_id' => (int) $forum_id,
  932. 'icon_id' => (int) $post_data['icon_id'],
  933. 'poster_id' => (int) $post_data['poster_id'],
  934. 'enable_sig' => (bool) $post_data['enable_sig'],
  935. 'enable_bbcode' => (bool) $post_data['enable_bbcode'],
  936. 'enable_smilies' => (bool) $post_data['enable_smilies'],
  937. 'enable_urls' => (bool) $post_data['enable_urls'],
  938. 'enable_indexing' => (bool) $post_data['enable_indexing'],
  939. 'message_md5' => (string) $message_md5,
  940. 'post_time' => (isset($post_data['post_time'])) ? (int) $post_data['post_time'] : $current_time,
  941. 'post_checksum' => (isset($post_data['post_checksum'])) ? (string) $post_data['post_checksum'] : '',
  942. 'post_edit_reason' => $post_data['post_edit_reason'],
  943. 'post_edit_user' => ($mode == 'edit') ? $user->data['user_id'] : ((isset($post_data['post_edit_user'])) ? (int) $post_data['post_edit_user'] : 0),
  944. 'forum_parents' => $post_data['forum_parents'],
  945. 'forum_name' => $post_data['forum_name'],
  946. 'notify' => $notify,
  947. 'notify_set' => $post_data['notify_set'],
  948. 'poster_ip' => (isset($post_data['poster_ip'])) ? $post_data['poster_ip'] : $user->ip,
  949. 'post_edit_locked' => (int) $post_data['post_edit_locked'],
  950. 'bbcode_bitfield' => $message_parser->bbcode_bitfield,
  951. 'bbcode_uid' => $message_parser->bbcode_uid,
  952. 'message' => $message_parser->message,
  953. 'attachment_data' => $message_parser->attachment_data,
  954. 'filename_data' => $message_parser->filename_data,
  955. 'topic_approved' => (isset($post_data['topic_approved'])) ? $post_data['topic_approved'] : false,
  956. 'post_approved' => (isset($post_data['post_approved'])) ? $post_data['post_approved'] : false,
  957. );
  958. if ($mode == 'edit')
  959. {
  960. $data['topic_replies_real'] = $post_data['topic_replies_real'];
  961. $data['topic_replies'] = $post_data['topic_replies'];
  962. }
  963. // The last parameter tells submit_post if search indexer has to be run
  964. $redirect_url = submit_post($mode, $post_data['post_subject'], $post_data['username'], $post_data['topic_type'], $poll, $data, $update_message, ($update_message || $update_subject) ? true : false);
  965. if ($config['enable_post_confirm'] && !$user->data['is_registered'] && (isset($captcha) && $captcha->is_solved() === true) && ($mode == 'post' || $mode == 'reply' || $mode == 'quote'))
  966. {
  967. $captcha->reset();
  968. }
  969. // Check the permissions for post approval. Moderators are not affected.
  970. if ((!$auth->acl_get('f_noapprove', $data['forum_id']) && !$auth->acl_get('m_approve', $data['forum_id']) && empty($data['force_approved_state'])) || (isset($data['force_approved_state']) && !$data['force_approved_state']))
  971. {
  972. meta_refresh(10, $redirect_url);
  973. $message = ($mode == 'edit') ? $user->lang['POST_EDITED_MOD'] : $user->lang['POST_STORED_MOD'];
  974. $message .= (($user->data['user_id'] == ANONYMOUS) ? '' : ' '. $user->lang['POST_APPROVAL_NOTIFY']);
  975. }
  976. else
  977. {
  978. meta_refresh(3, $redirect_url);
  979. $message = ($mode == 'edit') ? 'POST_EDITED' : 'POST_STORED';
  980. $message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['VIEW_MESSAGE'], '<a href="' . $redirect_url . '">', '</a>');
  981. }
  982. $message .= '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $data['forum_id']) . '">', '</a>');
  983. trigger_error($message);
  984. }
  985. }
  986. }
  987. // Preview
  988. if (!sizeof($error) && $preview)
  989. {
  990. $post_data['post_time'] = ($mode == 'edit') ? $post_data['post_time'] : $current_time;
  991. $preview_message = $message_parser->format_display($post_data['enable_bbcode'], $post_data['enable_urls'], $post_data['enable_smilies'], false);
  992. $preview_signature = ($mode == 'edit') ? $post_data['user_sig'] : $user->data['user_sig'];
  993. $preview_signature_uid = ($mode == 'edit') ? $post_data['user_sig_bbcode_uid'] : $user->data['user_sig_bbcode_uid'];
  994. $preview_signature_bitfield = ($mode == 'edit') ? $post_data['user_sig_bbcode_bitfield'] : $user->data['user_sig_bbcode_bitfield'];
  995. // Signature
  996. if ($post_data['enable_sig'] && $config['allow_sig'] && $preview_signature && $auth->acl_get('f_sigs', $forum_id))
  997. {
  998. $parse_sig = new parse_message($preview_signature);
  999. $parse_sig->bbcode_uid = $preview_signature_uid;
  1000. $parse_sig->bbcode_bitfield = $preview_signature_bitfield;
  1001. // Not sure about parameters for bbcode/smilies/urls... in signatures
  1002. $parse_sig->format_display($config['allow_sig_bbcode'], $config['allow_sig_links'], $config['allow_sig_smilies']);
  1003. $preview_signature = $parse_sig->message;
  1004. unset($parse_sig);
  1005. }
  1006. else
  1007. {
  1008. $preview_signature = '';
  1009. }
  1010. $preview_subject = censor_text($post_data['post_subject']);
  1011. // Poll Preview
  1012. if (!$poll_delete && ($mode == 'post' || ($mode == 'edit' && $post_id == $post_data['topic_first_post_id']/* && (!$post_data['poll_last_vote'] || $auth->acl_get('m_edit', $forum_id))*/))
  1013. && $auth->acl_get('f_poll', $forum_id))
  1014. {
  1015. $parse_poll = new parse_message($post_data['poll_title']);
  1016. $parse_poll->bbcode_uid = $message_parser->bbcode_uid;
  1017. $parse_poll->bbcode_bitfield = $message_parser->bbcode_bitfield;
  1018. $parse_poll->format_display($post_data['enable_bbcode'], $post_data['enable_urls'], $post_data['enable_smilies']);
  1019. if ($post_data['poll_length'])
  1020. {
  1021. $poll_end = ($post_data['poll_length'] * 86400) + (($post_data['poll_start']) ? $post_data['poll_start'] : time());
  1022. }
  1023. $template->assign_vars(array(
  1024. 'S_HAS_POLL_OPTIONS' => (sizeof($post_data['poll_options'])),
  1025. 'S_IS_MULTI_CHOICE' => ($post_data['poll_max_options'] > 1) ? true : false,
  1026. 'POLL_QUESTION' => $parse_poll->message,
  1027. 'L_POLL_LENGTH' => ($post_data['poll_length']) ? sprintf($user->lang['POLL_RUN_TILL'], $user->format_date($poll_end)) : '',
  1028. 'L_MAX_VOTES' => ($post_data['poll_max_options'] == 1) ? $user->lang['MAX_OPTION_SELECT'] : sprintf($user->lang['MAX_OPTIONS_SELECT'], $post_data['poll_max_options']))
  1029. );
  1030. $parse_poll->message = implode("\n", $post_data['poll_options']);
  1031. $parse_poll->format_display($post_data['enable_bbcode'], $post_data['enable_urls'], $post_data['enable_smilies']);
  1032. $preview_poll_options = explode('<br />', $parse_poll->message);
  1033. unset($parse_poll);
  1034. foreach ($preview_poll_options as $key => $option)
  1035. {
  1036. $template->assign_block_vars('poll_option', array(
  1037. 'POLL_OPTION_CAPTION' => $option,
  1038. 'POLL_OPTION_ID' => $key + 1)
  1039. );
  1040. }
  1041. unset($preview_poll_options);
  1042. }
  1043. // Attachment Preview
  1044. if (sizeof($message_parser->attachment_data))
  1045. {
  1046. $template->assign_var('S_HAS_ATTACHMENTS', true);
  1047. $update_count = array();
  1048. $attachment_data = $message_parser->attachment_data;
  1049. parse_attachments($forum_id, $preview_message, $attachment_data, $update_count, true);
  1050. foreach ($attachment_data as $i => $attachment)
  1051. {
  1052. $template->assign_block_vars('attachment', array(
  1053. 'DISPLAY_ATTACHMENT' => $attachment)
  1054. );
  1055. }
  1056. unset($attachment_data);
  1057. }
  1058. if (!sizeof($error))
  1059. {
  1060. $template->assign_vars(array(
  1061. 'PREVIEW_SUBJECT' => $preview_subject,
  1062. 'PREVIEW_MESSAGE' => $preview_message,
  1063. 'PREVIEW_SIGNATURE' => $preview_signature,
  1064. 'S_DISPLAY_PREVIEW' => true)
  1065. );
  1066. }
  1067. }
  1068. // Decode text for message display
  1069. $post_data['bbcode_uid'] = ($mode == 'quote' && !$preview && !$refresh && !sizeof($error)) ? $post_data['bbcode_uid'] : $message_parser->bbcode_uid;
  1070. $message_parser->decode_message($post_data['bbcode_uid']);
  1071. if ($mode == 'quote' && !$submit && !$preview && !$refresh)
  1072. {
  1073. if ($config['allow_bbcode'])
  1074. {
  1075. $message_parser->message = '[quote=&quot;' . $post_data['quote_username'] . '&quot;]' . censor_text(trim($message_parser->message)) . "[/quote]\n";
  1076. }
  1077. else
  1078. {
  1079. $offset = 0;
  1080. $quote_string = "&gt; ";
  1081. $message = censor_text(trim($message_parser->message));
  1082. // see if we are nesting. It's easily tricked but should work for one level of nesting
  1083. if (strpos($message, "&gt;") !== false)
  1084. {
  1085. $offset = 10;
  1086. }
  1087. $message = utf8_wordwrap($message, 75 + $offset, "\n");
  1088. $message = $quote_string . $message;
  1089. $message = str_replace("\n", "\n" . $quote_string, $message);
  1090. $message_parser->message = $post_data['quote_username'] . " " . $user->lang['WROTE'] . ":\n" . $message . "\n";
  1091. }
  1092. }
  1093. if (($mode == 'reply' || $mode == 'quote') && !$submit && !$preview && !$refresh)
  1094. {
  1095. $post_data['post_subject'] = ((strpos($post_data['post_subject'], 'Re: ') !== 0) ? 'Re: ' : '') . censor_text($post_data['post_subject']);
  1096. }
  1097. $attachment_data = $message_parser->attachment_data;
  1098. $filename_data = $message_parser->filename_data;
  1099. $post_data['post_text'] = $message_parser->message;
  1100. if (sizeof($post_data['poll_options']) || !empty($post_data['poll_title']))
  1101. {
  1102. $message_parser->message = $post_data['poll_title'];
  1103. $message_parser->bbcode_uid = $post_data['bbcode_uid'];
  1104. $message_parser->decode_message();
  1105. $post_data['poll_title'] = $message_parser->message;
  1106. $message_parser->message = implode("\n", $post_data['poll_options']);
  1107. $message_parser->decode_message();
  1108. $post_data['poll_options'] = explode("\n", $message_parser->message);
  1109. }
  1110. // MAIN POSTING PAGE BEGINS HERE
  1111. // Forum moderators?
  1112. $moderators = array();
  1113. if ($config['load_moderators'])
  1114. {
  1115. get_moderators($moderators, $forum_id);
  1116. }
  1117. // Generate smiley listing
  1118. generate_smilies('inline', $forum_id);
  1119. // Generate inline attachment select box
  1120. posting_gen_inline_attachments($attachment_data);
  1121. // Do show topic type selection only in first post.
  1122. $topic_type_toggle = false;
  1123. if ($mode == 'post' || ($mode == 'edit' && $post_id == $post_data['topic_first_post_id']))
  1124. {
  1125. $topic_type_toggle = posting_gen_topic_types($forum_id, $post_data['topic_type']);
  1126. }
  1127. $s_topic_icons = false;
  1128. if ($post_data['enable_icons'] && $auth->acl_get('f_icons', $forum_id))
  1129. {
  1130. $s_topic_icons = posting_gen_topic_icons($mode, $post_data['icon_id']);
  1131. }
  1132. $bbcode_checked = (isset($post_data['enable_bbcode'])) ? !$post_data['enable_bbcode'] : (($config['allow_bbcode']) ? !$user->optionget('bbcode') : 1);
  1133. $smilies_checked = (isset($post_data['enable_smilies'])) ? !$post_data['enable_smilies'] : (($config['allow_smilies']) ? !$user->optionget('smilies') : 1);
  1134. $urls_checked = (isset($post_data['enable_urls'])) ? !$post_data['enable_urls'] : 0;
  1135. $sig_checked = $post_data['enable_sig'];
  1136. $lock_topic_checked = (isset($topic_lock) && $topic_lock) ? $topic_lock : (($post_data['topic_status'] == ITEM_LOCKED) ? 1 : 0);
  1137. $lock_post_checked = (isset($post_lock)) ? $post_lock : $post_data['post_edit_locked'];
  1138. // If the user is replying or posting and not already watching this topic but set to always being notified we need to overwrite this setting
  1139. $notify_set = ($mode != 'edit' && $config['allow_topic_notify'] && $user->data['is_registered'] && !$post_data['notify_set']) ? $user->data['user_notify'] : $post_data['notify_set'];
  1140. $notify_checked = (isset($notify)) ? $notify : (($mode == 'post') ? $user->data['user_notify'] : $notify_set);
  1141. // Page title & action URL, include session_id for security purpose
  1142. $s_action = append_sid("{$phpbb_root_path}posting.$phpEx", "mode=$mode&amp;f=$forum_id", true, $user->session_id);
  1143. $s_action .= ($topic_id) ? "&amp;t=$topic_id" : '';
  1144. $s_action .= ($post_id) ? "&amp;p=$post_id" : '';
  1145. switch ($mode)
  1146. {
  1147. case 'post':
  1148. $page_title = $user->lang['POST_TOPIC'];
  1149. break;
  1150. case 'quote':
  1151. case 'reply':
  1152. $page_title = $user->lang['POST_REPLY'];
  1153. break;
  1154. case 'delete':
  1155. case 'edit':
  1156. $page_title = $user->lang['EDIT_POST'];
  1157. break;
  1158. }
  1159. // Build Navigation Links
  1160. generate_forum_nav($post_data);
  1161. // Build Forum Rules
  1162. generate_forum_rules($post_data);
  1163. // Posting uses is_solved for legacy reasons. Plugins have to use is_solved to force themselves to be displayed.
  1164. if ($config['enable_post_confirm'] && !$user->data['is_registered'] && (isset($captcha) && $captcha->is_solved() === false) && ($mode == 'post' || $mode == 'reply' || $mode == 'quote'))
  1165. {
  1166. $template->assign_vars(array(
  1167. 'S_CONFIRM_CODE' => true,
  1168. 'CAPTCHA_TEMPLATE' => $captcha->get_template(),
  1169. ));
  1170. }
  1171. $s_hidden_fields = ($mode == 'reply' || $mode == 'quote') ? '<input type="hidden" name="topic_cur_post_id" value="' . $post_data['topic_last_post_id'] . '" />' : '';
  1172. $s_hidden_fields .= '<input type="hidden" name="lastclick" value="' . $current_time . '" />';
  1173. $s_hidden_fields .= ($draft_id || isset($_REQUEST['draft_loaded'])) ? '<input type="hidden" name="draft_loaded" value="' . request_var('draft_loaded', $draft_id) . '" />' : '';
  1174. if ($mode == 'edit')
  1175. {
  1176. $s_hidden_fields .= build_hidden_fields(array(
  1177. 'edit_post_message_checksum' => $post_data['post_checksum'],
  1178. 'edit_post_subject_checksum' => $post_data['post_subject_md5'],
  1179. ));
  1180. }
  1181. // Add the confirm id/code pair to the hidden fields, else an error is displayed on next submit/preview
  1182. if (isset($captcha) && $captcha->is_solved() !== false)
  1183. {
  1184. $s_hidden_fields .= build_hidden_fields($captcha->get_hidden_fields());
  1185. }
  1186. $form_enctype = (@ini_get('file_uploads') == '0' || strtolower(@ini_get('file_uploads')) == 'off' || !$config['allow_attachments'] || !$auth->acl_get('u_attach') || !$auth->acl_get('f_attach', $forum_id)) ? '' : ' enctype="multipart/form-data"';
  1187. add_form_key('posting');
  1188. // Start assigning vars for main posting page ...
  1189. $template->assign_vars(array(
  1190. 'L_POST_A' => $page_title,
  1191. 'L_ICON' => ($mode == 'reply' || $mode == 'quote' || ($mode =