Post.php 113 KB

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