PageRenderTime 32ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/Sources/Post.php

https://github.com/smf-portal/SMF2.1
PHP | 2882 lines | 2139 code | 375 blank | 368 comment | 650 complexity | a3a1fb5bde19a03a3d3e59c7c45120ec MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * The job of this file is to handle everything related to posting replies,
  4. * new topics, quotes, and modifications to existing posts. It also handles
  5. * quoting posts by way of javascript.
  6. * Simple Machines Forum (SMF)
  7. *
  8. * @package SMF
  9. * @author Simple Machines http://www.simplemachines.org
  10. * @copyright 2012 Simple Machines
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.1 Alpha 1
  14. */
  15. if (!defined('SMF'))
  16. die('Hacking attempt...');
  17. /**
  18. * Handles showing the post screen, loading the post to be modified, and loading any post quoted.
  19. *
  20. * - additionally handles previews of posts.
  21. * - @uses the Post template and language file, main sub template.
  22. * - allows wireless access using the protocol_post sub template.
  23. * - requires different permissions depending on the actions, but most notably post_new, post_reply_own, and post_reply_any.
  24. * - shows options for the editing and posting of calendar events and attachments, as well as the posting of polls.
  25. * - accessed from ?action=post.
  26. *
  27. * @param array $post_errors holds any errors found tyring to post
  28. */
  29. function Post($post_errors = array())
  30. {
  31. global $txt, $scripturl, $topic, $modSettings, $board;
  32. global $user_info, $sc, $board_info, $context, $settings;
  33. global $sourcedir, $options, $smcFunc, $language;
  34. loadLanguage('Post');
  35. // You can't reply with a poll... hacker.
  36. if (isset($_REQUEST['poll']) && !empty($topic) && !isset($_REQUEST['msg']))
  37. unset($_REQUEST['poll']);
  38. // Posting an event?
  39. $context['make_event'] = isset($_REQUEST['calendar']);
  40. $context['robot_no_index'] = true;
  41. // You must be posting to *some* board.
  42. if (empty($board) && !$context['make_event'])
  43. fatal_lang_error('no_board', false);
  44. require_once($sourcedir . '/Subs-Post.php');
  45. if (isset($_REQUEST['xml']))
  46. {
  47. $context['sub_template'] = 'post';
  48. // Just in case of an earlier error...
  49. $context['preview_message'] = '';
  50. $context['preview_subject'] = '';
  51. }
  52. // No message is complete without a topic.
  53. if (empty($topic) && !empty($_REQUEST['msg']))
  54. {
  55. $request = $smcFunc['db_query']('', '
  56. SELECT id_topic
  57. FROM {db_prefix}messages
  58. WHERE id_msg = {int:msg}',
  59. array(
  60. 'msg' => (int) $_REQUEST['msg'],
  61. ));
  62. if ($smcFunc['db_num_rows']($request) != 1)
  63. unset($_REQUEST['msg'], $_POST['msg'], $_GET['msg']);
  64. else
  65. list ($topic) = $smcFunc['db_fetch_row']($request);
  66. $smcFunc['db_free_result']($request);
  67. }
  68. // Check if it's locked. It isn't locked if no topic is specified.
  69. if (!empty($topic))
  70. {
  71. $request = $smcFunc['db_query']('', '
  72. SELECT
  73. t.locked, IFNULL(ln.id_topic, 0) AS notify, t.is_sticky, t.id_poll, t.id_last_msg, mf.id_member,
  74. t.id_first_msg, mf.subject,
  75. CASE WHEN ml.poster_time > ml.modified_time THEN ml.poster_time ELSE ml.modified_time END AS last_post_time
  76. FROM {db_prefix}topics AS t
  77. LEFT JOIN {db_prefix}log_notify AS ln ON (ln.id_topic = t.id_topic AND ln.id_member = {int:current_member})
  78. LEFT JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
  79. LEFT JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  80. WHERE t.id_topic = {int:current_topic}
  81. LIMIT 1',
  82. array(
  83. 'current_member' => $user_info['id'],
  84. 'current_topic' => $topic,
  85. )
  86. );
  87. list ($locked, $context['notify'], $sticky, $pollID, $context['topic_last_message'], $id_member_poster, $id_first_msg, $first_subject, $lastPostTime) = $smcFunc['db_fetch_row']($request);
  88. $smcFunc['db_free_result']($request);
  89. // If this topic already has a poll, they sure can't add another.
  90. if (isset($_REQUEST['poll']) && $pollID > 0)
  91. unset($_REQUEST['poll']);
  92. if (empty($_REQUEST['msg']))
  93. {
  94. if ($user_info['is_guest'] && !allowedTo('post_reply_any') && (!$modSettings['postmod_active'] || !allowedTo('post_unapproved_replies_any')))
  95. is_not_guest();
  96. // By default the reply will be approved...
  97. $context['becomes_approved'] = true;
  98. if ($id_member_poster != $user_info['id'])
  99. {
  100. if ($modSettings['postmod_active'] && allowedTo('post_unapproved_replies_any') && !allowedTo('post_reply_any'))
  101. $context['becomes_approved'] = false;
  102. else
  103. isAllowedTo('post_reply_any');
  104. }
  105. elseif (!allowedTo('post_reply_any'))
  106. {
  107. if ($modSettings['postmod_active'] && allowedTo('post_unapproved_replies_own') && !allowedTo('post_reply_own'))
  108. $context['becomes_approved'] = false;
  109. else
  110. isAllowedTo('post_reply_own');
  111. }
  112. }
  113. else
  114. $context['becomes_approved'] = true;
  115. $context['can_lock'] = allowedTo('lock_any') || ($user_info['id'] == $id_member_poster && allowedTo('lock_own'));
  116. $context['can_sticky'] = allowedTo('make_sticky') && !empty($modSettings['enableStickyTopics']);
  117. $context['notify'] = !empty($context['notify']);
  118. $context['sticky'] = isset($_REQUEST['sticky']) ? !empty($_REQUEST['sticky']) : $sticky;
  119. // Check whether this is a really old post being bumped...
  120. if (!empty($modSettings['oldTopicDays']) && $lastPostTime + $modSettings['oldTopicDays'] * 86400 < time() && empty($sticky) && !isset($_REQUEST['subject']))
  121. $post_errors[] = array('old_topic', array($modSettings['oldTopicDays']));
  122. }
  123. else
  124. {
  125. $context['becomes_approved'] = true;
  126. if ((!$context['make_event'] || !empty($board)))
  127. {
  128. if ($modSettings['postmod_active'] && !allowedTo('post_new') && allowedTo('post_unapproved_topics'))
  129. $context['becomes_approved'] = false;
  130. else
  131. isAllowedTo('post_new');
  132. }
  133. $locked = 0;
  134. // @todo These won't work if you're making an event.
  135. $context['can_lock'] = allowedTo(array('lock_any', 'lock_own'));
  136. $context['can_sticky'] = allowedTo('make_sticky') && !empty($modSettings['enableStickyTopics']);
  137. $context['notify'] = !empty($context['notify']);
  138. $context['sticky'] = !empty($_REQUEST['sticky']);
  139. }
  140. // @todo These won't work if you're posting an event!
  141. $context['can_notify'] = allowedTo('mark_any_notify');
  142. $context['can_move'] = allowedTo('move_any');
  143. $context['move'] = !empty($_REQUEST['move']);
  144. $context['announce'] = !empty($_REQUEST['announce']);
  145. // You can only announce topics that will get approved...
  146. $context['can_announce'] = allowedTo('announce_topic') && $context['becomes_approved'];
  147. $context['locked'] = !empty($locked) || !empty($_REQUEST['lock']);
  148. $context['can_quote'] = empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC']));
  149. // Generally don't show the approval box... (Assume we want things approved)
  150. $context['show_approval'] = allowedTo('approve_posts') && $context['becomes_approved'] ? 2 : (allowedTo('approve_posts') ? 1 : 0);
  151. // An array to hold all the attachments for this topic.
  152. $context['current_attachments'] = array();
  153. // Don't allow a post if it's locked and you aren't all powerful.
  154. if ($locked && !allowedTo('moderate_board'))
  155. fatal_lang_error('topic_locked', false);
  156. // Check the users permissions - is the user allowed to add or post a poll?
  157. if (isset($_REQUEST['poll']) && $modSettings['pollMode'] == '1')
  158. {
  159. // New topic, new poll.
  160. if (empty($topic))
  161. isAllowedTo('poll_post');
  162. // This is an old topic - but it is yours! Can you add to it?
  163. elseif ($user_info['id'] == $id_member_poster && !allowedTo('poll_add_any'))
  164. isAllowedTo('poll_add_own');
  165. // If you're not the owner, can you add to any poll?
  166. else
  167. isAllowedTo('poll_add_any');
  168. require_once($sourcedir . '/Subs-Members.php');
  169. $allowedVoteGroups = groupsAllowedTo('poll_vote', $board);
  170. // Set up the poll options.
  171. $context['poll_options'] = array(
  172. 'max_votes' => empty($_POST['poll_max_votes']) ? '1' : max(1, $_POST['poll_max_votes']),
  173. 'hide' => empty($_POST['poll_hide']) ? 0 : $_POST['poll_hide'],
  174. 'expire' => !isset($_POST['poll_expire']) ? '' : $_POST['poll_expire'],
  175. 'change_vote' => isset($_POST['poll_change_vote']),
  176. 'guest_vote' => isset($_POST['poll_guest_vote']),
  177. 'guest_vote_enabled' => in_array(-1, $allowedVoteGroups['allowed']),
  178. );
  179. // Make all five poll choices empty.
  180. $context['choices'] = array(
  181. array('id' => 0, 'number' => 1, 'label' => '', 'is_last' => false),
  182. array('id' => 1, 'number' => 2, 'label' => '', 'is_last' => false),
  183. array('id' => 2, 'number' => 3, 'label' => '', 'is_last' => false),
  184. array('id' => 3, 'number' => 4, 'label' => '', 'is_last' => false),
  185. array('id' => 4, 'number' => 5, 'label' => '', 'is_last' => true)
  186. );
  187. $context['last_choice_id'] = 4;
  188. }
  189. if ($context['make_event'])
  190. {
  191. // They might want to pick a board.
  192. if (!isset($context['current_board']))
  193. $context['current_board'] = 0;
  194. // Start loading up the event info.
  195. $context['event'] = array();
  196. $context['event']['title'] = isset($_REQUEST['evtitle']) ? htmlspecialchars(stripslashes($_REQUEST['evtitle'])) : '';
  197. $context['event']['id'] = isset($_REQUEST['eventid']) ? (int) $_REQUEST['eventid'] : -1;
  198. $context['event']['new'] = $context['event']['id'] == -1;
  199. // Permissions check!
  200. isAllowedTo('calendar_post');
  201. // Editing an event? (but NOT previewing!?)
  202. if (empty($context['event']['new']) && !isset($_REQUEST['subject']))
  203. {
  204. // If the user doesn't have permission to edit the post in this topic, redirect them.
  205. if ((empty($id_member_poster) || $id_member_poster != $user_info['id'] || !allowedTo('modify_own')) && !allowedTo('modify_any'))
  206. {
  207. require_once($sourcedir . '/Calendar.php');
  208. return CalendarPost();
  209. }
  210. // Get the current event information.
  211. $request = $smcFunc['db_query']('', '
  212. SELECT
  213. id_member, title, MONTH(start_date) AS month, DAYOFMONTH(start_date) AS day,
  214. YEAR(start_date) AS year, (TO_DAYS(end_date) - TO_DAYS(start_date)) AS span
  215. FROM {db_prefix}calendar
  216. WHERE id_event = {int:id_event}
  217. LIMIT 1',
  218. array(
  219. 'id_event' => $context['event']['id'],
  220. )
  221. );
  222. $row = $smcFunc['db_fetch_assoc']($request);
  223. $smcFunc['db_free_result']($request);
  224. // Make sure the user is allowed to edit this event.
  225. if ($row['id_member'] != $user_info['id'])
  226. isAllowedTo('calendar_edit_any');
  227. elseif (!allowedTo('calendar_edit_any'))
  228. isAllowedTo('calendar_edit_own');
  229. $context['event']['month'] = $row['month'];
  230. $context['event']['day'] = $row['day'];
  231. $context['event']['year'] = $row['year'];
  232. $context['event']['title'] = $row['title'];
  233. $context['event']['span'] = $row['span'] + 1;
  234. }
  235. else
  236. {
  237. $today = getdate();
  238. // You must have a month and year specified!
  239. if (!isset($_REQUEST['month']))
  240. $_REQUEST['month'] = $today['mon'];
  241. if (!isset($_REQUEST['year']))
  242. $_REQUEST['year'] = $today['year'];
  243. $context['event']['month'] = (int) $_REQUEST['month'];
  244. $context['event']['year'] = (int) $_REQUEST['year'];
  245. $context['event']['day'] = isset($_REQUEST['day']) ? $_REQUEST['day'] : ($_REQUEST['month'] == $today['mon'] ? $today['mday'] : 0);
  246. $context['event']['span'] = isset($_REQUEST['span']) ? $_REQUEST['span'] : 1;
  247. // Make sure the year and month are in the valid range.
  248. if ($context['event']['month'] < 1 || $context['event']['month'] > 12)
  249. fatal_lang_error('invalid_month', false);
  250. if ($context['event']['year'] < $modSettings['cal_minyear'] || $context['event']['year'] > $modSettings['cal_maxyear'])
  251. fatal_lang_error('invalid_year', false);
  252. // Get a list of boards they can post in.
  253. $boards = boardsAllowedTo('post_new');
  254. if (empty($boards))
  255. fatal_lang_error('cannot_post_new', 'user');
  256. // Load a list of boards for this event in the context.
  257. require_once($sourcedir . '/Subs-MessageIndex.php');
  258. $boardListOptions = array(
  259. 'included_boards' => in_array(0, $boards) ? null : $boards,
  260. 'not_redirection' => true,
  261. 'use_permissions' => true,
  262. 'selected_board' => empty($context['current_board']) ? $modSettings['cal_defaultboard'] : $context['current_board'],
  263. );
  264. $context['event']['categories'] = getBoardList($boardListOptions);
  265. }
  266. // Find the last day of the month.
  267. $context['event']['last_day'] = (int) strftime('%d', mktime(0, 0, 0, $context['event']['month'] == 12 ? 1 : $context['event']['month'] + 1, 0, $context['event']['month'] == 12 ? $context['event']['year'] + 1 : $context['event']['year']));
  268. $context['event']['board'] = !empty($board) ? $board : $modSettings['cal_defaultboard'];
  269. }
  270. // See if any new replies have come along.
  271. // Huh, $_REQUEST['msg'] is set upon submit, so this doesn't get executed at submit
  272. // only at preview
  273. if (empty($_REQUEST['msg']) && !empty($topic))
  274. {
  275. if (empty($options['no_new_reply_warning']) && isset($_REQUEST['last_msg']) && $context['topic_last_message'] > $_REQUEST['last_msg'])
  276. {
  277. $request = $smcFunc['db_query']('', '
  278. SELECT COUNT(*)
  279. FROM {db_prefix}messages
  280. WHERE id_topic = {int:current_topic}
  281. AND id_msg > {int:last_msg}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  282. AND approved = {int:approved}') . '
  283. LIMIT 1',
  284. array(
  285. 'current_topic' => $topic,
  286. 'last_msg' => (int) $_REQUEST['last_msg'],
  287. 'approved' => 1,
  288. )
  289. );
  290. list ($context['new_replies']) = $smcFunc['db_fetch_row']($request);
  291. $smcFunc['db_free_result']($request);
  292. if (!empty($context['new_replies']))
  293. {
  294. if ($context['new_replies'] == 1)
  295. $txt['error_new_replies'] = isset($_GET['last_msg']) ? $txt['error_new_reply_reading'] : $txt['error_new_reply'];
  296. else
  297. $txt['error_new_replies'] = sprintf(isset($_GET['last_msg']) ? $txt['error_new_replies_reading'] : $txt['error_new_replies'], $context['new_replies']);
  298. $post_errors[] = 'new_replies';
  299. $modSettings['topicSummaryPosts'] = $context['new_replies'] > $modSettings['topicSummaryPosts'] ? max($modSettings['topicSummaryPosts'], 5) : $modSettings['topicSummaryPosts'];
  300. }
  301. }
  302. }
  303. // Get a response prefix (like 'Re:') in the default forum language.
  304. if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
  305. {
  306. if ($language === $user_info['language'])
  307. $context['response_prefix'] = $txt['response_prefix'];
  308. else
  309. {
  310. loadLanguage('index', $language, false);
  311. $context['response_prefix'] = $txt['response_prefix'];
  312. loadLanguage('index');
  313. }
  314. cache_put_data('response_prefix', $context['response_prefix'], 600);
  315. }
  316. // Previewing, modifying, or posting?
  317. // Do we have a body, but an error happened.
  318. if (isset($_REQUEST['message']) || !empty($context['post_error']))
  319. {
  320. // Validate inputs.
  321. if (empty($context['post_error']))
  322. {
  323. // This means they didn't click Post and get an error.
  324. $really_previewing = true;
  325. }
  326. else
  327. {
  328. if (!isset($_REQUEST['subject']))
  329. $_REQUEST['subject'] = '';
  330. if (!isset($_REQUEST['message']))
  331. $_REQUEST['message'] = '';
  332. if (!isset($_REQUEST['icon']))
  333. $_REQUEST['icon'] = 'xx';
  334. // They are previewing if they asked to preview (i.e. came from quick reply).
  335. $really_previewing = !empty($_POST['preview']);
  336. }
  337. // In order to keep the approval status flowing through, we have to pass it through the form...
  338. $context['becomes_approved'] = empty($_REQUEST['not_approved']);
  339. $context['show_approval'] = isset($_REQUEST['approve']) ? ($_REQUEST['approve'] ? 2 : 1) : 0;
  340. $context['can_announce'] &= $context['becomes_approved'];
  341. // Set up the inputs for the form.
  342. $form_subject = strtr($smcFunc['htmlspecialchars']($_REQUEST['subject']), array("\r" => '', "\n" => '', "\t" => ''));
  343. $form_message = $smcFunc['htmlspecialchars']($_REQUEST['message'], ENT_QUOTES);
  344. // Make sure the subject isn't too long - taking into account special characters.
  345. if ($smcFunc['strlen']($form_subject) > 100)
  346. $form_subject = $smcFunc['substr']($form_subject, 0, 100);
  347. if (isset($_REQUEST['poll']))
  348. {
  349. $context['question'] = isset($_REQUEST['question']) ? $smcFunc['htmlspecialchars'](trim($_REQUEST['question'])) : '';
  350. $context['choices'] = array();
  351. $choice_id = 0;
  352. $_POST['options'] = empty($_POST['options']) ? array() : htmlspecialchars__recursive($_POST['options']);
  353. foreach ($_POST['options'] as $option)
  354. {
  355. if (trim($option) == '')
  356. continue;
  357. $context['choices'][] = array(
  358. 'id' => $choice_id++,
  359. 'number' => $choice_id,
  360. 'label' => $option,
  361. 'is_last' => false
  362. );
  363. }
  364. // One empty option for those with js disabled...I know are few... :P
  365. $context['choices'][] = array(
  366. 'id' => $choice_id++,
  367. 'number' => $choice_id,
  368. 'label' => '',
  369. 'is_last' => false
  370. );
  371. if (count($context['choices']) < 2)
  372. {
  373. $context['choices'][] = array(
  374. 'id' => $choice_id++,
  375. 'number' => $choice_id,
  376. 'label' => '',
  377. 'is_last' => false
  378. );
  379. }
  380. $context['last_choice_id'] = $choice_id;
  381. $context['choices'][count($context['choices']) - 1]['is_last'] = true;
  382. }
  383. // Are you... a guest?
  384. if ($user_info['is_guest'])
  385. {
  386. $_REQUEST['guestname'] = !isset($_REQUEST['guestname']) ? '' : trim($_REQUEST['guestname']);
  387. $_REQUEST['email'] = !isset($_REQUEST['email']) ? '' : trim($_REQUEST['email']);
  388. $_REQUEST['guestname'] = htmlspecialchars($_REQUEST['guestname']);
  389. $context['name'] = $_REQUEST['guestname'];
  390. $_REQUEST['email'] = htmlspecialchars($_REQUEST['email']);
  391. $context['email'] = $_REQUEST['email'];
  392. $user_info['name'] = $_REQUEST['guestname'];
  393. }
  394. // Only show the preview stuff if they hit Preview.
  395. if (($really_previewing == true || isset($_REQUEST['xml'])) && !isset($_POST['id_draft']))
  396. {
  397. // Set up the preview message and subject and censor them...
  398. $context['preview_message'] = $form_message;
  399. preparsecode($form_message, true);
  400. preparsecode($context['preview_message']);
  401. // Do all bulletin board code tags, with or without smileys.
  402. $context['preview_message'] = parse_bbc($context['preview_message'], isset($_REQUEST['ns']) ? 0 : 1);
  403. censorText($context['preview_message']);
  404. if ($form_subject != '')
  405. {
  406. $context['preview_subject'] = $form_subject;
  407. censorText($context['preview_subject']);
  408. }
  409. else
  410. $context['preview_subject'] = '<em>' . $txt['no_subject'] . '</em>';
  411. // Protect any CDATA blocks.
  412. if (isset($_REQUEST['xml']))
  413. $context['preview_message'] = strtr($context['preview_message'], array(']]>' => ']]]]><![CDATA[>'));
  414. }
  415. // Set up the checkboxes.
  416. $context['notify'] = !empty($_REQUEST['notify']);
  417. $context['use_smileys'] = !isset($_REQUEST['ns']);
  418. $context['icon'] = isset($_REQUEST['icon']) ? preg_replace('~[\./\\\\*\':"<>]~', '', $_REQUEST['icon']) : 'xx';
  419. // Set the destination action for submission.
  420. $context['destination'] = 'post2;start=' . $_REQUEST['start'] . (isset($_REQUEST['msg']) ? ';msg=' . $_REQUEST['msg'] . ';' . $context['session_var'] . '=' . $context['session_id'] : '') . (isset($_REQUEST['poll']) ? ';poll' : '');
  421. $context['submit_label'] = isset($_REQUEST['msg']) ? $txt['save'] : $txt['post'];
  422. // Previewing an edit?
  423. if (isset($_REQUEST['msg']) && !empty($topic))
  424. {
  425. // Get the existing message.
  426. $request = $smcFunc['db_query']('', '
  427. SELECT
  428. m.id_member, m.modified_time, m.smileys_enabled, m.body,
  429. m.poster_name, m.poster_email, m.subject, m.icon, m.approved,
  430. IFNULL(a.size, -1) AS filesize, a.filename, a.id_attach,
  431. a.approved AS attachment_approved, t.id_member_started AS id_member_poster,
  432. m.poster_time, log.id_action
  433. FROM {db_prefix}messages AS m
  434. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
  435. LEFT JOIN {db_prefix}attachments AS a ON (a.id_msg = m.id_msg AND a.attachment_type = {int:attachment_type})
  436. LEFT JOIN {db_prefix}log_actions AS log ON (m.id_topic = log.id_topic AND log.action = {string:announce_action})
  437. WHERE m.id_msg = {int:id_msg}
  438. AND m.id_topic = {int:current_topic}',
  439. array(
  440. 'current_topic' => $topic,
  441. 'attachment_type' => 0,
  442. 'id_msg' => $_REQUEST['msg'],
  443. 'announce_action' => 'announce_topic',
  444. )
  445. );
  446. // The message they were trying to edit was most likely deleted.
  447. // @todo Change this error message?
  448. if ($smcFunc['db_num_rows']($request) == 0)
  449. fatal_lang_error('no_board', false);
  450. $row = $smcFunc['db_fetch_assoc']($request);
  451. $attachment_stuff = array($row);
  452. while ($row2 = $smcFunc['db_fetch_assoc']($request))
  453. $attachment_stuff[] = $row2;
  454. $smcFunc['db_free_result']($request);
  455. if ($row['id_member'] == $user_info['id'] && !allowedTo('modify_any'))
  456. {
  457. // Give an extra five minutes over the disable time threshold, so they can type - assuming the post is public.
  458. if ($row['approved'] && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + ($modSettings['edit_disable_time'] + 5) * 60 < time())
  459. fatal_lang_error('modify_post_time_passed', false);
  460. elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('modify_own'))
  461. isAllowedTo('modify_replies');
  462. else
  463. isAllowedTo('modify_own');
  464. }
  465. elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('modify_any'))
  466. isAllowedTo('modify_replies');
  467. else
  468. isAllowedTo('modify_any');
  469. if (!empty($modSettings['attachmentEnable']))
  470. {
  471. $request = $smcFunc['db_query']('', '
  472. SELECT IFNULL(size, -1) AS filesize, filename, id_attach, approved
  473. FROM {db_prefix}attachments
  474. WHERE id_msg = {int:id_msg}
  475. AND attachment_type = {int:attachment_type}
  476. ORDER BY id_attach',
  477. array(
  478. 'id_msg' => (int) $_REQUEST['msg'],
  479. 'attachment_type' => 0,
  480. )
  481. );
  482. while ($row = $smcFunc['db_fetch_assoc']($request))
  483. {
  484. if ($row['filesize'] <= 0)
  485. continue;
  486. $context['current_attachments'][] = array(
  487. 'name' => htmlspecialchars($row['filename']),
  488. 'size' => $row['filesize'],
  489. 'id' => $row['id_attach'],
  490. 'approved' => $row['approved'],
  491. );
  492. }
  493. $smcFunc['db_free_result']($request);
  494. }
  495. if ($context['can_announce'] && !empty($row['id_action']))
  496. {
  497. loadLanguage('Errors');
  498. $context['post_error']['messages'][] = $txt['error_topic_already_announced'];
  499. }
  500. // Allow moderators to change names....
  501. if (allowedTo('moderate_forum') && !empty($topic))
  502. {
  503. $request = $smcFunc['db_query']('', '
  504. SELECT id_member, poster_name, poster_email
  505. FROM {db_prefix}messages
  506. WHERE id_msg = {int:id_msg}
  507. AND id_topic = {int:current_topic}
  508. LIMIT 1',
  509. array(
  510. 'current_topic' => $topic,
  511. 'id_msg' => (int) $_REQUEST['msg'],
  512. )
  513. );
  514. $row = $smcFunc['db_fetch_assoc']($request);
  515. $smcFunc['db_free_result']($request);
  516. if (empty($row['id_member']))
  517. {
  518. $context['name'] = htmlspecialchars($row['poster_name']);
  519. $context['email'] = htmlspecialchars($row['poster_email']);
  520. }
  521. }
  522. }
  523. // No check is needed, since nothing is really posted.
  524. checkSubmitOnce('free');
  525. }
  526. // Editing a message...
  527. elseif (isset($_REQUEST['msg']) && !empty($topic))
  528. {
  529. $_REQUEST['msg'] = (int) $_REQUEST['msg'];
  530. // Get the existing message.
  531. $request = $smcFunc['db_query']('', '
  532. SELECT
  533. m.id_member, m.modified_time, m.modified_name, m.smileys_enabled, m.body,
  534. m.poster_name, m.poster_email, m.subject, m.icon, m.approved,
  535. IFNULL(a.size, -1) AS filesize, a.filename, a.id_attach,
  536. a.approved AS attachment_approved, t.id_member_started AS id_member_poster,
  537. m.poster_time, log.id_action
  538. FROM {db_prefix}messages AS m
  539. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
  540. LEFT JOIN {db_prefix}attachments AS a ON (a.id_msg = m.id_msg AND a.attachment_type = {int:attachment_type})
  541. LEFT JOIN {db_prefix}log_actions AS log ON (m.id_topic = log.id_topic AND log.action = {string:announce_action})
  542. WHERE m.id_msg = {int:id_msg}
  543. AND m.id_topic = {int:current_topic}',
  544. array(
  545. 'current_topic' => $topic,
  546. 'attachment_type' => 0,
  547. 'id_msg' => $_REQUEST['msg'],
  548. 'announce_action' => 'announce_topic',
  549. )
  550. );
  551. // The message they were trying to edit was most likely deleted.
  552. if ($smcFunc['db_num_rows']($request) == 0)
  553. fatal_lang_error('no_message', false);
  554. $row = $smcFunc['db_fetch_assoc']($request);
  555. $attachment_stuff = array($row);
  556. while ($row2 = $smcFunc['db_fetch_assoc']($request))
  557. $attachment_stuff[] = $row2;
  558. $smcFunc['db_free_result']($request);
  559. if ($row['id_member'] == $user_info['id'] && !allowedTo('modify_any'))
  560. {
  561. // Give an extra five minutes over the disable time threshold, so they can type - assuming the post is public.
  562. if ($row['approved'] && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + ($modSettings['edit_disable_time'] + 5) * 60 < time())
  563. fatal_lang_error('modify_post_time_passed', false);
  564. elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('modify_own'))
  565. isAllowedTo('modify_replies');
  566. else
  567. isAllowedTo('modify_own');
  568. }
  569. elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('modify_any'))
  570. isAllowedTo('modify_replies');
  571. else
  572. isAllowedTo('modify_any');
  573. if ($context['can_announce'] && !empty($row['id_action']))
  574. {
  575. loadLanguage('Errors');
  576. $context['post_error']['messages'][] = $txt['error_topic_already_announced'];
  577. }
  578. // When was it last modified?
  579. if (!empty($row['modified_time']))
  580. {
  581. $context['last_modified'] = timeformat($row['modified_time']);
  582. $context['last_modified_text'] = sprintf($txt['last_edit_by'], $context['last_modified'], $row['modified_name']);
  583. }
  584. // Get the stuff ready for the form.
  585. $form_subject = $row['subject'];
  586. $form_message = un_preparsecode($row['body']);
  587. censorText($form_message);
  588. censorText($form_subject);
  589. // Check the boxes that should be checked.
  590. $context['use_smileys'] = !empty($row['smileys_enabled']);
  591. $context['icon'] = $row['icon'];
  592. // Show an "approve" box if the user can approve it, and the message isn't approved.
  593. if (!$row['approved'] && !$context['show_approval'])
  594. $context['show_approval'] = allowedTo('approve_posts');
  595. // Sort the attachments so they are in the order saved
  596. $temp = array();
  597. foreach ($attachment_stuff as $attachment)
  598. {
  599. if ($attachment['filesize'] >= 0 && !empty($modSettings['attachmentEnable']))
  600. $temp[$attachment['id_attach']] = $attachment;
  601. }
  602. ksort($temp);
  603. // Load up 'em attachments!
  604. foreach ($temp as $attachment)
  605. {
  606. $context['current_attachments'][] = array(
  607. 'name' => htmlspecialchars($attachment['filename']),
  608. 'size' => $attachment['filesize'],
  609. 'id' => $attachment['id_attach'],
  610. 'approved' => $attachment['attachment_approved'],
  611. );
  612. }
  613. // Allow moderators to change names....
  614. if (allowedTo('moderate_forum') && empty($row['id_member']))
  615. {
  616. $context['name'] = htmlspecialchars($row['poster_name']);
  617. $context['email'] = htmlspecialchars($row['poster_email']);
  618. }
  619. // Set the destinaton.
  620. $context['destination'] = 'post2;start=' . $_REQUEST['start'] . ';msg=' . $_REQUEST['msg'] . ';' . $context['session_var'] . '=' . $context['session_id'] . (isset($_REQUEST['poll']) ? ';poll' : '');
  621. $context['submit_label'] = $txt['save'];
  622. }
  623. // Posting...
  624. else
  625. {
  626. // By default....
  627. $context['use_smileys'] = true;
  628. $context['icon'] = 'xx';
  629. if ($user_info['is_guest'])
  630. {
  631. $context['name'] = isset($_SESSION['guest_name']) ? $_SESSION['guest_name'] : '';
  632. $context['email'] = isset($_SESSION['guest_email']) ? $_SESSION['guest_email'] : '';
  633. }
  634. $context['destination'] = 'post2;start=' . $_REQUEST['start'] . (isset($_REQUEST['poll']) ? ';poll' : '');
  635. $context['submit_label'] = $txt['post'];
  636. // Posting a quoted reply?
  637. if (!empty($topic) && !empty($_REQUEST['quote']))
  638. {
  639. // Make sure they _can_ quote this post, and if so get it.
  640. $request = $smcFunc['db_query']('', '
  641. SELECT m.subject, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.body
  642. FROM {db_prefix}messages AS m
  643. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND {query_see_board})
  644. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  645. WHERE m.id_msg = {int:id_msg}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  646. AND m.approved = {int:is_approved}') . '
  647. LIMIT 1',
  648. array(
  649. 'id_msg' => (int) $_REQUEST['quote'],
  650. 'is_approved' => 1,
  651. )
  652. );
  653. if ($smcFunc['db_num_rows']($request) == 0)
  654. fatal_lang_error('quoted_post_deleted', false);
  655. list ($form_subject, $mname, $mdate, $form_message) = $smcFunc['db_fetch_row']($request);
  656. $smcFunc['db_free_result']($request);
  657. // Add 'Re: ' to the front of the quoted subject.
  658. if (trim($context['response_prefix']) != '' && $smcFunc['strpos']($form_subject, trim($context['response_prefix'])) !== 0)
  659. $form_subject = $context['response_prefix'] . $form_subject;
  660. // Censor the message and subject.
  661. censorText($form_message);
  662. censorText($form_subject);
  663. // But if it's in HTML world, turn them into htmlspecialchar's so they can be edited!
  664. if (strpos($form_message, '[html]') !== false)
  665. {
  666. $parts = preg_split('~(\[/code\]|\[code(?:=[^\]]+)?\])~i', $form_message, -1, PREG_SPLIT_DELIM_CAPTURE);
  667. for ($i = 0, $n = count($parts); $i < $n; $i++)
  668. {
  669. // It goes 0 = outside, 1 = begin tag, 2 = inside, 3 = close tag, repeat.
  670. if ($i % 4 == 0)
  671. $parts[$i] = preg_replace('~\[html\](.+?)\[/html\]~ise', '\'[html]\' . preg_replace(\'~<br\s?/?' . '>~i\', \'&lt;br /&gt;<br />\', \'$1\') . \'[/html]\'', $parts[$i]);
  672. }
  673. $form_message = implode('', $parts);
  674. }
  675. $form_message = preg_replace('~<br ?/?' . '>~i', "\n", $form_message);
  676. // Remove any nested quotes, if necessary.
  677. if (!empty($modSettings['removeNestedQuotes']))
  678. $form_message = preg_replace(array('~\n?\[quote.*?\].+?\[/quote\]\n?~is', '~^\n~', '~\[/quote\]~'), '', $form_message);
  679. // Add a quote string on the front and end.
  680. $form_message = '[quote author=' . $mname . ' link=topic=' . $topic . '.msg' . (int) $_REQUEST['quote'] . '#msg' . (int) $_REQUEST['quote'] . ' date=' . $mdate . ']' . "\n" . rtrim($form_message) . "\n" . '[/quote]';
  681. }
  682. // Posting a reply without a quote?
  683. elseif (!empty($topic) && empty($_REQUEST['quote']))
  684. {
  685. // Get the first message's subject.
  686. $form_subject = $first_subject;
  687. // Add 'Re: ' to the front of the subject.
  688. if (trim($context['response_prefix']) != '' && $form_subject != '' && $smcFunc['strpos']($form_subject, trim($context['response_prefix'])) !== 0)
  689. $form_subject = $context['response_prefix'] . $form_subject;
  690. // Censor the subject.
  691. censorText($form_subject);
  692. $form_message = '';
  693. }
  694. else
  695. {
  696. $form_subject = isset($_GET['subject']) ? $_GET['subject'] : '';
  697. $form_message = '';
  698. }
  699. }
  700. $context['can_post_attachment'] = !empty($modSettings['attachmentEnable']) && $modSettings['attachmentEnable'] == 1 && (allowedTo('post_attachment') || ($modSettings['postmod_active'] && allowedTo('post_unapproved_attachments')));
  701. if ($context['can_post_attachment'])
  702. {
  703. // If there are attachments, calculate the total size and how many.
  704. $context['attachments']['total_size'] = 0;
  705. $context['attachments']['quantity'] = 0;
  706. // If this isn't a new post, check the current attachments.
  707. if (isset($_REQUEST['msg']))
  708. {
  709. $context['attachments']['quantity'] = count($context['current_attachments']);
  710. foreach ($context['current_attachments'] as $attachment)
  711. $context['attachments']['total_size'] += $attachment['size'];
  712. }
  713. // A bit of house keeping first.
  714. if (!empty($_SESSION['temp_attachments']) && count($_SESSION['temp_attachments']) == 1)
  715. unset($_SESSION['temp_attachments']);
  716. if (!empty($_SESSION['temp_attachments']))
  717. {
  718. // Is this a request to delete them?
  719. if (isset($_GET['delete_temp']))
  720. {
  721. foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
  722. {
  723. if (strpos($attachID, 'post_tmp_' . $user_info['id']) !== false)
  724. if (file_exists($attachment['tmp_name']))
  725. unlink($attachment['tmp_name']);
  726. }
  727. $post_errors[] = 'temp_attachments_gone';
  728. $_SESSION['temp_attachments'] = array();
  729. }
  730. // Hmm, coming in fresh and there are files in session.
  731. elseif ($context['current_action'] != 'post2' || !empty($_POST['from_qr']))
  732. {
  733. // Let's be nice and see if they belong here first.
  734. if ((empty($_REQUEST['msg']) && empty($_SESSION['temp_attachments']['post']['msg']) && $_SESSION['temp_attachments']['post']['board'] == $board) || (!empty($_REQUEST['msg']) && $_SESSION['temp_attachments']['post']['msg'] == $_REQUEST['msg']))
  735. {
  736. // See if any files still exist before showing the warning message and the files attached.
  737. foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
  738. {
  739. if (strpos($attachID, 'post_tmp_' . $user_info['id']) === false)
  740. continue;
  741. if (file_exists($attachment['tmp_name']))
  742. {
  743. $post_errors[] = 'temp_attachments_new';
  744. $context['files_in_session_warning'] = $txt['attached_files_in_session'];
  745. unset($_SESSION['temp_attachments']['post']['files']);
  746. break;
  747. }
  748. }
  749. }
  750. else
  751. {
  752. // Since, they don't belong here. Let's inform the user that they exist..
  753. if (!empty($topic))
  754. $delete_link = '<a href="' . $scripturl . '?action=post' .(!empty($_REQUEST['msg']) ? (';msg=' . $_REQUEST['msg']) : '') . (!empty($_REQUEST['last_msg']) ? (';last_msg=' . $_REQUEST['last_msg']) : '') . ';topic=' . $topic . ';delete_temp">' . $txt['here'] . '</a>';
  755. else
  756. $delete_link = '<a href="' . $scripturl . '?action=post;board=' . $board . ';delete_temp">' . $txt['here'] . '</a>';
  757. // Compile a list of the files to show the user.
  758. $file_list = array();
  759. foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
  760. if (strpos($attachID, 'post_tmp_' . $user_info['id']) !== false)
  761. $file_list[] = $attachment['name'];
  762. $_SESSION['temp_attachments']['post']['files'] = $file_list;
  763. $file_list = '<div class="attachments">' . implode('<br />', $file_list) . '</div>';
  764. if (!empty($_SESSION['temp_attachments']['post']['msg']))
  765. {
  766. // We have a message id, so we can link back to the old topic they were trying to edit..
  767. $goback_link = '<a href="' . $scripturl . '?action=post' .(!empty($_SESSION['temp_attachments']['post']['msg']) ? (';msg=' . $_SESSION['temp_attachments']['post']['msg']) : '') . (!empty($_SESSION['temp_attachments']['post']['last_msg']) ? (';last_msg=' . $_SESSION['temp_attachments']['post']['last_msg']) : '') . ';topic=' . $_SESSION['temp_attachments']['post']['topic'] . ';additionalOptions">' . $txt['here'] . '</a>';
  768. $post_errors[] = array('temp_attachments_found', array($delete_link, $goback_link, $file_list));
  769. $context['ignore_temp_attachments'] = true;
  770. }
  771. else
  772. {
  773. $post_errors[] = array('temp_attachments_lost', array($delete_link, $file_list));
  774. $context['ignore_temp_attachments'] = true;
  775. }
  776. }
  777. }
  778. if (!empty($context['we_are_history']))
  779. $post_errors[] = $context['we_are_history'];
  780. foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
  781. {
  782. if (isset($context['ignore_temp_attachments']) || isset($_SESSION['temp_attachments']['post']['files']))
  783. break;
  784. if ($attachID != 'initial_error' && strpos($attachID, 'post_tmp_' . $user_info['id']) === false)
  785. continue;
  786. if ($attachID == 'initial_error')
  787. {
  788. $txt['error_attach_initial_error'] = $txt['attach_no_upload'] . '<div style="padding: 0 1em;">' . (is_array($attachment) ? vsprintf($txt[$attachment[0]], $attachment[1]) : $txt[$attachment]) . '</div>';
  789. $post_errors[] = 'attach_initial_error';
  790. unset($_SESSION['temp_attachments']);
  791. break;
  792. }
  793. // Show any errors which might of occured.
  794. if (!empty($attachment['errors']))
  795. {
  796. $txt['error_attach_errors'] = empty($txt['error_attach_errors']) ? '<br />' : '';
  797. $txt['error_attach_errors'] .= vsprintf($txt['attach_warning'], $attachment['name']) . '<div style="padding: 0 1em;">';
  798. foreach ($attachment['errors'] as $error)
  799. $txt['error_attach_errors'] .= (is_array($error) ? vsprintf($txt[$error[0]], $error[1]) : $txt[$error]) . '<br />';
  800. $txt['error_attach_errors'] .= '</div>';
  801. $post_errors[] = 'attach_errors';
  802. // Take out the trash.
  803. unset($_SESSION['temp_attachments'][$attachID]);
  804. if (file_exists($attachment['tmp_name']))
  805. unlink($attachment['tmp_name']);
  806. continue;
  807. }
  808. // More house keeping.
  809. if (!file_exists($attachment['tmp_name']))
  810. {
  811. unset($_SESSION['temp_attachments'][$attachID]);
  812. continue;
  813. }
  814. $context['attachments']['quantity']++;
  815. $context['attachments']['total_size'] += $attachment['size'];
  816. if (!isset($context['files_in_session_warning']))
  817. $context['files_in_session_warning'] = $txt['attached_files_in_session'];
  818. $context['current_attachments'][] = array(
  819. 'name' => '<u>' . htmlspecialchars($attachment['name']) . '</u>',
  820. 'size' => $attachment['size'],
  821. 'id' => $attachID,
  822. 'unchecked' => false,
  823. 'approved' => 1,
  824. );
  825. }
  826. }
  827. }
  828. // Do we need to show the visual verification image?
  829. $context['require_verification'] = !$user_info['is_mod'] && !$user_info['is_admin'] && !empty($modSettings['posts_require_captcha']) && ($user_info['posts'] < $modSettings['posts_require_captcha'] || ($user_info['is_guest'] && $modSettings['posts_require_captcha'] == -1));
  830. if ($context['require_verification'])
  831. {
  832. require_once($sourcedir . '/Subs-Editor.php');
  833. $verificationOptions = array(
  834. 'id' => 'post',
  835. );
  836. $context['require_verification'] = create_control_verification($verificationOptions);
  837. $context['visual_verification_id'] = $verificationOptions['id'];
  838. }
  839. // If they came from quick reply, and have to enter verification details, give them some notice.
  840. if (!empty($_REQUEST['from_qr']) && !empty($context['require_verification']))
  841. $post_errors[] = 'need_qr_verification';
  842. /*
  843. * There are two error types: serious and miinor. Serious errors
  844. * actually tell the user that a real error has occurred, while minor
  845. * errors are like warnings that let them know that something with
  846. * their post isn't right.
  847. */
  848. $minor_errors = array('not_approved', 'new_replies', 'old_topic', 'need_qr_verification', 'no_subject');
  849. call_integration_hook('integrate_post_errors', array($post_errors, $minor_errors));
  850. // Any errors occurred?
  851. if (!empty($post_errors))
  852. {
  853. loadLanguage('Errors');
  854. $context['error_type'] = 'minor';
  855. foreach ($post_errors as $post_error)
  856. if (is_array($post_error))
  857. {
  858. $post_error_id = $post_error[0];
  859. $context['post_error'][$post_error_id] = vsprintf($txt['error_' . $post_error_id], $post_error[1]);
  860. // If it's not a minor error flag it as such.
  861. if (!in_array($post_error_id, $minor_errors))
  862. $context['error_type'] = 'serious';
  863. }
  864. else
  865. {
  866. $context['post_error'][$post_error] = $txt['error_' . $post_error];
  867. // If it's not a minor error flag it as such.
  868. if (!in_array($post_error, $minor_errors))
  869. $context['error_type'] = 'serious';
  870. }
  871. }
  872. // What are you doing? Posting a poll, modifying, previewing, new post, or reply...
  873. if (isset($_REQUEST['poll']))
  874. $context['page_title'] = $txt['new_poll'];
  875. elseif ($context['make_event'])
  876. $context['page_title'] = $context['event']['id'] == -1 ? $txt['calendar_post_event'] : $txt['calendar_edit'];
  877. elseif (isset($_REQUEST['msg']))
  878. $context['page_title'] = $txt['modify_msg'];
  879. elseif (isset($_REQUEST['subject'], $context['preview_subject']))
  880. $context['page_title'] = $txt['preview'] . ' - ' . strip_tags($context['preview_subject']);
  881. elseif (empty($topic))
  882. $context['page_title'] = $txt['start_new_topic'];
  883. else
  884. $context['page_title'] = $txt['post_reply'];
  885. // Build the link tree.
  886. if (empty($topic))
  887. $context['linktree'][] = array(
  888. 'name' => '<em>' . $txt['start_new_topic'] . '</em>'
  889. );
  890. else
  891. $context['linktree'][] = array(
  892. 'url' => $scripturl . '?topic=' . $topic . '.' . $_REQUEST['start'],
  893. 'name' => $form_subject,
  894. 'extra_before' => '<span><strong class="nav">' . $context['page_title'] . ' ( </strong></span>',
  895. 'extra_after' => '<span><strong class="nav"> )</strong></span>'
  896. );
  897. // Give wireless a linktree url to the post screen, so that they can switch to full version.
  898. if (WIRELESS)
  899. $context['linktree'][count($context['linktree']) - 1]['url'] = $scripturl . '?action=post;' . (!empty($topic) ? 'topic=' . $topic : 'board=' . $board) . '.' . $_REQUEST['start'] . (isset($_REQUEST['msg']) ? ';msg=' . (int) $_REQUEST['msg'] . ';' . $context['session_var'] . '=' . $context['session_id'] : '');
  900. $context['subject'] = addcslashes($form_subject, '"');
  901. $context['message'] = str_replace(array('"', '<', '>', '&nbsp;'), array('&quot;', '&lt;', '&gt;', ' '), $form_message);
  902. // Are post drafts enabled?
  903. $context['drafts_save'] = !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_post_enabled']) && allowedTo('post_draft');
  904. $context['drafts_autosave'] = !empty($context['drafts_save']) && !empty($modSettings['drafts_autosave_enabled']) && allowedTo('post_autosave_draft');
  905. // Build a list of drafts that they can load in to the editor
  906. if (!empty($context['drafts_save']))
  907. {
  908. require_once($sourcedir . '/Drafts.php');
  909. ShowDrafts($user_info['id'], $topic);
  910. }
  911. // Needed for the editor and message icons.
  912. require_once($sourcedir . '/Subs-Editor.php');
  913. // Now create the editor.
  914. $editorOptions = array(
  915. 'id' => 'message',
  916. 'value' => $context['message'],
  917. 'labels' => array(
  918. 'post_button' => $context['submit_label'],
  919. ),
  920. // add height and width for the editor
  921. 'height' => '275px',
  922. 'width' => '100%',
  923. // We do XML preview here.
  924. 'preview_type' => 2,
  925. );
  926. create_control_richedit($editorOptions);
  927. // Store the ID.
  928. $context['post_box_name'] = $editorOptions['id'];
  929. $context['attached'] = '';
  930. $context['make_poll'] = isset($_REQUEST['poll']);
  931. // Message icons - customized icons are off?
  932. $context['icons'] = getMessageIcons($board);
  933. if (!empty($context['icons']))
  934. $context['icons'][count($context['icons']) - 1]['is_last'] = true;
  935. // Are we starting a poll? if set the poll icon as selected if its available
  936. if (isset($_REQUEST['poll']))
  937. {
  938. foreach ($context['icons'] as $icons)
  939. {
  940. if (isset($icons['value']) && $icons['value'] == 'poll')
  941. {
  942. // if found we are done
  943. $context['icon'] = 'poll';
  944. break;
  945. }
  946. }
  947. }
  948. $context['icon_url'] = '';
  949. for ($i = 0, $n = count($context['icons']); $i < $n; $i++)
  950. {
  951. $context['icons'][$i]['selected'] = $context['icon'] == $context['icons'][$i]['value'];
  952. if ($context['icons'][$i]['selected'])
  953. $context['icon_url'] = $context['icons'][$i]['url'];
  954. }
  955. if (empty($context['icon_url']))
  956. {
  957. $context['icon_url'] = $settings[file_exists($settings['theme_dir'] . '/images/post/' . $context['icon'] . '.png') ? 'images_url' : 'default_images_url'] . '/post/' . $context['icon'] . '.png';
  958. array_unshift($context['icons'], array(
  959. 'value' => $context['icon'],
  960. 'name' => $txt['current_icon'],
  961. 'url' => $context['icon_url'],
  962. 'is_last' => empty($context['icons']),
  963. 'selected' => true,
  964. ));
  965. }
  966. if (!empty($topic) && !empty($modSettings['topicSummaryPosts']))
  967. getTopic();
  968. // If the user can post attachments prepare the warning labels.
  969. if ($context['can_post_attachment'])
  970. {
  971. // If they've unchecked an attachment, they may still want to attach that many more files, but don't allow more than num_allowed_attachments.
  972. $context['num_allowed_attachments'] = empty($modSettings['attachmentNumPerPostLimit']) ? 50 : min($modSettings['attachmentNumPerPostLimit'] - count($context['current_attachments']), $modSettings['attachmentNumPerPostLimit']);
  973. $context['can_post_attachment_unapproved'] = allowedTo('post_attachment');
  974. $context['attachment_restrictions'] = array();
  975. $context['allowed_extensions'] = strtr(strtolower($modSettings['attachmentExtensions']), array(',' => ', '));
  976. $attachmentRestrictionTypes = array('attachmentNumPerPostLimit', 'attachmentPostLimit', 'attachmentSizeLimit');
  977. foreach ($attachmentRestrictionTypes as $type)
  978. if (!empty($modSettings[$type]))
  979. {
  980. $context['attachment_restrictions'][] = sprintf($txt['attach_restrict_' . $type], comma_format($modSettings[$type], 0));
  981. // Show some numbers. If they exist.
  982. if ($type == 'attachmentNumPerPostLimit' && $context['attachments']['quantity'] > 0)
  983. $context['attachment_restrictions'][] = sprintf($txt['attach_remaining'], $modSettings['attachmentNumPerPostLimit'] - $context['attachments']['quantity']);
  984. elseif ($type == 'attachmentPostLimit' && $context['attachments']['total_size'] > 0)
  985. $context['attachment_restrictions'][] = sprintf($txt['attach_available'], comma_format(round(max($modSettings['attachmentPostLimit'] - ($context['attachments']['total_size'] / 1028), 0)), 0));
  986. }
  987. }
  988. $context['back_to_topic'] = isset($_REQUEST['goback']) || (isset($_REQUEST['msg']) && !isset($_REQUEST['subject']));
  989. $context['show_additional_options'] = !empty($_POST['additional_options']) || isset($_SESSION['temp_attachments']['post']) || isset($_GET['additionalOptions']);
  990. $context['is_new_topic'] = empty($topic);
  991. $context['is_new_post'] = !isset($_REQUEST['msg']);
  992. $context['is_first_post'] = $context['is_new_topic'] || (isset($_REQUEST['msg']) && $_REQUEST['msg'] == $id_first_msg);
  993. // WYSIWYG only works if BBC is enabled
  994. $modSettings['disable_wysiwyg'] = !empty($modSettings['disable_wysiwyg']) || empty($modSettings['enableBBC']);
  995. // Register this form in the session variables.
  996. checkSubmitOnce('register');
  997. // Finally, load the template.
  998. if (WIRELESS && WIRELESS_PROTOCOL != 'wap')
  999. $context['sub_template'] = WIRELESS_PROTOCOL . '_post';
  1000. elseif (!isset($_REQUEST['xml']))
  1001. loadTemplate('Post');
  1002. }
  1003. /**
  1004. * Posts or saves the message composed with Post().
  1005. *
  1006. * requires various permissions depending on the action.
  1007. * handles attachment, post, and calendar saving.
  1008. * sends off notifications, and allows for announcements and moderation.
  1009. * accessed from ?action=post2.
  1010. */
  1011. function Post2()
  1012. {
  1013. global $board, $topic, $txt, $modSettings, $sourcedir, $context;
  1014. global $user_info, $board_info, $options, $smcFunc;
  1015. // Sneaking off, are we?
  1016. if (empty($_POST) && empty($topic))
  1017. {
  1018. if (empty($_SERVER['CONTENT_LENGTH']))
  1019. redirectexit('action=post;board=' . $board . '.0');
  1020. else
  1021. fatal_lang_error('post_upload_error', false);
  1022. }
  1023. elseif (empty($_POST) && !empty($topic))
  1024. redirectexit('action=post;topic=' . $topic . '.0');
  1025. // No need!
  1026. $context['robot_no_index'] = true;
  1027. // Previewing? Go back to start.
  1028. if (isset($_REQUEST['preview']))
  1029. return Post();
  1030. // Prevent double submission of this form.
  1031. checkSubmitOnce('check');
  1032. // No errors as yet.
  1033. $post_errors = array();
  1034. // If the session has timed out, let the user re-submit their form.
  1035. if (checkSession('post', '', false) != '')
  1036. $post_errors[] = 'session_timeout';
  1037. // Wrong verification code?
  1038. if (!$user_info['is_admin'] && !$user_info['is_mod'] && !empty($modSettings['posts_require_captcha']) && ($user_info['posts'] < $modSettings['posts_require_captcha'] || ($user_info['is_guest'] && $modSettings['posts_require_captcha'] == -1)))
  1039. {
  1040. require_once($sourcedir . '/Subs-Editor.php');
  1041. $verificationOptions = array(
  1042. 'id' => 'post',
  1043. );
  1044. $context['require_verification'] = create_control_verification($verificationOptions, true);
  1045. if (is_array($context['require_verification']))
  1046. $post_errors = array_merge($post_errors, $context['require_verification']);
  1047. }
  1048. require_once($sourcedir . '/Subs-Post.php');
  1049. loadLanguage('Post');
  1050. // Drafts enabled and needed?
  1051. if (!empty($modSettings['drafts_enabled']) && (isset($_POST['save_draft']) || isset($_POST['id_draft'])))
  1052. require_once($sourcedir . '/Drafts.php');
  1053. // First check to see if they are trying to delete any current attachments.
  1054. if (isset($_POST['attach_del']))
  1055. {
  1056. $keep_temp = array();
  1057. $keep_ids = array();
  1058. foreach ($_POST['attach_del'] as $dummy)
  1059. if (strpos($dummy, 'post_tmp_' . $user_info['id']) !== false)
  1060. $keep_temp[] = $dummy;
  1061. else
  1062. $keep_ids[] = (int) $dummy;
  1063. if (isset($_SESSION['temp_attachments']))
  1064. foreach($_SESSION['temp_attachments'] as $attachID => $attachment)
  1065. {
  1066. if ((isset($_SESSION['temp_attachments']['post']['files'], $attachment['name']) && in_array($attachment['name'], $_SESSION['temp_attachments']['post']['files'])) || in_array($attachID, $keep_temp) || strpos($attachID, 'post_tmp_' . $user_info['id']) === false)
  1067. continue;
  1068. unset($_SESSION['temp_attachments'][$attachID]);
  1069. unlink($attachment['tmp_name']);
  1070. }
  1071. if (!empty($_REQUEST['msg']))
  1072. {
  1073. require_once($sourcedir . '/ManageAttachments.php');
  1074. $attachmentQuery = array(
  1075. 'attachment_type' => 0,
  1076. 'id_msg' => (int) $_REQUEST['msg'],
  1077. 'not_id_attach' => $keep_ids,
  1078. );
  1079. removeAttachments($attachmentQuery);
  1080. }
  1081. }
  1082. // Then try to upload any attachments.
  1083. $context['can_post_attachment'] = !empty($modSettings['attachmentEnable']) && $modSettings['attachmentEnable'] == 1 && (allowedTo('post_attachment') || ($modSettings['postmod_active'] && allowedTo('post_unapproved_attachments')));
  1084. if ($context['can_post_attachment'] && empty($_POST['from_qr']))
  1085. {
  1086. require_once($sourcedir . '/Subs-Attachments.php');
  1087. processAttachments();
  1088. }
  1089. // If this isn't a new topic load the topic info that we need.
  1090. if (!empty($topic))
  1091. {
  1092. $request = $smcFunc['db_query']('', '
  1093. SELECT locked, is_sticky, id_poll, approved, id_first_msg, id_last_msg, id_member_started, id_board
  1094. FROM {db_prefix}topics
  1095. WHERE id_topic = {int:current_topic}
  1096. LIMIT 1',
  1097. array(
  1098. 'current_topic' => $topic,
  1099. )
  1100. );
  1101. $topic_info = $smcFunc['db_fetch_assoc']($request);
  1102. $smcFunc['db_free_result']($request);
  1103. // Though the topic should be there, it might have vanished.
  1104. if (!is_array($topic_info))
  1105. fatal_lang_error('topic_doesnt_exist');
  1106. // Did this topic suddenly move? Just checking...
  1107. if ($topic_

Large files files are truncated, but you can click here to view the full file