Post.php 112 KB

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