Post.php 106 KB

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