PageRenderTime 76ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/php/Sources/Post.php

https://github.com/dekoza/openshift-smf-2.0.7
PHP | 2894 lines | 2160 code | 381 blank | 353 comment | 656 complexity | 44f251ac3da3176cfb7752a5429b6936 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0.7
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /* The job of this file is to handle everything related to posting replies,
  15. new topics, quotes, and modifications to existing posts. It also handles
  16. quoting posts by way of javascript.
  17. void Post()
  18. - handles showing the post screen, loading the post to be modified, and
  19. loading any post quoted.
  20. - additionally handles previews of posts.
  21. - uses the Post template and language file, main sub template.
  22. - allows wireless access using the protocol_post sub template.
  23. - requires different permissions depending on the actions, but most
  24. notably post_new, post_reply_own, and post_reply_any.
  25. - shows options for the editing and posting of calendar events and
  26. attachments, as well as the posting of polls.
  27. - accessed from ?action=post.
  28. void Post2()
  29. - actually posts or saves the message composed with Post().
  30. - requires various permissions depending on the action.
  31. - handles attachment, post, and calendar saving.
  32. - sends off notifications, and allows for announcements and moderation.
  33. - accessed from ?action=post2.
  34. void AnnounceTopic()
  35. - handle the announce topic function (action=announce).
  36. - checks the topic announcement permissions and loads the announcement
  37. template.
  38. - requires the announce_topic permission.
  39. - uses the ManageMembers template and Post language file.
  40. - call the right function based on the sub-action.
  41. void AnnouncementSelectMembergroup()
  42. - lets the user select the membergroups that will receive the topic
  43. announcement.
  44. void AnnouncementSend()
  45. - splits the members to be sent a topic announcement into chunks.
  46. - composes notification messages in all languages needed.
  47. - does the actual sending of the topic announcements in chunks.
  48. - calculates a rough estimate of the percentage items sent.
  49. void notifyMembersBoard(notifyData)
  50. - notifies members who have requested notification for new topics
  51. posted on a board of said posts.
  52. - receives data on the topics to send out notifications to by the passed in array.
  53. - only sends notifications to those who can *currently* see the topic
  54. (it doesn't matter if they could when they requested notification.)
  55. - loads the Post language file multiple times for each language if the
  56. userLanguage setting is set.
  57. void getTopic()
  58. - gets a summary of the most recent posts in a topic.
  59. - depends on the topicSummaryPosts setting.
  60. - if you are editing a post, only shows posts previous to that post.
  61. void QuoteFast()
  62. - loads a post an inserts it into the current editing text box.
  63. - uses the Post language file.
  64. - uses special (sadly browser dependent) javascript to parse entities
  65. for internationalization reasons.
  66. - accessed with ?action=quotefast.
  67. void JavaScriptModify()
  68. // !!!
  69. */
  70. function Post()
  71. {
  72. global $txt, $scripturl, $topic, $modSettings, $board;
  73. global $user_info, $sc, $board_info, $context, $settings;
  74. global $sourcedir, $options, $smcFunc, $language;
  75. loadLanguage('Post');
  76. // You can't reply with a poll... hacker.
  77. if (isset($_REQUEST['poll']) && !empty($topic) && !isset($_REQUEST['msg']))
  78. unset($_REQUEST['poll']);
  79. // Posting an event?
  80. $context['make_event'] = isset($_REQUEST['calendar']);
  81. $context['robot_no_index'] = true;
  82. // You must be posting to *some* board.
  83. if (empty($board) && !$context['make_event'])
  84. fatal_lang_error('no_board', false);
  85. require_once($sourcedir . '/Subs-Post.php');
  86. if (isset($_REQUEST['xml']))
  87. {
  88. $context['sub_template'] = 'post';
  89. // Just in case of an earlier error...
  90. $context['preview_message'] = '';
  91. $context['preview_subject'] = '';
  92. }
  93. // No message is complete without a topic.
  94. if (empty($topic) && !empty($_REQUEST['msg']))
  95. {
  96. $request = $smcFunc['db_query']('', '
  97. SELECT id_topic
  98. FROM {db_prefix}messages
  99. WHERE id_msg = {int:msg}',
  100. array(
  101. 'msg' => (int) $_REQUEST['msg'],
  102. ));
  103. if ($smcFunc['db_num_rows']($request) != 1)
  104. unset($_REQUEST['msg'], $_POST['msg'], $_GET['msg']);
  105. else
  106. list ($topic) = $smcFunc['db_fetch_row']($request);
  107. $smcFunc['db_free_result']($request);
  108. }
  109. // Check if it's locked. It isn't locked if no topic is specified.
  110. if (!empty($topic))
  111. {
  112. $request = $smcFunc['db_query']('', '
  113. SELECT
  114. t.locked, IFNULL(ln.id_topic, 0) AS notify, t.is_sticky, t.id_poll, t.id_last_msg, mf.id_member,
  115. t.id_first_msg, mf.subject,
  116. CASE WHEN ml.poster_time > ml.modified_time THEN ml.poster_time ELSE ml.modified_time END AS last_post_time
  117. FROM {db_prefix}topics AS t
  118. LEFT JOIN {db_prefix}log_notify AS ln ON (ln.id_topic = t.id_topic AND ln.id_member = {int:current_member})
  119. LEFT JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
  120. LEFT JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  121. WHERE t.id_topic = {int:current_topic}
  122. LIMIT 1',
  123. array(
  124. 'current_member' => $user_info['id'],
  125. 'current_topic' => $topic,
  126. )
  127. );
  128. list ($locked, $context['notify'], $sticky, $pollID, $context['topic_last_message'], $id_member_poster, $id_first_msg, $first_subject, $lastPostTime) = $smcFunc['db_fetch_row']($request);
  129. $smcFunc['db_free_result']($request);
  130. // If this topic already has a poll, they sure can't add another.
  131. if (isset($_REQUEST['poll']) && $pollID > 0)
  132. unset($_REQUEST['poll']);
  133. if (empty($_REQUEST['msg']))
  134. {
  135. if ($user_info['is_guest'] && !allowedTo('post_reply_any') && (!$modSettings['postmod_active'] || !allowedTo('post_unapproved_replies_any')))
  136. is_not_guest();
  137. // By default the reply will be approved...
  138. $context['becomes_approved'] = true;
  139. if ($id_member_poster != $user_info['id'])
  140. {
  141. if ($modSettings['postmod_active'] && allowedTo('post_unapproved_replies_any') && !allowedTo('post_reply_any'))
  142. $context['becomes_approved'] = false;
  143. else
  144. isAllowedTo('post_reply_any');
  145. }
  146. elseif (!allowedTo('post_reply_any'))
  147. {
  148. if ($modSettings['postmod_active'] && allowedTo('post_unapproved_replies_own') && !allowedTo('post_reply_own'))
  149. $context['becomes_approved'] = false;
  150. else
  151. isAllowedTo('post_reply_own');
  152. }
  153. }
  154. else
  155. $context['becomes_approved'] = true;
  156. $context['can_lock'] = allowedTo('lock_any') || ($user_info['id'] == $id_member_poster && allowedTo('lock_own'));
  157. $context['can_sticky'] = allowedTo('make_sticky') && !empty($modSettings['enableStickyTopics']);
  158. $context['notify'] = !empty($context['notify']);
  159. $context['sticky'] = isset($_REQUEST['sticky']) ? !empty($_REQUEST['sticky']) : $sticky;
  160. }
  161. else
  162. {
  163. $context['becomes_approved'] = true;
  164. if ((!$context['make_event'] || !empty($board)))
  165. {
  166. if ($modSettings['postmod_active'] && !allowedTo('post_new') && allowedTo('post_unapproved_topics'))
  167. $context['becomes_approved'] = false;
  168. else
  169. isAllowedTo('post_new');
  170. }
  171. $locked = 0;
  172. // !!! These won't work if you're making an event.
  173. $context['can_lock'] = allowedTo(array('lock_any', 'lock_own'));
  174. $context['can_sticky'] = allowedTo('make_sticky') && !empty($modSettings['enableStickyTopics']);
  175. $context['notify'] = !empty($context['notify']);
  176. $context['sticky'] = !empty($_REQUEST['sticky']);
  177. }
  178. // !!! These won't work if you're posting an event!
  179. $context['can_notify'] = allowedTo('mark_any_notify');
  180. $context['can_move'] = allowedTo('move_any');
  181. $context['move'] = !empty($_REQUEST['move']);
  182. $context['announce'] = !empty($_REQUEST['announce']);
  183. // You can only announce topics that will get approved...
  184. $context['can_announce'] = allowedTo('announce_topic') && $context['becomes_approved'];
  185. $context['locked'] = !empty($locked) || !empty($_REQUEST['lock']);
  186. $context['can_quote'] = empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC']));
  187. // Generally don't show the approval box... (Assume we want things approved)
  188. $context['show_approval'] = false;
  189. // An array to hold all the attachments for this topic.
  190. $context['current_attachments'] = array();
  191. // Don't allow a post if it's locked and you aren't all powerful.
  192. if ($locked && !allowedTo('moderate_board'))
  193. fatal_lang_error('topic_locked', false);
  194. // Check the users permissions - is the user allowed to add or post a poll?
  195. if (isset($_REQUEST['poll']) && $modSettings['pollMode'] == '1')
  196. {
  197. // New topic, new poll.
  198. if (empty($topic))
  199. isAllowedTo('poll_post');
  200. // This is an old topic - but it is yours! Can you add to it?
  201. elseif ($user_info['id'] == $id_member_poster && !allowedTo('poll_add_any'))
  202. isAllowedTo('poll_add_own');
  203. // If you're not the owner, can you add to any poll?
  204. else
  205. isAllowedTo('poll_add_any');
  206. require_once($sourcedir . '/Subs-Members.php');
  207. $allowedVoteGroups = groupsAllowedTo('poll_vote', $board);
  208. // Set up the poll options.
  209. $context['poll_options'] = array(
  210. 'max_votes' => empty($_POST['poll_max_votes']) ? '1' : max(1, $_POST['poll_max_votes']),
  211. 'hide' => empty($_POST['poll_hide']) ? 0 : $_POST['poll_hide'],
  212. 'expire' => !isset($_POST['poll_expire']) ? '' : $_POST['poll_expire'],
  213. 'change_vote' => isset($_POST['poll_change_vote']),
  214. 'guest_vote' => isset($_POST['poll_guest_vote']),
  215. 'guest_vote_enabled' => in_array(-1, $allowedVoteGroups['allowed']),
  216. );
  217. // Make all five poll choices empty.
  218. $context['choices'] = array(
  219. array('id' => 0, 'number' => 1, 'label' => '', 'is_last' => false),
  220. array('id' => 1, 'number' => 2, 'label' => '', 'is_last' => false),
  221. array('id' => 2, 'number' => 3, 'label' => '', 'is_last' => false),
  222. array('id' => 3, 'number' => 4, 'label' => '', 'is_last' => false),
  223. array('id' => 4, 'number' => 5, 'label' => '', 'is_last' => true)
  224. );
  225. }
  226. if ($context['make_event'])
  227. {
  228. // They might want to pick a board.
  229. if (!isset($context['current_board']))
  230. $context['current_board'] = 0;
  231. // Start loading up the event info.
  232. $context['event'] = array();
  233. $context['event']['title'] = isset($_REQUEST['evtitle']) ? htmlspecialchars(stripslashes($_REQUEST['evtitle'])) : '';
  234. $context['event']['id'] = isset($_REQUEST['eventid']) ? (int) $_REQUEST['eventid'] : -1;
  235. $context['event']['new'] = $context['event']['id'] == -1;
  236. // Permissions check!
  237. isAllowedTo('calendar_post');
  238. // Editing an event? (but NOT previewing!?)
  239. if (!$context['event']['new'] && !isset($_REQUEST['subject']))
  240. {
  241. // If the user doesn't have permission to edit the post in this topic, redirect them.
  242. if ((empty($id_member_poster) || $id_member_poster != $user_info['id'] || !allowedTo('modify_own')) && !allowedTo('modify_any'))
  243. {
  244. require_once($sourcedir . '/Calendar.php');
  245. return CalendarPost();
  246. }
  247. // Get the current event information.
  248. $request = $smcFunc['db_query']('', '
  249. SELECT
  250. id_member, title, MONTH(start_date) AS month, DAYOFMONTH(start_date) AS day,
  251. YEAR(start_date) AS year, (TO_DAYS(end_date) - TO_DAYS(start_date)) AS span
  252. FROM {db_prefix}calendar
  253. WHERE id_event = {int:id_event}
  254. LIMIT 1',
  255. array(
  256. 'id_event' => $context['event']['id'],
  257. )
  258. );
  259. $row = $smcFunc['db_fetch_assoc']($request);
  260. $smcFunc['db_free_result']($request);
  261. // Make sure the user is allowed to edit this event.
  262. if ($row['id_member'] != $user_info['id'])
  263. isAllowedTo('calendar_edit_any');
  264. elseif (!allowedTo('calendar_edit_any'))
  265. isAllowedTo('calendar_edit_own');
  266. $context['event']['month'] = $row['month'];
  267. $context['event']['day'] = $row['day'];
  268. $context['event']['year'] = $row['year'];
  269. $context['event']['title'] = $row['title'];
  270. $context['event']['span'] = $row['span'] + 1;
  271. }
  272. else
  273. {
  274. $today = getdate();
  275. // You must have a month and year specified!
  276. if (!isset($_REQUEST['month']))
  277. $_REQUEST['month'] = $today['mon'];
  278. if (!isset($_REQUEST['year']))
  279. $_REQUEST['year'] = $today['year'];
  280. $context['event']['month'] = (int) $_REQUEST['month'];
  281. $context['event']['year'] = (int) $_REQUEST['year'];
  282. $context['event']['day'] = isset($_REQUEST['day']) ? $_REQUEST['day'] : ($_REQUEST['month'] == $today['mon'] ? $today['mday'] : 0);
  283. $context['event']['span'] = isset($_REQUEST['span']) ? $_REQUEST['span'] : 1;
  284. // Make sure the year and month are in the valid range.
  285. if ($context['event']['month'] < 1 || $context['event']['month'] > 12)
  286. fatal_lang_error('invalid_month', false);
  287. if ($context['event']['year'] < $modSettings['cal_minyear'] || $context['event']['year'] > $modSettings['cal_maxyear'])
  288. fatal_lang_error('invalid_year', false);
  289. // Get a list of boards they can post in.
  290. $boards = boardsAllowedTo('post_new');
  291. if (empty($boards))
  292. fatal_lang_error('cannot_post_new', 'user');
  293. // Load a list of boards for this event in the context.
  294. require_once($sourcedir . '/Subs-MessageIndex.php');
  295. $boardListOptions = array(
  296. 'included_boards' => in_array(0, $boards) ? null : $boards,
  297. 'not_redirection' => true,
  298. 'use_permissions' => true,
  299. 'selected_board' => empty($context['current_board']) ? $modSettings['cal_defaultboard'] : $context['current_board'],
  300. );
  301. $context['event']['categories'] = getBoardList($boardListOptions);
  302. }
  303. // Find the last day of the month.
  304. $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']));
  305. $context['event']['board'] = !empty($board) ? $board : $modSettings['cal_defaultboard'];
  306. }
  307. if (empty($context['post_errors']))
  308. $context['post_errors'] = array();
  309. // See if any new replies have come along.
  310. if (empty($_REQUEST['msg']) && !empty($topic))
  311. {
  312. if (empty($options['no_new_reply_warning']) && isset($_REQUEST['last_msg']) && $context['topic_last_message'] > $_REQUEST['last_msg'])
  313. {
  314. $request = $smcFunc['db_query']('', '
  315. SELECT COUNT(*)
  316. FROM {db_prefix}messages
  317. WHERE id_topic = {int:current_topic}
  318. AND id_msg > {int:last_msg}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  319. AND approved = {int:approved}') . '
  320. LIMIT 1',
  321. array(
  322. 'current_topic' => $topic,
  323. 'last_msg' => (int) $_REQUEST['last_msg'],
  324. 'approved' => 1,
  325. )
  326. );
  327. list ($context['new_replies']) = $smcFunc['db_fetch_row']($request);
  328. $smcFunc['db_free_result']($request);
  329. if (!empty($context['new_replies']))
  330. {
  331. if ($context['new_replies'] == 1)
  332. $txt['error_new_reply'] = isset($_GET['last_msg']) ? $txt['error_new_reply_reading'] : $txt['error_new_reply'];
  333. else
  334. $txt['error_new_replies'] = sprintf(isset($_GET['last_msg']) ? $txt['error_new_replies_reading'] : $txt['error_new_replies'], $context['new_replies']);
  335. // If they've come from the display page then we treat the error differently....
  336. if (isset($_GET['last_msg']))
  337. $newRepliesError = $context['new_replies'];
  338. else
  339. $context['post_error'][$context['new_replies'] == 1 ? 'new_reply' : 'new_replies'] = true;
  340. $modSettings['topicSummaryPosts'] = $context['new_replies'] > $modSettings['topicSummaryPosts'] ? max($modSettings['topicSummaryPosts'], 5) : $modSettings['topicSummaryPosts'];
  341. }
  342. }
  343. // Check whether this is a really old post being bumped...
  344. if (!empty($modSettings['oldTopicDays']) && $lastPostTime + $modSettings['oldTopicDays'] * 86400 < time() && empty($sticky) && !isset($_REQUEST['subject']))
  345. $oldTopicError = true;
  346. }
  347. // Get a response prefix (like 'Re:') in the default forum language.
  348. if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
  349. {
  350. if ($language === $user_info['language'])
  351. $context['response_prefix'] = $txt['response_prefix'];
  352. else
  353. {
  354. loadLanguage('index', $language, false);
  355. $context['response_prefix'] = $txt['response_prefix'];
  356. loadLanguage('index');
  357. }
  358. cache_put_data('response_prefix', $context['response_prefix'], 600);
  359. }
  360. // Previewing, modifying, or posting?
  361. if (isset($_REQUEST['message']) || !empty($context['post_error']))
  362. {
  363. // Validate inputs.
  364. if (empty($context['post_error']))
  365. {
  366. if (htmltrim__recursive(htmlspecialchars__recursive($_REQUEST['subject'])) == '')
  367. $context['post_error']['no_subject'] = true;
  368. if (htmltrim__recursive(htmlspecialchars__recursive($_REQUEST['message'])) == '')
  369. $context['post_error']['no_message'] = true;
  370. if (!empty($modSettings['max_messageLength']) && $smcFunc['strlen']($_REQUEST['message']) > $modSettings['max_messageLength'])
  371. $context['post_error']['long_message'] = true;
  372. // Are you... a guest?
  373. if ($user_info['is_guest'])
  374. {
  375. $_REQUEST['guestname'] = !isset($_REQUEST['guestname']) ? '' : trim($_REQUEST['guestname']);
  376. $_REQUEST['email'] = !isset($_REQUEST['email']) ? '' : trim($_REQUEST['email']);
  377. // Validate the name and email.
  378. if (!isset($_REQUEST['guestname']) || trim(strtr($_REQUEST['guestname'], '_', ' ')) == '')
  379. $context['post_error']['no_name'] = true;
  380. elseif ($smcFunc['strlen']($_REQUEST['guestname']) > 25)
  381. $context['post_error']['long_name'] = true;
  382. else
  383. {
  384. require_once($sourcedir . '/Subs-Members.php');
  385. if (isReservedName(htmlspecialchars($_REQUEST['guestname']), 0, true, false))
  386. $context['post_error']['bad_name'] = true;
  387. }
  388. if (empty($modSettings['guest_post_no_email']))
  389. {
  390. if (!isset($_REQUEST['email']) || $_REQUEST['email'] == '')
  391. $context['post_error']['no_email'] = true;
  392. elseif (preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $_REQUEST['email']) == 0)
  393. $context['post_error']['bad_email'] = true;
  394. }
  395. }
  396. // This is self explanatory - got any questions?
  397. if (isset($_REQUEST['question']) && trim($_REQUEST['question']) == '')
  398. $context['post_error']['no_question'] = true;
  399. // This means they didn't click Post and get an error.
  400. $really_previewing = true;
  401. }
  402. else
  403. {
  404. if (!isset($_REQUEST['subject']))
  405. $_REQUEST['subject'] = '';
  406. if (!isset($_REQUEST['message']))
  407. $_REQUEST['message'] = '';
  408. if (!isset($_REQUEST['icon']))
  409. $_REQUEST['icon'] = 'xx';
  410. // They are previewing if they asked to preview (i.e. came from quick reply).
  411. $really_previewing = !empty($_POST['preview']);
  412. }
  413. // In order to keep the approval status flowing through, we have to pass it through the form...
  414. $context['becomes_approved'] = empty($_REQUEST['not_approved']);
  415. $context['show_approval'] = isset($_REQUEST['approve']) ? ($_REQUEST['approve'] ? 2 : 1) : 0;
  416. $context['can_announce'] &= $context['becomes_approved'];
  417. // Set up the inputs for the form.
  418. $form_subject = strtr($smcFunc['htmlspecialchars']($_REQUEST['subject']), array("\r" => '', "\n" => '', "\t" => ''));
  419. $form_message = $smcFunc['htmlspecialchars']($_REQUEST['message'], ENT_QUOTES);
  420. // Make sure the subject isn't too long - taking into account special characters.
  421. if ($smcFunc['strlen']($form_subject) > 100)
  422. $form_subject = $smcFunc['substr']($form_subject, 0, 100);
  423. // Have we inadvertently trimmed off the subject of useful information?
  424. if ($smcFunc['htmltrim']($form_subject) === '')
  425. $context['post_error']['no_subject'] = true;
  426. // Any errors occurred?
  427. if (!empty($context['post_error']))
  428. {
  429. loadLanguage('Errors');
  430. $context['error_type'] = 'minor';
  431. $context['post_error']['messages'] = array();
  432. foreach ($context['post_error'] as $post_error => $dummy)
  433. {
  434. if ($post_error == 'messages')
  435. continue;
  436. if ($post_error == 'long_message')
  437. $txt['error_' . $post_error] = sprintf($txt['error_' . $post_error], $modSettings['max_messageLength']);
  438. $context['post_error']['messages'][] = $txt['error_' . $post_error];
  439. // If it's not a minor error flag it as such.
  440. if (!in_array($post_error, array('new_reply', 'not_approved', 'new_replies', 'old_topic', 'need_qr_verification')))
  441. $context['error_type'] = 'serious';
  442. }
  443. }
  444. if (isset($_REQUEST['poll']))
  445. {
  446. $context['question'] = isset($_REQUEST['question']) ? $smcFunc['htmlspecialchars'](trim($_REQUEST['question'])) : '';
  447. $context['choices'] = array();
  448. $choice_id = 0;
  449. $_POST['options'] = empty($_POST['options']) ? array() : htmlspecialchars__recursive($_POST['options']);
  450. foreach ($_POST['options'] as $option)
  451. {
  452. if (trim($option) == '')
  453. continue;
  454. $context['choices'][] = array(
  455. 'id' => $choice_id++,
  456. 'number' => $choice_id,
  457. 'label' => $option,
  458. 'is_last' => false
  459. );
  460. }
  461. if (count($context['choices']) < 2)
  462. {
  463. $context['choices'][] = array(
  464. 'id' => $choice_id++,
  465. 'number' => $choice_id,
  466. 'label' => '',
  467. 'is_last' => false
  468. );
  469. $context['choices'][] = array(
  470. 'id' => $choice_id++,
  471. 'number' => $choice_id,
  472. 'label' => '',
  473. 'is_last' => false
  474. );
  475. }
  476. $context['choices'][count($context['choices']) - 1]['is_last'] = true;
  477. }
  478. // Are you... a guest?
  479. if ($user_info['is_guest'])
  480. {
  481. $_REQUEST['guestname'] = !isset($_REQUEST['guestname']) ? '' : trim($_REQUEST['guestname']);
  482. $_REQUEST['email'] = !isset($_REQUEST['email']) ? '' : trim($_REQUEST['email']);
  483. $_REQUEST['guestname'] = htmlspecialchars($_REQUEST['guestname']);
  484. $context['name'] = $_REQUEST['guestname'];
  485. $_REQUEST['email'] = htmlspecialchars($_REQUEST['email']);
  486. $context['email'] = $_REQUEST['email'];
  487. $user_info['name'] = $_REQUEST['guestname'];
  488. }
  489. // Only show the preview stuff if they hit Preview.
  490. if ($really_previewing == true || isset($_REQUEST['xml']))
  491. {
  492. // Set up the preview message and subject and censor them...
  493. $context['preview_message'] = $form_message;
  494. preparsecode($form_message, true);
  495. preparsecode($context['preview_message']);
  496. // Do all bulletin board code tags, with or without smileys.
  497. $context['preview_message'] = parse_bbc($context['preview_message'], isset($_REQUEST['ns']) ? 0 : 1);
  498. if ($form_subject != '')
  499. {
  500. $context['preview_subject'] = $form_subject;
  501. censorText($context['preview_subject']);
  502. censorText($context['preview_message']);
  503. }
  504. else
  505. $context['preview_subject'] = '<em>' . $txt['no_subject'] . '</em>';
  506. // Protect any CDATA blocks.
  507. if (isset($_REQUEST['xml']))
  508. $context['preview_message'] = strtr($context['preview_message'], array(']]>' => ']]]]><![CDATA[>'));
  509. }
  510. // Set up the checkboxes.
  511. $context['notify'] = !empty($_REQUEST['notify']);
  512. $context['use_smileys'] = !isset($_REQUEST['ns']);
  513. $context['icon'] = isset($_REQUEST['icon']) ? preg_replace('~[\./\\\\*\':"<>]~', '', $_REQUEST['icon']) : 'xx';
  514. // Set the destination action for submission.
  515. $context['destination'] = 'post2;start=' . $_REQUEST['start'] . (isset($_REQUEST['msg']) ? ';msg=' . $_REQUEST['msg'] . ';' . $context['session_var'] . '=' . $context['session_id'] : '') . (isset($_REQUEST['poll']) ? ';poll' : '');
  516. $context['submit_label'] = isset($_REQUEST['msg']) ? $txt['save'] : $txt['post'];
  517. // Previewing an edit?
  518. if (isset($_REQUEST['msg']) && !empty($topic))
  519. {
  520. // Get the existing message.
  521. $request = $smcFunc['db_query']('', '
  522. SELECT
  523. m.id_member, m.modified_time, m.smileys_enabled, m.body,
  524. m.poster_name, m.poster_email, m.subject, m.icon, m.approved,
  525. IFNULL(a.size, -1) AS filesize, a.filename, a.id_attach,
  526. a.approved AS attachment_approved, t.id_member_started AS id_member_poster,
  527. m.poster_time
  528. FROM {db_prefix}messages AS m
  529. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
  530. LEFT JOIN {db_prefix}attachments AS a ON (a.id_msg = m.id_msg AND a.attachment_type = {int:attachment_type})
  531. WHERE m.id_msg = {int:id_msg}
  532. AND m.id_topic = {int:current_topic}',
  533. array(
  534. 'current_topic' => $topic,
  535. 'attachment_type' => 0,
  536. 'id_msg' => $_REQUEST['msg'],
  537. )
  538. );
  539. // The message they were trying to edit was most likely deleted.
  540. // !!! Change this error message?
  541. if ($smcFunc['db_num_rows']($request) == 0)
  542. fatal_lang_error('no_board', false);
  543. $row = $smcFunc['db_fetch_assoc']($request);
  544. $attachment_stuff = array($row);
  545. while ($row2 = $smcFunc['db_fetch_assoc']($request))
  546. $attachment_stuff[] = $row2;
  547. $smcFunc['db_free_result']($request);
  548. if ($row['id_member'] == $user_info['id'] && !allowedTo('modify_any'))
  549. {
  550. // Give an extra five minutes over the disable time threshold, so they can type - assuming the post is public.
  551. if ($row['approved'] && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + ($modSettings['edit_disable_time'] + 5) * 60 < time())
  552. fatal_lang_error('modify_post_time_passed', false);
  553. elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('modify_own'))
  554. isAllowedTo('modify_replies');
  555. else
  556. isAllowedTo('modify_own');
  557. }
  558. elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('modify_any'))
  559. isAllowedTo('modify_replies');
  560. else
  561. isAllowedTo('modify_any');
  562. if (!empty($modSettings['attachmentEnable']))
  563. {
  564. $request = $smcFunc['db_query']('', '
  565. SELECT IFNULL(size, -1) AS filesize, filename, id_attach, approved
  566. FROM {db_prefix}attachments
  567. WHERE id_msg = {int:id_msg}
  568. AND attachment_type = {int:attachment_type}',
  569. array(
  570. 'id_msg' => (int) $_REQUEST['msg'],
  571. 'attachment_type' => 0,
  572. )
  573. );
  574. while ($row = $smcFunc['db_fetch_assoc']($request))
  575. {
  576. if ($row['filesize'] <= 0)
  577. continue;
  578. $context['current_attachments'][] = array(
  579. 'name' => htmlspecialchars($row['filename']),
  580. 'id' => $row['id_attach'],
  581. 'approved' => $row['approved'],
  582. );
  583. }
  584. $smcFunc['db_free_result']($request);
  585. }
  586. // Allow moderators to change names....
  587. if (allowedTo('moderate_forum') && !empty($topic))
  588. {
  589. $request = $smcFunc['db_query']('', '
  590. SELECT id_member, poster_name, poster_email
  591. FROM {db_prefix}messages
  592. WHERE id_msg = {int:id_msg}
  593. AND id_topic = {int:current_topic}
  594. LIMIT 1',
  595. array(
  596. 'current_topic' => $topic,
  597. 'id_msg' => (int) $_REQUEST['msg'],
  598. )
  599. );
  600. $row = $smcFunc['db_fetch_assoc']($request);
  601. $smcFunc['db_free_result']($request);
  602. if (empty($row['id_member']))
  603. {
  604. $context['name'] = htmlspecialchars($row['poster_name']);
  605. $context['email'] = htmlspecialchars($row['poster_email']);
  606. }
  607. }
  608. }
  609. // No check is needed, since nothing is really posted.
  610. checkSubmitOnce('free');
  611. }
  612. // Editing a message...
  613. elseif (isset($_REQUEST['msg']) && !empty($topic))
  614. {
  615. $_REQUEST['msg'] = (int) $_REQUEST['msg'];
  616. // Get the existing message.
  617. $request = $smcFunc['db_query']('', '
  618. SELECT
  619. m.id_member, m.modified_time, m.smileys_enabled, m.body,
  620. m.poster_name, m.poster_email, m.subject, m.icon, m.approved,
  621. IFNULL(a.size, -1) AS filesize, a.filename, a.id_attach,
  622. a.approved AS attachment_approved, t.id_member_started AS id_member_poster,
  623. m.poster_time
  624. FROM {db_prefix}messages AS m
  625. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
  626. LEFT JOIN {db_prefix}attachments AS a ON (a.id_msg = m.id_msg AND a.attachment_type = {int:attachment_type})
  627. WHERE m.id_msg = {int:id_msg}
  628. AND m.id_topic = {int:current_topic}',
  629. array(
  630. 'current_topic' => $topic,
  631. 'attachment_type' => 0,
  632. 'id_msg' => $_REQUEST['msg'],
  633. )
  634. );
  635. // The message they were trying to edit was most likely deleted.
  636. // !!! Change this error message?
  637. if ($smcFunc['db_num_rows']($request) == 0)
  638. fatal_lang_error('no_board', false);
  639. $row = $smcFunc['db_fetch_assoc']($request);
  640. $attachment_stuff = array($row);
  641. while ($row2 = $smcFunc['db_fetch_assoc']($request))
  642. $attachment_stuff[] = $row2;
  643. $smcFunc['db_free_result']($request);
  644. if ($row['id_member'] == $user_info['id'] && !allowedTo('modify_any'))
  645. {
  646. // Give an extra five minutes over the disable time threshold, so they can type - assuming the post is public.
  647. if ($row['approved'] && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + ($modSettings['edit_disable_time'] + 5) * 60 < time())
  648. fatal_lang_error('modify_post_time_passed', false);
  649. elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('modify_own'))
  650. isAllowedTo('modify_replies');
  651. else
  652. isAllowedTo('modify_own');
  653. }
  654. elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('modify_any'))
  655. isAllowedTo('modify_replies');
  656. else
  657. isAllowedTo('modify_any');
  658. // When was it last modified?
  659. if (!empty($row['modified_time']))
  660. $context['last_modified'] = timeformat($row['modified_time']);
  661. // Get the stuff ready for the form.
  662. $form_subject = $row['subject'];
  663. $form_message = un_preparsecode($row['body']);
  664. censorText($form_message);
  665. censorText($form_subject);
  666. // Check the boxes that should be checked.
  667. $context['use_smileys'] = !empty($row['smileys_enabled']);
  668. $context['icon'] = $row['icon'];
  669. // Show an "approve" box if the user can approve it, and the message isn't approved.
  670. if (!$row['approved'] && !$context['show_approval'])
  671. $context['show_approval'] = allowedTo('approve_posts');
  672. // Load up 'em attachments!
  673. foreach ($attachment_stuff as $attachment)
  674. {
  675. if ($attachment['filesize'] >= 0 && !empty($modSettings['attachmentEnable']))
  676. $context['current_attachments'][] = array(
  677. 'name' => htmlspecialchars($attachment['filename']),
  678. 'id' => $attachment['id_attach'],
  679. 'approved' => $attachment['attachment_approved'],
  680. );
  681. }
  682. // Allow moderators to change names....
  683. if (allowedTo('moderate_forum') && empty($row['id_member']))
  684. {
  685. $context['name'] = htmlspecialchars($row['poster_name']);
  686. $context['email'] = htmlspecialchars($row['poster_email']);
  687. }
  688. // Set the destinaton.
  689. $context['destination'] = 'post2;start=' . $_REQUEST['start'] . ';msg=' . $_REQUEST['msg'] . ';' . $context['session_var'] . '=' . $context['session_id'] . (isset($_REQUEST['poll']) ? ';poll' : '');
  690. $context['submit_label'] = $txt['save'];
  691. }
  692. // Posting...
  693. else
  694. {
  695. // By default....
  696. $context['use_smileys'] = true;
  697. $context['icon'] = 'xx';
  698. if ($user_info['is_guest'])
  699. {
  700. $context['name'] = isset($_SESSION['guest_name']) ? $_SESSION['guest_name'] : '';
  701. $context['email'] = isset($_SESSION['guest_email']) ? $_SESSION['guest_email'] : '';
  702. }
  703. $context['destination'] = 'post2;start=' . $_REQUEST['start'] . (isset($_REQUEST['poll']) ? ';poll' : '');
  704. $context['submit_label'] = $txt['post'];
  705. // Posting a quoted reply?
  706. if (!empty($topic) && !empty($_REQUEST['quote']))
  707. {
  708. // Make sure they _can_ quote this post, and if so get it.
  709. $request = $smcFunc['db_query']('', '
  710. SELECT m.subject, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.body
  711. FROM {db_prefix}messages AS m
  712. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND {query_see_board})
  713. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  714. WHERE m.id_msg = {int:id_msg}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  715. AND m.approved = {int:is_approved}') . '
  716. LIMIT 1',
  717. array(
  718. 'id_msg' => (int) $_REQUEST['quote'],
  719. 'is_approved' => 1,
  720. )
  721. );
  722. if ($smcFunc['db_num_rows']($request) == 0)
  723. fatal_lang_error('quoted_post_deleted', false);
  724. list ($form_subject, $mname, $mdate, $form_message) = $smcFunc['db_fetch_row']($request);
  725. $smcFunc['db_free_result']($request);
  726. // Add 'Re: ' to the front of the quoted subject.
  727. if (trim($context['response_prefix']) != '' && $smcFunc['strpos']($form_subject, trim($context['response_prefix'])) !== 0)
  728. $form_subject = $context['response_prefix'] . $form_subject;
  729. // Censor the message and subject.
  730. censorText($form_message);
  731. censorText($form_subject);
  732. // But if it's in HTML world, turn them into htmlspecialchar's so they can be edited!
  733. if (strpos($form_message, '[html]') !== false)
  734. {
  735. $parts = preg_split('~(\[/code\]|\[code(?:=[^\]]+)?\])~i', $form_message, -1, PREG_SPLIT_DELIM_CAPTURE);
  736. for ($i = 0, $n = count($parts); $i < $n; $i++)
  737. {
  738. // It goes 0 = outside, 1 = begin tag, 2 = inside, 3 = close tag, repeat.
  739. if ($i % 4 == 0)
  740. $parts[$i] = preg_replace_callback('~\[html\](.+?)\[/html\]~is', create_function('$m', ' return \'[html]\' . preg_replace(\'~<br\s?/?' . '>~i\', \'&lt;br /&gt;<br />\', "$m[1]") . \'[/html]\';'), $parts[$i]);
  741. }
  742. $form_message = implode('', $parts);
  743. }
  744. $form_message = preg_replace('~<br ?/?' . '>~i', "\n", $form_message);
  745. // Remove any nested quotes, if necessary.
  746. if (!empty($modSettings['removeNestedQuotes']))
  747. $form_message = preg_replace(array('~\n?\[quote.*?\].+?\[/quote\]\n?~is', '~^\n~', '~\[/quote\]~'), '', $form_message);
  748. // Add a quote string on the front and end.
  749. $form_message = '[quote author=' . $mname . ' link=topic=' . $topic . '.msg' . (int) $_REQUEST['quote'] . '#msg' . (int) $_REQUEST['quote'] . ' date=' . $mdate . ']' . "\n" . rtrim($form_message) . "\n" . '[/quote]';
  750. }
  751. // Posting a reply without a quote?
  752. elseif (!empty($topic) && empty($_REQUEST['quote']))
  753. {
  754. // Get the first message's subject.
  755. $form_subject = $first_subject;
  756. // Add 'Re: ' to the front of the subject.
  757. if (trim($context['response_prefix']) != '' && $form_subject != '' && $smcFunc['strpos']($form_subject, trim($context['response_prefix'])) !== 0)
  758. $form_subject = $context['response_prefix'] . $form_subject;
  759. // Censor the subject.
  760. censorText($form_subject);
  761. $form_message = '';
  762. }
  763. else
  764. {
  765. $form_subject = isset($_GET['subject']) ? $_GET['subject'] : '';
  766. $form_message = '';
  767. }
  768. }
  769. // !!! This won't work if you're posting an event.
  770. if (allowedTo('post_attachment') || allowedTo('post_unapproved_attachments'))
  771. {
  772. if (empty($_SESSION['temp_attachments']))
  773. $_SESSION['temp_attachments'] = array();
  774. if (!empty($modSettings['currentAttachmentUploadDir']))
  775. {
  776. if (!is_array($modSettings['attachmentUploadDir']))
  777. $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
  778. // Just use the current path for temp files.
  779. $current_attach_dir = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']];
  780. }
  781. else
  782. $current_attach_dir = $modSettings['attachmentUploadDir'];
  783. // If this isn't a new post, check the current attachments.
  784. if (isset($_REQUEST['msg']))
  785. {
  786. $request = $smcFunc['db_query']('', '
  787. SELECT COUNT(*), SUM(size)
  788. FROM {db_prefix}attachments
  789. WHERE id_msg = {int:id_msg}
  790. AND attachment_type = {int:attachment_type}',
  791. array(
  792. 'id_msg' => (int) $_REQUEST['msg'],
  793. 'attachment_type' => 0,
  794. )
  795. );
  796. list ($quantity, $total_size) = $smcFunc['db_fetch_row']($request);
  797. $smcFunc['db_free_result']($request);
  798. }
  799. else
  800. {
  801. $quantity = 0;
  802. $total_size = 0;
  803. }
  804. $temp_start = 0;
  805. if (!empty($_SESSION['temp_attachments']))
  806. {
  807. if ($context['current_action'] != 'post2' || !empty($_POST['from_qr']))
  808. {
  809. $context['post_error']['messages'][] = $txt['error_temp_attachments'];
  810. $context['error_type'] = 'minor';
  811. }
  812. foreach ($_SESSION['temp_attachments'] as $attachID => $name)
  813. {
  814. $temp_start++;
  815. if (preg_match('~^post_tmp_' . $user_info['id'] . '_\d+$~', $attachID) == 0)
  816. {
  817. unset($_SESSION['temp_attachments'][$attachID]);
  818. continue;
  819. }
  820. if (!empty($_POST['attach_del']) && !in_array($attachID, $_POST['attach_del']))
  821. {
  822. $deleted_attachments = true;
  823. unset($_SESSION['temp_attachments'][$attachID]);
  824. @unlink($current_attach_dir . '/' . $attachID);
  825. continue;
  826. }
  827. $quantity++;
  828. $total_size += filesize($current_attach_dir . '/' . $attachID);
  829. $context['current_attachments'][] = array(
  830. 'name' => htmlspecialchars($name),
  831. 'id' => $attachID,
  832. 'approved' => 1,
  833. );
  834. }
  835. }
  836. if (!empty($_POST['attach_del']))
  837. {
  838. $del_temp = array();
  839. foreach ($_POST['attach_del'] as $i => $dummy)
  840. $del_temp[$i] = (int) $dummy;
  841. foreach ($context['current_attachments'] as $k => $dummy)
  842. if (!in_array($dummy['id'], $del_temp))
  843. {
  844. $context['current_attachments'][$k]['unchecked'] = true;
  845. $deleted_attachments = !isset($deleted_attachments) || is_bool($deleted_attachments) ? 1 : $deleted_attachments + 1;
  846. $quantity--;
  847. }
  848. }
  849. if (!empty($_FILES['attachment']))
  850. foreach ($_FILES['attachment']['tmp_name'] as $n => $dummy)
  851. {
  852. if ($_FILES['attachment']['name'][$n] == '')
  853. continue;
  854. if (!is_uploaded_file($_FILES['attachment']['tmp_name'][$n]) || (@ini_get('open_basedir') == '' && !file_exists($_FILES['attachment']['tmp_name'][$n])))
  855. fatal_lang_error('attach_timeout', 'critical');
  856. if (!empty($modSettings['attachmentSizeLimit']) && $_FILES['attachment']['size'][$n] > $modSettings['attachmentSizeLimit'] * 1024)
  857. fatal_lang_error('file_too_big', false, array($modSettings['attachmentSizeLimit']));
  858. $quantity++;
  859. if (!empty($modSettings['attachmentNumPerPostLimit']) && $quantity > $modSettings['attachmentNumPerPostLimit'])
  860. fatal_lang_error('attachments_limit_per_post', false, array($modSettings['attachmentNumPerPostLimit']));
  861. $total_size += $_FILES['attachment']['size'][$n];
  862. if (!empty($modSettings['attachmentPostLimit']) && $total_size > $modSettings['attachmentPostLimit'] * 1024)
  863. fatal_lang_error('file_too_big', false, array($modSettings['attachmentPostLimit']));
  864. if (!empty($modSettings['attachmentCheckExtensions']))
  865. {
  866. if (!in_array(strtolower(substr(strrchr($_FILES['attachment']['name'][$n], '.'), 1)), explode(',', strtolower($modSettings['attachmentExtensions']))))
  867. fatal_error($_FILES['attachment']['name'][$n] . '.<br />' . $txt['cant_upload_type'] . ' ' . $modSettings['attachmentExtensions'] . '.', false);
  868. }
  869. if (!empty($modSettings['attachmentDirSizeLimit']))
  870. {
  871. // Make sure the directory isn't full.
  872. $dirSize = 0;
  873. $dir = @opendir($current_attach_dir) or fatal_lang_error('cant_access_upload_path', 'critical');
  874. while ($file = readdir($dir))
  875. {
  876. if ($file == '.' || $file == '..')
  877. continue;
  878. if (preg_match('~^post_tmp_\d+_\d+$~', $file) != 0)
  879. {
  880. // Temp file is more than 5 hours old!
  881. if (filemtime($current_attach_dir . '/' . $file) < time() - 18000)
  882. @unlink($current_attach_dir . '/' . $file);
  883. continue;
  884. }
  885. $dirSize += filesize($current_attach_dir . '/' . $file);
  886. }
  887. closedir($dir);
  888. // Too big! Maybe you could zip it or something...
  889. if ($_FILES['attachment']['size'][$n] + $dirSize > $modSettings['attachmentDirSizeLimit'] * 1024)
  890. fatal_lang_error('ran_out_of_space');
  891. }
  892. if (!is_writable($current_attach_dir))
  893. fatal_lang_error('attachments_no_write', 'critical');
  894. $attachID = 'post_tmp_' . $user_info['id'] . '_' . $temp_start++;
  895. $_SESSION['temp_attachments'][$attachID] = basename($_FILES['attachment']['name'][$n]);
  896. $context['current_attachments'][] = array(
  897. 'name' => htmlspecialchars(basename($_FILES['attachment']['name'][$n])),
  898. 'id' => $attachID,
  899. 'approved' => 1,
  900. );
  901. $destName = $current_attach_dir . '/' . $attachID;
  902. if (!move_uploaded_file($_FILES['attachment']['tmp_name'][$n], $destName))
  903. fatal_lang_error('attach_timeout', 'critical');
  904. @chmod($destName, 0644);
  905. }
  906. }
  907. // If we are coming here to make a reply, and someone has already replied... make a special warning message.
  908. if (isset($newRepliesError))
  909. {
  910. $context['post_error']['messages'][] = $newRepliesError == 1 ? $txt['error_new_reply'] : $txt['error_new_replies'];
  911. $context['error_type'] = 'minor';
  912. }
  913. if (isset($oldTopicError))
  914. {
  915. $context['post_error']['messages'][] = sprintf($txt['error_old_topic'], $modSettings['oldTopicDays']);
  916. $context['error_type'] = 'minor';
  917. }
  918. // What are you doing? Posting a poll, modifying, previewing, new post, or reply...
  919. if (isset($_REQUEST['poll']))
  920. $context['page_title'] = $txt['new_poll'];
  921. elseif ($context['make_event'])
  922. $context['page_title'] = $context['event']['id'] == -1 ? $txt['calendar_post_event'] : $txt['calendar_edit'];
  923. elseif (isset($_REQUEST['msg']))
  924. $context['page_title'] = $txt['modify_msg'];
  925. elseif (isset($_REQUEST['subject'], $context['preview_subject']))
  926. $context['page_title'] = $txt['preview'] . ' - ' . strip_tags($context['preview_subject']);
  927. elseif (empty($topic))
  928. $context['page_title'] = $txt['start_new_topic'];
  929. else
  930. $context['page_title'] = $txt['post_reply'];
  931. // Build the link tree.
  932. if (empty($topic))
  933. $context['linktree'][] = array(
  934. 'name' => '<em>' . $txt['start_new_topic'] . '</em>'
  935. );
  936. else
  937. $context['linktree'][] = array(
  938. 'url' => $scripturl . '?topic=' . $topic . '.' . $_REQUEST['start'],
  939. 'name' => $form_subject,
  940. 'extra_before' => '<span' . ($settings['linktree_inline'] ? ' class="smalltext"' : '') . '><strong class="nav">' . $context['page_title'] . ' ( </strong></span>',
  941. 'extra_after' => '<span' . ($settings['linktree_inline'] ? ' class="smalltext"' : '') . '><strong class="nav"> )</strong></span>'
  942. );
  943. // Give wireless a linktree url to the post screen, so that they can switch to full version.
  944. if (WIRELESS)
  945. $context['linktree'][count($context['linktree']) - 1]['url'] = $scripturl . '?action=post;' . (!empty($topic) ? 'topic=' . $topic : 'board=' . $board) . '.' . $_REQUEST['start'] . (isset($_REQUEST['msg']) ? ';msg=' . (int) $_REQUEST['msg'] . ';' . $context['session_var'] . '=' . $context['session_id'] : '');
  946. // 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.
  947. // !!! This won't work if you're posting an event.
  948. $context['num_allowed_attachments'] = empty($modSettings['attachmentNumPerPostLimit']) ? 50 : min($modSettings['attachmentNumPerPostLimit'] - count($context['current_attachments']) + (isset($deleted_attachments) ? $deleted_attachments : 0), $modSettings['attachmentNumPerPostLimit']);
  949. $context['can_post_attachment'] = !empty($modSettings['attachmentEnable']) && $modSettings['attachmentEnable'] == 1 && (allowedTo('post_attachment') || ($modSettings['postmod_active'] && allowedTo('post_unapproved_attachments'))) && $context['num_allowed_attachments'] > 0;
  950. $context['can_post_attachment_unapproved'] = allowedTo('post_attachment');
  951. $context['subject'] = addcslashes($form_subject, '"');
  952. $context['message'] = str_replace(array('"', '<', '>', '&nbsp;'), array('&quot;', '&lt;', '&gt;', ' '), $form_message);
  953. // Needed for the editor and message icons.
  954. require_once($sourcedir . '/Subs-Editor.php');
  955. // Now create the editor.
  956. $editorOptions = array(
  957. 'id' => 'message',
  958. 'value' => $context['message'],
  959. 'labels' => array(
  960. 'post_button' => $context['submit_label'],
  961. ),
  962. // add height and width for the editor
  963. 'height' => '175px',
  964. 'width' => '100%',
  965. // We do XML preview here.
  966. 'preview_type' => 2,
  967. );
  968. create_control_richedit($editorOptions);
  969. // Store the ID.
  970. $context['post_box_name'] = $editorOptions['id'];
  971. $context['attached'] = '';
  972. $context['make_poll'] = isset($_REQUEST['poll']);
  973. // Message icons - customized icons are off?
  974. $context['icons'] = getMessageIcons($board);
  975. if (!empty($context['icons']))
  976. $context['icons'][count($context['icons']) - 1]['is_last'] = true;
  977. $context['icon_url'] = '';
  978. for ($i = 0, $n = count($context['icons']); $i < $n; $i++)
  979. {
  980. $context['icons'][$i]['selected'] = $context['icon'] == $context['icons'][$i]['value'];
  981. if ($context['icons'][$i]['selected'])
  982. $context['icon_url'] = $context['icons'][$i]['url'];
  983. }
  984. if (empty($context['icon_url']))
  985. {
  986. $context['icon_url'] = $settings[file_exists($settings['theme_dir'] . '/images/post/' . $context['icon'] . '.gif') ? 'images_url' : 'default_images_url'] . '/post/' . $context['icon'] . '.gif';
  987. array_unshift($context['icons'], array(
  988. 'value' => $context['icon'],
  989. 'name' => $txt['current_icon'],
  990. 'url' => $context['icon_url'],
  991. 'is_last' => empty($context['icons']),
  992. 'selected' => true,
  993. ));
  994. }
  995. if (!empty($topic) && !empty($modSettings['topicSummaryPosts']))
  996. getTopic();
  997. // If the user can post attachments prepare the warning labels.
  998. if ($context['can_post_attachment'])
  999. {
  1000. $context['allowed_extensions'] = strtr($modSettings['attachmentExtensions'], array(',' => ', '));
  1001. $context['attachment_restrictions'] = array();
  1002. $attachmentRestrictionTypes = array('attachmentNumPerPostLimit', 'attachmentPostLimit', 'attachmentSizeLimit');
  1003. foreach ($attachmentRestrictionTypes as $type)
  1004. if (!empty($modSettings[$type]))
  1005. $context['attachment_restrictions'][] = sprintf($txt['attach_restrict_' . $type], $modSettings[$type]);
  1006. }
  1007. $context['back_to_topic'] = isset($_REQUEST['goback']) || (isset($_REQUEST['msg']) && !isset($_REQUEST['subject']));
  1008. $context['show_additional_options'] = !empty($_POST['additional_options']) || !empty($_SESSION['temp_attachments']) || !empty($deleted_attachments);
  1009. $context['is_new_topic'] = empty($topic);
  1010. $context['is_new_post'] = !isset($_REQUEST['msg']);
  1011. $context['is_first_post'] = $context['is_new_topic'] || (isset($_REQUEST['msg']) && $_REQUEST['msg'] == $id_first_msg);
  1012. // Do we need to show the visual verification image?
  1013. $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));
  1014. if ($context['require_verification'])
  1015. {
  1016. require_once($sourcedir . '/Subs-Editor.php');
  1017. $verificationOptions = array(
  1018. 'id' => 'post',
  1019. );
  1020. $context['require_verification'] = create_control_verification($verificationOptions);
  1021. $context['visual_verification_id'] = $verificationOptions['id'];
  1022. }
  1023. // If they came from quick reply, and have to enter verification details, give them some notice.
  1024. if (!empty($_REQUEST['from_qr']) && !empty($context['require_verification']))
  1025. {
  1026. $context['post_error']['messages'][] = $txt['enter_verification_details'];
  1027. $context['error_type'] = 'minor';
  1028. }
  1029. // WYSIWYG only works if BBC is enabled
  1030. $modSettings['disable_wysiwyg'] = !empty($modSettings['disable_wysiwyg']) || empty($modSettings['enableBBC']);
  1031. // Register this form in the session variables.
  1032. checkSubmitOnce('register');
  1033. // Finally, load the template.
  1034. if (WIRELESS && WIRELESS_PROTOCOL != 'wap')
  1035. $context['sub_template'] = WIRELESS_PROTOCOL . '_post';
  1036. elseif (!isset($_REQUEST['xml']))
  1037. loadTemplate('Post');
  1038. }
  1039. function Post2()
  1040. {
  1041. global $board, $topic, $txt, $modSettings, $sourcedir, $context;
  1042. global $user_info, $board_info, $options, $smcFunc;
  1043. // Sneaking off, are we?
  1044. if (empty($_POST) && empty($topic))
  1045. redirectexit('action=post;board=' . $board . '.0');
  1046. elseif (empty($_POST) && !empty($topic))
  1047. redirectexit('action=post;topic=' . $topic . '.0');
  1048. // No need!
  1049. $context['robot_no_index'] = true;
  1050. // If we came from WYSIWYG then turn it back into BBC regardless.
  1051. if (!empty($_REQUEST['message_mode']) && isset($_REQUEST['message']))
  1052. {
  1053. require_once($sourcedir . '/Subs-Editor.php');
  1054. $_REQUEST['message'] = html_to_bbc($_REQUEST['message']);
  1055. // We need to unhtml it now as it gets done shortly.
  1056. $_REQUEST['message'] = un_htmlspecialchars($_REQUEST['message']);
  1057. // We need this for everything else.
  1058. $_POST['message'] = $_REQUEST['message'];
  1059. }
  1060. // Previewing? Go back to start.
  1061. if (isset($_REQUEST['preview']))
  1062. return Post();
  1063. // Prevent double submission of this form.
  1064. checkSubmitOnce('check');
  1065. // No errors as yet.
  1066. $post_errors = array();
  1067. // If the session has timed out, let the user re-submit their form.
  1068. if (checkSession('post', '', false) != '')
  1069. $post_errors[] = 'session_timeout';
  1070. // Wrong verification code?
  1071. 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)))
  1072. {
  1073. require_once($sourcedir . '/Subs-Editor.php');
  1074. $verificationOptions = array(
  1075. 'id' => 'post',
  1076. );
  1077. $context['require_verification'] = create_control_verification($verificationOptions, true);
  1078. if (is_array($context['require_verification']))
  1079. $post_errors = array_merge($post_errors, $context['require_verification']);
  1080. }
  1081. require_once($sourcedir . '/Subs-Post.php');
  1082. loadLanguage('Post');
  1083. // If this isn't a new topic load the topic info that we need.
  1084. if (!empty($topic))
  1085. {
  1086. $request = $smcFunc['db_query']('', '
  1087. SELECT locked, is_sticky, id_poll, approved, id_first_msg, id_last_msg, id_member_started, id_board
  1088. FROM {db_prefix}topics
  1089. WHERE id_topic = {int:current_topic}
  1090. LIMIT 1',
  1091. array(
  1092. 'current_topic' => $topic,
  1093. )
  1094. );
  1095. $topic_info = $smcFunc['db_fetch_assoc']($request);
  1096. $smcFunc['db_free_result']($request);
  1097. // Though the topic should be there, it might have vanished.
  1098. if (!is_array($topic_info))
  1099. fatal_lang_error('topic_doesnt_exist');
  1100. // Did this topic suddenly move? Just checking...
  1101. if ($topic_info['id_board'] != $board)
  1102. fatal_lang_error('not_a_topic');
  1103. }
  1104. // Replying to a topic?
  1105. if (!empty($topic) && !isset($_REQUEST['msg']))
  1106. {
  1107. // Don't allow a post if it's locked.
  1108. if ($topic_info['locked'] != 0 && !allowedTo('moderate_board'))
  1109. fatal_lang_error('topic_locked', false);
  1110. // Sorry, multiple polls aren't allowed... yet. You should stop giving me ideas :P.
  1111. if (isset($_REQUEST['poll']) && $topic_info['id_poll'] > 0)
  1112. unset($_REQUEST['poll']);
  1113. // Do the permissions and approval stuff...
  1114. $becomesApproved = true;
  1115. if ($topic_info['id_member_started'] != $user_info['id'])
  1116. {
  1117. if ($modSettings['postmod_active'] && allowedTo('post_unapproved_replies_any') && !allowedTo('post_reply_any'))
  1118. $becomesApproved = false;
  1119. else
  1120. isAllowedTo('post_reply_any');
  1121. }
  1122. elseif (!allowedTo('post_reply_any'))
  1123. {
  1124. if ($modSettings['postmod_active'] && allowedTo('post_unapproved_replies_own') && !allowedTo('post_reply_own'))
  1125. $becomesApproved = false;
  1126. else
  1127. isAllowedTo('post_reply_own');
  1128. }
  1129. if (isset($_POST['lock']))
  1130. {
  1131. // Nothing is changed to the lock.
  1132. if ((empty($topic_info['locked']) && empty($_POST['lock'])) || (!empty($_POST['lock']) && !empty($topic_info['locked'])))
  1133. unset($_POST['lock']);
  1134. // You're have no permission to lock this topic.
  1135. elseif (!allowedTo(array('lock_any', 'lock_own')) || (!allowedTo('lock_any') && $user_info['id'] != $topic_info['id_member_started']))
  1136. unset($_POST['lock']);
  1137. // You are allowed to (un)lock your own topic only.
  1138. elseif (!allowedTo('lock_any'))
  1139. {
  1140. // You cannot override a moderator lock.
  1141. if ($topic_info['locked'] == 1)
  1142. unset($_POST['lock']);
  1143. else
  1144. $_POST['lock'] = empty($_POST['lock']) ? 0 : 2;
  1145. }
  1146. // Hail mighty moderator, (un)lock this topic immediately.
  1147. else
  1148. $_POST['lock'] = empty($_POST['lock']) ? 0 : 1;
  1149. }
  1150. // So you wanna (un)sticky this...let's see.
  1151. if (isset($_POST['sticky']) && (empty($modSettings['enableStickyTopics']) || $_POST['sticky'] == $topic_info['is_sticky'] || !allowedTo('make_sticky')))
  1152. unset($_POST['sticky']);
  1153. // If the number of replies has changed, if the setting is enabled, go back to Post() - which handles the error.
  1154. if (empty($options['no_new_reply_warning']) && isset($_POST['last_msg']) && $topic_info['id_last_msg'] > $_POST['last_msg'])
  1155. {
  1156. $_REQUEST['preview'] = true;
  1157. return Post();
  1158. }
  1159. $posterIsGuest = $user_info['is_guest'];
  1160. }
  1161. // Posting a new topic.
  1162. elseif (empty($topic))
  1163. {
  1164. // Now don't be silly, new topics will get their own id_msg soon enough.
  1165. unset($_REQUEST['msg'], $_POST['msg'], $_GET['msg']);
  1166. // Do like, the permissions, for safety and stuff...
  1167. $becomesApproved = true;
  1168. if ($modSettings['postmod_active'] && !allowedTo('post_new') && allowedTo('post_unapproved_topics'))
  1169. $becomesApproved = false;
  1170. else
  1171. isAllowedTo('post_new');
  1172. if (isset($_POST['lock']))
  1173. {
  1174. // New topics are by default not locked.
  1175. if (empty($_POST['lock']))
  1176. unset($_POST['lock']);
  1177. // Besides, you need permission.
  1178. elseif (!allowedTo(array('lock_any', 'lock_own')))
  1179. unset($_POST['lock']);
  1180. // A moderator-lock (1) can override a user-lock (2).
  1181. else
  1182. $_POST['lock'] = allowedTo('lock_any') ? 1 : 2;
  1183. }
  1184. if (isset($_POST['sticky']) && (empty($modSettings['enableStickyTopics']) || empty($_POST['sticky']) || !allowedTo('make_sticky')))
  1185. unset($_POST['sticky']);
  1186. $posterIsGuest = $user_info['is_guest'];
  1187. }
  1188. // Modifying an existing message?
  1189. elseif (isset($_REQUEST['msg']) && !empty($topic))
  1190. {
  1191. $_REQUEST['msg'] = (int) $_REQUEST['msg'];
  1192. $request = $smcFunc['db_query']('', '
  1193. SELECT id_member, poster_name, poster_email, poster_time, approved
  1194. FROM {db_prefix}messages
  1195. WHERE id_msg = {int:id_msg}
  1196. LIMIT 1',
  1197. array(
  1198. 'id_msg' => $_REQUEST['msg'],
  1199. )
  1200. );
  1201. if ($smcFunc['db_num_rows']($request) == 0)
  1202. fatal_lang_error('cant_find_messages', false);
  1203. $row = $smcFunc['db_fetch_assoc']($request);
  1204. $smcFunc['db_free_result']($request);
  1205. if (!empty($topic_info['locked']) && !allowedTo('moderate_board'))
  1206. fatal_lang_error('topic_locked', false);
  1207. if (isset($_POST['lock']))
  1208. {
  1209. // Nothing changes to the lock status.
  1210. if ((empty($_POST['lock']) && empty($topic_info['locked'])) || (!empty($_POST['lock']) && !empty($topic_info['locked'])))
  1211. unset($_POST['lock']);
  1212. // You're simply not allowed to (un)lock this.
  1213. elseif (!allowedTo(array('lock_any', 'lock_own')) || (!allowedTo('lock_any') && $user_info['id'] != $topic_info['id_member_started']))
  1214. unset($_POST['lock']);
  1215. // You're only allowed to lock your own topics.
  1216. elseif (!allowedTo('lock_any'))
  1217. {
  1218. // You're not allowed to break a moderator's lock.
  1219. if ($topic_info['locked'] == 1)
  1220. unset($_POST['lock']);
  1221. // Lock it with a soft lock or unlock it.
  1222. else
  1223. $_POST['lock'] = empty($_POST['lock']) ? 0 : 2;
  1224. }
  1225. // You must be the moderator.
  1226. else
  1227. $_POST['lock'] = empty($_POST['lock']) ? 0 : 1;
  1228. }
  1229. // Change the sticky status of this topic?
  1230. if (isset($_POST['sticky']) && (!allowedTo('make_sticky') || $_POST['sticky'] == $topic_info['is_sticky']))
  1231. unset($_POST['sticky']);
  1232. if ($row['id_member'] == $user_info['id'] && !allowedTo('modify_any'))
  1233. {
  1234. if ((!$modSettings['postmod_active'] || $row['approved']) && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + ($modSettings['edit_disable_time'] + 5) * 60 < time())
  1235. fatal_lang_error('modify_post_time_passed', false);
  1236. elseif ($topic_info['id_member_started'] == $user_info['id'] && !allowedTo('modify_own'))
  1237. isAllowedTo('modify_replies');
  1238. else
  1239. isAllowedTo('modify_own');
  1240. }
  1241. elseif ($topic_info['id_member_started'] == $user_info['id'] && !allowedTo('modify_any'))
  1242. {
  1243. isAllowedTo('modify_replies');
  1244. // If you're modifying a reply, I say it better be logged...
  1245. $moderationAction = true;
  1246. }
  1247. else
  1248. {
  1249. isAllowedTo('modify_any');
  1250. // Log it, assuming you're not modifying your own post.
  1251. if ($row['id_member'] != $user_info['id'])
  1252. $moderationAction = true;
  1253. }
  1254. $posterIsGuest = empty($row['id_member']);
  1255. // Can they approve it?
  1256. $can_approve = allowedTo('approve_posts');
  1257. $becomesApproved = $modSettings['postmod_active'] ? ($can_approve && !$row['approved'] ? (!empty($_REQUEST['approve']) ? 1 : 0) : $row['approved']) : 1;
  1258. $approve_has_changed = $row['approved'] != $becomesApproved;
  1259. if (!allowedTo('moderate_forum') || !$posterIsGuest)
  1260. {
  1261. $_POST['guestname'] = $row['poster_name'];
  1262. $_POST['email'] = $row['poster_email'];
  1263. }
  1264. }
  1265. // If the poster is a guest evaluate the legality of name and email.
  1266. if ($posterIsGuest)
  1267. {
  1268. $_POST['guestname'] = !isset($_POST['guestname']) ? '' : trim($_POST['guestname']);
  1269. $_POST['email'] = !isset($_POST['email']) ? '' : trim($_POST['email']);
  1270. if ($_POST['guestname'] == '' || $_POST['guestname'] == '_')
  1271. $post_errors[] = 'no_name';
  1272. if ($smcFunc['strlen']($_POST['guestname']) > 25)
  1273. $post_errors[] = 'long_name';
  1274. if (empty($modSettings['guest_post_no_email']))
  1275. {
  1276. // Only check if they changed it!
  1277. if (!isset($row) || $row['poster_email'] != $_POST['email'])
  1278. {
  1279. if (!allowedTo('moderate_forum') && (!isset($_POST['email']) || $_POST['email'] == ''))
  1280. $post_errors[] = 'no_email';
  1281. if (!allowedTo('moderate_forum') && preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $_POST['email']) == 0)
  1282. $post_errors[] = 'bad_email';
  1283. }
  1284. // Now make sure this email address is not banned from posting.
  1285. isBannedEmail($_POST['email'], 'cannot_post', sprintf($txt['you_are_post_banned'], $txt['guest_title']));
  1286. }
  1287. // In case they are making multiple posts this visit, help them along by storing their name.
  1288. if (empty($post_errors))
  1289. {
  1290. $_SESSION['guest_name'] = $_POST['guestname'];
  1291. $_SESSION['guest_email'] = $_POST['email'];
  1292. }
  1293. }
  1294. // Check the subject and message.
  1295. if (!isset($_POST['subject']) || $smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['subject'])) === '')
  1296. $post_errors[] = 'no_subject';
  1297. if (!isset($_POST['message']) || $smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['message']), ENT_QUOTES) === '')
  1298. $post_errors[] = 'no_message';
  1299. elseif (!empty($modSettings['max_messageLength']) && $smcFunc['strlen']($_POST['message']) > $modSettings['max_messageLength'])
  1300. $post_errors[] = 'long_message';
  1301. else
  1302. {
  1303. // Prepare the message a bit for some additional testing.
  1304. $_POST['message'] = $smcFunc['htmlspecialchars']($_POST['message'], ENT_QUOTES);
  1305. // Preparse code. (Zef)
  1306. if ($user_info['is_guest'])
  1307. $user_info['name'] = $_POST['guestname'];
  1308. preparsecode($_POST['message']);
  1309. // Let's see if there's still some content left without the tags.
  1310. if ($smcFunc['htmltrim'](strip_tags(parse_bbc($_POST['message'], false), '<img>')) === '' && (!allowedTo('admin_forum') || strpos($_POST['message'], '[html]') === false))
  1311. $post_errors[] = 'no_message';
  1312. }
  1313. if (isset($_POST['calendar']) && !isset($_REQUEST['deleteevent']) && $smcFunc['htmltrim']($_POST['evtitle']) === '')
  1314. $post_errors[] = 'no_event';
  1315. // You are not!
  1316. if (isset($_POST['message']) && strtolower($_POST['message']) == 'i am the administrator.' && !$user_info['is_admin'])
  1317. fatal_error('Knave! Masquerader! Charlatan!', false);
  1318. // Validate the poll...
  1319. if (isset($_REQUEST['poll']) && $modSettings['pollMode'] == '1')
  1320. {
  1321. if (!empty($topic) && !isset($_REQUEST['msg']))
  1322. fatal_lang_error('no_access', false);
  1323. // This is a new topic... so it's a new poll.
  1324. if (empty($topic))
  1325. isAllowedTo('poll_post');
  1326. // Can you add to your own topics?
  1327. elseif ($user_info['id'] == $topic_info['id_member_started'] && !allowedTo('poll_add_any'))
  1328. isAllowedTo('poll_add_own');
  1329. // Can you add polls to any topic, then?
  1330. else
  1331. isAllowedTo('poll_add_any');
  1332. if (!isset($_POST['question']) || trim($_POST['question']) == '')
  1333. $post_errors[] = 'no_question';
  1334. $_POST['options'] = empty($_POST['options']) ? array() : htmltrim__recursive($_POST['options']);
  1335. // Get rid of empty ones.
  1336. foreach ($_POST['options'] as $k => $option)
  1337. if ($option == '')
  1338. unset($_POST['options'][$k], $_POST['options'][$k]);
  1339. // What are you going to vote between with one choice?!?
  1340. if (count($_POST['options']) < 2)
  1341. $post_errors[] = 'poll_few';
  1342. }
  1343. if ($posterIsGuest)
  1344. {
  1345. // If user is a guest, make sure the chosen name isn't taken.
  1346. require_once($sourcedir . '/Subs-Members.php');
  1347. if (isReservedName($_POST['guestname'], 0, true, false) && (!isset($row['poster_name']) || $_POST['guestname'] != $row['poster_name']))
  1348. $post_errors[] = 'bad_name';
  1349. }
  1350. // If the user isn't a guest, get his or her name and email.
  1351. elseif (!isset($_REQUEST['msg']))
  1352. {
  1353. $_POST['guestname'] = $user_info['username'];
  1354. $_POST['email'] = $user_info['email'];
  1355. }
  1356. // Any mistakes?
  1357. if (!empty($post_errors))
  1358. {
  1359. loadLanguage('Errors');
  1360. // Previewing.
  1361. $_REQUEST['preview'] = true;
  1362. $context['post_error'] = array('messages' => array());
  1363. foreach ($post_errors as $post_error)
  1364. {
  1365. $context['post_error'][$post_error] = true;
  1366. if ($post_error == 'long_message')
  1367. $txt['error_' . $post_error] = sprintf($txt['error_' . $post_error], $modSettings['max_messageLength']);
  1368. $context['post_error']['messages'][] = $txt['error_' . $post_error];
  1369. }
  1370. return Post();
  1371. }
  1372. // Make sure the user isn't spamming the board.
  1373. if (!isset($_REQUEST['msg']))
  1374. spamProtection('post');
  1375. // At about this point, we're posting and that's that.
  1376. ignore_user_abort(true);
  1377. @set_time_limit(300);
  1378. // Add special html entities to the subject, name, and email.
  1379. $_POST['subject'] = strtr($smcFunc['htmlspecialchars']($_POST['subject']), array("\r" => '', "\n" => '', "\t" => ''));
  1380. $_POST['guestname'] = htmlspecialchars($_POST['guestname']);
  1381. $_POST['email'] = htmlspecialchars($_POST['email']);
  1382. // At this point, we want to make sure the subject isn't too long.
  1383. if ($smcFunc['strlen']($_POST['subject']) > 100)
  1384. $_POST['subject'] = $smcFunc['substr']($_POST['subject'], 0, 100);
  1385. // Make the poll...
  1386. if (isset($_REQUEST['poll']))
  1387. {
  1388. // Make sure that the user has not entered a ridiculous number of options..
  1389. if (empty($_POST['poll_max_votes']) || $_POST['poll_max_votes'] <= 0)
  1390. $_POST['poll_max_votes'] = 1;
  1391. elseif ($_POST['poll_max_votes'] > count($_POST['options']))
  1392. $_POST['poll_max_votes'] = count($_POST['options']);
  1393. else
  1394. $_POST['poll_max_votes'] = (int) $_POST['poll_max_votes'];
  1395. $_POST['poll_expire'] = (int) $_POST['poll_expire'];
  1396. $_POST['poll_expire'] = $_POST['poll_expire'] > 9999 ? 9999 : ($_POST['poll_expire'] < 0 ? 0 : $_POST['poll_expire']);
  1397. // Just set it to zero if it's not there..
  1398. if (!isset($_POST['poll_hide']))
  1399. $_POST['poll_hide'] = 0;
  1400. else
  1401. $_POST['poll_hide'] = (int) $_POST['poll_hide'];
  1402. $_POST['poll_change_vote'] = isset($_POST['poll_change_vote']) ? 1 : 0;
  1403. $_POST['poll_guest_vote'] = isset($_POST['poll_guest_vote']) ? 1 : 0;
  1404. // Make sure guests are actually allowed to vote generally.
  1405. if ($_POST['poll_guest_vote'])
  1406. {
  1407. require_once($sourcedir . '/Subs-Members.php');
  1408. $allowedVoteGroups = groupsAllowedTo('poll_vote', $board);
  1409. if (!in_array(-1, $allowedVoteGroups['allowed']))
  1410. $_POST['poll_guest_vote'] = 0;
  1411. }
  1412. // If the user tries to set the poll too far in advance, don't let them.
  1413. if (!empty($_POST['poll_expire']) && $_POST['poll_expire'] < 1)
  1414. fatal_lang_error('poll_range_error', false);
  1415. // Don't allow them to select option 2 for hidden results if it's not time limited.
  1416. elseif (empty($_POST['poll_expire']) && $_POST['poll_hide'] == 2)
  1417. $_POST['poll_hide'] = 1;
  1418. // Clean up the question and answers.
  1419. $_POST['question'] = htmlspecialchars($_POST['question']);
  1420. $_POST['question'] = $smcFunc['truncate']($_POST['question'], 255);
  1421. $_POST['question'] = preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $_POST['question']);
  1422. $_POST['options'] = htmlspecialchars__recursive($_POST['options']);
  1423. }
  1424. // Check if they are trying to delete any current attachments....
  1425. if (isset($_REQUEST['msg'], $_POST['attach_del']) && (allowedTo('post_attachment') || ($modSettings['postmod_active'] && allowedTo('post_unapproved_attachments'))))
  1426. {
  1427. $del_temp = array();
  1428. foreach ($_POST['attach_del'] as $i => $dummy)
  1429. $del_temp[$i] = (int) $dummy;
  1430. require_once($sourcedir . '/ManageAttachments.php');
  1431. $attachmentQuery = array(
  1432. 'attachment_type' => 0,
  1433. 'id_msg' => (int) $_REQUEST['msg'],
  1434. 'not_id_attach' => $del_temp,
  1435. );
  1436. removeAttachments($attachmentQuery);
  1437. }
  1438. // ...or attach a new file...
  1439. if (isset($_FILES['attachment']['name']) || (!empty($_SESSION['temp_attachments']) && empty($_POST['from_qr'])))
  1440. {
  1441. // Verify they can post them!
  1442. if (!$modSettings['postmod_active'] || !allowedTo('post_unapproved_attachments'))
  1443. isAllowedTo('post_attachment');
  1444. // Make sure we're uploading to the right place.
  1445. if (!empty($modSettings['currentAttachmentUploadDir']))
  1446. {
  1447. if (!is_array($modSettings['attachmentUploadDir']))
  1448. $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
  1449. // The current directory, of course!
  1450. $current_attach_dir = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']];
  1451. }
  1452. else
  1453. $current_attach_dir = $modSettings['attachmentUploadDir'];
  1454. // If this isn't a new post, check the current attachments.
  1455. if (isset($_REQUEST['msg']))
  1456. {
  1457. $request = $smcFunc['db_query']('', '
  1458. SELECT COUNT(*), SUM(size)
  1459. FROM {db_prefix}attachments
  1460. WHERE id_msg = {int:id_msg}
  1461. AND attachment_type = {int:attachment_type}',
  1462. array(
  1463. 'id_msg' => (int) $_REQUEST['msg'],
  1464. 'attachment_type' => 0,
  1465. )
  1466. );
  1467. list ($quantity, $total_size) = $smcFunc['db_fetch_row']($request);
  1468. $smcFunc['db_free_result']($request);
  1469. }
  1470. else
  1471. {
  1472. $quantity = 0;
  1473. $total_size = 0;
  1474. }
  1475. if (!empty($_SESSION['temp_attachments']))
  1476. foreach ($_SESSION['temp_attachments'] as $attachID => $name)
  1477. {
  1478. if (preg_match('~^post_tmp_' . $user_info['id'] . '_\d+$~', $attachID) == 0)
  1479. continue;
  1480. if (!empty($_POST['attach_del']) && !in_array($attachID, $_POST['attach_del']))
  1481. {
  1482. unset($_SESSION['temp_attachments'][$attachID]);
  1483. @unlink($current_attach_dir . '/' . $attachID);
  1484. continue;
  1485. }
  1486. $_FILES['attachment']['tmp_name'][] = $attachID;
  1487. $_FILES['attachment']['name'][] = $name;
  1488. $_FILES['attachment']['size'][] = filesize($current_attach_dir . '/' . $attachID);
  1489. list ($_FILES['attachment']['width'][], $_FILES['attachment']['height'][]) = @getimagesize($current_attach_dir . '/' . $attachID);
  1490. unset($_SESSION['temp_attachments'][$attachID]);
  1491. }
  1492. if (!isset($_FILES['attachment']['name']))
  1493. $_FILES['attachment']['tmp_name'] = array();
  1494. $attachIDs = array();
  1495. foreach ($_FILES['attachment']['tmp_name'] as $n => $dummy)
  1496. {
  1497. if ($_FILES['attachment']['name'][$n] == '')
  1498. continue;
  1499. // Have we reached the maximum number of files we are allowed?
  1500. $quantity++;
  1501. if (!empty($modSettings['attachmentNumPerPostLimit']) && $quantity > $modSettings['attachmentNumPerPostLimit'])
  1502. {
  1503. checkSubmitOnce('free');
  1504. fatal_lang_error('attachments_limit_per_post', false, array($modSettings['attachmentNumPerPostLimit']));
  1505. }
  1506. // Check the total upload size for this post...
  1507. $total_size += $_FILES['attachment']['size'][$n];
  1508. if (!empty($modSettings['attachmentPostLimit']) && $total_size > $modSettings['attachmentPostLimit'] * 1024)
  1509. {
  1510. checkSubmitOnce('free');
  1511. fatal_lang_error('file_too_big', false, array($modSettings['attachmentPostLimit']));
  1512. }
  1513. $attachmentOptions = array(
  1514. 'post' => isset($_REQUEST['msg']) ? $_REQUEST['msg'] : 0,
  1515. 'poster' => $user_info['id'],
  1516. 'name' => $_FILES['attachment']['name'][$n],
  1517. 'tmp_name' => $_FILES['attachment']['tmp_name'][$n],
  1518. 'size' => $_FILES['attachment']['size'][$n],
  1519. 'approved' => !$modSettings['postmod_active'] || allowedTo('post_attachment'),
  1520. );
  1521. if (createAttachment($attachmentOptions))
  1522. {
  1523. $attachIDs[] = $attachmentOptions['id'];
  1524. if (!empty($attachmentOptions['thumb']))
  1525. $attachIDs[] = $attachmentOptions['thumb'];
  1526. }
  1527. else
  1528. {
  1529. if (in_array('could_not_upload', $attachmentOptions['errors']))
  1530. {
  1531. checkSubmitOnce('free');
  1532. fatal_lang_error('attach_timeout', 'critical');
  1533. }
  1534. if (in_array('too_large', $attachmentOptions['errors']))
  1535. {
  1536. checkSubmitOnce('free');
  1537. fatal_lang_error('file_too_big', false, array($modSettings['attachmentSizeLimit']));
  1538. }
  1539. if (in_array('bad_extension', $attachmentOptions['errors']))
  1540. {
  1541. checkSubmitOnce('free');
  1542. fatal_error($attachmentOptions['name'] . '.<br />' . $txt['cant_upload_type'] . ' ' . $modSettings['attachmentExtensions'] . '.', false);
  1543. }
  1544. if (in_array('directory_full', $attachmentOptions['errors']))
  1545. {
  1546. checkSubmitOnce('free');
  1547. fatal_lang_error('ran_out_of_space', 'critical');
  1548. }
  1549. if (in_array('bad_filename', $attachmentOptions['errors']))
  1550. {
  1551. checkSubmitOnce('free');
  1552. fatal_error(basename($attachmentOptions['name']) . '.<br />' . $txt['restricted_filename'] . '.', 'critical');
  1553. }
  1554. if (in_array('taken_filename', $attachmentOptions['errors']))
  1555. {
  1556. checkSubmitOnce('free');
  1557. fatal_lang_error('filename_exists');
  1558. }
  1559. if (in_array('bad_attachment', $attachmentOptions['errors']))
  1560. {
  1561. checkSubmitOnce('free');
  1562. fatal_lang_error('bad_attachment');
  1563. }
  1564. }
  1565. }
  1566. }
  1567. // Make the poll...
  1568. if (isset($_REQUEST['poll']))
  1569. {
  1570. // Create the poll.
  1571. $smcFunc['db_insert']('',
  1572. '{db_prefix}polls',
  1573. array(
  1574. 'question' => 'string-255', 'hide_results' => 'int', 'max_votes' => 'int', 'expire_time' => 'int', 'id_member' => 'int',
  1575. 'poster_name' => 'string-255', 'change_vote' => 'int', 'guest_vote' => 'int'
  1576. ),
  1577. array(
  1578. $_POST['question'], $_POST['poll_hide'], $_POST['poll_max_votes'], (empty($_POST['poll_expire']) ? 0 : time() + $_POST['poll_expire'] * 3600 * 24), $user_info['id'],
  1579. $_POST['guestname'], $_POST['poll_change_vote'], $_POST['poll_guest_vote'],
  1580. ),
  1581. array('id_poll')
  1582. );
  1583. $id_poll = $smcFunc['db_insert_id']('{db_prefix}polls', 'id_poll');
  1584. // Create each answer choice.
  1585. $i = 0;
  1586. $pollOptions = array();
  1587. foreach ($_POST['options'] as $option)
  1588. {
  1589. $pollOptions[] = array($id_poll, $i, $option);
  1590. $i++;
  1591. }
  1592. $smcFunc['db_insert']('insert',
  1593. '{db_prefix}poll_choices',
  1594. array('id_poll' => 'int', 'id_choice' => 'int', 'label' => 'string-255'),
  1595. $pollOptions,
  1596. array('id_poll', 'id_choice')
  1597. );
  1598. }
  1599. else
  1600. $id_poll = 0;
  1601. // Creating a new topic?
  1602. $newTopic = empty($_REQUEST['msg']) && empty($topic);
  1603. $_POST['icon'] = !empty($attachIDs) && $_POST['icon'] == 'xx' ? 'clip' : $_POST['icon'];
  1604. // Collect all parameters for the creation or modification of a post.
  1605. $msgOptions = array(
  1606. 'id' => empty($_REQUEST['msg']) ? 0 : (int) $_REQUEST['msg'],
  1607. 'subject' => $_POST['subject'],
  1608. 'body' => $_POST['message'],
  1609. 'icon' => preg_replace('~[\./\\\\*:"\'<>]~', '', $_POST['icon']),
  1610. 'smileys_enabled' => !isset($_POST['ns']),
  1611. 'attachments' => empty($attachIDs) ? array() : $attachIDs,
  1612. 'approved' => $becomesApproved,
  1613. );
  1614. $topicOptions = array(
  1615. 'id' => empty($topic) ? 0 : $topic,
  1616. 'board' => $board,
  1617. 'poll' => isset($_REQUEST['poll']) ? $id_poll : null,
  1618. 'lock_mode' => isset($_POST['lock']) ? (int) $_POST['lock'] : null,
  1619. 'sticky_mode' => isset($_POST['sticky']) && !empty($modSettings['enableStickyTopics']) ? (int) $_POST['sticky'] : null,
  1620. 'mark_as_read' => true,
  1621. 'is_approved' => !$modSettings['postmod_active'] || empty($topic) || !empty($board_info['cur_topic_approved']),
  1622. );
  1623. $posterOptions = array(
  1624. 'id' => $user_info['id'],
  1625. 'name' => $_POST['guestname'],
  1626. 'email' => $_POST['email'],
  1627. 'update_post_count' => !$user_info['is_guest'] && !isset($_REQUEST['msg']) && $board_info['posts_count'],
  1628. );
  1629. // This is an already existing message. Edit it.
  1630. if (!empty($_REQUEST['msg']))
  1631. {
  1632. // Have admins allowed people to hide their screwups?
  1633. if (time() - $row['poster_time'] > $modSettings['edit_wait_time'] || $user_info['id'] != $row['id_member'])
  1634. {
  1635. $msgOptions['modify_time'] = time();
  1636. $msgOptions['modify_name'] = $user_info['name'];
  1637. }
  1638. // This will save some time...
  1639. if (empty($approve_has_changed))
  1640. unset($msgOptions['approved']);
  1641. modifyPost($msgOptions, $topicOptions, $posterOptions);
  1642. }
  1643. // This is a new topic or an already existing one. Save it.
  1644. else
  1645. {
  1646. createPost($msgOptions, $topicOptions, $posterOptions);
  1647. if (isset($topicOptions['id']))
  1648. $topic = $topicOptions['id'];
  1649. }
  1650. // Editing or posting an event?
  1651. if (isset($_POST['calendar']) && (!isset($_REQUEST['eventid']) || $_REQUEST['eventid'] == -1))
  1652. {
  1653. require_once($sourcedir . '/Subs-Calendar.php');
  1654. // Make sure they can link an event to this post.
  1655. canLinkEvent();
  1656. // Insert the event.
  1657. $eventOptions = array(
  1658. 'board' => $board,
  1659. 'topic' => $topic,
  1660. 'title' => $_POST['evtitle'],
  1661. 'member' => $user_info['id'],
  1662. 'start_date' => sprintf('%04d-%02d-%02d', $_POST['year'], $_POST['month'], $_POST['day']),
  1663. 'span' => isset($_POST['span']) && $_POST['span'] > 0 ? min((int) $modSettings['cal_maxspan'], (int) $_POST['span'] - 1) : 0,
  1664. );
  1665. insertEvent($eventOptions);
  1666. }
  1667. elseif (isset($_POST['calendar']))
  1668. {
  1669. $_REQUEST['eventid'] = (int) $_REQUEST['eventid'];
  1670. // Validate the post...
  1671. require_once($sourcedir . '/Subs-Calendar.php');
  1672. validateEventPost();
  1673. // If you're not allowed to edit any events, you have to be the poster.
  1674. if (!allowedTo('calendar_edit_any'))
  1675. {
  1676. // Get the event's poster.
  1677. $request = $smcFunc['db_query']('', '
  1678. SELECT id_member
  1679. FROM {db_prefix}calendar
  1680. WHERE id_event = {int:id_event}',
  1681. array(
  1682. 'id_event' => $_REQUEST['eventid'],
  1683. )
  1684. );
  1685. $row2 = $smcFunc['db_fetch_assoc']($request);
  1686. $smcFunc['db_free_result']($request);
  1687. // Silly hacker, Trix are for kids. ...probably trademarked somewhere, this is FAIR USE! (parody...)
  1688. isAllowedTo('calendar_edit_' . ($row2['id_member'] == $user_info['id'] ? 'own' : 'any'));
  1689. }
  1690. // Delete it?
  1691. if (isset($_REQUEST['deleteevent']))
  1692. $smcFunc['db_query']('', '
  1693. DELETE FROM {db_prefix}calendar
  1694. WHERE id_event = {int:id_event}',
  1695. array(
  1696. 'id_event' => $_REQUEST['eventid'],
  1697. )
  1698. );
  1699. // ... or just update it?
  1700. else
  1701. {
  1702. $span = !empty($modSettings['cal_allowspan']) && !empty($_REQUEST['span']) ? min((int) $modSettings['cal_maxspan'], (int) $_REQUEST['span'] - 1) : 0;
  1703. $start_time = mktime(0, 0, 0, (int) $_REQUEST['month'], (int) $_REQUEST['day'], (int) $_REQUEST['year']);
  1704. $smcFunc['db_query']('', '
  1705. UPDATE {db_prefix}calendar
  1706. SET end_date = {date:end_date},
  1707. start_date = {date:start_date},
  1708. title = {string:title}
  1709. WHERE id_event = {int:id_event}',
  1710. array(
  1711. 'end_date' => strftime('%Y-%m-%d', $start_time + $span * 86400),
  1712. 'start_date' => strftime('%Y-%m-%d', $start_time),
  1713. 'id_event' => $_REQUEST['eventid'],
  1714. 'title' => $smcFunc['htmlspecialchars']($_REQUEST['evtitle'], ENT_QUOTES),
  1715. )
  1716. );
  1717. }
  1718. updateSettings(array(
  1719. 'calendar_updated' => time(),
  1720. ));
  1721. }
  1722. // Marking read should be done even for editing messages....
  1723. // Mark all the parents read. (since you just posted and they will be unread.)
  1724. if (!$user_info['is_guest'] && !empty($board_info['parent_boards']))
  1725. {
  1726. $smcFunc['db_query']('', '
  1727. UPDATE {db_prefix}log_boards
  1728. SET id_msg = {int:id_msg}
  1729. WHERE id_member = {int:current_member}
  1730. AND id_board IN ({array_int:board_list})',
  1731. array(
  1732. 'current_member' => $user_info['id'],
  1733. 'board_list' => array_keys($board_info['parent_boards']),
  1734. 'id_msg' => $modSettings['maxMsgID'],
  1735. )
  1736. );
  1737. }
  1738. // Turn notification on or off. (note this just blows smoke if it's already on or off.)
  1739. if (!empty($_POST['notify']) && allowedTo('mark_any_notify'))
  1740. {
  1741. $smcFunc['db_insert']('ignore',
  1742. '{db_prefix}log_notify',
  1743. array('id_member' => 'int', 'id_topic' => 'int', 'id_board' => 'int'),
  1744. array($user_info['id'], $topic, 0),
  1745. array('id_member', 'id_topic', 'id_board')
  1746. );
  1747. }
  1748. elseif (!$newTopic)
  1749. $smcFunc['db_query']('', '
  1750. DELETE FROM {db_prefix}log_notify
  1751. WHERE id_member = {int:current_member}
  1752. AND id_topic = {int:current_topic}',
  1753. array(
  1754. 'current_member' => $user_info['id'],
  1755. 'current_topic' => $topic,
  1756. )
  1757. );
  1758. // Log an act of moderation - modifying.
  1759. if (!empty($moderationAction))
  1760. logAction('modify', array('topic' => $topic, 'message' => (int) $_REQUEST['msg'], 'member' => $row['id_member'], 'board' => $board));
  1761. if (isset($_POST['lock']) && $_POST['lock'] != 2)
  1762. logAction('lock', array('topic' => $topicOptions['id'], 'board' => $topicOptions['board']));
  1763. if (isset($_POST['sticky']) && !empty($modSettings['enableStickyTopics']))
  1764. logAction('sticky', array('topic' => $topicOptions['id'], 'board' => $topicOptions['board']));
  1765. // Notify any members who have notification turned on for this topic - only do this if it's going to be approved(!)
  1766. if ($becomesApproved)
  1767. {
  1768. if ($newTopic)
  1769. {
  1770. $notifyData = array(
  1771. 'body' => $_POST['message'],
  1772. 'subject' => $_POST['subject'],
  1773. 'name' => $user_info['name'],
  1774. 'poster' => $user_info['id'],
  1775. 'msg' => $msgOptions['id'],
  1776. 'board' => $board,
  1777. 'topic' => $topic,
  1778. );
  1779. notifyMembersBoard($notifyData);
  1780. }
  1781. elseif (empty($_REQUEST['msg']))
  1782. {
  1783. // Only send it to everyone if the topic is approved, otherwise just to the topic starter if they want it.
  1784. if ($topic_info['approved'])
  1785. sendNotifications($topic, 'reply');
  1786. else
  1787. sendNotifications($topic, 'reply', array(), $topic_info['id_member_started']);
  1788. }
  1789. }
  1790. // Returning to the topic?
  1791. if (!empty($_REQUEST['goback']))
  1792. {
  1793. // Mark the board as read.... because it might get confusing otherwise.
  1794. $smcFunc['db_query']('', '
  1795. UPDATE {db_prefix}log_boards
  1796. SET id_msg = {int:maxMsgID}
  1797. WHERE id_member = {int:current_member}
  1798. AND id_board = {int:current_board}',
  1799. array(
  1800. 'current_board' => $board,
  1801. 'current_member' => $user_info['id'],
  1802. 'maxMsgID' => $modSettings['maxMsgID'],
  1803. )
  1804. );
  1805. }
  1806. if ($board_info['num_topics'] == 0)
  1807. cache_put_data('board-' . $board, null, 120);
  1808. if (!empty($_POST['announce_topic']))
  1809. redirectexit('action=announce;sa=selectgroup;topic=' . $topic . (!empty($_POST['move']) && allowedTo('move_any') ? ';move' : '') . (empty($_REQUEST['goback']) ? '' : ';goback'));
  1810. if (!empty($_POST['move']) && allowedTo('move_any'))
  1811. redirectexit('action=movetopic;topic=' . $topic . '.0' . (empty($_REQUEST['goback']) ? '' : ';goback'));
  1812. // Return to post if the mod is on.
  1813. if (isset($_REQUEST['msg']) && !empty($_REQUEST['goback']))
  1814. redirectexit('topic=' . $topic . '.msg' . $_REQUEST['msg'] . '#msg' . $_REQUEST['msg'], $context['browser']['is_ie']);
  1815. elseif (!empty($_REQUEST['goback']))
  1816. redirectexit('topic=' . $topic . '.new#new', $context['browser']['is_ie']);
  1817. // Dut-dut-duh-duh-DUH-duh-dut-duh-duh! *dances to the Final Fantasy Fanfare...*
  1818. else
  1819. redirectexit('board=' . $board . '.0');
  1820. }
  1821. // General function for topic announcements.
  1822. function AnnounceTopic()
  1823. {
  1824. global $context, $txt, $topic;
  1825. isAllowedTo('announce_topic');
  1826. validateSession();
  1827. if (empty($topic))
  1828. fatal_lang_error('topic_gone', false);
  1829. loadLanguage('Post');
  1830. loadTemplate('Post');
  1831. $subActions = array(
  1832. 'selectgroup' => 'AnnouncementSelectMembergroup',
  1833. 'send' => 'AnnouncementSend',
  1834. );
  1835. $context['page_title'] = $txt['announce_topic'];
  1836. // Call the function based on the sub-action.
  1837. $subActions[isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'selectgroup']();
  1838. }
  1839. // Allow a user to chose the membergroups to send the announcement to.
  1840. function AnnouncementSelectMembergroup()
  1841. {
  1842. global $txt, $context, $topic, $board, $board_info, $smcFunc;
  1843. $groups = array_merge($board_info['groups'], array(1));
  1844. foreach ($groups as $id => $group)
  1845. $groups[$id] = (int) $group;
  1846. $context['groups'] = array();
  1847. if (in_array(0, $groups))
  1848. {
  1849. $context['groups'][0] = array(
  1850. 'id' => 0,
  1851. 'name' => $txt['announce_regular_members'],
  1852. 'member_count' => 'n/a',
  1853. );
  1854. }
  1855. // Get all membergroups that have access to the board the announcement was made on.
  1856. $request = $smcFunc['db_query']('', '
  1857. SELECT mg.id_group, COUNT(mem.id_member) AS num_members
  1858. FROM {db_prefix}membergroups AS mg
  1859. LEFT JOIN {db_prefix}members AS mem ON (mem.id_group = mg.id_group OR FIND_IN_SET(mg.id_group, mem.additional_groups) != 0 OR mg.id_group = mem.id_post_group)
  1860. WHERE mg.id_group IN ({array_int:group_list})
  1861. GROUP BY mg.id_group',
  1862. array(
  1863. 'group_list' => $groups,
  1864. 'newbie_id_group' => 4,
  1865. )
  1866. );
  1867. while ($row = $smcFunc['db_fetch_assoc']($request))
  1868. {
  1869. $context['groups'][$row['id_group']] = array(
  1870. 'id' => $row['id_group'],
  1871. 'name' => '',
  1872. 'member_count' => $row['num_members'],
  1873. );
  1874. }
  1875. $smcFunc['db_free_result']($request);
  1876. // Now get the membergroup names.
  1877. $request = $smcFunc['db_query']('', '
  1878. SELECT id_group, group_name
  1879. FROM {db_prefix}membergroups
  1880. WHERE id_group IN ({array_int:group_list})',
  1881. array(
  1882. 'group_list' => $groups,
  1883. )
  1884. );
  1885. while ($row = $smcFunc['db_fetch_assoc']($request))
  1886. $context['groups'][$row['id_group']]['name'] = $row['group_name'];
  1887. $smcFunc['db_free_result']($request);
  1888. // Get the subject of the topic we're about to announce.
  1889. $request = $smcFunc['db_query']('', '
  1890. SELECT m.subject
  1891. FROM {db_prefix}topics AS t
  1892. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  1893. WHERE t.id_topic = {int:current_topic}',
  1894. array(
  1895. 'current_topic' => $topic,
  1896. )
  1897. );
  1898. list ($context['topic_subject']) = $smcFunc['db_fetch_row']($request);
  1899. $smcFunc['db_free_result']($request);
  1900. censorText($context['announce_topic']['subject']);
  1901. $context['move'] = isset($_REQUEST['move']) ? 1 : 0;
  1902. $context['go_back'] = isset($_REQUEST['goback']) ? 1 : 0;
  1903. $context['sub_template'] = 'announce';
  1904. }
  1905. // Send the announcement in chunks.
  1906. function AnnouncementSend()
  1907. {
  1908. global $topic, $board, $board_info, $context, $modSettings;
  1909. global $language, $scripturl, $txt, $user_info, $sourcedir, $smcFunc;
  1910. checkSession();
  1911. // !!! Might need an interface?
  1912. $chunkSize = empty($modSettings['mail_queue']) ? 50 : 500;
  1913. $context['start'] = empty($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'];
  1914. $groups = array_merge($board_info['groups'], array(1));
  1915. if (isset($_POST['membergroups']))
  1916. $_POST['who'] = explode(',', $_POST['membergroups']);
  1917. // Check whether at least one membergroup was selected.
  1918. if (empty($_POST['who']))
  1919. fatal_lang_error('no_membergroup_selected');
  1920. // Make sure all membergroups are integers and can access the board of the announcement.
  1921. foreach ($_POST['who'] as $id => $mg)
  1922. $_POST['who'][$id] = in_array((int) $mg, $groups) ? (int) $mg : 0;
  1923. // Get the topic subject and censor it.
  1924. $request = $smcFunc['db_query']('', '
  1925. SELECT m.id_msg, m.subject, m.body
  1926. FROM {db_prefix}topics AS t
  1927. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  1928. WHERE t.id_topic = {int:current_topic}',
  1929. array(
  1930. 'current_topic' => $topic,
  1931. )
  1932. );
  1933. list ($id_msg, $context['topic_subject'], $message) = $smcFunc['db_fetch_row']($request);
  1934. $smcFunc['db_free_result']($request);
  1935. censorText($context['topic_subject']);
  1936. censorText($message);
  1937. $message = trim(un_htmlspecialchars(strip_tags(strtr(parse_bbc($message, false, $id_msg), array('<br />' => "\n", '</div>' => "\n", '</li>' => "\n", '&#91;' => '[', '&#93;' => ']')))));
  1938. // We need this in order to be able send emails.
  1939. require_once($sourcedir . '/Subs-Post.php');
  1940. // Select the email addresses for this batch.
  1941. $request = $smcFunc['db_query']('', '
  1942. SELECT mem.id_member, mem.email_address, mem.lngfile
  1943. FROM {db_prefix}members AS mem
  1944. WHERE mem.id_member != {int:current_member}' . (!empty($modSettings['allow_disableAnnounce']) ? '
  1945. AND mem.notify_announcements = {int:notify_announcements}' : '') . '
  1946. AND mem.is_activated = {int:is_activated}
  1947. AND (mem.id_group IN ({array_int:group_list}) OR mem.id_post_group IN ({array_int:group_list}) OR FIND_IN_SET({raw:additional_group_list}, mem.additional_groups) != 0)
  1948. AND mem.id_member > {int:start}
  1949. ORDER BY mem.id_member
  1950. LIMIT ' . $chunkSize,
  1951. array(
  1952. 'current_member' => $user_info['id'],
  1953. 'group_list' => $_POST['who'],
  1954. 'notify_announcements' => 1,
  1955. 'is_activated' => 1,
  1956. 'start' => $context['start'],
  1957. 'additional_group_list' => implode(', mem.additional_groups) != 0 OR FIND_IN_SET(', $_POST['who']),
  1958. )
  1959. );
  1960. // All members have received a mail. Go to the next screen.
  1961. if ($smcFunc['db_num_rows']($request) == 0)
  1962. {
  1963. if (!empty($_REQUEST['move']) && allowedTo('move_any'))
  1964. redirectexit('action=movetopic;topic=' . $topic . '.0' . (empty($_REQUEST['goback']) ? '' : ';goback'));
  1965. elseif (!empty($_REQUEST['goback']))
  1966. redirectexit('topic=' . $topic . '.new;boardseen#new', $context['browser']['is_ie']);
  1967. else
  1968. redirectexit('board=' . $board . '.0');
  1969. }
  1970. // Loop through all members that'll receive an announcement in this batch.
  1971. while ($row = $smcFunc['db_fetch_assoc']($request))
  1972. {
  1973. $cur_language = empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'];
  1974. // If the language wasn't defined yet, load it and compose a notification message.
  1975. if (!isset($announcements[$cur_language]))
  1976. {
  1977. $replacements = array(
  1978. 'TOPICSUBJECT' => $context['topic_subject'],
  1979. 'MESSAGE' => $message,
  1980. 'TOPICLINK' => $scripturl . '?topic=' . $topic . '.0',
  1981. );
  1982. $emaildata = loadEmailTemplate('new_announcement', $replacements, $cur_language);
  1983. $announcements[$cur_language] = array(
  1984. 'subject' => $emaildata['subject'],
  1985. 'body' => $emaildata['body'],
  1986. 'recipients' => array(),
  1987. );
  1988. }
  1989. $announcements[$cur_language]['recipients'][$row['id_member']] = $row['email_address'];
  1990. $context['start'] = $row['id_member'];
  1991. }
  1992. $smcFunc['db_free_result']($request);
  1993. // For each language send a different mail - low priority...
  1994. foreach ($announcements as $lang => $mail)
  1995. sendmail($mail['recipients'], $mail['subject'], $mail['body'], null, null, false, 5);
  1996. $context['percentage_done'] = round(100 * $context['start'] / $modSettings['latestMember'], 1);
  1997. $context['move'] = empty($_REQUEST['move']) ? 0 : 1;
  1998. $context['go_back'] = empty($_REQUEST['goback']) ? 0 : 1;
  1999. $context['membergroups'] = implode(',', $_POST['who']);
  2000. $context['sub_template'] = 'announcement_send';
  2001. // Go back to the correct language for the user ;).
  2002. if (!empty($modSettings['userLanguage']))
  2003. loadLanguage('Post');
  2004. }
  2005. // Notify members of a new post.
  2006. function notifyMembersBoard(&$topicData)
  2007. {
  2008. global $txt, $scripturl, $language, $user_info;
  2009. global $modSettings, $sourcedir, $board, $smcFunc, $context;
  2010. require_once($sourcedir . '/Subs-Post.php');
  2011. // Do we have one or lots of topics?
  2012. if (isset($topicData['body']))
  2013. $topicData = array($topicData);
  2014. // Find out what boards we have... and clear out any rubbish!
  2015. $boards = array();
  2016. foreach ($topicData as $key => $topic)
  2017. {
  2018. if (!empty($topic['board']))
  2019. $boards[$topic['board']][] = $key;
  2020. else
  2021. {
  2022. unset($topic[$key]);
  2023. continue;
  2024. }
  2025. // Censor the subject and body...
  2026. censorText($topicData[$key]['subject']);
  2027. censorText($topicData[$key]['body']);
  2028. $topicData[$key]['subject'] = un_htmlspecialchars($topicData[$key]['subject']);
  2029. $topicData[$key]['body'] = trim(un_htmlspecialchars(strip_tags(strtr(parse_bbc($topicData[$key]['body'], false), array('<br />' => "\n", '</div>' => "\n", '</li>' => "\n", '&#91;' => '[', '&#93;' => ']')))));
  2030. }
  2031. // Just the board numbers.
  2032. $board_index = array_unique(array_keys($boards));
  2033. if (empty($board_index))
  2034. return;
  2035. // Yea, we need to add this to the digest queue.
  2036. $digest_insert = array();
  2037. foreach ($topicData as $id => $data)
  2038. $digest_insert[] = array($data['topic'], $data['msg'], 'topic', $user_info['id']);
  2039. $smcFunc['db_insert']('',
  2040. '{db_prefix}log_digest',
  2041. array(
  2042. 'id_topic' => 'int', 'id_msg' => 'int', 'note_type' => 'string', 'exclude' => 'int',
  2043. ),
  2044. $digest_insert,
  2045. array()
  2046. );
  2047. // Find the members with notification on for these boards.
  2048. $members = $smcFunc['db_query']('', '
  2049. SELECT
  2050. mem.id_member, mem.email_address, mem.notify_regularity, mem.notify_send_body, mem.lngfile,
  2051. ln.sent, ln.id_board, mem.id_group, mem.additional_groups, b.member_groups,
  2052. mem.id_post_group
  2053. FROM {db_prefix}log_notify AS ln
  2054. INNER JOIN {db_prefix}boards AS b ON (b.id_board = ln.id_board)
  2055. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = ln.id_member)
  2056. WHERE ln.id_board IN ({array_int:board_list})
  2057. AND mem.id_member != {int:current_member}
  2058. AND mem.is_activated = {int:is_activated}
  2059. AND mem.notify_types != {int:notify_types}
  2060. AND mem.notify_regularity < {int:notify_regularity}
  2061. ORDER BY mem.lngfile',
  2062. array(
  2063. 'current_member' => $user_info['id'],
  2064. 'board_list' => $board_index,
  2065. 'is_activated' => 1,
  2066. 'notify_types' => 4,
  2067. 'notify_regularity' => 2,
  2068. )
  2069. );
  2070. while ($rowmember = $smcFunc['db_fetch_assoc']($members))
  2071. {
  2072. if ($rowmember['id_group'] != 1)
  2073. {
  2074. $allowed = explode(',', $rowmember['member_groups']);
  2075. $rowmember['additional_groups'] = explode(',', $rowmember['additional_groups']);
  2076. $rowmember['additional_groups'][] = $rowmember['id_group'];
  2077. $rowmember['additional_groups'][] = $rowmember['id_post_group'];
  2078. if (count(array_intersect($allowed, $rowmember['additional_groups'])) == 0)
  2079. continue;
  2080. }
  2081. $langloaded = loadLanguage('EmailTemplates', empty($rowmember['lngfile']) || empty($modSettings['userLanguage']) ? $language : $rowmember['lngfile'], false);
  2082. // Now loop through all the notifications to send for this board.
  2083. if (empty($boards[$rowmember['id_board']]))
  2084. continue;
  2085. $sentOnceAlready = 0;
  2086. foreach ($boards[$rowmember['id_board']] as $key)
  2087. {
  2088. // Don't notify the guy who started the topic!
  2089. //!!! In this case actually send them a "it's approved hooray" email
  2090. if ($topicData[$key]['poster'] == $rowmember['id_member'])
  2091. continue;
  2092. // Setup the string for adding the body to the message, if a user wants it.
  2093. $send_body = empty($modSettings['disallow_sendBody']) && !empty($rowmember['notify_send_body']);
  2094. $replacements = array(
  2095. 'TOPICSUBJECT' => $topicData[$key]['subject'],
  2096. 'TOPICLINK' => $scripturl . '?topic=' . $topicData[$key]['topic'] . '.new#new',
  2097. 'MESSAGE' => $topicData[$key]['body'],
  2098. 'UNSUBSCRIBELINK' => $scripturl . '?action=notifyboard;board=' . $topicData[$key]['board'] . '.0',
  2099. );
  2100. if (!$send_body)
  2101. unset($replacements['MESSAGE']);
  2102. // Figure out which email to send off
  2103. $emailtype = '';
  2104. // Send only if once is off or it's on and it hasn't been sent.
  2105. if (!empty($rowmember['notify_regularity']) && !$sentOnceAlready && empty($rowmember['sent']))
  2106. $emailtype = 'notify_boards_once';
  2107. elseif (empty($rowmember['notify_regularity']))
  2108. $emailtype = 'notify_boards';
  2109. if (!empty($emailtype))
  2110. {
  2111. $emailtype .= $send_body ? '_body' : '';
  2112. $emaildata = loadEmailTemplate($emailtype, $replacements, $langloaded);
  2113. sendmail($rowmember['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 3);
  2114. }
  2115. $sentOnceAlready = 1;
  2116. }
  2117. }
  2118. $smcFunc['db_free_result']($members);
  2119. // Sent!
  2120. $smcFunc['db_query']('', '
  2121. UPDATE {db_prefix}log_notify
  2122. SET sent = {int:is_sent}
  2123. WHERE id_board IN ({array_int:board_list})
  2124. AND id_member != {int:current_member}',
  2125. array(
  2126. 'current_member' => $user_info['id'],
  2127. 'board_list' => $board_index,
  2128. 'is_sent' => 1,
  2129. )
  2130. );
  2131. }
  2132. // Get the topic for display purposes.
  2133. function getTopic()
  2134. {
  2135. global $topic, $modSettings, $context, $smcFunc, $counter, $options;
  2136. if (isset($_REQUEST['xml']))
  2137. $limit = '
  2138. LIMIT ' . (empty($context['new_replies']) ? '0' : $context['new_replies']);
  2139. else
  2140. $limit = empty($modSettings['topicSummaryPosts']) ? '' : '
  2141. LIMIT ' . (int) $modSettings['topicSummaryPosts'];
  2142. // If you're modifying, get only those posts before the current one. (otherwise get all.)
  2143. $request = $smcFunc['db_query']('', '
  2144. SELECT
  2145. IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
  2146. m.body, m.smileys_enabled, m.id_msg, m.id_member
  2147. FROM {db_prefix}messages AS m
  2148. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  2149. WHERE m.id_topic = {int:current_topic}' . (isset($_REQUEST['msg']) ? '
  2150. AND m.id_msg < {int:id_msg}' : '') .(!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  2151. AND m.approved = {int:approved}') . '
  2152. ORDER BY m.id_msg DESC' . $limit,
  2153. array(
  2154. 'current_topic' => $topic,
  2155. 'id_msg' => isset($_REQUEST['msg']) ? (int) $_REQUEST['msg'] : 0,
  2156. 'approved' => 1,
  2157. )
  2158. );
  2159. $context['previous_posts'] = array();
  2160. while ($row = $smcFunc['db_fetch_assoc']($request))
  2161. {
  2162. // Censor, BBC, ...
  2163. censorText($row['body']);
  2164. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  2165. // ...and store.
  2166. $context['previous_posts'][] = array(
  2167. 'counter' => $counter++,
  2168. 'alternate' => $counter % 2,
  2169. 'poster' => $row['poster_name'],
  2170. 'message' => $row['body'],
  2171. 'time' => timeformat($row['poster_time']),
  2172. 'timestamp' => forum_time(true, $row['poster_time']),
  2173. 'id' => $row['id_msg'],
  2174. 'is_new' => !empty($context['new_replies']),
  2175. 'is_ignored' => !empty($modSettings['enable_buddylist']) && !empty($options['posts_apply_ignore_list']) && in_array($row['id_member'], $context['user']['ignoreusers']),
  2176. );
  2177. if (!empty($context['new_replies']))
  2178. $context['new_replies']--;
  2179. }
  2180. $smcFunc['db_free_result']($request);
  2181. }
  2182. function QuoteFast()
  2183. {
  2184. global $modSettings, $user_info, $txt, $settings, $context;
  2185. global $sourcedir, $smcFunc;
  2186. loadLanguage('Post');
  2187. if (!isset($_REQUEST['xml']))
  2188. loadTemplate('Post');
  2189. include_once($sourcedir . '/Subs-Post.php');
  2190. $moderate_boards = boardsAllowedTo('moderate_board');
  2191. // Where we going if we need to?
  2192. $context['post_box_name'] = isset($_GET['pb']) ? $_GET['pb'] : '';
  2193. $request = $smcFunc['db_query']('', '
  2194. SELECT IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.body, m.id_topic, m.subject,
  2195. m.id_board, m.id_member, m.approved
  2196. FROM {db_prefix}messages AS m
  2197. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  2198. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND {query_see_board})
  2199. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  2200. WHERE m.id_msg = {int:id_msg}' . (isset($_REQUEST['modify']) || (!empty($moderate_boards) && $moderate_boards[0] == 0) ? '' : '
  2201. AND (t.locked = {int:not_locked}' . (empty($moderate_boards) ? '' : ' OR b.id_board IN ({array_int:moderation_board_list})') . ')') . '
  2202. LIMIT 1',
  2203. array(
  2204. 'current_member' => $user_info['id'],
  2205. 'moderation_board_list' => $moderate_boards,
  2206. 'id_msg' => (int) $_REQUEST['quote'],
  2207. 'not_locked' => 0,
  2208. )
  2209. );
  2210. $context['close_window'] = $smcFunc['db_num_rows']($request) == 0;
  2211. $row = $smcFunc['db_fetch_assoc']($request);
  2212. $smcFunc['db_free_result']($request);
  2213. $context['sub_template'] = 'quotefast';
  2214. if (!empty($row))
  2215. $can_view_post = $row['approved'] || ($row['id_member'] != 0 && $row['id_member'] == $user_info['id']) || allowedTo('approve_posts', $row['id_board']);
  2216. if (!empty($can_view_post))
  2217. {
  2218. // Remove special formatting we don't want anymore.
  2219. $row['body'] = un_preparsecode($row['body']);
  2220. // Censor the message!
  2221. censorText($row['body']);
  2222. $row['body'] = preg_replace('~<br ?/?' . '>~i', "\n", $row['body']);
  2223. // Want to modify a single message by double clicking it?
  2224. if (isset($_REQUEST['modify']))
  2225. {
  2226. censorText($row['subject']);
  2227. $context['sub_template'] = 'modifyfast';
  2228. $context['message'] = array(
  2229. 'id' => $_REQUEST['quote'],
  2230. 'body' => $row['body'],
  2231. 'subject' => addcslashes($row['subject'], '"'),
  2232. );
  2233. return;
  2234. }
  2235. // Remove any nested quotes.
  2236. if (!empty($modSettings['removeNestedQuotes']))
  2237. $row['body'] = preg_replace(array('~\n?\[quote.*?\].+?\[/quote\]\n?~is', '~^\n~', '~\[/quote\]~'), '', $row['body']);
  2238. // Make the body HTML if need be.
  2239. if (!empty($_REQUEST['mode']))
  2240. {
  2241. require_once($sourcedir . '/Subs-Editor.php');
  2242. $row['body'] = strtr($row['body'], array('&lt;' => '#smlt#', '&gt;' => '#smgt#', '&amp;' => '#smamp#'));
  2243. $row['body'] = bbc_to_html($row['body']);
  2244. $lb = '<br />';
  2245. }
  2246. else
  2247. $lb = "\n";
  2248. // Add a quote string on the front and end.
  2249. $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]';
  2250. $context['quote']['text'] = strtr(un_htmlspecialchars($context['quote']['xml']), array('\'' => '\\\'', '\\' => '\\\\', "\n" => '\\n', '</script>' => '</\' + \'script>'));
  2251. $context['quote']['xml'] = strtr($context['quote']['xml'], array('&nbsp;' => '&#160;', '<' => '&lt;', '>' => '&gt;'));
  2252. $context['quote']['mozilla'] = strtr($smcFunc['htmlspecialchars']($context['quote']['text']), array('&quot;' => '"'));
  2253. }
  2254. // !!! Needs a nicer interface.
  2255. // In case our message has been removed in the meantime.
  2256. elseif (isset($_REQUEST['modify']))
  2257. {
  2258. $context['sub_template'] = 'modifyfast';
  2259. $context['message'] = array(
  2260. 'id' => 0,
  2261. 'body' => '',
  2262. 'subject' => '',
  2263. );
  2264. }
  2265. else
  2266. $context['quote'] = array(
  2267. 'xml' => '',
  2268. 'mozilla' => '',
  2269. 'text' => '',
  2270. );
  2271. }
  2272. function JavaScriptModify()
  2273. {
  2274. global $sourcedir, $modSettings, $board, $topic, $txt;
  2275. global $user_info, $context, $smcFunc, $language;
  2276. // We have to have a topic!
  2277. if (empty($topic))
  2278. obExit(false);
  2279. checkSession('get');
  2280. require_once($sourcedir . '/Subs-Post.php');
  2281. // Assume the first message if no message ID was given.
  2282. $request = $smcFunc['db_query']('', '
  2283. SELECT
  2284. t.locked, t.num_replies, t.id_member_started, t.id_first_msg,
  2285. m.id_msg, m.id_member, m.poster_time, m.subject, m.smileys_enabled, m.body, m.icon,
  2286. m.modified_time, m.modified_name, m.approved
  2287. FROM {db_prefix}messages AS m
  2288. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
  2289. WHERE m.id_msg = {raw:id_msg}
  2290. AND m.id_topic = {int:current_topic}' . (allowedTo('approve_posts') ? '' : (!$modSettings['postmod_active'] ? '
  2291. AND (m.id_member != {int:guest_id} AND m.id_member = {int:current_member})' : '
  2292. AND (m.approved = {int:is_approved} OR (m.id_member != {int:guest_id} AND m.id_member = {int:current_member}))')),
  2293. array(
  2294. 'current_member' => $user_info['id'],
  2295. 'current_topic' => $topic,
  2296. 'id_msg' => empty($_REQUEST['msg']) ? 't.id_first_msg' : (int) $_REQUEST['msg'],
  2297. 'is_approved' => 1,
  2298. 'guest_id' => 0,
  2299. )
  2300. );
  2301. if ($smcFunc['db_num_rows']($request) == 0)
  2302. fatal_lang_error('no_board', false);
  2303. $row = $smcFunc['db_fetch_assoc']($request);
  2304. $smcFunc['db_free_result']($request);
  2305. // Change either body or subject requires permissions to modify messages.
  2306. if (isset($_POST['message']) || isset($_POST['subject']) || isset($_REQUEST['icon']))
  2307. {
  2308. if (!empty($row['locked']))
  2309. isAllowedTo('moderate_board');
  2310. if ($row['id_member'] == $user_info['id'] && !allowedTo('modify_any'))
  2311. {
  2312. if ((!$modSettings['postmod_active'] || $row['approved']) && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + ($modSettings['edit_disable_time'] + 5) * 60 < time())
  2313. fatal_lang_error('modify_post_time_passed', false);
  2314. elseif ($row['id_member_started'] == $user_info['id'] && !allowedTo('modify_own'))
  2315. isAllowedTo('modify_replies');
  2316. else
  2317. isAllowedTo('modify_own');
  2318. }
  2319. // Otherwise, they're locked out; someone who can modify the replies is needed.
  2320. elseif ($row['id_member_started'] == $user_info['id'] && !allowedTo('modify_any'))
  2321. isAllowedTo('modify_replies');
  2322. else
  2323. isAllowedTo('modify_any');
  2324. // Only log this action if it wasn't your message.
  2325. $moderationAction = $row['id_member'] != $user_info['id'];
  2326. }
  2327. $post_errors = array();
  2328. if (isset($_POST['subject']) && $smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['subject'])) !== '')
  2329. {
  2330. $_POST['subject'] = strtr($smcFunc['htmlspecialchars']($_POST['subject']), array("\r" => '', "\n" => '', "\t" => ''));
  2331. // Maximum number of characters.
  2332. if ($smcFunc['strlen']($_POST['subject']) > 100)
  2333. $_POST['subject'] = $smcFunc['substr']($_POST['subject'], 0, 100);
  2334. }
  2335. elseif (isset($_POST['subject']))
  2336. {
  2337. $post_errors[] = 'no_subject';
  2338. unset($_POST['subject']);
  2339. }
  2340. if (isset($_POST['message']))
  2341. {
  2342. if ($smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['message'])) === '')
  2343. {
  2344. $post_errors[] = 'no_message';
  2345. unset($_POST['message']);
  2346. }
  2347. elseif (!empty($modSettings['max_messageLength']) && $smcFunc['strlen']($_POST['message']) > $modSettings['max_messageLength'])
  2348. {
  2349. $post_errors[] = 'long_message';
  2350. unset($_POST['message']);
  2351. }
  2352. else
  2353. {
  2354. $_POST['message'] = $smcFunc['htmlspecialchars']($_POST['message'], ENT_QUOTES);
  2355. preparsecode($_POST['message']);
  2356. if ($smcFunc['htmltrim'](strip_tags(parse_bbc($_POST['message'], false), '<img>')) === '')
  2357. {
  2358. $post_errors[] = 'no_message';
  2359. unset($_POST['message']);
  2360. }
  2361. }
  2362. }
  2363. if (isset($_POST['lock']))
  2364. {
  2365. if (!allowedTo(array('lock_any', 'lock_own')) || (!allowedTo('lock_any') && $user_info['id'] != $row['id_member']))
  2366. unset($_POST['lock']);
  2367. elseif (!allowedTo('lock_any'))
  2368. {
  2369. if ($row['locked'] == 1)
  2370. unset($_POST['lock']);
  2371. else
  2372. $_POST['lock'] = empty($_POST['lock']) ? 0 : 2;
  2373. }
  2374. elseif (!empty($row['locked']) && !empty($_POST['lock']) || $_POST['lock'] == $row['locked'])
  2375. unset($_POST['lock']);
  2376. else
  2377. $_POST['lock'] = empty($_POST['lock']) ? 0 : 1;
  2378. }
  2379. if (isset($_POST['sticky']) && !allowedTo('make_sticky'))
  2380. unset($_POST['sticky']);
  2381. if (empty($post_errors))
  2382. {
  2383. $msgOptions = array(
  2384. 'id' => $row['id_msg'],
  2385. 'subject' => isset($_POST['subject']) ? $_POST['subject'] : null,
  2386. 'body' => isset($_POST['message']) ? $_POST['message'] : null,
  2387. 'icon' => isset($_REQUEST['icon']) ? preg_replace('~[\./\\\\*\':"<>]~', '', $_REQUEST['icon']) : null,
  2388. );
  2389. $topicOptions = array(
  2390. 'id' => $topic,
  2391. 'board' => $board,
  2392. 'lock_mode' => isset($_POST['lock']) ? (int) $_POST['lock'] : null,
  2393. 'sticky_mode' => isset($_POST['sticky']) && !empty($modSettings['enableStickyTopics']) ? (int) $_POST['sticky'] : null,
  2394. 'mark_as_read' => true,
  2395. );
  2396. $posterOptions = array();
  2397. // Only consider marking as editing if they have edited the subject, message or icon.
  2398. if ((isset($_POST['subject']) && $_POST['subject'] != $row['subject']) || (isset($_POST['message']) && $_POST['message'] != $row['body']) || (isset($_REQUEST['icon']) && $_REQUEST['icon'] != $row['icon']))
  2399. {
  2400. // And even then only if the time has passed...
  2401. if (time() - $row['poster_time'] > $modSettings['edit_wait_time'] || $user_info['id'] != $row['id_member'])
  2402. {
  2403. $msgOptions['modify_time'] = time();
  2404. $msgOptions['modify_name'] = $user_info['name'];
  2405. }
  2406. }
  2407. // If nothing was changed there's no need to add an entry to the moderation log.
  2408. else
  2409. $moderationAction = false;
  2410. modifyPost($msgOptions, $topicOptions, $posterOptions);
  2411. // If we didn't change anything this time but had before put back the old info.
  2412. if (!isset($msgOptions['modify_time']) && !empty($row['modified_time']))
  2413. {
  2414. $msgOptions['modify_time'] = $row['modified_time'];
  2415. $msgOptions['modify_name'] = $row['modified_name'];
  2416. }
  2417. // Changing the first subject updates other subjects to 'Re: new_subject'.
  2418. 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'))))
  2419. {
  2420. // Get the proper (default language) response prefix first.
  2421. if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
  2422. {
  2423. if ($language === $user_info['language'])
  2424. $context['response_prefix'] = $txt['response_prefix'];
  2425. else
  2426. {
  2427. loadLanguage('index', $language, false);
  2428. $context['response_prefix'] = $txt['response_prefix'];
  2429. loadLanguage('index');
  2430. }
  2431. cache_put_data('response_prefix', $context['response_prefix'], 600);
  2432. }
  2433. $smcFunc['db_query']('', '
  2434. UPDATE {db_prefix}messages
  2435. SET subject = {string:subject}
  2436. WHERE id_topic = {int:current_topic}
  2437. AND id_msg != {int:id_first_msg}',
  2438. array(
  2439. 'current_topic' => $topic,
  2440. 'id_first_msg' => $row['id_first_msg'],
  2441. 'subject' => $context['response_prefix'] . $_POST['subject'],
  2442. )
  2443. );
  2444. }
  2445. if (!empty($moderationAction))
  2446. logAction('modify', array('topic' => $topic, 'message' => $row['id_msg'], 'member' => $row['id_member'], 'board' => $board));
  2447. }
  2448. if (isset($_REQUEST['xml']))
  2449. {
  2450. $context['sub_template'] = 'modifydone';
  2451. if (empty($post_errors) && isset($msgOptions['subject']) && isset($msgOptions['body']))
  2452. {
  2453. $context['message'] = array(
  2454. 'id' => $row['id_msg'],
  2455. 'modified' => array(
  2456. 'time' => isset($msgOptions['modify_time']) ? timeformat($msgOptions['modify_time']) : '',
  2457. 'timestamp' => isset($msgOptions['modify_time']) ? forum_time(true, $msgOptions['modify_time']) : 0,
  2458. 'name' => isset($msgOptions['modify_time']) ? $msgOptions['modify_name'] : '',
  2459. ),
  2460. 'subject' => $msgOptions['subject'],
  2461. 'first_in_topic' => $row['id_msg'] == $row['id_first_msg'],
  2462. 'body' => strtr($msgOptions['body'], array(']]>' => ']]]]><![CDATA[>')),
  2463. );
  2464. censorText($context['message']['subject']);
  2465. censorText($context['message']['body']);
  2466. $context['message']['body'] = parse_bbc($context['message']['body'], $row['smileys_enabled'], $row['id_msg']);
  2467. }
  2468. // Topic?
  2469. elseif (empty($post_errors))
  2470. {
  2471. $context['sub_template'] = 'modifytopicdone';
  2472. $context['message'] = array(
  2473. 'id' => $row['id_msg'],
  2474. 'modified' => array(
  2475. 'time' => isset($msgOptions['modify_time']) ? timeformat($msgOptions['modify_time']) : '',
  2476. 'timestamp' => isset($msgOptions['modify_time']) ? forum_time(true, $msgOptions['modify_time']) : 0,
  2477. 'name' => isset($msgOptions['modify_time']) ? $msgOptions['modify_name'] : '',
  2478. ),
  2479. 'subject' => isset($msgOptions['subject']) ? $msgOptions['subject'] : '',
  2480. );
  2481. censorText($context['message']['subject']);
  2482. }
  2483. else
  2484. {
  2485. $context['message'] = array(
  2486. 'id' => $row['id_msg'],
  2487. 'errors' => array(),
  2488. 'error_in_subject' => in_array('no_subject', $post_errors),
  2489. 'error_in_body' => in_array('no_message', $post_errors) || in_array('long_message', $post_errors),
  2490. );
  2491. loadLanguage('Errors');
  2492. foreach ($post_errors as $post_error)
  2493. {
  2494. if ($post_error == 'long_message')
  2495. $context['message']['errors'][] = sprintf($txt['error_' . $post_error], $modSettings['max_messageLength']);
  2496. else
  2497. $context['message']['errors'][] = $txt['error_' . $post_error];
  2498. }
  2499. }
  2500. }
  2501. else
  2502. obExit(false);
  2503. }
  2504. ?>