Post.php 113 KB

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