Post.php 104 KB

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