Post.php 106 KB

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