PageRenderTime 69ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/sources/controllers/Post.controller.php

https://github.com/Arantor/Elkarte
PHP | 2388 lines | 1737 code | 324 blank | 327 comment | 575 complexity | 20edfe0bd7cdd63d6a54702e7833b1db MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * The job of this file is to handle everything related to posting replies,
  16. * new topics, quotes, and modifications to existing posts. It also handles
  17. * quoting posts by way of javascript.
  18. *
  19. */
  20. if (!defined('ELKARTE'))
  21. die('No access...');
  22. /**
  23. * Handles showing the post screen, loading the post to be modified, and loading any post quoted.
  24. *
  25. * - additionally handles previews of posts.
  26. * - @uses the Post template and language file, main sub template.
  27. * - allows wireless access using the protocol_post sub template.
  28. * - requires different permissions depending on the actions, but most notably post_new, post_reply_own, and post_reply_any.
  29. * - shows options for the editing and posting of calendar events and attachments, as well as the posting of polls.
  30. * - accessed from ?action=post.
  31. *
  32. * @param array $post_errors holds any errors found tyring to post
  33. */
  34. function action_post()
  35. {
  36. global $txt, $scripturl, $topic, $modSettings, $board;
  37. global $user_info, $context, $settings;
  38. global $options, $smcFunc, $language;
  39. loadLanguage('Post');
  40. // You can't reply with a poll... hacker.
  41. if (isset($_REQUEST['poll']) && !empty($topic) && !isset($_REQUEST['msg']))
  42. unset($_REQUEST['poll']);
  43. $post_errors = error_context::context('post', 1);
  44. $attach_errors = error_context::context('attachment', 1);
  45. // Posting an event?
  46. $context['make_event'] = isset($_REQUEST['calendar']);
  47. $context['robot_no_index'] = true;
  48. // You must be posting to *some* board.
  49. if (empty($board) && !$context['make_event'])
  50. fatal_lang_error('no_board', false);
  51. require_once(SUBSDIR . '/Post.subs.php');
  52. if (isset($_REQUEST['xml']))
  53. {
  54. $context['sub_template'] = 'post';
  55. // Just in case of an earlier error...
  56. $context['preview_message'] = '';
  57. $context['preview_subject'] = '';
  58. }
  59. // No message is complete without a topic.
  60. if (empty($topic) && !empty($_REQUEST['msg']))
  61. {
  62. $request = $smcFunc['db_query']('', '
  63. SELECT id_topic
  64. FROM {db_prefix}messages
  65. WHERE id_msg = {int:msg}',
  66. array(
  67. 'msg' => (int) $_REQUEST['msg'],
  68. ));
  69. if ($smcFunc['db_num_rows']($request) != 1)
  70. unset($_REQUEST['msg'], $_POST['msg'], $_GET['msg']);
  71. else
  72. list ($topic) = $smcFunc['db_fetch_row']($request);
  73. $smcFunc['db_free_result']($request);
  74. }
  75. // Check if it's locked. It isn't locked if no topic is specified.
  76. if (!empty($topic))
  77. {
  78. $request = $smcFunc['db_query']('', '
  79. SELECT
  80. t.locked, IFNULL(ln.id_topic, 0) AS notify, t.is_sticky, t.id_poll, t.id_last_msg, mf.id_member,
  81. t.id_first_msg, mf.subject,
  82. CASE WHEN ml.poster_time > ml.modified_time THEN ml.poster_time ELSE ml.modified_time END AS last_post_time
  83. FROM {db_prefix}topics AS t
  84. LEFT JOIN {db_prefix}log_notify AS ln ON (ln.id_topic = t.id_topic AND ln.id_member = {int:current_member})
  85. LEFT JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
  86. LEFT JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  87. WHERE t.id_topic = {int:current_topic}
  88. LIMIT 1',
  89. array(
  90. 'current_member' => $user_info['id'],
  91. 'current_topic' => $topic,
  92. )
  93. );
  94. list ($locked, $context['notify'], $sticky, $pollID, $context['topic_last_message'], $id_member_poster, $id_first_msg, $first_subject, $lastPostTime) = $smcFunc['db_fetch_row']($request);
  95. $smcFunc['db_free_result']($request);
  96. // If this topic already has a poll, they sure can't add another.
  97. if (isset($_REQUEST['poll']) && $pollID > 0)
  98. unset($_REQUEST['poll']);
  99. if (empty($_REQUEST['msg']))
  100. {
  101. if ($user_info['is_guest'] && !allowedTo('post_reply_any') && (!$modSettings['postmod_active'] || !allowedTo('post_unapproved_replies_any')))
  102. is_not_guest();
  103. // By default the reply will be approved...
  104. $context['becomes_approved'] = true;
  105. if ($id_member_poster != $user_info['id'])
  106. {
  107. if ($modSettings['postmod_active'] && allowedTo('post_unapproved_replies_any') && !allowedTo('post_reply_any'))
  108. $context['becomes_approved'] = false;
  109. else
  110. isAllowedTo('post_reply_any');
  111. }
  112. elseif (!allowedTo('post_reply_any'))
  113. {
  114. if ($modSettings['postmod_active'] && allowedTo('post_unapproved_replies_own') && !allowedTo('post_reply_own'))
  115. $context['becomes_approved'] = false;
  116. else
  117. isAllowedTo('post_reply_own');
  118. }
  119. }
  120. else
  121. $context['becomes_approved'] = true;
  122. $context['can_lock'] = allowedTo('lock_any') || ($user_info['id'] == $id_member_poster && allowedTo('lock_own'));
  123. $context['can_sticky'] = allowedTo('make_sticky') && !empty($modSettings['enableStickyTopics']);
  124. $context['notify'] = !empty($context['notify']);
  125. $context['sticky'] = isset($_REQUEST['sticky']) ? !empty($_REQUEST['sticky']) : $sticky;
  126. // Check whether this is a really old post being bumped...
  127. if (!empty($modSettings['oldTopicDays']) && $lastPostTime + $modSettings['oldTopicDays'] * 86400 < time() && empty($sticky) && !isset($_REQUEST['subject']))
  128. $post_errors->addError(array('old_topic', array($modSettings['oldTopicDays'])), 0);
  129. }
  130. else
  131. {
  132. $context['becomes_approved'] = true;
  133. if ((!$context['make_event'] || !empty($board)))
  134. {
  135. if ($modSettings['postmod_active'] && !allowedTo('post_new') && allowedTo('post_unapproved_topics'))
  136. $context['becomes_approved'] = false;
  137. else
  138. isAllowedTo('post_new');
  139. }
  140. $locked = 0;
  141. // @todo These won't work if you're making an event.
  142. $context['can_lock'] = allowedTo(array('lock_any', 'lock_own'));
  143. $context['can_sticky'] = allowedTo('make_sticky') && !empty($modSettings['enableStickyTopics']);
  144. $context['notify'] = !empty($context['notify']);
  145. $context['sticky'] = !empty($_REQUEST['sticky']);
  146. }
  147. // @todo These won't work if you're posting an event!
  148. $context['can_notify'] = allowedTo('mark_any_notify');
  149. $context['can_move'] = allowedTo('move_any');
  150. $context['move'] = !empty($_REQUEST['move']);
  151. $context['announce'] = !empty($_REQUEST['announce']);
  152. // You can only announce topics that will get approved...
  153. $context['can_announce'] = allowedTo('announce_topic') && $context['becomes_approved'];
  154. $context['locked'] = !empty($locked) || !empty($_REQUEST['lock']);
  155. $context['can_quote'] = empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC']));
  156. // Generally don't show the approval box... (Assume we want things approved)
  157. $context['show_approval'] = allowedTo('approve_posts') && $context['becomes_approved'] ? 2 : (allowedTo('approve_posts') ? 1 : 0);
  158. // An array to hold all the attachments for this topic.
  159. $context['current_attachments'] = array();
  160. // Don't allow a post if it's locked and you aren't all powerful.
  161. if ($locked && !allowedTo('moderate_board'))
  162. fatal_lang_error('topic_locked', false);
  163. // Check the users permissions - is the user allowed to add or post a poll?
  164. if (isset($_REQUEST['poll']) && $modSettings['pollMode'] == '1')
  165. {
  166. // New topic, new poll.
  167. if (empty($topic))
  168. isAllowedTo('poll_post');
  169. // This is an old topic - but it is yours! Can you add to it?
  170. elseif ($user_info['id'] == $id_member_poster && !allowedTo('poll_add_any'))
  171. isAllowedTo('poll_add_own');
  172. // If you're not the owner, can you add to any poll?
  173. else
  174. isAllowedTo('poll_add_any');
  175. $context['can_moderate_poll'] = true;
  176. require_once(SUBSDIR . '/Members.subs.php');
  177. $allowedVoteGroups = groupsAllowedTo('poll_vote', $board);
  178. // Set up the poll options.
  179. $context['poll'] = array(
  180. 'max_votes' => empty($_POST['poll_max_votes']) ? '1' : max(1, $_POST['poll_max_votes']),
  181. 'hide_results' => empty($_POST['poll_hide']) ? 0 : $_POST['poll_hide'],
  182. 'expiration' => !isset($_POST['poll_expire']) ? '' : $_POST['poll_expire'],
  183. 'change_vote' => isset($_POST['poll_change_vote']),
  184. 'guest_vote' => isset($_POST['poll_guest_vote']),
  185. 'guest_vote_allowed' => in_array(-1, $allowedVoteGroups['allowed']),
  186. );
  187. // Make all five poll choices empty.
  188. $context['choices'] = array(
  189. array('id' => 0, 'number' => 1, 'label' => '', 'is_last' => false),
  190. array('id' => 1, 'number' => 2, 'label' => '', 'is_last' => false),
  191. array('id' => 2, 'number' => 3, 'label' => '', 'is_last' => false),
  192. array('id' => 3, 'number' => 4, 'label' => '', 'is_last' => false),
  193. array('id' => 4, 'number' => 5, 'label' => '', 'is_last' => true)
  194. );
  195. $context['last_choice_id'] = 4;
  196. }
  197. if ($context['make_event'])
  198. {
  199. // They might want to pick a board.
  200. if (!isset($context['current_board']))
  201. $context['current_board'] = 0;
  202. // Start loading up the event info.
  203. $context['event'] = array();
  204. $context['event']['title'] = isset($_REQUEST['evtitle']) ? htmlspecialchars(stripslashes($_REQUEST['evtitle'])) : '';
  205. $context['event']['id'] = isset($_REQUEST['eventid']) ? (int) $_REQUEST['eventid'] : -1;
  206. $context['event']['new'] = $context['event']['id'] == -1;
  207. // Permissions check!
  208. isAllowedTo('calendar_post');
  209. // Editing an event? (but NOT previewing!?)
  210. if (empty($context['event']['new']) && !isset($_REQUEST['subject']))
  211. {
  212. // If the user doesn't have permission to edit the post in this topic, redirect them.
  213. if ((empty($id_member_poster) || $id_member_poster != $user_info['id'] || !allowedTo('modify_own')) && !allowedTo('modify_any'))
  214. {
  215. // @todo this shouldn't call directly CalendarPost()
  216. require_once(CONTROLLERDIR . '/Calendar.controller.php');
  217. return CalendarPost();
  218. }
  219. // Get the current event information.
  220. $request = $smcFunc['db_query']('', '
  221. SELECT
  222. id_member, title, MONTH(start_date) AS month, DAYOFMONTH(start_date) AS day,
  223. YEAR(start_date) AS year, (TO_DAYS(end_date) - TO_DAYS(start_date)) AS span
  224. FROM {db_prefix}calendar
  225. WHERE id_event = {int:id_event}
  226. LIMIT 1',
  227. array(
  228. 'id_event' => $context['event']['id'],
  229. )
  230. );
  231. $row = $smcFunc['db_fetch_assoc']($request);
  232. $smcFunc['db_free_result']($request);
  233. // Make sure the user is allowed to edit this event.
  234. if ($row['id_member'] != $user_info['id'])
  235. isAllowedTo('calendar_edit_any');
  236. elseif (!allowedTo('calendar_edit_any'))
  237. isAllowedTo('calendar_edit_own');
  238. $context['event']['month'] = $row['month'];
  239. $context['event']['day'] = $row['day'];
  240. $context['event']['year'] = $row['year'];
  241. $context['event']['title'] = $row['title'];
  242. $context['event']['span'] = $row['span'] + 1;
  243. }
  244. else
  245. {
  246. $today = getdate();
  247. // You must have a month and year specified!
  248. if (!isset($_REQUEST['month']))
  249. $_REQUEST['month'] = $today['mon'];
  250. if (!isset($_REQUEST['year']))
  251. $_REQUEST['year'] = $today['year'];
  252. $context['event']['month'] = (int) $_REQUEST['month'];
  253. $context['event']['year'] = (int) $_REQUEST['year'];
  254. $context['event']['day'] = isset($_REQUEST['day']) ? $_REQUEST['day'] : ($_REQUEST['month'] == $today['mon'] ? $today['mday'] : 0);
  255. $context['event']['span'] = isset($_REQUEST['span']) ? $_REQUEST['span'] : 1;
  256. // Make sure the year and month are in the valid range.
  257. if ($context['event']['month'] < 1 || $context['event']['month'] > 12)
  258. fatal_lang_error('invalid_month', false);
  259. if ($context['event']['year'] < $modSettings['cal_minyear'] || $context['event']['year'] > $modSettings['cal_maxyear'])
  260. fatal_lang_error('invalid_year', false);
  261. // Get a list of boards they can post in.
  262. $boards = boardsAllowedTo('post_new');
  263. if (empty($boards))
  264. fatal_lang_error('cannot_post_new', 'user');
  265. // Load a list of boards for this event in the context.
  266. require_once(SUBSDIR . '/MessageIndex.subs.php');
  267. $boardListOptions = array(
  268. 'included_boards' => in_array(0, $boards) ? null : $boards,
  269. 'not_redirection' => true,
  270. 'use_permissions' => true,
  271. 'selected_board' => empty($context['current_board']) ? $modSettings['cal_defaultboard'] : $context['current_board'],
  272. );
  273. $context['event']['categories'] = getBoardList($boardListOptions);
  274. }
  275. // Find the last day of the month.
  276. $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']));
  277. $context['event']['board'] = !empty($board) ? $board : $modSettings['cal_defaultboard'];
  278. }
  279. // See if any new replies have come along.
  280. // Huh, $_REQUEST['msg'] is set upon submit, so this doesn't get executed at submit
  281. // only at preview
  282. if (empty($_REQUEST['msg']) && !empty($topic))
  283. {
  284. if (empty($options['no_new_reply_warning']) && isset($_REQUEST['last_msg']) && $context['topic_last_message'] > $_REQUEST['last_msg'])
  285. {
  286. $request = $smcFunc['db_query']('', '
  287. SELECT COUNT(*)
  288. FROM {db_prefix}messages
  289. WHERE id_topic = {int:current_topic}
  290. AND id_msg > {int:last_msg}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  291. AND approved = {int:approved}') . '
  292. LIMIT 1',
  293. array(
  294. 'current_topic' => $topic,
  295. 'last_msg' => (int) $_REQUEST['last_msg'],
  296. 'approved' => 1,
  297. )
  298. );
  299. list ($context['new_replies']) = $smcFunc['db_fetch_row']($request);
  300. $smcFunc['db_free_result']($request);
  301. if (!empty($context['new_replies']))
  302. {
  303. if ($context['new_replies'] == 1)
  304. $txt['error_new_replies'] = isset($_GET['last_msg']) ? $txt['error_new_reply_reading'] : $txt['error_new_reply'];
  305. else
  306. $txt['error_new_replies'] = sprintf(isset($_GET['last_msg']) ? $txt['error_new_replies_reading'] : $txt['error_new_replies'], $context['new_replies']);
  307. $post_errors->addError('new_replies', 0);
  308. $modSettings['topicSummaryPosts'] = $context['new_replies'] > $modSettings['topicSummaryPosts'] ? max($modSettings['topicSummaryPosts'], 5) : $modSettings['topicSummaryPosts'];
  309. }
  310. }
  311. }
  312. // Get a response prefix (like 'Re:') in the default forum language.
  313. if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
  314. {
  315. if ($language === $user_info['language'])
  316. $context['response_prefix'] = $txt['response_prefix'];
  317. else
  318. {
  319. loadLanguage('index', $language, false);
  320. $context['response_prefix'] = $txt['response_prefix'];
  321. loadLanguage('index');
  322. }
  323. cache_put_data('response_prefix', $context['response_prefix'], 600);
  324. }
  325. // Previewing, modifying, or posting?
  326. // Do we have a body, but an error happened.
  327. if (isset($_REQUEST['message']) || $post_errors->hasErrors())
  328. {
  329. // Validate inputs.
  330. if (!$post_errors->hasErrors())
  331. {
  332. // This means they didn't click Post and get an error.
  333. $really_previewing = true;
  334. }
  335. else
  336. {
  337. if (!isset($_REQUEST['subject']))
  338. $_REQUEST['subject'] = '';
  339. if (!isset($_REQUEST['message']))
  340. $_REQUEST['message'] = '';
  341. if (!isset($_REQUEST['icon']))
  342. $_REQUEST['icon'] = 'xx';
  343. // They are previewing if they asked to preview (i.e. came from quick reply).
  344. $really_previewing = !empty($_POST['preview']);
  345. }
  346. // In order to keep the approval status flowing through, we have to pass it through the form...
  347. $context['becomes_approved'] = empty($_REQUEST['not_approved']);
  348. $context['show_approval'] = isset($_REQUEST['approve']) ? ($_REQUEST['approve'] ? 2 : 1) : 0;
  349. $context['can_announce'] &= $context['becomes_approved'];
  350. // Set up the inputs for the form.
  351. $form_subject = strtr($smcFunc['htmlspecialchars']($_REQUEST['subject']), array("\r" => '', "\n" => '', "\t" => ''));
  352. $form_message = $smcFunc['htmlspecialchars']($_REQUEST['message'], ENT_QUOTES);
  353. // Make sure the subject isn't too long - taking into account special characters.
  354. if ($smcFunc['strlen']($form_subject) > 100)
  355. $form_subject = $smcFunc['substr']($form_subject, 0, 100);
  356. if (isset($_REQUEST['poll']))
  357. {
  358. $context['poll']['question'] = isset($_REQUEST['question']) ? $smcFunc['htmlspecialchars'](trim($_REQUEST['question'])) : '';
  359. $context['choices'] = array();
  360. $choice_id = 0;
  361. $_POST['options'] = empty($_POST['options']) ? array() : htmlspecialchars__recursive($_POST['options']);
  362. foreach ($_POST['options'] as $option)
  363. {
  364. if (trim($option) == '')
  365. continue;
  366. $context['choices'][] = array(
  367. 'id' => $choice_id++,
  368. 'number' => $choice_id,
  369. 'label' => $option,
  370. 'is_last' => false
  371. );
  372. }
  373. // One empty option for those with js disabled...I know are few... :P
  374. $context['choices'][] = array(
  375. 'id' => $choice_id++,
  376. 'number' => $choice_id,
  377. 'label' => '',
  378. 'is_last' => false
  379. );
  380. if (count($context['choices']) < 2)
  381. {
  382. $context['choices'][] = array(
  383. 'id' => $choice_id++,
  384. 'number' => $choice_id,
  385. 'label' => '',
  386. 'is_last' => false
  387. );
  388. }
  389. $context['last_choice_id'] = $choice_id;
  390. $context['choices'][count($context['choices']) - 1]['is_last'] = true;
  391. }
  392. // Are you... a guest?
  393. if ($user_info['is_guest'])
  394. {
  395. $_REQUEST['guestname'] = !isset($_REQUEST['guestname']) ? '' : trim($_REQUEST['guestname']);
  396. $_REQUEST['email'] = !isset($_REQUEST['email']) ? '' : trim($_REQUEST['email']);
  397. $_REQUEST['guestname'] = htmlspecialchars($_REQUEST['guestname']);
  398. $context['name'] = $_REQUEST['guestname'];
  399. $_REQUEST['email'] = htmlspecialchars($_REQUEST['email']);
  400. $context['email'] = $_REQUEST['email'];
  401. $user_info['name'] = $_REQUEST['guestname'];
  402. }
  403. // Only show the preview stuff if they hit Preview.
  404. if (($really_previewing === true || isset($_REQUEST['xml'])) && !isset($_REQUEST['save_draft']))
  405. {
  406. // Set up the preview message and subject
  407. $context['preview_message'] = $form_message;
  408. preparsecode($form_message, true);
  409. // Do all bulletin board code thing on the message
  410. preparsecode($context['preview_message']);
  411. $context['preview_message'] = parse_bbc($context['preview_message'], isset($_REQUEST['ns']) ? 0 : 1);
  412. censorText($context['preview_message']);
  413. // Dont forget the subject
  414. $context['preview_subject'] = $form_subject;
  415. censorText($context['preview_subject']);
  416. // Any errors we should tell them about?
  417. if ($form_subject === '')
  418. {
  419. $post_errors->addError('no_subject', 0);
  420. $context['preview_subject'] = '<em>' . $txt['no_subject'] . '</em>';
  421. }
  422. if ($context['preview_message'] === '')
  423. $post_errors->addError('no_message');
  424. elseif (!empty($modSettings['max_messageLength']) && $smcFunc['strlen']($form_message) > $modSettings['max_messageLength'])
  425. $post_errors->addError(array('long_message', array($modSettings['max_messageLength'])));
  426. // Protect any CDATA blocks.
  427. if (isset($_REQUEST['xml']))
  428. $context['preview_message'] = strtr($context['preview_message'], array(']]>' => ']]]]><![CDATA[>'));
  429. }
  430. // Set up the checkboxes.
  431. $context['notify'] = !empty($_REQUEST['notify']);
  432. $context['use_smileys'] = !isset($_REQUEST['ns']);
  433. $context['icon'] = isset($_REQUEST['icon']) ? preg_replace('~[\./\\\\*\':"<>]~', '', $_REQUEST['icon']) : 'xx';
  434. // Set the destination action for submission.
  435. $context['destination'] = 'post2;start=' . $_REQUEST['start'] . (isset($_REQUEST['msg']) ? ';msg=' . $_REQUEST['msg'] . ';' . $context['session_var'] . '=' . $context['session_id'] : '') . (isset($_REQUEST['poll']) ? ';poll' : '');
  436. $context['submit_label'] = isset($_REQUEST['msg']) ? $txt['save'] : $txt['post'];
  437. // Previewing an edit?
  438. if (isset($_REQUEST['msg']) && !empty($topic))
  439. {
  440. require_once(SUBSDIR . '/Messages.subs.php');
  441. // Get the existing message.
  442. $message = getExistingMessage((int) $_REQUEST['msg'], $topic);
  443. // The message they were trying to edit was most likely deleted.
  444. // @todo Change this error message?
  445. if ($message === false)
  446. fatal_lang_error('no_board', false);
  447. $errors = checkMessagePermissions($message['message']);
  448. if (!empty($errors))
  449. foreach ($errors as $error)
  450. $post_errors->addError($error);
  451. prepareMessageContext($message);
  452. }
  453. // No check is needed, since nothing is really posted.
  454. checkSubmitOnce('free');
  455. }
  456. // Editing a message...
  457. elseif (isset($_REQUEST['msg']) && !empty($topic))
  458. {
  459. $_REQUEST['msg'] = (int) $_REQUEST['msg'];
  460. require_once(SUBSDIR . '/Messages.subs.php');
  461. // Get the existing message.
  462. $message = getExistingMessage((int) $_REQUEST['msg'], $topic);
  463. // The message they were trying to edit was most likely deleted.
  464. if ($message === false)
  465. fatal_lang_error('no_message', false);
  466. $errors = checkMessagePermissions($message['message']);
  467. if (!empty($errors))
  468. foreach ($errors as $error)
  469. $post_errors->addError($error);
  470. prepareMessageContext($message);
  471. // Get the stuff ready for the form.
  472. $form_subject = $message['message']['subject'];
  473. $form_message = un_preparsecode($message['message']['body']);
  474. censorText($form_message);
  475. censorText($form_subject);
  476. // Check the boxes that should be checked.
  477. $context['use_smileys'] = !empty($message['message']['smileys_enabled']);
  478. $context['icon'] = $message['message']['icon'];
  479. // Set the destinaton.
  480. $context['destination'] = 'post2;start=' . $_REQUEST['start'] . ';msg=' . $_REQUEST['msg'] . ';' . $context['session_var'] . '=' . $context['session_id'] . (isset($_REQUEST['poll']) ? ';poll' : '');
  481. $context['submit_label'] = $txt['save'];
  482. }
  483. // Posting...
  484. else
  485. {
  486. // By default....
  487. $context['use_smileys'] = true;
  488. $context['icon'] = 'xx';
  489. if ($user_info['is_guest'])
  490. {
  491. $context['name'] = isset($_SESSION['guest_name']) ? $_SESSION['guest_name'] : '';
  492. $context['email'] = isset($_SESSION['guest_email']) ? $_SESSION['guest_email'] : '';
  493. }
  494. $context['destination'] = 'post2;start=' . $_REQUEST['start'] . (isset($_REQUEST['poll']) ? ';poll' : '');
  495. $context['submit_label'] = $txt['post'];
  496. // Posting a quoted reply?
  497. if (!empty($topic) && !empty($_REQUEST['quote']))
  498. {
  499. // Make sure they _can_ quote this post, and if so get it.
  500. $request = $smcFunc['db_query']('', '
  501. SELECT m.subject, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.body
  502. FROM {db_prefix}messages AS m
  503. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND {query_see_board})
  504. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  505. WHERE m.id_msg = {int:id_msg}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  506. AND m.approved = {int:is_approved}') . '
  507. LIMIT 1',
  508. array(
  509. 'id_msg' => (int) $_REQUEST['quote'],
  510. 'is_approved' => 1,
  511. )
  512. );
  513. if ($smcFunc['db_num_rows']($request) == 0)
  514. fatal_lang_error('quoted_post_deleted', false);
  515. list ($form_subject, $mname, $mdate, $form_message) = $smcFunc['db_fetch_row']($request);
  516. $smcFunc['db_free_result']($request);
  517. // Add 'Re: ' to the front of the quoted subject.
  518. if (trim($context['response_prefix']) != '' && $smcFunc['strpos']($form_subject, trim($context['response_prefix'])) !== 0)
  519. $form_subject = $context['response_prefix'] . $form_subject;
  520. // Censor the message and subject.
  521. censorText($form_message);
  522. censorText($form_subject);
  523. // But if it's in HTML world, turn them into htmlspecialchar's so they can be edited!
  524. if (strpos($form_message, '[html]') !== false)
  525. {
  526. $parts = preg_split('~(\[/code\]|\[code(?:=[^\]]+)?\])~i', $form_message, -1, PREG_SPLIT_DELIM_CAPTURE);
  527. for ($i = 0, $n = count($parts); $i < $n; $i++)
  528. {
  529. // It goes 0 = outside, 1 = begin tag, 2 = inside, 3 = close tag, repeat.
  530. if ($i % 4 == 0)
  531. $parts[$i] = preg_replace('~\[html\](.+?)\[/html\]~ise', '\'[html]\' . preg_replace(\'~<br\s?/?' . '>~i\', \'&lt;br /&gt;<br />\', \'$1\') . \'[/html]\'', $parts[$i]);
  532. }
  533. $form_message = implode('', $parts);
  534. }
  535. $form_message = preg_replace('~<br ?/?' . '>~i', "\n", $form_message);
  536. // Remove any nested quotes, if necessary.
  537. if (!empty($modSettings['removeNestedQuotes']))
  538. $form_message = preg_replace(array('~\n?\[quote.*?\].+?\[/quote\]\n?~is', '~^\n~', '~\[/quote\]~'), '', $form_message);
  539. // Add a quote string on the front and end.
  540. $form_message = '[quote author=' . $mname . ' link=topic=' . $topic . '.msg' . (int) $_REQUEST['quote'] . '#msg' . (int) $_REQUEST['quote'] . ' date=' . $mdate . ']' . "\n" . rtrim($form_message) . "\n" . '[/quote]';
  541. }
  542. // Posting a reply without a quote?
  543. elseif (!empty($topic) && empty($_REQUEST['quote']))
  544. {
  545. // Get the first message's subject.
  546. $form_subject = $first_subject;
  547. // Add 'Re: ' to the front of the subject.
  548. if (trim($context['response_prefix']) != '' && $form_subject != '' && $smcFunc['strpos']($form_subject, trim($context['response_prefix'])) !== 0)
  549. $form_subject = $context['response_prefix'] . $form_subject;
  550. // Censor the subject.
  551. censorText($form_subject);
  552. $form_message = '';
  553. }
  554. else
  555. {
  556. $form_subject = isset($_GET['subject']) ? $_GET['subject'] : '';
  557. $form_message = '';
  558. }
  559. }
  560. $context['can_post_attachment'] = !empty($modSettings['attachmentEnable']) && $modSettings['attachmentEnable'] == 1 && (allowedTo('post_attachment') || ($modSettings['postmod_active'] && allowedTo('post_unapproved_attachments')));
  561. if ($context['can_post_attachment'])
  562. {
  563. // If there are attachments, calculate the total size and how many.
  564. $context['attachments']['total_size'] = 0;
  565. $context['attachments']['quantity'] = 0;
  566. // If this isn't a new post, check the current attachments.
  567. if (isset($_REQUEST['msg']))
  568. {
  569. $context['attachments']['quantity'] = count($context['current_attachments']);
  570. foreach ($context['current_attachments'] as $attachment)
  571. $context['attachments']['total_size'] += $attachment['size'];
  572. }
  573. // A bit of house keeping first.
  574. if (!empty($_SESSION['temp_attachments']) && count($_SESSION['temp_attachments']) == 1)
  575. unset($_SESSION['temp_attachments']);
  576. if (!empty($_SESSION['temp_attachments']))
  577. {
  578. // Is this a request to delete them?
  579. if (isset($_GET['delete_temp']))
  580. {
  581. foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
  582. {
  583. if (strpos($attachID, 'post_tmp_' . $user_info['id']) !== false)
  584. if (file_exists($attachment['tmp_name']))
  585. unlink($attachment['tmp_name']);
  586. }
  587. $attach_errors->addError('temp_attachments_gone');
  588. $_SESSION['temp_attachments'] = array();
  589. }
  590. // Hmm, coming in fresh and there are files in session.
  591. elseif ($context['current_action'] != 'post2' || !empty($_POST['from_qr']))
  592. {
  593. // Let's be nice and see if they belong here first.
  594. 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']))
  595. {
  596. // See if any files still exist before showing the warning message and the files attached.
  597. foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
  598. {
  599. if (strpos($attachID, 'post_tmp_' . $user_info['id']) === false)
  600. continue;
  601. if (file_exists($attachment['tmp_name']))
  602. {
  603. $attach_errors->addError('temp_attachments_new');
  604. $context['files_in_session_warning'] = $txt['attached_files_in_session'];
  605. unset($_SESSION['temp_attachments']['post']['files']);
  606. break;
  607. }
  608. }
  609. }
  610. else
  611. {
  612. // Since, they don't belong here. Let's inform the user that they exist..
  613. if (!empty($topic))
  614. $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>';
  615. else
  616. $delete_link = '<a href="' . $scripturl . '?action=post;board=' . $board . ';delete_temp">' . $txt['here'] . '</a>';
  617. // Compile a list of the files to show the user.
  618. $file_list = array();
  619. foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
  620. if (strpos($attachID, 'post_tmp_' . $user_info['id']) !== false)
  621. $file_list[] = $attachment['name'];
  622. $_SESSION['temp_attachments']['post']['files'] = $file_list;
  623. $file_list = '<div class="attachments">' . implode('<br />', $file_list) . '</div>';
  624. if (!empty($_SESSION['temp_attachments']['post']['msg']))
  625. {
  626. // We have a message id, so we can link back to the old topic they were trying to edit..
  627. $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>';
  628. $attach_errors->addError(array('temp_attachments_found', array($delete_link, $goback_link, $file_list)));
  629. $context['ignore_temp_attachments'] = true;
  630. }
  631. else
  632. {
  633. $attach_errors->addError(array('temp_attachments_lost', array($delete_link, $file_list)));
  634. $context['ignore_temp_attachments'] = true;
  635. }
  636. }
  637. }
  638. if (!empty($context['we_are_history']))
  639. $attach_errors->addError($context['we_are_history']);
  640. foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
  641. {
  642. if (isset($context['ignore_temp_attachments']) || isset($_SESSION['temp_attachments']['post']['files']))
  643. break;
  644. if ($attachID != 'initial_error' && strpos($attachID, 'post_tmp_' . $user_info['id']) === false)
  645. continue;
  646. if ($attachID == 'initial_error')
  647. {
  648. $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>';
  649. $attach_errors->addError('attach_initial_error');
  650. unset($_SESSION['temp_attachments']);
  651. break;
  652. }
  653. // Show any errors which might of occured.
  654. if (!empty($attachment['errors']))
  655. {
  656. $txt['error_attach_errors'] = empty($txt['error_attach_errors']) ? '<br />' : '';
  657. $txt['error_attach_errors'] .= vsprintf($txt['attach_warning'], $attachment['name']) . '<div style="padding: 0 1em;">';
  658. foreach ($attachment['errors'] as $error)
  659. $txt['error_attach_errors'] .= (is_array($error) ? vsprintf($txt[$error[0]], $error[1]) : $txt[$error]) . '<br />';
  660. $txt['error_attach_errors'] .= '</div>';
  661. $attach_errors->addError('attach_errors');
  662. // Take out the trash.
  663. unset($_SESSION['temp_attachments'][$attachID]);
  664. if (file_exists($attachment['tmp_name']))
  665. unlink($attachment['tmp_name']);
  666. continue;
  667. }
  668. // More house keeping.
  669. if (!file_exists($attachment['tmp_name']))
  670. {
  671. unset($_SESSION['temp_attachments'][$attachID]);
  672. continue;
  673. }
  674. $context['attachments']['quantity']++;
  675. $context['attachments']['total_size'] += $attachment['size'];
  676. if (!isset($context['files_in_session_warning']))
  677. $context['files_in_session_warning'] = $txt['attached_files_in_session'];
  678. $context['current_attachments'][] = array(
  679. 'name' => '<u>' . htmlspecialchars($attachment['name']) . '</u>',
  680. 'size' => $attachment['size'],
  681. 'id' => $attachID,
  682. 'unchecked' => false,
  683. 'approved' => 1,
  684. );
  685. }
  686. }
  687. }
  688. // Do we need to show the visual verification image?
  689. $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));
  690. if ($context['require_verification'])
  691. {
  692. require_once(SUBSDIR . '/Editor.subs.php');
  693. $verificationOptions = array(
  694. 'id' => 'post',
  695. );
  696. $context['require_verification'] = create_control_verification($verificationOptions);
  697. $context['visual_verification_id'] = $verificationOptions['id'];
  698. }
  699. // If they came from quick reply, and have to enter verification details, give them some notice.
  700. if (!empty($_REQUEST['from_qr']) && !empty($context['require_verification']))
  701. $post_errors->addError('need_qr_verification', 0);
  702. // Any errors occurred?
  703. $context['post_error'] = array(
  704. 'errors' => $post_errors->prepareErrors(),
  705. 'type' => $post_errors->getErrorType() == 0 ? 'minor' : 'serious',
  706. 'title' => $txt['error_while_submitting'],
  707. );
  708. $context['attachment_error'] = array(
  709. 'errors' => $attach_errors->prepareErrors(),
  710. 'type' => $attach_errors->getErrorType() == 0 ? 'minor' : 'serious',
  711. 'title' => $txt['error_while_submitting'],
  712. );
  713. // What are you doing? Posting a poll, modifying, previewing, new post, or reply...
  714. if (isset($_REQUEST['poll']))
  715. $context['page_title'] = $txt['new_poll'];
  716. elseif ($context['make_event'])
  717. $context['page_title'] = $context['event']['id'] == -1 ? $txt['calendar_post_event'] : $txt['calendar_edit'];
  718. elseif (isset($_REQUEST['msg']))
  719. $context['page_title'] = $txt['modify_msg'];
  720. elseif (isset($_REQUEST['subject'], $context['preview_subject']))
  721. $context['page_title'] = $txt['preview'] . ' - ' . strip_tags($context['preview_subject']);
  722. elseif (empty($topic))
  723. $context['page_title'] = $txt['start_new_topic'];
  724. else
  725. $context['page_title'] = $txt['post_reply'];
  726. // Update the topic summary, needed to show new posts in a preview
  727. if (!empty($topic) && !empty($modSettings['topicSummaryPosts']))
  728. getTopic();
  729. // Just ajax previewing then lets stop now
  730. if (isset($_REQUEST['xml']))
  731. obExit();
  732. // Build the link tree.
  733. if (empty($topic))
  734. $context['linktree'][] = array(
  735. 'name' => '<em>' . $txt['start_new_topic'] . '</em>'
  736. );
  737. else
  738. $context['linktree'][] = array(
  739. 'url' => $scripturl . '?topic=' . $topic . '.' . $_REQUEST['start'],
  740. 'name' => $form_subject,
  741. 'extra_before' => '<span><strong class="nav">' . $context['page_title'] . ' ( </strong></span>',
  742. 'extra_after' => '<span><strong class="nav"> )</strong></span>'
  743. );
  744. $context['subject'] = addcslashes($form_subject, '"');
  745. $context['message'] = str_replace(array('"', '<', '>', '&nbsp;'), array('&quot;', '&lt;', '&gt;', ' '), $form_message);
  746. // Are post drafts enabled?
  747. $context['drafts_save'] = !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_post_enabled']) && allowedTo('post_draft');
  748. $context['drafts_autosave'] = !empty($context['drafts_save']) && !empty($modSettings['drafts_autosave_enabled']) && allowedTo('post_autosave_draft');
  749. // Build a list of drafts that they can load in to the editor
  750. if (!empty($context['drafts_save']))
  751. {
  752. require_once(CONTROLLERDIR . '/Drafts.controller.php');
  753. action_showDrafts($user_info['id'], $topic);
  754. }
  755. // Needed for the editor and message icons.
  756. require_once(SUBSDIR . '/Editor.subs.php');
  757. // Now create the editor.
  758. $editorOptions = array(
  759. 'id' => 'message',
  760. 'value' => $context['message'],
  761. 'labels' => array(
  762. 'post_button' => $context['submit_label'],
  763. ),
  764. // add height and width for the editor
  765. 'height' => '275px',
  766. 'width' => '100%',
  767. // We do XML preview here.
  768. 'preview_type' => 2,
  769. // Live errors - try or die
  770. 'live_errors' => true
  771. );
  772. create_control_richedit($editorOptions);
  773. // Store the ID.
  774. $context['post_box_name'] = $editorOptions['id'];
  775. $context['attached'] = '';
  776. $context['make_poll'] = isset($_REQUEST['poll']);
  777. if ($context['make_poll'])
  778. loadTemplate('Poll');
  779. // Message icons - customized or not, retrieve them...
  780. $context['icons'] = getMessageIcons($board);
  781. $context['icon_url'] = '';
  782. if (!empty($context['icons']))
  783. {
  784. $context['icons'][count($context['icons']) - 1]['is_last'] = true;
  785. $context['icons'][0]['selected'] = true;
  786. $context['icon'] = $context['icons'][0]['value'];
  787. $context['icon_url'] = $context['icons'][0]['url'];
  788. }
  789. // Are we starting a poll? if set the poll icon as selected if its available
  790. if (isset($_REQUEST['poll']))
  791. {
  792. for ($i = 0, $n = count($context['icons']); $i < $n; $i++)
  793. {
  794. if ($context['icons'][$i]['value'] == 'poll')
  795. {
  796. $context['icons'][$i]['selected'] = true;
  797. $context['icon'] = 'poll';
  798. $context['icon_url'] = $context['icons'][$i]['url'];
  799. break;
  800. }
  801. }
  802. }
  803. // If the user can post attachments prepare the warning labels.
  804. if ($context['can_post_attachment'])
  805. {
  806. // 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.
  807. $context['num_allowed_attachments'] = empty($modSettings['attachmentNumPerPostLimit']) ? 50 : min($modSettings['attachmentNumPerPostLimit'] - count($context['current_attachments']), $modSettings['attachmentNumPerPostLimit']);
  808. $context['can_post_attachment_unapproved'] = allowedTo('post_attachment');
  809. $context['attachment_restrictions'] = array();
  810. $context['allowed_extensions'] = strtr(strtolower($modSettings['attachmentExtensions']), array(',' => ', '));
  811. $attachmentRestrictionTypes = array('attachmentNumPerPostLimit', 'attachmentPostLimit', 'attachmentSizeLimit');
  812. foreach ($attachmentRestrictionTypes as $type)
  813. if (!empty($modSettings[$type]))
  814. {
  815. $context['attachment_restrictions'][] = sprintf($txt['attach_restrict_' . $type], comma_format($modSettings[$type], 0));
  816. // Show some numbers. If they exist.
  817. if ($type == 'attachmentNumPerPostLimit' && $context['attachments']['quantity'] > 0)
  818. $context['attachment_restrictions'][] = sprintf($txt['attach_remaining'], $modSettings['attachmentNumPerPostLimit'] - $context['attachments']['quantity']);
  819. elseif ($type == 'attachmentPostLimit' && $context['attachments']['total_size'] > 0)
  820. $context['attachment_restrictions'][] = sprintf($txt['attach_available'], comma_format(round(max($modSettings['attachmentPostLimit'] - ($context['attachments']['total_size'] / 1028), 0)), 0));
  821. }
  822. }
  823. $context['back_to_topic'] = isset($_REQUEST['goback']) || (isset($_REQUEST['msg']) && !isset($_REQUEST['subject']));
  824. $context['show_additional_options'] = !empty($_POST['additional_options']) || isset($_SESSION['temp_attachments']['post']) || isset($_GET['additionalOptions']);
  825. $context['is_new_topic'] = empty($topic);
  826. $context['is_new_post'] = !isset($_REQUEST['msg']);
  827. $context['is_first_post'] = $context['is_new_topic'] || (isset($_REQUEST['msg']) && $_REQUEST['msg'] == $id_first_msg);
  828. // WYSIWYG only works if BBC is enabled
  829. $modSettings['disable_wysiwyg'] = !empty($modSettings['disable_wysiwyg']) || empty($modSettings['enableBBC']);
  830. // Register this form in the session variables.
  831. checkSubmitOnce('register');
  832. // Finally, load the template.
  833. if (!isset($_REQUEST['xml']))
  834. loadTemplate('Post');
  835. }
  836. /**
  837. * Posts or saves the message composed with Post().
  838. *
  839. * requires various permissions depending on the action.
  840. * handles attachment, post, and calendar saving.
  841. * sends off notifications, and allows for announcements and moderation.
  842. * accessed from ?action=post2.
  843. */
  844. function action_post2()
  845. {
  846. global $board, $topic, $txt, $modSettings, $context;
  847. global $user_info, $board_info, $options, $smcFunc, $scripturl, $settings;
  848. // Sneaking off, are we?
  849. if (empty($_POST) && empty($topic))
  850. {
  851. if (empty($_SERVER['CONTENT_LENGTH']))
  852. redirectexit('action=post;board=' . $board . '.0');
  853. else
  854. fatal_lang_error('post_upload_error', false);
  855. }
  856. elseif (empty($_POST) && !empty($topic))
  857. redirectexit('action=post;topic=' . $topic . '.0');
  858. // No need!
  859. $context['robot_no_index'] = true;
  860. // Previewing? Go back to start.
  861. if (isset($_REQUEST['preview']))
  862. return action_post();
  863. // Prevent double submission of this form.
  864. checkSubmitOnce('check');
  865. // No errors as yet.
  866. $post_errors = error_context::context('post', 1);
  867. $attach_errors = attachment_error_context::context();
  868. // If the session has timed out, let the user re-submit their form.
  869. if (checkSession('post', '', false) != '')
  870. $post_errors->addError('session_timeout');
  871. // Wrong verification code?
  872. 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)))
  873. {
  874. require_once(SUBSDIR . '/Editor.subs.php');
  875. $verificationOptions = array(
  876. 'id' => 'post',
  877. );
  878. $context['require_verification'] = create_control_verification($verificationOptions, true);
  879. }
  880. require_once(SUBSDIR . '/Post.subs.php');
  881. loadLanguage('Post');
  882. // Drafts enabled and needed?
  883. if (!empty($modSettings['drafts_enabled']) && (isset($_POST['save_draft']) || isset($_POST['id_draft'])))
  884. require_once(CONTROLLERDIR . '/Drafts.controller.php');
  885. // First check to see if they are trying to delete any current attachments.
  886. if (isset($_POST['attach_del']))
  887. {
  888. $keep_temp = array();
  889. $keep_ids = array();
  890. foreach ($_POST['attach_del'] as $dummy)
  891. if (strpos($dummy, 'post_tmp_' . $user_info['id']) !== false)
  892. $keep_temp[] = $dummy;
  893. else
  894. $keep_ids[] = (int) $dummy;
  895. if (isset($_SESSION['temp_attachments']))
  896. foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
  897. {
  898. 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)
  899. continue;
  900. unset($_SESSION['temp_attachments'][$attachID]);
  901. unlink($attachment['tmp_name']);
  902. }
  903. if (!empty($_REQUEST['msg']))
  904. {
  905. require_once(SUBSDIR . '/Attachments.subs.php');
  906. $attachmentQuery = array(
  907. 'attachment_type' => 0,
  908. 'id_msg' => (int) $_REQUEST['msg'],
  909. 'not_id_attach' => $keep_ids,
  910. );
  911. removeAttachments($attachmentQuery);
  912. }
  913. }
  914. // Then try to upload any attachments.
  915. $context['can_post_attachment'] = !empty($modSettings['attachmentEnable']) && $modSettings['attachmentEnable'] == 1 && (allowedTo('post_attachment') || ($modSettings['postmod_active'] && allowedTo('post_unapproved_attachments')));
  916. if ($context['can_post_attachment'] && empty($_POST['from_qr']))
  917. {
  918. require_once(SUBSDIR . '/Attachments.subs.php');
  919. processAttachments();
  920. }
  921. // If this isn't a new topic load the topic info that we need.
  922. if (!empty($topic))
  923. {
  924. $request = $smcFunc['db_query']('', '
  925. SELECT locked, is_sticky, id_poll, approved, id_first_msg, id_last_msg, id_member_started, id_board
  926. FROM {db_prefix}topics
  927. WHERE id_topic = {int:current_topic}
  928. LIMIT 1',
  929. array(
  930. 'current_topic' => $topic,
  931. )
  932. );
  933. $topic_info = $smcFunc['db_fetch_assoc']($request);
  934. $smcFunc['db_free_result']($request);
  935. // Though the topic should be there, it might have vanished.
  936. if (!is_array($topic_info))
  937. fatal_lang_error('topic_doesnt_exist');
  938. // Did this topic suddenly move? Just checking...
  939. if ($topic_info['id_board'] != $board)
  940. fatal_lang_error('not_a_topic');
  941. }
  942. // Replying to a topic?
  943. if (!empty($topic) && !isset($_REQUEST['msg']))
  944. {
  945. // Don't allow a post if it's locked.
  946. if ($topic_info['locked'] != 0 && !allowedTo('moderate_board'))
  947. fatal_lang_error('topic_locked', false);
  948. // Sorry, multiple polls aren't allowed... yet. You should stop giving me ideas :P.
  949. if (isset($_REQUEST['poll']) && $topic_info['id_poll'] > 0)
  950. unset($_REQUEST['poll']);
  951. // Do the permissions and approval stuff...
  952. $becomesApproved = true;
  953. if ($topic_info['id_member_started'] != $user_info['id'])
  954. {
  955. if ($modSettings['postmod_active'] && allowedTo('post_unapproved_replies_any') && !allowedTo('post_reply_any'))
  956. $becomesApproved = false;
  957. else
  958. isAllowedTo('post_reply_any');
  959. }
  960. elseif (!allowedTo('post_reply_any'))
  961. {
  962. if ($modSettings['postmod_active'] && allowedTo('post_unapproved_replies_own') && !allowedTo('post_reply_own'))
  963. $becomesApproved = false;
  964. else
  965. isAllowedTo('post_reply_own');
  966. }
  967. if (isset($_POST['lock']))
  968. {
  969. // Nothing is changed to the lock.
  970. if ((empty($topic_info['locked']) && empty($_POST['lock'])) || (!empty($_POST['lock']) && !empty($topic_info['locked'])))
  971. unset($_POST['lock']);
  972. // You're have no permission to lock this topic.
  973. elseif (!allowedTo(array('lock_any', 'lock_own')) || (!allowedTo('lock_any') && $user_info['id'] != $topic_info['id_member_started']))
  974. unset($_POST['lock']);
  975. // You are allowed to (un)lock your own topic only.
  976. elseif (!allowedTo('lock_any'))
  977. {
  978. // You cannot override a moderator lock.
  979. if ($topic_info['locked'] == 1)
  980. unset($_POST['lock']);
  981. else
  982. $_POST['lock'] = empty($_POST['lock']) ? 0 : 2;
  983. }
  984. // Hail mighty moderator, (un)lock this topic immediately.
  985. else
  986. $_POST['lock'] = empty($_POST['lock']) ? 0 : 1;
  987. }
  988. // So you wanna (un)sticky this...let's see.
  989. if (isset($_POST['sticky']) && (empty($modSettings['enableStickyTopics']) || $_POST['sticky'] == $topic_info['is_sticky'] || !allowedTo('make_sticky')))
  990. unset($_POST['sticky']);
  991. // If drafts are enabled, then pass this off
  992. if (!empty($modSettings['drafts_enabled']) && isset($_POST['save_draft']))
  993. {
  994. saveDraft();
  995. return action_post();
  996. }
  997. // If the number of replies has changed, if the setting is enabled, go back to action_post() - which handles the error.
  998. if (empty($options['no_new_reply_warning']) && isset($_POST['last_msg']) && $topic_info['id_last_msg'] > $_POST['last_msg'])
  999. {
  1000. $_REQUEST['preview'] = true;
  1001. return action_post();
  1002. }
  1003. $posterIsGuest = $user_info['is_guest'];
  1004. }
  1005. // Posting a new topic.
  1006. elseif (empty($topic))
  1007. {
  1008. // Now don't be silly, new topics will get their own id_msg soon enough.
  1009. unset($_REQUEST['msg'], $_POST['msg'], $_GET['msg']);
  1010. // Do like, the permissions, for safety and stuff...
  1011. $becomesApproved = true;
  1012. if ($modSettings['postmod_active'] && !allowedTo('post_new') && allowedTo('post_unapproved_topics'))
  1013. $becomesApproved = false;
  1014. else
  1015. isAllowedTo('post_new');
  1016. if (isset($_POST['lock']))
  1017. {
  1018. // New topics are by default not locked.
  1019. if (empty($_POST['lock']))
  1020. unset($_POST['lock']);
  1021. // Besides, you need permission.
  1022. elseif (!allowedTo(array('lock_any', 'lock_own')))
  1023. unset($_POST['lock']);
  1024. // A moderator-lock (1) can override a user-lock (2).
  1025. else
  1026. $_POST['lock'] = allowedTo('lock_any') ? 1 : 2;
  1027. }
  1028. if (isset($_POST['sticky']) && (empty($modSettings['enableStickyTopics']) || empty($_POST['sticky']) || !allowedTo('make_sticky')))
  1029. unset($_POST['sticky']);
  1030. // Saving your new topic as a draft first?
  1031. if (!empty($modSettings['drafts_enabled']) && isset($_POST['save_draft']))
  1032. {
  1033. saveDraft();
  1034. return action_post();
  1035. }
  1036. $posterIsGuest = $user_info['is_guest'];
  1037. }
  1038. // Modifying an existing message?
  1039. elseif (isset($_REQUEST['msg']) && !empty($topic))
  1040. {
  1041. $_REQUEST['msg'] = (int) $_REQUEST['msg'];
  1042. $request = $smcFunc['db_query']('', '
  1043. SELECT id_member, poster_name, poster_email, poster_time, approved
  1044. FROM {db_prefix}messages
  1045. WHERE id_msg = {int:id_msg}
  1046. LIMIT 1',
  1047. array(
  1048. 'id_msg' => $_REQUEST['msg'],
  1049. )
  1050. );
  1051. if ($smcFunc['db_num_rows']($request) == 0)
  1052. fatal_lang_error('cant_find_messages', false);
  1053. $row = $smcFunc['db_fetch_assoc']($request);
  1054. $smcFunc['db_free_result']($request);
  1055. if (!empty($topic_info['locked']) && !allowedTo('moderate_board'))
  1056. fatal_lang_error('topic_locked', false);
  1057. if (isset($_POST['lock']))
  1058. {
  1059. // Nothing changes to the lock status.
  1060. if ((empty($_POST['lock']) && empty($topic_info['locked'])) || (!empty($_POST['lock']) && !empty($topic_info['locked'])))
  1061. unset($_POST['lock']);
  1062. // You're simply not allowed to (un)lock this.
  1063. elseif (!allowedTo(array('lock_any', 'lock_own')) || (!allowedTo('lock_any') && $user_info['id'] != $topic_info['id_member_started']))
  1064. unset($_POST['lock']);
  1065. // You're only allowed to lock your own topics.
  1066. elseif (!allowedTo('lock_any'))
  1067. {
  1068. // You're not allowed to break a moderator's lock.
  1069. if ($topic_info['locked'] == 1)
  1070. unset($_POST['lock']);
  1071. // Lock it with a soft lock or unlock it.
  1072. else
  1073. $_POST['lock'] = empty($_POST['lock']) ? 0 : 2;
  1074. }
  1075. // You must be the moderator.
  1076. else
  1077. $_POST['lock'] = empty($_POST['lock']) ? 0 : 1;
  1078. }
  1079. // Change the sticky status of this topic?
  1080. if (isset($_POST['sticky']) && (!allowedTo('make_sticky') || $_POST['sticky'] == $topic_info['is_sticky']))
  1081. unset($_POST['sticky']);
  1082. if ($row['id_member'] == $user_info['id'] && !allowedTo('modify_any'))
  1083. {
  1084. if ((!$modSettings['postmod_active'] || $row['approved']) && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + ($modSettings['edit_disable_time'] + 5) * 60 < time())
  1085. fatal_lang_error('modify_post_time_passed', false);
  1086. elseif ($topic_info['id_member_started'] == $user_info['id'] && !allowedTo('modify_own'))
  1087. isAllowedTo('modify_replies');
  1088. else
  1089. isAllowedTo('modify_own');
  1090. }
  1091. elseif ($topic_info['id_member_started'] == $user_info['id'] && !allowedTo('modify_any'))
  1092. {
  1093. isAllowedTo('modify_replies');
  1094. // If you're modifying a reply, I say it better be logged...
  1095. $moderationAction = true;
  1096. }
  1097. else
  1098. {
  1099. isAllowedTo('modify_any');
  1100. // Log it, assuming you're not modifying your own post.
  1101. if ($row['id_member'] != $user_info['id'])
  1102. $moderationAction = true;
  1103. }
  1104. // If drafts are enabled, then lets send this off to save
  1105. if (!empty($modSettings['drafts_enabled']) && isset($_POST['save_draft']))
  1106. {
  1107. saveDraft();
  1108. return action_post();
  1109. }
  1110. $posterIsGuest = empty($row['id_member']);
  1111. // Can they approve it?
  1112. $can_approve = allowedTo('approve_posts');
  1113. $becomesApproved = $modSettings['postmod_active'] ? ($can_approve && !$row['approved'] ? (!empty($_REQUEST['approve']) ? 1 : 0) : $row['approved']) : 1;
  1114. $approve_has_changed = $row['approved'] != $becomesApproved;
  1115. if (!allowedTo('moderate_forum') || !$posterIsGuest)
  1116. {
  1117. $_POST['guestname'] = $row['poster_name'];
  1118. $_POST['email'] = $row['poster_email'];
  1119. }
  1120. }
  1121. // Incase we want to override
  1122. if (allowedTo('approve_posts'))
  1123. {
  1124. $becomesApproved = !isset($_REQUEST['approve']) || !empty($_REQUEST['approve']) ? 1 : 0;
  1125. $approve_has_changed = isset($row['approved']) ? $row['approved'] != $becomesApproved : false;
  1126. }
  1127. // If the poster is a guest evaluate the legality of name and email.
  1128. if ($posterIsGuest)
  1129. {
  1130. $_POST['guestname'] = !isset($_POST['guestname']) ? '' : trim($_POST['guestname']);
  1131. $_POST['email'] = !isset($_POST['email']) ? '' : trim($_POST['email']);
  1132. if ($_POST['guestname'] == '' || $_POST['guestname'] == '_')
  1133. $post_errors->addError('no_name');
  1134. if ($smcFunc['strlen']($_POST['guestname']) > 25)
  1135. $post_errors->addError('long_name');
  1136. if (empty($modSettings['guest_post_no_email']))
  1137. {
  1138. // Only check if they changed it!
  1139. if (!isset($row) || $row['poster_email'] != $_POST['email'])
  1140. {
  1141. if (!allowedTo('moderate_forum') && (!isset($_POST['email']) || $_POST['email'] == ''))
  1142. $post_errors->addError('no_email');
  1143. if (!allowedTo('moderate_forum') && preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $_POST['email']) == 0)
  1144. $post_errors->addError('bad_email');
  1145. }
  1146. // Now make sure this email address is not banned from posting.
  1147. isBannedEmail($_POST['email'], 'cannot_post', sprintf($txt['you_are_post_banned'], $txt['guest_title']));
  1148. }
  1149. // In case they are making multiple posts this visit, help them along by storing their name.
  1150. if (!$post_errors->hasErrors())
  1151. {
  1152. $_SESSION['guest_name'] = $_POST['guestname'];
  1153. $_SESSION['guest_email'] = $_POST['email'];
  1154. }
  1155. }
  1156. // Check the subject and message.
  1157. if (!isset($_POST['subject']) || $smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['subject'])) === '')
  1158. $post_errors->addError('no_subject', 0);
  1159. if (!isset($_POST['message']) || $smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['message']), ENT_QUOTES) === '')
  1160. $post_errors->addError('no_message');
  1161. elseif (!empty($modSettings['max_messageLength']) && $smcFunc['strlen']($_POST['message']) > $modSettings['max_messageLength'])
  1162. $post_errors->addError(array('long_message', array($modSettings['max_messageLength'])));
  1163. else
  1164. {
  1165. // Prepare the message a bit for some additional testing.
  1166. $_POST['message'] = $smcFunc['htmlspecialchars']($_POST['message'], ENT_QUOTES);
  1167. // Preparse code. (Zef)
  1168. if ($user_info['is_guest'])
  1169. $user_info['name'] = $_POST['guestname'];
  1170. preparsecode($_POST['message']);
  1171. // Let's see if there's still some content left without the tags.
  1172. if ($smcFunc['htmltrim'](strip_tags(parse_bbc($_POST['message'], false), '<img>')) === '' && (!allowedTo('admin_forum') || strpos($_POST['message'], '[html]') === false))
  1173. $post_errors->addError('no_message');
  1174. }
  1175. if (isset($_POST['calendar']) && !isset($_REQUEST['deleteevent']) && $smcFunc['htmltrim']($_POST['evtitle']) === '')
  1176. $post_errors->addError('no_event');
  1177. // Validate the poll...
  1178. if (isset($_REQUEST['poll']) && $modSettings['pollMode'] == '1')
  1179. {
  1180. if (!empty($topic) && !isset($_REQUEST['msg']))
  1181. fatal_lang_error('no_access', false);
  1182. // This is a new topic... so it's a new poll.
  1183. if (empty($topic))
  1184. isAllowedTo('poll_post');
  1185. // Can you add to your own topics?
  1186. elseif ($user_info['id'] == $topic_info['id_member_started'] && !allowedTo('poll_add_any'))
  1187. isAllowedTo('poll_add_own');
  1188. // Can you add polls to any topic, then?
  1189. else
  1190. isAllowedTo('poll_add_any');
  1191. if (!isset($_POST['question']) || trim($_POST['question']) == '')
  1192. $post_errors->addError('no_question');
  1193. $_POST['options'] = empty($_POST['options']) ? array() : htmltrim__recursive($_POST['options']);
  1194. // Get rid of empty ones.
  1195. foreach ($_POST['options'] as $k => $option)
  1196. if ($option == '')
  1197. unset($_POST['options'][$k], $_POST['options'][$k]);
  1198. // What are you going to vote between with one choice?!?
  1199. if (count($_POST['options']) < 2)
  1200. $post_errors->addError('poll_few');
  1201. elseif (count($_POST['options']) > 256)
  1202. $post_errors->addError('poll_many');
  1203. }
  1204. if ($posterIsGuest)
  1205. {
  1206. // If user is a guest, make sure the chosen name isn't taken.
  1207. require_once(SUBSDIR . '/Members.subs.php');
  1208. if (isReservedName($_POST['guestname'], 0, true, false) && (!isset($row['poster_name']) || $_POST['guestname'] != $row['poster_name']))
  1209. $post_errors->addError('bad_name');
  1210. }
  1211. // If the user isn't a guest, get his or her name and email.
  1212. elseif (!isset($_REQUEST['msg']))
  1213. {
  1214. $_POST['guestname'] = $user_info['username'];
  1215. $_POST['email'] = $user_info['email'];
  1216. }
  1217. // Any mistakes?
  1218. if ($post_errors->hasErrors())
  1219. {
  1220. // Previewing.
  1221. $_REQUEST['preview'] = true;
  1222. return action_post();
  1223. }
  1224. // Make sure the user isn't spamming the board.
  1225. if (!isset($_REQUEST['msg']))
  1226. spamProtection('post');
  1227. // At about this point, we're posting and that's that.
  1228. ignore_user_abort(true);
  1229. @set_time_limit(300);
  1230. // Add special html entities to the subject, name, and email.
  1231. $_POST['subject'] = strtr($smcFunc['htmlspecialchars']($_POST['subject']), array("\r" => '', "\n" => '', "\t" => ''));
  1232. $_POST['guestname'] = htmlspecialchars($_POST['guestname']);
  1233. $_POST['email'] = htmlspecialchars($_POST['email']);
  1234. // At this point, we want to make sure the subject isn't too long.
  1235. if ($smcFunc['strlen']($_POST['subject']) > 100)
  1236. $_POST['subject'] = $smcFunc['substr']($_POST['subject'], 0, 100);
  1237. // Make the poll...
  1238. if (isset($_REQUEST['poll']))
  1239. {
  1240. // Make sure that the user has not entered a ridiculous number of options..
  1241. if (empty($_POST['poll_max_votes']) || $_POST['poll_max_votes'] <= 0)
  1242. $_POST['poll_max_votes'] = 1;
  1243. elseif ($_POST['poll_max_votes'] > count($_POST['options']))
  1244. $_POST['poll_max_votes'] = count($_POST['options']);
  1245. else
  1246. $_POST['poll_max_votes'] = (int) $_POST['poll_max_votes'];
  1247. $_POST['poll_expire'] = (int) $_POST['poll_expire'];
  1248. $_POST['poll_expire'] = $_POST['poll_expire'] > 9999 ? 9999 : ($_POST['poll_expire'] < 0 ? 0 : $_POST['poll_expire']);
  1249. // Just set it to zero if it's not there..
  1250. if (!isset($_POST['poll_hide']))
  1251. $_POST['poll_hide'] = 0;
  1252. else
  1253. $_POST['poll_hide'] = (int) $_POST['poll_hide'];
  1254. $_POST['poll_change_vote'] = isset($_POST['poll_change_vote']) ? 1 : 0;
  1255. $_POST['poll_guest_vote'] = isset($_POST['poll_guest_vote']) ? 1 : 0;
  1256. // Make sure guests are actually allowed to vote generally.
  1257. if ($_POST['poll_guest_vote'])
  1258. {
  1259. require_once(SUBSDIR . '/Members.subs.php');
  1260. $allowedVoteGroups = groupsAllowedTo('poll_vote', $board);
  1261. if (!in_array(-1, $allowedVoteGroups['allowed']))
  1262. $_POST['poll_guest_vote'] = 0;
  1263. }
  1264. // If the user tries to set the poll too far in advance, don't let them.
  1265. if (!empty($_POST['poll_expire']) && $_POST['poll_expire'] < 1)
  1266. fatal_lang_error('poll_range_error', false);
  1267. // Don't allow them to select option 2 for hidden results if it's not time limited.
  1268. elseif (empty($_POST['poll_expire']) && $_POST['poll_hide'] == 2)
  1269. $_POST['poll_hide'] = 1;
  1270. // Clean up the question and answers.
  1271. $_POST['question'] = htmlspecialchars($_POST['question']);
  1272. $_POST['question'] = $smcFunc['truncate']($_POST['question'], 255);
  1273. $_POST['question'] = preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $_POST['question']);
  1274. $_POST['options'] = htmlspecialchars__recursive($_POST['options']);
  1275. }
  1276. // ...or attach a new file...
  1277. if (empty($ignore_temp) && $context['can_post_attachment'] && !empty($_SESSION['temp_attachments']) && empty($_POST['from_qr']))
  1278. {
  1279. $attachIDs = array();
  1280. if (!empty($context['we_are_history']))
  1281. $attach_errors->addError('temp_attachments_flushed');
  1282. foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
  1283. {
  1284. if ($attachID != 'initial_error' && strpos($attachID, 'post_tmp_' . $user_info['id']) === false)
  1285. continue;
  1286. // If there was an initial error just show that message.
  1287. if ($attachID == 'initial_error')
  1288. {
  1289. $attach_errors->addError($txt['attach_no_upload']);
  1290. $attach_errors->addError(is_array($attachment) ? vsprintf($txt[$attachment[0]], $attachment[1]) : $txt[$attachment]);
  1291. unset($_SESSION['temp_attachments']);
  1292. break;
  1293. }
  1294. // No errors, then try to create the attachment
  1295. if (empty($attachment['errors']))
  1296. {
  1297. // Load the attachmentOptions array with the data needed to create an attachment
  1298. $attachmentOptions = array(
  1299. 'post' => isset($_REQUEST['msg']) ? $_REQUEST['msg'] : 0,
  1300. 'poster' => $user_info['id'],
  1301. 'name' => $attachment['name'],
  1302. 'tmp_name' => $attachment['tmp_name'],
  1303. 'size' => isset($attachment['size']) ? $attachment['size'] : 0,
  1304. 'mime_type' => isset($attachment['type']) ? $attachment['type'] : '',
  1305. 'id_folder' => isset($attachment['id_folder']) ? $attachment['id_folder'] : 0,
  1306. 'approved' => !$modSettings['postmod_active'] || allowedTo('post_attachment'),
  1307. 'errors' => $attachment['errors'],
  1308. );
  1309. if (createAttachment($attachmentOptions))
  1310. {
  1311. $attachIDs[] = $attachmentOptions['id'];
  1312. if (!empty($attachmentOptions['thumb']))
  1313. $attachIDs[] = $attachmentOptions['thumb'];
  1314. }
  1315. }
  1316. // We have errors on this file, build out the issues for display to the user
  1317. else
  1318. {
  1319. // Sort out the errors for display and delete any associated files.
  1320. $attach_errors->addAttach($attachID, $attachment['name']);
  1321. $log_these = array('attachments_no_create', 'attachments_no_write', 'attach_timeout', 'ran_out_of_space', 'cant_access_upload_path', 'attach_0_byte_file');
  1322. foreach ($attachment['errors'] as $error)
  1323. {
  1324. if (!is_array($error))
  1325. {
  1326. $attach_errors->addError($txt[$error], $attachID);
  1327. if (in_array($error, $log_these))
  1328. log_error($attachment['name'] . ': ' . $txt[$error], 'critical');
  1329. }
  1330. else
  1331. $attach_errors->addError(vsprintf($txt[$error[0]], $error[1]), $attachID);
  1332. }
  1333. if (file_exists($attachment['tmp_name']))
  1334. unlink($attachment['tmp_name']);
  1335. }
  1336. }
  1337. unset($_SESSION['temp_attachments']);
  1338. }
  1339. // Make the poll...
  1340. if (isset($_REQUEST['poll']))
  1341. {
  1342. // Create the poll.
  1343. $smcFunc['db_insert']('',
  1344. '{db_prefix}polls',
  1345. array(
  1346. 'question' => 'string-255', 'hide_results' => 'int', 'max_votes' => 'int', 'expire_time' => 'int', 'id_member' => 'int',
  1347. 'poster_name' => 'string-255', 'change_vote' => 'int', 'guest_vote' => 'int'
  1348. ),
  1349. array(
  1350. $_POST['question'], $_POST['poll_hide'], $_POST['poll_max_votes'], (empty($_POST['poll_expire']) ? 0 : time() + $_POST['poll_expire'] * 3600 * 24), $user_info['id'],
  1351. $_POST['guestname'], $_POST['poll_change_vote'], $_POST['poll_guest_vote'],
  1352. ),
  1353. array('id_poll')
  1354. );
  1355. $id_poll = $smcFunc['db_insert_id']('{db_prefix}polls', 'id_poll');
  1356. // Create each answer choice.
  1357. $i = 0;
  1358. $pollOptions = array();
  1359. foreach ($_POST['options'] as $option)
  1360. {
  1361. $pollOptions[] = array($id_poll, $i, $option);
  1362. $i++;
  1363. }
  1364. $smcFunc['db_insert']('insert',
  1365. '{db_prefix}poll_choices',
  1366. array('id_poll' => 'int', 'id_choice' => 'int', 'label' => 'string-255'),
  1367. $pollOptions,
  1368. array('id_poll', 'id_choice')
  1369. );
  1370. call_integration_hook('integrate_poll_add_edit', array($id_poll, false));
  1371. }
  1372. else
  1373. $id_poll = 0;
  1374. // Creating a new topic?
  1375. $newTopic = empty($_REQUEST['msg']) && empty($topic);
  1376. $_POST['icon'] = !empty($attachIDs) && $_POST['icon'] == 'xx' ? 'clip' : $_POST['icon'];
  1377. // Collect all parameters for the creation or modification of a post.
  1378. $msgOptions = array(
  1379. 'id' => empty($_REQUEST['msg']) ? 0 : (int) $_REQUEST['msg'],
  1380. 'subject' => $_POST['subject'],
  1381. 'body' => $_POST['message'],
  1382. 'icon' => preg_replace('~[\./\\\\*:"\'<>]~', '', $_POST['icon']),
  1383. 'smileys_enabled' => !isset($_POST['ns']),
  1384. 'attachments' => empty($attachIDs) ? array() : $attachIDs,
  1385. 'approved' => $becomesApproved,
  1386. );
  1387. $topicOptions = array(
  1388. 'id' => empty($topic) ? 0 : $topic,
  1389. 'board' => $board,
  1390. 'poll' => isset($_REQUEST['poll']) ? $id_poll : null,
  1391. 'lock_mode' => isset($_POST['lock']) ? (int) $_POST['lock'] : null,
  1392. 'sticky_mode' => isset($_POST['sticky']) && !empty($modSettings['enableStickyTopics']) ? (int) $_POST['sticky'] : null,
  1393. 'mark_as_read' => true,
  1394. 'is_approved' => !$modSettings['postmod_active'] || empty($topic) || !empty($board_info['cur_topic_approved']),
  1395. );
  1396. $posterOptions = array(
  1397. 'id' => $user_info['id'],
  1398. 'name' => $_POST['guestname'],
  1399. 'email' => $_POST['email'],
  1400. 'update_post_count' => !$user_info['is_guest'] && !isset($_REQUEST['msg']) && $board_info['posts_count'],
  1401. );
  1402. // This is an already existing message. Edit it.
  1403. if (!empty($_REQUEST['msg']))
  1404. {
  1405. // Have admins allowed people to hide their screwups?
  1406. if (time() - $row['poster_time'] > $modSettings['edit_wait_time'] || $user_info['id'] != $row['id_member'])
  1407. {
  1408. $msgOptions['modify_time'] = time();
  1409. $msgOptions['modify_name'] = $user_info['name'];
  1410. }
  1411. // This will save some time...
  1412. if (empty($approve_has_changed))
  1413. unset($msgOptions['approved']);
  1414. modifyPost($msgOptions, $topicOptions, $posterOptions);
  1415. }
  1416. // This is a new topic or an already existing one. Save it.
  1417. else
  1418. {
  1419. createPost($msgOptions, $topicOptions, $posterOptions);
  1420. if (isset($topicOptions['id']))
  1421. $topic = $topicOptions['id'];
  1422. }
  1423. // If we had a draft for this, its time to remove it since it was just posted
  1424. if (!empty($modSettings['drafts_enabled']) && !empty($_POST['id_draft']))
  1425. deleteDrafts($_POST['id_draft'], $user_info['id']);
  1426. // Editing or posting an event?
  1427. if (isset($_POST['calendar']) && (!isset($_REQUEST['eventid']) || $_REQUEST['eventid'] == -1))
  1428. {
  1429. require_once(SUBSDIR . '/Calendar.subs.php');
  1430. // Make sure they can link an event to this post.
  1431. canLinkEvent();
  1432. // Insert the event.
  1433. $eventOptions = array(
  1434. 'board' => $board,
  1435. 'topic' => $topic,
  1436. 'title' => $_POST['evtitle'],
  1437. 'member' => $user_info['id'],
  1438. 'start_date' => sprintf('%04d-%02d-%02d', $_POST['year'], $_POST['month'], $_POST['day']),
  1439. 'span' => isset($_POST['span']) && $_POST['span'] > 0 ? min((int) $modSettings['cal_maxspan'], (int) $_POST['span'] - 1) : 0,
  1440. );
  1441. insertEvent($eventOptions);
  1442. }
  1443. elseif (isset($_POST['calendar']))
  1444. {
  1445. $_REQUEST['eventid'] = (int) $_REQUEST['eventid'];
  1446. // Validate the post...
  1447. require_once(SUBSDIR . '/Calendar.subs.php');
  1448. validateEventPost();
  1449. // If you're not allowed to edit any events, you have to be the poster.
  1450. if (!allowedTo('calendar_edit_any'))
  1451. {
  1452. // Get the event's poster.
  1453. $request = $smcFunc['db_query']('', '
  1454. SELECT id_member
  1455. FROM {db_prefix}calendar
  1456. WHERE id_event = {int:id_event}',
  1457. array(
  1458. 'id_event' => $_REQUEST['eventid'],
  1459. )
  1460. );
  1461. $row2 = $smcFunc['db_fetch_assoc']($request);
  1462. $smcFunc['db_free_result']($request);
  1463. // Silly hacker, Trix are for kids. ...probably trademarked somewhere, this is FAIR USE! (parody...)
  1464. isAllowedTo('calendar_edit_' . ($row2['id_member'] == $user_info['id'] ? 'own' : 'any'));
  1465. }
  1466. // Delete it?
  1467. if (isset($_REQUEST['deleteevent']))
  1468. $smcFunc['db_query']('', '
  1469. DELETE FROM {db_prefix}calendar
  1470. WHERE id_event = {int:id_event}',
  1471. array(
  1472. 'id_event' => $_REQUEST['eventid'],
  1473. )
  1474. );
  1475. // ... or just update it?
  1476. else
  1477. {
  1478. $span = !empty($modSettings['cal_allowspan']) && !empty($_REQUEST['span']) ? min((int) $modSettings['cal_maxspan'], (int) $_REQUEST['span'] - 1) : 0;
  1479. $start_time = mktime(0, 0, 0, (int) $_REQUEST['month'], (int) $_REQUEST['day'], (int) $_REQUEST['year']);
  1480. $smcFunc['db_query']('', '
  1481. UPDATE {db_prefix}calendar
  1482. SET end_date = {date:end_date},
  1483. start_date = {date:start_date},
  1484. title = {string:title}
  1485. WHERE id_event = {int:id_event}',
  1486. array(
  1487. 'end_date' => strftime('%Y-%m-%d', $start_time + $span * 86400),
  1488. 'start_date' => strftime('%Y-%m-%d', $start_time),
  1489. 'id_event' => $_REQUEST['eventid'],
  1490. 'title' => $smcFunc['htmlspecialchars']($_REQUEST['evtitle'], ENT_QUOTES),
  1491. )
  1492. );
  1493. }
  1494. updateSettings(array(
  1495. 'calendar_updated' => time(),
  1496. ));
  1497. }
  1498. // Marking read should be done even for editing messages....
  1499. // Mark all the parents read. (since you just posted and they will be unread.)
  1500. if (!$user_info['is_guest'] && !empty($board_info['parent_boards']))
  1501. {
  1502. $smcFunc['db_query']('', '
  1503. UPDATE {db_prefix}log_boards
  1504. SET id_msg = {int:id_msg}
  1505. WHERE id_member = {int:current_member}
  1506. AND id_board IN ({array_int:board_list})',
  1507. array(
  1508. 'current_member' => $user_info['id'],
  1509. 'board_list' => array_keys($board_info['parent_boards']),
  1510. 'id_msg' => $modSettings['maxMsgID'],
  1511. )
  1512. );
  1513. }
  1514. // Turn notification on or off. (note this just blows smoke if it's already on or off.)
  1515. if (!empty($_POST['notify']) && allowedTo('mark_any_notify'))
  1516. {
  1517. $smcFunc['db_insert']('ignore',
  1518. '{db_prefix}log_notify',
  1519. array('id_member' => 'int', 'id_topic' => 'int', 'id_board' => 'int'),
  1520. array($user_info['id'], $topic, 0),
  1521. array('id_member', 'id_topic', 'id_board')
  1522. );
  1523. }
  1524. elseif (!$newTopic)
  1525. $smcFunc['db_query']('', '
  1526. DELETE FROM {db_prefix}log_notify
  1527. WHERE id_member = {int:current_member}
  1528. AND id_topic = {int:current_topic}',
  1529. array(
  1530. 'current_member' => $user_info['id'],
  1531. 'current_topic' => $topic,
  1532. )
  1533. );
  1534. // Log an act of moderation - modifying.
  1535. if (!empty($moderationAction))
  1536. logAction('modify', array('topic' => $topic, 'message' => (int) $_REQUEST['msg'], 'member' => $row['id_member'], 'board' => $board));
  1537. if (isset($_POST['lock']) && $_POST['lock'] != 2)
  1538. logAction(empty($_POST['lock']) ? 'unlock' : 'lock', array('topic' => $topicOptions['id'], 'board' => $topicOptions['board']));
  1539. if (isset($_POST['sticky']) && !empty($modSettings['enableStickyTopics']))
  1540. logAction('sticky', array('topic' => $topicOptions['id'], 'board' => $topicOptions['board']));
  1541. // Notify any members who have notification turned on for this topic - only do this if it's going to be approved(!)
  1542. if ($becomesApproved)
  1543. {
  1544. if ($newTopic)
  1545. {
  1546. $notifyData = array(
  1547. 'body' => $_POST['message'],
  1548. 'subject' => $_POST['subject'],
  1549. 'name' => $user_info['name'],
  1550. 'poster' => $user_info['id'],
  1551. 'msg' => $msgOptions['id'],
  1552. 'board' => $board,
  1553. 'topic' => $topic,
  1554. );
  1555. notifyMembersBoard($notifyData);
  1556. }
  1557. elseif (empty($_REQUEST['msg']))
  1558. {
  1559. // Only send it to everyone if the topic is approved, otherwise just to the topic starter if they want it.
  1560. if ($topic_info['approved'])
  1561. sendNotifications($topic, 'reply');
  1562. else
  1563. sendNotifications($topic, 'reply', array(), $topic_info['id_member_started']);
  1564. }
  1565. }
  1566. // Returning to the topic?
  1567. if (!empty($_REQUEST['goback']))
  1568. {
  1569. // Mark the board as read.... because it might get confusing otherwise.
  1570. $smcFunc['db_query']('', '
  1571. UPDATE {db_prefix}log_boards
  1572. SET id_msg = {int:maxMsgID}
  1573. WHERE id_member = {int:current_member}
  1574. AND id_board = {int:current_board}',
  1575. array(
  1576. 'current_board' => $board,
  1577. 'current_member' => $user_info['id'],
  1578. 'maxMsgID' => $modSettings['maxMsgID'],
  1579. )
  1580. );
  1581. }
  1582. if ($board_info['num_topics'] == 0)
  1583. cache_put_data('board-' . $board, null, 120);
  1584. if (!empty($_POST['announce_topic']))
  1585. redirectexit('action=announce;sa=selectgroup;topic=' . $topic . (!empty($_POST['move']) && allowedTo('move_any') ? ';move' : '') . (empty($_REQUEST['goback']) ? '' : ';goback'));
  1586. if (!empty($_POST['move']) && allowedTo('move_any'))
  1587. redirectexit('action=movetopic;topic=' . $topic . '.0' . (empty($_REQUEST['goback']) ? '' : ';goback'));
  1588. // If there are attachment errors. Let's show a list to the user.
  1589. if ($attach_errors->hasErrors())
  1590. {
  1591. loadTemplate('Errors');
  1592. $context['sub_template'] = 'attachment_errors';
  1593. $context['page_title'] = $txt['error_occured'];
  1594. $errors = $attach_errors->prepareErrors();
  1595. foreach ($errors as $key => $error)
  1596. {
  1597. $context['attachment_error_keys'][] = $key . '_error';
  1598. $context[$key . '_error'] = $error;
  1599. }
  1600. $context['linktree'][] = array(
  1601. 'url' => $scripturl . '?topic=' . $topic . '.0',
  1602. 'name' => $_POST['subject'],
  1603. 'extra_before' => !empty($settings['linktree_inline']) ? $txt['topic'] . ': ' : ''
  1604. );
  1605. if (isset($_REQUEST['msg']))
  1606. $context['redirect_link'] = $scripturl . '?topic=' . $topic . '.msg' . $_REQUEST['msg'] . '#msg' . $_REQUEST['msg'];
  1607. else
  1608. $context['redirect_link'] = $scripturl . '?topic=' . $topic . '.new#new';
  1609. $context['back_link'] = $scripturl . '?action=post;msg=' . $msgOptions['id'] . ';topic=' . $topic . ';additionalOptions#postAttachment';
  1610. obExit(null, true);
  1611. }
  1612. // Return to post if the mod is on.
  1613. if (isset($_REQUEST['msg']) && !empty($_REQUEST['goback']))
  1614. redirectexit('topic=' . $topic . '.msg' . $_REQUEST['msg'] . '#msg' . $_REQUEST['msg'], isBrowser('ie'));
  1615. elseif (!empty($_REQUEST['goback']))
  1616. redirectexit('topic=' . $topic . '.new#new', isBrowser('ie'));
  1617. // Dut-dut-duh-duh-DUH-duh-dut-duh-duh! *dances to the Final Fantasy Fanfare...*
  1618. else
  1619. redirectexit('board=' . $board . '.0');
  1620. }
  1621. /**
  1622. * Get the topic for display purposes.
  1623. *
  1624. * gets a summary of the most recent posts in a topic.
  1625. * depends on the topicSummaryPosts setting.
  1626. * if you are editing a post, only shows posts previous to that post.
  1627. */
  1628. function getTopic()
  1629. {
  1630. global $topic, $modSettings, $context, $smcFunc, $counter, $options;
  1631. if (isset($_REQUEST['xml']))
  1632. $limit = '
  1633. LIMIT ' . (empty($context['new_replies']) ? '0' : $context['new_replies']);
  1634. else
  1635. $limit = empty($modSettings['topicSummaryPosts']) ? '' : '
  1636. LIMIT ' . (int) $modSettings['topicSummaryPosts'];
  1637. // If you're modifying, get only those posts before the current one. (otherwise get all.)
  1638. $request = $smcFunc['db_query']('', '
  1639. SELECT
  1640. IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
  1641. m.body, m.smileys_enabled, m.id_msg, m.id_member
  1642. FROM {db_prefix}messages AS m
  1643. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  1644. WHERE m.id_topic = {int:current_topic}' . (isset($_REQUEST['msg']) ? '
  1645. AND m.id_msg < {int:id_msg}' : '') .(!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  1646. AND m.approved = {int:approved}') . '
  1647. ORDER BY m.id_msg DESC' . $limit,
  1648. array(
  1649. 'current_topic' => $topic,
  1650. 'id_msg' => isset($_REQUEST['msg']) ? (int) $_REQUEST['msg'] : 0,
  1651. 'approved' => 1,
  1652. )
  1653. );
  1654. $context['previous_posts'] = array();
  1655. while ($row = $smcFunc['db_fetch_assoc']($request))
  1656. {
  1657. // Censor, BBC, ...
  1658. censorText($row['body']);
  1659. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  1660. // ...and store.
  1661. $context['previous_posts'][] = array(
  1662. 'counter' => $counter++,
  1663. 'alternate' => $counter % 2,
  1664. 'poster' => $row['poster_name'],
  1665. 'message' => $row['body'],
  1666. 'time' => timeformat($row['poster_time']),
  1667. 'timestamp' => forum_time(true, $row['poster_time']),
  1668. 'id' => $row['id_msg'],
  1669. 'is_new' => !empty($context['new_replies']),
  1670. 'is_ignored' => !empty($modSettings['enable_buddylist']) && !empty($options['posts_apply_ignore_list']) && in_array($row['id_member'], $context['user']['ignoreusers']),
  1671. );
  1672. if (!empty($context['new_replies']))
  1673. $context['new_replies']--;
  1674. }
  1675. $smcFunc['db_free_result']($request);
  1676. }
  1677. /**
  1678. * Loads a post an inserts it into the current editing text box.
  1679. * uses the Post language file.
  1680. * uses special (sadly browser dependent) javascript to parse entities for internationalization reasons.
  1681. * accessed with ?action=quotefast.
  1682. */
  1683. function action_quotefast()
  1684. {
  1685. global $modSettings, $user_info, $txt, $settings, $context;
  1686. global $smcFunc;
  1687. loadLanguage('Post');
  1688. if (!isset($_REQUEST['xml']))
  1689. {
  1690. loadTemplate('Post');
  1691. loadJavascriptFile('post.js', array(), 'post_scripts');
  1692. }
  1693. include_once(SUBSDIR . '/Post.subs.php');
  1694. $moderate_boards = boardsAllowedTo('moderate_board');
  1695. // Where we going if we need to?
  1696. $context['post_box_name'] = isset($_GET['pb']) ? $_GET['pb'] : '';
  1697. $request = $smcFunc['db_query']('', '
  1698. SELECT IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.body, m.id_topic, m.subject,
  1699. m.id_board, m.id_member, m.approved
  1700. FROM {db_prefix}messages AS m
  1701. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  1702. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND {query_see_board})
  1703. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  1704. WHERE m.id_msg = {int:id_msg}' . (isset($_REQUEST['modify']) || (!empty($moderate_boards) && $moderate_boards[0] == 0) ? '' : '
  1705. AND (t.locked = {int:not_locked}' . (empty($moderate_boards) ? '' : ' OR b.id_board IN ({array_int:moderation_board_list})') . ')') . '
  1706. LIMIT 1',
  1707. array(
  1708. 'current_member' => $user_info['id'],
  1709. 'moderation_board_list' => $moderate_boards,
  1710. 'id_msg' => (int) $_REQUEST['quote'],
  1711. 'not_locked' => 0,
  1712. )
  1713. );
  1714. $context['close_window'] = $smcFunc['db_num_rows']($request) == 0;
  1715. $row = $smcFunc['db_fetch_assoc']($request);
  1716. $smcFunc['db_free_result']($request);
  1717. $context['sub_template'] = 'quotefast';
  1718. if (!empty($row))
  1719. $can_view_post = $row['approved'] || ($row['id_member'] != 0 && $row['id_member'] == $user_info['id']) || allowedTo('approve_posts', $row['id_board']);
  1720. if (!empty($can_view_post))
  1721. {
  1722. // Remove special formatting we don't want anymore.
  1723. $row['body'] = un_preparsecode($row['body']);
  1724. // Censor the message!
  1725. censorText($row['body']);
  1726. $row['body'] = preg_replace('~<br ?/?' . '>~i', "\n", $row['body']);
  1727. // Want to modify a single message by double clicking it?
  1728. if (isset($_REQUEST['modify']))
  1729. {
  1730. censorText($row['subject']);
  1731. $context['sub_template'] = 'modifyfast';
  1732. $context['message'] = array(
  1733. 'id' => $_REQUEST['quote'],
  1734. 'body' => $row['body'],
  1735. 'subject' => addcslashes($row['subject'], '"'),
  1736. );
  1737. return;
  1738. }
  1739. // Remove any nested quotes.
  1740. if (!empty($modSettings['removeNestedQuotes']))
  1741. $row['body'] = preg_replace(array('~\n?\[quote.*?\].+?\[/quote\]\n?~is', '~^\n~', '~\[/quote\]~'), '', $row['body']);
  1742. $lb = "\n";
  1743. // Add a quote string on the front and end.
  1744. $context['quote']['xml'] = '[quote author=' . $row['poster_name'] . ' link=topic=' . $row['id_topic'] . '.msg' . (int) $_REQUEST['quote'] . '#msg' . (int) $_REQUEST['quote'] . ' date=' . $row['poster_time'] . ']' . $lb . $row['body'] . $lb . '[/quote]';
  1745. $context['quote']['text'] = strtr(un_htmlspecialchars($context['quote']['xml']), array('\'' => '\\\'', '\\' => '\\\\', "\n" => '\\n', '</script>' => '</\' + \'script>'));
  1746. $context['quote']['xml'] = strtr($context['quote']['xml'], array('&nbsp;' => '&#160;', '<' => '&lt;', '>' => '&gt;'));
  1747. $context['quote']['mozilla'] = strtr($smcFunc['htmlspecialchars']($context['quote']['text']), array('&quot;' => '"'));
  1748. }
  1749. //@todo Needs a nicer interface.
  1750. // In case our message has been removed in the meantime.
  1751. elseif (isset($_REQUEST['modify']))
  1752. {
  1753. $context['sub_template'] = 'modifyfast';
  1754. $context['message'] = array(
  1755. 'id' => 0,
  1756. 'body' => '',
  1757. 'subject' => '',
  1758. );
  1759. }
  1760. else
  1761. $context['quote'] = array(
  1762. 'xml' => '',
  1763. 'mozilla' => '',
  1764. 'text' => '',
  1765. );
  1766. }
  1767. /**
  1768. * Used to edit the body or subject of a message inline
  1769. * called from action=jsmodify from script and topic js
  1770. */
  1771. function action_jsmodify()
  1772. {
  1773. global $modSettings, $board, $topic, $txt;
  1774. global $user_info, $context, $smcFunc, $language;
  1775. // We have to have a topic!
  1776. if (empty($topic))
  1777. obExit(false);
  1778. checkSession('get');
  1779. require_once(SUBSDIR . '/Post.subs.php');
  1780. // Assume the first message if no message ID was given.
  1781. $request = $smcFunc['db_query']('', '
  1782. SELECT
  1783. t.locked, t.num_replies, t.id_member_started, t.id_first_msg,
  1784. m.id_msg, m.id_member, m.poster_time, m.subject, m.smileys_enabled, m.body, m.icon,
  1785. m.modified_time, m.modified_name, m.approved
  1786. FROM {db_prefix}messages AS m
  1787. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
  1788. WHERE m.id_msg = {raw:id_msg}
  1789. AND m.id_topic = {int:current_topic}' . (allowedTo('modify_any') || allowedTo('approve_posts') ? '' : (!$modSettings['postmod_active'] ? '
  1790. AND (m.id_member != {int:guest_id} AND m.id_member = {int:current_member})' : '
  1791. AND (m.approved = {int:is_approved} OR (m.id_member != {int:guest_id} AND m.id_member = {int:current_member}))')),
  1792. array(
  1793. 'current_member' => $user_info['id'],
  1794. 'current_topic' => $topic,
  1795. 'id_msg' => empty($_REQUEST['msg']) ? 't.id_first_msg' : (int) $_REQUEST['msg'],
  1796. 'is_approved' => 1,
  1797. 'guest_id' => 0,
  1798. )
  1799. );
  1800. if ($smcFunc['db_num_rows']($request) == 0)
  1801. fatal_lang_error('no_board', false);
  1802. $row = $smcFunc['db_fetch_assoc']($request);
  1803. $smcFunc['db_free_result']($request);
  1804. // Change either body or subject requires permissions to modify messages.
  1805. if (isset($_POST['message']) || isset($_POST['subject']) || isset($_REQUEST['icon']))
  1806. {
  1807. if (!empty($row['locked']))
  1808. isAllowedTo('moderate_board');
  1809. if ($row['id_member'] == $user_info['id'] && !allowedTo('modify_any'))
  1810. {
  1811. if ((!$modSettings['postmod_active'] || $row['approved']) && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + ($modSettings['edit_disable_time'] + 5) * 60 < time())
  1812. fatal_lang_error('modify_post_time_passed', false);
  1813. elseif ($row['id_member_started'] == $user_info['id'] && !allowedTo('modify_own'))
  1814. isAllowedTo('modify_replies');
  1815. else
  1816. isAllowedTo('modify_own');
  1817. }
  1818. // Otherwise, they're locked out; someone who can modify the replies is needed.
  1819. elseif ($row['id_member_started'] == $user_info['id'] && !allowedTo('modify_any'))
  1820. isAllowedTo('modify_replies');
  1821. else
  1822. isAllowedTo('modify_any');
  1823. // Only log this action if it wasn't your message.
  1824. $moderationAction = $row['id_member'] != $user_info['id'];
  1825. }
  1826. $post_errors = error_context::context('post', 1);
  1827. if (isset($_POST['subject']) && $smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['subject'])) !== '')
  1828. {
  1829. $_POST['subject'] = strtr($smcFunc['htmlspecialchars']($_POST['subject']), array("\r" => '', "\n" => '', "\t" => ''));
  1830. // Maximum number of characters.
  1831. if ($smcFunc['strlen']($_POST['subject']) > 100)
  1832. $_POST['subject'] = $smcFunc['substr']($_POST['subject'], 0, 100);
  1833. }
  1834. elseif (isset($_POST['subject']))
  1835. {
  1836. $post_errors->addError('no_subject', 0);
  1837. unset($_POST['subject']);
  1838. }
  1839. if (isset($_POST['message']))
  1840. {
  1841. if ($smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['message'])) === '')
  1842. {
  1843. $post_errors->addError('no_message');
  1844. unset($_POST['message']);
  1845. }
  1846. elseif (!empty($modSettings['max_messageLength']) && $smcFunc['strlen']($_POST['message']) > $modSettings['max_messageLength'])
  1847. {
  1848. $post_errors->addError(array('long_message', array($modSettings['max_messageLength'])));
  1849. unset($_POST['message']);
  1850. }
  1851. else
  1852. {
  1853. $_POST['message'] = $smcFunc['htmlspecialchars']($_POST['message'], ENT_QUOTES);
  1854. preparsecode($_POST['message']);
  1855. if ($smcFunc['htmltrim'](strip_tags(parse_bbc($_POST['message'], false), '<img>')) === '')
  1856. {
  1857. $post_errors->addError('no_message');
  1858. unset($_POST['message']);
  1859. }
  1860. }
  1861. }
  1862. if (isset($_POST['lock']))
  1863. {
  1864. if (!allowedTo(array('lock_any', 'lock_own')) || (!allowedTo('lock_any') && $user_info['id'] != $row['id_member']))
  1865. unset($_POST['lock']);
  1866. elseif (!allowedTo('lock_any'))
  1867. {
  1868. if ($row['locked'] == 1)
  1869. unset($_POST['lock']);
  1870. else
  1871. $_POST['lock'] = empty($_POST['lock']) ? 0 : 2;
  1872. }
  1873. elseif (!empty($row['locked']) && !empty($_POST['lock']) || $_POST['lock'] == $row['locked'])
  1874. unset($_POST['lock']);
  1875. else
  1876. $_POST['lock'] = empty($_POST['lock']) ? 0 : 1;
  1877. }
  1878. if (isset($_POST['sticky']) && !allowedTo('make_sticky'))
  1879. unset($_POST['sticky']);
  1880. if (!$post_errors->hasErrors())
  1881. {
  1882. $msgOptions = array(
  1883. 'id' => $row['id_msg'],
  1884. 'subject' => isset($_POST['subject']) ? $_POST['subject'] : null,
  1885. 'body' => isset($_POST['message']) ? $_POST['message'] : null,
  1886. 'icon' => isset($_REQUEST['icon']) ? preg_replace('~[\./\\\\*\':"<>]~', '', $_REQUEST['icon']) : null,
  1887. );
  1888. $topicOptions = array(
  1889. 'id' => $topic,
  1890. 'board' => $board,
  1891. 'lock_mode' => isset($_POST['lock']) ? (int) $_POST['lock'] : null,
  1892. 'sticky_mode' => isset($_POST['sticky']) && !empty($modSettings['enableStickyTopics']) ? (int) $_POST['sticky'] : null,
  1893. 'mark_as_read' => true,
  1894. );
  1895. $posterOptions = array();
  1896. // Only consider marking as editing if they have edited the subject, message or icon.
  1897. if ((isset($_POST['subject']) && $_POST['subject'] != $row['subject']) || (isset($_POST['message']) && $_POST['message'] != $row['body']) || (isset($_REQUEST['icon']) && $_REQUEST['icon'] != $row['icon']))
  1898. {
  1899. // And even then only if the time has passed...
  1900. if (time() - $row['poster_time'] > $modSettings['edit_wait_time'] || $user_info['id'] != $row['id_member'])
  1901. {
  1902. $msgOptions['modify_time'] = time();
  1903. $msgOptions['modify_name'] = $user_info['name'];
  1904. }
  1905. }
  1906. // If nothing was changed there's no need to add an entry to the moderation log.
  1907. else
  1908. $moderationAction = false;
  1909. modifyPost($msgOptions, $topicOptions, $posterOptions);
  1910. // If we didn't change anything this time but had before put back the old info.
  1911. if (!isset($msgOptions['modify_time']) && !empty($row['modified_time']))
  1912. {
  1913. $msgOptions['modify_time'] = $row['modified_time'];
  1914. $msgOptions['modify_name'] = $row['modified_name'];
  1915. }
  1916. // Changing the first subject updates other subjects to 'Re: new_subject'.
  1917. if (isset($_POST['subject']) && isset($_REQUEST['change_all_subjects']) && $row['id_first_msg'] == $row['id_msg'] && !empty($row['num_replies']) && (allowedTo('modify_any') || ($row['id_member_started'] == $user_info['id'] && allowedTo('modify_replies'))))
  1918. {
  1919. // Get the proper (default language) response prefix first.
  1920. if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
  1921. {
  1922. if ($language === $user_info['language'])
  1923. $context['response_prefix'] = $txt['response_prefix'];
  1924. else
  1925. {
  1926. loadLanguage('index', $language, false);
  1927. $context['response_prefix'] = $txt['response_prefix'];
  1928. loadLanguage('index');
  1929. }
  1930. cache_put_data('response_prefix', $context['response_prefix'], 600);
  1931. }
  1932. $smcFunc['db_query']('', '
  1933. UPDATE {db_prefix}messages
  1934. SET subject = {string:subject}
  1935. WHERE id_topic = {int:current_topic}
  1936. AND id_msg != {int:id_first_msg}',
  1937. array(
  1938. 'current_topic' => $topic,
  1939. 'id_first_msg' => $row['id_first_msg'],
  1940. 'subject' => $context['response_prefix'] . $_POST['subject'],
  1941. )
  1942. );
  1943. }
  1944. if (!empty($moderationAction))
  1945. logAction('modify', array('topic' => $topic, 'message' => $row['id_msg'], 'member' => $row['id_member'], 'board' => $board));
  1946. }
  1947. if (isset($_REQUEST['xml']))
  1948. {
  1949. $context['sub_template'] = 'modifydone';
  1950. if (!$post_errors->hasErrors() && isset($msgOptions['subject']) && isset($msgOptions['body']))
  1951. {
  1952. $context['message'] = array(
  1953. 'id' => $row['id_msg'],
  1954. 'modified' => array(
  1955. 'time' => isset($msgOptions['modify_time']) ? timeformat($msgOptions['modify_time']) : '',
  1956. 'timestamp' => isset($msgOptions['modify_time']) ? forum_time(true, $msgOptions['modify_time']) : 0,
  1957. 'name' => isset($msgOptions['modify_time']) ? $msgOptions['modify_name'] : '',
  1958. ),
  1959. 'subject' => $msgOptions['subject'],
  1960. 'first_in_topic' => $row['id_msg'] == $row['id_first_msg'],
  1961. 'body' => strtr($msgOptions['body'], array(']]>' => ']]]]><![CDATA[>')),
  1962. );
  1963. censorText($context['message']['subject']);
  1964. censorText($context['message']['body']);
  1965. $context['message']['body'] = parse_bbc($context['message']['body'], $row['smileys_enabled'], $row['id_msg']);
  1966. }
  1967. // Topic?
  1968. elseif (!$post_errors->hasErrors())
  1969. {
  1970. $context['sub_template'] = 'modifytopicdone';
  1971. $context['message'] = array(
  1972. 'id' => $row['id_msg'],
  1973. 'modified' => array(
  1974. 'time' => isset($msgOptions['modify_time']) ? timeformat($msgOptions['modify_time']) : '',
  1975. 'timestamp' => isset($msgOptions['modify_time']) ? forum_time(true, $msgOptions['modify_time']) : 0,
  1976. 'name' => isset($msgOptions['modify_time']) ? $msgOptions['modify_name'] : '',
  1977. ),
  1978. 'subject' => isset($msgOptions['subject']) ? $msgOptions['subject'] : '',
  1979. );
  1980. censorText($context['message']['subject']);
  1981. }
  1982. else
  1983. {
  1984. $context['message'] = array(
  1985. 'id' => $row['id_msg'],
  1986. 'errors' => array(),
  1987. 'error_in_subject' => $post_errors->hasError('no_subject'),
  1988. 'error_in_body' => $post_errors->hasError('no_message') || $post_errors->hasError('long_message'),
  1989. );
  1990. $context['message']['errors'] = $post_errors->prepareErrors();
  1991. }
  1992. }
  1993. else
  1994. obExit(false);
  1995. }
  1996. /**
  1997. * Spell checks the post for typos ;).
  1998. * It uses the pspell library, which MUST be installed.
  1999. * It has problems with internationalization.
  2000. * It is accessed via ?action=spellcheck.
  2001. */
  2002. function action_spellcheck()
  2003. {
  2004. global $txt, $context, $smcFunc;
  2005. // A list of "words" we know about but pspell doesn't.
  2006. $known_words = array('elkarte', 'php', 'mysql', 'www', 'gif', 'jpeg', 'png', 'http', 'grandia', 'terranigma', 'rpgs');
  2007. loadLanguage('Post');
  2008. loadTemplate('Post');
  2009. // Okay, this looks funny, but it actually fixes a weird bug.
  2010. ob_start();
  2011. $old = error_reporting(0);
  2012. // See, first, some windows machines don't load pspell properly on the first try. Dumb, but this is a workaround.
  2013. pspell_new('en');
  2014. // Next, the dictionary in question may not exist. So, we try it... but...
  2015. $pspell_link = pspell_new($txt['lang_dictionary'], $txt['lang_spelling'], '', 'utf-8', PSPELL_FAST | PSPELL_RUN_TOGETHER);
  2016. // Most people don't have anything but English installed... So we use English as a last resort.
  2017. if (!$pspell_link)
  2018. $pspell_link = pspell_new('en', '', '', '', PSPELL_FAST | PSPELL_RUN_TOGETHER);
  2019. error_reporting($old);
  2020. ob_end_clean();
  2021. if (!isset($_POST['spellstring']) || !$pspell_link)
  2022. die;
  2023. // Construct a bit of Javascript code.
  2024. $context['spell_js'] = '
  2025. var txt = {"done": "' . $txt['spellcheck_done'] . '"};
  2026. var mispstr = ' . ($_POST['fulleditor'] === 'true' ? 'window.opener.spellCheckGetText(spell_fieldname)' : 'window.opener.document.forms[spell_formname][spell_fieldname].value') . ';
  2027. var misps = Array(';
  2028. // Get all the words (Javascript already separated them).
  2029. $alphas = explode("\n", strtr($_POST['spellstring'], array("\r" => '')));
  2030. $found_words = false;
  2031. for ($i = 0, $n = count($alphas); $i < $n; $i++)
  2032. {
  2033. // Words are sent like 'word|offset_begin|offset_end'.
  2034. $check_word = explode('|', $alphas[$i]);
  2035. // If the word is a known word, or spelled right...
  2036. if (in_array($smcFunc['strtolower']($check_word[0]), $known_words) || pspell_check($pspell_link, $check_word[0]) || !isset($check_word[2]))
  2037. continue;
  2038. // Find the word, and move up the "last occurance" to here.
  2039. $found_words = true;
  2040. // Add on the javascript for this misspelling.
  2041. $context['spell_js'] .= '
  2042. new misp("' . strtr($check_word[0], array('\\' => '\\\\', '"' => '\\"', '<' => '', '&gt;' => '')) . '", ' . (int) $check_word[1] . ', ' . (int) $check_word[2] . ', [';
  2043. // If there are suggestions, add them in...
  2044. $suggestions = pspell_suggest($pspell_link, $check_word[0]);
  2045. if (!empty($suggestions))
  2046. {
  2047. // But first check they aren't going to be censored - no naughty words!
  2048. foreach ($suggestions as $k => $word)
  2049. if ($suggestions[$k] != censorText($word))
  2050. unset($suggestions[$k]);
  2051. if (!empty($suggestions))
  2052. $context['spell_js'] .= '"' . implode('", "', $suggestions) . '"';
  2053. }
  2054. $context['spell_js'] .= ']),';
  2055. }
  2056. // If words were found, take off the last comma.
  2057. if ($found_words)
  2058. $context['spell_js'] = substr($context['spell_js'], 0, -1);
  2059. $context['spell_js'] .= '
  2060. );';
  2061. // And instruct the template system to just show the spellcheck sub template.
  2062. $context['template_layers'] = array();
  2063. $context['sub_template'] = 'spellcheck';
  2064. }