PersonalMessage.php 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631
  1. <?php
  2. /**
  3. * This file is mainly meant for controlling the actions related to personal
  4. * messages. It allows viewing, sending, deleting, and marking personal
  5. * messages. For compatibility reasons, they are often called "instant messages".
  6. *
  7. * Simple Machines Forum (SMF)
  8. *
  9. * @package SMF
  10. * @author Simple Machines http://www.simplemachines.org
  11. * @copyright 2011 Simple Machines
  12. * @license http://www.simplemachines.org/about/smf/license.php BSD
  13. *
  14. * @version 2.1 Alpha 1
  15. */
  16. if (!defined('SMF'))
  17. die('Hacking attempt...');
  18. /**
  19. * This helps organize things...
  20. * @todo this should be a simple dispatcher....
  21. */
  22. function MessageMain()
  23. {
  24. global $txt, $scripturl, $sourcedir, $context, $user_info, $user_settings, $smcFunc, $modSettings;
  25. // No guests!
  26. is_not_guest();
  27. // You're not supposed to be here at all, if you can't even read PMs.
  28. isAllowedTo('pm_read');
  29. // This file contains the basic functions for sending a PM.
  30. require_once($sourcedir . '/Subs-Post.php');
  31. loadLanguage('PersonalMessage');
  32. if (WIRELESS && WIRELESS_PROTOCOL == 'wap')
  33. fatal_lang_error('wireless_error_notyet', false);
  34. elseif (WIRELESS)
  35. $context['sub_template'] = WIRELESS_PROTOCOL . '_pm';
  36. else
  37. loadTemplate('PersonalMessage');
  38. // Load up the members maximum message capacity.
  39. if ($user_info['is_admin'])
  40. $context['message_limit'] = 0;
  41. elseif (($context['message_limit'] = cache_get_data('msgLimit:' . $user_info['id'], 360)) === null)
  42. {
  43. /**
  44. * @todo Why do we do this? It seems like if they have any limit we should use it.
  45. */
  46. $request = $smcFunc['db_query']('', '
  47. SELECT MAX(max_messages) AS top_limit, MIN(max_messages) AS bottom_limit
  48. FROM {db_prefix}membergroups
  49. WHERE id_group IN ({array_int:users_groups})',
  50. array(
  51. 'users_groups' => $user_info['groups'],
  52. )
  53. );
  54. list ($maxMessage, $minMessage) = $smcFunc['db_fetch_row']($request);
  55. $smcFunc['db_free_result']($request);
  56. $context['message_limit'] = $minMessage == 0 ? 0 : $maxMessage;
  57. // Save us doing it again!
  58. cache_put_data('msgLimit:' . $user_info['id'], $context['message_limit'], 360);
  59. }
  60. // Prepare the context for the capacity bar.
  61. if (!empty($context['message_limit']))
  62. {
  63. $bar = ($user_info['messages'] * 100) / $context['message_limit'];
  64. $context['limit_bar'] = array(
  65. 'messages' => $user_info['messages'],
  66. 'allowed' => $context['message_limit'],
  67. 'percent' => $bar,
  68. 'bar' => min(100, (int) $bar),
  69. 'text' => sprintf($txt['pm_currently_using'], $user_info['messages'], round($bar, 1)),
  70. );
  71. }
  72. // a previous message was sent successfully? show a small indication.
  73. if (isset($_GET['done']) && ($_GET['done'] == 'sent'))
  74. $context['pm_sent'] = true;
  75. // Now we have the labels, and assuming we have unsorted mail, apply our rules!
  76. if ($user_settings['new_pm'])
  77. {
  78. $context['labels'] = $user_settings['message_labels'] == '' ? array() : explode(',', $user_settings['message_labels']);
  79. foreach ($context['labels'] as $id_label => $label_name)
  80. $context['labels'][(int) $id_label] = array(
  81. 'id' => $id_label,
  82. 'name' => trim($label_name),
  83. 'messages' => 0,
  84. 'unread_messages' => 0,
  85. );
  86. $context['labels'][-1] = array(
  87. 'id' => -1,
  88. 'name' => $txt['pm_msg_label_inbox'],
  89. 'messages' => 0,
  90. 'unread_messages' => 0,
  91. );
  92. ApplyRules();
  93. updateMemberData($user_info['id'], array('new_pm' => 0));
  94. $smcFunc['db_query']('', '
  95. UPDATE {db_prefix}pm_recipients
  96. SET is_new = {int:not_new}
  97. WHERE id_member = {int:current_member}',
  98. array(
  99. 'current_member' => $user_info['id'],
  100. 'not_new' => 0,
  101. )
  102. );
  103. }
  104. // Load the label data.
  105. if ($user_settings['new_pm'] || ($context['labels'] = cache_get_data('labelCounts:' . $user_info['id'], 720)) === null)
  106. {
  107. $context['labels'] = $user_settings['message_labels'] == '' ? array() : explode(',', $user_settings['message_labels']);
  108. foreach ($context['labels'] as $id_label => $label_name)
  109. $context['labels'][(int) $id_label] = array(
  110. 'id' => $id_label,
  111. 'name' => trim($label_name),
  112. 'messages' => 0,
  113. 'unread_messages' => 0,
  114. );
  115. $context['labels'][-1] = array(
  116. 'id' => -1,
  117. 'name' => $txt['pm_msg_label_inbox'],
  118. 'messages' => 0,
  119. 'unread_messages' => 0,
  120. );
  121. // Looks like we need to reseek!
  122. $result = $smcFunc['db_query']('', '
  123. SELECT labels, is_read, COUNT(*) AS num
  124. FROM {db_prefix}pm_recipients
  125. WHERE id_member = {int:current_member}
  126. AND deleted = {int:not_deleted}
  127. GROUP BY labels, is_read',
  128. array(
  129. 'current_member' => $user_info['id'],
  130. 'not_deleted' => 0,
  131. )
  132. );
  133. while ($row = $smcFunc['db_fetch_assoc']($result))
  134. {
  135. $this_labels = explode(',', $row['labels']);
  136. foreach ($this_labels as $this_label)
  137. {
  138. $context['labels'][(int) $this_label]['messages'] += $row['num'];
  139. if (!($row['is_read'] & 1))
  140. $context['labels'][(int) $this_label]['unread_messages'] += $row['num'];
  141. }
  142. }
  143. $smcFunc['db_free_result']($result);
  144. // Store it please!
  145. cache_put_data('labelCounts:' . $user_info['id'], $context['labels'], 720);
  146. }
  147. // This determines if we have more labels than just the standard inbox.
  148. $context['currently_using_labels'] = count($context['labels']) > 1 ? 1 : 0;
  149. // Some stuff for the labels...
  150. $context['current_label_id'] = isset($_REQUEST['l']) && isset($context['labels'][(int) $_REQUEST['l']]) ? (int) $_REQUEST['l'] : -1;
  151. $context['current_label'] = &$context['labels'][(int) $context['current_label_id']]['name'];
  152. $context['folder'] = !isset($_REQUEST['f']) || $_REQUEST['f'] != 'sent' ? 'inbox' : 'sent';
  153. // This is convenient. Do you know how annoying it is to do this every time?!
  154. $context['current_label_redirect'] = 'action=pm;f=' . $context['folder'] . (isset($_GET['start']) ? ';start=' . $_GET['start'] : '') . (isset($_REQUEST['l']) ? ';l=' . $_REQUEST['l'] : '');
  155. $context['can_issue_warning'] = in_array('w', $context['admin_features']) && allowedTo('issue_warning') && $modSettings['warning_settings'][0] == 1;
  156. // Build the linktree for all the actions...
  157. $context['linktree'][] = array(
  158. 'url' => $scripturl . '?action=pm',
  159. 'name' => $txt['personal_messages']
  160. );
  161. // Preferences...
  162. $context['display_mode'] = WIRELESS ? 0 : $user_settings['pm_prefs'] & 3;
  163. $subActions = array(
  164. 'addbuddy' => 'WirelessAddBuddy',
  165. 'manlabels' => 'ManageLabels',
  166. 'manrules' => 'ManageRules',
  167. 'pmactions' => 'MessageActionsApply',
  168. 'prune' => 'MessagePrune',
  169. 'removeall' => 'MessageKillAllQuery',
  170. 'removeall2' => 'MessageKillAll',
  171. 'report' => 'ReportMessage',
  172. 'search' => 'MessageSearch',
  173. 'search2' => 'MessageSearch2',
  174. 'send' => 'MessagePost',
  175. 'send2' => 'MessagePost2',
  176. 'settings' => 'MessageSettings',
  177. );
  178. if (!isset($_REQUEST['sa']) || !isset($subActions[$_REQUEST['sa']]))
  179. MessageFolder();
  180. else
  181. {
  182. messageIndexBar($_REQUEST['sa']);
  183. $subActions[$_REQUEST['sa']]();
  184. }
  185. }
  186. /**
  187. * A sidebar to easily access different areas of the section
  188. *
  189. * @param string $area
  190. */
  191. function messageIndexBar($area)
  192. {
  193. global $txt, $context, $scripturl, $sourcedir, $sc, $modSettings, $settings, $user_info, $options;
  194. $pm_areas = array(
  195. 'folders' => array(
  196. 'title' => $txt['pm_messages'],
  197. 'areas' => array(
  198. 'send' => array(
  199. 'label' => $txt['new_message'],
  200. 'custom_url' => $scripturl . '?action=pm;sa=send',
  201. 'permission' => allowedTo('pm_send'),
  202. ),
  203. 'inbox' => array(
  204. 'label' => $txt['inbox'],
  205. 'custom_url' => $scripturl . '?action=pm',
  206. ),
  207. 'sent' => array(
  208. 'label' => $txt['sent_items'],
  209. 'custom_url' => $scripturl . '?action=pm;f=sent',
  210. ),
  211. ),
  212. ),
  213. 'labels' => array(
  214. 'title' => $txt['pm_labels'],
  215. 'areas' => array(),
  216. ),
  217. 'actions' => array(
  218. 'title' => $txt['pm_actions'],
  219. 'areas' => array(
  220. 'search' => array(
  221. 'label' => $txt['pm_search_bar_title'],
  222. 'custom_url' => $scripturl . '?action=pm;sa=search',
  223. ),
  224. 'prune' => array(
  225. 'label' => $txt['pm_prune'],
  226. 'custom_url' => $scripturl . '?action=pm;sa=prune'
  227. ),
  228. ),
  229. ),
  230. 'pref' => array(
  231. 'title' => $txt['pm_preferences'],
  232. 'areas' => array(
  233. 'manlabels' => array(
  234. 'label' => $txt['pm_manage_labels'],
  235. 'custom_url' => $scripturl . '?action=pm;sa=manlabels',
  236. ),
  237. 'manrules' => array(
  238. 'label' => $txt['pm_manage_rules'],
  239. 'custom_url' => $scripturl . '?action=pm;sa=manrules',
  240. ),
  241. 'settings' => array(
  242. 'label' => $txt['pm_settings'],
  243. 'custom_url' => $scripturl . '?action=pm;sa=settings',
  244. ),
  245. ),
  246. ),
  247. );
  248. // Handle labels.
  249. if (empty($context['currently_using_labels']))
  250. unset($pm_areas['labels']);
  251. else
  252. {
  253. // Note we send labels by id as it will have less problems in the querystring.
  254. $unread_in_labels = 0;
  255. foreach ($context['labels'] as $label)
  256. {
  257. if ($label['id'] == -1)
  258. continue;
  259. // Count the amount of unread items in labels.
  260. $unread_in_labels += $label['unread_messages'];
  261. // Add the label to the menu.
  262. $pm_areas['labels']['areas']['label' . $label['id']] = array(
  263. 'label' => $label['name'] . (!empty($label['unread_messages']) ? ' (<strong>' . $label['unread_messages'] . '</strong>)' : ''),
  264. 'custom_url' => $scripturl . '?action=pm;l=' . $label['id'],
  265. 'unread_messages' => $label['unread_messages'],
  266. 'messages' => $label['messages'],
  267. );
  268. }
  269. if (!empty($unread_in_labels))
  270. $pm_areas['labels']['title'] .= ' (' . $unread_in_labels . ')';
  271. }
  272. $pm_areas['folders']['areas']['inbox']['unread_messages'] = &$context['labels'][-1]['unread_messages'];
  273. $pm_areas['folders']['areas']['inbox']['messages'] = &$context['labels'][-1]['messages'];
  274. if (!empty($context['labels'][-1]['unread_messages']))
  275. {
  276. $pm_areas['folders']['areas']['inbox']['label'] .= ' (<strong>' . $context['labels'][-1]['unread_messages'] . '</strong>)';
  277. $pm_areas['folders']['title'] .= ' (' . $context['labels'][-1]['unread_messages'] . ')';
  278. }
  279. // Do we have a limit on the amount of messages we can keep?
  280. if (!empty($context['message_limit']))
  281. {
  282. $bar = round(($user_info['messages'] * 100) / $context['message_limit'], 1);
  283. $context['limit_bar'] = array(
  284. 'messages' => $user_info['messages'],
  285. 'allowed' => $context['message_limit'],
  286. 'percent' => $bar,
  287. 'bar' => $bar > 100 ? 100 : (int) $bar,
  288. 'text' => sprintf($txt['pm_currently_using'], $user_info['messages'], $bar)
  289. );
  290. }
  291. require_once($sourcedir . '/Subs-Menu.php');
  292. // What page is this, again?
  293. $current_page = $scripturl . '?action=pm' . (!empty($_REQUEST['sa']) ? ';sa=' . $_REQUEST['sa'] : '') . (!empty($context['folder']) ? ';f=' . $context['folder'] : '') . (!empty($context['current_label_id']) ? ';l=' . $context['current_label_id'] : '');
  294. // Set a few options for the menu.
  295. $menuOptions = array(
  296. 'current_area' => $area,
  297. 'disable_url_session_check' => true,
  298. 'toggle_url' => $current_page . ';togglebar',
  299. 'toggle_redirect_url' => $current_page,
  300. );
  301. // Actually create the menu!
  302. $pm_include_data = createMenu($pm_areas, $menuOptions);
  303. unset($pm_areas);
  304. // Make a note of the Unique ID for this menu.
  305. $context['pm_menu_id'] = $context['max_menu_id'];
  306. $context['pm_menu_name'] = 'menu_data_' . $context['pm_menu_id'];
  307. // Set the selected item.
  308. $context['menu_item_selected'] = $pm_include_data['current_area'];
  309. // obExit will know what to do!
  310. if (!WIRELESS)
  311. $context['template_layers'][] = 'pm';
  312. }
  313. /**
  314. * A folder, ie. inbox/sent etc.
  315. */
  316. function MessageFolder()
  317. {
  318. global $txt, $scripturl, $modSettings, $context, $subjects_request;
  319. global $messages_request, $user_info, $recipients, $options, $smcFunc, $memberContext, $user_settings;
  320. // Changing view?
  321. if (isset($_GET['view']))
  322. {
  323. $context['display_mode'] = $context['display_mode'] > 1 ? 0 : $context['display_mode'] + 1;
  324. updateMemberData($user_info['id'], array('pm_prefs' => ($user_settings['pm_prefs'] & 252) | $context['display_mode']));
  325. }
  326. // Make sure the starting location is valid.
  327. if (isset($_GET['start']) && $_GET['start'] != 'new')
  328. $_GET['start'] = (int) $_GET['start'];
  329. elseif (!isset($_GET['start']) && !empty($options['view_newest_pm_first']))
  330. $_GET['start'] = 0;
  331. else
  332. $_GET['start'] = 'new';
  333. // Set up some basic theme stuff.
  334. $context['from_or_to'] = $context['folder'] != 'sent' ? 'from' : 'to';
  335. $context['get_pmessage'] = 'prepareMessageContext';
  336. $context['signature_enabled'] = substr($modSettings['signature_settings'], 0, 1) == 1;
  337. $labelQuery = $context['folder'] != 'sent' ? '
  338. AND FIND_IN_SET(' . $context['current_label_id'] . ', pmr.labels) != 0' : '';
  339. // Set the index bar correct!
  340. messageIndexBar($context['current_label_id'] == -1 ? $context['folder'] : 'label' . $context['current_label_id']);
  341. // Sorting the folder.
  342. $sort_methods = array(
  343. 'date' => 'pm.id_pm',
  344. 'name' => 'IFNULL(mem.real_name, \'\')',
  345. 'subject' => 'pm.subject',
  346. );
  347. // They didn't pick one, use the forum default.
  348. if (!isset($_GET['sort']) || !isset($sort_methods[$_GET['sort']]))
  349. {
  350. $context['sort_by'] = 'date';
  351. $_GET['sort'] = 'pm.id_pm';
  352. // An overriding setting?
  353. $descending = !empty($options['view_newest_pm_first']);
  354. }
  355. // Otherwise use the defaults: ascending, by date.
  356. else
  357. {
  358. $context['sort_by'] = $_GET['sort'];
  359. $_GET['sort'] = $sort_methods[$_GET['sort']];
  360. $descending = isset($_GET['desc']);
  361. }
  362. $context['sort_direction'] = $descending ? 'down' : 'up';
  363. // Why would you want access to your sent items if you're not allowed to send anything?
  364. if ($context['folder'] == 'sent')
  365. isAllowedTo('pm_send');
  366. // Set the text to resemble the current folder.
  367. $pmbox = $context['folder'] != 'sent' ? $txt['inbox'] : $txt['sent_items'];
  368. $txt['delete_all'] = str_replace('PMBOX', $pmbox, $txt['delete_all']);
  369. // Now, build the link tree!
  370. if ($context['current_label_id'] == -1)
  371. $context['linktree'][] = array(
  372. 'url' => $scripturl . '?action=pm;f=' . $context['folder'],
  373. 'name' => $pmbox
  374. );
  375. // Build it further for a label.
  376. if ($context['current_label_id'] != -1)
  377. $context['linktree'][] = array(
  378. 'url' => $scripturl . '?action=pm;f=' . $context['folder'] . ';l=' . $context['current_label_id'],
  379. 'name' => $txt['pm_current_label'] . ': ' . $context['current_label']
  380. );
  381. // Figure out how many messages there are.
  382. if ($context['folder'] == 'sent')
  383. $request = $smcFunc['db_query']('', '
  384. SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
  385. FROM {db_prefix}personal_messages AS pm
  386. WHERE pm.id_member_from = {int:current_member}
  387. AND pm.deleted_by_sender = {int:not_deleted}',
  388. array(
  389. 'current_member' => $user_info['id'],
  390. 'not_deleted' => 0,
  391. )
  392. );
  393. else
  394. $request = $smcFunc['db_query']('', '
  395. SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
  396. FROM {db_prefix}pm_recipients AS pmr' . ($context['display_mode'] == 2 ? '
  397. INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)' : '') . '
  398. WHERE pmr.id_member = {int:current_member}
  399. AND pmr.deleted = {int:not_deleted}' . $labelQuery,
  400. array(
  401. 'current_member' => $user_info['id'],
  402. 'not_deleted' => 0,
  403. )
  404. );
  405. list ($max_messages) = $smcFunc['db_fetch_row']($request);
  406. $smcFunc['db_free_result']($request);
  407. // Only show the button if there are messages to delete.
  408. $context['show_delete'] = $max_messages > 0;
  409. // Start on the last page.
  410. if (!is_numeric($_GET['start']) || $_GET['start'] >= $max_messages)
  411. $_GET['start'] = ($max_messages - 1) - (($max_messages - 1) % $modSettings['defaultMaxMessages']);
  412. elseif ($_GET['start'] < 0)
  413. $_GET['start'] = 0;
  414. // ... but wait - what if we want to start from a specific message?
  415. if (isset($_GET['pmid']))
  416. {
  417. $pmID = (int) $_GET['pmid'];
  418. // Make sure you have access to this PM.
  419. if (!isAccessiblePM($pmID, $context['folder'] == 'sent' ? 'outbox' : 'inbox'))
  420. fatal_lang_error('no_access', false);
  421. $context['current_pm'] = $pmID;
  422. // With only one page of PM's we're gonna want page 1.
  423. if ($max_messages <= $modSettings['defaultMaxMessages'])
  424. $_GET['start'] = 0;
  425. // If we pass kstart we assume we're in the right place.
  426. elseif (!isset($_GET['kstart']))
  427. {
  428. if ($context['folder'] == 'sent')
  429. $request = $smcFunc['db_query']('', '
  430. SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
  431. FROM {db_prefix}personal_messages
  432. WHERE id_member_from = {int:current_member}
  433. AND deleted_by_sender = {int:not_deleted}
  434. AND id_pm ' . ($descending ? '>' : '<') . ' {int:id_pm}',
  435. array(
  436. 'current_member' => $user_info['id'],
  437. 'not_deleted' => 0,
  438. 'id_pm' => $pmID,
  439. )
  440. );
  441. else
  442. $request = $smcFunc['db_query']('', '
  443. SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
  444. FROM {db_prefix}pm_recipients AS pmr' . ($context['display_mode'] == 2 ? '
  445. INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)' : '') . '
  446. WHERE pmr.id_member = {int:current_member}
  447. AND pmr.deleted = {int:not_deleted}' . $labelQuery . '
  448. AND pmr.id_pm ' . ($descending ? '>' : '<') . ' {int:id_pm}',
  449. array(
  450. 'current_member' => $user_info['id'],
  451. 'not_deleted' => 0,
  452. 'id_pm' => $pmID,
  453. )
  454. );
  455. list ($_GET['start']) = $smcFunc['db_fetch_row']($request);
  456. $smcFunc['db_free_result']($request);
  457. // To stop the page index's being abnormal, start the page on the page the message would normally be located on...
  458. $_GET['start'] = $modSettings['defaultMaxMessages'] * (int) ($_GET['start'] / $modSettings['defaultMaxMessages']);
  459. }
  460. }
  461. // Sanitize and validate pmsg variable if set.
  462. if (isset($_GET['pmsg']))
  463. {
  464. $pmsg = (int) $_GET['pmsg'];
  465. if (!isAccessiblePM($pmsg, $context['folder'] == 'sent' ? 'outbox' : 'inbox'))
  466. fatal_lang_error('no_access', false);
  467. }
  468. // Set up the page index.
  469. $context['page_index'] = constructPageIndex($scripturl . '?action=pm;f=' . $context['folder'] . (isset($_REQUEST['l']) ? ';l=' . (int) $_REQUEST['l'] : '') . ';sort=' . $context['sort_by'] . ($descending ? ';desc' : ''), $_GET['start'], $max_messages, $modSettings['defaultMaxMessages']);
  470. $context['start'] = $_GET['start'];
  471. // Determine the navigation context (especially useful for the wireless template).
  472. $context['links'] = array(
  473. 'first' => $_GET['start'] >= $modSettings['defaultMaxMessages'] ? $scripturl . '?action=pm;start=0' : '',
  474. 'prev' => $_GET['start'] >= $modSettings['defaultMaxMessages'] ? $scripturl . '?action=pm;start=' . ($_GET['start'] - $modSettings['defaultMaxMessages']) : '',
  475. 'next' => $_GET['start'] + $modSettings['defaultMaxMessages'] < $max_messages ? $scripturl . '?action=pm;start=' . ($_GET['start'] + $modSettings['defaultMaxMessages']) : '',
  476. 'last' => $_GET['start'] + $modSettings['defaultMaxMessages'] < $max_messages ? $scripturl . '?action=pm;start=' . (floor(($max_messages - 1) / $modSettings['defaultMaxMessages']) * $modSettings['defaultMaxMessages']) : '',
  477. 'up' => $scripturl,
  478. );
  479. $context['page_info'] = array(
  480. 'current_page' => $_GET['start'] / $modSettings['defaultMaxMessages'] + 1,
  481. 'num_pages' => floor(($max_messages - 1) / $modSettings['defaultMaxMessages']) + 1
  482. );
  483. // First work out what messages we need to see - if grouped is a little trickier...
  484. if ($context['display_mode'] == 2)
  485. {
  486. // On a non-default sort due to PostgreSQL we have to do a harder sort.
  487. if ($smcFunc['db_title'] == 'PostgreSQL' && $_GET['sort'] != 'pm.id_pm')
  488. {
  489. $sub_request = $smcFunc['db_query']('', '
  490. SELECT MAX({raw:sort}) AS sort_param, pm.id_pm_head
  491. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($context['sort_by'] == 'name' ? '
  492. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
  493. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
  494. AND pmr.id_member = {int:current_member}
  495. AND pmr.deleted = {int:not_deleted}
  496. ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
  497. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})') : '') . '
  498. WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
  499. AND pm.deleted_by_sender = {int:not_deleted}' : '1=1') . (empty($pmsg) ? '' : '
  500. AND pm.id_pm = {int:id_pm}') . '
  501. GROUP BY pm.id_pm_head
  502. ORDER BY sort_param' . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
  503. LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
  504. array(
  505. 'current_member' => $user_info['id'],
  506. 'not_deleted' => 0,
  507. 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  508. 'id_pm' => isset($pmsg) ? $pmsg : '0',
  509. 'sort' => $_GET['sort'],
  510. )
  511. );
  512. $sub_pms = array();
  513. while ($row = $smcFunc['db_fetch_assoc']($sub_request))
  514. $sub_pms[$row['id_pm_head']] = $row['sort_param'];
  515. $smcFunc['db_free_result']($sub_request);
  516. $request = $smcFunc['db_query']('', '
  517. SELECT pm.id_pm AS id_pm, pm.id_pm_head
  518. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($context['sort_by'] == 'name' ? '
  519. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
  520. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
  521. AND pmr.id_member = {int:current_member}
  522. AND pmr.deleted = {int:not_deleted}
  523. ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
  524. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})') : '') . '
  525. WHERE ' . (empty($sub_pms) ? '0=1' : 'pm.id_pm IN ({array_int:pm_list})') . '
  526. ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
  527. LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
  528. array(
  529. 'current_member' => $user_info['id'],
  530. 'pm_list' => array_keys($sub_pms),
  531. 'not_deleted' => 0,
  532. 'sort' => $_GET['sort'],
  533. 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  534. )
  535. );
  536. }
  537. else
  538. {
  539. $request = $smcFunc['db_query']('pm_conversation_list', '
  540. SELECT MAX(pm.id_pm) AS id_pm, pm.id_pm_head
  541. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($context['sort_by'] == 'name' ? '
  542. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
  543. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
  544. AND pmr.id_member = {int:current_member}
  545. AND pmr.deleted = {int:deleted_by}
  546. ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
  547. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})') : '') . '
  548. WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
  549. AND pm.deleted_by_sender = {int:deleted_by}' : '1=1') . (empty($pmsg) ? '' : '
  550. AND pm.id_pm = {int:pmsg}') . '
  551. GROUP BY pm.id_pm_head
  552. ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($_GET['pmsg']) ? '
  553. LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
  554. array(
  555. 'current_member' => $user_info['id'],
  556. 'deleted_by' => 0,
  557. 'sort' => $_GET['sort'],
  558. 'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  559. 'pmsg' => isset($pmsg) ? (int) $pmsg : 0,
  560. )
  561. );
  562. }
  563. }
  564. // This is kinda simple!
  565. else
  566. {
  567. /**
  568. * @todo SLOW This query uses a filesort. (inbox only.)
  569. */
  570. $request = $smcFunc['db_query']('', '
  571. SELECT pm.id_pm, pm.id_pm_head, pm.id_member_from
  572. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '' . ($context['sort_by'] == 'name' ? '
  573. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
  574. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
  575. AND pmr.id_member = {int:current_member}
  576. AND pmr.deleted = {int:is_deleted}
  577. ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
  578. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})') : '') . '
  579. WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {raw:current_member}
  580. AND pm.deleted_by_sender = {int:is_deleted}' : '1=1') . (empty($pmsg) ? '' : '
  581. AND pm.id_pm = {int:pmsg}') . '
  582. ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'pmr.id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
  583. LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
  584. array(
  585. 'current_member' => $user_info['id'],
  586. 'is_deleted' => 0,
  587. 'sort' => $_GET['sort'],
  588. 'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  589. 'pmsg' => isset($pmsg) ? (int) $pmsg : 0,
  590. )
  591. );
  592. }
  593. // Load the id_pms and initialize recipients.
  594. $pms = array();
  595. $lastData = array();
  596. $posters = $context['folder'] == 'sent' ? array($user_info['id']) : array();
  597. $recipients = array();
  598. while ($row = $smcFunc['db_fetch_assoc']($request))
  599. {
  600. if (!isset($recipients[$row['id_pm']]))
  601. {
  602. if (isset($row['id_member_from']))
  603. $posters[$row['id_pm']] = $row['id_member_from'];
  604. $pms[$row['id_pm']] = $row['id_pm'];
  605. $recipients[$row['id_pm']] = array(
  606. 'to' => array(),
  607. 'bcc' => array()
  608. );
  609. }
  610. // Keep track of the last message so we know what the head is without another query!
  611. if ((empty($pmID) && (empty($options['view_newest_pm_first']) || !isset($lastData))) || empty($lastData) || (!empty($pmID) && $pmID == $row['id_pm']))
  612. $lastData = array(
  613. 'id' => $row['id_pm'],
  614. 'head' => $row['id_pm_head'],
  615. );
  616. }
  617. $smcFunc['db_free_result']($request);
  618. // Make sure that we have been given a correct head pm id!
  619. if ($context['display_mode'] == 2 && !empty($pmID) && $pmID != $lastData['id'])
  620. fatal_lang_error('no_access', false);
  621. if (!empty($pms))
  622. {
  623. // Select the correct current message.
  624. if (empty($pmID))
  625. $context['current_pm'] = $lastData['id'];
  626. // This is a list of the pm's that are used for "full" display.
  627. if ($context['display_mode'] == 0)
  628. $display_pms = $pms;
  629. else
  630. $display_pms = array($context['current_pm']);
  631. // At this point we know the main id_pm's. But - if we are looking at conversations we need the others!
  632. if ($context['display_mode'] == 2)
  633. {
  634. $request = $smcFunc['db_query']('', '
  635. SELECT pm.id_pm, pm.id_member_from, pm.deleted_by_sender, pmr.id_member, pmr.deleted
  636. FROM {db_prefix}personal_messages AS pm
  637. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
  638. WHERE pm.id_pm_head = {int:id_pm_head}
  639. AND ((pm.id_member_from = {int:current_member} AND pm.deleted_by_sender = {int:not_deleted})
  640. OR (pmr.id_member = {int:current_member} AND pmr.deleted = {int:not_deleted}))
  641. ORDER BY pm.id_pm',
  642. array(
  643. 'current_member' => $user_info['id'],
  644. 'id_pm_head' => $lastData['head'],
  645. 'not_deleted' => 0,
  646. )
  647. );
  648. while ($row = $smcFunc['db_fetch_assoc']($request))
  649. {
  650. // This is, frankly, a joke. We will put in a workaround for people sending to themselves - yawn!
  651. if ($context['folder'] == 'sent' && $row['id_member_from'] == $user_info['id'] && $row['deleted_by_sender'] == 1)
  652. continue;
  653. elseif ($row['id_member'] == $user_info['id'] & $row['deleted'] == 1)
  654. continue;
  655. if (!isset($recipients[$row['id_pm']]))
  656. $recipients[$row['id_pm']] = array(
  657. 'to' => array(),
  658. 'bcc' => array()
  659. );
  660. $display_pms[] = $row['id_pm'];
  661. $posters[$row['id_pm']] = $row['id_member_from'];
  662. }
  663. $smcFunc['db_free_result']($request);
  664. }
  665. // This is pretty much EVERY pm!
  666. $all_pms = array_merge($pms, $display_pms);
  667. $all_pms = array_unique($all_pms);
  668. // Get recipients (don't include bcc-recipients for your inbox, you're not supposed to know :P).
  669. $request = $smcFunc['db_query']('', '
  670. SELECT pmr.id_pm, mem_to.id_member AS id_member_to, mem_to.real_name AS to_name, pmr.bcc, pmr.labels, pmr.is_read
  671. FROM {db_prefix}pm_recipients AS pmr
  672. LEFT JOIN {db_prefix}members AS mem_to ON (mem_to.id_member = pmr.id_member)
  673. WHERE pmr.id_pm IN ({array_int:pm_list})',
  674. array(
  675. 'pm_list' => $all_pms,
  676. )
  677. );
  678. $context['message_labels'] = array();
  679. $context['message_replied'] = array();
  680. $context['message_unread'] = array();
  681. while ($row = $smcFunc['db_fetch_assoc']($request))
  682. {
  683. if ($context['folder'] == 'sent' || empty($row['bcc']))
  684. $recipients[$row['id_pm']][empty($row['bcc']) ? 'to' : 'bcc'][] = empty($row['id_member_to']) ? $txt['guest_title'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_to'] . '">' . $row['to_name'] . '</a>';
  685. if ($row['id_member_to'] == $user_info['id'] && $context['folder'] != 'sent')
  686. {
  687. $context['message_replied'][$row['id_pm']] = $row['is_read'] & 2;
  688. $context['message_unread'][$row['id_pm']] = $row['is_read'] == 0;
  689. $row['labels'] = $row['labels'] == '' ? array() : explode(',', $row['labels']);
  690. foreach ($row['labels'] as $v)
  691. {
  692. if (isset($context['labels'][(int) $v]))
  693. $context['message_labels'][$row['id_pm']][(int) $v] = array('id' => $v, 'name' => $context['labels'][(int) $v]['name']);
  694. }
  695. }
  696. }
  697. $smcFunc['db_free_result']($request);
  698. // Make sure we don't load unnecessary data.
  699. if ($context['display_mode'] == 1)
  700. {
  701. foreach ($posters as $k => $v)
  702. if (!in_array($k, $display_pms))
  703. unset($posters[$k]);
  704. }
  705. // Load any users....
  706. $posters = array_unique($posters);
  707. if (!empty($posters))
  708. loadMemberData($posters);
  709. // If we're on grouped/restricted view get a restricted list of messages.
  710. if ($context['display_mode'] != 0)
  711. {
  712. // Get the order right.
  713. $orderBy = array();
  714. foreach (array_reverse($pms) as $pm)
  715. $orderBy[] = 'pm.id_pm = ' . $pm;
  716. // Seperate query for these bits!
  717. $subjects_request = $smcFunc['db_query']('', '
  718. SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.msgtime, IFNULL(mem.real_name, pm.from_name) AS from_name,
  719. IFNULL(mem.id_member, 0) AS not_guest
  720. FROM {db_prefix}personal_messages AS pm
  721. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
  722. WHERE pm.id_pm IN ({array_int:pm_list})
  723. ORDER BY ' . implode(', ', $orderBy) . '
  724. LIMIT ' . count($pms),
  725. array(
  726. 'pm_list' => $pms,
  727. )
  728. );
  729. }
  730. // Execute the query!
  731. $messages_request = $smcFunc['db_query']('', '
  732. SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name
  733. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '
  734. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') . ($context['sort_by'] == 'name' ? '
  735. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})' : '') . '
  736. WHERE pm.id_pm IN ({array_int:display_pms})' . ($context['folder'] == 'sent' ? '
  737. GROUP BY pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name' : '') . '
  738. ORDER BY ' . ($context['display_mode'] == 2 ? 'pm.id_pm' : $_GET['sort']) . ($descending ? ' DESC' : ' ASC') . '
  739. LIMIT ' . count($display_pms),
  740. array(
  741. 'display_pms' => $display_pms,
  742. 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  743. )
  744. );
  745. }
  746. else
  747. $messages_request = false;
  748. $context['can_send_pm'] = allowedTo('pm_send');
  749. if (!WIRELESS)
  750. $context['sub_template'] = 'folder';
  751. $context['page_title'] = $txt['pm_inbox'];
  752. // Finally mark the relevant messages as read.
  753. if ($context['folder'] != 'sent' && !empty($context['labels'][(int) $context['current_label_id']]['unread_messages']))
  754. {
  755. // If the display mode is "old sk00l" do them all...
  756. if ($context['display_mode'] == 0)
  757. markMessages(null, $context['current_label_id']);
  758. // Otherwise do just the current one!
  759. elseif (!empty($context['current_pm']))
  760. markMessages($display_pms, $context['current_label_id']);
  761. }
  762. }
  763. /**
  764. * Get a personal message for the theme. (used to save memory.)
  765. *
  766. * @param $type
  767. * @param $reset
  768. */
  769. function prepareMessageContext($type = 'subject', $reset = false)
  770. {
  771. global $txt, $scripturl, $modSettings, $context, $messages_request, $memberContext, $recipients, $smcFunc;
  772. global $user_info, $subjects_request;
  773. // Count the current message number....
  774. static $counter = null;
  775. if ($counter === null || $reset)
  776. $counter = $context['start'];
  777. static $temp_pm_selected = null;
  778. if ($temp_pm_selected === null)
  779. {
  780. $temp_pm_selected = isset($_SESSION['pm_selected']) ? $_SESSION['pm_selected'] : array();
  781. $_SESSION['pm_selected'] = array();
  782. }
  783. // If we're in non-boring view do something exciting!
  784. if ($context['display_mode'] != 0 && $subjects_request && $type == 'subject')
  785. {
  786. $subject = $smcFunc['db_fetch_assoc']($subjects_request);
  787. if (!$subject)
  788. {
  789. $smcFunc['db_free_result']($subjects_request);
  790. return false;
  791. }
  792. $subject['subject'] = $subject['subject'] == '' ? $txt['no_subject'] : $subject['subject'];
  793. censorText($subject['subject']);
  794. $output = array(
  795. 'id' => $subject['id_pm'],
  796. 'member' => array(
  797. 'id' => $subject['id_member_from'],
  798. 'name' => $subject['from_name'],
  799. 'link' => $subject['not_guest'] ? '<a href="' . $scripturl . '?action=profile;u=' . $subject['id_member_from'] . '">' . $subject['from_name'] . '</a>' : $subject['from_name'],
  800. ),
  801. 'recipients' => &$recipients[$subject['id_pm']],
  802. 'subject' => $subject['subject'],
  803. 'time' => timeformat($subject['msgtime']),
  804. 'timestamp' => forum_time(true, $subject['msgtime']),
  805. 'number_recipients' => count($recipients[$subject['id_pm']]['to']),
  806. 'labels' => &$context['message_labels'][$subject['id_pm']],
  807. 'fully_labeled' => count($context['message_labels'][$subject['id_pm']]) == count($context['labels']),
  808. 'is_replied_to' => &$context['message_replied'][$subject['id_pm']],
  809. 'is_unread' => &$context['message_unread'][$subject['id_pm']],
  810. 'is_selected' => !empty($temp_pm_selected) && in_array($subject['id_pm'], $temp_pm_selected),
  811. );
  812. return $output;
  813. }
  814. // Bail if it's false, ie. no messages.
  815. if ($messages_request == false)
  816. return false;
  817. // Reset the data?
  818. if ($reset == true)
  819. return @$smcFunc['db_data_seek']($messages_request, 0);
  820. // Get the next one... bail if anything goes wrong.
  821. $message = $smcFunc['db_fetch_assoc']($messages_request);
  822. if (!$message)
  823. {
  824. if ($type != 'subject')
  825. $smcFunc['db_free_result']($messages_request);
  826. return false;
  827. }
  828. // Use '(no subject)' if none was specified.
  829. $message['subject'] = $message['subject'] == '' ? $txt['no_subject'] : $message['subject'];
  830. // Load the message's information - if it's not there, load the guest information.
  831. if (!loadMemberContext($message['id_member_from'], true))
  832. {
  833. $memberContext[$message['id_member_from']]['name'] = $message['from_name'];
  834. $memberContext[$message['id_member_from']]['id'] = 0;
  835. // Sometimes the forum sends messages itself (Warnings are an example) - in this case don't label it from a guest.
  836. $memberContext[$message['id_member_from']]['group'] = $message['from_name'] == $context['forum_name'] ? '' : $txt['guest_title'];
  837. $memberContext[$message['id_member_from']]['link'] = $message['from_name'];
  838. $memberContext[$message['id_member_from']]['email'] = '';
  839. $memberContext[$message['id_member_from']]['show_email'] = showEmailAddress(true, 0);
  840. $memberContext[$message['id_member_from']]['is_guest'] = true;
  841. }
  842. else
  843. {
  844. $memberContext[$message['id_member_from']]['can_view_profile'] = allowedTo('profile_view_any') || ($message['id_member_from'] == $user_info['id'] && allowedTo('profile_view_own'));
  845. $memberContext[$message['id_member_from']]['can_see_warning'] = !isset($context['disabled_fields']['warning_status']) && $memberContext[$message['id_member_from']]['warning_status'] && ($context['user']['can_mod'] || (!empty($modSettings['warning_show']) && ($modSettings['warning_show'] > 1 || $message['id_member_from'] == $user_info['id'])));
  846. }
  847. // Censor all the important text...
  848. censorText($message['body']);
  849. censorText($message['subject']);
  850. // Run UBBC interpreter on the message.
  851. $message['body'] = parse_bbc($message['body'], true, 'pm' . $message['id_pm']);
  852. // Send the array.
  853. $output = array(
  854. 'alternate' => $counter % 2,
  855. 'id' => $message['id_pm'],
  856. 'member' => &$memberContext[$message['id_member_from']],
  857. 'subject' => $message['subject'],
  858. 'time' => timeformat($message['msgtime']),
  859. 'timestamp' => forum_time(true, $message['msgtime']),
  860. 'counter' => $counter,
  861. 'body' => $message['body'],
  862. 'recipients' => &$recipients[$message['id_pm']],
  863. 'number_recipients' => count($recipients[$message['id_pm']]['to']),
  864. 'labels' => &$context['message_labels'][$message['id_pm']],
  865. 'fully_labeled' => count($context['message_labels'][$message['id_pm']]) == count($context['labels']),
  866. 'is_replied_to' => &$context['message_replied'][$message['id_pm']],
  867. 'is_unread' => &$context['message_unread'][$message['id_pm']],
  868. 'is_selected' => !empty($temp_pm_selected) && in_array($message['id_pm'], $temp_pm_selected),
  869. );
  870. $counter++;
  871. return $output;
  872. }
  873. /**
  874. * Allows to search through personal messages.
  875. */
  876. function MessageSearch()
  877. {
  878. global $context, $txt, $scripturl, $modSettings, $smcFunc;
  879. if (isset($_REQUEST['params']))
  880. {
  881. $temp_params = explode('|"|', base64_decode(strtr($_REQUEST['params'], array(' ' => '+'))));
  882. $context['search_params'] = array();
  883. foreach ($temp_params as $i => $data)
  884. {
  885. @list ($k, $v) = explode('|\'|', $data);
  886. $context['search_params'][$k] = $v;
  887. }
  888. }
  889. if (isset($_REQUEST['search']))
  890. $context['search_params']['search'] = un_htmlspecialchars($_REQUEST['search']);
  891. if (isset($context['search_params']['search']))
  892. $context['search_params']['search'] = htmlspecialchars($context['search_params']['search']);
  893. if (isset($context['search_params']['userspec']))
  894. $context['search_params']['userspec'] = htmlspecialchars($context['search_params']['userspec']);
  895. if (!empty($context['search_params']['searchtype']))
  896. $context['search_params']['searchtype'] = 2;
  897. if (!empty($context['search_params']['minage']))
  898. $context['search_params']['minage'] = (int) $context['search_params']['minage'];
  899. if (!empty($context['search_params']['maxage']))
  900. $context['search_params']['maxage'] = (int) $context['search_params']['maxage'];
  901. $context['search_params']['subject_only'] = !empty($context['search_params']['subject_only']);
  902. $context['search_params']['show_complete'] = !empty($context['search_params']['show_complete']);
  903. // Create the array of labels to be searched.
  904. $context['search_labels'] = array();
  905. $searchedLabels = isset($context['search_params']['labels']) && $context['search_params']['labels'] != '' ? explode(',', $context['search_params']['labels']) : array();
  906. foreach ($context['labels'] as $label)
  907. {
  908. $context['search_labels'][] = array(
  909. 'id' => $label['id'],
  910. 'name' => $label['name'],
  911. 'checked' => !empty($searchedLabels) ? in_array($label['id'], $searchedLabels) : true,
  912. );
  913. }
  914. // Are all the labels checked?
  915. $context['check_all'] = empty($searchedLabels) || count($context['search_labels']) == count($searchedLabels);
  916. // Load the error text strings if there were errors in the search.
  917. if (!empty($context['search_errors']))
  918. {
  919. loadLanguage('Errors');
  920. $context['search_errors']['messages'] = array();
  921. foreach ($context['search_errors'] as $search_error => $dummy)
  922. {
  923. if ($search_error == 'messages')
  924. continue;
  925. $context['search_errors']['messages'][] = $txt['error_' . $search_error];
  926. }
  927. }
  928. $context['simple_search'] = isset($context['search_params']['advanced']) ? empty($context['search_params']['advanced']) : !empty($modSettings['simpleSearch']) && !isset($_REQUEST['advanced']);
  929. $context['page_title'] = $txt['pm_search_title'];
  930. $context['sub_template'] = 'search';
  931. $context['linktree'][] = array(
  932. 'url' => $scripturl . '?action=pm;sa=search',
  933. 'name' => $txt['pm_search_bar_title'],
  934. );
  935. }
  936. /**
  937. * Actually do the search of personal messages.
  938. */
  939. function MessageSearch2()
  940. {
  941. global $scripturl, $modSettings, $user_info, $context, $txt;
  942. global $memberContext, $smcFunc;
  943. if (!empty($context['load_average']) && !empty($modSettings['loadavg_search']) && $context['load_average'] >= $modSettings['loadavg_search'])
  944. fatal_lang_error('loadavg_search_disabled', false);
  945. /**
  946. * @todo For the moment force the folder to the inbox.
  947. * @todo Maybe set the inbox based on a cookie or theme setting?
  948. */
  949. $context['folder'] = 'inbox';
  950. // Some useful general permissions.
  951. $context['can_send_pm'] = allowedTo('pm_send');
  952. // Some hardcoded veriables that can be tweaked if required.
  953. $maxMembersToSearch = 500;
  954. // Extract all the search parameters.
  955. $search_params = array();
  956. if (isset($_REQUEST['params']))
  957. {
  958. $temp_params = explode('|"|', base64_decode(strtr($_REQUEST['params'], array(' ' => '+'))));
  959. foreach ($temp_params as $i => $data)
  960. {
  961. @list ($k, $v) = explode('|\'|', $data);
  962. $search_params[$k] = $v;
  963. }
  964. }
  965. $context['start'] = isset($_GET['start']) ? (int) $_GET['start'] : 0;
  966. // Store whether simple search was used (needed if the user wants to do another query).
  967. if (!isset($search_params['advanced']))
  968. $search_params['advanced'] = empty($_REQUEST['advanced']) ? 0 : 1;
  969. // 1 => 'allwords' (default, don't set as param) / 2 => 'anywords'.
  970. if (!empty($search_params['searchtype']) || (!empty($_REQUEST['searchtype']) && $_REQUEST['searchtype'] == 2))
  971. $search_params['searchtype'] = 2;
  972. // Minimum age of messages. Default to zero (don't set param in that case).
  973. if (!empty($search_params['minage']) || (!empty($_REQUEST['minage']) && $_REQUEST['minage'] > 0))
  974. $search_params['minage'] = !empty($search_params['minage']) ? (int) $search_params['minage'] : (int) $_REQUEST['minage'];
  975. // Maximum age of messages. Default to infinite (9999 days: param not set).
  976. if (!empty($search_params['maxage']) || (!empty($_REQUEST['maxage']) && $_REQUEST['maxage'] != 9999))
  977. $search_params['maxage'] = !empty($search_params['maxage']) ? (int) $search_params['maxage'] : (int) $_REQUEST['maxage'];
  978. $search_params['subject_only'] = !empty($search_params['subject_only']) || !empty($_REQUEST['subject_only']);
  979. $search_params['show_complete'] = !empty($search_params['show_complete']) || !empty($_REQUEST['show_complete']);
  980. // Default the user name to a wildcard matching every user (*).
  981. if (!empty($search_params['user_spec']) || (!empty($_REQUEST['userspec']) && $_REQUEST['userspec'] != '*'))
  982. $search_params['userspec'] = isset($search_params['userspec']) ? $search_params['userspec'] : $_REQUEST['userspec'];
  983. // This will be full of all kinds of parameters!
  984. $searchq_parameters = array();
  985. // If there's no specific user, then don't mention it in the main query.
  986. if (empty($search_params['userspec']))
  987. $userQuery = '';
  988. else
  989. {
  990. $userString = strtr($smcFunc['htmlspecialchars']($search_params['userspec'], ENT_QUOTES), array('&quot;' => '"'));
  991. $userString = strtr($userString, array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_'));
  992. preg_match_all('~"([^"]+)"~', $userString, $matches);
  993. $possible_users = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $userString)));
  994. for ($k = 0, $n = count($possible_users); $k < $n; $k++)
  995. {
  996. $possible_users[$k] = trim($possible_users[$k]);
  997. if (strlen($possible_users[$k]) == 0)
  998. unset($possible_users[$k]);
  999. }
  1000. // Who matches those criteria?
  1001. /**
  1002. * @todo This doesn't support sent item searching.
  1003. */
  1004. $request = $smcFunc['db_query']('', '
  1005. SELECT id_member
  1006. FROM {db_prefix}members
  1007. WHERE real_name LIKE {raw:real_name_implode}',
  1008. array(
  1009. 'real_name_implode' => '\'' . implode('\' OR real_name LIKE \'', $possible_users) . '\'',
  1010. )
  1011. );
  1012. // Simply do nothing if there're too many members matching the criteria.
  1013. if ($smcFunc['db_num_rows']($request) > $maxMembersToSearch)
  1014. $userQuery = '';
  1015. elseif ($smcFunc['db_num_rows']($request) == 0)
  1016. {
  1017. $userQuery = 'AND pm.id_member_from = 0 AND (pm.from_name LIKE {raw:guest_user_name_implode})';
  1018. $searchq_parameters['guest_user_name_implode'] = '\'' . implode('\' OR pm.from_name LIKE \'', $possible_users) . '\'';
  1019. }
  1020. else
  1021. {
  1022. $memberlist = array();
  1023. while ($row = $smcFunc['db_fetch_assoc']($request))
  1024. $memberlist[] = $row['id_member'];
  1025. $userQuery = 'AND (pm.id_member_from IN ({array_int:member_list}) OR (pm.id_member_from = 0 AND (pm.from_name LIKE {raw:guest_user_name_implode})))';
  1026. $searchq_parameters['guest_user_name_implode'] = '\'' . implode('\' OR pm.from_name LIKE \'', $possible_users) . '\'';
  1027. $searchq_parameters['member_list'] = $memberlist;
  1028. }
  1029. $smcFunc['db_free_result']($request);
  1030. }
  1031. // Setup the sorting variables...
  1032. /**
  1033. * @todo Add more in here!
  1034. */
  1035. $sort_columns = array(
  1036. 'pm.id_pm',
  1037. );
  1038. if (empty($search_params['sort']) && !empty($_REQUEST['sort']))
  1039. list ($search_params['sort'], $search_params['sort_dir']) = array_pad(explode('|', $_REQUEST['sort']), 2, '');
  1040. $search_params['sort'] = !empty($search_params['sort']) && in_array($search_params['sort'], $sort_columns) ? $search_params['sort'] : 'pm.id_pm';
  1041. $search_params['sort_dir'] = !empty($search_params['sort_dir']) && $search_params['sort_dir'] == 'asc' ? 'asc' : 'desc';
  1042. // Sort out any labels we may be searching by.
  1043. $labelQuery = '';
  1044. if ($context['folder'] == 'inbox' && !empty($search_params['advanced']) && $context['currently_using_labels'])
  1045. {
  1046. // Came here from pagination? Put them back into $_REQUEST for sanitization.
  1047. if (isset($search_params['labels']))
  1048. $_REQUEST['searchlabel'] = explode(',', $search_params['labels']);
  1049. // Assuming we have some labels - make them all integers.
  1050. if (!empty($_REQUEST['searchlabel']) && is_array($_REQUEST['searchlabel']))
  1051. {
  1052. foreach ($_REQUEST['searchlabel'] as $key => $id)
  1053. $_REQUEST['searchlabel'][$key] = (int) $id;
  1054. }
  1055. else
  1056. $_REQUEST['searchlabel'] = array();
  1057. // Now that everything is cleaned up a bit, make the labels a param.
  1058. $search_params['labels'] = implode(',', $_REQUEST['searchlabel']);
  1059. // No labels selected? That must be an error!
  1060. if (empty($_REQUEST['searchlabel']))
  1061. $context['search_errors']['no_labels_selected'] = true;
  1062. // Otherwise prepare the query!
  1063. elseif (count($_REQUEST['searchlabel']) != count($context['labels']))
  1064. {
  1065. $labelQuery = '
  1066. AND {raw:label_implode}';
  1067. $labelStatements = array();
  1068. foreach ($_REQUEST['searchlabel'] as $label)
  1069. $labelStatements[] = $smcFunc['db_quote']('FIND_IN_SET({string:label}, pmr.labels) != 0', array(
  1070. 'label' => $label,
  1071. ));
  1072. $searchq_parameters['label_implode'] = '(' . implode(' OR ', $labelStatements) . ')';
  1073. }
  1074. }
  1075. // What are we actually searching for?
  1076. $search_params['search'] = !empty($search_params['search']) ? $search_params['search'] : (isset($_REQUEST['search']) ? $_REQUEST['search'] : '');
  1077. // If we ain't got nothing - we should error!
  1078. if (!isset($search_params['search']) || $search_params['search'] == '')
  1079. $context['search_errors']['invalid_search_string'] = true;
  1080. // Extract phrase parts first (e.g. some words "this is a phrase" some more words.)
  1081. preg_match_all('~(?:^|\s)([-]?)"([^"]+)"(?:$|\s)~' . ($context['utf8'] ? 'u' : ''), $search_params['search'], $matches, PREG_PATTERN_ORDER);
  1082. $searchArray = $matches[2];
  1083. // Remove the phrase parts and extract the words.
  1084. $tempSearch = explode(' ', preg_replace('~(?:^|\s)(?:[-]?)"(?:[^"]+)"(?:$|\s)~' . ($context['utf8'] ? 'u' : ''), ' ', $search_params['search']));
  1085. // A minus sign in front of a word excludes the word.... so...
  1086. $excludedWords = array();
  1087. // .. first, we check for things like -"some words", but not "-some words".
  1088. foreach ($matches[1] as $index => $word)
  1089. if ($word == '-')
  1090. {
  1091. $word = $smcFunc['strtolower'](trim($searchArray[$index]));
  1092. if (strlen($word) > 0)
  1093. $excludedWords[] = $word;
  1094. unset($searchArray[$index]);
  1095. }
  1096. // Now we look for -test, etc.... normaller.
  1097. foreach ($tempSearch as $index => $word)
  1098. {
  1099. $word = trim($word);
  1100. if ($word[0] == '.')
  1101. {
  1102. $word = substr($smcFunc['strtolower']($word), 1);
  1103. if (strlen($word) > 0)
  1104. $excludedWords[] = $word;
  1105. unset($tempSearch[$index]);
  1106. }
  1107. }
  1108. $searchArray = array_merge($searchArray, $tempSearch);
  1109. // Trim everything and make sure there are no words that are the same.
  1110. foreach ($searchArray as $index => $value)
  1111. {
  1112. $searchArray[$index] = $smcFunc['strtolower'](trim($value));
  1113. if ($searchArray[$index] == '')
  1114. unset($searchArray[$index]);
  1115. else
  1116. {
  1117. // Sort out entities first.
  1118. $searchArray[$index] = $smcFunc['htmlspecialchars']($searchArray[$index]);
  1119. }
  1120. }
  1121. $searchArray = array_unique($searchArray);
  1122. // Create an array of replacements for highlighting.
  1123. $context['mark'] = array();
  1124. foreach ($searchArray as $word)
  1125. $context['mark'][$word] = '<strong class="highlight">' . $word . '</strong>';
  1126. // This contains *everything*
  1127. $searchWords = array_merge($searchArray, $excludedWords);
  1128. // Make sure at least one word is being searched for.
  1129. if (empty($searchArray))
  1130. $context['search_errors']['invalid_search_string'] = true;
  1131. // Sort out the search query so the user can edit it - if they want.
  1132. $context['search_params'] = $search_params;
  1133. if (isset($context['search_params']['search']))
  1134. $context['search_params']['search'] = htmlspecialchars($context['search_params']['search']);
  1135. if (isset($context['search_params']['userspec']))
  1136. $context['search_params']['userspec'] = htmlspecialchars($context['search_params']['userspec']);
  1137. // Now we have all the parameters, combine them together for pagination and the like...
  1138. $context['params'] = array();
  1139. foreach ($search_params as $k => $v)
  1140. $context['params'][] = $k . '|\'|' . $v;
  1141. $context['params'] = base64_encode(implode('|"|', $context['params']));
  1142. // Compile the subject query part.
  1143. $andQueryParts = array();
  1144. foreach ($searchWords as $index => $word)
  1145. {
  1146. if ($word == '')
  1147. continue;
  1148. if ($search_params['subject_only'])
  1149. $andQueryParts[] = 'pm.subject' . (in_array($word, $excludedWords) ? ' NOT' : '') . ' LIKE {string:search_' . $index . '}';
  1150. else
  1151. $andQueryParts[] = '(pm.subject' . (in_array($word, $excludedWords) ? ' NOT' : '') . ' LIKE {string:search_' . $index . '} ' . (in_array($word, $excludedWords) ? 'AND pm.body NOT' : 'OR pm.body') . ' LIKE {string:search_' . $index . '})';
  1152. $searchq_parameters['search_' . $index] = '%' . strtr($word, array('_' => '\\_', '%' => '\\%')) . '%';
  1153. }
  1154. $searchQuery = ' 1=1';
  1155. if (!empty($andQueryParts))
  1156. $searchQuery = implode(!empty($search_params['searchtype']) && $search_params['searchtype'] == 2 ? ' OR ' : ' AND ', $andQueryParts);
  1157. // Age limits?
  1158. $timeQuery = '';
  1159. if (!empty($search_params['minage']))
  1160. $timeQuery .= ' AND pm.msgtime < ' . (time() - $search_params['minage'] * 86400);
  1161. if (!empty($search_params['maxage']))
  1162. $timeQuery .= ' AND pm.msgtime > ' . (time() - $search_params['maxage'] * 86400);
  1163. // If we have errors - return back to the first screen...
  1164. if (!empty($context['search_errors']))
  1165. {
  1166. $_REQUEST['params'] = $context['params'];
  1167. return MessageSearch();
  1168. }
  1169. // Get the amount of results.
  1170. $request = $smcFunc['db_query']('', '
  1171. SELECT COUNT(*)
  1172. FROM {db_prefix}pm_recipients AS pmr
  1173. INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)
  1174. WHERE ' . ($context['folder'] == 'inbox' ? '
  1175. pmr.id_member = {int:current_member}
  1176. AND pmr.deleted = {int:not_deleted}' : '
  1177. pm.id_member_from = {int:current_member}
  1178. AND pm.deleted_by_sender = {int:not_deleted}') . '
  1179. ' . $userQuery . $labelQuery . $timeQuery . '
  1180. AND (' . $searchQuery . ')',
  1181. array_merge($searchq_parameters, array(
  1182. 'current_member' => $user_info['id'],
  1183. 'not_deleted' => 0,
  1184. ))
  1185. );
  1186. list ($numResults) = $smcFunc['db_fetch_row']($request);
  1187. $smcFunc['db_free_result']($request);
  1188. // Get all the matching messages... using standard search only (No caching and the like!)
  1189. // @todo This doesn't support sent item searching yet.
  1190. $request = $smcFunc['db_query']('', '
  1191. SELECT pm.id_pm, pm.id_pm_head, pm.id_member_from
  1192. FROM {db_prefix}pm_recipients AS pmr
  1193. INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)
  1194. WHERE ' . ($context['folder'] == 'inbox' ? '
  1195. pmr.id_member = {int:current_member}
  1196. AND pmr.deleted = {int:not_deleted}' : '
  1197. pm.id_member_from = {int:current_member}
  1198. AND pm.deleted_by_sender = {int:not_deleted}') . '
  1199. ' . $userQuery . $labelQuery . $timeQuery . '
  1200. AND (' . $searchQuery . ')
  1201. ORDER BY ' . $search_params['sort'] . ' ' . $search_params['sort_dir'] . '
  1202. LIMIT ' . $context['start'] . ', ' . $modSettings['search_results_per_page'],
  1203. array_merge($searchq_parameters, array(
  1204. 'current_member' => $user_info['id'],
  1205. 'not_deleted' => 0,
  1206. ))
  1207. );
  1208. $foundMessages = array();
  1209. $posters = array();
  1210. $head_pms = array();
  1211. while ($row = $smcFunc['db_fetch_assoc']($request))
  1212. {
  1213. $foundMessages[] = $row['id_pm'];
  1214. $posters[] = $row['id_member_from'];
  1215. $head_pms[$row['id_pm']] = $row['id_pm_head'];
  1216. }
  1217. $smcFunc['db_free_result']($request);
  1218. // Find the real head pms!
  1219. if ($context['display_mode'] == 2 && !empty($head_pms))
  1220. {
  1221. $request = $smcFunc['db_query']('', '
  1222. SELECT MAX(pm.id_pm) AS id_pm, pm.id_pm_head
  1223. FROM {db_prefix}personal_messages AS pm
  1224. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
  1225. WHERE pm.id_pm_head IN ({array_int:head_pms})
  1226. AND pmr.id_member = {int:current_member}
  1227. AND pmr.deleted = {int:not_deleted}
  1228. GROUP BY pm.id_pm_head
  1229. LIMIT {int:limit}',
  1230. array(
  1231. 'head_pms' => array_unique($head_pms),
  1232. 'current_member' => $user_info['id'],
  1233. 'not_deleted' => 0,
  1234. 'limit' => count($head_pms),
  1235. )
  1236. );
  1237. $real_pm_ids = array();
  1238. while ($row = $smcFunc['db_fetch_assoc']($request))
  1239. $real_pm_ids[$row['id_pm_head']] = $row['id_pm'];
  1240. $smcFunc['db_free_result']($request);
  1241. }
  1242. // Load the users...
  1243. $posters = array_unique($posters);
  1244. if (!empty($posters))
  1245. loadMemberData($posters);
  1246. // Sort out the page index.
  1247. $context['page_index'] = constructPageIndex($scripturl . '?action=pm;sa=search2;params=' . $context['params'], $_GET['start'], $numResults, $modSettings['search_results_per_page'], false);
  1248. $context['message_labels'] = array();
  1249. $context['message_replied'] = array();
  1250. $context['personal_messages'] = array();
  1251. if (!empty($foundMessages))
  1252. {
  1253. // Now get recipients (but don't include bcc-recipients for your inbox, you're not supposed to know :P!)
  1254. $request = $smcFunc['db_query']('', '
  1255. SELECT
  1256. pmr.id_pm, mem_to.id_member AS id_member_to, mem_to.real_name AS to_name,
  1257. pmr.bcc, pmr.labels, pmr.is_read
  1258. FROM {db_prefix}pm_recipients AS pmr
  1259. LEFT JOIN {db_prefix}members AS mem_to ON (mem_to.id_member = pmr.id_member)
  1260. WHERE pmr.id_pm IN ({array_int:message_list})',
  1261. array(
  1262. 'message_list' => $foundMessages,
  1263. )
  1264. );
  1265. while ($row = $smcFunc['db_fetch_assoc']($request))
  1266. {
  1267. if ($context['folder'] == 'sent' || empty($row['bcc']))
  1268. $recipients[$row['id_pm']][empty($row['bcc']) ? 'to' : 'bcc'][] = empty($row['id_member_to']) ? $txt['guest_title'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_to'] . '">' . $row['to_name'] . '</a>';
  1269. if ($row['id_member_to'] == $user_info['id'] && $context['folder'] != 'sent')
  1270. {
  1271. $context['message_replied'][$row['id_pm']] = $row['is_read'] & 2;
  1272. $row['labels'] = $row['labels'] == '' ? array() : explode(',', $row['labels']);
  1273. // This is a special need for linking to messages.
  1274. foreach ($row['labels'] as $v)
  1275. {
  1276. if (isset($context['labels'][(int) $v]))
  1277. $context['message_labels'][$row['id_pm']][(int) $v] = array('id' => $v, 'name' => $context['labels'][(int) $v]['name']);
  1278. // Here we find the first label on a message - for linking to posts in results
  1279. if (!isset($context['first_label'][$row['id_pm']]) && !in_array('-1', $row['labels']))
  1280. $context['first_label'][$row['id_pm']] = (int) $v;
  1281. }
  1282. }
  1283. }
  1284. // Prepare the query for the callback!
  1285. $request = $smcFunc['db_query']('', '
  1286. SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name
  1287. FROM {db_prefix}personal_messages AS pm
  1288. WHERE pm.id_pm IN ({array_int:message_list})
  1289. ORDER BY ' . $search_params['sort'] . ' ' . $search_params['sort_dir'] . '
  1290. LIMIT ' . count($foundMessages),
  1291. array(
  1292. 'message_list' => $foundMessages,
  1293. )
  1294. );
  1295. $counter = 0;
  1296. while ($row = $smcFunc['db_fetch_assoc']($request))
  1297. {
  1298. // If there's no message subject, use the default.
  1299. $row['subject'] = $row['subject'] == '' ? $txt['no_subject'] : $row['subject'];
  1300. // Load this posters context info, if it ain't there then fill in the essentials...
  1301. if (!loadMemberContext($row['id_member_from'], true))
  1302. {
  1303. $memberContext[$row['id_member_from']]['name'] = $row['from_name'];
  1304. $memberContext[$row['id_member_from']]['id'] = 0;
  1305. $memberContext[$row['id_member_from']]['group'] = $txt['guest_title'];
  1306. $memberContext[$row['id_member_from']]['link'] = $row['from_name'];
  1307. $memberContext[$row['id_member_from']]['email'] = '';
  1308. $memberContext[$row['id_member_from']]['show_email'] = showEmailAddress(true, 0);
  1309. $memberContext[$row['id_member_from']]['is_guest'] = true;
  1310. }
  1311. // Censor anything we don't want to see...
  1312. censorText($row['body']);
  1313. censorText($row['subject']);
  1314. // Parse out any BBC...
  1315. $row['body'] = parse_bbc($row['body'], true, 'pm' . $row['id_pm']);
  1316. $href = $scripturl . '?action=pm;f=' . $context['folder'] . (isset($context['first_label'][$row['id_pm']]) ? ';l=' . $context['first_label'][$row['id_pm']] : '') . ';pmid=' . ($context['display_mode'] == 2 && isset($real_pm_ids[$head_pms[$row['id_pm']]]) ? $real_pm_ids[$head_pms[$row['id_pm']]] : $row['id_pm']) . '#msg' . $row['id_pm'];
  1317. $context['personal_messages'][] = array(
  1318. 'id' => $row['id_pm'],
  1319. 'member' => &$memberContext[$row['id_member_from']],
  1320. 'subject' => $row['subject'],
  1321. 'body' => $row['body'],
  1322. 'time' => timeformat($row['msgtime']),
  1323. 'recipients' => &$recipients[$row['id_pm']],
  1324. 'labels' => &$context['message_labels'][$row['id_pm']],
  1325. 'fully_labeled' => count($context['message_labels'][$row['id_pm']]) == count($context['labels']),
  1326. 'is_replied_to' => &$context['message_replied'][$row['id_pm']],
  1327. 'href' => $href,
  1328. 'link' => '<a href="' . $href . '">' . $row['subject'] . '</a>',
  1329. 'counter' => ++$counter,
  1330. );
  1331. }
  1332. $smcFunc['db_free_result']($request);
  1333. }
  1334. // Finish off the context.
  1335. $context['page_title'] = $txt['pm_search_title'];
  1336. $context['sub_template'] = 'search_results';
  1337. $context['menu_data_' . $context['pm_menu_id']]['current_area'] = 'search';
  1338. $context['linktree'][] = array(
  1339. 'url' => $scripturl . '?action=pm;sa=search',
  1340. 'name' => $txt['pm_search_bar_title'],
  1341. );
  1342. }
  1343. /**
  1344. * Send a new message?
  1345. */
  1346. function MessagePost()
  1347. {
  1348. global $txt, $sourcedir, $scripturl, $modSettings;
  1349. global $context, $options, $smcFunc, $language, $user_info;
  1350. isAllowedTo('pm_send');
  1351. loadLanguage('PersonalMessage');
  1352. // Just in case it was loaded from somewhere else.
  1353. if (!WIRELESS)
  1354. {
  1355. loadTemplate('PersonalMessage');
  1356. $context['sub_template'] = 'send';
  1357. }
  1358. // Extract out the spam settings - cause it's neat.
  1359. list ($modSettings['max_pm_recipients'], $modSettings['pm_posts_verification'], $modSettings['pm_posts_per_hour']) = explode(',', $modSettings['pm_spam_settings']);
  1360. // Set the title...
  1361. $context['page_title'] = $txt['send_message'];
  1362. $context['reply'] = isset($_REQUEST['pmsg']) || isset($_REQUEST['quote']);
  1363. // Check whether we've gone over the limit of messages we can send per hour.
  1364. if (!empty($modSettings['pm_posts_per_hour']) && !allowedTo(array('admin_forum', 'moderate_forum', 'send_mail')) && $user_info['mod_cache']['bq'] == '0=1' && $user_info['mod_cache']['gq'] == '0=1')
  1365. {
  1366. // How many messages have they sent this last hour?
  1367. $request = $smcFunc['db_query']('', '
  1368. SELECT COUNT(pr.id_pm) AS post_count
  1369. FROM {db_prefix}personal_messages AS pm
  1370. INNER JOIN {db_prefix}pm_recipients AS pr ON (pr.id_pm = pm.id_pm)
  1371. WHERE pm.id_member_from = {int:current_member}
  1372. AND pm.msgtime > {int:msgtime}',
  1373. array(
  1374. 'current_member' => $user_info['id'],
  1375. 'msgtime' => time() - 3600,
  1376. )
  1377. );
  1378. list ($postCount) = $smcFunc['db_fetch_row']($request);
  1379. $smcFunc['db_free_result']($request);
  1380. if (!empty($postCount) && $postCount >= $modSettings['pm_posts_per_hour'])
  1381. fatal_lang_error('pm_too_many_per_hour', true, array($modSettings['pm_posts_per_hour']));
  1382. }
  1383. // Quoting/Replying to a message?
  1384. if (!empty($_REQUEST['pmsg']))
  1385. {
  1386. $pmsg = (int) $_REQUEST['pmsg'];
  1387. // Make sure this is yours.
  1388. if (!isAccessiblePM($pmsg))
  1389. fatal_lang_error('no_access', false);
  1390. // Work out whether this is one you've received?
  1391. $request = $smcFunc['db_query']('', '
  1392. SELECT
  1393. id_pm
  1394. FROM {db_prefix}pm_recipients
  1395. WHERE id_pm = {int:id_pm}
  1396. AND id_member = {int:current_member}
  1397. LIMIT 1',
  1398. array(
  1399. 'current_member' => $user_info['id'],
  1400. 'id_pm' => $pmsg,
  1401. )
  1402. );
  1403. $isReceived = $smcFunc['db_num_rows']($request) != 0;
  1404. $smcFunc['db_free_result']($request);
  1405. // Get the quoted message (and make sure you're allowed to see this quote!).
  1406. $request = $smcFunc['db_query']('', '
  1407. SELECT
  1408. pm.id_pm, CASE WHEN pm.id_pm_head = {int:id_pm_head_empty} THEN pm.id_pm ELSE pm.id_pm_head END AS pm_head,
  1409. pm.body, pm.subject, pm.msgtime, mem.member_name, IFNULL(mem.id_member, 0) AS id_member,
  1410. IFNULL(mem.real_name, pm.from_name) AS real_name
  1411. FROM {db_prefix}personal_messages AS pm' . (!$isReceived ? '' : '
  1412. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = {int:id_pm})') . '
  1413. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
  1414. WHERE pm.id_pm = {int:id_pm}' . (!$isReceived ? '
  1415. AND pm.id_member_from = {int:current_member}' : '
  1416. AND pmr.id_member = {int:current_member}') . '
  1417. LIMIT 1',
  1418. array(
  1419. 'current_member' => $user_info['id'],
  1420. 'id_pm_head_empty' => 0,
  1421. 'id_pm' => $pmsg,
  1422. )
  1423. );
  1424. if ($smcFunc['db_num_rows']($request) == 0)
  1425. fatal_lang_error('pm_not_yours', false);
  1426. $row_quoted = $smcFunc['db_fetch_assoc']($request);
  1427. $smcFunc['db_free_result']($request);
  1428. // Censor the message.
  1429. censorText($row_quoted['subject']);
  1430. censorText($row_quoted['body']);
  1431. // Add 'Re: ' to it....
  1432. if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
  1433. {
  1434. if ($language === $user_info['language'])
  1435. $context['response_prefix'] = $txt['response_prefix'];
  1436. else
  1437. {
  1438. loadLanguage('index', $language, false);
  1439. $context['response_prefix'] = $txt['response_prefix'];
  1440. loadLanguage('index');
  1441. }
  1442. cache_put_data('response_prefix', $context['response_prefix'], 600);
  1443. }
  1444. $form_subject = $row_quoted['subject'];
  1445. if ($context['reply'] && trim($context['response_prefix']) != '' && $smcFunc['strpos']($form_subject, trim($context['response_prefix'])) !== 0)
  1446. $form_subject = $context['response_prefix'] . $form_subject;
  1447. if (isset($_REQUEST['quote']))
  1448. {
  1449. // Remove any nested quotes and <br />...
  1450. $form_message = preg_replace('~<br ?/?' . '>~i', "\n", $row_quoted['body']);
  1451. if (!empty($modSettings['removeNestedQuotes']))
  1452. $form_message = preg_replace(array('~\n?\[quote.*?\].+?\[/quote\]\n?~is', '~^\n~', '~\[/quote\]~'), '', $form_message);
  1453. if (empty($row_quoted['id_member']))
  1454. $form_message = '[quote author=&quot;' . $row_quoted['real_name'] . '&quot;]' . "\n" . $form_message . "\n" . '[/quote]';
  1455. else
  1456. $form_message = '[quote author=' . $row_quoted['real_name'] . ' link=action=profile;u=' . $row_quoted['id_member'] . ' date=' . $row_quoted['msgtime'] . ']' . "\n" . $form_message . "\n" . '[/quote]';
  1457. }
  1458. else
  1459. $form_message = '';
  1460. // Do the BBC thang on the message.
  1461. $row_quoted['body'] = parse_bbc($row_quoted['body'], true, 'pm' . $row_quoted['id_pm']);
  1462. // Set up the quoted message array.
  1463. $context['quoted_message'] = array(
  1464. 'id' => $row_quoted['id_pm'],
  1465. 'pm_head' => $row_quoted['pm_head'],
  1466. 'member' => array(
  1467. 'name' => $row_quoted['real_name'],
  1468. 'username' => $row_quoted['member_name'],
  1469. 'id' => $row_quoted['id_member'],
  1470. 'href' => !empty($row_quoted['id_member']) ? $scripturl . '?action=profile;u=' . $row_quoted['id_member'] : '',
  1471. 'link' => !empty($row_quoted['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row_quoted['id_member'] . '">' . $row_quoted['real_name'] . '</a>' : $row_quoted['real_name'],
  1472. ),
  1473. 'subject' => $row_quoted['subject'],
  1474. 'time' => timeformat($row_quoted['msgtime']),
  1475. 'timestamp' => forum_time(true, $row_quoted['msgtime']),
  1476. 'body' => $row_quoted['body']
  1477. );
  1478. }
  1479. else
  1480. {
  1481. $context['quoted_message'] = false;
  1482. $form_subject = '';
  1483. $form_message = '';
  1484. }
  1485. $context['recipients'] = array(
  1486. 'to' => array(),
  1487. 'bcc' => array(),
  1488. );
  1489. // Sending by ID? Replying to all? Fetch the real_name(s).
  1490. if (isset($_REQUEST['u']))
  1491. {
  1492. // If the user is replying to all, get all the other members this was sent to..
  1493. if ($_REQUEST['u'] == 'all' && isset($row_quoted))
  1494. {
  1495. // Firstly, to reply to all we clearly already have $row_quoted - so have the original member from.
  1496. if ($row_quoted['id_member'] != $user_info['id'])
  1497. $context['recipients']['to'][] = array(
  1498. 'id' => $row_quoted['id_member'],
  1499. 'name' => htmlspecialchars($row_quoted['real_name']),
  1500. );
  1501. // Now to get the others.
  1502. $request = $smcFunc['db_query']('', '
  1503. SELECT mem.id_member, mem.real_name
  1504. FROM {db_prefix}pm_recipients AS pmr
  1505. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = pmr.id_member)
  1506. WHERE pmr.id_pm = {int:id_pm}
  1507. AND pmr.id_member != {int:current_member}
  1508. AND pmr.bcc = {int:not_bcc}',
  1509. array(
  1510. 'current_member' => $user_info['id'],
  1511. 'id_pm' => $pmsg,
  1512. 'not_bcc' => 0,
  1513. )
  1514. );
  1515. while ($row = $smcFunc['db_fetch_assoc']($request))
  1516. $context['recipients']['to'][] = array(
  1517. 'id' => $row['id_member'],
  1518. 'name' => $row['real_name'],
  1519. );
  1520. $smcFunc['db_free_result']($request);
  1521. }
  1522. else
  1523. {
  1524. $_REQUEST['u'] = explode(',', $_REQUEST['u']);
  1525. foreach ($_REQUEST['u'] as $key => $uID)
  1526. $_REQUEST['u'][$key] = (int) $uID;
  1527. $_REQUEST['u'] = array_unique($_REQUEST['u']);
  1528. $request = $smcFunc['db_query']('', '
  1529. SELECT id_member, real_name
  1530. FROM {db_prefix}members
  1531. WHERE id_member IN ({array_int:member_list})
  1532. LIMIT ' . count($_REQUEST['u']),
  1533. array(
  1534. 'member_list' => $_REQUEST['u'],
  1535. )
  1536. );
  1537. while ($row = $smcFunc['db_fetch_assoc']($request))
  1538. $context['recipients']['to'][] = array(
  1539. 'id' => $row['id_member'],
  1540. 'name' => $row['real_name'],
  1541. );
  1542. $smcFunc['db_free_result']($request);
  1543. }
  1544. // Get a literal name list in case the user has JavaScript disabled.
  1545. $names = array();
  1546. foreach ($context['recipients']['to'] as $to)
  1547. $names[] = $to['name'];
  1548. $context['to_value'] = empty($names) ? '' : '&quot;' . implode('&quot;, &quot;', $names) . '&quot;';
  1549. }
  1550. else
  1551. $context['to_value'] = '';
  1552. // Set the defaults...
  1553. $context['subject'] = $form_subject != '' ? $form_subject : $txt['no_subject'];
  1554. $context['message'] = str_replace(array('"', '<', '>', '&nbsp;'), array('&quot;', '&lt;', '&gt;', ' '), $form_message);
  1555. $context['post_error'] = array();
  1556. $context['copy_to_outbox'] = !empty($options['copy_to_outbox']);
  1557. // And build the link tree.
  1558. $context['linktree'][] = array(
  1559. 'url' => $scripturl . '?action=pm;sa=send',
  1560. 'name' => $txt['new_message']
  1561. );
  1562. $modSettings['disable_wysiwyg'] = !empty($modSettings['disable_wysiwyg']) || empty($modSettings['enableBBC']);
  1563. // Needed for the WYSIWYG editor.
  1564. require_once($sourcedir . '/Subs-Editor.php');
  1565. // Now create the editor.
  1566. $editorOptions = array(
  1567. 'id' => 'message',
  1568. 'value' => $context['message'],
  1569. 'height' => '175px',
  1570. 'width' => '100%',
  1571. 'labels' => array(
  1572. 'post_button' => $txt['send_message'],
  1573. ),
  1574. );
  1575. create_control_richedit($editorOptions);
  1576. // Store the ID for old compatibility.
  1577. $context['post_box_name'] = $editorOptions['id'];
  1578. $context['bcc_value'] = '';
  1579. $context['require_verification'] = !$user_info['is_admin'] && !empty($modSettings['pm_posts_verification']) && $user_info['posts'] < $modSettings['pm_posts_verification'];
  1580. if ($context['require_verification'])
  1581. {
  1582. $verificationOptions = array(
  1583. 'id' => 'pm',
  1584. );
  1585. $context['require_verification'] = create_control_verification($verificationOptions);
  1586. $context['visual_verification_id'] = $verificationOptions['id'];
  1587. }
  1588. // Register this form and get a sequence number in $context.
  1589. checkSubmitOnce('register');
  1590. }
  1591. /**
  1592. * An error in the message...
  1593. *
  1594. * @param $error_types
  1595. * @param $named_recipients
  1596. * @param $recipient_ids
  1597. */
  1598. function messagePostError($error_types, $named_recipients, $recipient_ids = array())
  1599. {
  1600. global $txt, $context, $scripturl, $modSettings;
  1601. global $smcFunc, $user_info, $sourcedir;
  1602. $context['menu_data_' . $context['pm_menu_id']]['current_area'] = 'send';
  1603. if (!WIRELESS)
  1604. $context['sub_template'] = 'send';
  1605. $context['page_title'] = $txt['send_message'];
  1606. // Got some known members?
  1607. $context['recipients'] = array(
  1608. 'to' => array(),
  1609. 'bcc' => array(),
  1610. );
  1611. if (!empty($recipient_ids['to']) || !empty($recipient_ids['bcc']))
  1612. {
  1613. $allRecipients = array_merge($recipient_ids['to'], $recipient_ids['bcc']);
  1614. $request = $smcFunc['db_query']('', '
  1615. SELECT id_member, real_name
  1616. FROM {db_prefix}members
  1617. WHERE id_member IN ({array_int:member_list})',
  1618. array(
  1619. 'member_list' => $allRecipients,
  1620. )
  1621. );
  1622. while ($row = $smcFunc['db_fetch_assoc']($request))
  1623. {
  1624. $recipientType = in_array($row['id_member'], $recipient_ids['bcc']) ? 'bcc' : 'to';
  1625. $context['recipients'][$recipientType][] = array(
  1626. 'id' => $row['id_member'],
  1627. 'name' => $row['real_name'],
  1628. );
  1629. }
  1630. $smcFunc['db_free_result']($request);
  1631. }
  1632. // Set everything up like before....
  1633. $context['subject'] = isset($_REQUEST['subject']) ? $smcFunc['htmlspecialchars']($_REQUEST['subject']) : '';
  1634. $context['message'] = isset($_REQUEST['message']) ? str_replace(array(' '), array('&nbsp; '), $smcFunc['htmlspecialchars']($_REQUEST['message'])) : '';
  1635. $context['copy_to_outbox'] = !empty($_REQUEST['outbox']);
  1636. $context['reply'] = !empty($_REQUEST['replied_to']);
  1637. if ($context['reply'])
  1638. {
  1639. $_REQUEST['replied_to'] = (int) $_REQUEST['replied_to'];
  1640. $request = $smcFunc['db_query']('', '
  1641. SELECT
  1642. pm.id_pm, CASE WHEN pm.id_pm_head = {int:no_id_pm_head} THEN pm.id_pm ELSE pm.id_pm_head END AS pm_head,
  1643. pm.body, pm.subject, pm.msgtime, mem.member_name, IFNULL(mem.id_member, 0) AS id_member,
  1644. IFNULL(mem.real_name, pm.from_name) AS real_name
  1645. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '' : '
  1646. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = {int:replied_to})') . '
  1647. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
  1648. WHERE pm.id_pm = {int:replied_to}' . ($context['folder'] == 'sent' ? '
  1649. AND pm.id_member_from = {int:current_member}' : '
  1650. AND pmr.id_member = {int:current_member}') . '
  1651. LIMIT 1',
  1652. array(
  1653. 'current_member' => $user_info['id'],
  1654. 'no_id_pm_head' => 0,
  1655. 'replied_to' => $_REQUEST['replied_to'],
  1656. )
  1657. );
  1658. if ($smcFunc['db_num_rows']($request) == 0)
  1659. fatal_lang_error('pm_not_yours', false);
  1660. $row_quoted = $smcFunc['db_fetch_assoc']($request);
  1661. $smcFunc['db_free_result']($request);
  1662. censorText($row_quoted['subject']);
  1663. censorText($row_quoted['body']);
  1664. $context['quoted_message'] = array(
  1665. 'id' => $row_quoted['id_pm'],
  1666. 'pm_head' => $row_quoted['pm_head'],
  1667. 'member' => array(
  1668. 'name' => $row_quoted['real_name'],
  1669. 'username' => $row_quoted['member_name'],
  1670. 'id' => $row_quoted['id_member'],
  1671. 'href' => !empty($row_quoted['id_member']) ? $scripturl . '?action=profile;u=' . $row_quoted['id_member'] : '',
  1672. 'link' => !empty($row_quoted['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row_quoted['id_member'] . '">' . $row_quoted['real_name'] . '</a>' : $row_quoted['real_name'],
  1673. ),
  1674. 'subject' => $row_quoted['subject'],
  1675. 'time' => timeformat($row_quoted['msgtime']),
  1676. 'timestamp' => forum_time(true, $row_quoted['msgtime']),
  1677. 'body' => parse_bbc($row_quoted['body'], true, 'pm' . $row_quoted['id_pm']),
  1678. );
  1679. }
  1680. // Build the link tree....
  1681. $context['linktree'][] = array(
  1682. 'url' => $scripturl . '?action=pm;sa=send',
  1683. 'name' => $txt['new_message']
  1684. );
  1685. // Set each of the errors for the template.
  1686. loadLanguage('Errors');
  1687. $context['post_error'] = array(
  1688. 'messages' => array(),
  1689. );
  1690. foreach ($error_types as $error_type)
  1691. {
  1692. $context['post_error'][$error_type] = true;
  1693. if (isset($txt['error_' . $error_type]))
  1694. {
  1695. if ($error_type == 'long_message')
  1696. $txt['error_' . $error_type] = sprintf($txt['error_' . $error_type], $modSettings['max_messageLength']);
  1697. $context['post_error']['messages'][] = $txt['error_' . $error_type];
  1698. }
  1699. }
  1700. // We need to load the editor once more.
  1701. require_once($sourcedir . '/Subs-Editor.php');
  1702. // Create it...
  1703. $editorOptions = array(
  1704. 'id' => 'message',
  1705. 'value' => $context['message'],
  1706. 'width' => '90%',
  1707. 'labels' => array(
  1708. 'post_button' => $txt['send_message'],
  1709. ),
  1710. );
  1711. create_control_richedit($editorOptions);
  1712. // ... and store the ID again...
  1713. $context['post_box_name'] = $editorOptions['id'];
  1714. // Check whether we need to show the code again.
  1715. $context['require_verification'] = !$user_info['is_admin'] && !empty($modSettings['pm_posts_verification']) && $user_info['posts'] < $modSettings['pm_posts_verification'];
  1716. if ($context['require_verification'])
  1717. {
  1718. require_once($sourcedir . '/Subs-Editor.php');
  1719. $verificationOptions = array(
  1720. 'id' => 'pm',
  1721. );
  1722. $context['require_verification'] = create_control_verification($verificationOptions);
  1723. $context['visual_verification_id'] = $verificationOptions['id'];
  1724. }
  1725. $context['to_value'] = empty($named_recipients['to']) ? '' : '&quot;' . implode('&quot;, &quot;', $named_recipients['to']) . '&quot;';
  1726. $context['bcc_value'] = empty($named_recipients['bcc']) ? '' : '&quot;' . implode('&quot;, &quot;', $named_recipients['bcc']) . '&quot;';
  1727. // No check for the previous submission is needed.
  1728. checkSubmitOnce('free');
  1729. // Acquire a new form sequence number.
  1730. checkSubmitOnce('register');
  1731. }
  1732. /**
  1733. * Send it!
  1734. */
  1735. function MessagePost2()
  1736. {
  1737. global $txt, $context, $sourcedir;
  1738. global $user_info, $modSettings, $scripturl, $smcFunc;
  1739. isAllowedTo('pm_send');
  1740. require_once($sourcedir . '/Subs-Auth.php');
  1741. loadLanguage('PersonalMessage', '', false);
  1742. // Extract out the spam settings - it saves database space!
  1743. list ($modSettings['max_pm_recipients'], $modSettings['pm_posts_verification'], $modSettings['pm_posts_per_hour']) = explode(',', $modSettings['pm_spam_settings']);
  1744. // Check whether we've gone over the limit of messages we can send per hour - fatal error if fails!
  1745. if (!empty($modSettings['pm_posts_per_hour']) && !allowedTo(array('admin_forum', 'moderate_forum', 'send_mail')) && $user_info['mod_cache']['bq'] == '0=1' && $user_info['mod_cache']['gq'] == '0=1')
  1746. {
  1747. // How many have they sent this last hour?
  1748. $request = $smcFunc['db_query']('', '
  1749. SELECT COUNT(pr.id_pm) AS post_count
  1750. FROM {db_prefix}personal_messages AS pm
  1751. INNER JOIN {db_prefix}pm_recipients AS pr ON (pr.id_pm = pm.id_pm)
  1752. WHERE pm.id_member_from = {int:current_member}
  1753. AND pm.msgtime > {int:msgtime}',
  1754. array(
  1755. 'current_member' => $user_info['id'],
  1756. 'msgtime' => time() - 3600,
  1757. )
  1758. );
  1759. list ($postCount) = $smcFunc['db_fetch_row']($request);
  1760. $smcFunc['db_free_result']($request);
  1761. if (!empty($postCount) && $postCount >= $modSettings['pm_posts_per_hour'])
  1762. fatal_lang_error('pm_too_many_per_hour', true, array($modSettings['pm_posts_per_hour']));
  1763. }
  1764. // If we came from WYSIWYG then turn it back into BBC regardless.
  1765. if (!empty($_POST['message_mode']) && isset($_POST['message']))
  1766. {
  1767. require_once($sourcedir . '/Subs-Editor.php');
  1768. $_POST['message'] = html_to_bbc($_POST['message']);
  1769. // We need to unhtml it now as it gets done shortly.
  1770. $_POST['message'] = un_htmlspecialchars($_POST['message']);
  1771. // We need this in case of errors etc.
  1772. $_REQUEST['message'] = $_POST['message'];
  1773. }
  1774. // Initialize the errors we're about to make.
  1775. $post_errors = array();
  1776. // If your session timed out, show an error, but do allow to re-submit.
  1777. if (checkSession('post', '', false) != '')
  1778. $post_errors[] = 'session_timeout';
  1779. $_REQUEST['subject'] = isset($_REQUEST['subject']) ? trim($_REQUEST['subject']) : '';
  1780. $_REQUEST['to'] = empty($_POST['to']) ? (empty($_GET['to']) ? '' : $_GET['to']) : $_POST['to'];
  1781. $_REQUEST['bcc'] = empty($_POST['bcc']) ? (empty($_GET['bcc']) ? '' : $_GET['bcc']) : $_POST['bcc'];
  1782. // Route the input from the 'u' parameter to the 'to'-list.
  1783. if (!empty($_POST['u']))
  1784. $_POST['recipient_to'] = explode(',', $_POST['u']);
  1785. // Construct the list of recipients.
  1786. $recipientList = array();
  1787. $namedRecipientList = array();
  1788. $namesNotFound = array();
  1789. foreach (array('to', 'bcc') as $recipientType)
  1790. {
  1791. // First, let's see if there's user ID's given.
  1792. $recipientList[$recipientType] = array();
  1793. if (!empty($_POST['recipient_' . $recipientType]) && is_array($_POST['recipient_' . $recipientType]))
  1794. {
  1795. foreach ($_POST['recipient_' . $recipientType] as $recipient)
  1796. $recipientList[$recipientType][] = (int) $recipient;
  1797. }
  1798. // Are there also literal names set?
  1799. if (!empty($_REQUEST[$recipientType]))
  1800. {
  1801. // We're going to take out the "s anyway ;).
  1802. $recipientString = strtr($_REQUEST[$recipientType], array('\\"' => '"'));
  1803. preg_match_all('~"([^"]+)"~', $recipientString, $matches);
  1804. $namedRecipientList[$recipientType] = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $recipientString))));
  1805. foreach ($namedRecipientList[$recipientType] as $index => $recipient)
  1806. {
  1807. if (strlen(trim($recipient)) > 0)
  1808. $namedRecipientList[$recipientType][$index] = $smcFunc['htmlspecialchars']($smcFunc['strtolower'](trim($recipient)));
  1809. else
  1810. unset($namedRecipientList[$recipientType][$index]);
  1811. }
  1812. if (!empty($namedRecipientList[$recipientType]))
  1813. {
  1814. $foundMembers = findMembers($namedRecipientList[$recipientType]);
  1815. // Assume all are not found, until proven otherwise.
  1816. $namesNotFound[$recipientType] = $namedRecipientList[$recipientType];
  1817. foreach ($foundMembers as $member)
  1818. {
  1819. $testNames = array(
  1820. $smcFunc['strtolower']($member['username']),
  1821. $smcFunc['strtolower']($member['name']),
  1822. $smcFunc['strtolower']($member['email']),
  1823. );
  1824. if (count(array_intersect($testNames, $namedRecipientList[$recipientType])) !== 0)
  1825. {
  1826. $recipientList[$recipientType][] = $member['id'];
  1827. // Get rid of this username, since we found it.
  1828. $namesNotFound[$recipientType] = array_diff($namesNotFound[$recipientType], $testNames);
  1829. }
  1830. }
  1831. }
  1832. }
  1833. // Selected a recipient to be deleted? Remove them now.
  1834. if (!empty($_POST['delete_recipient']))
  1835. $recipientList[$recipientType] = array_diff($recipientList[$recipientType], array((int) $_POST['delete_recipient']));
  1836. // Make sure we don't include the same name twice
  1837. $recipientList[$recipientType] = array_unique($recipientList[$recipientType]);
  1838. }
  1839. // Are we changing the recipients some how?
  1840. $is_recipient_change = !empty($_POST['delete_recipient']) || !empty($_POST['to_submit']) || !empty($_POST['bcc_submit']);
  1841. // Check if there's at least one recipient.
  1842. if (empty($recipientList['to']) && empty($recipientList['bcc']))
  1843. $post_errors[] = 'no_to';
  1844. // Make sure that we remove the members who did get it from the screen.
  1845. if (!$is_recipient_change)
  1846. {
  1847. foreach ($recipientList as $recipientType => $dummy)
  1848. {
  1849. if (!empty($namesNotFound[$recipientType]))
  1850. {
  1851. $post_errors[] = 'bad_' . $recipientType;
  1852. // Since we already have a post error, remove the previous one.
  1853. $post_errors = array_diff($post_errors, array('no_to'));
  1854. foreach ($namesNotFound[$recipientType] as $name)
  1855. $context['send_log']['failed'][] = sprintf($txt['pm_error_user_not_found'], $name);
  1856. }
  1857. }
  1858. }
  1859. // Did they make any mistakes?
  1860. if ($_REQUEST['subject'] == '')
  1861. $post_errors[] = 'no_subject';
  1862. if (!isset($_REQUEST['message']) || $_REQUEST['message'] == '')
  1863. $post_errors[] = 'no_message';
  1864. elseif (!empty($modSettings['max_messageLength']) && $smcFunc['strlen']($_REQUEST['message']) > $modSettings['max_messageLength'])
  1865. $post_errors[] = 'long_message';
  1866. else
  1867. {
  1868. // Preparse the message.
  1869. $message = $_REQUEST['message'];
  1870. preparsecode($message);
  1871. // Make sure there's still some content left without the tags.
  1872. if ($smcFunc['htmltrim'](strip_tags(parse_bbc($smcFunc['htmlspecialchars']($message, ENT_QUOTES), false), '<img>')) === '' && (!allowedTo('admin_forum') || strpos($message, '[html]') === false))
  1873. $post_errors[] = 'no_message';
  1874. }
  1875. // Wrong verification code?
  1876. if (!$user_info['is_admin'] && !empty($modSettings['pm_posts_verification']) && $user_info['posts'] < $modSettings['pm_posts_verification'])
  1877. {
  1878. require_once($sourcedir . '/Subs-Editor.php');
  1879. $verificationOptions = array(
  1880. 'id' => 'pm',
  1881. );
  1882. $context['require_verification'] = create_control_verification($verificationOptions, true);
  1883. if (is_array($context['require_verification']))
  1884. {
  1885. $post_errors = array_merge($post_errors, $context['require_verification']);
  1886. }
  1887. }
  1888. // If they did, give a chance to make ammends.
  1889. if (!empty($post_errors) && !$is_recipient_change && !isset($_REQUEST['preview']))
  1890. return messagePostError($post_errors, $namedRecipientList, $recipientList);
  1891. // Want to take a second glance before you send?
  1892. if (isset($_REQUEST['preview']))
  1893. {
  1894. // Set everything up to be displayed.
  1895. $context['preview_subject'] = $smcFunc['htmlspecialchars']($_REQUEST['subject']);
  1896. $context['preview_message'] = $smcFunc['htmlspecialchars']($_REQUEST['message'], ENT_QUOTES);
  1897. preparsecode($context['preview_message'], true);
  1898. // Parse out the BBC if it is enabled.
  1899. $context['preview_message'] = parse_bbc($context['preview_message']);
  1900. // Censor, as always.
  1901. censorText($context['preview_subject']);
  1902. censorText($context['preview_message']);
  1903. // Set a descriptive title.
  1904. $context['page_title'] = $txt['preview'] . ' - ' . $context['preview_subject'];
  1905. // Pretend they messed up but don't ignore if they really did :P.
  1906. return messagePostError($post_errors, $namedRecipientList, $recipientList);
  1907. }
  1908. // Adding a recipient cause javascript ain't working?
  1909. elseif ($is_recipient_change)
  1910. {
  1911. // Maybe we couldn't find one?
  1912. foreach ($namesNotFound as $recipientType => $names)
  1913. {
  1914. $post_errors[] = 'bad_' . $recipientType;
  1915. foreach ($names as $name)
  1916. $context['send_log']['failed'][] = sprintf($txt['pm_error_user_not_found'], $name);
  1917. }
  1918. return messagePostError(array(), $namedRecipientList, $recipientList);
  1919. }
  1920. // Before we send the PM, let's make sure we don't have an abuse of numbers.
  1921. elseif (!empty($modSettings['max_pm_recipients']) && count($recipientList['to']) + count($recipientList['bcc']) > $modSettings['max_pm_recipients'] && !allowedTo(array('moderate_forum', 'send_mail', 'admin_forum')))
  1922. {
  1923. $context['send_log'] = array(
  1924. 'sent' => array(),
  1925. 'failed' => array(sprintf($txt['pm_too_many_recipients'], $modSettings['max_pm_recipients'])),
  1926. );
  1927. return messagePostError($post_errors, $namedRecipientList, $recipientList);
  1928. }
  1929. // Protect from message spamming.
  1930. spamProtection('pm');
  1931. // Prevent double submission of this form.
  1932. checkSubmitOnce('check');
  1933. // Do the actual sending of the PM.
  1934. if (!empty($recipientList['to']) || !empty($recipientList['bcc']))
  1935. $context['send_log'] = sendpm($recipientList, $_REQUEST['subject'], $_REQUEST['message'], !empty($_REQUEST['outbox']), null, !empty($_REQUEST['pm_head']) ? (int) $_REQUEST['pm_head'] : 0);
  1936. else
  1937. $context['send_log'] = array(
  1938. 'sent' => array(),
  1939. 'failed' => array()
  1940. );
  1941. // Mark the message as "replied to".
  1942. if (!empty($context['send_log']['sent']) && !empty($_REQUEST['replied_to']) && isset($_REQUEST['f']) && $_REQUEST['f'] == 'inbox')
  1943. {
  1944. $smcFunc['db_query']('', '
  1945. UPDATE {db_prefix}pm_recipients
  1946. SET is_read = is_read | 2
  1947. WHERE id_pm = {int:replied_to}
  1948. AND id_member = {int:current_member}',
  1949. array(
  1950. 'current_member' => $user_info['id'],
  1951. 'replied_to' => (int) $_REQUEST['replied_to'],
  1952. )
  1953. );
  1954. }
  1955. // If one or more of the recipient were invalid, go back to the post screen with the failed usernames.
  1956. if (!empty($context['send_log']['failed']))
  1957. return messagePostError($post_errors, $namesNotFound, array(
  1958. 'to' => array_intersect($recipientList['to'], $context['send_log']['failed']),
  1959. 'bcc' => array_intersect($recipientList['bcc'], $context['send_log']['failed'])
  1960. ));
  1961. // Message sent successfully?
  1962. if (!empty($context['send_log']) && empty($context['send_log']['failed']))
  1963. $context['current_label_redirect'] = $context['current_label_redirect'] . ';done=sent';
  1964. // Go back to the where they sent from, if possible...
  1965. redirectexit($context['current_label_redirect']);
  1966. }
  1967. /**
  1968. * This function lists all buddies for wireless protocols.
  1969. */
  1970. function WirelessAddBuddy()
  1971. {
  1972. global $scripturl, $txt, $user_info, $context, $smcFunc;
  1973. isAllowedTo('pm_send');
  1974. $context['page_title'] = $txt['wireless_pm_add_buddy'];
  1975. $current_buddies = empty($_REQUEST['u']) ? array() : explode(',', $_REQUEST['u']);
  1976. foreach ($current_buddies as $key => $buddy)
  1977. $current_buddies[$key] = (int) $buddy;
  1978. $base_url = $scripturl . '?action=pm;sa=send;u=' . (empty($current_buddies) ? '' : implode(',', $current_buddies) . ',');
  1979. $context['pm_href'] = $scripturl . '?action=pm;sa=send' . (empty($current_buddies) ? '' : ';u=' . implode(',', $current_buddies));
  1980. $context['buddies'] = array();
  1981. if (!empty($user_info['buddies']))
  1982. {
  1983. $request = $smcFunc['db_query']('', '
  1984. SELECT id_member, real_name
  1985. FROM {db_prefix}members
  1986. WHERE id_member IN ({array_int:buddy_list})
  1987. ORDER BY real_name
  1988. LIMIT ' . count($user_info['buddies']),
  1989. array(
  1990. 'buddy_list' => $user_info['buddies'],
  1991. )
  1992. );
  1993. while ($row = $smcFunc['db_fetch_assoc']($request))
  1994. $context['buddies'][] = array(
  1995. 'id' => $row['id_member'],
  1996. 'name' => $row['real_name'],
  1997. 'selected' => in_array($row['id_member'], $current_buddies),
  1998. 'add_href' => $base_url . $row['id_member'],
  1999. );
  2000. $smcFunc['db_free_result']($request);
  2001. }
  2002. }
  2003. /**
  2004. * This function performs all additional stuff...
  2005. */
  2006. function MessageActionsApply()
  2007. {
  2008. global $txt, $context, $user_info, $options, $smcFunc;
  2009. checkSession('request');
  2010. if (isset($_REQUEST['del_selected']))
  2011. $_REQUEST['pm_action'] = 'delete';
  2012. if (isset($_REQUEST['pm_action']) && $_REQUEST['pm_action'] != '' && !empty($_REQUEST['pms']) && is_array($_REQUEST['pms']))
  2013. {
  2014. foreach ($_REQUEST['pms'] as $pm)
  2015. $_REQUEST['pm_actions'][(int) $pm] = $_REQUEST['pm_action'];
  2016. }
  2017. if (empty($_REQUEST['pm_actions']))
  2018. redirectexit($context['current_label_redirect']);
  2019. // If we are in conversation, we may need to apply this to every message in the conversation.
  2020. if ($context['display_mode'] == 2 && isset($_REQUEST['conversation']))
  2021. {
  2022. $id_pms = array();
  2023. foreach ($_REQUEST['pm_actions'] as $pm => $dummy)
  2024. $id_pms[] = (int) $pm;
  2025. $request = $smcFunc['db_query']('', '
  2026. SELECT id_pm_head, id_pm
  2027. FROM {db_prefix}personal_messages
  2028. WHERE id_pm IN ({array_int:id_pms})',
  2029. array(
  2030. 'id_pms' => $id_pms,
  2031. )
  2032. );
  2033. $pm_heads = array();
  2034. while ($row = $smcFunc['db_fetch_assoc']($request))
  2035. $pm_heads[$row['id_pm_head']] = $row['id_pm'];
  2036. $smcFunc['db_free_result']($request);
  2037. $request = $smcFunc['db_query']('', '
  2038. SELECT id_pm, id_pm_head
  2039. FROM {db_prefix}personal_messages
  2040. WHERE id_pm_head IN ({array_int:pm_heads})',
  2041. array(
  2042. 'pm_heads' => array_keys($pm_heads),
  2043. )
  2044. );
  2045. // Copy the action from the single to PM to the others.
  2046. while ($row = $smcFunc['db_fetch_assoc']($request))
  2047. {
  2048. if (isset($pm_heads[$row['id_pm_head']]) && isset($_REQUEST['pm_actions'][$pm_heads[$row['id_pm_head']]]))
  2049. $_REQUEST['pm_actions'][$row['id_pm']] = $_REQUEST['pm_actions'][$pm_heads[$row['id_pm_head']]];
  2050. }
  2051. $smcFunc['db_free_result']($request);
  2052. }
  2053. $to_delete = array();
  2054. $to_label = array();
  2055. $label_type = array();
  2056. foreach ($_REQUEST['pm_actions'] as $pm => $action)
  2057. {
  2058. if ($action === 'delete')
  2059. $to_delete[] = (int) $pm;
  2060. else
  2061. {
  2062. if (strpos($action, 'add_') === 0)
  2063. {
  2064. $type = 'add';
  2065. $action = substr($action, 4);
  2066. }
  2067. elseif (strpos($action, 'rem_') === 0)
  2068. {
  2069. $type = 'rem';
  2070. $action = substr($action, 4);
  2071. }
  2072. else
  2073. $type = 'unk';
  2074. if ($action == '-1' || $action == '0' || (int) $action > 0)
  2075. {
  2076. $to_label[(int) $pm] = (int) $action;
  2077. $label_type[(int) $pm] = $type;
  2078. }
  2079. }
  2080. }
  2081. // Deleting, it looks like?
  2082. if (!empty($to_delete))
  2083. deleteMessages($to_delete, $context['display_mode'] == 2 ? null : $context['folder']);
  2084. // Are we labeling anything?
  2085. if (!empty($to_label) && $context['folder'] == 'inbox')
  2086. {
  2087. $updateErrors = 0;
  2088. // Get information about each message...
  2089. $request = $smcFunc['db_query']('', '
  2090. SELECT id_pm, labels
  2091. FROM {db_prefix}pm_recipients
  2092. WHERE id_member = {int:current_member}
  2093. AND id_pm IN ({array_int:to_label})
  2094. LIMIT ' . count($to_label),
  2095. array(
  2096. 'current_member' => $user_info['id'],
  2097. 'to_label' => array_keys($to_label),
  2098. )
  2099. );
  2100. while ($row = $smcFunc['db_fetch_assoc']($request))
  2101. {
  2102. $labels = $row['labels'] == '' ? array('-1') : explode(',', trim($row['labels']));
  2103. // Already exists? Then... unset it!
  2104. $ID_LABEL = array_search($to_label[$row['id_pm']], $labels);
  2105. if ($ID_LABEL !== false && $label_type[$row['id_pm']] !== 'add')
  2106. unset($labels[$ID_LABEL]);
  2107. elseif ($label_type[$row['id_pm']] !== 'rem')
  2108. $labels[] = $to_label[$row['id_pm']];
  2109. if (!empty($options['pm_remove_inbox_label']) && $to_label[$row['id_pm']] != '-1' && ($key = array_search('-1', $labels)) !== false)
  2110. unset($labels[$key]);
  2111. $set = implode(',', array_unique($labels));
  2112. if ($set == '')
  2113. $set = '-1';
  2114. // Check that this string isn't going to be too large for the database.
  2115. if ($set > 60)
  2116. $updateErrors++;
  2117. else
  2118. {
  2119. $smcFunc['db_query']('', '
  2120. UPDATE {db_prefix}pm_recipients
  2121. SET labels = {string:labels}
  2122. WHERE id_pm = {int:id_pm}
  2123. AND id_member = {int:current_member}',
  2124. array(
  2125. 'current_member' => $user_info['id'],
  2126. 'id_pm' => $row['id_pm'],
  2127. 'labels' => $set,
  2128. )
  2129. );
  2130. }
  2131. }
  2132. $smcFunc['db_free_result']($request);
  2133. // Any errors?
  2134. /**
  2135. * @todo Separate the sprintf?
  2136. */
  2137. if (!empty($updateErrors))
  2138. fatal_lang_error('labels_too_many', true, array($updateErrors));
  2139. }
  2140. // Back to the folder.
  2141. $_SESSION['pm_selected'] = array_keys($to_label);
  2142. redirectexit($context['current_label_redirect'] . (count($to_label) == 1 ? '#msg' . $_SESSION['pm_selected'][0] : ''), count($to_label) == 1 && $context['browser']['is_ie']);
  2143. }
  2144. /**
  2145. * Are you sure you want to PERMANENTLY (mostly) delete ALL your messages?
  2146. */
  2147. function MessageKillAllQuery()
  2148. {
  2149. global $txt, $context;
  2150. // Only have to set up the template....
  2151. $context['sub_template'] = 'ask_delete';
  2152. $context['page_title'] = $txt['delete_all'];
  2153. $context['delete_all'] = $_REQUEST['f'] == 'all';
  2154. // And set the folder name...
  2155. $txt['delete_all'] = str_replace('PMBOX', $context['folder'] != 'sent' ? $txt['inbox'] : $txt['sent_items'], $txt['delete_all']);
  2156. }
  2157. /**
  2158. * Delete ALL the messages!
  2159. */
  2160. function MessageKillAll()
  2161. {
  2162. global $context;
  2163. checkSession('get');
  2164. // If all then delete all messages the user has.
  2165. if ($_REQUEST['f'] == 'all')
  2166. deleteMessages(null, null);
  2167. // Otherwise just the selected folder.
  2168. else
  2169. deleteMessages(null, $_REQUEST['f'] != 'sent' ? 'inbox' : 'sent');
  2170. // Done... all gone.
  2171. redirectexit($context['current_label_redirect']);
  2172. }
  2173. /**
  2174. * This function allows the user to delete all messages older than so many days.
  2175. */
  2176. function MessagePrune()
  2177. {
  2178. global $txt, $context, $user_info, $scripturl, $smcFunc;
  2179. // Actually delete the messages.
  2180. if (isset($_REQUEST['age']))
  2181. {
  2182. checkSession();
  2183. // Calculate the time to delete before.
  2184. $deleteTime = max(0, time() - (86400 * (int) $_REQUEST['age']));
  2185. // Array to store the IDs in.
  2186. $toDelete = array();
  2187. // Select all the messages they have sent older than $deleteTime.
  2188. $request = $smcFunc['db_query']('', '
  2189. SELECT id_pm
  2190. FROM {db_prefix}personal_messages
  2191. WHERE deleted_by_sender = {int:not_deleted}
  2192. AND id_member_from = {int:current_member}
  2193. AND msgtime < {int:msgtime}',
  2194. array(
  2195. 'current_member' => $user_info['id'],
  2196. 'not_deleted' => 0,
  2197. 'msgtime' => $deleteTime,
  2198. )
  2199. );
  2200. while ($row = $smcFunc['db_fetch_row']($request))
  2201. $toDelete[] = $row[0];
  2202. $smcFunc['db_free_result']($request);
  2203. // Select all messages in their inbox older than $deleteTime.
  2204. $request = $smcFunc['db_query']('', '
  2205. SELECT pmr.id_pm
  2206. FROM {db_prefix}pm_recipients AS pmr
  2207. INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)
  2208. WHERE pmr.deleted = {int:not_deleted}
  2209. AND pmr.id_member = {int:current_member}
  2210. AND pm.msgtime < {int:msgtime}',
  2211. array(
  2212. 'current_member' => $user_info['id'],
  2213. 'not_deleted' => 0,
  2214. 'msgtime' => $deleteTime,
  2215. )
  2216. );
  2217. while ($row = $smcFunc['db_fetch_assoc']($request))
  2218. $toDelete[] = $row['id_pm'];
  2219. $smcFunc['db_free_result']($request);
  2220. // Delete the actual messages.
  2221. deleteMessages($toDelete);
  2222. // Go back to their inbox.
  2223. redirectexit($context['current_label_redirect']);
  2224. }
  2225. // Build the link tree elements.
  2226. $context['linktree'][] = array(
  2227. 'url' => $scripturl . '?action=pm;sa=prune',
  2228. 'name' => $txt['pm_prune']
  2229. );
  2230. $context['sub_template'] = 'prune';
  2231. $context['page_title'] = $txt['pm_prune'];
  2232. }
  2233. /**
  2234. * Delete the specified personal messages.
  2235. */
  2236. function deleteMessages($personal_messages, $folder = null, $owner = null)
  2237. {
  2238. global $user_info, $smcFunc;
  2239. if ($owner === null)
  2240. $owner = array($user_info['id']);
  2241. elseif (empty($owner))
  2242. return;
  2243. elseif (!is_array($owner))
  2244. $owner = array($owner);
  2245. if ($personal_messages !== null)
  2246. {
  2247. if (empty($personal_messages) || !is_array($personal_messages))
  2248. return;
  2249. foreach ($personal_messages as $index => $delete_id)
  2250. $personal_messages[$index] = (int) $delete_id;
  2251. $where = '
  2252. AND id_pm IN ({array_int:pm_list})';
  2253. }
  2254. else
  2255. $where = '';
  2256. if ($folder == 'sent' || $folder === null)
  2257. {
  2258. $smcFunc['db_query']('', '
  2259. UPDATE {db_prefix}personal_messages
  2260. SET deleted_by_sender = {int:is_deleted}
  2261. WHERE id_member_from IN ({array_int:member_list})
  2262. AND deleted_by_sender = {int:not_deleted}' . $where,
  2263. array(
  2264. 'member_list' => $owner,
  2265. 'is_deleted' => 1,
  2266. 'not_deleted' => 0,
  2267. 'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
  2268. )
  2269. );
  2270. }
  2271. if ($folder != 'sent' || $folder === null)
  2272. {
  2273. // Calculate the number of messages each member's gonna lose...
  2274. $request = $smcFunc['db_query']('', '
  2275. SELECT id_member, COUNT(*) AS num_deleted_messages, CASE WHEN is_read & 1 >= 1 THEN 1 ELSE 0 END AS is_read
  2276. FROM {db_prefix}pm_recipients
  2277. WHERE id_member IN ({array_int:member_list})
  2278. AND deleted = {int:not_deleted}' . $where . '
  2279. GROUP BY id_member, is_read',
  2280. array(
  2281. 'member_list' => $owner,
  2282. 'not_deleted' => 0,
  2283. 'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
  2284. )
  2285. );
  2286. // ...And update the statistics accordingly - now including unread messages!.
  2287. while ($row = $smcFunc['db_fetch_assoc']($request))
  2288. {
  2289. if ($row['is_read'])
  2290. updateMemberData($row['id_member'], array('instant_messages' => $where == '' ? 0 : 'instant_messages - ' . $row['num_deleted_messages']));
  2291. else
  2292. updateMemberData($row['id_member'], array('instant_messages' => $where == '' ? 0 : 'instant_messages - ' . $row['num_deleted_messages'], 'unread_messages' => $where == '' ? 0 : 'unread_messages - ' . $row['num_deleted_messages']));
  2293. // If this is the current member we need to make their message count correct.
  2294. if ($user_info['id'] == $row['id_member'])
  2295. {
  2296. $user_info['messages'] -= $row['num_deleted_messages'];
  2297. if (!($row['is_read']))
  2298. $user_info['unread_messages'] -= $row['num_deleted_messages'];
  2299. }
  2300. }
  2301. $smcFunc['db_free_result']($request);
  2302. // Do the actual deletion.
  2303. $smcFunc['db_query']('', '
  2304. UPDATE {db_prefix}pm_recipients
  2305. SET deleted = {int:is_deleted}
  2306. WHERE id_member IN ({array_int:member_list})
  2307. AND deleted = {int:not_deleted}' . $where,
  2308. array(
  2309. 'member_list' => $owner,
  2310. 'is_deleted' => 1,
  2311. 'not_deleted' => 0,
  2312. 'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
  2313. )
  2314. );
  2315. }
  2316. // If sender and recipients all have deleted their message, it can be removed.
  2317. $request = $smcFunc['db_query']('', '
  2318. SELECT pm.id_pm AS sender, pmr.id_pm
  2319. FROM {db_prefix}personal_messages AS pm
  2320. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm AND pmr.deleted = {int:not_deleted})
  2321. WHERE pm.deleted_by_sender = {int:is_deleted}
  2322. ' . str_replace('id_pm', 'pm.id_pm', $where) . '
  2323. GROUP BY sender, pmr.id_pm
  2324. HAVING pmr.id_pm IS null',
  2325. array(
  2326. 'not_deleted' => 0,
  2327. 'is_deleted' => 1,
  2328. 'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
  2329. )
  2330. );
  2331. $remove_pms = array();
  2332. while ($row = $smcFunc['db_fetch_assoc']($request))
  2333. $remove_pms[] = $row['sender'];
  2334. $smcFunc['db_free_result']($request);
  2335. if (!empty($remove_pms))
  2336. {
  2337. $smcFunc['db_query']('', '
  2338. DELETE FROM {db_prefix}personal_messages
  2339. WHERE id_pm IN ({array_int:pm_list})',
  2340. array(
  2341. 'pm_list' => $remove_pms,
  2342. )
  2343. );
  2344. $smcFunc['db_query']('', '
  2345. DELETE FROM {db_prefix}pm_recipients
  2346. WHERE id_pm IN ({array_int:pm_list})',
  2347. array(
  2348. 'pm_list' => $remove_pms,
  2349. )
  2350. );
  2351. }
  2352. // Any cached numbers may be wrong now.
  2353. cache_put_data('labelCounts:' . $user_info['id'], null, 720);
  2354. }
  2355. // Mark personal messages read.
  2356. function markMessages($personal_messages = null, $label = null, $owner = null)
  2357. {
  2358. global $user_info, $context, $smcFunc;
  2359. if ($owner === null)
  2360. $owner = $user_info['id'];
  2361. $smcFunc['db_query']('', '
  2362. UPDATE {db_prefix}pm_recipients
  2363. SET is_read = is_read | 1
  2364. WHERE id_member = {int:id_member}
  2365. AND NOT (is_read & 1 >= 1)' . ($label === null ? '' : '
  2366. AND FIND_IN_SET({string:label}, labels) != 0') . ($personal_messages !== null ? '
  2367. AND id_pm IN ({array_int:personal_messages})' : ''),
  2368. array(
  2369. 'personal_messages' => $personal_messages,
  2370. 'id_member' => $owner,
  2371. 'label' => $label,
  2372. )
  2373. );
  2374. // If something wasn't marked as read, get the number of unread messages remaining.
  2375. if ($smcFunc['db_affected_rows']() > 0)
  2376. {
  2377. if ($owner == $user_info['id'])
  2378. {
  2379. foreach ($context['labels'] as $label)
  2380. $context['labels'][(int) $label['id']]['unread_messages'] = 0;
  2381. }
  2382. $result = $smcFunc['db_query']('', '
  2383. SELECT labels, COUNT(*) AS num
  2384. FROM {db_prefix}pm_recipients
  2385. WHERE id_member = {int:id_member}
  2386. AND NOT (is_read & 1 >= 1)
  2387. AND deleted = {int:is_not_deleted}
  2388. GROUP BY labels',
  2389. array(
  2390. 'id_member' => $owner,
  2391. 'is_not_deleted' => 0,
  2392. )
  2393. );
  2394. $total_unread = 0;
  2395. while ($row = $smcFunc['db_fetch_assoc']($result))
  2396. {
  2397. $total_unread += $row['num'];
  2398. if ($owner != $user_info['id'])
  2399. continue;
  2400. $this_labels = explode(',', $row['labels']);
  2401. foreach ($this_labels as $this_label)
  2402. $context['labels'][(int) $this_label]['unread_messages'] += $row['num'];
  2403. }
  2404. $smcFunc['db_free_result']($result);
  2405. // Need to store all this.
  2406. cache_put_data('labelCounts:' . $owner, $context['labels'], 720);
  2407. updateMemberData($owner, array('unread_messages' => $total_unread));
  2408. // If it was for the current member, reflect this in the $user_info array too.
  2409. if ($owner == $user_info['id'])
  2410. $user_info['unread_messages'] = $total_unread;
  2411. }
  2412. }
  2413. /**
  2414. * This function handles adding, deleting and editing labels on messages.
  2415. */
  2416. function ManageLabels()
  2417. {
  2418. global $txt, $context, $user_info, $scripturl, $smcFunc;
  2419. // Build the link tree elements...
  2420. $context['linktree'][] = array(
  2421. 'url' => $scripturl . '?action=pm;sa=manlabels',
  2422. 'name' => $txt['pm_manage_labels']
  2423. );
  2424. $context['page_title'] = $txt['pm_manage_labels'];
  2425. $context['sub_template'] = 'labels';
  2426. $the_labels = array();
  2427. // Add all existing labels to the array to save, slashing them as necessary...
  2428. foreach ($context['labels'] as $label)
  2429. {
  2430. if ($label['id'] != -1)
  2431. $the_labels[$label['id']] = $label['name'];
  2432. }
  2433. if (isset($_POST[$context['session_var']]))
  2434. {
  2435. checkSession('post');
  2436. // This will be for updating messages.
  2437. $message_changes = array();
  2438. $new_labels = array();
  2439. $rule_changes = array();
  2440. // Will most likely need this.
  2441. LoadRules();
  2442. // Adding a new label?
  2443. if (isset($_POST['add']))
  2444. {
  2445. $_POST['label'] = strtr($smcFunc['htmlspecialchars'](trim($_POST['label'])), array(',' => '&#044;'));
  2446. if ($smcFunc['strlen']($_POST['label']) > 30)
  2447. $_POST['label'] = $smcFunc['substr']($_POST['label'], 0, 30);
  2448. if ($_POST['label'] != '')
  2449. $the_labels[] = $_POST['label'];
  2450. }
  2451. // Deleting an existing label?
  2452. elseif (isset($_POST['delete'], $_POST['delete_label']))
  2453. {
  2454. $i = 0;
  2455. foreach ($the_labels as $id => $name)
  2456. {
  2457. if (isset($_POST['delete_label'][$id]))
  2458. {
  2459. unset($the_labels[$id]);
  2460. $message_changes[$id] = true;
  2461. }
  2462. else
  2463. $new_labels[$id] = $i++;
  2464. }
  2465. }
  2466. // The hardest one to deal with... changes.
  2467. elseif (isset($_POST['save']) && !empty($_POST['label_name']))
  2468. {
  2469. $i = 0;
  2470. foreach ($the_labels as $id => $name)
  2471. {
  2472. if ($id == -1)
  2473. continue;
  2474. elseif (isset($_POST['label_name'][$id]))
  2475. {
  2476. $_POST['label_name'][$id] = trim(strtr($smcFunc['htmlspecialchars']($_POST['label_name'][$id]), array(',' => '&#044;')));
  2477. if ($smcFunc['strlen']($_POST['label_name'][$id]) > 30)
  2478. $_POST['label_name'][$id] = $smcFunc['substr']($_POST['label_name'][$id], 0, 30);
  2479. if ($_POST['label_name'][$id] != '')
  2480. {
  2481. $the_labels[(int) $id] = $_POST['label_name'][$id];
  2482. $new_labels[$id] = $i++;
  2483. }
  2484. else
  2485. {
  2486. unset($the_labels[(int) $id]);
  2487. $message_changes[(int) $id] = true;
  2488. }
  2489. }
  2490. else
  2491. $new_labels[$id] = $i++;
  2492. }
  2493. }
  2494. // Save the label status.
  2495. updateMemberData($user_info['id'], array('message_labels' => implode(',', $the_labels)));
  2496. // Update all the messages currently with any label changes in them!
  2497. if (!empty($message_changes))
  2498. {
  2499. $searchArray = array_keys($message_changes);
  2500. if (!empty($new_labels))
  2501. {
  2502. for ($i = max($searchArray) + 1, $n = max(array_keys($new_labels)); $i <= $n; $i++)
  2503. $searchArray[] = $i;
  2504. }
  2505. // Now find the messages to change.
  2506. $request = $smcFunc['db_query']('', '
  2507. SELECT id_pm, labels
  2508. FROM {db_prefix}pm_recipients
  2509. WHERE FIND_IN_SET({raw:find_label_implode}, labels) != 0
  2510. AND id_member = {int:current_member}',
  2511. array(
  2512. 'current_member' => $user_info['id'],
  2513. 'find_label_implode' => '\'' . implode('\', labels) != 0 OR FIND_IN_SET(\'', $searchArray) . '\'',
  2514. )
  2515. );
  2516. while ($row = $smcFunc['db_fetch_assoc']($request))
  2517. {
  2518. // Do the long task of updating them...
  2519. $toChange = explode(',', $row['labels']);
  2520. foreach ($toChange as $key => $value)
  2521. if (in_array($value, $searchArray))
  2522. {
  2523. if (isset($new_labels[$value]))
  2524. $toChange[$key] = $new_labels[$value];
  2525. else
  2526. unset($toChange[$key]);
  2527. }
  2528. if (empty($toChange))
  2529. $toChange[] = '-1';
  2530. // Update the message.
  2531. $smcFunc['db_query']('', '
  2532. UPDATE {db_prefix}pm_recipients
  2533. SET labels = {string:new_labels}
  2534. WHERE id_pm = {int:id_pm}
  2535. AND id_member = {int:current_member}',
  2536. array(
  2537. 'current_member' => $user_info['id'],
  2538. 'id_pm' => $row['id_pm'],
  2539. 'new_labels' => implode(',', array_unique($toChange)),
  2540. )
  2541. );
  2542. }
  2543. $smcFunc['db_free_result']($request);
  2544. // Now do the same the rules - check through each rule.
  2545. foreach ($context['rules'] as $k => $rule)
  2546. {
  2547. // Each action...
  2548. foreach ($rule['actions'] as $k2 => $action)
  2549. {
  2550. if ($action['t'] != 'lab' || !in_array($action['v'], $searchArray))
  2551. continue;
  2552. $rule_changes[] = $rule['id'];
  2553. // If we're here we have a label which is either changed or gone...
  2554. if (isset($new_labels[$action['v']]))
  2555. $context['rules'][$k]['actions'][$k2]['v'] = $new_labels[$action['v']];
  2556. else
  2557. unset($context['rules'][$k]['actions'][$k2]);
  2558. }
  2559. }
  2560. }
  2561. // If we have rules to change do so now.
  2562. if (!empty($rule_changes))
  2563. {
  2564. $rule_changes = array_unique($rule_changes);
  2565. // Update/delete as appropriate.
  2566. foreach ($rule_changes as $k => $id)
  2567. if (!empty($context['rules'][$id]['actions']))
  2568. {
  2569. $smcFunc['db_query']('', '
  2570. UPDATE {db_prefix}pm_rules
  2571. SET actions = {string:actions}
  2572. WHERE id_rule = {int:id_rule}
  2573. AND id_member = {int:current_member}',
  2574. array(
  2575. 'current_member' => $user_info['id'],
  2576. 'id_rule' => $id,
  2577. 'actions' => serialize($context['rules'][$id]['actions']),
  2578. )
  2579. );
  2580. unset($rule_changes[$k]);
  2581. }
  2582. // Anything left here means it's lost all actions...
  2583. if (!empty($rule_changes))
  2584. $smcFunc['db_query']('', '
  2585. DELETE FROM {db_prefix}pm_rules
  2586. WHERE id_rule IN ({array_int:rule_list})
  2587. AND id_member = {int:current_member}',
  2588. array(
  2589. 'current_member' => $user_info['id'],
  2590. 'rule_list' => $rule_changes,
  2591. )
  2592. );
  2593. }
  2594. // Make sure we're not caching this!
  2595. cache_put_data('labelCounts:' . $user_info['id'], null, 720);
  2596. // To make the changes appear right away, redirect.
  2597. redirectexit('action=pm;sa=manlabels');
  2598. }
  2599. }
  2600. /**
  2601. * Allows to edit Personal Message Settings.
  2602. *
  2603. * @uses Profile.php
  2604. * @uses Profile-Modify.php
  2605. * @uses Profile template.
  2606. * @uses Profile language file.
  2607. */
  2608. function MessageSettings()
  2609. {
  2610. global $txt, $user_settings, $user_info, $context, $sourcedir, $smcFunc;
  2611. global $scripturl, $profile_vars, $cur_profile, $user_profile;
  2612. // Need this for the display.
  2613. require_once($sourcedir . '/Profile.php');
  2614. require_once($sourcedir . '/Profile-Modify.php');
  2615. // We want them to submit back to here.
  2616. $context['profile_custom_submit_url'] = $scripturl . '?action=pm;sa=settings;save';
  2617. loadMemberData($user_info['id'], false, 'profile');
  2618. $cur_profile = $user_profile[$user_info['id']];
  2619. loadLanguage('Profile');
  2620. loadTemplate('Profile');
  2621. $context['page_title'] = $txt['pm_settings'];
  2622. $context['user']['is_owner'] = true;
  2623. $context['id_member'] = $user_info['id'];
  2624. $context['require_password'] = false;
  2625. $context['menu_item_selected'] = 'settings';
  2626. $context['submit_button_text'] = $txt['pm_settings'];
  2627. $context['profile_header_text'] = $txt['personal_messages'];
  2628. // Add our position to the linktree.
  2629. $context['linktree'][] = array(
  2630. 'url' => $scripturl . '?action=pm;sa=settings',
  2631. 'name' => $txt['pm_settings']
  2632. );
  2633. // Are they saving?
  2634. if (isset($_REQUEST['save']))
  2635. {
  2636. checkSession('post');
  2637. // Mimic what profile would do.
  2638. $_POST = htmltrim__recursive($_POST);
  2639. $_POST = htmlspecialchars__recursive($_POST);
  2640. // Save the fields.
  2641. saveProfileFields();
  2642. if (!empty($profile_vars))
  2643. updateMemberData($user_info['id'], $profile_vars);
  2644. }
  2645. // Load up the fields.
  2646. pmprefs($user_info['id']);
  2647. }
  2648. /**
  2649. * Allows the user to report a personal message to an administrator.
  2650. * In the first instance requires that the ID of the message to report
  2651. * is passed through $_GET.
  2652. * It allows the user to report to either a particular administrator - or
  2653. * the whole admin team.
  2654. * It will forward on a copy of the original message without allowing the
  2655. * reporter to make changes.
  2656. *
  2657. * @uses report_message sub-template.
  2658. */
  2659. function ReportMessage()
  2660. {
  2661. global $txt, $context, $scripturl, $sourcedir;
  2662. global $user_info, $language, $modSettings, $smcFunc;
  2663. // Check that this feature is even enabled!
  2664. if (empty($modSettings['enableReportPM']) || empty($_REQUEST['pmsg']))
  2665. fatal_lang_error('no_access', false);
  2666. $pmsg = (int) $_REQUEST['pmsg'];
  2667. if (!isAccessiblePM($pmsg, 'inbox'))
  2668. fatal_lang_error('no_access', false);
  2669. $context['pm_id'] = $pmsg;
  2670. $context['page_title'] = $txt['pm_report_title'];
  2671. // If we're here, just send the user to the template, with a few useful context bits.
  2672. if (!isset($_POST['report']))
  2673. {
  2674. $context['sub_template'] = 'report_message';
  2675. /**
  2676. * @todo I don't like being able to pick who to send it to. Favoritism, etc. sucks.
  2677. */
  2678. // Now, get all the administrators.
  2679. $request = $smcFunc['db_query']('', '
  2680. SELECT id_member, real_name
  2681. FROM {db_prefix}members
  2682. WHERE id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0
  2683. ORDER BY real_name',
  2684. array(
  2685. 'admin_group' => 1,
  2686. )
  2687. );
  2688. $context['admins'] = array();
  2689. while ($row = $smcFunc['db_fetch_assoc']($request))
  2690. $context['admins'][$row['id_member']] = $row['real_name'];
  2691. $smcFunc['db_free_result']($request);
  2692. // How many admins in total?
  2693. $context['admin_count'] = count($context['admins']);
  2694. }
  2695. // Otherwise, let's get down to the sending stuff.
  2696. else
  2697. {
  2698. // Check the session before proceeding any further!
  2699. checkSession('post');
  2700. // First, pull out the message contents, and verify it actually went to them!
  2701. $request = $smcFunc['db_query']('', '
  2702. SELECT pm.subject, pm.body, pm.msgtime, pm.id_member_from, IFNULL(m.real_name, pm.from_name) AS sender_name
  2703. FROM {db_prefix}personal_messages AS pm
  2704. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
  2705. LEFT JOIN {db_prefix}members AS m ON (m.id_member = pm.id_member_from)
  2706. WHERE pm.id_pm = {int:id_pm}
  2707. AND pmr.id_member = {int:current_member}
  2708. AND pmr.deleted = {int:not_deleted}
  2709. LIMIT 1',
  2710. array(
  2711. 'current_member' => $user_info['id'],
  2712. 'id_pm' => $context['pm_id'],
  2713. 'not_deleted' => 0,
  2714. )
  2715. );
  2716. // Can only be a hacker here!
  2717. if ($smcFunc['db_num_rows']($request) == 0)
  2718. fatal_lang_error('no_access', false);
  2719. list ($subject, $body, $time, $memberFromID, $memberFromName) = $smcFunc['db_fetch_row']($request);
  2720. $smcFunc['db_free_result']($request);
  2721. // Remove the line breaks...
  2722. $body = preg_replace('~<br ?/?' . '>~i', "\n", $body);
  2723. // Get any other recipients of the email.
  2724. $request = $smcFunc['db_query']('', '
  2725. SELECT mem_to.id_member AS id_member_to, mem_to.real_name AS to_name, pmr.bcc
  2726. FROM {db_prefix}pm_recipients AS pmr
  2727. LEFT JOIN {db_prefix}members AS mem_to ON (mem_to.id_member = pmr.id_member)
  2728. WHERE pmr.id_pm = {int:id_pm}
  2729. AND pmr.id_member != {int:current_member}',
  2730. array(
  2731. 'current_member' => $user_info['id'],
  2732. 'id_pm' => $context['pm_id'],
  2733. )
  2734. );
  2735. $recipients = array();
  2736. $hidden_recipients = 0;
  2737. while ($row = $smcFunc['db_fetch_assoc']($request))
  2738. {
  2739. // If it's hidden still don't reveal their names - privacy after all ;)
  2740. if ($row['bcc'])
  2741. $hidden_recipients++;
  2742. else
  2743. $recipients[] = '[url=' . $scripturl . '?action=profile;u=' . $row['id_member_to'] . ']' . $row['to_name'] . '[/url]';
  2744. }
  2745. $smcFunc['db_free_result']($request);
  2746. if ($hidden_recipients)
  2747. $recipients[] = sprintf($txt['pm_report_pm_hidden'], $hidden_recipients);
  2748. // Now let's get out and loop through the admins.
  2749. $request = $smcFunc['db_query']('', '
  2750. SELECT id_member, real_name, lngfile
  2751. FROM {db_prefix}members
  2752. WHERE (id_group = {int:admin_id} OR FIND_IN_SET({int:admin_id}, additional_groups) != 0)
  2753. ' . (empty($_POST['ID_ADMIN']) ? '' : 'AND id_member = {int:specific_admin}') . '
  2754. ORDER BY lngfile',
  2755. array(
  2756. 'admin_id' => 1,
  2757. 'specific_admin' => isset($_POST['ID_ADMIN']) ? (int) $_POST['ID_ADMIN'] : 0,
  2758. )
  2759. );
  2760. // Maybe we shouldn't advertise this?
  2761. if ($smcFunc['db_num_rows']($request) == 0)
  2762. fatal_lang_error('no_access', false);
  2763. $memberFromName = un_htmlspecialchars($memberFromName);
  2764. // Prepare the message storage array.
  2765. $messagesToSend = array();
  2766. // Loop through each admin, and add them to the right language pile...
  2767. while ($row = $smcFunc['db_fetch_assoc']($request))
  2768. {
  2769. // Need to send in the correct language!
  2770. $cur_language = empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'];
  2771. if (!isset($messagesToSend[$cur_language]))
  2772. {
  2773. loadLanguage('PersonalMessage', $cur_language, false);
  2774. // Make the body.
  2775. $report_body = str_replace(array('{REPORTER}', '{SENDER}'), array(un_htmlspecialchars($user_info['name']), $memberFromName), $txt['pm_report_pm_user_sent']);
  2776. $report_body .= "\n" . '[b]' . $_POST['reason'] . '[/b]' . "\n\n";
  2777. if (!empty($recipients))
  2778. $report_body .= $txt['pm_report_pm_other_recipients'] . ' ' . implode(', ', $recipients) . "\n\n";
  2779. $report_body .= $txt['pm_report_pm_unedited_below'] . "\n" . '[quote author=' . (empty($memberFromID) ? '&quot;' . $memberFromName . '&quot;' : $memberFromName . ' link=action=profile;u=' . $memberFromID . ' date=' . $time) . ']' . "\n" . un_htmlspecialchars($body) . '[/quote]';
  2780. // Plonk it in the array ;)
  2781. $messagesToSend[$cur_language] = array(
  2782. 'subject' => ($smcFunc['strpos']($subject, $txt['pm_report_pm_subject']) === false ? $txt['pm_report_pm_subject'] : '') . un_htmlspecialchars($subject),
  2783. 'body' => $report_body,
  2784. 'recipients' => array(
  2785. 'to' => array(),
  2786. 'bcc' => array()
  2787. ),
  2788. );
  2789. }
  2790. // Add them to the list.
  2791. $messagesToSend[$cur_language]['recipients']['to'][$row['id_member']] = $row['id_member'];
  2792. }
  2793. $smcFunc['db_free_result']($request);
  2794. // Send a different email for each language.
  2795. foreach ($messagesToSend as $lang => $message)
  2796. sendpm($message['recipients'], $message['subject'], $message['body']);
  2797. // Give the user their own language back!
  2798. if (!empty($modSettings['userLanguage']))
  2799. loadLanguage('PersonalMessage', '', false);
  2800. // Leave them with a template.
  2801. $context['sub_template'] = 'report_message_complete';
  2802. }
  2803. }
  2804. /**
  2805. * List all rules, and allow adding/entering etc...
  2806. */
  2807. function ManageRules()
  2808. {
  2809. global $txt, $context, $user_info, $scripturl, $smcFunc;
  2810. // The link tree - gotta have this :o
  2811. $context['linktree'][] = array(
  2812. 'url' => $scripturl . '?action=pm;sa=manrules',
  2813. 'name' => $txt['pm_manage_rules']
  2814. );
  2815. $context['page_title'] = $txt['pm_manage_rules'];
  2816. $context['sub_template'] = 'rules';
  2817. // Load them... load them!!
  2818. LoadRules();
  2819. // Likely to need all the groups!
  2820. $request = $smcFunc['db_query']('', '
  2821. SELECT mg.id_group, mg.group_name, IFNULL(gm.id_member, 0) AS can_moderate, mg.hidden
  2822. FROM {db_prefix}membergroups AS mg
  2823. LEFT JOIN {db_prefix}group_moderators AS gm ON (gm.id_group = mg.id_group AND gm.id_member = {int:current_member})
  2824. WHERE mg.min_posts = {int:min_posts}
  2825. AND mg.id_group != {int:moderator_group}
  2826. AND mg.hidden = {int:not_hidden}
  2827. ORDER BY mg.group_name',
  2828. array(
  2829. 'current_member' => $user_info['id'],
  2830. 'min_posts' => -1,
  2831. 'moderator_group' => 3,
  2832. 'not_hidden' => 0,
  2833. )
  2834. );
  2835. $context['groups'] = array();
  2836. while ($row = $smcFunc['db_fetch_assoc']($request))
  2837. {
  2838. // Hide hidden groups!
  2839. if ($row['hidden'] && !$row['can_moderate'] && !allowedTo('manage_membergroups'))
  2840. continue;
  2841. $context['groups'][$row['id_group']] = $row['group_name'];
  2842. }
  2843. $smcFunc['db_free_result']($request);
  2844. // Applying all rules?
  2845. if (isset($_GET['apply']))
  2846. {
  2847. checkSession('get');
  2848. ApplyRules(true);
  2849. redirectexit('action=pm;sa=manrules');
  2850. }
  2851. // Editing a specific one?
  2852. if (isset($_GET['add']))
  2853. {
  2854. $context['rid'] = isset($_GET['rid']) && isset($context['rules'][$_GET['rid']])? (int) $_GET['rid'] : 0;
  2855. $context['sub_template'] = 'add_rule';
  2856. // Current rule information...
  2857. if ($context['rid'])
  2858. {
  2859. $context['rule'] = $context['rules'][$context['rid']];
  2860. $members = array();
  2861. // Need to get member names!
  2862. foreach ($context['rule']['criteria'] as $k => $criteria)
  2863. if ($criteria['t'] == 'mid' && !empty($criteria['v']))
  2864. $members[(int) $criteria['v']] = $k;
  2865. if (!empty($members))
  2866. {
  2867. $request = $smcFunc['db_query']('', '
  2868. SELECT id_member, member_name
  2869. FROM {db_prefix}members
  2870. WHERE id_member IN ({array_int:member_list})',
  2871. array(
  2872. 'member_list' => array_keys($members),
  2873. )
  2874. );
  2875. while ($row = $smcFunc['db_fetch_assoc']($request))
  2876. $context['rule']['criteria'][$members[$row['id_member']]]['v'] = $row['member_name'];
  2877. $smcFunc['db_free_result']($request);
  2878. }
  2879. }
  2880. else
  2881. $context['rule'] = array(
  2882. 'id' => '',
  2883. 'name' => '',
  2884. 'criteria' => array(),
  2885. 'actions' => array(),
  2886. 'logic' => 'and',
  2887. );
  2888. }
  2889. // Saving?
  2890. elseif (isset($_GET['save']))
  2891. {
  2892. checkSession('post');
  2893. $context['rid'] = isset($_GET['rid']) && isset($context['rules'][$_GET['rid']])? (int) $_GET['rid'] : 0;
  2894. // Name is easy!
  2895. $ruleName = $smcFunc['htmlspecialchars'](trim($_POST['rule_name']));
  2896. if (empty($ruleName))
  2897. fatal_lang_error('pm_rule_no_name', false);
  2898. // Sanity check...
  2899. if (empty($_POST['ruletype']) || empty($_POST['acttype']))
  2900. fatal_lang_error('pm_rule_no_criteria', false);
  2901. // Let's do the criteria first - it's also hardest!
  2902. $criteria = array();
  2903. foreach ($_POST['ruletype'] as $ind => $type)
  2904. {
  2905. // Check everything is here...
  2906. if ($type == 'gid' && (!isset($_POST['ruledefgroup'][$ind]) || !isset($context['groups'][$_POST['ruledefgroup'][$ind]])))
  2907. continue;
  2908. elseif ($type != 'bud' && !isset($_POST['ruledef'][$ind]))
  2909. continue;
  2910. // Members need to be found.
  2911. if ($type == 'mid')
  2912. {
  2913. $name = trim($_POST['ruledef'][$ind]);
  2914. $request = $smcFunc['db_query']('', '
  2915. SELECT id_member
  2916. FROM {db_prefix}members
  2917. WHERE real_name = {string:member_name}
  2918. OR member_name = {string:member_name}',
  2919. array(
  2920. 'member_name' => $name,
  2921. )
  2922. );
  2923. if ($smcFunc['db_num_rows']($request) == 0)
  2924. continue;
  2925. list ($memID) = $smcFunc['db_fetch_row']($request);
  2926. $smcFunc['db_free_result']($request);
  2927. $criteria[] = array('t' => 'mid', 'v' => $memID);
  2928. }
  2929. elseif ($type == 'bud')
  2930. $criteria[] = array('t' => 'bud', 'v' => 1);
  2931. elseif ($type == 'gid')
  2932. $criteria[] = array('t' => 'gid', 'v' => (int) $_POST['ruledefgroup'][$ind]);
  2933. elseif (in_array($type, array('sub', 'msg')) && trim($_POST['ruledef'][$ind]) != '')
  2934. $criteria[] = array('t' => $type, 'v' => $smcFunc['htmlspecialchars'](trim($_POST['ruledef'][$ind])));
  2935. }
  2936. // Also do the actions!
  2937. $actions = array();
  2938. $doDelete = 0;
  2939. $isOr = $_POST['rule_logic'] == 'or' ? 1 : 0;
  2940. foreach ($_POST['acttype'] as $ind => $type)
  2941. {
  2942. // Picking a valid label?
  2943. if ($type == 'lab' && (!isset($_POST['labdef'][$ind]) || !isset($context['labels'][$_POST['labdef'][$ind] - 1])))
  2944. continue;
  2945. // Record what we're doing.
  2946. if ($type == 'del')
  2947. $doDelete = 1;
  2948. elseif ($type == 'lab')
  2949. $actions[] = array('t' => 'lab', 'v' => (int) $_POST['labdef'][$ind] - 1);
  2950. }
  2951. if (empty($criteria) || (empty($actions) && !$doDelete))
  2952. fatal_lang_error('pm_rule_no_criteria', false);
  2953. // What are we storing?
  2954. $criteria = serialize($criteria);
  2955. $actions = serialize($actions);
  2956. // Create the rule?
  2957. if (empty($context['rid']))
  2958. $smcFunc['db_insert']('',
  2959. '{db_prefix}pm_rules',
  2960. array(
  2961. 'id_member' => 'int', 'rule_name' => 'string', 'criteria' => 'string', 'actions' => 'string',
  2962. 'delete_pm' => 'int', 'is_or' => 'int',
  2963. ),
  2964. array(
  2965. $user_info['id'], $ruleName, $criteria, $actions, $doDelete, $isOr,
  2966. ),
  2967. array('id_rule')
  2968. );
  2969. else
  2970. $smcFunc['db_query']('', '
  2971. UPDATE {db_prefix}pm_rules
  2972. SET rule_name = {string:rule_name}, criteria = {string:criteria}, actions = {string:actions},
  2973. delete_pm = {int:delete_pm}, is_or = {int:is_or}
  2974. WHERE id_rule = {int:id_rule}
  2975. AND id_member = {int:current_member}',
  2976. array(
  2977. 'current_member' => $user_info['id'],
  2978. 'delete_pm' => $doDelete,
  2979. 'is_or' => $isOr,
  2980. 'id_rule' => $context['rid'],
  2981. 'rule_name' => $ruleName,
  2982. 'criteria' => $criteria,
  2983. 'actions' => $actions,
  2984. )
  2985. );
  2986. redirectexit('action=pm;sa=manrules');
  2987. }
  2988. // Deleting?
  2989. elseif (isset($_POST['delselected']) && !empty($_POST['delrule']))
  2990. {
  2991. checkSession('post');
  2992. $toDelete = array();
  2993. foreach ($_POST['delrule'] as $k => $v)
  2994. $toDelete[] = (int) $k;
  2995. if (!empty($toDelete))
  2996. $smcFunc['db_query']('', '
  2997. DELETE FROM {db_prefix}pm_rules
  2998. WHERE id_rule IN ({array_int:delete_list})
  2999. AND id_member = {int:current_member}',
  3000. array(
  3001. 'current_member' => $user_info['id'],
  3002. 'delete_list' => $toDelete,
  3003. )
  3004. );
  3005. redirectexit('action=pm;sa=manrules');
  3006. }
  3007. }
  3008. /**
  3009. * This will apply rules to all unread messages. If all_messages is set will, clearly, do it to all!
  3010. *
  3011. * @param bool $all_messages = false
  3012. */
  3013. function ApplyRules($all_messages = false)
  3014. {
  3015. global $user_info, $smcFunc, $context, $options;
  3016. // Want this - duh!
  3017. loadRules();
  3018. // No rules?
  3019. if (empty($context['rules']))
  3020. return;
  3021. // Just unread ones?
  3022. $ruleQuery = $all_messages ? '' : ' AND pmr.is_new = 1';
  3023. /**
  3024. * @todo Apply all should have timeout protection!
  3025. */
  3026. // Get all the messages that match this.
  3027. $request = $smcFunc['db_query']('', '
  3028. SELECT
  3029. pmr.id_pm, pm.id_member_from, pm.subject, pm.body, mem.id_group, pmr.labels
  3030. FROM {db_prefix}pm_recipients AS pmr
  3031. INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)
  3032. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
  3033. WHERE pmr.id_member = {int:current_member}
  3034. AND pmr.deleted = {int:not_deleted}
  3035. ' . $ruleQuery,
  3036. array(
  3037. 'current_member' => $user_info['id'],
  3038. 'not_deleted' => 0,
  3039. )
  3040. );
  3041. $actions = array();
  3042. while ($row = $smcFunc['db_fetch_assoc']($request))
  3043. {
  3044. foreach ($context['rules'] as $rule)
  3045. {
  3046. $match = false;
  3047. // Loop through all the criteria hoping to make a match.
  3048. foreach ($rule['criteria'] as $criterium)
  3049. {
  3050. if (($criterium['t'] == 'mid' && $criterium['v'] == $row['id_member_from']) || ($criterium['t'] == 'gid' && $criterium['v'] == $row['id_group']) || ($criterium['t'] == 'sub' && strpos($row['subject'], $criterium['v']) !== false) || ($criterium['t'] == 'msg' && strpos($row['body'], $criterium['v']) !== false))
  3051. $match = true;
  3052. // If we're adding and one criteria don't match then we stop!
  3053. elseif ($rule['logic'] == 'and')
  3054. {
  3055. $match = false;
  3056. break;
  3057. }
  3058. }
  3059. // If we have a match the rule must be true - act!
  3060. if ($match)
  3061. {
  3062. if ($rule['delete'])
  3063. $actions['deletes'][] = $row['id_pm'];
  3064. else
  3065. {
  3066. foreach ($rule['actions'] as $ruleAction)
  3067. {
  3068. if ($ruleAction['t'] == 'lab')
  3069. {
  3070. // Get a basic pot started!
  3071. if (!isset($actions['labels'][$row['id_pm']]))
  3072. $actions['labels'][$row['id_pm']] = empty($row['labels']) ? array() : explode(',', $row['labels']);
  3073. $actions['labels'][$row['id_pm']][] = $ruleAction['v'];
  3074. }
  3075. }
  3076. }
  3077. }
  3078. }
  3079. }
  3080. $smcFunc['db_free_result']($request);
  3081. // Deletes are easy!
  3082. if (!empty($actions['deletes']))
  3083. deleteMessages($actions['deletes']);
  3084. // Relabel?
  3085. if (!empty($actions['labels']))
  3086. {
  3087. foreach ($actions['labels'] as $pm => $labels)
  3088. {
  3089. // Quickly check each label is valid!
  3090. $realLabels = array();
  3091. foreach ($context['labels'] as $label)
  3092. if (in_array($label['id'], $labels) && ($label['id'] != -1 || empty($options['pm_remove_inbox_label'])))
  3093. $realLabels[] = $label['id'];
  3094. $smcFunc['db_query']('', '
  3095. UPDATE {db_prefix}pm_recipients
  3096. SET labels = {string:new_labels}
  3097. WHERE id_pm = {int:id_pm}
  3098. AND id_member = {int:current_member}',
  3099. array(
  3100. 'current_member' => $user_info['id'],
  3101. 'id_pm' => $pm,
  3102. 'new_labels' => empty($realLabels) ? '' : implode(',', $realLabels),
  3103. )
  3104. );
  3105. }
  3106. }
  3107. }
  3108. /**
  3109. * Load up all the rules for the current user.
  3110. *
  3111. * @param bool $reload = false
  3112. */
  3113. function LoadRules($reload = false)
  3114. {
  3115. global $user_info, $context, $smcFunc;
  3116. if (isset($context['rules']) && !$reload)
  3117. return;
  3118. $request = $smcFunc['db_query']('', '
  3119. SELECT
  3120. id_rule, rule_name, criteria, actions, delete_pm, is_or
  3121. FROM {db_prefix}pm_rules
  3122. WHERE id_member = {int:current_member}',
  3123. array(
  3124. 'current_member' => $user_info['id'],
  3125. )
  3126. );
  3127. $context['rules'] = array();
  3128. // Simply fill in the data!
  3129. while ($row = $smcFunc['db_fetch_assoc']($request))
  3130. {
  3131. $context['rules'][$row['id_rule']] = array(
  3132. 'id' => $row['id_rule'],
  3133. 'name' => $row['rule_name'],
  3134. 'criteria' => unserialize($row['criteria']),
  3135. 'actions' => unserialize($row['actions']),
  3136. 'delete' => $row['delete_pm'],
  3137. 'logic' => $row['is_or'] ? 'or' : 'and',
  3138. );
  3139. if ($row['delete_pm'])
  3140. $context['rules'][$row['id_rule']]['actions'][] = array('t' => 'del', 'v' => 1);
  3141. }
  3142. $smcFunc['db_free_result']($request);
  3143. }
  3144. // Check if the PM is available to the current user.
  3145. function isAccessiblePM($pmID, $validFor = 'in_or_outbox')
  3146. {
  3147. global $user_info, $smcFunc;
  3148. $request = $smcFunc['db_query']('', '
  3149. SELECT
  3150. pm.id_member_from = {int:id_current_member} AND pm.deleted_by_sender = {int:not_deleted} AS valid_for_outbox,
  3151. pmr.id_pm IS NOT NULL AS valid_for_inbox
  3152. FROM {db_prefix}personal_messages AS pm
  3153. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm AND pmr.id_member = {int:id_current_member} AND pmr.deleted = {int:not_deleted})
  3154. WHERE pm.id_pm = {int:id_pm}
  3155. AND ((pm.id_member_from = {int:id_current_member} AND pm.deleted_by_sender = {int:not_deleted}) OR pmr.id_pm IS NOT NULL)',
  3156. array(
  3157. 'id_pm' => $pmID,
  3158. 'id_current_member' => $user_info['id'],
  3159. 'not_deleted' => 0,
  3160. )
  3161. );
  3162. if ($smcFunc['db_num_rows']($request) === 0)
  3163. {
  3164. $smcFunc['db_free_result']($request);
  3165. return false;
  3166. }
  3167. $validationResult = $smcFunc['db_fetch_assoc']($request);
  3168. $smcFunc['db_free_result']($request);
  3169. switch ($validFor)
  3170. {
  3171. case 'inbox':
  3172. return !empty($validationResult['valid_for_inbox']);
  3173. break;
  3174. case 'outbox':
  3175. return !empty($validationResult['valid_for_outbox']);
  3176. break;
  3177. case 'in_or_outbox':
  3178. return !empty($validationResult['valid_for_inbox']) || !empty($validationResult['valid_for_outbox']);
  3179. break;
  3180. default:
  3181. trigger_error('Undefined validation type given', E_USER_ERROR);
  3182. break;
  3183. }
  3184. }
  3185. ?>