PageRenderTime 71ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/forum/Sources/Post.php

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