2
0

Post.php 105 KB

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