forked from elkarte/Elkarte
/
PersonalMessage.controller.php
3136 lines (2669 loc) · 103 KB
/
PersonalMessage.controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This file is mainly meant for controlling the actions related to personal
* messages. It allows viewing, sending, deleting, and marking.
* For compatibility reasons, they are often called "instant messages".
*
* @name ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause
*
* This file contains code covered by:
* copyright: 2011 Simple Machines (http://www.simplemachines.org)
* license: BSD, See included LICENSE.TXT for terms and conditions.
*
* @version 1.1.7
*
*/
use ElkArte\Errors\ErrorContext;
/**
* PersonalMessage_Controller class
* It allows viewing, sending, deleting, and marking personal messages
*
* @package PersonalMessage
*/
class PersonalMessage_Controller extends Action_Controller
{
/**
* $_search_params will carry all settings that differ from the default
* search parameters. That way, the URLs involved in a search page will
* be kept as short as possible.
* @var array
*/
private $_search_params = array();
/**
* $_searchq_parameters will carry all the values needed by S_search_params
* @var array
*/
private $_searchq_parameters = array();
/**
* This method is executed before any other in this file (when the class is
* loaded by the dispatcher).
*
* What it does:
*
* - It sets the context, load templates and language file(s), as necessary
* for the function that will be called.
*/
public function pre_dispatch()
{
global $txt, $scripturl, $context, $user_info, $user_settings, $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 our PM functions such as mark, send, delete
require_once(SUBSDIR . '/PersonalMessage.subs.php');
// Templates, language, javascripts
loadLanguage('PersonalMessage');
loadJavascriptFile(array('PersonalMessage.js', 'suggest.js'));
if (!isset($this->_req->query->xml))
{
loadTemplate('PersonalMessage');
}
$this->_events->trigger('pre_dispatch', array('xml' => isset($this->_req->query->xml)));
// Load up the members maximum message capacity.
$this->_loadMessageLimit();
// A previous message was sent successfully? show a small indication.
if ($this->_req->getQuery('done') === 'sent')
{
$context['pm_sent'] = true;
}
// Load the label counts data.
if ($user_settings['new_pm'] || !Cache::instance()->getVar($context['labels'], 'labelCounts:' . $user_info['id'], 720))
{
$this->_loadLabels();
// Get the message count for each label
$context['labels'] = loadPMLabels($context['labels']);
}
// Now we have the labels, and assuming we have unsorted mail, apply our rules!
if ($user_settings['new_pm'])
{
// Apply our rules to the new PM's
applyRules();
require_once(SUBSDIR . '/Members.subs.php');
updateMemberData($user_info['id'], array('new_pm' => 0));
// Turn the new PM's status off, for the popup alert, since they have entered the PM area
toggleNewPM($user_info['id']);
}
// 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($this->_req->query->l) && isset($context['labels'][(int) $this->_req->query->l]) ? (int) $this->_req->query->l : -1;
$context['current_label'] = &$context['labels'][(int) $context['current_label_id']]['name'];
$context['folder'] = !isset($this->_req->query->f) || $this->_req->query->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($this->_req->query->start) ? ';start=' . $this->_req->query->start : '') . (isset($this->_req->query->l) ? ';l=' . $this->_req->query->l : '');
$context['can_issue_warning'] = in_array('w', $context['admin_features']) && allowedTo('issue_warning') && !empty($modSettings['warning_enable']);
// Build the linktree for all the actions...
$context['linktree'][] = array(
'url' => $scripturl . '?action=pm',
'name' => $txt['personal_messages']
);
// Preferences...
$context['display_mode'] = $user_settings['pm_prefs'] & 3;
}
/**
* Load a members message limit and prepares the limit bar
*/
private function _loadMessageLimit()
{
global $context, $txt, $user_info;
$context['message_limit'] = loadMessageLimit();
// 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)),
);
}
}
/**
* Loads the user defined label's for use in the template etc.
*/
private function _loadLabels()
{
global $context, $txt, $user_settings;
$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,
);
}
// The default inbox is always available
$context['labels'][-1] = array(
'id' => -1,
'name' => $txt['pm_msg_label_inbox'],
'messages' => 0,
'unread_messages' => 0,
);
}
/**
* This is the main function of personal messages, called before the action handler.
*
* What it does:
*
* - PersonalMessages is a menu-based controller.
* - It sets up the menu.
* - Calls from the menu the appropriate method/function for the current area.
*
* @see Action_Controller::action_index()
*/
public function action_index()
{
global $context;
// Finally all the things we know how to do
$subActions = array(
'manlabels' => array($this, 'action_manlabels', 'permission' => 'pm_read'),
'manrules' => array($this, 'action_manrules', 'permission' => 'pm_read'),
'markunread' => array($this, 'action_markunread', 'permission' => 'pm_read'),
'pmactions' => array($this, 'action_pmactions', 'permission' => 'pm_read'),
'prune' => array($this, 'action_prune', 'permission' => 'pm_read'),
'removeall' => array($this, 'action_removeall', 'permission' => 'pm_read'),
'removeall2' => array($this, 'action_removeall2', 'permission' => 'pm_read'),
'report' => array($this, 'action_report', 'permission' => 'pm_read'),
'search' => array($this, 'action_search', 'permission' => 'pm_read'),
'search2' => array($this, 'action_search2', 'permission' => 'pm_read'),
'send' => array($this, 'action_send', 'permission' => 'pm_read'),
'send2' => array($this, 'action_send2', 'permission' => 'pm_read'),
'settings' => array($this, 'action_settings', 'permission' => 'pm_read'),
'inbox' => array($this, 'action_folder', 'permission' => 'pm_read'),
);
// Set up our action array
$action = new Action('pm_index');
// Known action, go to it, otherwise the inbox for you
$subAction = $action->initialize($subActions, 'inbox');
// Set the right index bar for the action
if ($subAction === 'inbox')
{
$this->_messageIndexBar($context['current_label_id'] == -1 ? $context['folder'] : 'label' . $context['current_label_id']);
}
elseif (!isset($this->_req->query->xml))
{
$this->_messageIndexBar($subAction);
}
// And off we go!
$action->dispatch($subAction);
}
/**
* A menu to easily access different areas of the PM section
*
* @param string $area
*
* @throws Elk_Exception no_access
*/
private function _messageIndexBar($area)
{
global $txt, $context, $scripturl, $user_info;
require_once(SUBSDIR . '/Menu.subs.php');
$pm_areas = array(
'folders' => array(
'title' => $txt['pm_messages'],
'counter' => 'unread_messages',
'areas' => array(
'inbox' => array(
'label' => $txt['inbox'],
'custom_url' => $scripturl . '?action=pm',
'counter' => 'unread_messages',
),
'send' => array(
'label' => $txt['new_message'],
'custom_url' => $scripturl . '?action=pm;sa=send',
'permission' => 'pm_send',
),
'sent' => array(
'label' => $txt['sent_items'],
'custom_url' => $scripturl . '?action=pm;f=sent',
),
),
),
'labels' => array(
'title' => $txt['pm_labels'],
'counter' => 'labels_unread_total',
'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.
$label_counters = array('unread_messages' => $context['labels'][-1]['unread_messages']);
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 query string.
$label_counters['labels_unread_total'] = 0;
foreach ($context['labels'] as $label)
{
if ($label['id'] == -1)
{
continue;
}
// Count the amount of unread items in labels.
$label_counters['labels_unread_total'] += $label['unread_messages'];
// Add the label to the menu.
$pm_areas['labels']['areas']['label' . $label['id']] = array(
'label' => $label['name'],
'custom_url' => $scripturl . '?action=pm;l=' . $label['id'],
'counter' => 'label' . $label['id'],
'messages' => $label['messages'],
);
$label_counters['label' . $label['id']] = $label['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)
);
}
// Set a few options for the menu.
$menuOptions = array(
'current_area' => $area,
'hook' => 'pm',
'disable_url_session_check' => true,
'counters' => !empty($label_counters) ? $label_counters : 0,
'default_include_dir' => CONTROLLERDIR,
);
// 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() !== true))
{
throw new Elk_Exception('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.
$context['menu_item_selected'] = $pm_include_data['current_area'];
// Grab the file needed for this action
if (isset($pm_include_data['file']))
{
require_once($pm_include_data['file']);
}
// Set the template for this area and add the profile layer.
if (!isset($this->_req->query->xml))
{
$template_layers = Template_Layers::instance();
$template_layers->add('pm');
}
}
/**
* Display a folder, ie. inbox/sent etc.
*
* @uses folder sub template
* @uses subject_list, pm template layers
*/
public function action_folder()
{
global $txt, $scripturl, $modSettings, $context, $subjects_request;
global $messages_request, $user_info, $recipients, $options, $user_settings;
// Changing view?
if (isset($this->_req->query->view))
{
$context['display_mode'] = $context['display_mode'] > 1 ? 0 : $context['display_mode'] + 1;
require_once(SUBSDIR . '/Members.subs.php');
updateMemberData($user_info['id'], array('pm_prefs' => ($user_settings['pm_prefs'] & 252) | $context['display_mode']));
}
// Make sure the starting location is valid.
if (isset($this->_req->query->start) && $this->_req->query->start !== 'new')
{
$start = (int) $this->_req->query->start;
}
elseif (!isset($this->_req->query->start) && !empty($options['view_newest_pm_first']))
{
$start = 0;
}
else
{
$start = 'new';
}
// Set up some basic template stuff.
$context['from_or_to'] = $context['folder'] !== 'sent' ? 'from' : 'to';
$context['get_pmessage'] = 'preparePMContext_callback';
$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();
// Set the template layers we need
$template_layers = Template_Layers::instance();
$template_layers->addAfter('subject_list', 'pm');
$labelQuery = $context['folder'] !== 'sent' ? '
AND FIND_IN_SET(' . $context['current_label_id'] . ', pmr.labels) != 0' : '';
// They didn't pick a sort, so we use the forum default.
$sort_by = !isset($this->_req->query->sort) ? 'date' : $this->_req->query->sort;
$descending = isset($this->_req->query->desc);
// Set our sort by query
switch ($sort_by)
{
case 'date':
$sort_by_query = 'pm.id_pm';
if (!empty($options['view_newest_pm_first']) && !isset($this->_req->query->desc) && !isset($this->_req->query->asc))
{
$descending = true;
}
break;
case 'name':
$sort_by_query = 'COALESCE(mem.real_name, \'\')';
break;
case 'subject':
$sort_by_query = 'pm.subject';
break;
default:
$sort_by_query = 'pm.id_pm';
}
// 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 if we also have 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.
$max_messages = getPMCount(false, null, $labelQuery);
// Only show the button if there are messages to delete.
$context['show_delete'] = $max_messages > 0;
// Start on the last page.
if (!is_numeric($start) || $start >= $max_messages)
{
$start = ($max_messages - 1) - (($max_messages - 1) % $modSettings['defaultMaxMessages']);
}
elseif ($start < 0)
{
$start = 0;
}
// ... but wait - what if we want to start from a specific message?
if (isset($this->_req->query->pmid))
{
$pmID = (int) $this->_req->query->pmid;
// Make sure you have access to this PM.
if (!isAccessiblePM($pmID, $context['folder'] === 'sent' ? 'outbox' : 'inbox'))
{
throw new Elk_Exception('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'])
{
$start = 0;
}
// If we pass kstart we assume we're in the right place.
elseif (!isset($this->_req->query->kstart))
{
$start = getPMCount($descending, $pmID, $labelQuery);
// To stop the page index's being abnormal, start the page on the page the message
// would normally be located on...
$start = $modSettings['defaultMaxMessages'] * (int) ($start / $modSettings['defaultMaxMessages']);
}
}
// Sanitize and validate pmsg variable if set.
if (isset($this->_req->query->pmsg))
{
$pmsg = (int) $this->_req->query->pmsg;
if (!isAccessiblePM($pmsg, $context['folder'] === 'sent' ? 'outbox' : 'inbox'))
{
throw new Elk_Exception('no_access', false);
}
}
// Determine the navigation context
$context['links'] += array(
'prev' => $start >= $modSettings['defaultMaxMessages'] ? $scripturl . '?action=pm;start=' . ($start - $modSettings['defaultMaxMessages']) : '',
'next' => $start + $modSettings['defaultMaxMessages'] < $max_messages ? $scripturl . '?action=pm;start=' . ($start + $modSettings['defaultMaxMessages']) : '',
);
$context['page_info'] = array(
'current_page' => $start / $modSettings['defaultMaxMessages'] + 1,
'num_pages' => floor(($max_messages - 1) / $modSettings['defaultMaxMessages']) + 1
);
// We now know what they want, so lets fetch those PM's
list ($pms, $posters, $recipients, $lastData) = loadPMs(array(
'sort_by_query' => $sort_by_query,
'display_mode' => $context['display_mode'],
'sort_by' => $sort_by,
'label_query' => $labelQuery,
'pmsg' => isset($pmsg) ? (int) $pmsg : 0,
'descending' => $descending,
'start' => $start,
'limit' => $modSettings['defaultMaxMessages'],
'folder' => $context['folder'],
'pmid' => isset($pmID) ? $pmID : 0,
), $user_info['id']);
// Make sure that we have been given a correct head pm id if we are in conversation mode
if ($context['display_mode'] == 2 && !empty($pmID) && $pmID != $lastData['id'])
{
throw new Elk_Exception('no_access', false);
}
// If loadPMs returned results, lets show the pm subject list
if (!empty($pms))
{
// Tell the template if no pm has specifically been selected
if (empty($pmID))
{
$context['current_pm'] = 0;
}
// This is a list of the pm's that are used for "show all" display.
if ($context['display_mode'] == 0)
{
$display_pms = $pms;
}
// Just use the last pm the user received to start things off
else
{
$display_pms = array($lastData['id']);
}
// At this point we know the main id_pm's. But if we are looking at conversations we need
// the PMs that make up the conversation
if ($context['display_mode'] == 2)
{
list($display_pms, $posters) = loadConversationList($lastData['head'], $recipients, $context['folder']);
// Conversation list may expose additional PM's being displayed
$all_pms = array_unique(array_merge($pms, $display_pms));
// See if any of these 'listing' PM's are in a conversation thread that has unread entries
$context['conversation_unread'] = loadConversationUnreadStatus($all_pms);
}
// This is pretty much EVERY pm!
else
{
$all_pms = array_unique(array_merge($pms, $display_pms));
}
// Get recipients (don't include bcc-recipients for your inbox, you're not supposed to know :P).
list($context['message_labels'], $context['message_replied'], $context['message_unread']) = loadPMRecipientInfo($all_pms, $recipients, $context['folder']);
// Make sure we don't load any unnecessary data for one at a time mode
if ($context['display_mode'] == 1)
{
foreach ($posters as $pm_key => $sender)
{
if (!in_array($pm_key, $display_pms))
{
unset($posters[$pm_key]);
}
}
}
// Load some information about the message sender
$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;
// Separate query for these bits, preparePMContext_callback will use it as required
$subjects_request = loadPMSubjectRequest($pms, $orderBy);
}
// Execute the load message query if a message has been chosen and let
// preparePMContext_callback fetch the results. Otherwise just show the pm selection list
if (empty($pmsg) && empty($pmID) && $context['display_mode'] != 0)
{
$messages_request = false;
}
else
{
$messages_request = loadPMMessageRequest($display_pms, $sort_by_query, $sort_by, $descending, $context['display_mode'], $context['folder']);
}
}
else
{
$messages_request = false;
}
// Prepare some items for the template
$context['can_send_pm'] = allowedTo('pm_send');
$context['can_send_email'] = allowedTo('send_email_to_members');
$context['sub_template'] = 'folder';
$context['page_title'] = $txt['pm_inbox'];
$context['sort_direction'] = $descending ? 'down' : 'up';
$context['sort_by'] = $sort_by;
// Auto video embedding enabled, someone may have a link in a PM
if (!empty($messages_request) && !empty($modSettings['enableVideoEmbeding']))
{
addInlineJavascript('
$(function() {
$().linkifyvideo(oEmbedtext);
});', true
);
}
if (!empty($messages_request) && !empty($context['show_delete']))
{
Template_Layers::instance()->addEnd('pm_pages_and_buttons');
}
// Set up the page index.
$context['page_index'] = constructPageIndex($scripturl . '?action=pm;f=' . $context['folder'] . (isset($this->_req->query->l) ? ';l=' . (int) $this->_req->query->l : '') . ';sort=' . $context['sort_by'] . ($descending ? ';desc' : ''), $start, $max_messages, $modSettings['defaultMaxMessages']);
$context['start'] = $start;
$context['pm_form_url'] = $scripturl . '?action=pm;sa=pmactions;' . ($context['display_mode'] == 2 ? 'conversation;' : '') . 'f=' . $context['folder'] . ';start=' . $context['start'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '');
// 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 currently displayed ones!
elseif (!empty($context['current_pm']))
{
markMessages($display_pms, $context['current_label_id']);
}
}
// Build the conversation button array.
if ($context['display_mode'] === 2 && !empty($context['current_pm']))
{
$context['conversation_buttons'] = array(
'delete' => array(
'text' => 'delete_conversation',
'image' => 'delete.png',
'lang' => true,
'url' => $scripturl . '?action=pm;sa=pmactions;pm_actions%5B' . $context['current_pm'] . '%5D=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');
}
}
/**
* Send a new personal message?
*/
public function action_send()
{
global $txt, $scripturl, $modSettings, $context, $user_info;
// Load in some text and template dependencies
loadLanguage('PersonalMessage');
loadTemplate('PersonalMessage');
// Set the template we will use
$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 up some items for the template
$context['page_title'] = $txt['send_message'];
$context['reply'] = isset($this->_req->query->pmsg) || isset($this->_req->query->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?
$pmCount = pmCount($user_info['id'], 3600);
if (!empty($pmCount) && $pmCount >= $modSettings['pm_posts_per_hour'])
{
throw new Elk_Exception('pm_too_many_per_hour', true, array($modSettings['pm_posts_per_hour']));
}
}
try
{
$this->_events->trigger('before_set_context', array('pmsg' => isset($this->_req->query->pmsg) ? $this->_req->query->pmsg : (isset($this->_req->query->quote) ? $this->_req->query->quote : 0)));
}
catch (Pm_Error_Exception $e)
{
return $this->messagePostError($e->namedRecipientList, $e->recipientList, $e->msgOptions);
}
// Quoting / Replying to a message?
if (!empty($this->_req->query->pmsg))
{
$pmsg = $this->_req->getQuery('pmsg', 'intval');
// Make sure this is accessible (not deleted)
if (!isAccessiblePM($pmsg))
{
throw new Elk_Exception('no_access', false);
}
// Validate that this is one has been received?
$isReceived = checkPMReceived($pmsg);
// Get the quoted message (and make sure you're allowed to see this quote!).
$row_quoted = loadPMQuote($pmsg, $isReceived);
if ($row_quoted === false)
{
throw new Elk_Exception('pm_not_yours', false);
}
// Censor the message.
$row_quoted['subject'] = censor($row_quoted['subject']);
$row_quoted['body'] = censor($row_quoted['body']);
// Lets make sure we mark this one as read
markMessages($pmsg);
// Figure out which flavor or 'Re: ' to use
$context['response_prefix'] = response_prefix();
$form_subject = $row_quoted['subject'];
// Add 'Re: ' to it....
if ($context['reply'] && trim($context['response_prefix']) != '' && Util::strpos($form_subject, trim($context['response_prefix'])) !== 0)
{
$form_subject = $context['response_prefix'] . $form_subject;
}
// If quoting, lets clean up some things and set the quote header for the pm body
if (isset($this->_req->query->quote))
{
// Remove any nested quotes and <br />...
$form_message = preg_replace('~<br ?/?' . '>~i', "\n", $row_quoted['body']);
$form_message = removeNestedQuotes($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.
$bbc_parser = \BBC\ParserWrapper::instance();
$row_quoted['body'] = $bbc_parser->parsePM($row_quoted['body']);
// 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' => standardTime($row_quoted['msgtime']),
'html_time' => htmlTime($row_quoted['msgtime']),
'timestamp' => forum_time(true, $row_quoted['msgtime']),
'body' => $row_quoted['body']
);
}
// A new message it is then
else
{
$context['quoted_message'] = false;
$form_subject = '';
$form_message = '';
}
// Start of like we don't know where this is going
$context['recipients'] = array(
'to' => array(),
'bcc' => array(),
);
// Sending by ID? Replying to all? Fetch the real_name(s).
if (isset($this->_req->query->u))
{
// If the user is replying to all, get all the other members this was sent to..
if ($this->_req->query->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'], ENT_COMPAT, 'UTF-8'),
);
}
// Now to get all the others.
$context['recipients']['to'] = array_merge($context['recipients']['to'], isset($pmsg) ? loadPMRecipientsAll($pmsg) : array());
}
else
{
$users = array_map('intval', explode(',', $this->_req->query->u));
$users = array_unique($users);
// For all the member's this is going to, get their display name.
require_once(SUBSDIR . '/Members.subs.php');
$result = getBasicMemberData($users);
foreach ($result as $row)
{
$context['recipients']['to'][] = array(
'id' => $row['id_member'],
'name' => $row['real_name'],
);
}
}
// 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);
// And build the link tree.
$context['linktree'][] = array(
'url' => $scripturl . '?action=pm;sa=send',
'name' => $txt['new_message']
);
// Needed for the editor.
require_once(SUBSDIR . '/Editor.subs.php');
// Now create the editor.
$editorOptions = array(
'id' => 'message',
'value' => $context['message'],
'height' => '250px',
'width' => '100%',
'labels' => array(
'post_button' => $txt['send_message'],
),
'preview_type' => 2,
);
// Trigger the prepare_send_context PM event
$this->_events->trigger('prepare_send_context', array('editorOptions' => &$editorOptions));
create_control_richedit($editorOptions);
// No one is bcc'ed just yet
$context['bcc_value'] = '';
// Register this form and get a sequence number in $context.
checkSubmitOnce('register');
}
/**
* Send a personal message.
*/
public function action_send2()
{
global $txt, $context, $user_info, $modSettings;
// All the helpers we need
require_once(SUBSDIR . '/Auth.subs.php');
require_once(SUBSDIR . '/Post.subs.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 = ErrorContext::context('pm', 1);
// 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?
$pmCount = pmCount($user_info['id'], 3600);
if (!empty($pmCount) && $pmCount >= $modSettings['pm_posts_per_hour'])
{
if (!isset($this->_req->query->xml))
{
throw new Elk_Exception('pm_too_many_per_hour', true, array($modSettings['pm_posts_per_hour']));
}
else
{
$post_errors->addError('pm_too_many_per_hour');
}
}
}
// If your session timed out, show an error, but do allow to re-submit.
if (!isset($this->_req->query->xml) && checkSession('post', '', false) != '')
{
$post_errors->addError('session_timeout');
}
$this->_req->post->subject = isset($this->_req->post->subject) ? strtr(Util::htmltrim($this->_req->post->subject), array("\r" => '', "\n" => '', "\t" => '')) : '';
$this->_req->post->to = $this->_req->getPost('to', 'trim', empty($this->_req->query->to) ? '' : $this->_req->query->to);
$this->_req->post->bcc = $this->_req->getPost('bcc', 'trim', empty($this->_req->query->bcc) ? '' : $this->_req->query->bcc);
// Route the input from the 'u' parameter to the 'to'-list.
if (!empty($this->_req->post->u))
{
$this->_req->post->recipient_to = explode(',', $this->_req->post->u);
}
$bbc_parser = \BBC\ParserWrapper::instance();