Post.php 107 KB

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