Post.php 106 KB

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