Post.php 104 KB

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