Post.php 106 KB

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