12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713 |
- <?php
- /**
- * This file is mainly meant for controlling the actions related to personal
- * messages. It allows viewing, sending, deleting, and marking personal
- * messages. For compatibility reasons, they are often called "instant messages".
- *
- * Simple Machines Forum (SMF)
- *
- * @package SMF
- * @author Simple Machines http://www.simplemachines.org
- * @copyright 2012 Simple Machines Forum contributors
- * @license http://www.simplemachines.org/about/smf/license.php BSD
- *
- * @version 2.1 Alpha 1
- */
- if (!defined('SMF'))
- die('Hacking attempt...');
- /**
- * This helps organize things...
- * @todo this should be a simple dispatcher....
- */
- function MessageMain()
- {
- global $txt, $scripturl, $sourcedir, $context, $user_info, $user_settings, $smcFunc, $modSettings;
- // No guests!
- is_not_guest();
- // You're not supposed to be here at all, if you can't even read PMs.
- isAllowedTo('pm_read');
- // This file contains the basic functions for sending a PM.
- require_once($sourcedir . '/Subs-Post.php');
- loadLanguage('PersonalMessage+Drafts');
- if (WIRELESS && WIRELESS_PROTOCOL == 'wap')
- fatal_lang_error('wireless_error_notyet', false);
- elseif (WIRELESS)
- $context['sub_template'] = WIRELESS_PROTOCOL . '_pm';
- elseif (!isset($_REQUEST['xml']))
- loadTemplate('PersonalMessage');
- // Load up the members maximum message capacity.
- if ($user_info['is_admin'])
- $context['message_limit'] = 0;
- elseif (($context['message_limit'] = cache_get_data('msgLimit:' . $user_info['id'], 360)) === null)
- {
- // @todo Why do we do this? It seems like if they have any limit we should use it.
- $request = $smcFunc['db_query']('', '
- SELECT MAX(max_messages) AS top_limit, MIN(max_messages) AS bottom_limit
- FROM {db_prefix}membergroups
- WHERE id_group IN ({array_int:users_groups})',
- array(
- 'users_groups' => $user_info['groups'],
- )
- );
- list ($maxMessage, $minMessage) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- $context['message_limit'] = $minMessage == 0 ? 0 : $maxMessage;
- // Save us doing it again!
- cache_put_data('msgLimit:' . $user_info['id'], $context['message_limit'], 360);
- }
- // Prepare the context for the capacity bar.
- if (!empty($context['message_limit']))
- {
- $bar = ($user_info['messages'] * 100) / $context['message_limit'];
- $context['limit_bar'] = array(
- 'messages' => $user_info['messages'],
- 'allowed' => $context['message_limit'],
- 'percent' => $bar,
- 'bar' => min(100, (int) $bar),
- 'text' => sprintf($txt['pm_currently_using'], $user_info['messages'], round($bar, 1)),
- );
- }
- // a previous message was sent successfully? show a small indication.
- if (isset($_GET['done']) && ($_GET['done'] == 'sent'))
- $context['pm_sent'] = true;
- // Now we have the labels, and assuming we have unsorted mail, apply our rules!
- if ($user_settings['new_pm'])
- {
- $context['labels'] = $user_settings['message_labels'] == '' ? array() : explode(',', $user_settings['message_labels']);
- foreach ($context['labels'] as $id_label => $label_name)
- $context['labels'][(int) $id_label] = array(
- 'id' => $id_label,
- 'name' => trim($label_name),
- 'messages' => 0,
- 'unread_messages' => 0,
- );
- $context['labels'][-1] = array(
- 'id' => -1,
- 'name' => $txt['pm_msg_label_inbox'],
- 'messages' => 0,
- 'unread_messages' => 0,
- );
- ApplyRules();
- updateMemberData($user_info['id'], array('new_pm' => 0));
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}pm_recipients
- SET is_new = {int:not_new}
- WHERE id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'not_new' => 0,
- )
- );
- }
- // Load the label data.
- if ($user_settings['new_pm'] || ($context['labels'] = cache_get_data('labelCounts:' . $user_info['id'], 720)) === null)
- {
- $context['labels'] = $user_settings['message_labels'] == '' ? array() : explode(',', $user_settings['message_labels']);
- foreach ($context['labels'] as $id_label => $label_name)
- $context['labels'][(int) $id_label] = array(
- 'id' => $id_label,
- 'name' => trim($label_name),
- 'messages' => 0,
- 'unread_messages' => 0,
- );
- $context['labels'][-1] = array(
- 'id' => -1,
- 'name' => $txt['pm_msg_label_inbox'],
- 'messages' => 0,
- 'unread_messages' => 0,
- );
- // Looks like we need to reseek!
- $result = $smcFunc['db_query']('', '
- SELECT labels, is_read, COUNT(*) AS num
- FROM {db_prefix}pm_recipients
- WHERE id_member = {int:current_member}
- AND deleted = {int:not_deleted}
- GROUP BY labels, is_read',
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($result))
- {
- $this_labels = explode(',', $row['labels']);
- foreach ($this_labels as $this_label)
- {
- $context['labels'][(int) $this_label]['messages'] += $row['num'];
- if (!($row['is_read'] & 1))
- $context['labels'][(int) $this_label]['unread_messages'] += $row['num'];
- }
- }
- $smcFunc['db_free_result']($result);
- // Store it please!
- cache_put_data('labelCounts:' . $user_info['id'], $context['labels'], 720);
- }
- // This determines if we have more labels than just the standard inbox.
- $context['currently_using_labels'] = count($context['labels']) > 1 ? 1 : 0;
- // Some stuff for the labels...
- $context['current_label_id'] = isset($_REQUEST['l']) && isset($context['labels'][(int) $_REQUEST['l']]) ? (int) $_REQUEST['l'] : -1;
- $context['current_label'] = &$context['labels'][(int) $context['current_label_id']]['name'];
- $context['folder'] = !isset($_REQUEST['f']) || $_REQUEST['f'] != 'sent' ? 'inbox' : 'sent';
- // This is convenient. Do you know how annoying it is to do this every time?!
- $context['current_label_redirect'] = 'action=pm;f=' . $context['folder'] . (isset($_GET['start']) ? ';start=' . $_GET['start'] : '') . (isset($_REQUEST['l']) ? ';l=' . $_REQUEST['l'] : '');
- $context['can_issue_warning'] = in_array('w', $context['admin_features']) && allowedTo('issue_warning') && $modSettings['warning_settings'][0] == 1;
- // Are PM drafts enabled?
- $context['drafts_pm_save'] = !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_pm_enabled']) && allowedTo('pm_draft');
- $context['drafts_autosave'] = !empty($context['drafts_pm_save']) && !empty($modSettings['drafts_autosave_enabled']) && allowedTo('pm_autosave_draft');
-
- // Build the linktree for all the actions...
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm',
- 'name' => $txt['personal_messages']
- );
- // Preferences...
- $context['display_mode'] = WIRELESS ? 0 : $user_settings['pm_prefs'] & 3;
- $subActions = array(
- 'addbuddy' => 'WirelessAddBuddy',
- 'manlabels' => 'ManageLabels',
- 'manrules' => 'ManageRules',
- 'pmactions' => 'MessageActionsApply',
- 'prune' => 'MessagePrune',
- 'removeall' => 'MessageKillAllQuery',
- 'removeall2' => 'MessageKillAll',
- 'report' => 'ReportMessage',
- 'search' => 'MessageSearch',
- 'search2' => 'MessageSearch2',
- 'send' => 'MessagePost',
- 'send2' => 'MessagePost2',
- 'settings' => 'MessageSettings',
- 'showpmdrafts' => 'MessageDrafts',
- );
- if (!isset($_REQUEST['sa']) || !isset($subActions[$_REQUEST['sa']]))
- MessageFolder();
- else
- {
- if (!isset($_REQUEST['xml']))
- messageIndexBar($_REQUEST['sa']);
- $subActions[$_REQUEST['sa']]();
- }
- }
- /**
- * A menu to easily access different areas of the PM section
- *
- * @param string $area
- */
- function messageIndexBar($area)
- {
- global $txt, $context, $scripturl, $sourcedir, $sc, $modSettings, $settings, $user_info, $options;
- $pm_areas = array(
- 'folders' => array(
- 'title' => $txt['pm_messages'],
- 'areas' => array(
- 'send' => array(
- 'label' => $txt['new_message'],
- 'custom_url' => $scripturl . '?action=pm;sa=send',
- 'permission' => allowedTo('pm_send'),
- ),
- 'inbox' => array(
- 'label' => $txt['inbox'],
- 'custom_url' => $scripturl . '?action=pm',
- ),
- 'sent' => array(
- 'label' => $txt['sent_items'],
- 'custom_url' => $scripturl . '?action=pm;f=sent',
- ),
- 'drafts' => array(
- 'label' => $txt['drafts_show'],
- 'custom_url' => $scripturl . '?action=pm;sa=showpmdrafts',
- 'permission' => allowedTo('pm_draft'),
- 'enabled' => !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_pm_enabled']),
- ),
- ),
- ),
- 'labels' => array(
- 'title' => $txt['pm_labels'],
- 'areas' => array(),
- ),
- 'actions' => array(
- 'title' => $txt['pm_actions'],
- 'areas' => array(
- 'search' => array(
- 'label' => $txt['pm_search_bar_title'],
- 'custom_url' => $scripturl . '?action=pm;sa=search',
- ),
- 'prune' => array(
- 'label' => $txt['pm_prune'],
- 'custom_url' => $scripturl . '?action=pm;sa=prune'
- ),
- ),
- ),
- 'pref' => array(
- 'title' => $txt['pm_preferences'],
- 'areas' => array(
- 'manlabels' => array(
- 'label' => $txt['pm_manage_labels'],
- 'custom_url' => $scripturl . '?action=pm;sa=manlabels',
- ),
- 'manrules' => array(
- 'label' => $txt['pm_manage_rules'],
- 'custom_url' => $scripturl . '?action=pm;sa=manrules',
- ),
- 'settings' => array(
- 'label' => $txt['pm_settings'],
- 'custom_url' => $scripturl . '?action=pm;sa=settings',
- ),
- ),
- ),
- );
- // Handle labels.
- if (empty($context['currently_using_labels']))
- unset($pm_areas['labels']);
- else
- {
- // Note we send labels by id as it will have less problems in the querystring.
- $unread_in_labels = 0;
- foreach ($context['labels'] as $label)
- {
- if ($label['id'] == -1)
- continue;
- // Count the amount of unread items in labels.
- $unread_in_labels += $label['unread_messages'];
- // Add the label to the menu.
- $pm_areas['labels']['areas']['label' . $label['id']] = array(
- 'label' => $label['name'] . (!empty($label['unread_messages']) ? ' (<strong>' . $label['unread_messages'] . '</strong>)' : ''),
- 'custom_url' => $scripturl . '?action=pm;l=' . $label['id'],
- 'unread_messages' => $label['unread_messages'],
- 'messages' => $label['messages'],
- );
- }
- if (!empty($unread_in_labels))
- $pm_areas['labels']['title'] .= ' (' . $unread_in_labels . ')';
- }
- $pm_areas['folders']['areas']['inbox']['unread_messages'] = &$context['labels'][-1]['unread_messages'];
- $pm_areas['folders']['areas']['inbox']['messages'] = &$context['labels'][-1]['messages'];
- if (!empty($context['labels'][-1]['unread_messages']))
- {
- $pm_areas['folders']['areas']['inbox']['label'] .= ' (<strong>' . $context['labels'][-1]['unread_messages'] . '</strong>)';
- $pm_areas['folders']['title'] .= ' (' . $context['labels'][-1]['unread_messages'] . ')';
- }
- // Do we have a limit on the amount of messages we can keep?
- if (!empty($context['message_limit']))
- {
- $bar = round(($user_info['messages'] * 100) / $context['message_limit'], 1);
- $context['limit_bar'] = array(
- 'messages' => $user_info['messages'],
- 'allowed' => $context['message_limit'],
- 'percent' => $bar,
- 'bar' => $bar > 100 ? 100 : (int) $bar,
- 'text' => sprintf($txt['pm_currently_using'], $user_info['messages'], $bar)
- );
- }
- require_once($sourcedir . '/Subs-Menu.php');
- // What page is this, again?
- $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'] : '');
- // Set a few options for the menu.
- $menuOptions = array(
- 'current_area' => $area,
- 'disable_url_session_check' => true,
- );
- // Actually create the menu!
- $pm_include_data = createMenu($pm_areas, $menuOptions);
- unset($pm_areas);
- // No menu means no access.
- if (!$pm_include_data && (!$user_info['is_guest'] || validateSession()))
- fatal_lang_error('no_access', false);
- // Make a note of the Unique ID for this menu.
- $context['pm_menu_id'] = $context['max_menu_id'];
- $context['pm_menu_name'] = 'menu_data_' . $context['pm_menu_id'];
- // Set the selected item.
- $current_area = $pm_include_data['current_area'];
- $context['menu_item_selected'] = $current_area;
- // Set the template for this area and add the profile layer.
- if (!WIRELESS && !isset($_REQUEST['xml']))
- $context['template_layers'][] = 'pm';
- }
- /**
- * A folder, ie. inbox/sent etc.
- */
- function MessageFolder()
- {
- global $txt, $scripturl, $modSettings, $context, $subjects_request;
- global $messages_request, $user_info, $recipients, $options, $smcFunc, $memberContext, $user_settings;
- // Changing view?
- if (isset($_GET['view']))
- {
- $context['display_mode'] = $context['display_mode'] > 1 ? 0 : $context['display_mode'] + 1;
- updateMemberData($user_info['id'], array('pm_prefs' => ($user_settings['pm_prefs'] & 252) | $context['display_mode']));
- }
- // Make sure the starting location is valid.
- if (isset($_GET['start']) && $_GET['start'] != 'new')
- $_GET['start'] = (int) $_GET['start'];
- elseif (!isset($_GET['start']) && !empty($options['view_newest_pm_first']))
- $_GET['start'] = 0;
- else
- $_GET['start'] = 'new';
- // Set up some basic theme stuff.
- $context['from_or_to'] = $context['folder'] != 'sent' ? 'from' : 'to';
- $context['get_pmessage'] = 'prepareMessageContext';
- $context['signature_enabled'] = substr($modSettings['signature_settings'], 0, 1) == 1;
- $context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
- $labelQuery = $context['folder'] != 'sent' ? '
- AND FIND_IN_SET(' . $context['current_label_id'] . ', pmr.labels) != 0' : '';
- // Set the index bar correct!
- messageIndexBar($context['current_label_id'] == -1 ? $context['folder'] : 'label' . $context['current_label_id']);
- // Sorting the folder.
- $sort_methods = array(
- 'date' => 'pm.id_pm',
- 'name' => 'IFNULL(mem.real_name, \'\')',
- 'subject' => 'pm.subject',
- );
- // They didn't pick one, use the forum default.
- if (!isset($_GET['sort']) || !isset($sort_methods[$_GET['sort']]))
- {
- $context['sort_by'] = 'date';
- $_GET['sort'] = 'pm.id_pm';
- // An overriding setting?
- $descending = !empty($options['view_newest_pm_first']);
- }
- // Otherwise use the defaults: ascending, by date.
- else
- {
- $context['sort_by'] = $_GET['sort'];
- $_GET['sort'] = $sort_methods[$_GET['sort']];
- $descending = isset($_GET['desc']);
- }
- $context['sort_direction'] = $descending ? 'down' : 'up';
- // Set the text to resemble the current folder.
- $pmbox = $context['folder'] != 'sent' ? $txt['inbox'] : $txt['sent_items'];
- $txt['delete_all'] = str_replace('PMBOX', $pmbox, $txt['delete_all']);
- // Now, build the link tree!
- if ($context['current_label_id'] == -1)
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm;f=' . $context['folder'],
- 'name' => $pmbox
- );
- // Build it further for a label.
- if ($context['current_label_id'] != -1)
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm;f=' . $context['folder'] . ';l=' . $context['current_label_id'],
- 'name' => $txt['pm_current_label'] . ': ' . $context['current_label']
- );
- // Figure out how many messages there are.
- if ($context['folder'] == 'sent')
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
- FROM {db_prefix}personal_messages AS pm
- WHERE pm.id_member_from = {int:current_member}
- AND pm.deleted_by_sender = {int:not_deleted}',
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- )
- );
- else
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
- FROM {db_prefix}pm_recipients AS pmr' . ($context['display_mode'] == 2 ? '
- INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)' : '') . '
- WHERE pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:not_deleted}' . $labelQuery,
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- )
- );
- list ($max_messages) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- // Only show the button if there are messages to delete.
- $context['show_delete'] = $max_messages > 0;
- // Start on the last page.
- if (!is_numeric($_GET['start']) || $_GET['start'] >= $max_messages)
- $_GET['start'] = ($max_messages - 1) - (($max_messages - 1) % $modSettings['defaultMaxMessages']);
- elseif ($_GET['start'] < 0)
- $_GET['start'] = 0;
- // ... but wait - what if we want to start from a specific message?
- if (isset($_GET['pmid']))
- {
- $pmID = (int) $_GET['pmid'];
- // Make sure you have access to this PM.
- if (!isAccessiblePM($pmID, $context['folder'] == 'sent' ? 'outbox' : 'inbox'))
- fatal_lang_error('no_access', false);
- $context['current_pm'] = $pmID;
- // With only one page of PM's we're gonna want page 1.
- if ($max_messages <= $modSettings['defaultMaxMessages'])
- $_GET['start'] = 0;
- // If we pass kstart we assume we're in the right place.
- elseif (!isset($_GET['kstart']))
- {
- if ($context['folder'] == 'sent')
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
- FROM {db_prefix}personal_messages
- WHERE id_member_from = {int:current_member}
- AND deleted_by_sender = {int:not_deleted}
- AND id_pm ' . ($descending ? '>' : '<') . ' {int:id_pm}',
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- 'id_pm' => $pmID,
- )
- );
- else
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
- FROM {db_prefix}pm_recipients AS pmr' . ($context['display_mode'] == 2 ? '
- INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)' : '') . '
- WHERE pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:not_deleted}' . $labelQuery . '
- AND pmr.id_pm ' . ($descending ? '>' : '<') . ' {int:id_pm}',
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- 'id_pm' => $pmID,
- )
- );
- list ($_GET['start']) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- // To stop the page index's being abnormal, start the page on the page the message would normally be located on...
- $_GET['start'] = $modSettings['defaultMaxMessages'] * (int) ($_GET['start'] / $modSettings['defaultMaxMessages']);
- }
- }
- // Sanitize and validate pmsg variable if set.
- if (isset($_GET['pmsg']))
- {
- $pmsg = (int) $_GET['pmsg'];
- if (!isAccessiblePM($pmsg, $context['folder'] == 'sent' ? 'outbox' : 'inbox'))
- fatal_lang_error('no_access', false);
- }
- // Set up the page index.
- $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']);
- $context['start'] = $_GET['start'];
- // Determine the navigation context (especially useful for the wireless template).
- $context['links'] = array(
- 'first' => $_GET['start'] >= $modSettings['defaultMaxMessages'] ? $scripturl . '?action=pm;start=0' : '',
- 'prev' => $_GET['start'] >= $modSettings['defaultMaxMessages'] ? $scripturl . '?action=pm;start=' . ($_GET['start'] - $modSettings['defaultMaxMessages']) : '',
- 'next' => $_GET['start'] + $modSettings['defaultMaxMessages'] < $max_messages ? $scripturl . '?action=pm;start=' . ($_GET['start'] + $modSettings['defaultMaxMessages']) : '',
- 'last' => $_GET['start'] + $modSettings['defaultMaxMessages'] < $max_messages ? $scripturl . '?action=pm;start=' . (floor(($max_messages - 1) / $modSettings['defaultMaxMessages']) * $modSettings['defaultMaxMessages']) : '',
- 'up' => $scripturl,
- );
- $context['page_info'] = array(
- 'current_page' => $_GET['start'] / $modSettings['defaultMaxMessages'] + 1,
- 'num_pages' => floor(($max_messages - 1) / $modSettings['defaultMaxMessages']) + 1
- );
- // First work out what messages we need to see - if grouped is a little trickier...
- if ($context['display_mode'] == 2)
- {
- // On a non-default sort due to PostgreSQL we have to do a harder sort.
- if ($smcFunc['db_title'] == 'PostgreSQL' && $_GET['sort'] != 'pm.id_pm')
- {
- $sub_request = $smcFunc['db_query']('', '
- SELECT MAX({raw:sort}) AS sort_param, pm.id_pm_head
- FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($context['sort_by'] == 'name' ? '
- LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
- AND pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:not_deleted}
- ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})') : '') . '
- WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
- AND pm.deleted_by_sender = {int:not_deleted}' : '1=1') . (empty($pmsg) ? '' : '
- AND pm.id_pm = {int:id_pm}') . '
- GROUP BY pm.id_pm_head
- ORDER BY sort_param' . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
- LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
- 'id_pm' => isset($pmsg) ? $pmsg : '0',
- 'sort' => $_GET['sort'],
- )
- );
- $sub_pms = array();
- while ($row = $smcFunc['db_fetch_assoc']($sub_request))
- $sub_pms[$row['id_pm_head']] = $row['sort_param'];
- $smcFunc['db_free_result']($sub_request);
- $request = $smcFunc['db_query']('', '
- SELECT pm.id_pm AS id_pm, pm.id_pm_head
- FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($context['sort_by'] == 'name' ? '
- LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
- AND pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:not_deleted}
- ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})') : '') . '
- WHERE ' . (empty($sub_pms) ? '0=1' : 'pm.id_pm IN ({array_int:pm_list})') . '
- ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
- LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
- array(
- 'current_member' => $user_info['id'],
- 'pm_list' => array_keys($sub_pms),
- 'not_deleted' => 0,
- 'sort' => $_GET['sort'],
- 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
- )
- );
- }
- else
- {
- $request = $smcFunc['db_query']('pm_conversation_list', '
- SELECT MAX(pm.id_pm) AS id_pm, pm.id_pm_head
- FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($context['sort_by'] == 'name' ? '
- LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
- AND pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:deleted_by}
- ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})') : '') . '
- WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
- AND pm.deleted_by_sender = {int:deleted_by}' : '1=1') . (empty($pmsg) ? '' : '
- AND pm.id_pm = {int:pmsg}') . '
- GROUP BY pm.id_pm_head
- ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($_GET['pmsg']) ? '
- LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
- array(
- 'current_member' => $user_info['id'],
- 'deleted_by' => 0,
- 'sort' => $_GET['sort'],
- 'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
- 'pmsg' => isset($pmsg) ? (int) $pmsg : 0,
- )
- );
- }
- }
- // This is kinda simple!
- else
- {
- // @todo SLOW This query uses a filesort. (inbox only.)
- $request = $smcFunc['db_query']('', '
- SELECT pm.id_pm, pm.id_pm_head, pm.id_member_from
- FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '' . ($context['sort_by'] == 'name' ? '
- LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
- AND pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:is_deleted}
- ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})') : '') . '
- WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {raw:current_member}
- AND pm.deleted_by_sender = {int:is_deleted}' : '1=1') . (empty($pmsg) ? '' : '
- AND pm.id_pm = {int:pmsg}') . '
- ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'pmr.id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
- LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
- array(
- 'current_member' => $user_info['id'],
- 'is_deleted' => 0,
- 'sort' => $_GET['sort'],
- 'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
- 'pmsg' => isset($pmsg) ? (int) $pmsg : 0,
- )
- );
- }
- // Load the id_pms and initialize recipients.
- $pms = array();
- $lastData = array();
- $posters = $context['folder'] == 'sent' ? array($user_info['id']) : array();
- $recipients = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- if (!isset($recipients[$row['id_pm']]))
- {
- if (isset($row['id_member_from']))
- $posters[$row['id_pm']] = $row['id_member_from'];
- $pms[$row['id_pm']] = $row['id_pm'];
- $recipients[$row['id_pm']] = array(
- 'to' => array(),
- 'bcc' => array()
- );
- }
- // Keep track of the last message so we know what the head is without another query!
- if ((empty($pmID) && (empty($options['view_newest_pm_first']) || !isset($lastData))) || empty($lastData) || (!empty($pmID) && $pmID == $row['id_pm']))
- $lastData = array(
- 'id' => $row['id_pm'],
- 'head' => $row['id_pm_head'],
- );
- }
- $smcFunc['db_free_result']($request);
- // Make sure that we have been given a correct head pm id!
- if ($context['display_mode'] == 2 && !empty($pmID) && $pmID != $lastData['id'])
- fatal_lang_error('no_access', false);
- if (!empty($pms))
- {
- // Select the correct current message.
- if (empty($pmID))
- $context['current_pm'] = $lastData['id'];
- // This is a list of the pm's that are used for "full" display.
- if ($context['display_mode'] == 0)
- $display_pms = $pms;
- else
- $display_pms = array($context['current_pm']);
- // At this point we know the main id_pm's. But - if we are looking at conversations we need the others!
- if ($context['display_mode'] == 2)
- {
- $request = $smcFunc['db_query']('', '
- SELECT pm.id_pm, pm.id_member_from, pm.deleted_by_sender, pmr.id_member, pmr.deleted
- FROM {db_prefix}personal_messages AS pm
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
- WHERE pm.id_pm_head = {int:id_pm_head}
- AND ((pm.id_member_from = {int:current_member} AND pm.deleted_by_sender = {int:not_deleted})
- OR (pmr.id_member = {int:current_member} AND pmr.deleted = {int:not_deleted}))
- ORDER BY pm.id_pm',
- array(
- 'current_member' => $user_info['id'],
- 'id_pm_head' => $lastData['head'],
- 'not_deleted' => 0,
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- // This is, frankly, a joke. We will put in a workaround for people sending to themselves - yawn!
- if ($context['folder'] == 'sent' && $row['id_member_from'] == $user_info['id'] && $row['deleted_by_sender'] == 1)
- continue;
- elseif ($row['id_member'] == $user_info['id'] & $row['deleted'] == 1)
- continue;
- if (!isset($recipients[$row['id_pm']]))
- $recipients[$row['id_pm']] = array(
- 'to' => array(),
- 'bcc' => array()
- );
- $display_pms[] = $row['id_pm'];
- $posters[$row['id_pm']] = $row['id_member_from'];
- }
- $smcFunc['db_free_result']($request);
- }
- // This is pretty much EVERY pm!
- $all_pms = array_merge($pms, $display_pms);
- $all_pms = array_unique($all_pms);
- // Get recipients (don't include bcc-recipients for your inbox, you're not supposed to know :P).
- $request = $smcFunc['db_query']('', '
- 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
- FROM {db_prefix}pm_recipients AS pmr
- LEFT JOIN {db_prefix}members AS mem_to ON (mem_to.id_member = pmr.id_member)
- WHERE pmr.id_pm IN ({array_int:pm_list})',
- array(
- 'pm_list' => $all_pms,
- )
- );
- $context['message_labels'] = array();
- $context['message_replied'] = array();
- $context['message_unread'] = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- if ($context['folder'] == 'sent' || empty($row['bcc']))
- $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>';
- if ($row['id_member_to'] == $user_info['id'] && $context['folder'] != 'sent')
- {
- $context['message_replied'][$row['id_pm']] = $row['is_read'] & 2;
- $context['message_unread'][$row['id_pm']] = $row['is_read'] == 0;
- $row['labels'] = $row['labels'] == '' ? array() : explode(',', $row['labels']);
- foreach ($row['labels'] as $v)
- {
- if (isset($context['labels'][(int) $v]))
- $context['message_labels'][$row['id_pm']][(int) $v] = array('id' => $v, 'name' => $context['labels'][(int) $v]['name']);
- }
- }
- }
- $smcFunc['db_free_result']($request);
- // Make sure we don't load unnecessary data.
- if ($context['display_mode'] == 1)
- {
- foreach ($posters as $k => $v)
- if (!in_array($k, $display_pms))
- unset($posters[$k]);
- }
- // Load any users....
- $posters = array_unique($posters);
- if (!empty($posters))
- loadMemberData($posters);
- // If we're on grouped/restricted view get a restricted list of messages.
- if ($context['display_mode'] != 0)
- {
- // Get the order right.
- $orderBy = array();
- foreach (array_reverse($pms) as $pm)
- $orderBy[] = 'pm.id_pm = ' . $pm;
- // Seperate query for these bits!
- $subjects_request = $smcFunc['db_query']('', '
- SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.msgtime, IFNULL(mem.real_name, pm.from_name) AS from_name,
- IFNULL(mem.id_member, 0) AS not_guest
- FROM {db_prefix}personal_messages AS pm
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
- WHERE pm.id_pm IN ({array_int:pm_list})
- ORDER BY ' . implode(', ', $orderBy) . '
- LIMIT ' . count($pms),
- array(
- 'pm_list' => $pms,
- )
- );
- }
- // Execute the query!
- $messages_request = $smcFunc['db_query']('', '
- SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name
- FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '
- LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') . ($context['sort_by'] == 'name' ? '
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})' : '') . '
- WHERE pm.id_pm IN ({array_int:display_pms})' . ($context['folder'] == 'sent' ? '
- GROUP BY pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name' : '') . '
- ORDER BY ' . ($context['display_mode'] == 2 ? 'pm.id_pm' : $_GET['sort']) . ($descending ? ' DESC' : ' ASC') . '
- LIMIT ' . count($display_pms),
- array(
- 'display_pms' => $display_pms,
- 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
- )
- );
- }
- else
- $messages_request = false;
- $context['can_send_pm'] = allowedTo('pm_send');
- $context['can_send_email'] = allowedTo('send_email_to_members');
- if (!WIRELESS)
- $context['sub_template'] = 'folder';
- $context['page_title'] = $txt['pm_inbox'];
- // Finally mark the relevant messages as read.
- if ($context['folder'] != 'sent' && !empty($context['labels'][(int) $context['current_label_id']]['unread_messages']))
- {
- // If the display mode is "old sk00l" do them all...
- if ($context['display_mode'] == 0)
- markMessages(null, $context['current_label_id']);
- // Otherwise do just the current one!
- elseif (!empty($context['current_pm']))
- markMessages($display_pms, $context['current_label_id']);
- }
- // Build the conversation button array.
- if ($context['display_mode'] == 2)
- {
- $context['conversation_buttons'] = array(
- 'reply' => array('text' => 'reply_to_all', 'image' => 'reply.png', 'lang' => true, 'url' => $scripturl . '?action=pm;sa=send;f=' . $context['folder'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '') . ';pmsg=' . $context['current_pm'] . ';u=all', 'active' => true),
- 'delete' => array('text' => 'delete_conversation', 'image' => 'delete.png', 'lang' => true, 'url' => $scripturl . '?action=pm;sa=pmactions;pm_actions[' . $context['current_pm'] . ']=delete;conversation;f=' . $context['folder'] . ';start=' . $context['start'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '') . ';' . $context['session_var'] . '=' . $context['session_id'], 'custom' => 'onclick="return confirm(\'' . addslashes($txt['remove_message']) . '?\');"'),
- );
- // Allow mods to add additional buttons here
- call_integration_hook('integrate_conversation_buttons');
- }
- }
- /**
- * Get a personal message for the theme. (used to save memory.)
- *
- * @param $type
- * @param $reset
- */
- function prepareMessageContext($type = 'subject', $reset = false)
- {
- global $txt, $scripturl, $modSettings, $settings, $context, $messages_request, $memberContext, $recipients, $smcFunc;
- global $user_info, $subjects_request;
- // Count the current message number....
- static $counter = null;
- if ($counter === null || $reset)
- $counter = $context['start'];
- static $temp_pm_selected = null;
- if ($temp_pm_selected === null)
- {
- $temp_pm_selected = isset($_SESSION['pm_selected']) ? $_SESSION['pm_selected'] : array();
- $_SESSION['pm_selected'] = array();
- }
- // If we're in non-boring view do something exciting!
- if ($context['display_mode'] != 0 && $subjects_request && $type == 'subject')
- {
- $subject = $smcFunc['db_fetch_assoc']($subjects_request);
- if (!$subject)
- {
- $smcFunc['db_free_result']($subjects_request);
- return false;
- }
- $subject['subject'] = $subject['subject'] == '' ? $txt['no_subject'] : $subject['subject'];
- censorText($subject['subject']);
- $output = array(
- 'id' => $subject['id_pm'],
- 'member' => array(
- 'id' => $subject['id_member_from'],
- 'name' => $subject['from_name'],
- 'link' => $subject['not_guest'] ? '<a href="' . $scripturl . '?action=profile;u=' . $subject['id_member_from'] . '">' . $subject['from_name'] . '</a>' : $subject['from_name'],
- ),
- 'recipients' => &$recipients[$subject['id_pm']],
- 'subject' => $subject['subject'],
- 'time' => timeformat($subject['msgtime']),
- 'timestamp' => forum_time(true, $subject['msgtime']),
- 'number_recipients' => count($recipients[$subject['id_pm']]['to']),
- 'labels' => &$context['message_labels'][$subject['id_pm']],
- 'fully_labeled' => count($context['message_labels'][$subject['id_pm']]) == count($context['labels']),
- 'is_replied_to' => &$context['message_replied'][$subject['id_pm']],
- 'is_unread' => &$context['message_unread'][$subject['id_pm']],
- 'is_selected' => !empty($temp_pm_selected) && in_array($subject['id_pm'], $temp_pm_selected),
- );
- return $output;
- }
- // Bail if it's false, ie. no messages.
- if ($messages_request == false)
- return false;
- // Reset the data?
- if ($reset == true)
- return @$smcFunc['db_data_seek']($messages_request, 0);
- // Get the next one... bail if anything goes wrong.
- $message = $smcFunc['db_fetch_assoc']($messages_request);
- if (!$message)
- {
- if ($type != 'subject')
- $smcFunc['db_free_result']($messages_request);
- return false;
- }
- // Use '(no subject)' if none was specified.
- $message['subject'] = $message['subject'] == '' ? $txt['no_subject'] : $message['subject'];
- // Load the message's information - if it's not there, load the guest information.
- if (!loadMemberContext($message['id_member_from'], true))
- {
- $memberContext[$message['id_member_from']]['name'] = $message['from_name'];
- $memberContext[$message['id_member_from']]['id'] = 0;
- // Sometimes the forum sends messages itself (Warnings are an example) - in this case don't label it from a guest.
- $memberContext[$message['id_member_from']]['group'] = $message['from_name'] == $context['forum_name'] ? '' : $txt['guest_title'];
- $memberContext[$message['id_member_from']]['link'] = $message['from_name'];
- $memberContext[$message['id_member_from']]['email'] = '';
- $memberContext[$message['id_member_from']]['show_email'] = showEmailAddress(true, 0);
- $memberContext[$message['id_member_from']]['is_guest'] = true;
- }
- else
- {
- $memberContext[$message['id_member_from']]['can_view_profile'] = allowedTo('profile_view_any') || ($message['id_member_from'] == $user_info['id'] && allowedTo('profile_view_own'));
- $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'])));
- }
- $memberContext[$message['id_member_from']]['show_profile_buttons'] = $settings['show_profile_buttons'] && (!empty($memberContext[$message['id_member_from']]['can_view_profile']) || (!empty($memberContext[$message['id_member_from']]['website']['url']) && !isset($context['disabled_fields']['website'])) || (in_array($memberContext[$message['id_member_from']]['show_email'], array('yes', 'yes_permission_override', 'no_through_forum'))) || $context['can_send_pm']);
- // Censor all the important text...
- censorText($message['body']);
- censorText($message['subject']);
- // Run UBBC interpreter on the message.
- $message['body'] = parse_bbc($message['body'], true, 'pm' . $message['id_pm']);
- // Send the array.
- $output = array(
- 'alternate' => $counter % 2,
- 'id' => $message['id_pm'],
- 'member' => &$memberContext[$message['id_member_from']],
- 'subject' => $message['subject'],
- 'time' => timeformat($message['msgtime']),
- 'timestamp' => forum_time(true, $message['msgtime']),
- 'counter' => $counter,
- 'body' => $message['body'],
- 'recipients' => &$recipients[$message['id_pm']],
- 'number_recipients' => count($recipients[$message['id_pm']]['to']),
- 'labels' => &$context['message_labels'][$message['id_pm']],
- 'fully_labeled' => count($context['message_labels'][$message['id_pm']]) == count($context['labels']),
- 'is_replied_to' => &$context['message_replied'][$message['id_pm']],
- 'is_unread' => &$context['message_unread'][$message['id_pm']],
- 'is_selected' => !empty($temp_pm_selected) && in_array($message['id_pm'], $temp_pm_selected),
- 'is_message_author' => $message['id_member_from'] == $user_info['id'],
- 'can_report' => !empty($modSettings['enableReportPM']),
- 'can_see_ip' => allowedTo('moderate_forum') || ($message['id_member'] == $user_info['id'] && !empty($user_info['id'])),
- );
- $counter++;
- return $output;
- }
- /**
- * Allows to search through personal messages.
- */
- function MessageSearch()
- {
- global $context, $txt, $scripturl, $modSettings, $smcFunc;
- if (isset($_REQUEST['params']))
- {
- $temp_params = explode('|"|', base64_decode(strtr($_REQUEST['params'], array(' ' => '+'))));
- $context['search_params'] = array();
- foreach ($temp_params as $i => $data)
- {
- @list ($k, $v) = explode('|\'|', $data);
- $context['search_params'][$k] = $v;
- }
- }
- if (isset($_REQUEST['search']))
- $context['search_params']['search'] = un_htmlspecialchars($_REQUEST['search']);
- if (isset($context['search_params']['search']))
- $context['search_params']['search'] = htmlspecialchars($context['search_params']['search']);
- if (isset($context['search_params']['userspec']))
- $context['search_params']['userspec'] = htmlspecialchars($context['search_params']['userspec']);
- if (!empty($context['search_params']['searchtype']))
- $context['search_params']['searchtype'] = 2;
- if (!empty($context['search_params']['minage']))
- $context['search_params']['minage'] = (int) $context['search_params']['minage'];
- if (!empty($context['search_params']['maxage']))
- $context['search_params']['maxage'] = (int) $context['search_params']['maxage'];
- $context['search_params']['subject_only'] = !empty($context['search_params']['subject_only']);
- $context['search_params']['show_complete'] = !empty($context['search_params']['show_complete']);
- // Create the array of labels to be searched.
- $context['search_labels'] = array();
- $searchedLabels = isset($context['search_params']['labels']) && $context['search_params']['labels'] != '' ? explode(',', $context['search_params']['labels']) : array();
- foreach ($context['labels'] as $label)
- {
- $context['search_labels'][] = array(
- 'id' => $label['id'],
- 'name' => $label['name'],
- 'checked' => !empty($searchedLabels) ? in_array($label['id'], $searchedLabels) : true,
- );
- }
- // Are all the labels checked?
- $context['check_all'] = empty($searchedLabels) || count($context['search_labels']) == count($searchedLabels);
- // Load the error text strings if there were errors in the search.
- if (!empty($context['search_errors']))
- {
- loadLanguage('Errors');
- $context['search_errors']['messages'] = array();
- foreach ($context['search_errors'] as $search_error => $dummy)
- {
- if ($search_error == 'messages')
- continue;
- $context['search_errors']['messages'][] = $txt['error_' . $search_error];
- }
- }
- $context['simple_search'] = isset($context['search_params']['advanced']) ? empty($context['search_params']['advanced']) : !empty($modSettings['simpleSearch']) && !isset($_REQUEST['advanced']);
- $context['page_title'] = $txt['pm_search_title'];
- $context['sub_template'] = 'search';
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm;sa=search',
- 'name' => $txt['pm_search_bar_title'],
- );
- }
- /**
- * Actually do the search of personal messages.
- */
- function MessageSearch2()
- {
- global $scripturl, $modSettings, $user_info, $context, $txt;
- global $memberContext, $smcFunc;
- if (!empty($context['load_average']) && !empty($modSettings['loadavg_search']) && $context['load_average'] >= $modSettings['loadavg_search'])
- fatal_lang_error('loadavg_search_disabled', false);
- /**
- * @todo For the moment force the folder to the inbox.
- * @todo Maybe set the inbox based on a cookie or theme setting?
- */
- $context['folder'] = 'inbox';
- // Some useful general permissions.
- $context['can_send_pm'] = allowedTo('pm_send');
- // Some hardcoded veriables that can be tweaked if required.
- $maxMembersToSearch = 500;
- // Extract all the search parameters.
- $search_params = array();
- if (isset($_REQUEST['params']))
- {
- $temp_params = explode('|"|', base64_decode(strtr($_REQUEST['params'], array(' ' => '+'))));
- foreach ($temp_params as $i => $data)
- {
- @list ($k, $v) = explode('|\'|', $data);
- $search_params[$k] = $v;
- }
- }
- $context['start'] = isset($_GET['start']) ? (int) $_GET['start'] : 0;
- // Store whether simple search was used (needed if the user wants to do another query).
- if (!isset($search_params['advanced']))
- $search_params['advanced'] = empty($_REQUEST['advanced']) ? 0 : 1;
- // 1 => 'allwords' (default, don't set as param) / 2 => 'anywords'.
- if (!empty($search_params['searchtype']) || (!empty($_REQUEST['searchtype']) && $_REQUEST['searchtype'] == 2))
- $search_params['searchtype'] = 2;
- // Minimum age of messages. Default to zero (don't set param in that case).
- if (!empty($search_params['minage']) || (!empty($_REQUEST['minage']) && $_REQUEST['minage'] > 0))
- $search_params['minage'] = !empty($search_params['minage']) ? (int) $search_params['minage'] : (int) $_REQUEST['minage'];
- // Maximum age of messages. Default to infinite (9999 days: param not set).
- if (!empty($search_params['maxage']) || (!empty($_REQUEST['maxage']) && $_REQUEST['maxage'] != 9999))
- $search_params['maxage'] = !empty($search_params['maxage']) ? (int) $search_params['maxage'] : (int) $_REQUEST['maxage'];
- $search_params['subject_only'] = !empty($search_params['subject_only']) || !empty($_REQUEST['subject_only']);
- $search_params['show_complete'] = !empty($search_params['show_complete']) || !empty($_REQUEST['show_complete']);
- // Default the user name to a wildcard matching every user (*).
- if (!empty($search_params['user_spec']) || (!empty($_REQUEST['userspec']) && $_REQUEST['userspec'] != '*'))
- $search_params['userspec'] = isset($search_params['userspec']) ? $search_params['userspec'] : $_REQUEST['userspec'];
- // This will be full of all kinds of parameters!
- $searchq_parameters = array();
- // If there's no specific user, then don't mention it in the main query.
- if (empty($search_params['userspec']))
- $userQuery = '';
- else
- {
- $userString = strtr($smcFunc['htmlspecialchars']($search_params['userspec'], ENT_QUOTES), array('"' => '"'));
- $userString = strtr($userString, array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_'));
- preg_match_all('~"([^"]+)"~', $userString, $matches);
- $possible_users = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $userString)));
- for ($k = 0, $n = count($possible_users); $k < $n; $k++)
- {
- $possible_users[$k] = trim($possible_users[$k]);
- if (strlen($possible_users[$k]) == 0)
- unset($possible_users[$k]);
- }
- // Who matches those criteria?
- // @todo This doesn't support sent item searching.
- $request = $smcFunc['db_query']('', '
- SELECT id_member
- FROM {db_prefix}members
- WHERE real_name LIKE {raw:real_name_implode}',
- array(
- 'real_name_implode' => '\'' . implode('\' OR real_name LIKE \'', $possible_users) . '\'',
- )
- );
- // Simply do nothing if there're too many members matching the criteria.
- if ($smcFunc['db_num_rows']($request) > $maxMembersToSearch)
- $userQuery = '';
- elseif ($smcFunc['db_num_rows']($request) == 0)
- {
- $userQuery = 'AND pm.id_member_from = 0 AND (pm.from_name LIKE {raw:guest_user_name_implode})';
- $searchq_parameters['guest_user_name_implode'] = '\'' . implode('\' OR pm.from_name LIKE \'', $possible_users) . '\'';
- }
- else
- {
- $memberlist = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $memberlist[] = $row['id_member'];
- $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})))';
- $searchq_parameters['guest_user_name_implode'] = '\'' . implode('\' OR pm.from_name LIKE \'', $possible_users) . '\'';
- $searchq_parameters['member_list'] = $memberlist;
- }
- $smcFunc['db_free_result']($request);
- }
- // Setup the sorting variables...
- // @todo Add more in here!
- $sort_columns = array(
- 'pm.id_pm',
- );
- if (empty($search_params['sort']) && !empty($_REQUEST['sort']))
- list ($search_params['sort'], $search_params['sort_dir']) = array_pad(explode('|', $_REQUEST['sort']), 2, '');
- $search_params['sort'] = !empty($search_params['sort']) && in_array($search_params['sort'], $sort_columns) ? $search_params['sort'] : 'pm.id_pm';
- $search_params['sort_dir'] = !empty($search_params['sort_dir']) && $search_params['sort_dir'] == 'asc' ? 'asc' : 'desc';
- // Sort out any labels we may be searching by.
- $labelQuery = '';
- if ($context['folder'] == 'inbox' && !empty($search_params['advanced']) && $context['currently_using_labels'])
- {
- // Came here from pagination? Put them back into $_REQUEST for sanitization.
- if (isset($search_params['labels']))
- $_REQUEST['searchlabel'] = explode(',', $search_params['labels']);
- // Assuming we have some labels - make them all integers.
- if (!empty($_REQUEST['searchlabel']) && is_array($_REQUEST['searchlabel']))
- {
- foreach ($_REQUEST['searchlabel'] as $key => $id)
- $_REQUEST['searchlabel'][$key] = (int) $id;
- }
- else
- $_REQUEST['searchlabel'] = array();
- // Now that everything is cleaned up a bit, make the labels a param.
- $search_params['labels'] = implode(',', $_REQUEST['searchlabel']);
- // No labels selected? That must be an error!
- if (empty($_REQUEST['searchlabel']))
- $context['search_errors']['no_labels_selected'] = true;
- // Otherwise prepare the query!
- elseif (count($_REQUEST['searchlabel']) != count($context['labels']))
- {
- $labelQuery = '
- AND {raw:label_implode}';
- $labelStatements = array();
- foreach ($_REQUEST['searchlabel'] as $label)
- $labelStatements[] = $smcFunc['db_quote']('FIND_IN_SET({string:label}, pmr.labels) != 0', array(
- 'label' => $label,
- ));
- $searchq_parameters['label_implode'] = '(' . implode(' OR ', $labelStatements) . ')';
- }
- }
- // What are we actually searching for?
- $search_params['search'] = !empty($search_params['search']) ? $search_params['search'] : (isset($_REQUEST['search']) ? $_REQUEST['search'] : '');
- // If we ain't got nothing - we should error!
- if (!isset($search_params['search']) || $search_params['search'] == '')
- $context['search_errors']['invalid_search_string'] = true;
- // Extract phrase parts first (e.g. some words "this is a phrase" some more words.)
- preg_match_all('~(?:^|\s)([-]?)"([^"]+)"(?:$|\s)~' . ($context['utf8'] ? 'u' : ''), $search_params['search'], $matches, PREG_PATTERN_ORDER);
- $searchArray = $matches[2];
- // Remove the phrase parts and extract the words.
- $tempSearch = explode(' ', preg_replace('~(?:^|\s)(?:[-]?)"(?:[^"]+)"(?:$|\s)~' . ($context['utf8'] ? 'u' : ''), ' ', $search_params['search']));
- // A minus sign in front of a word excludes the word.... so...
- $excludedWords = array();
- // .. first, we check for things like -"some words", but not "-some words".
- foreach ($matches[1] as $index => $word)
- if ($word == '-')
- {
- $word = $smcFunc['strtolower'](trim($searchArray[$index]));
- if (strlen($word) > 0)
- $excludedWords[] = $word;
- unset($searchArray[$index]);
- }
- // Now we look for -test, etc.... normaller.
- foreach ($tempSearch as $index => $word)
- {
- if (strpos(trim($word), '-') === 0)
- {
- $word = substr($smcFunc['strtolower']($word), 1);
- if (strlen($word) > 0)
- $excludedWords[] = $word;
- unset($tempSearch[$index]);
- }
- }
- $searchArray = array_merge($searchArray, $tempSearch);
- // Trim everything and make sure there are no words that are the same.
- foreach ($searchArray as $index => $value)
- {
- $searchArray[$index] = $smcFunc['strtolower'](trim($value));
- if ($searchArray[$index] == '')
- unset($searchArray[$index]);
- else
- {
- // Sort out entities first.
- $searchArray[$index] = $smcFunc['htmlspecialchars']($searchArray[$index]);
- }
- }
- $searchArray = array_unique($searchArray);
- // Create an array of replacements for highlighting.
- $context['mark'] = array();
- foreach ($searchArray as $word)
- $context['mark'][$word] = '<strong class="highlight">' . $word . '</strong>';
- // This contains *everything*
- $searchWords = array_merge($searchArray, $excludedWords);
- // Make sure at least one word is being searched for.
- if (empty($searchArray))
- $context['search_errors']['invalid_search_string'] = true;
- // Sort out the search query so the user can edit it - if they want.
- $context['search_params'] = $search_params;
- if (isset($context['search_params']['search']))
- $context['search_params']['search'] = htmlspecialchars($context['search_params']['search']);
- if (isset($context['search_params']['userspec']))
- $context['search_params']['userspec'] = htmlspecialchars($context['search_params']['userspec']);
- // Now we have all the parameters, combine them together for pagination and the like...
- $context['params'] = array();
- foreach ($search_params as $k => $v)
- $context['params'][] = $k . '|\'|' . $v;
- $context['params'] = base64_encode(implode('|"|', $context['params']));
- // Compile the subject query part.
- $andQueryParts = array();
- foreach ($searchWords as $index => $word)
- {
- if ($word == '')
- continue;
- if ($search_params['subject_only'])
- $andQueryParts[] = 'pm.subject' . (in_array($word, $excludedWords) ? ' NOT' : '') . ' LIKE {string:search_' . $index . '}';
- else
- $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 . '})';
- $searchq_parameters['search_' . $index] = '%' . strtr($word, array('_' => '\\_', '%' => '\\%')) . '%';
- }
- $searchQuery = ' 1=1';
- if (!empty($andQueryParts))
- $searchQuery = implode(!empty($search_params['searchtype']) && $search_params['searchtype'] == 2 ? ' OR ' : ' AND ', $andQueryParts);
- // Age limits?
- $timeQuery = '';
- if (!empty($search_params['minage']))
- $timeQuery .= ' AND pm.msgtime < ' . (time() - $search_params['minage'] * 86400);
- if (!empty($search_params['maxage']))
- $timeQuery .= ' AND pm.msgtime > ' . (time() - $search_params['maxage'] * 86400);
- // If we have errors - return back to the first screen...
- if (!empty($context['search_errors']))
- {
- $_REQUEST['params'] = $context['params'];
- return MessageSearch();
- }
- // Get the amount of results.
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(*)
- FROM {db_prefix}pm_recipients AS pmr
- INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)
- WHERE ' . ($context['folder'] == 'inbox' ? '
- pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:not_deleted}' : '
- pm.id_member_from = {int:current_member}
- AND pm.deleted_by_sender = {int:not_deleted}') . '
- ' . $userQuery . $labelQuery . $timeQuery . '
- AND (' . $searchQuery . ')',
- array_merge($searchq_parameters, array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- ))
- );
- list ($numResults) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- // Get all the matching messages... using standard search only (No caching and the like!)
- // @todo This doesn't support sent item searching yet.
- $request = $smcFunc['db_query']('', '
- SELECT pm.id_pm, pm.id_pm_head, pm.id_member_from
- FROM {db_prefix}pm_recipients AS pmr
- INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)
- WHERE ' . ($context['folder'] == 'inbox' ? '
- pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:not_deleted}' : '
- pm.id_member_from = {int:current_member}
- AND pm.deleted_by_sender = {int:not_deleted}') . '
- ' . $userQuery . $labelQuery . $timeQuery . '
- AND (' . $searchQuery . ')
- ORDER BY ' . $search_params['sort'] . ' ' . $search_params['sort_dir'] . '
- LIMIT ' . $context['start'] . ', ' . $modSettings['search_results_per_page'],
- array_merge($searchq_parameters, array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- ))
- );
- $foundMessages = array();
- $posters = array();
- $head_pms = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- $foundMessages[] = $row['id_pm'];
- $posters[] = $row['id_member_from'];
- $head_pms[$row['id_pm']] = $row['id_pm_head'];
- }
- $smcFunc['db_free_result']($request);
- // Find the real head pms!
- if ($context['display_mode'] == 2 && !empty($head_pms))
- {
- $request = $smcFunc['db_query']('', '
- SELECT MAX(pm.id_pm) AS id_pm, pm.id_pm_head
- FROM {db_prefix}personal_messages AS pm
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
- WHERE pm.id_pm_head IN ({array_int:head_pms})
- AND pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:not_deleted}
- GROUP BY pm.id_pm_head
- LIMIT {int:limit}',
- array(
- 'head_pms' => array_unique($head_pms),
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- 'limit' => count($head_pms),
- )
- );
- $real_pm_ids = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $real_pm_ids[$row['id_pm_head']] = $row['id_pm'];
- $smcFunc['db_free_result']($request);
- }
- // Load the users...
- $posters = array_unique($posters);
- if (!empty($posters))
- loadMemberData($posters);
- // Sort out the page index.
- $context['page_index'] = constructPageIndex($scripturl . '?action=pm;sa=search2;params=' . $context['params'], $_GET['start'], $numResults, $modSettings['search_results_per_page'], false);
- $context['message_labels'] = array();
- $context['message_replied'] = array();
- $context['personal_messages'] = array();
- if (!empty($foundMessages))
- {
- // Now get recipients (but don't include bcc-recipients for your inbox, you're not supposed to know :P!)
- $request = $smcFunc['db_query']('', '
- 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
- FROM {db_prefix}pm_recipients AS pmr
- LEFT JOIN {db_prefix}members AS mem_to ON (mem_to.id_member = pmr.id_member)
- WHERE pmr.id_pm IN ({array_int:message_list})',
- array(
- 'message_list' => $foundMessages,
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- if ($context['folder'] == 'sent' || empty($row['bcc']))
- $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>';
- if ($row['id_member_to'] == $user_info['id'] && $context['folder'] != 'sent')
- {
- $context['message_replied'][$row['id_pm']] = $row['is_read'] & 2;
- $row['labels'] = $row['labels'] == '' ? array() : explode(',', $row['labels']);
- // This is a special need for linking to messages.
- foreach ($row['labels'] as $v)
- {
- if (isset($context['labels'][(int) $v]))
- $context['message_labels'][$row['id_pm']][(int) $v] = array('id' => $v, 'name' => $context['labels'][(int) $v]['name']);
- // Here we find the first label on a message - for linking to posts in results
- if (!isset($context['first_label'][$row['id_pm']]) && !in_array('-1', $row['labels']))
- $context['first_label'][$row['id_pm']] = (int) $v;
- }
- }
- }
- // Prepare the query for the callback!
- $request = $smcFunc['db_query']('', '
- SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name
- FROM {db_prefix}personal_messages AS pm
- WHERE pm.id_pm IN ({array_int:message_list})
- ORDER BY ' . $search_params['sort'] . ' ' . $search_params['sort_dir'] . '
- LIMIT ' . count($foundMessages),
- array(
- 'message_list' => $foundMessages,
- )
- );
- $counter = 0;
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- // If there's no message subject, use the default.
- $row['subject'] = $row['subject'] == '' ? $txt['no_subject'] : $row['subject'];
- // Load this posters context info, if it ain't there then fill in the essentials...
- if (!loadMemberContext($row['id_member_from'], true))
- {
- $memberContext[$row['id_member_from']]['name'] = $row['from_name'];
- $memberContext[$row['id_member_from']]['id'] = 0;
- $memberContext[$row['id_member_from']]['group'] = $txt['guest_title'];
- $memberContext[$row['id_member_from']]['link'] = $row['from_name'];
- $memberContext[$row['id_member_from']]['email'] = '';
- $memberContext[$row['id_member_from']]['show_email'] = showEmailAddress(true, 0);
- $memberContext[$row['id_member_from']]['is_guest'] = true;
- }
- // Censor anything we don't want to see...
- censorText($row['body']);
- censorText($row['subject']);
- // Parse out any BBC...
- $row['body'] = parse_bbc($row['body'], true, 'pm' . $row['id_pm']);
- $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'];
- $context['personal_messages'][] = array(
- 'id' => $row['id_pm'],
- 'member' => &$memberContext[$row['id_member_from']],
- 'subject' => $row['subject'],
- 'body' => $row['body'],
- 'time' => timeformat($row['msgtime']),
- 'recipients' => &$recipients[$row['id_pm']],
- 'labels' => &$context['message_labels'][$row['id_pm']],
- 'fully_labeled' => count($context['message_labels'][$row['id_pm']]) == count($context['labels']),
- 'is_replied_to' => &$context['message_replied'][$row['id_pm']],
- 'href' => $href,
- 'link' => '<a href="' . $href . '">' . $row['subject'] . '</a>',
- 'counter' => ++$counter,
- );
- }
- $smcFunc['db_free_result']($request);
- }
- // Finish off the context.
- $context['page_title'] = $txt['pm_search_title'];
- $context['sub_template'] = 'search_results';
- $context['menu_data_' . $context['pm_menu_id']]['current_area'] = 'search';
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm;sa=search',
- 'name' => $txt['pm_search_bar_title'],
- );
- }
- /**
- * Send a new message?
- */
- function MessagePost()
- {
- global $txt, $sourcedir, $scripturl, $modSettings;
- global $context, $options, $smcFunc, $language, $user_info;
- isAllowedTo('pm_send');
- loadLanguage('PersonalMessage');
- // Just in case it was loaded from somewhere else.
- if (!WIRELESS)
- {
- loadTemplate('PersonalMessage');
- $context['sub_template'] = 'send';
- }
- // Extract out the spam settings - cause it's neat.
- list ($modSettings['max_pm_recipients'], $modSettings['pm_posts_verification'], $modSettings['pm_posts_per_hour']) = explode(',', $modSettings['pm_spam_settings']);
- // Set the title...
- $context['page_title'] = $txt['send_message'];
- $context['reply'] = isset($_REQUEST['pmsg']) || isset($_REQUEST['quote']);
- // Check whether we've gone over the limit of messages we can send per hour.
- 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')
- {
- // How many messages have they sent this last hour?
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(pr.id_pm) AS post_count
- FROM {db_prefix}personal_messages AS pm
- INNER JOIN {db_prefix}pm_recipients AS pr ON (pr.id_pm = pm.id_pm)
- WHERE pm.id_member_from = {int:current_member}
- AND pm.msgtime > {int:msgtime}',
- array(
- 'current_member' => $user_info['id'],
- 'msgtime' => time() - 3600,
- )
- );
- list ($postCount) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- if (!empty($postCount) && $postCount >= $modSettings['pm_posts_per_hour'])
- fatal_lang_error('pm_too_many_per_hour', true, array($modSettings['pm_posts_per_hour']));
- }
- // Quoting/Replying to a message?
- if (!empty($_REQUEST['pmsg']))
- {
- $pmsg = (int) $_REQUEST['pmsg'];
- // Make sure this is yours.
- if (!isAccessiblePM($pmsg))
- fatal_lang_error('no_access', false);
- // Work out whether this is one you've received?
- $request = $smcFunc['db_query']('', '
- SELECT
- id_pm
- FROM {db_prefix}pm_recipients
- WHERE id_pm = {int:id_pm}
- AND id_member = {int:current_member}
- LIMIT 1',
- array(
- 'current_member' => $user_info['id'],
- 'id_pm' => $pmsg,
- )
- );
- $isReceived = $smcFunc['db_num_rows']($request) != 0;
- $smcFunc['db_free_result']($request);
- // Get the quoted message (and make sure you're allowed to see this quote!).
- $request = $smcFunc['db_query']('', '
- SELECT
- 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,
- pm.body, pm.subject, pm.msgtime, mem.member_name, IFNULL(mem.id_member, 0) AS id_member,
- IFNULL(mem.real_name, pm.from_name) AS real_name
- FROM {db_prefix}personal_messages AS pm' . (!$isReceived ? '' : '
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = {int:id_pm})') . '
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
- WHERE pm.id_pm = {int:id_pm}' . (!$isReceived ? '
- AND pm.id_member_from = {int:current_member}' : '
- AND pmr.id_member = {int:current_member}') . '
- LIMIT 1',
- array(
- 'current_member' => $user_info['id'],
- 'id_pm_head_empty' => 0,
- 'id_pm' => $pmsg,
- )
- );
- if ($smcFunc['db_num_rows']($request) == 0)
- fatal_lang_error('pm_not_yours', false);
- $row_quoted = $smcFunc['db_fetch_assoc']($request);
- $smcFunc['db_free_result']($request);
- // Censor the message.
- censorText($row_quoted['subject']);
- censorText($row_quoted['body']);
- // Add 'Re: ' to it....
- if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
- {
- if ($language === $user_info['language'])
- $context['response_prefix'] = $txt['response_prefix'];
- else
- {
- loadLanguage('index', $language, false);
- $context['response_prefix'] = $txt['response_prefix'];
- loadLanguage('index');
- }
- cache_put_data('response_prefix', $context['response_prefix'], 600);
- }
- $form_subject = $row_quoted['subject'];
- if ($context['reply'] && trim($context['response_prefix']) != '' && $smcFunc['strpos']($form_subject, trim($context['response_prefix'])) !== 0)
- $form_subject = $context['response_prefix'] . $form_subject;
- if (isset($_REQUEST['quote']))
- {
- // Remove any nested quotes and <br />...
- $form_message = preg_replace('~<br ?/?' . '>~i', "\n", $row_quoted['body']);
- if (!empty($modSettings['removeNestedQuotes']))
- $form_message = preg_replace(array('~\n?\[quote.*?\].+?\[/quote\]\n?~is', '~^\n~', '~\[/quote\]~'), '', $form_message);
- if (empty($row_quoted['id_member']))
- $form_message = '[quote author="' . $row_quoted['real_name'] . '"]' . "\n" . $form_message . "\n" . '[/quote]';
- else
- $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]';
- }
- else
- $form_message = '';
- // Do the BBC thang on the message.
- $row_quoted['body'] = parse_bbc($row_quoted['body'], true, 'pm' . $row_quoted['id_pm']);
- // Set up the quoted message array.
- $context['quoted_message'] = array(
- 'id' => $row_quoted['id_pm'],
- 'pm_head' => $row_quoted['pm_head'],
- 'member' => array(
- 'name' => $row_quoted['real_name'],
- 'username' => $row_quoted['member_name'],
- 'id' => $row_quoted['id_member'],
- 'href' => !empty($row_quoted['id_member']) ? $scripturl . '?action=profile;u=' . $row_quoted['id_member'] : '',
- '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'],
- ),
- 'subject' => $row_quoted['subject'],
- 'time' => timeformat($row_quoted['msgtime']),
- 'timestamp' => forum_time(true, $row_quoted['msgtime']),
- 'body' => $row_quoted['body']
- );
- }
- else
- {
- $context['quoted_message'] = false;
- $form_subject = '';
- $form_message = '';
- }
- $context['recipients'] = array(
- 'to' => array(),
- 'bcc' => array(),
- );
- // Sending by ID? Replying to all? Fetch the real_name(s).
- if (isset($_REQUEST['u']))
- {
- // If the user is replying to all, get all the other members this was sent to..
- if ($_REQUEST['u'] == 'all' && isset($row_quoted))
- {
- // Firstly, to reply to all we clearly already have $row_quoted - so have the original member from.
- if ($row_quoted['id_member'] != $user_info['id'])
- $context['recipients']['to'][] = array(
- 'id' => $row_quoted['id_member'],
- 'name' => htmlspecialchars($row_quoted['real_name']),
- );
- // Now to get the others.
- $request = $smcFunc['db_query']('', '
- SELECT mem.id_member, mem.real_name
- FROM {db_prefix}pm_recipients AS pmr
- INNER JOIN {db_prefix}members AS mem ON (mem.id_member = pmr.id_member)
- WHERE pmr.id_pm = {int:id_pm}
- AND pmr.id_member != {int:current_member}
- AND pmr.bcc = {int:not_bcc}',
- array(
- 'current_member' => $user_info['id'],
- 'id_pm' => $pmsg,
- 'not_bcc' => 0,
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $context['recipients']['to'][] = array(
- 'id' => $row['id_member'],
- 'name' => $row['real_name'],
- );
- $smcFunc['db_free_result']($request);
- }
- else
- {
- $_REQUEST['u'] = explode(',', $_REQUEST['u']);
- foreach ($_REQUEST['u'] as $key => $uID)
- $_REQUEST['u'][$key] = (int) $uID;
- $_REQUEST['u'] = array_unique($_REQUEST['u']);
- $request = $smcFunc['db_query']('', '
- SELECT id_member, real_name
- FROM {db_prefix}members
- WHERE id_member IN ({array_int:member_list})
- LIMIT ' . count($_REQUEST['u']),
- array(
- 'member_list' => $_REQUEST['u'],
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $context['recipients']['to'][] = array(
- 'id' => $row['id_member'],
- 'name' => $row['real_name'],
- );
- $smcFunc['db_free_result']($request);
- }
- // Get a literal name list in case the user has JavaScript disabled.
- $names = array();
- foreach ($context['recipients']['to'] as $to)
- $names[] = $to['name'];
- $context['to_value'] = empty($names) ? '' : '"' . implode('", "', $names) . '"';
- }
- else
- $context['to_value'] = '';
- // Set the defaults...
- $context['subject'] = $form_subject;
- $context['message'] = str_replace(array('"', '<', '>', ' '), array('"', '<', '>', ' '), $form_message);
- $context['post_error'] = array();
- $context['copy_to_outbox'] = !empty($options['copy_to_outbox']);
- // And build the link tree.
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm;sa=send',
- 'name' => $txt['new_message']
- );
- $modSettings['disable_wysiwyg'] = !empty($modSettings['disable_wysiwyg']) || empty($modSettings['enableBBC']);
- // Generate a list of drafts that they can load in to the editor
- if (!empty($context['drafts_pm_save']))
- {
- require_once($sourcedir . '/Drafts.php');
- $pm_seed = isset($_REQUEST['pmsg']) ? $_REQUEST['pmsg'] : (isset($_REQUEST['quote']) ? $_REQUEST['quote'] : 0);
- ShowDrafts($user_info['id'], $pm_seed, 1);
- }
- // Needed for the WYSIWYG editor.
- require_once($sourcedir . '/Subs-Editor.php');
- // Now create the editor.
- $editorOptions = array(
- 'id' => 'message',
- 'value' => $context['message'],
- 'height' => '175px',
- 'width' => '100%',
- 'labels' => array(
- 'post_button' => $txt['send_message'],
- ),
- 'preview_type' => 2,
- );
- create_control_richedit($editorOptions);
- // Store the ID for old compatibility.
- $context['post_box_name'] = $editorOptions['id'];
- $context['bcc_value'] = '';
- $context['require_verification'] = !$user_info['is_admin'] && !empty($modSettings['pm_posts_verification']) && $user_info['posts'] < $modSettings['pm_posts_verification'];
- if ($context['require_verification'])
- {
- $verificationOptions = array(
- 'id' => 'pm',
- );
- $context['require_verification'] = create_control_verification($verificationOptions);
- $context['visual_verification_id'] = $verificationOptions['id'];
- }
- // Register this form and get a sequence number in $context.
- checkSubmitOnce('register');
- }
- /**
- * This function allows the user to view their PM drafts
- */
- function MessageDrafts()
- {
- global $context, $sourcedir, $user_info, $modSettings;
- // validate with loadMemberData()
- $memberResult = loadMemberData($user_info['id'], false);
- if (!is_array($memberResult))
- fatal_lang_error('not_a_user', false);
- list ($memID) = $memberResult;
- // drafts is where the functions reside
- require_once($sourcedir . '/Drafts.php');
- showPMDrafts($memID);
- }
- /**
- * An error in the message...
- *
- * @param $error_types
- * @param $named_recipients
- * @param $recipient_ids
- */
- function messagePostError($error_types, $named_recipients, $recipient_ids = array())
- {
- global $txt, $context, $scripturl, $modSettings;
- global $smcFunc, $user_info, $sourcedir;
- if (!isset($_REQUEST['xml']))
- $context['menu_data_' . $context['pm_menu_id']]['current_area'] = 'send';
- if (!WIRELESS && !isset($_REQUEST['xml']))
- $context['sub_template'] = 'send';
- elseif (isset($_REQUEST['xml']))
- $context['sub_template'] = 'pm';
- $context['page_title'] = $txt['send_message'];
- // Got some known members?
- $context['recipients'] = array(
- 'to' => array(),
- 'bcc' => array(),
- );
- if (!empty($recipient_ids['to']) || !empty($recipient_ids['bcc']))
- {
- $allRecipients = array_merge($recipient_ids['to'], $recipient_ids['bcc']);
- $request = $smcFunc['db_query']('', '
- SELECT id_member, real_name
- FROM {db_prefix}members
- WHERE id_member IN ({array_int:member_list})',
- array(
- 'member_list' => $allRecipients,
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- $recipientType = in_array($row['id_member'], $recipient_ids['bcc']) ? 'bcc' : 'to';
- $context['recipients'][$recipientType][] = array(
- 'id' => $row['id_member'],
- 'name' => $row['real_name'],
- );
- }
- $smcFunc['db_free_result']($request);
- }
- // Set everything up like before....
- $context['subject'] = isset($_REQUEST['subject']) ? $smcFunc['htmlspecialchars']($_REQUEST['subject']) : '';
- $context['message'] = isset($_REQUEST['message']) ? str_replace(array(' '), array(' '), $smcFunc['htmlspecialchars']($_REQUEST['message'])) : '';
- $context['copy_to_outbox'] = !empty($_REQUEST['outbox']);
- $context['reply'] = !empty($_REQUEST['replied_to']);
- if ($context['reply'])
- {
- $_REQUEST['replied_to'] = (int) $_REQUEST['replied_to'];
- $request = $smcFunc['db_query']('', '
- SELECT
- 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,
- pm.body, pm.subject, pm.msgtime, mem.member_name, IFNULL(mem.id_member, 0) AS id_member,
- IFNULL(mem.real_name, pm.from_name) AS real_name
- FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '' : '
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = {int:replied_to})') . '
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
- WHERE pm.id_pm = {int:replied_to}' . ($context['folder'] == 'sent' ? '
- AND pm.id_member_from = {int:current_member}' : '
- AND pmr.id_member = {int:current_member}') . '
- LIMIT 1',
- array(
- 'current_member' => $user_info['id'],
- 'no_id_pm_head' => 0,
- 'replied_to' => $_REQUEST['replied_to'],
- )
- );
- if ($smcFunc['db_num_rows']($request) == 0)
- {
- if (!isset($_REQUEST['xml']))
- fatal_lang_error('pm_not_yours', false);
- else
- $error_types[] = 'pm_not_yours';
- }
- $row_quoted = $smcFunc['db_fetch_assoc']($request);
- $smcFunc['db_free_result']($request);
- censorText($row_quoted['subject']);
- censorText($row_quoted['body']);
- $context['quoted_message'] = array(
- 'id' => $row_quoted['id_pm'],
- 'pm_head' => $row_quoted['pm_head'],
- 'member' => array(
- 'name' => $row_quoted['real_name'],
- 'username' => $row_quoted['member_name'],
- 'id' => $row_quoted['id_member'],
- 'href' => !empty($row_quoted['id_member']) ? $scripturl . '?action=profile;u=' . $row_quoted['id_member'] : '',
- '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'],
- ),
- 'subject' => $row_quoted['subject'],
- 'time' => timeformat($row_quoted['msgtime']),
- 'timestamp' => forum_time(true, $row_quoted['msgtime']),
- 'body' => parse_bbc($row_quoted['body'], true, 'pm' . $row_quoted['id_pm']),
- );
- }
- // Build the link tree....
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm;sa=send',
- 'name' => $txt['new_message']
- );
- // Set each of the errors for the template.
- loadLanguage('Errors');
- $context['error_type'] = 'minor';
- $context['post_error'] = array(
- 'messages' => array(),
- // @todo error handling: maybe fatal errors can be error_type => serious
- 'error_type' => '',
- );
- foreach ($error_types as $error_type)
- {
- $context['post_error'][$error_type] = true;
- if (isset($txt['error_' . $error_type]))
- {
- if ($error_type == 'long_message')
- $txt['error_' . $error_type] = sprintf($txt['error_' . $error_type], $modSettings['max_messageLength']);
- $context['post_error']['messages'][] = $txt['error_' . $error_type];
- }
- // If it's not a minor error flag it as such.
- if (!in_array($error_type, array('new_reply', 'not_approved', 'new_replies', 'old_topic', 'need_qr_verification', 'no_subject')))
- $context['error_type'] = 'serious';
- }
- // We need to load the editor once more.
- require_once($sourcedir . '/Subs-Editor.php');
- // Create it...
- $editorOptions = array(
- 'id' => 'message',
- 'value' => $context['message'],
- 'width' => '90%',
- 'labels' => array(
- 'post_button' => $txt['send_message'],
- ),
- 'preview_type' => 2,
- );
- create_control_richedit($editorOptions);
- // ... and store the ID again...
- $context['post_box_name'] = $editorOptions['id'];
- // Check whether we need to show the code again.
- $context['require_verification'] = !$user_info['is_admin'] && !empty($modSettings['pm_posts_verification']) && $user_info['posts'] < $modSettings['pm_posts_verification'];
- if ($context['require_verification'] && !isset($_REQUEST['xml']))
- {
- require_once($sourcedir . '/Subs-Editor.php');
- $verificationOptions = array(
- 'id' => 'pm',
- );
- $context['require_verification'] = create_control_verification($verificationOptions);
- $context['visual_verification_id'] = $verificationOptions['id'];
- }
- $context['to_value'] = empty($named_recipients['to']) ? '' : '"' . implode('", "', $named_recipients['to']) . '"';
- $context['bcc_value'] = empty($named_recipients['bcc']) ? '' : '"' . implode('", "', $named_recipients['bcc']) . '"';
- // No check for the previous submission is needed.
- checkSubmitOnce('free');
- // Acquire a new form sequence number.
- checkSubmitOnce('register');
- }
- /**
- * Send it!
- */
- function MessagePost2()
- {
- global $txt, $context, $sourcedir;
- global $user_info, $modSettings, $scripturl, $smcFunc;
- isAllowedTo('pm_send');
- require_once($sourcedir . '/Subs-Auth.php');
-
- // PM Drafts enabled and needed?
- if ($context['drafts_pm_save'] && (isset($_POST['save_draft']) || isset($_POST['id_pm_draft'])))
- require_once($sourcedir . '/Drafts.php');
- loadLanguage('PersonalMessage', '', false);
- // Extract out the spam settings - it saves database space!
- list ($modSettings['max_pm_recipients'], $modSettings['pm_posts_verification'], $modSettings['pm_posts_per_hour']) = explode(',', $modSettings['pm_spam_settings']);
- // Initialize the errors we're about to make.
- $post_errors = array();
- // Check whether we've gone over the limit of messages we can send per hour - fatal error if fails!
- 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')
- {
- // How many have they sent this last hour?
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(pr.id_pm) AS post_count
- FROM {db_prefix}personal_messages AS pm
- INNER JOIN {db_prefix}pm_recipients AS pr ON (pr.id_pm = pm.id_pm)
- WHERE pm.id_member_from = {int:current_member}
- AND pm.msgtime > {int:msgtime}',
- array(
- 'current_member' => $user_info['id'],
- 'msgtime' => time() - 3600,
- )
- );
- list ($postCount) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- if (!empty($postCount) && $postCount >= $modSettings['pm_posts_per_hour'])
- {
- if (!isset($_REQUEST['xml']))
- fatal_lang_error('pm_too_many_per_hour', true, array($modSettings['pm_posts_per_hour']));
- else
- $post_errors[] = 'pm_too_many_per_hour';
- }
- }
- // If your session timed out, show an error, but do allow to re-submit.
- if (!isset($_REQUEST['xml']) && checkSession('post', '', false) != '')
- $post_errors[] = 'session_timeout';
- $_REQUEST['subject'] = isset($_REQUEST['subject']) ? trim($_REQUEST['subject']) : '';
- $_REQUEST['to'] = empty($_POST['to']) ? (empty($_GET['to']) ? '' : $_GET['to']) : $_POST['to'];
- $_REQUEST['bcc'] = empty($_POST['bcc']) ? (empty($_GET['bcc']) ? '' : $_GET['bcc']) : $_POST['bcc'];
- // Route the input from the 'u' parameter to the 'to'-list.
- if (!empty($_POST['u']))
- $_POST['recipient_to'] = explode(',', $_POST['u']);
- // Construct the list of recipients.
- $recipientList = array();
- $namedRecipientList = array();
- $namesNotFound = array();
- foreach (array('to', 'bcc') as $recipientType)
- {
- // First, let's see if there's user ID's given.
- $recipientList[$recipientType] = array();
- if (!empty($_POST['recipient_' . $recipientType]) && is_array($_POST['recipient_' . $recipientType]))
- {
- foreach ($_POST['recipient_' . $recipientType] as $recipient)
- $recipientList[$recipientType][] = (int) $recipient;
- }
- // Are there also literal names set?
- if (!empty($_REQUEST[$recipientType]))
- {
- // We're going to take out the "s anyway ;).
- $recipientString = strtr($_REQUEST[$recipientType], array('\\"' => '"'));
- preg_match_all('~"([^"]+)"~', $recipientString, $matches);
- $namedRecipientList[$recipientType] = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $recipientString))));
- foreach ($namedRecipientList[$recipientType] as $index => $recipient)
- {
- if (strlen(trim($recipient)) > 0)
- $namedRecipientList[$recipientType][$index] = $smcFunc['htmlspecialchars']($smcFunc['strtolower'](trim($recipient)));
- else
- unset($namedRecipientList[$recipientType][$index]);
- }
- if (!empty($namedRecipientList[$recipientType]))
- {
- $foundMembers = findMembers($namedRecipientList[$recipientType]);
- // Assume all are not found, until proven otherwise.
- $namesNotFound[$recipientType] = $namedRecipientList[$recipientType];
- foreach ($foundMembers as $member)
- {
- $testNames = array(
- $smcFunc['strtolower']($member['username']),
- $smcFunc['strtolower']($member['name']),
- $smcFunc['strtolower']($member['email']),
- );
- if (count(array_intersect($testNames, $namedRecipientList[$recipientType])) !== 0)
- {
- $recipientList[$recipientType][] = $member['id'];
- // Get rid of this username, since we found it.
- $namesNotFound[$recipientType] = array_diff($namesNotFound[$recipientType], $testNames);
- }
- }
- }
- }
- // Selected a recipient to be deleted? Remove them now.
- if (!empty($_POST['delete_recipient']))
- $recipientList[$recipientType] = array_diff($recipientList[$recipientType], array((int) $_POST['delete_recipient']));
- // Make sure we don't include the same name twice
- $recipientList[$recipientType] = array_unique($recipientList[$recipientType]);
- }
- // Are we changing the recipients some how?
- $is_recipient_change = !empty($_POST['delete_recipient']) || !empty($_POST['to_submit']) || !empty($_POST['bcc_submit']);
- // Check if there's at least one recipient.
- if (empty($recipientList['to']) && empty($recipientList['bcc']))
- $post_errors[] = 'no_to';
- // Make sure that we remove the members who did get it from the screen.
- if (!$is_recipient_change)
- {
- foreach ($recipientList as $recipientType => $dummy)
- {
- if (!empty($namesNotFound[$recipientType]))
- {
- $post_errors[] = 'bad_' . $recipientType;
- // Since we already have a post error, remove the previous one.
- $post_errors = array_diff($post_errors, array('no_to'));
- foreach ($namesNotFound[$recipientType] as $name)
- $context['send_log']['failed'][] = sprintf($txt['pm_error_user_not_found'], $name);
- }
- }
- }
- // Did they make any mistakes?
- if ($_REQUEST['subject'] == '')
- $post_errors[] = 'no_subject';
- if (!isset($_REQUEST['message']) || $_REQUEST['message'] == '')
- $post_errors[] = 'no_message';
- elseif (!empty($modSettings['max_messageLength']) && $smcFunc['strlen']($_REQUEST['message']) > $modSettings['max_messageLength'])
- $post_errors[] = 'long_message';
- else
- {
- // Preparse the message.
- $message = $_REQUEST['message'];
- preparsecode($message);
- // Make sure there's still some content left without the tags.
- if ($smcFunc['htmltrim'](strip_tags(parse_bbc($smcFunc['htmlspecialchars']($message, ENT_QUOTES), false), '<img>')) === '' && (!allowedTo('admin_forum') || strpos($message, '[html]') === false))
- $post_errors[] = 'no_message';
- }
- // Wrong verification code?
- if (!$user_info['is_admin'] && !isset($_REQUEST['xml']) && !empty($modSettings['pm_posts_verification']) && $user_info['posts'] < $modSettings['pm_posts_verification'])
- {
- require_once($sourcedir . '/Subs-Editor.php');
- $verificationOptions = array(
- 'id' => 'pm',
- );
- $context['require_verification'] = create_control_verification($verificationOptions, true);
- if (is_array($context['require_verification']))
- $post_errors = array_merge($post_errors, $context['require_verification']);
- }
- // If they did, give a chance to make ammends.
- if (!empty($post_errors) && !$is_recipient_change && !isset($_REQUEST['preview']) && !isset($_REQUEST['xml']))
- return messagePostError($post_errors, $namedRecipientList, $recipientList);
- // Want to take a second glance before you send?
- if (isset($_REQUEST['preview']))
- {
- // Set everything up to be displayed.
- $context['preview_subject'] = $smcFunc['htmlspecialchars']($_REQUEST['subject']);
- $context['preview_message'] = $smcFunc['htmlspecialchars']($_REQUEST['message'], ENT_QUOTES);
- preparsecode($context['preview_message'], true);
- // Parse out the BBC if it is enabled.
- $context['preview_message'] = parse_bbc($context['preview_message']);
- // Censor, as always.
- censorText($context['preview_subject']);
- censorText($context['preview_message']);
- // Set a descriptive title.
- $context['page_title'] = $txt['preview'] . ' - ' . $context['preview_subject'];
- // Pretend they messed up but don't ignore if they really did :P.
- return messagePostError($post_errors, $namedRecipientList, $recipientList);
- }
- // Adding a recipient cause javascript ain't working?
- elseif ($is_recipient_change)
- {
- // Maybe we couldn't find one?
- foreach ($namesNotFound as $recipientType => $names)
- {
- $post_errors[] = 'bad_' . $recipientType;
- foreach ($names as $name)
- $context['send_log']['failed'][] = sprintf($txt['pm_error_user_not_found'], $name);
- }
- return messagePostError(array(), $namedRecipientList, $recipientList);
- }
- // Want to save this as a draft and think about it some more?
- if ($context['drafts_pm_save'] && isset($_POST['save_draft']))
- {
- SavePMDraft($post_errors, $recipientList);
- return messagePostError($post_errors, $namedRecipientList, $recipientList);
- }
- // Before we send the PM, let's make sure we don't have an abuse of numbers.
- elseif (!empty($modSettings['max_pm_recipients']) && count($recipientList['to']) + count($recipientList['bcc']) > $modSettings['max_pm_recipients'] && !allowedTo(array('moderate_forum', 'send_mail', 'admin_forum')))
- {
- $context['send_log'] = array(
- 'sent' => array(),
- 'failed' => array(sprintf($txt['pm_too_many_recipients'], $modSettings['max_pm_recipients'])),
- );
- return messagePostError($post_errors, $namedRecipientList, $recipientList);
- }
- // Protect from message spamming.
- spamProtection('pm');
- // Prevent double submission of this form.
- checkSubmitOnce('check');
- // Do the actual sending of the PM.
- if (!empty($recipientList['to']) || !empty($recipientList['bcc']))
- $context['send_log'] = sendpm($recipientList, $_REQUEST['subject'], $_REQUEST['message'], !empty($_REQUEST['outbox']), null, !empty($_REQUEST['pm_head']) ? (int) $_REQUEST['pm_head'] : 0);
- else
- $context['send_log'] = array(
- 'sent' => array(),
- 'failed' => array()
- );
- // Mark the message as "replied to".
- if (!empty($context['send_log']['sent']) && !empty($_REQUEST['replied_to']) && isset($_REQUEST['f']) && $_REQUEST['f'] == 'inbox')
- {
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}pm_recipients
- SET is_read = is_read | 2
- WHERE id_pm = {int:replied_to}
- AND id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'replied_to' => (int) $_REQUEST['replied_to'],
- )
- );
- }
- // If one or more of the recipient were invalid, go back to the post screen with the failed usernames.
- if (!empty($context['send_log']['failed']))
- return messagePostError($post_errors, $namesNotFound, array(
- 'to' => array_intersect($recipientList['to'], $context['send_log']['failed']),
- 'bcc' => array_intersect($recipientList['bcc'], $context['send_log']['failed'])
- ));
- // Message sent successfully?
- if (!empty($context['send_log']) && empty($context['send_log']['failed']))
- {
- $context['current_label_redirect'] = $context['current_label_redirect'] . ';done=sent';
-
- // If we had a PM draft for this one, then its time to remove it since it was just sent
- if ($context['drafts_pm_save'] && !empty($_POST['id_pm_draft']))
- DeleteDraft($_POST['id_pm_draft']);
- }
- // Go back to the where they sent from, if possible...
- redirectexit($context['current_label_redirect']);
- }
- /**
- * This function lists all buddies for wireless protocols.
- */
- function WirelessAddBuddy()
- {
- global $scripturl, $txt, $user_info, $context, $smcFunc;
- isAllowedTo('pm_send');
- $context['page_title'] = $txt['wireless_pm_add_buddy'];
- $current_buddies = empty($_REQUEST['u']) ? array() : explode(',', $_REQUEST['u']);
- foreach ($current_buddies as $key => $buddy)
- $current_buddies[$key] = (int) $buddy;
- $base_url = $scripturl . '?action=pm;sa=send;u=' . (empty($current_buddies) ? '' : implode(',', $current_buddies) . ',');
- $context['pm_href'] = $scripturl . '?action=pm;sa=send' . (empty($current_buddies) ? '' : ';u=' . implode(',', $current_buddies));
- $context['buddies'] = array();
- if (!empty($user_info['buddies']))
- {
- $request = $smcFunc['db_query']('', '
- SELECT id_member, real_name
- FROM {db_prefix}members
- WHERE id_member IN ({array_int:buddy_list})
- ORDER BY real_name
- LIMIT ' . count($user_info['buddies']),
- array(
- 'buddy_list' => $user_info['buddies'],
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $context['buddies'][] = array(
- 'id' => $row['id_member'],
- 'name' => $row['real_name'],
- 'selected' => in_array($row['id_member'], $current_buddies),
- 'add_href' => $base_url . $row['id_member'],
- );
- $smcFunc['db_free_result']($request);
- }
- }
- /**
- * This function performs all additional stuff...
- */
- function MessageActionsApply()
- {
- global $txt, $context, $user_info, $options, $smcFunc;
- checkSession('request');
- if (isset($_REQUEST['del_selected']))
- $_REQUEST['pm_action'] = 'delete';
- if (isset($_REQUEST['pm_action']) && $_REQUEST['pm_action'] != '' && !empty($_REQUEST['pms']) && is_array($_REQUEST['pms']))
- {
- foreach ($_REQUEST['pms'] as $pm)
- $_REQUEST['pm_actions'][(int) $pm] = $_REQUEST['pm_action'];
- }
- if (empty($_REQUEST['pm_actions']))
- redirectexit($context['current_label_redirect']);
- // If we are in conversation, we may need to apply this to every message in the conversation.
- if ($context['display_mode'] == 2 && isset($_REQUEST['conversation']))
- {
- $id_pms = array();
- foreach ($_REQUEST['pm_actions'] as $pm => $dummy)
- $id_pms[] = (int) $pm;
- $request = $smcFunc['db_query']('', '
- SELECT id_pm_head, id_pm
- FROM {db_prefix}personal_messages
- WHERE id_pm IN ({array_int:id_pms})',
- array(
- 'id_pms' => $id_pms,
- )
- );
- $pm_heads = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $pm_heads[$row['id_pm_head']] = $row['id_pm'];
- $smcFunc['db_free_result']($request);
- $request = $smcFunc['db_query']('', '
- SELECT id_pm, id_pm_head
- FROM {db_prefix}personal_messages
- WHERE id_pm_head IN ({array_int:pm_heads})',
- array(
- 'pm_heads' => array_keys($pm_heads),
- )
- );
- // Copy the action from the single to PM to the others.
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- if (isset($pm_heads[$row['id_pm_head']]) && isset($_REQUEST['pm_actions'][$pm_heads[$row['id_pm_head']]]))
- $_REQUEST['pm_actions'][$row['id_pm']] = $_REQUEST['pm_actions'][$pm_heads[$row['id_pm_head']]];
- }
- $smcFunc['db_free_result']($request);
- }
- $to_delete = array();
- $to_label = array();
- $label_type = array();
- foreach ($_REQUEST['pm_actions'] as $pm => $action)
- {
- if ($action === 'delete')
- $to_delete[] = (int) $pm;
- else
- {
- if (substr($action, 0, 4) == 'add_')
- {
- $type = 'add';
- $action = substr($action, 4);
- }
- elseif (substr($action, 0, 4) == 'rem_')
- {
- $type = 'rem';
- $action = substr($action, 4);
- }
- else
- $type = 'unk';
- if ($action == '-1' || $action == '0' || (int) $action > 0)
- {
- $to_label[(int) $pm] = (int) $action;
- $label_type[(int) $pm] = $type;
- }
- }
- }
- // Deleting, it looks like?
- if (!empty($to_delete))
- deleteMessages($to_delete, $context['display_mode'] == 2 ? null : $context['folder']);
- // Are we labeling anything?
- if (!empty($to_label) && $context['folder'] == 'inbox')
- {
- $updateErrors = 0;
- // Get information about each message...
- $request = $smcFunc['db_query']('', '
- SELECT id_pm, labels
- FROM {db_prefix}pm_recipients
- WHERE id_member = {int:current_member}
- AND id_pm IN ({array_int:to_label})
- LIMIT ' . count($to_label),
- array(
- 'current_member' => $user_info['id'],
- 'to_label' => array_keys($to_label),
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- $labels = $row['labels'] == '' ? array('-1') : explode(',', trim($row['labels']));
- // Already exists? Then... unset it!
- $ID_LABEL = array_search($to_label[$row['id_pm']], $labels);
- if ($ID_LABEL !== false && $label_type[$row['id_pm']] !== 'add')
- unset($labels[$ID_LABEL]);
- elseif ($label_type[$row['id_pm']] !== 'rem')
- $labels[] = $to_label[$row['id_pm']];
- if (!empty($options['pm_remove_inbox_label']) && $to_label[$row['id_pm']] != '-1' && ($key = array_search('-1', $labels)) !== false)
- unset($labels[$key]);
- $set = implode(',', array_unique($labels));
- if ($set == '')
- $set = '-1';
- // Check that this string isn't going to be too large for the database.
- if ($set > 60)
- $updateErrors++;
- else
- {
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}pm_recipients
- SET labels = {string:labels}
- WHERE id_pm = {int:id_pm}
- AND id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'id_pm' => $row['id_pm'],
- 'labels' => $set,
- )
- );
- }
- }
- $smcFunc['db_free_result']($request);
- // Any errors?
- // @todo Separate the sprintf?
- if (!empty($updateErrors))
- fatal_lang_error('labels_too_many', true, array($updateErrors));
- }
- // Back to the folder.
- $_SESSION['pm_selected'] = array_keys($to_label);
- redirectexit($context['current_label_redirect'] . (count($to_label) == 1 ? '#msg' . $_SESSION['pm_selected'][0] : ''), count($to_label) == 1 && isBrowser('ie'));
- }
- /**
- * Are you sure you want to PERMANENTLY (mostly) delete ALL your messages?
- */
- function MessageKillAllQuery()
- {
- global $txt, $context;
- // Only have to set up the template....
- $context['sub_template'] = 'ask_delete';
- $context['page_title'] = $txt['delete_all'];
- $context['delete_all'] = $_REQUEST['f'] == 'all';
- // And set the folder name...
- $txt['delete_all'] = str_replace('PMBOX', $context['folder'] != 'sent' ? $txt['inbox'] : $txt['sent_items'], $txt['delete_all']);
- }
- /**
- * Delete ALL the messages!
- */
- function MessageKillAll()
- {
- global $context;
- checkSession('get');
- // If all then delete all messages the user has.
- if ($_REQUEST['f'] == 'all')
- deleteMessages(null, null);
- // Otherwise just the selected folder.
- else
- deleteMessages(null, $_REQUEST['f'] != 'sent' ? 'inbox' : 'sent');
- // Done... all gone.
- redirectexit($context['current_label_redirect']);
- }
- /**
- * This function allows the user to delete all messages older than so many days.
- */
- function MessagePrune()
- {
- global $txt, $context, $user_info, $scripturl, $smcFunc;
- // Actually delete the messages.
- if (isset($_REQUEST['age']))
- {
- checkSession();
- // Calculate the time to delete before.
- $deleteTime = max(0, time() - (86400 * (int) $_REQUEST['age']));
- // Array to store the IDs in.
- $toDelete = array();
- // Select all the messages they have sent older than $deleteTime.
- $request = $smcFunc['db_query']('', '
- SELECT id_pm
- FROM {db_prefix}personal_messages
- WHERE deleted_by_sender = {int:not_deleted}
- AND id_member_from = {int:current_member}
- AND msgtime < {int:msgtime}',
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- 'msgtime' => $deleteTime,
- )
- );
- while ($row = $smcFunc['db_fetch_row']($request))
- $toDelete[] = $row[0];
- $smcFunc['db_free_result']($request);
- // Select all messages in their inbox older than $deleteTime.
- $request = $smcFunc['db_query']('', '
- SELECT pmr.id_pm
- FROM {db_prefix}pm_recipients AS pmr
- INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)
- WHERE pmr.deleted = {int:not_deleted}
- AND pmr.id_member = {int:current_member}
- AND pm.msgtime < {int:msgtime}',
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- 'msgtime' => $deleteTime,
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $toDelete[] = $row['id_pm'];
- $smcFunc['db_free_result']($request);
- // Delete the actual messages.
- deleteMessages($toDelete);
- // Go back to their inbox.
- redirectexit($context['current_label_redirect']);
- }
- // Build the link tree elements.
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm;sa=prune',
- 'name' => $txt['pm_prune']
- );
- $context['sub_template'] = 'prune';
- $context['page_title'] = $txt['pm_prune'];
- }
- /**
- * Delete the specified personal messages.
- *
- * @param array $personal_messages array of pm ids
- * @param string $folder = null
- * @param int $owner = null
- */
- function deleteMessages($personal_messages, $folder = null, $owner = null)
- {
- global $user_info, $smcFunc;
- if ($owner === null)
- $owner = array($user_info['id']);
- elseif (empty($owner))
- return;
- elseif (!is_array($owner))
- $owner = array($owner);
- if ($personal_messages !== null)
- {
- if (empty($personal_messages) || !is_array($personal_messages))
- return;
- foreach ($personal_messages as $index => $delete_id)
- $personal_messages[$index] = (int) $delete_id;
- $where = '
- AND id_pm IN ({array_int:pm_list})';
- }
- else
- $where = '';
- if ($folder == 'sent' || $folder === null)
- {
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}personal_messages
- SET deleted_by_sender = {int:is_deleted}
- WHERE id_member_from IN ({array_int:member_list})
- AND deleted_by_sender = {int:not_deleted}' . $where,
- array(
- 'member_list' => $owner,
- 'is_deleted' => 1,
- 'not_deleted' => 0,
- 'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
- )
- );
- }
- if ($folder != 'sent' || $folder === null)
- {
- // Calculate the number of messages each member's gonna lose...
- $request = $smcFunc['db_query']('', '
- SELECT id_member, COUNT(*) AS num_deleted_messages, CASE WHEN is_read & 1 >= 1 THEN 1 ELSE 0 END AS is_read
- FROM {db_prefix}pm_recipients
- WHERE id_member IN ({array_int:member_list})
- AND deleted = {int:not_deleted}' . $where . '
- GROUP BY id_member, is_read',
- array(
- 'member_list' => $owner,
- 'not_deleted' => 0,
- 'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
- )
- );
- // ...And update the statistics accordingly - now including unread messages!.
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- if ($row['is_read'])
- updateMemberData($row['id_member'], array('instant_messages' => $where == '' ? 0 : 'instant_messages - ' . $row['num_deleted_messages']));
- else
- 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']));
- // If this is the current member we need to make their message count correct.
- if ($user_info['id'] == $row['id_member'])
- {
- $user_info['messages'] -= $row['num_deleted_messages'];
- if (!($row['is_read']))
- $user_info['unread_messages'] -= $row['num_deleted_messages'];
- }
- }
- $smcFunc['db_free_result']($request);
- // Do the actual deletion.
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}pm_recipients
- SET deleted = {int:is_deleted}
- WHERE id_member IN ({array_int:member_list})
- AND deleted = {int:not_deleted}' . $where,
- array(
- 'member_list' => $owner,
- 'is_deleted' => 1,
- 'not_deleted' => 0,
- 'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
- )
- );
- }
- // If sender and recipients all have deleted their message, it can be removed.
- $request = $smcFunc['db_query']('', '
- SELECT pm.id_pm AS sender, pmr.id_pm
- FROM {db_prefix}personal_messages AS pm
- LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm AND pmr.deleted = {int:not_deleted})
- WHERE pm.deleted_by_sender = {int:is_deleted}
- ' . str_replace('id_pm', 'pm.id_pm', $where) . '
- GROUP BY sender, pmr.id_pm
- HAVING pmr.id_pm IS null',
- array(
- 'not_deleted' => 0,
- 'is_deleted' => 1,
- 'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
- )
- );
- $remove_pms = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $remove_pms[] = $row['sender'];
- $smcFunc['db_free_result']($request);
- if (!empty($remove_pms))
- {
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}personal_messages
- WHERE id_pm IN ({array_int:pm_list})',
- array(
- 'pm_list' => $remove_pms,
- )
- );
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}pm_recipients
- WHERE id_pm IN ({array_int:pm_list})',
- array(
- 'pm_list' => $remove_pms,
- )
- );
- }
- // Any cached numbers may be wrong now.
- cache_put_data('labelCounts:' . $user_info['id'], null, 720);
- }
- /**
- * Mark the specified personal messages read.
- *
- * @param array $personal_messages = null, array of pm ids
- * @param string $label = null, if label is set, only marks messages with that label
- * @param int $owner = null, if owner is set, marks messages owned by that member id
- */
- function markMessages($personal_messages = null, $label = null, $owner = null)
- {
- global $user_info, $context, $smcFunc;
- if ($owner === null)
- $owner = $user_info['id'];
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}pm_recipients
- SET is_read = is_read | 1
- WHERE id_member = {int:id_member}
- AND NOT (is_read & 1 >= 1)' . ($label === null ? '' : '
- AND FIND_IN_SET({string:label}, labels) != 0') . ($personal_messages !== null ? '
- AND id_pm IN ({array_int:personal_messages})' : ''),
- array(
- 'personal_messages' => $personal_messages,
- 'id_member' => $owner,
- 'label' => $label,
- )
- );
- // If something wasn't marked as read, get the number of unread messages remaining.
- if ($smcFunc['db_affected_rows']() > 0)
- {
- if ($owner == $user_info['id'])
- {
- foreach ($context['labels'] as $label)
- $context['labels'][(int) $label['id']]['unread_messages'] = 0;
- }
- $result = $smcFunc['db_query']('', '
- SELECT labels, COUNT(*) AS num
- FROM {db_prefix}pm_recipients
- WHERE id_member = {int:id_member}
- AND NOT (is_read & 1 >= 1)
- AND deleted = {int:is_not_deleted}
- GROUP BY labels',
- array(
- 'id_member' => $owner,
- 'is_not_deleted' => 0,
- )
- );
- $total_unread = 0;
- while ($row = $smcFunc['db_fetch_assoc']($result))
- {
- $total_unread += $row['num'];
- if ($owner != $user_info['id'])
- continue;
- $this_labels = explode(',', $row['labels']);
- foreach ($this_labels as $this_label)
- $context['labels'][(int) $this_label]['unread_messages'] += $row['num'];
- }
- $smcFunc['db_free_result']($result);
- // Need to store all this.
- cache_put_data('labelCounts:' . $owner, $context['labels'], 720);
- updateMemberData($owner, array('unread_messages' => $total_unread));
- // If it was for the current member, reflect this in the $user_info array too.
- if ($owner == $user_info['id'])
- $user_info['unread_messages'] = $total_unread;
- }
- }
- /**
- * This function handles adding, deleting and editing labels on messages.
- */
- function ManageLabels()
- {
- global $txt, $context, $user_info, $scripturl, $smcFunc;
- // Build the link tree elements...
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm;sa=manlabels',
- 'name' => $txt['pm_manage_labels']
- );
- $context['page_title'] = $txt['pm_manage_labels'];
- $context['sub_template'] = 'labels';
- $the_labels = array();
- // Add all existing labels to the array to save, slashing them as necessary...
- foreach ($context['labels'] as $label)
- {
- if ($label['id'] != -1)
- $the_labels[$label['id']] = $label['name'];
- }
- if (isset($_POST[$context['session_var']]))
- {
- checkSession('post');
- // This will be for updating messages.
- $message_changes = array();
- $new_labels = array();
- $rule_changes = array();
- // Will most likely need this.
- LoadRules();
- // Adding a new label?
- if (isset($_POST['add']))
- {
- $_POST['label'] = strtr($smcFunc['htmlspecialchars'](trim($_POST['label'])), array(',' => ','));
- if ($smcFunc['strlen']($_POST['label']) > 30)
- $_POST['label'] = $smcFunc['substr']($_POST['label'], 0, 30);
- if ($_POST['label'] != '')
- $the_labels[] = $_POST['label'];
- }
- // Deleting an existing label?
- elseif (isset($_POST['delete'], $_POST['delete_label']))
- {
- $i = 0;
- foreach ($the_labels as $id => $name)
- {
- if (isset($_POST['delete_label'][$id]))
- {
- unset($the_labels[$id]);
- $message_changes[$id] = true;
- }
- else
- $new_labels[$id] = $i++;
- }
- }
- // The hardest one to deal with... changes.
- elseif (isset($_POST['save']) && !empty($_POST['label_name']))
- {
- $i = 0;
- foreach ($the_labels as $id => $name)
- {
- if ($id == -1)
- continue;
- elseif (isset($_POST['label_name'][$id]))
- {
- $_POST['label_name'][$id] = trim(strtr($smcFunc['htmlspecialchars']($_POST['label_name'][$id]), array(',' => ',')));
- if ($smcFunc['strlen']($_POST['label_name'][$id]) > 30)
- $_POST['label_name'][$id] = $smcFunc['substr']($_POST['label_name'][$id], 0, 30);
- if ($_POST['label_name'][$id] != '')
- {
- $the_labels[(int) $id] = $_POST['label_name'][$id];
- $new_labels[$id] = $i++;
- }
- else
- {
- unset($the_labels[(int) $id]);
- $message_changes[(int) $id] = true;
- }
- }
- else
- $new_labels[$id] = $i++;
- }
- }
- // Save the label status.
- updateMemberData($user_info['id'], array('message_labels' => implode(',', $the_labels)));
- // Update all the messages currently with any label changes in them!
- if (!empty($message_changes))
- {
- $searchArray = array_keys($message_changes);
- if (!empty($new_labels))
- {
- for ($i = max($searchArray) + 1, $n = max(array_keys($new_labels)); $i <= $n; $i++)
- $searchArray[] = $i;
- }
- // Now find the messages to change.
- $request = $smcFunc['db_query']('', '
- SELECT id_pm, labels
- FROM {db_prefix}pm_recipients
- WHERE FIND_IN_SET({raw:find_label_implode}, labels) != 0
- AND id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'find_label_implode' => '\'' . implode('\', labels) != 0 OR FIND_IN_SET(\'', $searchArray) . '\'',
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- // Do the long task of updating them...
- $toChange = explode(',', $row['labels']);
- foreach ($toChange as $key => $value)
- if (in_array($value, $searchArray))
- {
- if (isset($new_labels[$value]))
- $toChange[$key] = $new_labels[$value];
- else
- unset($toChange[$key]);
- }
- if (empty($toChange))
- $toChange[] = '-1';
- // Update the message.
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}pm_recipients
- SET labels = {string:new_labels}
- WHERE id_pm = {int:id_pm}
- AND id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'id_pm' => $row['id_pm'],
- 'new_labels' => implode(',', array_unique($toChange)),
- )
- );
- }
- $smcFunc['db_free_result']($request);
- // Now do the same the rules - check through each rule.
- foreach ($context['rules'] as $k => $rule)
- {
- // Each action...
- foreach ($rule['actions'] as $k2 => $action)
- {
- if ($action['t'] != 'lab' || !in_array($action['v'], $searchArray))
- continue;
- $rule_changes[] = $rule['id'];
- // If we're here we have a label which is either changed or gone...
- if (isset($new_labels[$action['v']]))
- $context['rules'][$k]['actions'][$k2]['v'] = $new_labels[$action['v']];
- else
- unset($context['rules'][$k]['actions'][$k2]);
- }
- }
- }
- // If we have rules to change do so now.
- if (!empty($rule_changes))
- {
- $rule_changes = array_unique($rule_changes);
- // Update/delete as appropriate.
- foreach ($rule_changes as $k => $id)
- if (!empty($context['rules'][$id]['actions']))
- {
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}pm_rules
- SET actions = {string:actions}
- WHERE id_rule = {int:id_rule}
- AND id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'id_rule' => $id,
- 'actions' => serialize($context['rules'][$id]['actions']),
- )
- );
- unset($rule_changes[$k]);
- }
- // Anything left here means it's lost all actions...
- if (!empty($rule_changes))
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}pm_rules
- WHERE id_rule IN ({array_int:rule_list})
- AND id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'rule_list' => $rule_changes,
- )
- );
- }
- // Make sure we're not caching this!
- cache_put_data('labelCounts:' . $user_info['id'], null, 720);
- // To make the changes appear right away, redirect.
- redirectexit('action=pm;sa=manlabels');
- }
- }
- /**
- * Allows to edit Personal Message Settings.
- *
- * @uses Profile.php
- * @uses Profile-Modify.php
- * @uses Profile template.
- * @uses Profile language file.
- */
- function MessageSettings()
- {
- global $txt, $user_settings, $user_info, $context, $sourcedir, $smcFunc;
- global $scripturl, $profile_vars, $cur_profile, $user_profile;
- // Need this for the display.
- require_once($sourcedir . '/Profile.php');
- require_once($sourcedir . '/Profile-Modify.php');
- // We want them to submit back to here.
- $context['profile_custom_submit_url'] = $scripturl . '?action=pm;sa=settings;save';
- loadMemberData($user_info['id'], false, 'profile');
- $cur_profile = $user_profile[$user_info['id']];
- loadLanguage('Profile');
- loadTemplate('Profile');
- $context['page_title'] = $txt['pm_settings'];
- $context['user']['is_owner'] = true;
- $context['id_member'] = $user_info['id'];
- $context['require_password'] = false;
- $context['menu_item_selected'] = 'settings';
- $context['submit_button_text'] = $txt['pm_settings'];
- $context['profile_header_text'] = $txt['personal_messages'];
- // Add our position to the linktree.
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm;sa=settings',
- 'name' => $txt['pm_settings']
- );
- // Are they saving?
- if (isset($_REQUEST['save']))
- {
- checkSession('post');
- // Mimic what profile would do.
- $_POST = htmltrim__recursive($_POST);
- $_POST = htmlspecialchars__recursive($_POST);
- // Save the fields.
- saveProfileFields();
- if (!empty($profile_vars))
- updateMemberData($user_info['id'], $profile_vars);
- }
- // Load up the fields.
- pmprefs($user_info['id']);
- }
- /**
- * Allows the user to report a personal message to an administrator.
- *
- * - In the first instance requires that the ID of the message to report is passed through $_GET.
- * - It allows the user to report to either a particular administrator - or the whole admin team.
- * - It will forward on a copy of the original message without allowing the reporter to make changes.
- *
- * @uses report_message sub-template.
- */
- function ReportMessage()
- {
- global $txt, $context, $scripturl, $sourcedir;
- global $user_info, $language, $modSettings, $smcFunc;
- // Check that this feature is even enabled!
- if (empty($modSettings['enableReportPM']) || empty($_REQUEST['pmsg']))
- fatal_lang_error('no_access', false);
- $pmsg = (int) $_REQUEST['pmsg'];
- if (!isAccessiblePM($pmsg, 'inbox'))
- fatal_lang_error('no_access', false);
- $context['pm_id'] = $pmsg;
- $context['page_title'] = $txt['pm_report_title'];
- // If we're here, just send the user to the template, with a few useful context bits.
- if (!isset($_POST['report']))
- {
- $context['sub_template'] = 'report_message';
- // @todo I don't like being able to pick who to send it to. Favoritism, etc. sucks.
- // Now, get all the administrators.
- $request = $smcFunc['db_query']('', '
- SELECT id_member, real_name
- FROM {db_prefix}members
- WHERE id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0
- ORDER BY real_name',
- array(
- 'admin_group' => 1,
- )
- );
- $context['admins'] = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $context['admins'][$row['id_member']] = $row['real_name'];
- $smcFunc['db_free_result']($request);
- // How many admins in total?
- $context['admin_count'] = count($context['admins']);
- }
- // Otherwise, let's get down to the sending stuff.
- else
- {
- // Check the session before proceeding any further!
- checkSession('post');
- // First, pull out the message contents, and verify it actually went to them!
- $request = $smcFunc['db_query']('', '
- SELECT pm.subject, pm.body, pm.msgtime, pm.id_member_from, IFNULL(m.real_name, pm.from_name) AS sender_name
- FROM {db_prefix}personal_messages AS pm
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
- LEFT JOIN {db_prefix}members AS m ON (m.id_member = pm.id_member_from)
- WHERE pm.id_pm = {int:id_pm}
- AND pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:not_deleted}
- LIMIT 1',
- array(
- 'current_member' => $user_info['id'],
- 'id_pm' => $context['pm_id'],
- 'not_deleted' => 0,
- )
- );
- // Can only be a hacker here!
- if ($smcFunc['db_num_rows']($request) == 0)
- fatal_lang_error('no_access', false);
- list ($subject, $body, $time, $memberFromID, $memberFromName) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- // Remove the line breaks...
- $body = preg_replace('~<br ?/?' . '>~i', "\n", $body);
- // Get any other recipients of the email.
- $request = $smcFunc['db_query']('', '
- SELECT mem_to.id_member AS id_member_to, mem_to.real_name AS to_name, pmr.bcc
- FROM {db_prefix}pm_recipients AS pmr
- LEFT JOIN {db_prefix}members AS mem_to ON (mem_to.id_member = pmr.id_member)
- WHERE pmr.id_pm = {int:id_pm}
- AND pmr.id_member != {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'id_pm' => $context['pm_id'],
- )
- );
- $recipients = array();
- $hidden_recipients = 0;
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- // If it's hidden still don't reveal their names - privacy after all ;)
- if ($row['bcc'])
- $hidden_recipients++;
- else
- $recipients[] = '[url=' . $scripturl . '?action=profile;u=' . $row['id_member_to'] . ']' . $row['to_name'] . '[/url]';
- }
- $smcFunc['db_free_result']($request);
- if ($hidden_recipients)
- $recipients[] = sprintf($txt['pm_report_pm_hidden'], $hidden_recipients);
- // Now let's get out and loop through the admins.
- $request = $smcFunc['db_query']('', '
- SELECT id_member, real_name, lngfile
- FROM {db_prefix}members
- WHERE (id_group = {int:admin_id} OR FIND_IN_SET({int:admin_id}, additional_groups) != 0)
- ' . (empty($_POST['id_admin']) ? '' : 'AND id_member = {int:specific_admin}') . '
- ORDER BY lngfile',
- array(
- 'admin_id' => 1,
- 'specific_admin' => isset($_POST['id_admin']) ? (int) $_POST['id_admin'] : 0,
- )
- );
- // Maybe we shouldn't advertise this?
- if ($smcFunc['db_num_rows']($request) == 0)
- fatal_lang_error('no_access', false);
- $memberFromName = un_htmlspecialchars($memberFromName);
- // Prepare the message storage array.
- $messagesToSend = array();
- // Loop through each admin, and add them to the right language pile...
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- // Need to send in the correct language!
- $cur_language = empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'];
- if (!isset($messagesToSend[$cur_language]))
- {
- loadLanguage('PersonalMessage', $cur_language, false);
- // Make the body.
- $report_body = str_replace(array('{REPORTER}', '{SENDER}'), array(un_htmlspecialchars($user_info['name']), $memberFromName), $txt['pm_report_pm_user_sent']);
- $report_body .= "\n" . '[b]' . $_POST['reason'] . '[/b]' . "\n\n";
- if (!empty($recipients))
- $report_body .= $txt['pm_report_pm_other_recipients'] . ' ' . implode(', ', $recipients) . "\n\n";
- $report_body .= $txt['pm_report_pm_unedited_below'] . "\n" . '[quote author=' . (empty($memberFromID) ? '"' . $memberFromName . '"' : $memberFromName . ' link=action=profile;u=' . $memberFromID . ' date=' . $time) . ']' . "\n" . un_htmlspecialchars($body) . '[/quote]';
- // Plonk it in the array ;)
- $messagesToSend[$cur_language] = array(
- 'subject' => ($smcFunc['strpos']($subject, $txt['pm_report_pm_subject']) === false ? $txt['pm_report_pm_subject'] : '') . un_htmlspecialchars($subject),
- 'body' => $report_body,
- 'recipients' => array(
- 'to' => array(),
- 'bcc' => array()
- ),
- );
- }
- // Add them to the list.
- $messagesToSend[$cur_language]['recipients']['to'][$row['id_member']] = $row['id_member'];
- }
- $smcFunc['db_free_result']($request);
- // Send a different email for each language.
- foreach ($messagesToSend as $lang => $message)
- sendpm($message['recipients'], $message['subject'], $message['body']);
- // Give the user their own language back!
- if (!empty($modSettings['userLanguage']))
- loadLanguage('PersonalMessage', '', false);
- // Leave them with a template.
- $context['sub_template'] = 'report_message_complete';
- }
- }
- /**
- * List all rules, and allow adding/entering etc...
- */
- function ManageRules()
- {
- global $txt, $context, $user_info, $scripturl, $smcFunc;
- // The link tree - gotta have this :o
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm;sa=manrules',
- 'name' => $txt['pm_manage_rules']
- );
- $context['page_title'] = $txt['pm_manage_rules'];
- $context['sub_template'] = 'rules';
- // Load them... load them!!
- LoadRules();
- // Likely to need all the groups!
- $request = $smcFunc['db_query']('', '
- SELECT mg.id_group, mg.group_name, IFNULL(gm.id_member, 0) AS can_moderate, mg.hidden
- FROM {db_prefix}membergroups AS mg
- LEFT JOIN {db_prefix}group_moderators AS gm ON (gm.id_group = mg.id_group AND gm.id_member = {int:current_member})
- WHERE mg.min_posts = {int:min_posts}
- AND mg.id_group != {int:moderator_group}
- AND mg.hidden = {int:not_hidden}
- ORDER BY mg.group_name',
- array(
- 'current_member' => $user_info['id'],
- 'min_posts' => -1,
- 'moderator_group' => 3,
- 'not_hidden' => 0,
- )
- );
- $context['groups'] = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- // Hide hidden groups!
- if ($row['hidden'] && !$row['can_moderate'] && !allowedTo('manage_membergroups'))
- continue;
- $context['groups'][$row['id_group']] = $row['group_name'];
- }
- $smcFunc['db_free_result']($request);
- // Applying all rules?
- if (isset($_GET['apply']))
- {
- checkSession('get');
- ApplyRules(true);
- redirectexit('action=pm;sa=manrules');
- }
- // Editing a specific one?
- if (isset($_GET['add']))
- {
- $context['rid'] = isset($_GET['rid']) && isset($context['rules'][$_GET['rid']])? (int) $_GET['rid'] : 0;
- $context['sub_template'] = 'add_rule';
- // Current rule information...
- if ($context['rid'])
- {
- $context['rule'] = $context['rules'][$context['rid']];
- $members = array();
- // Need to get member names!
- foreach ($context['rule']['criteria'] as $k => $criteria)
- if ($criteria['t'] == 'mid' && !empty($criteria['v']))
- $members[(int) $criteria['v']] = $k;
- if (!empty($members))
- {
- $request = $smcFunc['db_query']('', '
- SELECT id_member, member_name
- FROM {db_prefix}members
- WHERE id_member IN ({array_int:member_list})',
- array(
- 'member_list' => array_keys($members),
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $context['rule']['criteria'][$members[$row['id_member']]]['v'] = $row['member_name'];
- $smcFunc['db_free_result']($request);
- }
- }
- else
- $context['rule'] = array(
- 'id' => '',
- 'name' => '',
- 'criteria' => array(),
- 'actions' => array(),
- 'logic' => 'and',
- );
- }
- // Saving?
- elseif (isset($_GET['save']))
- {
- checkSession('post');
- $context['rid'] = isset($_GET['rid']) && isset($context['rules'][$_GET['rid']])? (int) $_GET['rid'] : 0;
- // Name is easy!
- $ruleName = $smcFunc['htmlspecialchars'](trim($_POST['rule_name']));
- if (empty($ruleName))
- fatal_lang_error('pm_rule_no_name', false);
- // Sanity check...
- if (empty($_POST['ruletype']) || empty($_POST['acttype']))
- fatal_lang_error('pm_rule_no_criteria', false);
- // Let's do the criteria first - it's also hardest!
- $criteria = array();
- foreach ($_POST['ruletype'] as $ind => $type)
- {
- // Check everything is here...
- if ($type == 'gid' && (!isset($_POST['ruledefgroup'][$ind]) || !isset($context['groups'][$_POST['ruledefgroup'][$ind]])))
- continue;
- elseif ($type != 'bud' && !isset($_POST['ruledef'][$ind]))
- continue;
- // Members need to be found.
- if ($type == 'mid')
- {
- $name = trim($_POST['ruledef'][$ind]);
- $request = $smcFunc['db_query']('', '
- SELECT id_member
- FROM {db_prefix}members
- WHERE real_name = {string:member_name}
- OR member_name = {string:member_name}',
- array(
- 'member_name' => $name,
- )
- );
- if ($smcFunc['db_num_rows']($request) == 0)
- continue;
- list ($memID) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- $criteria[] = array('t' => 'mid', 'v' => $memID);
- }
- elseif ($type == 'bud')
- $criteria[] = array('t' => 'bud', 'v' => 1);
- elseif ($type == 'gid')
- $criteria[] = array('t' => 'gid', 'v' => (int) $_POST['ruledefgroup'][$ind]);
- elseif (in_array($type, array('sub', 'msg')) && trim($_POST['ruledef'][$ind]) != '')
- $criteria[] = array('t' => $type, 'v' => $smcFunc['htmlspecialchars'](trim($_POST['ruledef'][$ind])));
- }
- // Also do the actions!
- $actions = array();
- $doDelete = 0;
- $isOr = $_POST['rule_logic'] == 'or' ? 1 : 0;
- foreach ($_POST['acttype'] as $ind => $type)
- {
- // Picking a valid label?
- if ($type == 'lab' && (!isset($_POST['labdef'][$ind]) || !isset($context['labels'][$_POST['labdef'][$ind] - 1])))
- continue;
- // Record what we're doing.
- if ($type == 'del')
- $doDelete = 1;
- elseif ($type == 'lab')
- $actions[] = array('t' => 'lab', 'v' => (int) $_POST['labdef'][$ind] - 1);
- }
- if (empty($criteria) || (empty($actions) && !$doDelete))
- fatal_lang_error('pm_rule_no_criteria', false);
- // What are we storing?
- $criteria = serialize($criteria);
- $actions = serialize($actions);
- // Create the rule?
- if (empty($context['rid']))
- $smcFunc['db_insert']('',
- '{db_prefix}pm_rules',
- array(
- 'id_member' => 'int', 'rule_name' => 'string', 'criteria' => 'string', 'actions' => 'string',
- 'delete_pm' => 'int', 'is_or' => 'int',
- ),
- array(
- $user_info['id'], $ruleName, $criteria, $actions, $doDelete, $isOr,
- ),
- array('id_rule')
- );
- else
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}pm_rules
- SET rule_name = {string:rule_name}, criteria = {string:criteria}, actions = {string:actions},
- delete_pm = {int:delete_pm}, is_or = {int:is_or}
- WHERE id_rule = {int:id_rule}
- AND id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'delete_pm' => $doDelete,
- 'is_or' => $isOr,
- 'id_rule' => $context['rid'],
- 'rule_name' => $ruleName,
- 'criteria' => $criteria,
- 'actions' => $actions,
- )
- );
- redirectexit('action=pm;sa=manrules');
- }
- // Deleting?
- elseif (isset($_POST['delselected']) && !empty($_POST['delrule']))
- {
- checkSession('post');
- $toDelete = array();
- foreach ($_POST['delrule'] as $k => $v)
- $toDelete[] = (int) $k;
- if (!empty($toDelete))
- $smcFunc['db_query']('', '
- DELETE FROM {db_prefix}pm_rules
- WHERE id_rule IN ({array_int:delete_list})
- AND id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'delete_list' => $toDelete,
- )
- );
- redirectexit('action=pm;sa=manrules');
- }
- }
- /**
- * This will apply rules to all unread messages. If all_messages is set will, clearly, do it to all!
- *
- * @param bool $all_messages = false
- */
- function ApplyRules($all_messages = false)
- {
- global $user_info, $smcFunc, $context, $options;
- // Want this - duh!
- loadRules();
- // No rules?
- if (empty($context['rules']))
- return;
- // Just unread ones?
- $ruleQuery = $all_messages ? '' : ' AND pmr.is_new = 1';
- // @todo Apply all should have timeout protection!
- // Get all the messages that match this.
- $request = $smcFunc['db_query']('', '
- SELECT
- pmr.id_pm, pm.id_member_from, pm.subject, pm.body, mem.id_group, pmr.labels
- FROM {db_prefix}pm_recipients AS pmr
- INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
- WHERE pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:not_deleted}
- ' . $ruleQuery,
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- )
- );
- $actions = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- foreach ($context['rules'] as $rule)
- {
- $match = false;
- // Loop through all the criteria hoping to make a match.
- foreach ($rule['criteria'] as $criterium)
- {
- 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))
- $match = true;
- // If we're adding and one criteria don't match then we stop!
- elseif ($rule['logic'] == 'and')
- {
- $match = false;
- break;
- }
- }
- // If we have a match the rule must be true - act!
- if ($match)
- {
- if ($rule['delete'])
- $actions['deletes'][] = $row['id_pm'];
- else
- {
- foreach ($rule['actions'] as $ruleAction)
- {
- if ($ruleAction['t'] == 'lab')
- {
- // Get a basic pot started!
- if (!isset($actions['labels'][$row['id_pm']]))
- $actions['labels'][$row['id_pm']] = empty($row['labels']) ? array() : explode(',', $row['labels']);
- $actions['labels'][$row['id_pm']][] = $ruleAction['v'];
- }
- }
- }
- }
- }
- }
- $smcFunc['db_free_result']($request);
- // Deletes are easy!
- if (!empty($actions['deletes']))
- deleteMessages($actions['deletes']);
- // Relabel?
- if (!empty($actions['labels']))
- {
- foreach ($actions['labels'] as $pm => $labels)
- {
- // Quickly check each label is valid!
- $realLabels = array();
- foreach ($context['labels'] as $label)
- if (in_array($label['id'], $labels) && ($label['id'] != -1 || empty($options['pm_remove_inbox_label'])))
- $realLabels[] = $label['id'];
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}pm_recipients
- SET labels = {string:new_labels}
- WHERE id_pm = {int:id_pm}
- AND id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'id_pm' => $pm,
- 'new_labels' => empty($realLabels) ? '' : implode(',', $realLabels),
- )
- );
- }
- }
- }
- /**
- * Load up all the rules for the current user.
- *
- * @param bool $reload = false
- */
- function LoadRules($reload = false)
- {
- global $user_info, $context, $smcFunc;
- if (isset($context['rules']) && !$reload)
- return;
- $request = $smcFunc['db_query']('', '
- SELECT
- id_rule, rule_name, criteria, actions, delete_pm, is_or
- FROM {db_prefix}pm_rules
- WHERE id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- )
- );
- $context['rules'] = array();
- // Simply fill in the data!
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- $context['rules'][$row['id_rule']] = array(
- 'id' => $row['id_rule'],
- 'name' => $row['rule_name'],
- 'criteria' => unserialize($row['criteria']),
- 'actions' => unserialize($row['actions']),
- 'delete' => $row['delete_pm'],
- 'logic' => $row['is_or'] ? 'or' : 'and',
- );
- if ($row['delete_pm'])
- $context['rules'][$row['id_rule']]['actions'][] = array('t' => 'del', 'v' => 1);
- }
- $smcFunc['db_free_result']($request);
- }
- /**
- * Check if the PM is available to the current user.
- *
- * @param int $pmID
- * @param $validFor
- * @return boolean
- */
- function isAccessiblePM($pmID, $validFor = 'in_or_outbox')
- {
- global $user_info, $smcFunc;
- $request = $smcFunc['db_query']('', '
- SELECT
- pm.id_member_from = {int:id_current_member} AND pm.deleted_by_sender = {int:not_deleted} AS valid_for_outbox,
- pmr.id_pm IS NOT NULL AS valid_for_inbox
- FROM {db_prefix}personal_messages AS pm
- 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})
- WHERE pm.id_pm = {int:id_pm}
- AND ((pm.id_member_from = {int:id_current_member} AND pm.deleted_by_sender = {int:not_deleted}) OR pmr.id_pm IS NOT NULL)',
- array(
- 'id_pm' => $pmID,
- 'id_current_member' => $user_info['id'],
- 'not_deleted' => 0,
- )
- );
- if ($smcFunc['db_num_rows']($request) === 0)
- {
- $smcFunc['db_free_result']($request);
- return false;
- }
- $validationResult = $smcFunc['db_fetch_assoc']($request);
- $smcFunc['db_free_result']($request);
- switch ($validFor)
- {
- case 'inbox':
- return !empty($validationResult['valid_for_inbox']);
- break;
- case 'outbox':
- return !empty($validationResult['valid_for_outbox']);
- break;
- case 'in_or_outbox':
- return !empty($validationResult['valid_for_inbox']) || !empty($validationResult['valid_for_outbox']);
- break;
- default:
- trigger_error('Undefined validation type given', E_USER_ERROR);
- break;
- }
- }
- ?>
|