PageRenderTime 33ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/phpBB/posting.php

https://github.com/naderman/phpbb-orchestra
PHP | 1404 lines | 1073 code | 223 blank | 108 comment | 484 complexity | 433ff2f8a563dbb8322ec9861e477c9e 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. $meta_url = phpbb_bump_topic($forum_id, $topic_id, $post_data, $current_time);
  272. meta_refresh(3, $meta_url);
  273. $message = $user->lang['TOPIC_BUMPED'] . '<br /><br />' . sprintf($user->lang['VIEW_MESSAGE'], '<a href="' . $meta_url . '">', '</a>');
  274. $message .= '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id) . '">', '</a>');
  275. trigger_error($message);
  276. }
  277. trigger_error('BUMP_ERROR');
  278. }
  279. // Subject length limiting to 60 characters if first post...
  280. if ($mode == 'post' || ($mode == 'edit' && $post_data['topic_first_post_id'] == $post_data['post_id']))
  281. {
  282. $template->assign_var('S_NEW_MESSAGE', true);
  283. }
  284. // Determine some vars
  285. if (isset($post_data['poster_id']) && $post_data['poster_id'] == ANONYMOUS)
  286. {
  287. $post_data['quote_username'] = (!empty($post_data['post_username'])) ? $post_data['post_username'] : $user->lang['GUEST'];
  288. }
  289. else
  290. {
  291. $post_data['quote_username'] = isset($post_data['username']) ? $post_data['username'] : '';
  292. }
  293. $post_data['post_edit_locked'] = (isset($post_data['post_edit_locked'])) ? (int) $post_data['post_edit_locked'] : 0;
  294. $post_data['post_subject_md5'] = (isset($post_data['post_subject']) && $mode == 'edit') ? md5($post_data['post_subject']) : '';
  295. $post_data['post_subject'] = (in_array($mode, array('quote', 'edit'))) ? $post_data['post_subject'] : ((isset($post_data['topic_title'])) ? $post_data['topic_title'] : '');
  296. $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;
  297. $post_data['poll_length'] = (!empty($post_data['poll_length'])) ? (int) $post_data['poll_length'] / 86400 : 0;
  298. $post_data['poll_start'] = (!empty($post_data['poll_start'])) ? (int) $post_data['poll_start'] : 0;
  299. $post_data['icon_id'] = (!isset($post_data['icon_id']) || in_array($mode, array('quote', 'reply'))) ? 0 : (int) $post_data['icon_id'];
  300. $post_data['poll_options'] = array();
  301. // Get Poll Data
  302. if ($post_data['poll_start'])
  303. {
  304. $sql = 'SELECT poll_option_text
  305. FROM ' . POLL_OPTIONS_TABLE . "
  306. WHERE topic_id = $topic_id
  307. ORDER BY poll_option_id";
  308. $result = $db->sql_query($sql);
  309. while ($row = $db->sql_fetchrow($result))
  310. {
  311. $post_data['poll_options'][] = trim($row['poll_option_text']);
  312. }
  313. $db->sql_freeresult($result);
  314. }
  315. if ($mode == 'edit')
  316. {
  317. $original_poll_data = array(
  318. 'poll_title' => $post_data['poll_title'],
  319. 'poll_length' => $post_data['poll_length'],
  320. 'poll_max_options' => $post_data['poll_max_options'],
  321. 'poll_option_text' => implode("\n", $post_data['poll_options']),
  322. 'poll_start' => $post_data['poll_start'],
  323. 'poll_last_vote' => $post_data['poll_last_vote'],
  324. 'poll_vote_change' => $post_data['poll_vote_change'],
  325. );
  326. }
  327. $orig_poll_options_size = sizeof($post_data['poll_options']);
  328. $message_parser = new parse_message();
  329. if (isset($post_data['post_text']))
  330. {
  331. $message_parser->message = &$post_data['post_text'];
  332. unset($post_data['post_text']);
  333. }
  334. // Set some default variables
  335. $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);
  336. foreach ($uninit as $var_name => $default_value)
  337. {
  338. if (!isset($post_data[$var_name]))
  339. {
  340. $post_data[$var_name] = $default_value;
  341. }
  342. }
  343. unset($uninit);
  344. // Always check if the submitted attachment data is valid and belongs to the user.
  345. // Further down (especially in submit_post()) we do not check this again.
  346. $message_parser->get_submitted_attachment_data($post_data['poster_id']);
  347. if ($post_data['post_attachment'] && !$submit && !$refresh && !$preview && $mode == 'edit')
  348. {
  349. // Do not change to SELECT *
  350. $sql = 'SELECT attach_id, is_orphan, attach_comment, real_filename
  351. FROM ' . ATTACHMENTS_TABLE . "
  352. WHERE post_msg_id = $post_id
  353. AND in_message = 0
  354. AND is_orphan = 0
  355. ORDER BY filetime DESC";
  356. $result = $db->sql_query($sql);
  357. $message_parser->attachment_data = array_merge($message_parser->attachment_data, $db->sql_fetchrowset($result));
  358. $db->sql_freeresult($result);
  359. }
  360. if ($post_data['poster_id'] == ANONYMOUS)
  361. {
  362. $post_data['username'] = ($mode == 'quote' || $mode == 'edit') ? trim($post_data['post_username']) : '';
  363. }
  364. else
  365. {
  366. $post_data['username'] = ($mode == 'quote' || $mode == 'edit') ? trim($post_data['username']) : '';
  367. }
  368. $post_data['enable_urls'] = $post_data['enable_magic_url'];
  369. if ($mode != 'edit')
  370. {
  371. $post_data['enable_sig'] = ($config['allow_sig'] && $user->optionget('attachsig')) ? true: false;
  372. $post_data['enable_smilies'] = ($config['allow_smilies'] && $user->optionget('smilies')) ? true : false;
  373. $post_data['enable_bbcode'] = ($config['allow_bbcode'] && $user->optionget('bbcode')) ? true : false;
  374. $post_data['enable_urls'] = true;
  375. }
  376. $post_data['enable_magic_url'] = $post_data['drafts'] = false;
  377. // User own some drafts?
  378. if ($user->data['is_registered'] && $auth->acl_get('u_savedrafts') && ($mode == 'reply' || $mode == 'post' || $mode == 'quote'))
  379. {
  380. $sql = 'SELECT draft_id
  381. FROM ' . DRAFTS_TABLE . '
  382. WHERE user_id = ' . $user->data['user_id'] .
  383. (($forum_id) ? ' AND forum_id = ' . (int) $forum_id : '') .
  384. (($topic_id) ? ' AND topic_id = ' . (int) $topic_id : '') .
  385. (($draft_id) ? " AND draft_id <> $draft_id" : '');
  386. $result = $db->sql_query_limit($sql, 1);
  387. if ($db->sql_fetchrow($result))
  388. {
  389. $post_data['drafts'] = true;
  390. }
  391. $db->sql_freeresult($result);
  392. }
  393. $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);
  394. // Check if user is watching this topic
  395. if ($mode != 'post' && $config['allow_topic_notify'] && $user->data['is_registered'])
  396. {
  397. $sql = 'SELECT topic_id
  398. FROM ' . TOPICS_WATCH_TABLE . '
  399. WHERE topic_id = ' . $topic_id . '
  400. AND user_id = ' . $user->data['user_id'];
  401. $result = $db->sql_query($sql);
  402. $post_data['notify_set'] = (int) $db->sql_fetchfield('topic_id');
  403. $db->sql_freeresult($result);
  404. }
  405. // Do we want to edit our post ?
  406. if ($mode == 'edit' && $post_data['bbcode_uid'])
  407. {
  408. $message_parser->bbcode_uid = $post_data['bbcode_uid'];
  409. }
  410. // HTML, BBCode, Smilies, Images and Flash status
  411. $bbcode_status = ($config['allow_bbcode'] && $auth->acl_get('f_bbcode', $forum_id)) ? true : false;
  412. $smilies_status = ($config['allow_smilies'] && $auth->acl_get('f_smilies', $forum_id)) ? true : false;
  413. $img_status = ($bbcode_status && $auth->acl_get('f_img', $forum_id)) ? true : false;
  414. $url_status = ($config['allow_post_links']) ? true : false;
  415. $flash_status = ($bbcode_status && $auth->acl_get('f_flash', $forum_id) && $config['allow_post_flash']) ? true : false;
  416. $quote_status = true;
  417. // Save Draft
  418. if ($save && $user->data['is_registered'] && $auth->acl_get('u_savedrafts') && ($mode == 'reply' || $mode == 'post' || $mode == 'quote'))
  419. {
  420. $subject = utf8_normalize_nfc(request_var('subject', '', true));
  421. $subject = (!$subject && $mode != 'post') ? $post_data['topic_title'] : $subject;
  422. $message = utf8_normalize_nfc(request_var('message', '', true));
  423. if ($subject && $message)
  424. {
  425. if (confirm_box(true))
  426. {
  427. $sql = 'INSERT INTO ' . DRAFTS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  428. 'user_id' => (int) $user->data['user_id'],
  429. 'topic_id' => (int) $topic_id,
  430. 'forum_id' => (int) $forum_id,
  431. 'save_time' => (int) $current_time,
  432. 'draft_subject' => (string) $subject,
  433. 'draft_message' => (string) $message)
  434. );
  435. $db->sql_query($sql);
  436. $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");
  437. meta_refresh(3, $meta_info);
  438. $message = $user->lang['DRAFT_SAVED'] . '<br /><br />';
  439. $message .= ($mode != 'post') ? sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $meta_info . '">', '</a>') . '<br /><br />' : '';
  440. $message .= sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id) . '">', '</a>');
  441. trigger_error($message);
  442. }
  443. else
  444. {
  445. $s_hidden_fields = build_hidden_fields(array(
  446. 'mode' => $mode,
  447. 'save' => true,
  448. 'f' => $forum_id,
  449. 't' => $topic_id,
  450. 'subject' => $subject,
  451. 'message' => $message,
  452. 'attachment_data' => $message_parser->attachment_data,
  453. )
  454. );
  455. $hidden_fields = array(
  456. 'icon_id' => 0,
  457. 'disable_bbcode' => false,
  458. 'disable_smilies' => false,
  459. 'disable_magic_url' => false,
  460. 'attach_sig' => true,
  461. 'lock_topic' => false,
  462. 'topic_type' => POST_NORMAL,
  463. 'topic_time_limit' => 0,
  464. 'poll_title' => '',
  465. 'poll_option_text' => '',
  466. 'poll_max_options' => 1,
  467. 'poll_length' => 0,
  468. 'poll_vote_change' => false,
  469. );
  470. foreach ($hidden_fields as $name => $default)
  471. {
  472. if (!isset($_POST[$name]))
  473. {
  474. // Don't include it, if its not available
  475. unset($hidden_fields[$name]);
  476. continue;
  477. }
  478. if (is_bool($default))
  479. {
  480. // Use the string representation
  481. $hidden_fields[$name] = request_var($name, '');
  482. }
  483. else
  484. {
  485. $hidden_fields[$name] = request_var($name, $default);
  486. }
  487. }
  488. $s_hidden_fields .= build_hidden_fields($hidden_fields);
  489. confirm_box(false, 'SAVE_DRAFT', $s_hidden_fields);
  490. }
  491. }
  492. else
  493. {
  494. if (utf8_clean_string($subject) === '')
  495. {
  496. $error[] = $user->lang['EMPTY_SUBJECT'];
  497. }
  498. if (utf8_clean_string($message) === '')
  499. {
  500. $error[] = $user->lang['TOO_FEW_CHARS'];
  501. }
  502. }
  503. unset($subject, $message);
  504. }
  505. // Load requested Draft
  506. if ($draft_id && ($mode == 'reply' || $mode == 'quote' || $mode == 'post') && $user->data['is_registered'] && $auth->acl_get('u_savedrafts'))
  507. {
  508. $sql = 'SELECT draft_subject, draft_message
  509. FROM ' . DRAFTS_TABLE . "
  510. WHERE draft_id = $draft_id
  511. AND user_id = " . $user->data['user_id'];
  512. $result = $db->sql_query_limit($sql, 1);
  513. $row = $db->sql_fetchrow($result);
  514. $db->sql_freeresult($result);
  515. if ($row)
  516. {
  517. $post_data['post_subject'] = $row['draft_subject'];
  518. $message_parser->message = $row['draft_message'];
  519. $template->assign_var('S_DRAFT_LOADED', true);
  520. }
  521. else
  522. {
  523. $draft_id = 0;
  524. }
  525. }
  526. // Load draft overview
  527. if ($load && ($mode == 'reply' || $mode == 'quote' || $mode == 'post') && $post_data['drafts'])
  528. {
  529. load_drafts($topic_id, $forum_id);
  530. }
  531. if ($submit || $preview || $refresh)
  532. {
  533. $post_data['topic_cur_post_id'] = request_var('topic_cur_post_id', 0);
  534. $post_data['post_subject'] = utf8_normalize_nfc(request_var('subject', '', true));
  535. $message_parser->message = utf8_normalize_nfc(request_var('message', '', true));
  536. $post_data['username'] = utf8_normalize_nfc(request_var('username', $post_data['username'], true));
  537. $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)) : '';
  538. $post_data['orig_topic_type'] = $post_data['topic_type'];
  539. $post_data['topic_type'] = request_var('topic_type', (($mode != 'post') ? (int) $post_data['topic_type'] : POST_NORMAL));
  540. $post_data['topic_time_limit'] = request_var('topic_time_limit', (($mode != 'post') ? (int) $post_data['topic_time_limit'] : 0));
  541. if ($post_data['enable_icons'] && $auth->acl_get('f_icons', $forum_id))
  542. {
  543. $post_data['icon_id'] = request_var('icon', (int) $post_data['icon_id']);
  544. }
  545. $post_data['enable_bbcode'] = (!$bbcode_status || isset($_POST['disable_bbcode'])) ? false : true;
  546. $post_data['enable_smilies'] = (!$smilies_status || isset($_POST['disable_smilies'])) ? false : true;
  547. $post_data['enable_urls'] = (isset($_POST['disable_magic_url'])) ? 0 : 1;
  548. $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);
  549. if ($config['allow_topic_notify'] && $user->data['is_registered'])
  550. {
  551. $notify = (isset($_POST['notify'])) ? true : false;
  552. }
  553. else
  554. {
  555. $notify = false;
  556. }
  557. $topic_lock = (isset($_POST['lock_topic'])) ? true : false;
  558. $post_lock = (isset($_POST['lock_post'])) ? true : false;
  559. $poll_delete = (isset($_POST['poll_delete'])) ? true : false;
  560. if ($submit)
  561. {
  562. $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);
  563. $status_switch = ($status_switch != $check_value);
  564. }
  565. else
  566. {
  567. $status_switch = 1;
  568. }
  569. // Delete Poll
  570. if ($poll_delete && $mode == 'edit' && sizeof($post_data['poll_options']) &&
  571. ((!$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)))
  572. {
  573. if ($submit && check_form_key('posting'))
  574. {
  575. $sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . "
  576. WHERE topic_id = $topic_id";
  577. $db->sql_query($sql);
  578. $sql = 'DELETE FROM ' . POLL_VOTES_TABLE . "
  579. WHERE topic_id = $topic_id";
  580. $db->sql_query($sql);
  581. $topic_sql = array(
  582. 'poll_title' => '',
  583. 'poll_start' => 0,
  584. 'poll_length' => 0,
  585. 'poll_last_vote' => 0,
  586. 'poll_max_options' => 0,
  587. 'poll_vote_change' => 0
  588. );
  589. $sql = 'UPDATE ' . TOPICS_TABLE . '
  590. SET ' . $db->sql_build_array('UPDATE', $topic_sql) . "
  591. WHERE topic_id = $topic_id";
  592. $db->sql_query($sql);
  593. }
  594. $post_data['poll_title'] = $post_data['poll_option_text'] = '';
  595. $post_data['poll_vote_change'] = $post_data['poll_max_options'] = $post_data['poll_length'] = 0;
  596. }
  597. else
  598. {
  599. $post_data['poll_title'] = utf8_normalize_nfc(request_var('poll_title', '', true));
  600. $post_data['poll_length'] = request_var('poll_length', 0);
  601. $post_data['poll_option_text'] = utf8_normalize_nfc(request_var('poll_option_text', '', true));
  602. $post_data['poll_max_options'] = request_var('poll_max_options', 1);
  603. $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;
  604. }
  605. // If replying/quoting and last post id has changed
  606. // give user option to continue submit or return to post
  607. // notify and show user the post made between his request and the final submit
  608. if (($mode == 'reply' || $mode == 'quote') && $post_data['topic_cur_post_id'] && $post_data['topic_cur_post_id'] != $post_data['topic_last_post_id'])
  609. {
  610. // Only do so if it is allowed forum-wide
  611. if ($post_data['forum_flags'] & FORUM_FLAG_POST_REVIEW)
  612. {
  613. if (topic_review($topic_id, $forum_id, 'post_review', $post_data['topic_cur_post_id']))
  614. {
  615. $template->assign_var('S_POST_REVIEW', true);
  616. }
  617. $submit = false;
  618. $refresh = true;
  619. }
  620. }
  621. // Parse Attachments - before checksum is calculated
  622. $message_parser->parse_attachments('fileupload', $mode, $forum_id, $submit, $preview, $refresh);
  623. // Grab md5 'checksum' of new message
  624. $message_md5 = md5($message_parser->message);
  625. // If editing and checksum has changed we know the post was edited while we're editing
  626. // Notify and show user the changed post
  627. if ($mode == 'edit' && $post_data['forum_flags'] & FORUM_FLAG_POST_REVIEW)
  628. {
  629. $edit_post_message_checksum = request_var('edit_post_message_checksum', '');
  630. $edit_post_subject_checksum = request_var('edit_post_subject_checksum', '');
  631. // $post_data['post_checksum'] is the checksum of the post submitted in the meantime
  632. // $message_md5 is the checksum of the post we're about to submit
  633. // $edit_post_message_checksum is the checksum of the post we're editing
  634. // ...
  635. // We make sure nobody else made exactly the same change
  636. // we're about to submit by also checking $message_md5 != $post_data['post_checksum']
  637. if (($edit_post_message_checksum !== '' && $edit_post_message_checksum != $post_data['post_checksum'] && $message_md5 != $post_data['post_checksum'])
  638. || ($edit_post_subject_checksum !== '' && $edit_post_subject_checksum != $post_data['post_subject_md5'] && md5($post_data['post_subject']) != $post_data['post_subject_md5']))
  639. {
  640. if (topic_review($topic_id, $forum_id, 'post_review_edit', $post_id))
  641. {
  642. $template->assign_vars(array(
  643. 'S_POST_REVIEW' => true,
  644. 'L_POST_REVIEW' => $user->lang['POST_REVIEW_EDIT'],
  645. 'L_POST_REVIEW_EXPLAIN' => $user->lang['POST_REVIEW_EDIT_EXPLAIN'],
  646. ));
  647. }
  648. $submit = false;
  649. $refresh = true;
  650. }
  651. }
  652. // Check checksum ... don't re-parse message if the same
  653. $update_message = ($mode != 'edit' || $message_md5 != $post_data['post_checksum'] || $status_switch || strlen($post_data['bbcode_uid']) < BBCODE_UID_LEN) ? true : false;
  654. // Also check if subject got updated...
  655. $update_subject = $mode != 'edit' || ($post_data['post_subject_md5'] && $post_data['post_subject_md5'] != md5($post_data['post_subject']));
  656. // Parse message
  657. if ($update_message)
  658. {
  659. if (sizeof($message_parser->warn_msg))
  660. {
  661. $error[] = implode('<br />', $message_parser->warn_msg);
  662. $message_parser->warn_msg = array();
  663. }
  664. $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']);
  665. // On a refresh we do not care about message parsing errors
  666. if (sizeof($message_parser->warn_msg) && $refresh)
  667. {
  668. $message_parser->warn_msg = array();
  669. }
  670. }
  671. else
  672. {
  673. $message_parser->bbcode_bitfield = $post_data['bbcode_bitfield'];
  674. }
  675. if ($mode != 'edit' && !$preview && !$refresh && $config['flood_interval'] && !$auth->acl_get('f_ignoreflood', $forum_id))
  676. {
  677. // Flood check
  678. $last_post_time = 0;
  679. if ($user->data['is_registered'])
  680. {
  681. $last_post_time = $user->data['user_lastpost_time'];
  682. }
  683. else
  684. {
  685. $sql = 'SELECT post_time AS last_post_time
  686. FROM ' . POSTS_TABLE . "
  687. WHERE poster_ip = '" . $user->ip . "'
  688. AND post_time > " . ($current_time - $config['flood_interval']);
  689. $result = $db->sql_query_limit($sql, 1);
  690. if ($row = $db->sql_fetchrow($result))
  691. {
  692. $last_post_time = $row['last_post_time'];
  693. }
  694. $db->sql_freeresult($result);
  695. }
  696. if ($last_post_time && ($current_time - $last_post_time) < intval($config['flood_interval']))
  697. {
  698. $error[] = $user->lang['FLOOD_ERROR'];
  699. }
  700. }
  701. // Validate username
  702. 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']))
  703. {
  704. include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  705. $user->add_lang('ucp');
  706. if (($result = validate_username($post_data['username'], (!empty($post_data['post_username'])) ? $post_data['post_username'] : '')) !== false)
  707. {
  708. $error[] = $user->lang[$result . '_USERNAME'];
  709. }
  710. if (($result = validate_string($post_data['username'], false, $config['min_name_chars'], $config['max_name_chars'])) !== false)
  711. {
  712. $min_max_amount = ($result == 'TOO_SHORT') ? $config['min_name_chars'] : $config['max_name_chars'];
  713. $error[] = sprintf($user->lang['FIELD_' . $result], $user->lang['USERNAME'], $min_max_amount);
  714. }
  715. }
  716. if ($config['enable_post_confirm'] && !$user->data['is_registered'] && in_array($mode, array('quote', 'post', 'reply')))
  717. {
  718. $captcha_data = array(
  719. 'message' => utf8_normalize_nfc(request_var('message', '', true)),
  720. 'subject' => utf8_normalize_nfc(request_var('subject', '', true)),
  721. 'username' => utf8_normalize_nfc(request_var('username', '', true)),
  722. );
  723. $vc_response = $captcha->validate($captcha_data);
  724. if ($vc_response)
  725. {
  726. $error[] = $vc_response;
  727. }
  728. }
  729. // check form
  730. if (($submit || $preview) && !check_form_key('posting'))
  731. {
  732. $error[] = $user->lang['FORM_INVALID'];
  733. }
  734. // Parse subject
  735. if (!$preview && !$refresh && utf8_clean_string($post_data['post_subject']) === '' && ($mode == 'post' || ($mode == 'edit' && $post_data['topic_first_post_id'] == $post_id)))
  736. {
  737. $error[] = $user->lang['EMPTY_SUBJECT'];
  738. }
  739. $post_data['poll_last_vote'] = (isset($post_data['poll_last_vote'])) ? $post_data['poll_last_vote'] : 0;
  740. if ($post_data['poll_option_text'] &&
  741. ($mode == 'post' || ($mode == 'edit' && $post_id == $post_data['topic_first_post_id']/* && (!$post_data['poll_last_vote'] || $auth->acl_get('m_edit', $forum_id))*/))
  742. && $auth->acl_get('f_poll', $forum_id))
  743. {
  744. $poll = array(
  745. 'poll_title' => $post_data['poll_title'],
  746. 'poll_length' => $post_data['poll_length'],
  747. 'poll_max_options' => $post_data['poll_max_options'],
  748. 'poll_option_text' => $post_data['poll_option_text'],
  749. 'poll_start' => $post_data['poll_start'],
  750. 'poll_last_vote' => $post_data['poll_last_vote'],
  751. 'poll_vote_change' => $post_data['poll_vote_change'],
  752. 'enable_bbcode' => $post_data['enable_bbcode'],
  753. 'enable_urls' => $post_data['enable_urls'],
  754. 'enable_smilies' => $post_data['enable_smilies'],
  755. 'img_status' => $img_status
  756. );
  757. $message_parser->parse_poll($poll);
  758. $post_data['poll_options'] = (isset($poll['poll_options'])) ? $poll['poll_options'] : '';
  759. $post_data['poll_title'] = (isset($poll['poll_title'])) ? $poll['poll_title'] : '';
  760. /* We reset votes, therefore also allow removing options
  761. if ($post_data['poll_last_vote'] && ($poll['poll_options_size'] < $orig_poll_options_size))
  762. {
  763. $message_parser->warn_msg[] = $user->lang['NO_DELETE_POLL_OPTIONS'];
  764. }*/
  765. }
  766. else if (!$auth->acl_get('f_poll', $forum_id) && ($mode == 'edit') && ($post_id == $post_data['topic_first_post_id']) && ($original_poll_data['poll_title'] != ''))
  767. {
  768. // We have a poll but the editing user is not permitted to create/edit it.
  769. // So we just keep the original poll-data.
  770. $poll = array_merge($original_poll_data, array(
  771. 'enable_bbcode' => $post_data['enable_bbcode'],
  772. 'enable_urls' => $post_data['enable_urls'],
  773. 'enable_smilies' => $post_data['enable_smilies'],
  774. 'img_status' => $img_status,
  775. ));
  776. $message_parser->parse_poll($poll);
  777. $post_data['poll_options'] = (isset($poll['poll_options'])) ? $poll['poll_options'] : '';
  778. $post_data['poll_title'] = (isset($poll['poll_title'])) ? $poll['poll_title'] : '';
  779. }
  780. else
  781. {
  782. $poll = array();
  783. }
  784. // Check topic type
  785. if ($post_data['topic_type'] != POST_NORMAL && ($mode == 'post' || ($mode == 'edit' && $post_data['topic_first_post_id'] == $post_id)))
  786. {
  787. switch ($post_data['topic_type'])
  788. {
  789. case POST_GLOBAL:
  790. case POST_ANNOUNCE:
  791. $auth_option = 'f_announce';
  792. break;
  793. case POST_STICKY:
  794. $auth_option = 'f_sticky';
  795. break;
  796. default:
  797. $auth_option = '';
  798. break;
  799. }
  800. if (!$auth->acl_get($auth_option, $forum_id))
  801. {
  802. // There is a special case where a user edits his post whereby the topic type got changed by an admin/mod.
  803. // Another case would be a mod not having sticky permissions for example but edit permissions.
  804. if ($mode == 'edit')
  805. {
  806. // To prevent non-authed users messing around with the topic type we reset it to the original one.
  807. $post_data['topic_type'] = $post_data['orig_topic_type'];
  808. }
  809. else
  810. {
  811. $error[] = $user->lang['CANNOT_POST_' . str_replace('F_', '', strtoupper($auth_option))];
  812. }
  813. }
  814. }
  815. if (sizeof($message_parser->warn_msg))
  816. {
  817. $error[] = implode('<br />', $message_parser->warn_msg);
  818. }
  819. // DNSBL check
  820. if ($config['check_dnsbl'] && !$refresh)
  821. {
  822. if (($dnsbl = $user->check_dnsbl('post')) !== false)
  823. {
  824. $error[] = sprintf($user->lang['IP_BLACKLISTED'], $user->ip, $dnsbl[1]);
  825. }
  826. }
  827. // Store message, sync counters
  828. if (!sizeof($error) && $submit)
  829. {
  830. // Check if we want to de-globalize the topic... and ask for new forum
  831. if ($post_data['topic_type'] != POST_GLOBAL)
  832. {
  833. $sql = 'SELECT topic_type, forum_id
  834. FROM ' . TOPICS_TABLE . "
  835. WHERE topic_id = $topic_id";
  836. $result = $db->sql_query($sql);
  837. $row = $db->sql_fetchrow($result);
  838. $db->sql_freeresult($result);
  839. if ($row && !$row['forum_id'] && $row['topic_type'] == POST_GLOBAL)
  840. {
  841. $to_forum_id = request_var('to_forum_id', 0);
  842. if ($to_forum_id)
  843. {
  844. $sql = 'SELECT forum_type
  845. FROM ' . FORUMS_TABLE . '
  846. WHERE forum_id = ' . $to_forum_id;
  847. $result = $db->sql_query($sql);
  848. $forum_type = (int) $db->sql_fetchfield('forum_type');
  849. $db->sql_freeresult($result);
  850. 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)))
  851. {
  852. $to_forum_id = 0;
  853. }
  854. }
  855. if (!$to_forum_id)
  856. {
  857. include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  858. $template->assign_vars(array(
  859. 'S_FORUM_SELECT' => make_forum_select(false, false, false, true, true, true),
  860. 'S_UNGLOBALISE' => true)
  861. );
  862. $submit = false;
  863. $refresh = true;
  864. }
  865. else
  866. {
  867. if (!$auth->acl_get('f_post', $to_forum_id))
  868. {
  869. // This will only be triggered if the user tried to trick the forum.
  870. trigger_error('NOT_AUTHORISED');
  871. }
  872. $forum_id = $to_forum_id;
  873. }
  874. }
  875. }
  876. if ($submit)
  877. {
  878. // Lock/Unlock Topic
  879. $change_topic_status = $post_data['topic_status'];
  880. $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;
  881. if ($post_data['topic_status'] == ITEM_LOCKED && !$topic_lock && $perm_lock_unlock)
  882. {
  883. $change_topic_status = ITEM_UNLOCKED;
  884. }
  885. else if ($post_data['topic_status'] == ITEM_UNLOCKED && $topic_lock && $perm_lock_unlock)
  886. {
  887. $change_topic_status = ITEM_LOCKED;
  888. }
  889. if ($change_topic_status != $post_data['topic_status'])
  890. {
  891. $sql = 'UPDATE ' . TOPICS_TABLE . "
  892. SET topic_status = $change_topic_status
  893. WHERE topic_id = $topic_id
  894. AND topic_moved_id = 0";
  895. $db->sql_query($sql);
  896. $user_lock = ($auth->acl_get('f_user_lock', $forum_id) && $user->data['is_registered'] && $user->data['user_id'] == $post_data['topic_poster']) ? 'USER_' : '';
  897. add_log('mod', $forum_id, $topic_id, 'LOG_' . $user_lock . (($change_topic_status == ITEM_LOCKED) ? 'LOCK' : 'UNLOCK'), $post_data['topic_title']);
  898. }
  899. // Lock/Unlock Post Edit
  900. if ($mode == 'edit' && $post_data['post_edit_locked'] == ITEM_LOCKED && !$post_lock && $auth->acl_get('m_edit', $forum_id))
  901. {
  902. $post_data['post_edit_locked'] = ITEM_UNLOCKED;
  903. }
  904. else if ($mode == 'edit' && $post_data['post_edit_locked'] == ITEM_UNLOCKED && $post_lock && $auth->acl_get('m_edit', $forum_id))
  905. {
  906. $post_data['post_edit_locked'] = ITEM_LOCKED;
  907. }
  908. $data = array(
  909. 'topic_title' => (empty($post_data['topic_title'])) ? $post_data['post_subject'] : $post_data['topic_title'],
  910. 'topic_first_post_id' => (isset($post_data['topic_first_post_id'])) ? (int) $post_data['topic_first_post_id'] : 0,
  911. 'topic_last_post_id' => (isset($post_data['topic_last_post_id'])) ? (int) $post_data['topic_last_post_id'] : 0,
  912. 'topic_time_limit' => (int) $post_data['topic_time_limit'],
  913. 'topic_attachment' => (isset($post_data['topic_attachment'])) ? (int) $post_data['topic_attachment'] : 0,
  914. 'post_id' => (int) $post_id,
  915. 'topic_id' => (int) $topic_id,
  916. 'forum_id' => (int) $forum_id,
  917. 'icon_id' => (int) $post_data['icon_id'],
  918. 'poster_id' => (int) $post_data['poster_id'],
  919. 'enable_sig' => (bool) $post_data['enable_sig'],
  920. 'enable_bbcode' => (bool) $post_data['enable_bbcode'],
  921. 'enable_smilies' => (bool) $post_data['enable_smilies'],
  922. 'enable_urls' => (bool) $post_data['enable_urls'],
  923. 'enable_indexing' => (bool) $post_data['enable_indexing'],
  924. 'message_md5' => (string) $message_md5,
  925. 'post_time' => (isset($post_data['post_time'])) ? (int) $post_data['post_time'] : $current_time,
  926. 'post_checksum' => (isset($post_data['post_checksum'])) ? (string) $post_data['post_checksum'] : '',
  927. 'post_edit_reason' => $post_data['post_edit_reason'],
  928. 'post_edit_user' => ($mode == 'edit') ? $user->data['user_id'] : ((isset($post_data['post_edit_user'])) ? (int) $post_data['post_edit_user'] : 0),
  929. 'forum_parents' => $post_data['forum_parents'],
  930. 'forum_name' => $post_data['forum_name'],
  931. 'notify' => $notify,
  932. 'notify_set' => $post_data['notify_set'],
  933. 'poster_ip' => (isset($post_data['poster_ip'])) ? $post_data['poster_ip'] : $user->ip,
  934. 'post_edit_locked' => (int) $post_data['post_edit_locked'],
  935. 'bbcode_bitfield' => $message_parser->bbcode_bitfield,
  936. 'bbcode_uid' => $message_parser->bbcode_uid,
  937. 'message' => $message_parser->message,
  938. 'attachment_data' => $message_parser->attachment_data,
  939. 'filename_data' => $message_parser->filename_data,
  940. 'topic_approved' => (isset($post_data['topic_approved'])) ? $post_data['topic_approved'] : false,
  941. 'post_approved' => (isset($post_data['post_approved'])) ? $post_data['post_approved'] : false,
  942. );
  943. if ($mode == 'edit')
  944. {
  945. $data['topic_replies_real'] = $post_data['topic_replies_real'];
  946. $data['topic_replies'] = $post_data['topic_replies'];
  947. }
  948. // The last parameter tells submit_post if search indexer has to be run
  949. $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);
  950. if ($config['enable_post_confirm'] && !$user->data['is_registered'] && (isset($captcha) && $captcha->is_solved() === true) && ($mode == 'post' || $mode == 'reply' || $mode == 'quote'))
  951. {
  952. $captcha->reset();
  953. }
  954. // Check the permissions for post approval. Moderators are not affected.
  955. 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']))
  956. {
  957. meta_refresh(10, $redirect_url);
  958. $message = ($mode == 'edit') ? $user->lang['POST_EDITED_MOD'] : $user->lang['POST_STORED_MOD'];
  959. $message .= (($user->data['user_id'] == ANONYMOUS) ? '' : ' '. $user->lang['POST_APPROVAL_NOTIFY']);
  960. }
  961. else
  962. {
  963. meta_refresh(3, $redirect_url);
  964. $message = ($mode == 'edit') ? 'POST_EDITED' : 'POST_STORED';
  965. $message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['VIEW_MESSAGE'], '<a href="' . $redirect_url . '">', '</a>');
  966. }
  967. $message .= '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $data['forum_id']) . '">', '</a>');
  968. trigger_error($message);
  969. }
  970. }
  971. }
  972. // Preview
  973. if (!sizeof($error) && $preview)
  974. {
  975. $post_data['post_time'] = ($mode == 'edit') ? $post_data['post_time'] : $current_time;
  976. $preview_message = $message_parser->format_display($post_data['enable_bbcode'], $post_data['enable_urls'], $post_data['enable_smilies'], false);
  977. $preview_signature = ($mode == 'edit') ? $post_data['user_sig'] : $user->data['user_sig'];
  978. $preview_signature_uid = ($mode == 'edit') ? $post_data['user_sig_bbcode_uid'] : $user->data['user_sig_bbcode_uid'];
  979. $preview_signature_bitfield = ($mode == 'edit') ? $post_data['user_sig_bbcode_bitfield'] : $user->data['user_sig_bbcode_bitfield'];
  980. // Signature
  981. if ($post_data['enable_sig'] && $config['allow_sig'] && $preview_signature && $auth->acl_get('f_sigs', $forum_id))
  982. {
  983. $parse_sig = new parse_message($preview_signature);
  984. $parse_sig->bbcode_uid = $preview_signature_uid;
  985. $parse_sig->bbcode_bitfield = $preview_signature_bitfield;
  986. // Not sure about parameters for bbcode/smilies/urls... in signatures
  987. $parse_sig->format_display($config['allow_sig_bbcode'], $config['allow_sig_links'], $config['allow_sig_smilies']);
  988. $preview_signature = $parse_sig->message;
  989. unset($parse_sig);
  990. }
  991. else
  992. {
  993. $preview_signature = '';
  994. }
  995. $preview_subject = censor_text($post_data['post_subject']);
  996. // Poll Preview
  997. 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))*/))
  998. && $auth->acl_get('f_poll', $forum_id))
  999. {
  1000. $parse_poll = new parse_message($post_data['poll_title']);
  1001. $parse_poll->bbcode_uid = $message_parser->bbcode_uid;
  1002. $parse_poll->bbcode_bitfield = $message_parser->bbcode_bitfield;
  1003. $parse_poll->format_display($post_data['enable_bbcode'], $post_data['enable_urls'], $post_data['enable_smilies']);
  1004. if ($post_data['poll_length'])
  1005. {
  1006. $poll_end = ($post_data['poll_length'] * 86400) + (($post_data['poll_start']) ? $post_data['poll_start'] : time());
  1007. }
  1008. $template->assign_vars(array(
  1009. 'S_HAS_POLL_OPTIONS' => (sizeof($post_data['poll_options'])),
  1010. 'S_IS_MULTI_CHOICE' => ($post_data['poll_max_options'] > 1) ? true : false,
  1011. 'POLL_QUESTION' => $parse_poll->message,
  1012. 'L_POLL_LENGTH' => ($post_data['poll_length']) ? sprintf($user->lang['POLL_RUN_TILL'], $user->format_date($poll_end)) : '',
  1013. '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']))
  1014. );
  1015. $parse_poll->message = implode("\n", $post_data['poll_options']);
  1016. $parse_poll->format_display($post_data['enable_bbcode'], $post_data['enable_urls'], $post_data['enable_smilies']);
  1017. $preview_poll_options = explode('<br />', $parse_poll->message);
  1018. unset($parse_poll);
  1019. foreach ($preview_poll_options as $key => $option)
  1020. {
  1021. $template->assign_block_vars('poll_option', array(
  1022. 'POLL_OPTION_CAPTION' => $option,
  1023. 'POLL_OPTION_ID' => $key + 1)
  1024. );
  1025. }
  1026. unset($preview_poll_options);
  1027. }
  1028. // Attachment Preview
  1029. if (sizeof($message_parser->attachment_data))
  1030. {
  1031. $template->assign_var('S_HAS_ATTACHMENTS', true);
  1032. $update_count = array();
  1033. $attachment_data = $message_parser->attachment_data;
  1034. parse_attachments($forum_id, $preview_message, $attachment_data, $update_count, true);
  1035. foreach ($attachment_data as $i => $attachment)
  1036. {
  1037. $template->assign_block_vars('attachment', array(
  1038. 'DISPLAY_ATTACHMENT' => $attachment)
  1039. );
  1040. }
  1041. unset($attachment_data);
  1042. }
  1043. if (!sizeof($error))
  1044. {
  1045. $template->assign_vars(array(
  1046. 'PREVIEW_SUBJECT' => $preview_subject,
  1047. 'PREVIEW_MESSAGE' => $preview_message,
  1048. 'PREVIEW_SIGNATURE' => $preview_signature,
  1049. 'S_DISPLAY_PREVIEW' => true)
  1050. );
  1051. }
  1052. }
  1053. // Decode text for message display
  1054. $post_data['bbcode_uid'] = ($mode == 'quote' && !$preview && !$refresh && !sizeof($error)) ? $post_data['bbcode_uid'] : $message_parser->bbcode_uid;
  1055. $message_parser->decode_message($post_data['bbcode_uid']);
  1056. if ($mode == 'quote' && !$submit && !$preview && !$refresh)
  1057. {
  1058. if ($config['allow_bbcode'])
  1059. {
  1060. $message_parser->message = '[quote=&quot;' . $post_data['quote_username'] . '&quot;]' . censor_text(trim($message_parser->message)) . "[/quote]\n";
  1061. }
  1062. else
  1063. {
  1064. $offset = 0;
  1065. $quote_string = "&gt; ";
  1066. $message = censor_text(trim($message_parser->message));
  1067. // see if we are nesting. It's easily tricked but should work for one level of nesting
  1068. if (strpos($message, "&gt;") !== false)
  1069. {
  1070. $offset = 10;
  1071. }
  1072. $message = utf8_wordwrap($message, 75 + $offset, "\n");
  1073. $message = $quote_string . $message;
  1074. $message = str_replace("\n", "\n" . $quote_string, $message);
  1075. $message_parser->message = $post_data['quote_username'] . " " . $user->lang['WROTE'] . ":\n" . $message . "\n";
  1076. }
  1077. }
  1078. if (($mode == 'reply' || $mode == 'quote') && !$submit && !$preview && !$refresh)
  1079. {
  1080. $post_data['post_subject'] = ((strpos($post_data['post_subject'], 'Re: ') !== 0) ? 'Re: ' : '') . censor_text($post_data['post_subject']);
  1081. }
  1082. $attachment_data = $message_parser->attachment_data;
  1083. $filename_data = $message_parser->filename_data;
  1084. $post_data['post_text'] = $message_parser->message;
  1085. if (sizeof($post_data['poll_options']) || !empty($post_data['poll_title']))
  1086. {
  1087. $message_parser->message = $post_data['poll_title'];
  1088. $message_parser->bbcode_uid = $post_data['bbcode_uid'];
  1089. $message_parser->decode_message();
  1090. $post_data['poll_title'] = $message_parser->message;
  1091. $message_parser->message = implode("\n", $post_data['poll_options']);
  1092. $message_parser->decode_message();
  1093. $post_data['poll_options'] = explode("\n", $message_parser->message);
  1094. }
  1095. // MAIN POSTING PAGE BEGINS HERE
  1096. // Forum moderators?
  1097. $moderators = array();
  1098. if ($config['load_moderators'])
  1099. {
  1100. get_moderators($moderators, $forum_id);
  1101. }
  1102. // Generate smiley listing
  1103. generate_smilies('inline', $forum_id);
  1104. // Generate inline attachment select box
  1105. posting_gen_inline_attachments($attachment_data);
  1106. // Do show topic type selection only in first post.
  1107. $topic_type_toggle = false;
  1108. if ($mode == 'post' || ($mode == 'edit' && $post_id == $post_data['topic_first_post_id']))
  1109. {
  1110. $topic_type_toggle = posting_gen_topic_types($forum_id, $post_data['topic_type']);
  1111. }
  1112. $s_topic_icons = false;
  1113. if ($post_data['enable_icons'] && $auth->acl_get('f_icons', $forum_id))
  1114. {
  1115. $s_topic_icons = posting_gen_topic_icons($mode, $post_data['icon_id']);
  1116. }
  1117. $bbcode_checked = (isset($post_data['enable_bbcode'])) ? !$post_data['enable_bbcode'] : (($config['allow_bbcode']) ? !$user->optionget('bbcode') : 1);
  1118. $smilies_checked = (isset($post_data['enable_smilies'])) ? !$post_data['enable_smilies'] : (($config['allow_smilies']) ? !$user->optionget('smilies') : 1);
  1119. $urls_checked = (isset($post_data['enable_urls'])) ? !$post_data['enable_urls'] : 0;
  1120. $sig_checked = $post_data['enable_sig'];
  1121. $lock_topic_checked = (isset($topic_lock) && $topic_lock) ? $topic_lock : (($post_data['topic_status'] == ITEM_LOCKED) ? 1 : 0);
  1122. $lock_post_checked = (isset($post_lock)) ? $post_lock : $post_data['post_edit_locked'];
  1123. // 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
  1124. $notify_set = ($mode != 'edit' && $config['allow_topic_notify'] && $user->data['is_registered'] && !$post_data['notify_set']) ? $user->data['user_notify'] : $post_data['notify_set'];
  1125. $notify_checked = (isset($notify)) ? $notify : (($mode == 'post') ? $user->data['user_notify'] : $notify_set);
  1126. // Page title & action URL
  1127. $s_action = append_sid("{$phpbb_root_path}posting.$phpEx", "mode=$mode&amp;f=$forum_id");
  1128. $s_action .= ($topic_id) ? "&amp;t=$topic_id" : '';
  1129. $s_action .= ($post_id) ? "&amp;p=$post_id" : '';
  1130. switch ($mode)
  1131. {
  1132. case 'post':
  1133. $page_title = $user->lang['POST_TOPIC'];
  1134. break;
  1135. case 'quote':
  1136. case 'reply':
  1137. $page_title = $user->lang['POST_REPLY'];
  1138. break;
  1139. case 'delete':
  1140. case 'edit':
  1141. $page_title = $user->lang['EDIT_POST'];
  1142. break;
  1143. }
  1144. // Build Navigation Links
  1145. generate_forum_nav($post_data);
  1146. // Build Forum Rules
  1147. generate_forum_rules($post_data);
  1148. // Posting uses is_solved for legacy reasons. Plugins have to use is_solved to force themselves to be displayed.
  1149. if ($config['enable_post_confirm'] && !$user->data['is_registered'] && (isset($captcha) && $captcha->is_solved() === false) && ($mode == 'post' || $mode == 'reply' || $mode == 'quote'))
  1150. {
  1151. $template->assign_vars(array(
  1152. 'S_CONFIRM_CODE' => true,
  1153. 'CAPTCHA_TEMPLATE' => $captcha->get_template(),
  1154. ));
  1155. }
  1156. $s_hidden_fields = ($mode == 'reply' || $mode == 'quote') ? '<input type="hidden" name="topic_cur_post_id" value="' . $post_data['topic_last_post_id'] . '" />' : '';
  1157. $s_hidden_fields .= '<input type="hidden" name="lastclick" value="' . $current_time . '" />';
  1158. $s_hidden_fields .= ($draft_id || isset($_REQUEST['draft_loaded'])) ? '<input type="hidden" name="draft_loaded" value="' . request_var('draft_loaded', $draft_id) . '" />' : '';
  1159. if ($mode == 'edit')
  1160. {
  1161. $s_hidden_fields .= build_hidden_fields(array(
  1162. 'edit_post_message_checksum' => $post_data['post_checksum'],
  1163. 'edit_post_subject_checksum' => $post_data['post_subject_md5'],
  1164. ));
  1165. }
  1166. // Add the confirm id/code pair to the hidden fields, else an error is displayed on next submit/preview
  1167. if (isset($captcha) && $captcha->is_solved() !== false)
  1168. {
  1169. $s_hidden_fields .= build_hidden_fields($captcha->get_hidden_fields());
  1170. }
  1171. $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"';
  1172. add_form_key('posting');
  1173. // Start assigning vars for main posting page ...
  1174. $template->assign_vars(array(
  1175. 'L_POST_A' => $page_title,
  1176. 'L_ICON' => ($mode == 'reply' || $mode == 'quote' || ($mode == 'edit' && $post_id != $post_data['topic_first_post_id'])) ? $user->lang['POST_ICON'] : $user->lang['TOPIC_ICON'],
  1177. 'L_MESSAGE_BODY_EXPLAIN' => (intval($config['max_post_chars'])) ? sprintf($user->lang['MESSAGE_BODY_EXPLAIN'], intval($config['max_post_chars'])) : '',
  1178. 'FORUM_NAME' => $post_data['forum_name'],
  1179. 'FORUM_DESC' => ($post_data['forum_desc']) ? generate_text_for_display($post_data['forum_desc'], $post_data['forum_desc_uid'], $post_data['forum_desc_bitfield'], $post_data['forum_desc_options']) : '',
  1180. 'TOPIC_TITLE' => censor_text($post_data['topic_title']),
  1181. 'MODERATORS' => (sizeof($moderators)) ? im