-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprivatemsg.module
executable file
·3570 lines (3296 loc) · 112 KB
/
privatemsg.module
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
/**
* @file
* Allows users to send private messages to other users.
*/
/**
* Status constant for read messages.
*/
define('PRIVATEMSG_READ', 0);
/**
* Status constant for unread messages.
*/
define('PRIVATEMSG_UNREAD', 1);
/**
* Show unlimited messages in a thread.
*/
define('PRIVATEMSG_UNLIMITED', 'unlimited');
/**
* Implements hook_permission().
*/
function privatemsg_permission() {
$items = array(
'administer privatemsg settings' => array(
'title' => t('Administer privatemsg'),
'description' => t('Perform maintenance tasks for privatemsg'),
),
'read privatemsg' => array(
'title' => t('Read private messages'),
'description' => t('Read private messages'),
),
'read all private messages' => array(
'title' => t('Read all private messages'),
'description' => t('Includes messages of other users'),
),
'write privatemsg' => array(
'title' => t('Write new private messages'),
'description' => t('Write new private messages'),
),
'delete privatemsg' => array(
'title' => t('Delete private messages'),
'description' => t('Allows users to delete messages they can read.'),
),
'allow disabling privatemsg' => array(
'title' => t('Allow disabling private messages'),
'description' => t("Allows user to disable privatemsg so that they can't receive or send any private messages.")
),
'reply only privatemsg' => array(
'title' => t('Reply to private messages'),
'description' => t('Allows to reply to private messages but not send new ones. Note that the write new private messages permission includes replies.')
),
'use tokens in privatemsg' => array(
'title' => t('Use tokens in private messages'),
'description' => t("Allows user to use available tokens when sending private messages.")
),
'select text format for privatemsg' => array(
'title' => t('Select text format for private messages'),
'description' => t('Allows to choose the text format when sending private messages. Otherwise, the default is used.'),
),
);
if (module_exists('akismet')) {
$items['report private messages to akismet'] = array(
'title' => t('Report private messages to Akismet'),
'description' => t('Allows users to report messages as spam or unwanted content while deleting messages, if Akismet is set up to check private messages.'),
);
}
return $items;
}
/**
* Implements hook_config_info().
*/
function privatemsg_config_info() {
$prefixes['privatemsg.settings'] = array(
'label' => t('Private message settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_autoload_info().
*/
function privatemsg_autoload_info() {
return array(
'PrivatemsgMessage' => 'privatemsg.entity.inc',
'PrivatemsgMessageController' => 'privatemsg.controller.inc',
'views_handler_field_privatemsg_link' => 'views/views_handler_field_privatemsg_link.inc',
);
}
/**
* Generate array of user objects based on a string.
*
*
* @param string $string
* A string with user id, for example 1,2,4. Returned by the list query.
* @param int|null $slice
*
* @return array
* Array with user objects.
*/
function _privatemsg_generate_user_array($string, $slice = NULL) {
// Convert user uid list (uid1,uid2,uid3) into an array. If $slice is not NULL
// pass that as argument to array_slice(). For example, -4 will only load the
// last four users.
// This is done to avoid loading user objects that are not displayed, for
// obvious performance reasons.
$users = array();
if ($string) {
$users = explode(',', $string);
}
if (!is_null($slice)) {
$users = array_slice($users, $slice);
}
$participants = array();
foreach ($users as $uid) {
// If it is an integer, it is a user id.
if ((int) $uid > 0) {
$user_ids = privatemsg_user_load_multiple(array($uid));
if ($account = array_shift($user_ids)) {
$participants[privatemsg_recipient_key($account)] = $account;
}
}
elseif (strpos($uid, '_') !== FALSE) {
list($type, $id) = explode('_', $uid);
$type_info = privatemsg_recipient_get_type($type);
if ($type_info && isset($type_info['load']) && is_callable($type_info['load'])) {
$temp_load = $type_info['load'](array($id), $type);
if ($participant = array_shift($temp_load)) {
$participants[privatemsg_recipient_key($participant)] = $participant;
}
}
}
}
return $participants;
}
/**
* Format an array of user objects.
*
* @param array $part_array
* Array with user objects, for example the one returned by
* _privatemsg_generate_user_array.
* @param int|null $limit
* Limit the number of user objects which should be displayed.
* @param bool $no_text
* When TRUE, don't display the Participants/From text.
* @return string
* String with formatted user objects, like user1, user2.
*/
function _privatemsg_format_participants($part_array, $limit = NULL, $no_text = FALSE) {
global $user;
if (count($part_array) > 0) {
$to = array();
$limited = FALSE;
foreach ($part_array as $account) {
// Directly address the current user.
if (isset($account->type) && in_array($account->type, array(
'hidden',
'user'
)) && $account->recipient == $user->uid
) {
array_unshift($to, $no_text ? t('You', array(), array('context' => 'Dative')) : t('you', array(), array('context' => 'Dative')));
continue;
}
// Don't display recipients with type hidden.
if (isset($account->type) && $account->type == 'hidden') {
continue;
}
if (is_int($limit) && count($to) >= $limit) {
$limited = TRUE;
break;
}
$to[] = privatemsg_recipient_format($account);
}
$limit_string = '';
if ($limited) {
$limit_string = t(' and others');
}
if ($no_text) {
return implode(', ', $to) . $limit_string;
}
$last = array_pop($to);
if (count($to) == 0) { // Only one participant
return t("From !last", array('!last' => $last));
}
else { // Multiple participants..
$participants = implode(', ', $to);
return t('Between !participants and !last', array(
'!participants' => $participants,
'!last' => $last
));
}
}
return '';
}
/**
* Implements hook_menu().
*/
function privatemsg_menu() {
$items['messages'] = array(
'title' => 'Messages',
'title callback' => 'privatemsg_title_callback',
'page callback' => 'privatemsg_list_page',
'page arguments' => array('list'),
'file' => 'privatemsg.pages.inc',
'access callback' => 'privatemsg_user_access',
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'user-menu',
);
$items['messages/list'] = array(
'title' => 'Messages',
'page callback' => 'privatemsg_list_page',
'page arguments' => array('list'),
'file' => 'privatemsg.pages.inc',
'access callback' => 'privatemsg_user_access',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
'menu_name' => 'user-menu',
);
$items['messages/view/%privatemsg_thread'] = array(
// Set the third argument to TRUE so that we can show access denied instead
// of not found.
'load arguments' => array(NULL, NULL, TRUE),
'title' => 'Read message',
'page callback' => 'privatemsg_view',
'page arguments' => array(2),
'file' => 'privatemsg.pages.inc',
'access callback' => 'privatemsg_view_access',
'access arguments' => array(2),
'type' => MENU_LOCAL_TASK,
'weight' => -5,
'menu_name' => 'user-menu',
);
$items['messages/delete/%privatemsg_thread/%privatemsg_message'] = array(
'title' => 'Delete message',
'page callback' => 'backdrop_get_form',
'page arguments' => array('privatemsg_delete', 2, 3),
'file' => 'privatemsg.pages.inc',
'access callback' => 'privatemsg_user_access',
'access arguments' => array('delete privatemsg'),
'type' => MENU_CALLBACK,
'weight' => -10,
'menu_name' => 'user-menu',
);
$items['messages/new'] = array(
'title' => 'Write new message',
'page callback' => 'backdrop_get_form',
'page arguments' => array('privatemsg_new', 2, 3, NULL),
'file' => 'privatemsg.pages.inc',
'access callback' => 'privatemsg_user_access',
'access arguments' => array('write privatemsg'),
'type' => MENU_LOCAL_ACTION,
'weight' => -3,
'menu_name' => 'user-menu',
);
// Auto-completes available user names & removes duplicates.
$items['messages/autocomplete'] = array(
'page callback' => 'privatemsg_autocomplete',
'file' => 'privatemsg.pages.inc',
'access callback' => 'privatemsg_user_access',
'access arguments' => array('write privatemsg'),
'type' => MENU_CALLBACK,
);
$items['admin/config/messaging'] = array(
'title' => 'Messaging',
'description' => 'Messaging systems.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
'file path' => backdrop_get_path('module', 'system'),
);
$items['admin/config/messaging/privatemsg'] = array(
'title' => 'Private message settings',
'description' => 'Configure private messaging settings.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('privatemsg_admin_settings'),
'file' => 'privatemsg.admin.inc',
'access arguments' => array('administer privatemsg settings'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/messaging/privatemsg/settings'] = array(
'title' => 'Private message settings',
'description' => 'Configure private messaging settings.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('privatemsg_admin_settings'),
'file' => 'privatemsg.admin.inc',
'access arguments' => array('administer privatemsg settings'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['messages/undo/action'] = array(
'title' => 'Private messages',
'description' => 'Undo last thread action',
'page callback' => 'privatemsg_undo_action',
'file' => 'privatemsg.pages.inc',
'access arguments' => array('read privatemsg'),
'type' => MENU_CALLBACK,
'menu' => 'user-menu',
);
$items['user/%/messages'] = array(
'title' => 'Messages',
'page callback' => 'privatemsg_list_page',
'page arguments' => array('list', 1),
'file' => 'privatemsg.pages.inc',
'access callback' => 'privatemsg_user_access',
'access arguments' => array('read all private messages'),
'type' => MENU_LOCAL_TASK,
);
if (module_exists('devel_generate')) {
$items['admin/devel/generate/privatemsg'] = array(
'title' => 'Generate private messages',
'description' => 'Generate a given number of private messages. Optionally delete current private messages.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('privatemsg_devel_generate_form'),
'access arguments' => array('administer privatemsg settings'),
'type' => MENU_LOCAL_TASK,
'file' => 'privatemsg.devel_generate.inc',
'weight' => 5,
);
}
return $items;
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function privatemsg_menu_local_tasks_alter(&$data, $router_item, $root_path) {
// Add action link to 'messages/new' on 'messages' page.
$add_to_array = array('messages/list', 'messages/inbox', 'messages/sent');
foreach ($add_to_array as $add_to) {
if (strpos($root_path, $add_to) !== FALSE) {
$item = menu_get_item('messages/new');
if ($item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
break;
}
}
}
/**
* Privatemsg wrapper for user_access.
*
* Never allow anonymous user access as that doesn't makes sense.
*
* @param string $permission
* Permission string, defaults to read privatemsg
* @param User|null $account
*
* @return bool
* TRUE if user has access, FALSE if not
*
* @ingroup api
*/
function privatemsg_user_access($permission = 'read privatemsg', $account = NULL) {
static $disabled_displayed = FALSE;
if ($account === NULL) {
global $user;
$account = $user;
}
if (!$account->uid) { // Disallow anonymous access, regardless of permissions
return FALSE;
}
if (privatemsg_is_disabled($account) && ($permission == 'write privatemsg')) {
if (arg(0) == 'messages' && config_get('privatemsg.settings', 'display_disabled_message') && !$disabled_displayed) {
$disabled_displayed = TRUE;
backdrop_set_message(t('You have disabled Privatemsg and are not allowed to write messages. Go to your <a href="@settings_url">Account settings</a> to enable it again.', array('@settings_url' => url('user/' . $account->uid . '/edit'))), 'warning');
}
return FALSE;
}
if (!user_access($permission, $account)) {
return FALSE;
}
return TRUE;
}
/**
* Check access to the view messages page.
*
* Function to restrict the access of the view messages page to just the
* messages/view/% pages and not to leave tabs artifact on other lower
* level pages such as the messages/new/%.
*
* @param array $thread
* A array containing all information about a specific thread, generated by
* privatemsg_thread_load().
*
* @ingroup api
*
* @return bool
*/
function privatemsg_view_access($thread) {
// Do not allow access to threads without messages.
if (empty($thread['messages'])) {
// Count all messages, if there
return FALSE;
}
if (privatemsg_user_access('read privatemsg') && arg(1) == 'view') {
return TRUE;
}
return FALSE;
}
/**
* Checks the status of private messaging for provided user.
*
* @param User $account
* @return bool
* TRUE if user has disabled private messaging, FALSE otherwise
*/
function privatemsg_is_disabled($account) {
// Make sure account exists
if (!$account || !isset($account->uid) || !$account->uid) {
return FALSE;
}
// Make sure we have a fully loaded user object and try to load it if not.
if ((!empty($account->roles) || $account = user_load($account->uid)) && user_access('allow disabling privatemsg', $account)) {
$ids = privatemsg_get_default_setting_ids($account);
return (bool) privatemsg_get_setting('disabled', $ids);
}
return FALSE;
}
/**
* Load a thread with all the messages and participants.
*
* This function is called by the menu system through the %privatemsg_thread
* wildcard.
*
* @param int $thread_id
* Thread id, pmi.thread_id or pm.mid of the first message in that thread.
* @param User $account
* User object for which the thread should be loaded, defaults to
* the current user.
* @param string $start
* Message offset from the start of the thread.
* @param bool $useAccessDenied
* Set to TRUE if the function should forward to the access denied page
* instead of not found. This is used by the menu system because that does
* load arguments before access checks are made. Defaults to FALSE.
*
* @return object
* Thread object, with keys messages, participants, title and user. messages
* contains an array of messages, participants an array of user, subject the
* subject of the thread and user the user viewing the thread.
*
* If no messages are found, or the thread_id is invalid, the function returns
* FALSE.
* @ingroup api
*/
function privatemsg_thread_load($thread_id, $account = NULL, $start = NULL, $useAccessDenied = FALSE) {
$threads = &backdrop_static(__FUNCTION__, array());
$thread_id = (int) $thread_id;
$config = config('privatemsg.settings');
if ($thread_id > 0) {
$thread = array('thread_id' => $thread_id);
if (is_null($account)) {
global $user;
$account = clone $user;
}
if (!isset($threads[$account->uid])) {
$threads[$account->uid] = array();
}
if (!array_key_exists($thread_id, $threads[$account->uid])) {
// Load the list of participants.
$thread['participants'] = _privatemsg_load_thread_participants($thread_id, $account, FALSE, 'view');
$thread['read_all'] = FALSE;
if (empty($thread['participants']) && privatemsg_user_access('read all private messages', $account)) {
$thread['read_all'] = TRUE;
// Load all participants.
$thread['participants'] = _privatemsg_load_thread_participants($thread_id, FALSE, FALSE, 'view');
}
// Load messages returned by the messages query with privatemsg_message_load_multiple().
$query = _privatemsg_assemble_query('messages', array($thread_id), $thread['read_all'] ? NULL : $account);
// Use subquery to bypass group by since it is not possible to alter
// existing GROUP BY statements.
$countQuery = db_select($query);
$countQuery->addExpression('COUNT(*)');
$thread['message_count'] = $thread['to'] = $countQuery->execute()
->fetchField();
$thread['from'] = 1;
// Check if we need to limit the messages.
$max_amount = $config->get('view_max_amount');
// If there is no start value, select based on get params.
if (is_null($start)) {
if (isset($_GET['start']) && $_GET['start'] < $thread['message_count']) {
$start = $_GET['start'];
}
elseif (!$config->get('view_use_max_as_default') && $max_amount == PRIVATEMSG_UNLIMITED) {
$start = PRIVATEMSG_UNLIMITED;
}
else {
$start = $thread['message_count'] - ($config->get('view_use_max_as_default') ? $config->get('view_default_amount') : $max_amount);
}
}
if ($start != PRIVATEMSG_UNLIMITED) {
if ($max_amount == PRIVATEMSG_UNLIMITED) {
$last_page = 0;
$max_amount = $thread['message_count'];
}
else {
// Calculate the number of messages on the "last" page to avoid
// message overlap.
// Note - the last page lists the earliest messages, not the latest.
$paging_count = $config->get('view_use_max_as_default') ? $thread['message_count'] - $config->get('view_default_amount') : $thread['message_count'];
$last_page = $paging_count % $max_amount;
}
// Sanity check - we cannot start from a negative number.
if ($start < 0) {
$start = 0;
}
$thread['start'] = $start;
//If there are newer messages on the page, show pager link allowing to go to the newer messages.
if (($start + $max_amount + 1) < $thread['message_count']) {
$thread['to'] = $start + $max_amount;
$thread['newer_start'] = $start + $max_amount;
}
if ($start - $max_amount >= 0) {
$thread['older_start'] = $start - $max_amount;
}
elseif ($start > 0) {
$thread['older_start'] = 0;
}
// Do not show messages on the last page that would show on the page
// before. This will only work when using the visual pager.
if ($start < $last_page && $max_amount != PRIVATEMSG_UNLIMITED && $max_amount < $thread['message_count']) {
unset($thread['older_start']);
$thread['to'] = $thread['newer_start'] = $max_amount = $last_page;
// Start from the first message - this is a specific hack to make sure
// the message display has sane paging on the last page.
$start = 0;
}
// Visual counts start from 1 instead of zero, so plus one.
$thread['from'] = $start + 1;
$query->range($start, $max_amount);
}
$conditions = array();
if (!$thread['read_all']) {
$conditions['account'] = $account;
}
// If the $ids parameter is empty, privatemsg_message_load_multiple will
// load all threads.
// @see https://drupal.org/node/2033161
$ids = $query->execute()->fetchCol();
if (count($ids)) {
$thread['messages'] = privatemsg_message_load_multiple($ids, $conditions);
}
// If there are no messages, don't allow access to the thread.
if (empty($thread['messages'])) {
if ($useAccessDenied) {
// Generate new query with read all to see if the thread does exist.
$query = _privatemsg_assemble_query('messages', array($thread_id), NULL);
$exists = $query->countQuery()->execute()->fetchField();
if (!$exists) {
// Thread does not exist, display 404.
$thread = FALSE;
}
}
else {
$thread = FALSE;
}
}
else {
// General data, assume subject is the same for all messages of that thread.
$thread['user'] = $account;
$message = current($thread['messages']);
$thread['subject'] = $thread['subject-tokenized'] = $message->subject;
if ($message->has_tokens) {
$thread['subject-tokenized'] = privatemsg_token_replace($thread['subject'], array('privatemsg_message' => $message), array(
'sanitize' => TRUE,
'privatemsg-show-span' => FALSE
));
}
}
$threads[$account->uid][$thread_id] = $thread;
}
return $threads[$account->uid][$thread_id];
}
return FALSE;
}
/**
* Implements hook_privatemsg_view_template().
*
* Allows modules to define different message view template.
*
* This hook returns information about available themes for privatemsg viewing.
*
* array(
* 'machine_template_name' => 'Human readable template name',
* 'machine_template_name_2' => 'Human readable template name 2'
* };
*/
function privatemsg_privatemsg_view_template() {
return array(
'privatemsg-view' => 'Default view',
);
}
/**
* Implements hook_cron().
*
* If the flush feature is enabled, a given amount of deleted messages that are
* old enough are flushed.
*/
function privatemsg_cron() {
$config = config('privatemsg.settings');
if ($config->get('flush_enabled')) {
$query = _privatemsg_assemble_query('deleted', $config->get('flush_days'), $config->get('flush_max'));
foreach ($query->execute()->fetchCol() as $mid) {
$message = privatemsg_message_load($mid);
module_invoke_all('privatemsg_message_flush', $message);
// Delete recipients of the message.
db_delete('pm_index')
->condition('mid', $mid)
->execute();
// Delete message itself.
db_delete('pm_message')
->condition('mid', $mid)
->execute();
}
}
// Number of user ids to process for this cron run.
$total_remaining = $config->get('cron_recipient_per_run');
$current_process = state_get('privatemsg_cron_recipient_process');
// Instead of doing the order by in the database, which can be slow, we load
// all results and the do the handling there. Additionally, explicitly specify
// the desired types. If there are more than a few dozen results the site is
// unhealthy anyway because this cron is unable to keep up with the
// unprocessed recipients.
$rows = array();
// Get all type keys except user.
$types = privatemsg_recipient_get_types();
unset($types['user']);
$types = array_keys($types);
// If there are no other recipient types, there is nothing to do.
if (empty($types)) {
return;
}
$result = db_query("SELECT pmi.recipient, pmi.type, pmi.mid FROM {pm_index} pmi WHERE pmi.type IN (:types) AND pmi.is_new = 1", array(':types' => $types));
foreach ($result as $row) {
// If this is equal to the row that is currently processed, add it first in
// the array.
if (!empty($current_process) && $current_process['mid'] == $row->mid && $current_process['type'] == $row->type && $current_process['recipient'] == $row->recipient) {
array_unshift($rows, $row);
}
else {
$rows[] = $row;
}
}
foreach ($rows as $row) {
$type = privatemsg_recipient_get_type($row->type);
if (isset($type['load']) && is_callable($type['load'])) {
$loaded = $type['load'](array($row->recipient), $row->type);
if (empty($loaded)) {
continue;
}
$recipient = reset($loaded);
}
// Check if we already started to process this recipient.
$offset = 0;
if (!empty($current_process) && $current_process['mid'] == $row->mid && $current_process['recipient'] == $row->recipient && $current_process['type'] == $row->type) {
$offset = $current_process['offset'];
}
$load_function = $type['generate recipients'];
$uids = $load_function($recipient, $total_remaining, $offset);
if (!empty($uids)) {
foreach ($uids as $uid) {
privatemsg_message_change_recipient($row->mid, $uid, 'hidden');
}
}
// If less than the total remaining uids were returned, we are finished.
if (count($uids) < $total_remaining) {
$total_remaining -= count($uids);
db_update('pm_index')
->fields(array('is_new' => PRIVATEMSG_READ))
->condition('mid', $row->mid)
->condition('recipient', $row->recipient)
->condition('type', $row->type)
->execute();
// Reset current process if necessary.
if ($offset > 0) {
state_set('privatemsg_cron_recipient_process', array());
}
}
else {
// We are not yet finished, save current process and break.
$existing_offset = isset($current_process['offset']) ? $current_process['offset'] : 0;
$current_process = (array) $row;
$current_process['offset'] = $existing_offset + count($uids);
state_set('privatemsg_cron_recipient_process', $current_process);
break;
}
}
}
/**
* Implements hook_theme().
*/
function privatemsg_theme() {
$templates = array(
'privatemsg_view' => array(
'variables' => array('message' => NULL),
'template' => config_get('privatemsg.settings', 'private_message_view_template'),
// 'privatemsg',
),
'privatemsg_from' => array(
'variables' => array('author' => NULL),
'template' => 'privatemsg-from',
),
'privatemsg_recipients' => array(
'variables' => array('message' => NULL),
'template' => 'privatemsg-recipients',
),
'privatemsg_between' => array(
'variables' => array('recipients' => NULL),
'template' => 'privatemsg-between',
),
// Define pattern for header/field templates. The theme system will register all
// theme functions that start with the defined pattern.
'privatemsg_list_header' => array(
'file' => 'privatemsg.theme.inc',
'path' => backdrop_get_path('module', 'privatemsg'),
'pattern' => 'privatemsg_list_header__',
'variables' => array(),
),
'privatemsg_list_field' => array(
'file' => 'privatemsg.theme.inc',
'path' => backdrop_get_path('module', 'privatemsg'),
'pattern' => 'privatemsg_list_field__',
'variables' => array('thread' => array()),
),
'privatemsg_new_block' => array(
'file' => 'privatemsg.theme.inc',
'path' => backdrop_get_path('module', 'privatemsg'),
'variables' => array('count'),
),
'privatemsg_username' => array(
'file' => 'privatemsg.theme.inc',
'path' => backdrop_get_path('module', 'privatemsg'),
'variables' => array('recipient' => NULL, 'options' => array()),
),
);
// Include the theme file to load the theme suggestions.
module_load_include('inc', 'privatemsg', 'privatemsg.theme');
$templates += backdrop_find_theme_functions($templates, array('theme'));
return $templates;
}
/**
* Process variables for privatemsg-view.tpl.php.
*/
function template_preprocess_privatemsg_view(&$vars) {
global $user;
$message = $vars['message'];
$vars['mid'] = isset($message->mid) ? $message->mid : NULL;
$vars['message_classes'] = isset($message->classes) ? $message->classes : array();
$vars['thread_id'] = isset($message->thread_id) ? $message->thread_id : NULL;
$vars['author_picture'] = theme('user_picture', array('account' => $message->author));
// Directly address the current user if they are the author.
if ($user->uid == $message->author->uid) {
$vars['author_name_link'] = t('You');
}
else {
$vars['author_name_link'] = privatemsg_recipient_format($message->author);
}
$vars['message_timestamp'] = privatemsg_format_date($message->timestamp);
$message->content = array(
'#view_mode' => 'message',
'body' => array(
'#markup' => check_markup($message->body, $message->format),
'#weight' => -4,
),
);
if ($message->has_tokens) {
// Replace tokens including option to add a notice if the user is not a
// recipient.
$message->content['body']['#markup'] = privatemsg_token_replace($message->content['body']['#markup'], array('privatemsg_message' => $message), array(
'privatemsg-token-notice' => TRUE,
'sanitize' => TRUE
));
}
// Build fields content.
field_attach_prepare_view('privatemsg_message', array($vars['mid'] => $message), 'message');
$message->content += field_attach_view('privatemsg_message', $message, 'message');
// Render message body.
$vars['message_body'] = backdrop_render($message->content);
if (isset($vars['mid']) && isset($vars['thread_id']) && privatemsg_user_access('delete privatemsg')) {
$vars['message_actions'][] = array(
'title' => t('Delete'),
'href' => 'messages/delete/' . $vars['thread_id'] . '/' . $vars['mid']
);
}
$vars['message_anchors'][] = 'privatemsg-mid-' . $vars['mid'];
if (!empty($message->is_new)) {
$vars['message_anchors'][] = 'new';
$vars['new'] = backdrop_ucfirst(t('new'));
}
// call hook_privatemsg_message_view_alter
backdrop_alter('privatemsg_message_view', $vars);
$vars['message_actions'] = !empty($vars['message_actions']) ? theme('links', array(
'links' => $vars['message_actions'],
'attributes' => array(
'class' => array(
'privatemsg-message-actions',
'links',
'inline'
)
)
)) : '';
$vars['anchors'] = '';
foreach ($vars['message_anchors'] as $anchor) {
$vars['anchors'] .= '<a name="' . $anchor . '"></a>';
}
}
/**
* Process variables for privatemsg-recipients.tpl.php.
*/
function template_preprocess_privatemsg_recipients(&$vars) {
$vars['participants'] = ''; // assign a default empty value
if (isset($vars['thread']['participants'])) {
$vars['participants'] = _privatemsg_format_participants($vars['thread']['participants']);
}
}
/**
* Changes the read/new status of a single message.
*
* @param int $pmid
* Message id
* @param int $status
* Either PRIVATEMSG_READ or PRIVATEMSG_UNREAD
* @param User|null $account
* User object, defaults to the current user
*/
function privatemsg_message_change_status($pmid, $status, $account = NULL) {
if (!$account) {
global $user;
$account = $user;
}
db_update('pm_index')
->fields(array('is_new' => $status))
->condition('mid', $pmid)
->condition('recipient', $account->uid)
->condition('type', array('hidden', 'user'))
->execute();
// Allows modules to respond to the status change.
module_invoke_all('privatemsg_message_status_changed', $pmid, $status, $account);
}
/**
* Return number of unread messages for an account.
*
* @param User|null $account
* Specify the user for which the unread count should be loaded.
*
* @return int
* The number of unread messages.
* @ingroup api
*/
function privatemsg_unread_count($account = NULL) {
$counts = &backdrop_static(__FUNCTION__, array());
if (!$account || $account->uid == 0) {
global $user;
$account = $user;
}
if (!isset($counts[$account->uid])) {
$counts[$account->uid] = _privatemsg_assemble_query('unread_count', $account)
->execute()
->fetchField();
}
return $counts[$account->uid];
}
/**
* Load all participants of a thread.
*
* @param int $thread_id
* Thread ID for which the participants should be loaded.
* @param User $account
* For which account should the messages be loaded. *
* @param bool $ignore_hidden
* Ignores hidden participants.
* @param string $access
* Which access permission should be checked (write or view).
*
* @return array
* Array with all visible/writable participants for that thread.
*/
function _privatemsg_load_thread_participants($thread_id, $account, $ignore_hidden = TRUE, $access = 'write') {
$query = _privatemsg_assemble_query('participants', $thread_id, $account);
$participants = array();
$to_load = array();
foreach ($query->execute() as $participant) {
if ($ignore_hidden && $participant->type == 'hidden') {
continue;
}
elseif (privatemsg_recipient_access($participant->type, $access, $participant)) {
$to_load[$participant->type][] = $participant->recipient;
}
}
// Now, load all non-user recipients.
foreach ($to_load as $type => $ids) {
$type_info = privatemsg_recipient_get_type($type);
if (isset($type_info['load']) && is_callable($type_info['load'])) {
$loaded = $type_info['load']($ids, $type);
if (is_array($loaded)) {
$participants += $loaded;
}
}
}
if ($access == 'write' && $account) {
// Remove author if loading participants for writing and when they are not the
// only recipient.
if (isset($participants['user_' . $account->uid]) && count($participants) > 1) {
unset($participants['user_' . $account->uid]);
}
}
return $participants;
}
/**
* Extract the valid usernames of a string and loads them.
*
* This function is used to parse a string supplied by a username autocomplete
* field and load all user objects.
*
* @param string $string
* A string in the form "usernameA, usernameB, ...".
* @return array $type
* Array of recipient types this should be limited to.
*
* @return array
* Array, first element is an array of loaded user objects, second an array
* with invalid names.
*
*/
function _privatemsg_parse_userstring($input, $types_limitations = array()) {
if (is_string($input)) {
$input = explode(',', $input);
}
// Start working through the input array.
$invalid = array();
$recipients = array();
$duplicates = array();
$denieds = array();
foreach ($input as $string) {