Post.php 114 KB

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