PersonalMessage.php 123 KB

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