forked from SimpleMachines/SMF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PersonalMessage.php
4202 lines (3670 loc) · 144 KB
/
PersonalMessage.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 personal
* messages. For compatibility reasons, they are often called "instant messages".
*
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines https://www.simplemachines.org
* @copyright 2022 Simple Machines and individual contributors
* @license https://www.simplemachines.org/about/smf/license.php BSD
*
* @version 2.1.0
*/
if (!defined('SMF'))
die('No direct access...');
/**
* This helps organize things...
*
* @todo this should be a simple dispatcher....
*/
function MessageMain()
{
global $txt, $scripturl, $sourcedir, $context, $user_info, $user_settings, $smcFunc, $modSettings, $options;
// 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 (!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;
$context['labels'] = array();
// Load the label data.
if ($user_settings['new_pm'] || ($context['labels'] = cache_get_data('labelCounts:' . $user_info['id'], 720)) === null)
{
// Looks like we need to reseek!
// Inbox "label"
$context['labels'][-1] = array(
'id' => -1,
'name' => $txt['pm_msg_label_inbox'],
'messages' => 0,
'unread_messages' => 0,
);
// First get the inbox counts
// The CASE WHEN here is because is_read is set to 3 when you reply to a message
$result = $smcFunc['db_query']('', '
SELECT COUNT(*) AS total, SUM(is_read & 1) AS num_read
FROM {db_prefix}pm_recipients
WHERE id_member = {int:current_member}
AND in_inbox = {int:in_inbox}
AND deleted = {int:not_deleted}',
array(
'current_member' => $user_info['id'],
'in_inbox' => 1,
'not_deleted' => 0,
)
);
while ($row = $smcFunc['db_fetch_assoc']($result))
{
$context['labels'][-1]['messages'] = $row['total'];
$context['labels'][-1]['unread_messages'] = $row['total'] - $row['num_read'];
}
$smcFunc['db_free_result']($result);
// Now load info about all the other labels
$result = $smcFunc['db_query']('', '
SELECT l.id_label, l.name, COALESCE(SUM(pr.is_read & 1), 0) AS num_read, COALESCE(COUNT(pr.id_pm), 0) AS total
FROM {db_prefix}pm_labels AS l
LEFT JOIN {db_prefix}pm_labeled_messages AS pl ON (pl.id_label = l.id_label)
LEFT JOIN {db_prefix}pm_recipients AS pr ON (pr.id_pm = pl.id_pm)
WHERE l.id_member = {int:current_member}
GROUP BY l.id_label, l.name',
array(
'current_member' => $user_info['id'],
)
);
while ($row = $smcFunc['db_fetch_assoc']($result))
{
$context['labels'][$row['id_label']] = array(
'id' => $row['id_label'],
'name' => $row['name'],
'messages' => $row['total'],
'unread_messages' => $row['total'] - $row['num_read']
);
}
$smcFunc['db_free_result']($result);
// Store it please!
cache_put_data('labelCounts:' . $user_info['id'], $context['labels'], 720);
}
// Now we have the labels, and assuming we have unsorted mail, apply our rules!
if ($user_settings['new_pm'])
{
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,
)
);
}
// 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'][$_REQUEST['l']]) ? (int) $_REQUEST['l'] : -1;
$context['current_label'] = &$context['labels'][$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'] = allowedTo('issue_warning') && $modSettings['warning_settings'][0] == 1;
$context['can_moderate_forum'] = allowedTo('moderate_forum');
// Are PM drafts enabled?
$context['drafts_pm_save'] = !empty($modSettings['drafts_pm_enabled']) && allowedTo('pm_draft');
$context['drafts_autosave'] = !empty($context['drafts_pm_save']) && !empty($modSettings['drafts_autosave_enabled']) && !empty($options['drafts_autosave_enabled']);
// 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;
$subActions = array(
'popup' => 'MessagePopup',
'manlabels' => 'ManageLabels',
'manrules' => 'ManageRules',
'pmactions' => 'MessageActionsApply',
'prune' => 'MessagePrune',
'removeall2' => 'MessageKillAll',
'report' => 'ReportMessage',
'search' => 'MessageSearch',
'search2' => 'MessageSearch2',
'send' => 'MessagePost',
'send2' => 'MessagePost2',
'settings' => 'MessageSettings',
'showpmdrafts' => 'MessageDrafts',
);
if (!isset($_REQUEST['sa']) || !isset($subActions[$_REQUEST['sa']]))
{
$_REQUEST['sa'] = '';
MessageFolder();
}
else
{
if (!isset($_REQUEST['xml']) && $_REQUEST['sa'] != 'popup')
messageIndexBar($_REQUEST['sa']);
call_helper($subActions[$_REQUEST['sa']]);
}
}
/**
* A menu to easily access different areas of the PM section
*
* @param string $area The area we're currently in
*/
function messageIndexBar($area)
{
global $txt, $context, $scripturl, $sourcedir, $modSettings, $user_info;
$pm_areas = array(
'folders' => array(
'title' => $txt['pm_messages'],
'areas' => array(
'inbox' => array(
'label' => $txt['inbox'],
'custom_url' => $scripturl . '?action=pm',
'amt' => 0,
),
'send' => array(
'label' => $txt['new_message'],
'custom_url' => $scripturl . '?action=pm;sa=send',
'permission' => 'pm_send',
'amt' => 0,
),
'sent' => array(
'label' => $txt['sent_items'],
'custom_url' => $scripturl . '?action=pm;f=sent',
'amt' => 0,
),
'drafts' => array(
'label' => $txt['drafts_show'],
'custom_url' => $scripturl . '?action=pm;sa=showpmdrafts',
'permission' => 'pm_draft',
'enabled' => !empty($modSettings['drafts_pm_enabled']),
'amt' => 0,
),
),
'amt' => 0,
),
'labels' => array(
'title' => $txt['pm_labels'],
'areas' => array(),
'amt' => 0,
),
'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.
foreach ($context['labels'] as $label)
{
if ($label['id'] == -1)
continue;
// Count the amount of unread items in labels.
$pm_areas['labels']['amt'] += $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'],
'amt' => $label['unread_messages'],
'unread_messages' => $label['unread_messages'],
'messages' => $label['messages'],
'icon' => 'folder',
);
}
}
$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']['amt'] = $context['labels'][-1]['unread_messages'];
$pm_areas['folders']['amt'] = $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');
// 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 (!isset($_REQUEST['xml']))
$context['template_layers'][] = 'pm';
}
/**
* The popup for when we ask for the popup from the user.
*/
function MessagePopup()
{
global $context, $modSettings, $smcFunc, $memberContext, $scripturl, $user_settings, $db_show_debug;
// We do not want to output debug information here.
$db_show_debug = false;
// We only want to output our little layer here.
$context['template_layers'] = array();
$context['sub_template'] = 'pm_popup';
$context['can_send_pm'] = allowedTo('pm_send');
$context['can_draft'] = allowedTo('pm_draft') && !empty($modSettings['drafts_pm_enabled']);
// So are we loading stuff?
$request = $smcFunc['db_query']('', '
SELECT id_pm
FROM {db_prefix}pm_recipients AS pmr
WHERE pmr.id_member = {int:current_member}
AND is_read = {int:not_read}
AND deleted = {int:not_deleted}
ORDER BY id_pm',
array(
'current_member' => $context['user']['id'],
'not_read' => 0,
'not_deleted' => 0,
)
);
$pms = array();
while ($row = $smcFunc['db_fetch_row']($request))
$pms[] = $row[0];
$smcFunc['db_free_result']($request);
if (!empty($pms))
{
// Just quickly, it's possible that the number of PMs can get out of sync.
$count_unread = count($pms);
if ($count_unread != $user_settings['unread_messages'])
{
updateMemberData($context['user']['id'], array('unread_messages' => $count_unread));
$context['user']['unread_messages'] = count($pms);
}
// Now, actually fetch me some PMs. Make sure we track the senders, got some work to do for them.
$senders = array();
$request = $smcFunc['db_query']('', '
SELECT pm.id_pm, pm.id_pm_head, COALESCE(mem.id_member, pm.id_member_from) AS id_member_from,
COALESCE(mem.real_name, pm.from_name) AS member_from, pm.msgtime AS timestamp, pm.subject
FROM {db_prefix}personal_messages AS pm
LEFT JOIN {db_prefix}members AS mem ON (pm.id_member_from = mem.id_member)
WHERE pm.id_pm IN ({array_int:id_pms})',
array(
'id_pms' => $pms,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (!empty($row['id_member_from']))
$senders[] = $row['id_member_from'];
$row['replied_to_you'] = $row['id_pm'] != $row['id_pm_head'];
$row['time'] = timeformat($row['timestamp']);
$row['pm_link'] = '<a href="' . $scripturl . '?action=pm;f=inbox;pmsg=' . $row['id_pm'] . '">' . $row['subject'] . '</a>';
$context['unread_pms'][$row['id_pm']] = $row;
}
$smcFunc['db_free_result']($request);
$senders = loadMemberData($senders);
foreach ($senders as $member)
loadMemberContext($member);
// Having loaded everyone, attach them to the PMs.
foreach ($context['unread_pms'] as $id_pm => $details)
if (!empty($memberContext[$details['id_member_from']]))
$context['unread_pms'][$id_pm]['member'] = &$memberContext[$details['id_member_from']];
}
}
/**
* A folder, ie. inbox/sent etc.
*/
function MessageFolder()
{
global $txt, $scripturl, $modSettings, $context, $subjects_request;
global $messages_request, $user_info, $recipients, $options, $smcFunc, $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();
// Prevent signature images from going outside the box.
if ($context['signature_enabled'])
{
list ($sig_limits, $sig_bbc) = explode(':', $modSettings['signature_settings']);
$sig_limits = explode(',', $sig_limits);
if (!empty($sig_limits[5]) || !empty($sig_limits[6]))
addInlineCss('
.signature img { ' . (!empty($sig_limits[5]) ? 'max-width: ' . (int) $sig_limits[5] . 'px; ' : '') . (!empty($sig_limits[6]) ? 'max-height: ' . (int) $sig_limits[6] . 'px; ' : '') . '}');
}
$labelJoin = '';
$labelQuery = '';
$labelQuery2 = '';
// SMF logic: If you're viewing a label, it's still the inbox
if ($context['folder'] == 'inbox' && $context['current_label_id'] == -1)
{
$labelQuery = '
AND pmr.in_inbox = 1';
}
elseif ($context['folder'] != 'sent')
{
$labelJoin = '
INNER JOIN {db_prefix}pm_labeled_messages AS pl ON (pl.id_pm = pmr.id_pm)';
$labelQuery2 = '
AND pl.id_label = ' . $context['current_label_id'];
}
// 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' => 'COALESCE(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)' : '') . $labelJoin . '
WHERE pmr.id_member = {int:current_member}
AND pmr.deleted = {int:not_deleted}' . $labelQuery . $labelQuery2,
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;
$maxPerPage = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
// Start on the last page.
if (!is_numeric($_GET['start']) || $_GET['start'] >= $max_messages)
$_GET['start'] = ($max_messages - 1) - (($max_messages - 1) % $maxPerPage);
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 <= $maxPerPage)
$_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)' : '') . $labelJoin . '
WHERE pmr.id_member = {int:current_member}
AND pmr.deleted = {int:not_deleted}' . $labelQuery . $labelQuery2 . '
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'] = $maxPerPage * (int) ($_GET['start'] / $maxPerPage);
}
}
// 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, $maxPerPage);
$context['start'] = $_GET['start'];
// Determine the navigation context.
$context['links'] = array(
'first' => $_GET['start'] >= $maxPerPage ? $scripturl . '?action=pm;start=0' : '',
'prev' => $_GET['start'] >= $maxPerPage ? $scripturl . '?action=pm;start=' . ($_GET['start'] - $maxPerPage) : '',
'next' => $_GET['start'] + $maxPerPage < $max_messages ? $scripturl . '?action=pm;start=' . ($_GET['start'] + $maxPerPage) : '',
'last' => $_GET['start'] + $maxPerPage < $max_messages ? $scripturl . '?action=pm;start=' . (floor(($max_messages - 1) / $maxPerPage) * $maxPerPage) : '',
'up' => $scripturl,
);
$context['page_info'] = array(
'current_page' => $_GET['start'] / $maxPerPage + 1,
'num_pages' => floor(($max_messages - 1) / $maxPerPage) + 1
);
// First work out what messages we need to see - if grouped is a little trickier...
if ($context['display_mode'] == 2)
{
if ($context['folder'] != 'sent' && $context['folder'] != 'inbox')
{
$labelJoin = '
INNER JOIN {db_prefix}pm_labeled_messages AS pl ON (pl.id_pm = pm.id_pm)';
$labelQuery = '';
$labelQuery2 = '
AND pl.id_label = ' . $context['current_label_id'];
}
$request = $smcFunc['db_query']('', '
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 . ')') . $labelJoin . ($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}') . $labelQuery2 . '
GROUP BY pm.id_pm_head' . ($_GET['sort'] != 'pm.id_pm' ? ',' . $_GET['sort'] : '') . '
ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($_GET['pmsg']) ? '
LIMIT ' . $_GET['start'] . ', ' . $maxPerPage : ''),
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 . ')') . $labelJoin . ($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:is_deleted}' : '1=1') . (empty($pmsg) ? '' : '
AND pm.id_pm = {int:pmsg}') . $labelQuery2 . '
ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'pmr.id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
LIMIT ' . $_GET['start'] . ', ' . $maxPerPage : ''),
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.in_inbox, 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>';
$context['folder'] == 'sent' && $context['display_mode'] != 2 ? $context['message_replied'][$row['id_pm']] = $row['is_read'] & 2 : '';
}
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;
// Get the labels for this PM
$request2 = $smcFunc['db_query']('', '
SELECT id_label
FROM {db_prefix}pm_labeled_messages
WHERE id_pm = {int:current_pm}',
array(
'current_pm' => $row['id_pm'],
)
);
while ($row2 = $smcFunc['db_fetch_assoc']($request2))
{
$l_id = $row2['id_label'];
if (isset($context['labels'][$l_id]))
$context['message_labels'][$row['id_pm']][$l_id] = array('id' => $l_id, 'name' => $context['labels'][$l_id]['name']);
}
$smcFunc['db_free_result']($request2);
// Is this in the inbox as well?
if ($row['in_inbox'] == 1)
{
$context['message_labels'][$row['id_pm']][-1] = array('id' => -1, 'name' => $context['labels'][-1]['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....
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, COALESCE(pm.id_member_from, 0) AS id_member_from, pm.msgtime, COALESCE(mem.real_name, pm.from_name) AS from_name,
mem.id_member
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 {int:limit}',
array(
'pm_list' => $pms,
'limit' => count($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' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . '
LIMIT {int:limit}',
array(
'display_pms' => $display_pms,
'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
'limit' => count($display_pms),
'sort' => $_GET['sort'],
)
);
// Build the conversation button array.
if ($context['display_mode'] == 2)
{
$context['conversation_buttons'] = array(
'delete' => array('text' => 'delete_conversation', 'image' => 'delete.png', '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' => 'data-confirm="' . $txt['remove_conversation'] . '"', 'class' => 'you_sure'),
);
// Allow mods to add additional buttons here
call_integration_hook('integrate_conversation_buttons');
}
}
else
$messages_request = false;
$context['can_send_pm'] = allowedTo('pm_send');
$context['can_send_email'] = allowedTo('moderate_forum');
$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']);
}
}
/**
* Get a personal message for the theme. (used to save memory.)
*
* @param string $type The type of message
* @param bool $reset Whether to reset the internal pointer
* @return bool|array False on failure, otherwise an array of info
*/
function prepareMessageContext($type = 'subject', $reset = false)
{
global $txt, $scripturl, $modSettings, $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;
}