Post.php 105 KB

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