This repository was archived by the owner on Mar 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions_privmsgs.php
2303 lines (1957 loc) · 67.6 KB
/
functions_privmsgs.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
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
*/
if (!defined('IN_PHPBB'))
{
exit;
}
// wesnoth mod begin
define('WESNOTH_JETREL_PM_LIMIT', 100000);
// wesnoth mod end
/*
Ability to simply add own rules by doing three things:
1) Add an appropriate constant
2) Add a new check array to the global_privmsgs_rules variable and the condition array (if one is required)
3) Add a new language variable to ucp.php
The user is then able to select the new rule. It will be checked against and handled as specified.
To add new actions (yes, checks can be added here too) to the rule management, the core code has to be modified.
*/
define('RULE_IS_LIKE', 1); // Is Like
define('RULE_IS_NOT_LIKE', 2); // Is Not Like
define('RULE_IS', 3); // Is
define('RULE_IS_NOT', 4); // Is Not
define('RULE_BEGINS_WITH', 5); // Begins with
define('RULE_ENDS_WITH', 6); // Ends with
define('RULE_IS_FRIEND', 7); // Is Friend
define('RULE_IS_FOE', 8); // Is Foe
define('RULE_IS_USER', 9); // Is User
define('RULE_IS_GROUP', 10); // Is In Usergroup
define('RULE_ANSWERED', 11); // Answered
define('RULE_FORWARDED', 12); // Forwarded
define('RULE_TO_GROUP', 14); // Usergroup
define('RULE_TO_ME', 15); // Me
define('ACTION_PLACE_INTO_FOLDER', 1);
define('ACTION_MARK_AS_READ', 2);
define('ACTION_MARK_AS_IMPORTANT', 3);
define('ACTION_DELETE_MESSAGE', 4);
define('CHECK_SUBJECT', 1);
define('CHECK_SENDER', 2);
define('CHECK_MESSAGE', 3);
define('CHECK_STATUS', 4);
define('CHECK_TO', 5);
/**
* Global private message rules
* These rules define what to do if a rule is hit
*/
$global_privmsgs_rules = array(
CHECK_SUBJECT => array(
RULE_IS_LIKE => array('check0' => 'message_subject', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
RULE_IS_NOT_LIKE => array('check0' => 'message_subject', 'function' => '!(preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0}))'),
RULE_IS => array('check0' => 'message_subject', 'function' => '{CHECK0} == {STRING}'),
RULE_IS_NOT => array('check0' => 'message_subject', 'function' => '{CHECK0} != {STRING}'),
RULE_BEGINS_WITH => array('check0' => 'message_subject', 'function' => 'preg_match("/^" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
RULE_ENDS_WITH => array('check0' => 'message_subject', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "$/i", {CHECK0})'),
),
CHECK_SENDER => array(
RULE_IS_LIKE => array('check0' => 'username', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
RULE_IS_NOT_LIKE => array('check0' => 'username', 'function' => '!(preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0}))'),
RULE_IS => array('check0' => 'username', 'function' => '{CHECK0} == {STRING}'),
RULE_IS_NOT => array('check0' => 'username', 'function' => '{CHECK0} != {STRING}'),
RULE_BEGINS_WITH => array('check0' => 'username', 'function' => 'preg_match("/^" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
RULE_ENDS_WITH => array('check0' => 'username', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "$/i", {CHECK0})'),
RULE_IS_FRIEND => array('check0' => 'friend', 'function' => '{CHECK0} == 1'),
RULE_IS_FOE => array('check0' => 'foe', 'function' => '{CHECK0} == 1'),
RULE_IS_USER => array('check0' => 'author_id', 'function' => '{CHECK0} == {USER_ID}'),
RULE_IS_GROUP => array('check0' => 'author_in_group', 'function' => 'in_array({GROUP_ID}, {CHECK0})'),
),
CHECK_MESSAGE => array(
RULE_IS_LIKE => array('check0' => 'message_text', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
RULE_IS_NOT_LIKE => array('check0' => 'message_text', 'function' => '!(preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0}))'),
RULE_IS => array('check0' => 'message_text', 'function' => '{CHECK0} == {STRING}'),
RULE_IS_NOT => array('check0' => 'message_text', 'function' => '{CHECK0} != {STRING}'),
),
CHECK_STATUS => array(
RULE_ANSWERED => array('check0' => 'pm_replied', 'function' => '{CHECK0} == 1'),
RULE_FORWARDED => array('check0' => 'pm_forwarded', 'function' => '{CHECK0} == 1'),
),
CHECK_TO => array(
RULE_TO_GROUP => array('check0' => 'to', 'check1' => 'bcc', 'check2' => 'user_in_group', 'function' => 'in_array("g_" . {CHECK2}, {CHECK0}) || in_array("g_" . {CHECK2}, {CHECK1})'),
RULE_TO_ME => array('check0' => 'to', 'check1' => 'bcc', 'function' => 'in_array("u_" . $user_id, {CHECK0}) || in_array("u_" . $user_id, {CHECK1})'),
)
);
/**
* This is for defining which condition fields to show for which Rule
*/
$global_rule_conditions = array(
RULE_IS_LIKE => 'text',
RULE_IS_NOT_LIKE => 'text',
RULE_IS => 'text',
RULE_IS_NOT => 'text',
RULE_BEGINS_WITH => 'text',
RULE_ENDS_WITH => 'text',
RULE_IS_USER => 'user',
RULE_IS_GROUP => 'group'
);
/**
* Get all folder
*/
function get_folder($user_id, $folder_id = false)
{
global $db, $user, $template;
global $phpbb_root_path, $phpEx;
$folder = array();
// Get folder information
$sql = 'SELECT folder_id, COUNT(msg_id) as num_messages, SUM(pm_unread) as num_unread
FROM ' . PRIVMSGS_TO_TABLE . "
WHERE user_id = $user_id
AND folder_id <> " . PRIVMSGS_NO_BOX . '
GROUP BY folder_id';
$result = $db->sql_query($sql);
$num_messages = $num_unread = array();
// start mod view or mark unread posts
// edited the block below to keep track of $total_board_unreads
$total_board_unreads = 0;
while ($row = $db->sql_fetchrow($result))
{
$num_messages[(int) $row['folder_id']] = $row['num_messages'];
$num_unread[(int) $row['folder_id']] = $row['num_unread'];
$total_board_unreads += $row['num_unread'];
}
// end mod view or mark unread posts
$db->sql_freeresult($result);
// Make sure the default boxes are defined
$available_folder = array(PRIVMSGS_INBOX, PRIVMSGS_OUTBOX, PRIVMSGS_SENTBOX);
foreach ($available_folder as $default_folder)
{
if (!isset($num_messages[$default_folder]))
{
$num_messages[$default_folder] = 0;
}
if (!isset($num_unread[$default_folder]))
{
$num_unread[$default_folder] = 0;
}
}
// Adjust unread status for outbox
$num_unread[PRIVMSGS_OUTBOX] = $num_messages[PRIVMSGS_OUTBOX];
$folder[PRIVMSGS_INBOX] = array(
'folder_name' => $user->lang['PM_INBOX'],
'num_messages' => $num_messages[PRIVMSGS_INBOX],
'unread_messages' => $num_unread[PRIVMSGS_INBOX]
);
// Custom Folder
$sql = 'SELECT folder_id, folder_name, pm_count
FROM ' . PRIVMSGS_FOLDER_TABLE . "
WHERE user_id = $user_id";
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$folder[$row['folder_id']] = array(
'folder_name' => $row['folder_name'],
'num_messages' => $row['pm_count'],
'unread_messages' => ((isset($num_unread[$row['folder_id']])) ? $num_unread[$row['folder_id']] : 0)
);
}
$db->sql_freeresult($result);
$folder[PRIVMSGS_OUTBOX] = array(
'folder_name' => $user->lang['PM_OUTBOX'],
'num_messages' => $num_messages[PRIVMSGS_OUTBOX],
'unread_messages' => $num_unread[PRIVMSGS_OUTBOX]
);
$folder[PRIVMSGS_SENTBOX] = array(
'folder_name' => $user->lang['PM_SENTBOX'],
'num_messages' => $num_messages[PRIVMSGS_SENTBOX],
'unread_messages' => $num_unread[PRIVMSGS_SENTBOX]
);
// start mod view or mark unread posts
$folder[PRIVMSGS_UNREADBOX] = array(
'folder_name' => $user->lang['UNREAD_MESSAGES'],
'num_messages' => $total_board_unreads,
'unread_messages' => $total_board_unreads
);
//end mod view or mark unread posts
// Define Folder Array for template designers (and for making custom folders usable by the template too)
foreach ($folder as $f_id => $folder_ary)
{
// start mod view or mark unread posts (and end mod too)...added reference to unreads in next line
$folder_id_name = ($f_id == PRIVMSGS_INBOX) ? 'inbox' : (($f_id == PRIVMSGS_OUTBOX) ? 'outbox' : (($f_id == PRIVMSGS_UNREADBOX) ? 'unreadbox' : 'sentbox'));
$template->assign_block_vars('folder', array(
'FOLDER_ID' => $f_id,
'FOLDER_NAME' => $folder_ary['folder_name'],
'NUM_MESSAGES' => $folder_ary['num_messages'],
'UNREAD_MESSAGES' => $folder_ary['unread_messages'],
'U_FOLDER' => ($f_id > 0) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=' . $f_id) : append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=' . $folder_id_name),
'S_CUR_FOLDER' => ($f_id === $folder_id) ? true : false,
'S_UNREAD_MESSAGES' => ($folder_ary['unread_messages']) ? true : false,
'S_CUSTOM_FOLDER' => ($f_id > 0) ? true : false)
);
}
if ($folder_id !== false && $folder_id !== PRIVMSGS_HOLD_BOX && !isset($folder[$folder_id]))
{
trigger_error('UNKNOWN_FOLDER');
}
return $folder;
}
/**
* Delete Messages From Sentbox
* we are doing this here because this saves us a bunch of checks and queries
*/
function clean_sentbox($num_sentbox_messages)
{
global $db, $user, $config;
// wesnoth mod begin
//
// Wesnoth PM limits override mod
//
// Check if user is Jetrel
$sql = 'SELECT username
FROM ' . USERS_TABLE . '
WHERE user_id=' . $user->data['user_id'] . '
AND username='."'Jetrel'";
$result = $db->sql_query($sql);
$is_Jetrel = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
// If user is Jetrel set the limit skyhigh, otherwise check the regular
// message limit
$message_limit = ($is_Jetrel) ? WESNOTH_JETREL_PM_LIMIT : $user->data['message_limit'];
// Replace $user->data['message_limit'] calls with $message_limit
// wesnoth mod end
if($message_limit && $num_sentbox_messages > $message_limit) // wesnoth mod
{
// Delete old messages
$sql = 'SELECT t.msg_id
FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p
WHERE t.msg_id = p.msg_id
AND t.user_id = ' . $user->data['user_id'] . '
AND t.folder_id = ' . PRIVMSGS_SENTBOX . '
ORDER BY p.message_time ASC';
// wesnoth mod begin
//$result = $db->sql_query_limit($sql, ($num_sentbox_messages - $user->data['message_limit']));
$result = $db->sql_query_limit($sql, ($num_sentbox_messages - $message_limit)); // wesnoth mod
// wesnoth mod end
$delete_ids = array();
while ($row = $db->sql_fetchrow($result))
{
$delete_ids[] = $row['msg_id'];
}
$db->sql_freeresult($result);
delete_pm($user->data['user_id'], $delete_ids, PRIVMSGS_SENTBOX);
}
}
/**
* Check Rule against Message Information
*/
function check_rule(&$rules, &$rule_row, &$message_row, $user_id)
{
global $user, $config;
if (!isset($rules[$rule_row['rule_check']][$rule_row['rule_connection']]))
{
return false;
}
$check_ary = $rules[$rule_row['rule_check']][$rule_row['rule_connection']];
// Replace Check Literals
$evaluate = $check_ary['function'];
$evaluate = preg_replace('/{(CHECK[0-9])}/', '$message_row[$check_ary[strtolower("\1")]]', $evaluate);
// Replace Rule Literals
$evaluate = preg_replace('/{(STRING|USER_ID|GROUP_ID)}/', '$rule_row["rule_" . strtolower("\1")]', $evaluate);
// Evil Statement
$result = false;
eval('$result = (' . $evaluate . ') ? true : false;');
if (!$result)
{
return false;
}
switch ($rule_row['rule_action'])
{
case ACTION_PLACE_INTO_FOLDER:
return array('action' => $rule_row['rule_action'], 'folder_id' => $rule_row['rule_folder_id']);
break;
case ACTION_MARK_AS_READ:
case ACTION_MARK_AS_IMPORTANT:
return array('action' => $rule_row['rule_action'], 'pm_unread' => $message_row['pm_unread'], 'pm_marked' => $message_row['pm_marked']);
break;
case ACTION_DELETE_MESSAGE:
global $db, $auth;
// Check for admins/mods - users are not allowed to remove those messages...
// We do the check here to make sure the data we use is consistent
$sql = 'SELECT user_id, user_type, user_permissions
FROM ' . USERS_TABLE . '
WHERE user_id = ' . (int) $message_row['author_id'];
$result = $db->sql_query($sql);
$userdata = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$auth2 = new auth();
$auth2->acl($userdata);
if (!$auth2->acl_get('a_') && !$auth2->acl_get('m_') && !$auth2->acl_getf_global('m_'))
{
return array('action' => $rule_row['rule_action'], 'pm_unread' => $message_row['pm_unread'], 'pm_marked' => $message_row['pm_marked']);
}
return false;
break;
default:
return false;
}
return false;
}
/**
* Update user PM count
*/
function update_pm_counts()
{
global $user, $db;
// Update unread count
$sql = 'SELECT COUNT(msg_id) as num_messages
FROM ' . PRIVMSGS_TO_TABLE . '
WHERE pm_unread = 1
AND folder_id <> ' . PRIVMSGS_OUTBOX . '
AND user_id = ' . $user->data['user_id'];
$result = $db->sql_query($sql);
$user->data['user_unread_privmsg'] = (int) $db->sql_fetchfield('num_messages');
$db->sql_freeresult($result);
// Update new pm count
$sql = 'SELECT COUNT(msg_id) as num_messages
FROM ' . PRIVMSGS_TO_TABLE . '
WHERE pm_new = 1
AND folder_id IN (' . PRIVMSGS_NO_BOX . ', ' . PRIVMSGS_HOLD_BOX . ')
AND user_id = ' . $user->data['user_id'];
$result = $db->sql_query($sql);
$user->data['user_new_privmsg'] = (int) $db->sql_fetchfield('num_messages');
$db->sql_freeresult($result);
$db->sql_query('UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
'user_unread_privmsg' => (int) $user->data['user_unread_privmsg'],
'user_new_privmsg' => (int) $user->data['user_new_privmsg'],
)) . ' WHERE user_id = ' . $user->data['user_id']);
// Ok, here we need to repair something, other boxes than privmsgs_no_box and privmsgs_hold_box should not carry the pm_new flag.
if (!$user->data['user_new_privmsg'])
{
$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
SET pm_new = 0
WHERE pm_new = 1
AND folder_id NOT IN (' . PRIVMSGS_NO_BOX . ', ' . PRIVMSGS_HOLD_BOX . ')
AND user_id = ' . $user->data['user_id'];
$db->sql_query($sql);
}
}
/**
* Place new messages into appropriate folder
*/
function place_pm_into_folder(&$global_privmsgs_rules, $release = false)
{
global $db, $user, $config;
if (!$user->data['user_new_privmsg'])
{
return array('not_moved' => 0, 'removed' => 0);
}
$user_message_rules = (int) $user->data['user_message_rules'];
$user_id = (int) $user->data['user_id'];
$action_ary = $move_into_folder = array();
$num_removed = 0;
// Newly processing on-hold messages
if ($release)
{
$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
SET folder_id = ' . PRIVMSGS_NO_BOX . '
WHERE folder_id = ' . PRIVMSGS_HOLD_BOX . "
AND user_id = $user_id";
$db->sql_query($sql);
}
// Get those messages not yet placed into any box
$retrieve_sql = 'SELECT t.*, p.*, u.username, u.user_id, u.group_id
FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p, ' . USERS_TABLE . " u
WHERE t.user_id = $user_id
AND p.author_id = u.user_id
AND t.folder_id = " . PRIVMSGS_NO_BOX . '
AND t.msg_id = p.msg_id';
// Just place into the appropriate arrays if no rules need to be checked
if (!$user_message_rules)
{
$result = $db->sql_query($retrieve_sql);
while ($row = $db->sql_fetchrow($result))
{
$action_ary[$row['msg_id']][] = array('action' => false);
}
$db->sql_freeresult($result);
}
else
{
$user_rules = $zebra = $check_rows = array();
$user_ids = $memberships = array();
// First of all, grab all rules and retrieve friends/foes
$sql = 'SELECT *
FROM ' . PRIVMSGS_RULES_TABLE . "
WHERE user_id = $user_id";
$result = $db->sql_query($sql);
$user_rules = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);
if (sizeof($user_rules))
{
$sql = 'SELECT zebra_id, friend, foe
FROM ' . ZEBRA_TABLE . "
WHERE user_id = $user_id";
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$zebra[$row['zebra_id']] = $row;
}
$db->sql_freeresult($result);
}
// Now build a bare-bone check_row array
$result = $db->sql_query($retrieve_sql);
while ($row = $db->sql_fetchrow($result))
{
$check_rows[] = array_merge($row, array(
'to' => explode(':', $row['to_address']),
'bcc' => explode(':', $row['bcc_address']),
'friend' => (isset($zebra[$row['author_id']])) ? $zebra[$row['author_id']]['friend'] : 0,
'foe' => (isset($zebra[$row['author_id']])) ? $zebra[$row['author_id']]['foe'] : 0,
'user_in_group' => array($user->data['group_id']),
'author_in_group' => array())
);
$user_ids[] = $row['user_id'];
}
$db->sql_freeresult($result);
// Retrieve user memberships
if (sizeof($user_ids))
{
$sql = 'SELECT *
FROM ' . USER_GROUP_TABLE . '
WHERE ' . $db->sql_in_set('user_id', $user_ids) . '
AND user_pending = 0';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$memberships[$row['user_id']][] = $row['group_id'];
}
$db->sql_freeresult($result);
}
// Now place into the appropriate folder
foreach ($check_rows as $row)
{
// Add membership if set
if (isset($memberships[$row['author_id']]))
{
$row['author_in_group'] = $memberships[$row['user_id']];
}
// Check Rule - this should be very quick since we have all information we need
$is_match = false;
foreach ($user_rules as $rule_row)
{
if (($action = check_rule($global_privmsgs_rules, $rule_row, $row, $user_id)) !== false)
{
$is_match = true;
$action_ary[$row['msg_id']][] = $action;
}
}
if (!$is_match)
{
$action_ary[$row['msg_id']][] = array('action' => false);
}
}
unset($user_rules, $zebra, $check_rows, $user_ids, $memberships);
}
// We place actions into arrays, to save queries.
$sql = $unread_ids = $delete_ids = $important_ids = array();
foreach ($action_ary as $msg_id => $msg_ary)
{
// It is allowed to execute actions more than once, except placing messages into folder
$folder_action = $message_removed = false;
foreach ($msg_ary as $pos => $rule_ary)
{
if ($folder_action && $rule_ary['action'] == ACTION_PLACE_INTO_FOLDER)
{
continue;
}
switch ($rule_ary['action'])
{
case ACTION_PLACE_INTO_FOLDER:
// Folder actions have precedence, so we will remove any other ones
$folder_action = true;
$move_into_folder[(int) $rule_ary['folder_id']][] = $msg_id;
break;
case ACTION_MARK_AS_READ:
if ($rule_ary['pm_unread'])
{
$unread_ids[] = $msg_id;
}
break;
case ACTION_DELETE_MESSAGE:
$delete_ids[] = $msg_id;
$message_removed = true;
break;
case ACTION_MARK_AS_IMPORTANT:
if (!$rule_ary['pm_marked'])
{
$important_ids[] = $msg_id;
}
break;
}
}
// We place this here because it could happen that the messages are doubled if a rule marks a message and then moves it into a specific
// folder. Here we simply move the message into the INBOX if it gets not removed and also not put into a custom folder.
if (!$folder_action && !$message_removed)
{
$move_into_folder[PRIVMSGS_INBOX][] = $msg_id;
}
}
// Do not change the order of processing
// The number of queries needed to be executed here highly depends on the defined rules and are
// only gone through if new messages arrive.
// Delete messages
if (sizeof($delete_ids))
{
$num_removed += sizeof($delete_ids);
delete_pm($user_id, $delete_ids, PRIVMSGS_NO_BOX);
}
// Set messages to Unread
if (sizeof($unread_ids))
{
$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
SET pm_unread = 0
WHERE ' . $db->sql_in_set('msg_id', $unread_ids) . "
AND user_id = $user_id
AND folder_id = " . PRIVMSGS_NO_BOX;
$db->sql_query($sql);
}
// mark messages as important
if (sizeof($important_ids))
{
$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
SET pm_marked = 1 - pm_marked
WHERE folder_id = ' . PRIVMSGS_NO_BOX . "
AND user_id = $user_id
AND " . $db->sql_in_set('msg_id', $important_ids);
$db->sql_query($sql);
}
// Move into folder
$folder = array();
if (sizeof($move_into_folder))
{
// Determine Full Folder Action - we need the move to folder id later eventually
$full_folder_action = ($user->data['user_full_folder'] == FULL_FOLDER_NONE) ? ($config['full_folder_action'] - (FULL_FOLDER_NONE*(-1))) : $user->data['user_full_folder'];
$sql_folder = array_keys($move_into_folder);
if ($full_folder_action >= 0)
{
$sql_folder[] = $full_folder_action;
}
$sql = 'SELECT folder_id, pm_count
FROM ' . PRIVMSGS_FOLDER_TABLE . '
WHERE ' . $db->sql_in_set('folder_id', $sql_folder) . "
AND user_id = $user_id";
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$folder[(int) $row['folder_id']] = (int) $row['pm_count'];
}
$db->sql_freeresult($result);
unset($sql_folder);
if (isset($move_into_folder[PRIVMSGS_INBOX]))
{
$sql = 'SELECT COUNT(msg_id) as num_messages
FROM ' . PRIVMSGS_TO_TABLE . "
WHERE user_id = $user_id
AND folder_id = " . PRIVMSGS_INBOX;
$result = $db->sql_query($sql);
$folder[PRIVMSGS_INBOX] = (int) $db->sql_fetchfield('num_messages');
$db->sql_freeresult($result);
}
}
// wesnoth mod begin
//
// Wesnoth PM limits override mod
//
$sql = 'SELECT username
FROM ' . USERS_TABLE . '
WHERE user_id=' . $user->data['user_id'] . '
AND username='."'Jetrel'";
$result = $db->sql_query($sql);
$is_Jetrel = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
// If user is Jetrel set the limit skyhigh
$message_limit = ($is_Jetrel) ? WESNOTH_JETREL_PM_LIMIT : $user->data['message_limit'];
//Replace $user->data['message_limit'] calls with $message_limit
// wesnoth mod end
// Here we have ideally only one folder to move into
foreach ($move_into_folder as $folder_id => $msg_ary)
{
$dest_folder = $folder_id;
$full_folder_action = FULL_FOLDER_NONE;
// Check Message Limit - we calculate with the complete array, most of the time it is one message
// But we are making sure that the other way around works too (more messages in queue than allowed to be stored)
// if ($user->data['message_limit'] && $folder[$folder_id] && ($folder[$folder_id] + sizeof($msg_ary)) > $user->data['message_limit'])
if ($message_limit && $folder[$folder_id] && ($folder[$folder_id] + sizeof($msg_ary)) > $message_limit) // wesnoth mod
{
$full_folder_action = ($user->data['user_full_folder'] == FULL_FOLDER_NONE) ? ($config['full_folder_action'] - (FULL_FOLDER_NONE*(-1))) : $user->data['user_full_folder'];
// If destination folder itself is full...
// if ($full_folder_action >= 0 && ($folder[$full_folder_action] + sizeof($msg_ary)) > $user->data['message_limit'])
if ($full_folder_action >= 0 && ($folder[$full_folder_action] + sizeof($msg_ary)) > $message_limit) // wesnoth mod
{
$full_folder_action = $config['full_folder_action'] - (FULL_FOLDER_NONE*(-1));
}
// If Full Folder Action is to move to another folder, we simply adjust the destination folder
if ($full_folder_action >= 0)
{
$dest_folder = $full_folder_action;
}
else if ($full_folder_action == FULL_FOLDER_DELETE)
{
// Delete some messages. NOTE: Ordered by msg_id here instead of message_time!
$sql = 'SELECT msg_id
FROM ' . PRIVMSGS_TO_TABLE . "
WHERE user_id = $user_id
AND folder_id = $dest_folder
ORDER BY msg_id ASC";
// $result = $db->sql_query_limit($sql, (($folder[$dest_folder] + sizeof($msg_ary)) - $user->data['message_limit']));
$result = $db->sql_query_limit($sql, (($folder[$dest_folder] + sizeof($msg_ary)) - $message_limit));
$delete_ids = array();
while ($row = $db->sql_fetchrow($result))
{
$delete_ids[] = $row['msg_id'];
}
$db->sql_freeresult($result);
$num_removed += sizeof($delete_ids);
delete_pm($user_id, $delete_ids, $dest_folder);
}
}
//
if ($full_folder_action == FULL_FOLDER_HOLD)
{
$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
SET folder_id = ' . PRIVMSGS_HOLD_BOX . '
WHERE folder_id = ' . PRIVMSGS_NO_BOX . "
AND user_id = $user_id
AND " . $db->sql_in_set('msg_id', $msg_ary);
$db->sql_query($sql);
}
else
{
$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
SET folder_id = $dest_folder, pm_new = 0
WHERE folder_id = " . PRIVMSGS_NO_BOX . "
AND user_id = $user_id
AND pm_new = 1
AND " . $db->sql_in_set('msg_id', $msg_ary);
$db->sql_query($sql);
if ($dest_folder != PRIVMSGS_INBOX)
{
$sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . '
SET pm_count = pm_count + ' . (int) $db->sql_affectedrows() . "
WHERE folder_id = $dest_folder
AND user_id = $user_id";
$db->sql_query($sql);
}
}
}
if (sizeof($action_ary))
{
// Move from OUTBOX to SENTBOX
// We are not checking any full folder status here... SENTBOX is a special treatment (old messages get deleted)
$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
SET folder_id = ' . PRIVMSGS_SENTBOX . '
WHERE folder_id = ' . PRIVMSGS_OUTBOX . '
AND ' . $db->sql_in_set('msg_id', array_keys($action_ary));
$db->sql_query($sql);
}
// Update new/unread count
update_pm_counts();
// Now check how many messages got not moved...
$sql = 'SELECT COUNT(msg_id) as num_messages
FROM ' . PRIVMSGS_TO_TABLE . "
WHERE user_id = $user_id
AND folder_id = " . PRIVMSGS_HOLD_BOX;
$result = $db->sql_query($sql);
$num_not_moved = (int) $db->sql_fetchfield('num_messages');
$db->sql_freeresult($result);
return array('not_moved' => $num_not_moved, 'removed' => $num_removed);
}
/**
* Move PM from one to another folder
*/
function move_pm($user_id, $message_limit, $move_msg_ids, $dest_folder, $cur_folder_id)
{
global $db, $user;
global $phpbb_root_path, $phpEx;
$num_moved = 0;
if (!is_array($move_msg_ids))
{
$move_msg_ids = array($move_msg_ids);
}
if (sizeof($move_msg_ids) && !in_array($dest_folder, array(PRIVMSGS_NO_BOX, PRIVMSGS_OUTBOX, PRIVMSGS_SENTBOX)) &&
!in_array($cur_folder_id, array(PRIVMSGS_NO_BOX, PRIVMSGS_OUTBOX)) && $cur_folder_id != $dest_folder)
{
// We have to check the destination folder ;)
if ($dest_folder != PRIVMSGS_INBOX)
{
$sql = 'SELECT folder_id, folder_name, pm_count
FROM ' . PRIVMSGS_FOLDER_TABLE . "
WHERE folder_id = $dest_folder
AND user_id = $user_id";
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
if (!$row)
{
trigger_error('NOT_AUTHORISED');
}
if ($message_limit && $row['pm_count'] + sizeof($move_msg_ids) > $message_limit)
{
$message = sprintf($user->lang['NOT_ENOUGH_SPACE_FOLDER'], $row['folder_name']) . '<br /><br />';
$message .= sprintf($user->lang['CLICK_RETURN_FOLDER'], '<a href="' . append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=' . $row['folder_id']) . '">', '</a>', $row['folder_name']);
trigger_error($message);
}
}
else
{
$sql = 'SELECT COUNT(msg_id) as num_messages
FROM ' . PRIVMSGS_TO_TABLE . '
WHERE folder_id = ' . PRIVMSGS_INBOX . "
AND user_id = $user_id";
$result = $db->sql_query($sql);
$num_messages = (int) $db->sql_fetchfield('num_messages');
$db->sql_freeresult($result);
if ($message_limit && $num_messages + sizeof($move_msg_ids) > $message_limit)
{
$message = sprintf($user->lang['NOT_ENOUGH_SPACE_FOLDER'], $user->lang['PM_INBOX']) . '<br /><br />';
$message .= sprintf($user->lang['CLICK_RETURN_FOLDER'], '<a href="' . append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=inbox') . '">', '</a>', $user->lang['PM_INBOX']);
trigger_error($message);
}
}
$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
SET folder_id = $dest_folder
WHERE folder_id = $cur_folder_id
AND user_id = $user_id
AND " . $db->sql_in_set('msg_id', $move_msg_ids);
$db->sql_query($sql);
$num_moved = $db->sql_affectedrows();
// Update pm counts
if ($num_moved)
{
if (!in_array($cur_folder_id, array(PRIVMSGS_INBOX, PRIVMSGS_OUTBOX, PRIVMSGS_SENTBOX)))
{
$sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . "
SET pm_count = pm_count - $num_moved
WHERE folder_id = $cur_folder_id
AND user_id = $user_id";
$db->sql_query($sql);
}
if ($dest_folder != PRIVMSGS_INBOX)
{
$sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . "
SET pm_count = pm_count + $num_moved
WHERE folder_id = $dest_folder
AND user_id = $user_id";
$db->sql_query($sql);
}
}
}
else if (in_array($cur_folder_id, array(PRIVMSGS_NO_BOX, PRIVMSGS_OUTBOX)))
{
trigger_error('CANNOT_MOVE_SPECIAL');
}
return $num_moved;
}
/**
* Update unread message status
*/
function update_unread_status($unread, $msg_id, $user_id, $folder_id)
{
if (!$unread)
{
return;
}
global $db, $user;
$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
SET pm_unread = 0
WHERE msg_id = $msg_id
AND user_id = $user_id
AND folder_id = $folder_id";
$db->sql_query($sql);
$sql = 'UPDATE ' . USERS_TABLE . "
SET user_unread_privmsg = user_unread_privmsg - 1
WHERE user_id = $user_id";
$db->sql_query($sql);
if ($user->data['user_id'] == $user_id)
{
$user->data['user_unread_privmsg']--;
// Try to cope with previous wrong conversions...
if ($user->data['user_unread_privmsg'] < 0)
{
$sql = 'UPDATE ' . USERS_TABLE . "
SET user_unread_privmsg = 0
WHERE user_id = $user_id";
$db->sql_query($sql);
$user->data['user_unread_privmsg'] = 0;
}
}
}
/**
* Handle all actions possible with marked messages
*/
function handle_mark_actions($user_id, $mark_action)
{
global $db, $user, $phpbb_root_path, $phpEx;
$msg_ids = request_var('marked_msg_id', array(0));
$cur_folder_id = request_var('cur_folder_id', PRIVMSGS_NO_BOX);
$confirm = (isset($_POST['confirm'])) ? true : false;
if (!sizeof($msg_ids))
{
return false;
}
// start mod view or mark unread posts (and end mod too)...in next line,
// we define a variable that will allow us to ignore $cur_folder_id constraint
// in queries when we are talking about unreadbox (since that's not a real folder);
// then, we use that variable rather than 'folder_id = $cur_folder_id' in the first three cases in the switch below.
$cur_folder_sql = ($cur_folder_id != PRIVMSGS_UNREADBOX) ? "folder_id = $cur_folder_id AND" : '';
switch ($mark_action)
{
// start mod view or mark unread posts
case 'mark_marked_read':
$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
SET pm_unread = 0
WHERE $cur_folder_sql
user_id = $user_id
AND " . $db->sql_in_set('msg_id', $msg_ids);
$db->sql_query($sql);
update_pm_counts();
break;
case 'mark_marked_unread':
$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
SET pm_unread = 1
WHERE $cur_folder_sql
user_id = $user_id
AND " . $db->sql_in_set('msg_id', $msg_ids);
$db->sql_query($sql);
update_pm_counts();
break;
// end mod view or mark unread posts
case 'mark_important':
$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
SET pm_marked = 1 - pm_marked
WHERE $cur_folder_sql
user_id = $user_id
AND " . $db->sql_in_set('msg_id', $msg_ids);
$db->sql_query($sql);
break;
case 'delete_marked':
global $auth;
if (!$auth->acl_get('u_pm_delete'))
{
trigger_error('NO_AUTH_DELETE_MESSAGE');
}
if (confirm_box(true))
{
delete_pm($user_id, $msg_ids, $cur_folder_id);
$success_msg = (sizeof($msg_ids) == 1) ? 'MESSAGE_DELETED' : 'MESSAGES_DELETED';
$redirect = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=' . $cur_folder_id);
meta_refresh(3, $redirect);
trigger_error($user->lang[$success_msg] . '<br /><br />' . sprintf($user->lang['RETURN_FOLDER'], '<a href="' . $redirect . '">', '</a>'));
}
else
{