/
email_api.php
1797 lines (1518 loc) · 65.6 KB
/
email_api.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
# MantisBT - A PHP based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* Email API
*
* @package CoreAPI
* @subpackage EmailAPI
* @copyright Copyright 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*
* @uses access_api.php
* @uses authentication_api.php
* @uses bug_api.php
* @uses bugnote_api.php
* @uses category_api.php
* @uses config_api.php
* @uses constant_inc.php
* @uses current_user_api.php
* @uses custom_field_api.php
* @uses database_api.php
* @uses email_queue_api.php
* @uses event_api.php
* @uses helper_api.php
* @uses history_api.php
* @uses lang_api.php
* @uses logging_api.php
* @uses project_api.php
* @uses relationship_api.php
* @uses sponsorship_api.php
* @uses string_api.php
* @uses user_api.php
* @uses user_pref_api.php
* @uses utility_api.php
*
* @uses PHPMailerAutoload.php PHPMailer library
*/
require_api( 'access_api.php' );
require_api( 'authentication_api.php' );
require_api( 'bug_api.php' );
require_api( 'bugnote_api.php' );
require_api( 'category_api.php' );
require_api( 'config_api.php' );
require_api( 'constant_inc.php' );
require_api( 'current_user_api.php' );
require_api( 'custom_field_api.php' );
require_api( 'database_api.php' );
require_api( 'email_queue_api.php' );
require_api( 'event_api.php' );
require_api( 'helper_api.php' );
require_api( 'history_api.php' );
require_api( 'lang_api.php' );
require_api( 'logging_api.php' );
require_api( 'project_api.php' );
require_api( 'relationship_api.php' );
require_api( 'sponsorship_api.php' );
require_api( 'string_api.php' );
require_api( 'user_api.php' );
require_api( 'user_pref_api.php' );
require_api( 'utility_api.php' );
require_lib( 'phpmailer/PHPMailerAutoload.php' );
# reusable object of class SMTP
$g_phpMailer = null;
/**
* Indicates how generated emails will be processed by the shutdown function
* at the end of the current request's execution; this is a binary flag:
* - EMAIL_SHUTDOWN_SKIP Initial state: do nothing (no generated emails)
* - EMAIL_SHUTDOWN_GENERATED Emails will be sent, unless $g_email_send_using_cronjob is ON
* - EMAIL_SHUTDOWN_FORCE All queued emails will be sent regardless of cronjob settings
* @see email_shutdown_function()
* @global $g_email_shutdown_processing
*/
$g_email_shutdown_processing = EMAIL_SHUTDOWN_SKIP;
/**
* Regex for valid email addresses
* @see string_insert_hrefs()
* This pattern is consistent with email addresses validation logic
* @see $g_validate_email
* Uses the standard HTML5 pattern defined in
* {@link http://www.w3.org/TR/html5/forms.html#valid-e-mail-address}
* Note: the original regex from the spec has been modified to
* - escape the '/' in the first character class definition
* - remove the '^' and '$' anchors to allow matching anywhere in a string
*
* @return string
*/
function email_regex_simple() {
return "/[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/";
}
/**
* check to see that the format is valid and that the mx record exists
* @param string $p_email An email address.
* @return boolean
*/
function email_is_valid( $p_email ) {
$t_validate_email = config_get( 'validate_email' );
# if we don't validate then just accept
# If blank email is allowed or current user is admin, then accept blank emails which are useful for
# accounts that should never receive email notifications (e.g. anonymous account)
if( OFF == $t_validate_email ||
ON == config_get( 'use_ldap_email' ) ||
( is_blank( $p_email ) && ( ON == config_get( 'allow_blank_email' ) || current_user_is_administrator() ) )
) {
return true;
}
# E-mail validation method
# Note: PHPMailer offers alternative validation methods.
# It was decided in PR 172 (https://github.com/mantisbt/mantisbt/pull/172)
# to just default to HTML5 without over-complicating things for end users
# by offering a potentially confusing choice between the different methods.
# Refer to PHPMailer documentation for ValidateAddress method for details.
# @link https://github.com/PHPMailer/PHPMailer/blob/v5.2.9/class.phpmailer.php#L863
$t_method = 'html5';
# check email address is a valid format
log_event( LOG_EMAIL_VERBOSE, "Validating address '$p_email' with method '$t_method'" );
if( PHPMailer::ValidateAddress( $p_email, $t_method ) ) {
$t_domain = substr( $p_email, strpos( $p_email, '@' ) + 1 );
# see if we're limited to a set of known domains
$t_limit_email_domains = config_get( 'limit_email_domains' );
if( !empty( $t_limit_email_domains ) ) {
foreach( $t_limit_email_domains as $t_email_domain ) {
if( 0 == strcasecmp( $t_email_domain, $t_domain ) ) {
return true; # no need to check mx record details (below) if we've explicity allowed the domain
}
}
log_event( LOG_EMAIL, "failed - not in limited domains list '$t_limit_email_domains'" );
return false;
}
if( ON == config_get( 'check_mx_record' ) ) {
$t_mx = '';
# Check for valid mx records
if( getmxrr( $t_domain, $t_mx ) ) {
return true;
} else {
$t_host = $t_domain . '.';
# for no mx record... try dns check
if( checkdnsrr( $t_host, 'ANY' ) ) {
return true;
}
log_event( LOG_EMAIL, "failed - mx/dns record check" );
}
} else {
# Email format was valid but didn't check for valid mx records
return true;
}
} else {
log_event( LOG_EMAIL, "failed - invalid address" );
}
# Everything failed. The email is invalid
return false;
}
/**
* Check if the email address is valid trigger an ERROR if it isn't
* @param string $p_email An email address.
* @return void
*/
function email_ensure_valid( $p_email ) {
if( !email_is_valid( $p_email ) ) {
trigger_error( ERROR_EMAIL_INVALID, ERROR );
}
}
/**
* Check if the email address is disposable
* @param string $p_email An email address.
* @return boolean
*/
function email_is_disposable( $p_email ) {
if( !class_exists( 'DisposableEmailChecker' ) ) {
require_lib( 'disposable/disposable.php' );
}
return DisposableEmailChecker::is_disposable_email( $p_email );
}
/**
* Check if the email address is disposable
* trigger an ERROR if it isn't
* @param string $p_email An email address.
* @return void
*/
function email_ensure_not_disposable( $p_email ) {
if( email_is_disposable( $p_email ) ) {
trigger_error( ERROR_EMAIL_DISPOSABLE, ERROR );
}
}
/**
* Get the value associated with the specific action and flag.
* For example, you can get the value associated with notifying "admin"
* on action "new", i.e. notify administrators on new bugs which can be
* ON or OFF.
* @param string $p_action Action.
* @param string $p_flag Flag.
* @return integer 1 - enabled, 0 - disabled.
*/
function email_notify_flag( $p_action, $p_flag ) {
# If flag is specified for the specific event, use that.
$t_notify_flags = config_get( 'notify_flags' );
if( isset( $t_notify_flags[$p_action][$p_flag] ) ) {
return $t_notify_flags[$p_action][$p_flag];
}
# If not, then use the default if specified in database or global.
# Note that web UI may not support or specify all flags (e.g. explicit),
# hence, if config is retrieved from database it may not have the flag.
$t_default_notify_flags = config_get( 'default_notify_flags' );
if( isset( $t_default_notify_flags[$p_flag] ) ) {
return $t_default_notify_flags[$p_flag];
}
# If the flag is not specified so far, then force using global config which
# should have all flags specified.
$t_global_default_notify_flags = config_get_global( 'default_notify_flags' );
if( isset( $t_global_default_notify_flags[$p_flag] ) ) {
return $t_global_default_notify_flags[$p_flag];
}
return OFF;
}
/**
* Collect valid email recipients for email notification
* @todo yarick123: email_collect_recipients(...) will be completely rewritten to provide additional information such as language, user access,..
* @todo yarick123:sort recipients list by language to reduce switches between different languages
* @param integer $p_bug_id A bug identifier.
* @param string $p_notify_type Notification type.
* @param array $p_extra_user_ids_to_email Array of additional email addresses to notify.
* @return array
*/
function email_collect_recipients( $p_bug_id, $p_notify_type, array $p_extra_user_ids_to_email = array() ) {
$t_recipients = array();
# add explicitly specified users
$t_explicit_enabled = ( ON == email_notify_flag( $p_notify_type, 'explicit' ) );
foreach ( $p_extra_user_ids_to_email as $t_user_id ) {
if ( $t_explicit_enabled ) {
$t_recipients[$t_user_id] = true;
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, add @U%d (explicitly specified)', $p_bug_id, $t_user_id );
} else {
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, skip @U%d (explicit disabled)', $p_bug_id, $t_user_id );
}
}
# add Reporter
$t_reporter_id = bug_get_field( $p_bug_id, 'reporter_id' );
if( ON == email_notify_flag( $p_notify_type, 'reporter' ) ) {
$t_recipients[$t_reporter_id] = true;
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, add @U%d (reporter)', $p_bug_id, $t_reporter_id );
} else {
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, skip @U%d (reporter disabled)', $p_bug_id, $t_reporter_id );
}
# add Handler
$t_handler_id = bug_get_field( $p_bug_id, 'handler_id' );
if( $t_handler_id > 0 ) {
if( ON == email_notify_flag( $p_notify_type, 'handler' ) ) {
$t_recipients[$t_handler_id] = true;
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, add @U%d (handler)', $p_bug_id, $t_handler_id );
} else {
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, skip @U%d (handler disabled)', $p_bug_id, $t_handler_id );
}
}
$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
# add users monitoring the bug
$t_monitoring_enabled = ON == email_notify_flag( $p_notify_type, 'monitor' );
db_param_push();
$t_query = 'SELECT DISTINCT user_id FROM {bug_monitor} WHERE bug_id=' . db_param();
$t_result = db_query( $t_query, array( $p_bug_id ) );
while( $t_row = db_fetch_array( $t_result ) ) {
$t_user_id = $t_row['user_id'];
if ( $t_monitoring_enabled ) {
$t_recipients[$t_user_id] = true;
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, add @U%d (monitoring)', $p_bug_id, $t_user_id );
} else {
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, skip @U%d (monitoring disabled)', $p_bug_id, $t_user_id );
}
}
# add Category Owner
if( ON == email_notify_flag( $p_notify_type, 'category' ) ) {
$t_category_id = bug_get_field( $p_bug_id, 'category_id' );
if( $t_category_id > 0 ) {
$t_category_assigned_to = category_get_field( $t_category_id, 'user_id' );
if( $t_category_assigned_to > 0 ) {
$t_recipients[$t_category_assigned_to] = true;
log_event( LOG_EMAIL_RECIPIENT, sprintf( 'Issue = #%d, add Category Owner = @U%d', $p_bug_id, $t_category_assigned_to ) );
}
}
}
# add users who contributed bugnotes
$t_bugnote_id = bugnote_get_latest_id( $p_bug_id );
$t_bugnote_date = bugnote_get_field( $t_bugnote_id, 'last_modified' );
$t_bug = bug_get( $p_bug_id );
$t_bug_date = $t_bug->last_updated;
$t_notes_enabled = ( ON == email_notify_flag( $p_notify_type, 'bugnotes' ) );
db_param_push();
$t_query = 'SELECT DISTINCT reporter_id FROM {bugnote} WHERE bug_id = ' . db_param();
$t_result = db_query( $t_query, array( $p_bug_id ) );
while( $t_row = db_fetch_array( $t_result ) ) {
$t_user_id = $t_row['reporter_id'];
if ( $t_notes_enabled ) {
$t_recipients[$t_user_id] = true;
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, add @U%d (note author)', $p_bug_id, $t_user_id );
} else {
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, skip @U%d (note author disabled)', $p_bug_id, $t_user_id );
}
}
# add project users who meet the thresholds
$t_bug_is_private = bug_get_field( $p_bug_id, 'view_state' ) == VS_PRIVATE;
$t_threshold_min = email_notify_flag( $p_notify_type, 'threshold_min' );
$t_threshold_max = email_notify_flag( $p_notify_type, 'threshold_max' );
$t_threshold_users = project_get_all_user_rows( $t_project_id, $t_threshold_min );
foreach( $t_threshold_users as $t_user ) {
if( $t_user['access_level'] <= $t_threshold_max ) {
if( !$t_bug_is_private || access_compare_level( $t_user['access_level'], config_get( 'private_bug_threshold' ) ) ) {
$t_recipients[$t_user['id']] = true;
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, add @U%d (based on access level)', $p_bug_id, $t_user['id'] );
}
}
}
# add users as specified by plugins
$t_recipients_include_data = event_signal( 'EVENT_NOTIFY_USER_INCLUDE', array( $p_bug_id, $p_notify_type ) );
foreach( $t_recipients_include_data as $t_plugin => $t_recipients_include_data2 ) {
foreach( $t_recipients_include_data2 as $t_callback => $t_recipients_included ) {
# only handle if we get an array from the callback
if( is_array( $t_recipients_included ) ) {
foreach( $t_recipients_included as $t_user_id ) {
$t_recipients[$t_user_id] = true;
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, add @U%d (by %s plugin)', $p_bug_id, $t_user_id, $t_plugin );
}
}
}
}
# FIXME: the value of $p_notify_type could at this stage be either a status
# or a built-in actions such as 'owner and 'sponsor'. We have absolutely no
# idea whether 'new' is indicating a new bug has been filed, or if the
# status of an existing bug has been changed to 'new'. Therefore it is best
# to just assume built-in actions have precedence over status changes.
switch( $p_notify_type ) {
case 'new':
case 'feedback': # This isn't really a built-in action (delete me!)
case 'reopened':
case 'resolved':
case 'closed':
case 'bugnote':
$t_pref_field = 'email_on_' . $p_notify_type;
break;
case 'owner':
# The email_on_assigned notification type is now effectively
# email_on_change_of_handler.
$t_pref_field = 'email_on_assigned';
break;
case 'deleted':
case 'updated':
case 'sponsor':
case 'relation':
case 'monitor':
case 'priority': # This is never used, but exists in the database!
# Issue #19459 these notification actions are not actually implemented
# in the database and therefore aren't adjustable on a per-user
# basis! The exception is 'monitor' that makes no sense being a
# customisable per-user preference.
default:
# Anything not built-in is probably going to be a status
$t_pref_field = 'email_on_status';
break;
}
# @@@ we could optimize by modifiying user_cache() to take an array
# of user ids so we could pull them all in. We'll see if it's necessary
$t_final_recipients = array();
$t_user_ids = array_keys( $t_recipients );
user_cache_array_rows( $t_user_ids );
user_pref_cache_array_rows( $t_user_ids );
user_pref_cache_array_rows( $t_user_ids, $t_bug->project_id );
# Check whether users should receive the emails
# and put email address to $t_recipients[user_id]
foreach( $t_recipients as $t_id => $t_ignore ) {
# Possibly eliminate the current user
if( ( auth_get_current_user_id() == $t_id ) && ( OFF == config_get( 'email_receive_own' ) ) ) {
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, drop @U%d (own action)', $p_bug_id, $t_id );
continue;
}
# Eliminate users who don't exist anymore or who are disabled
if( !user_exists( $t_id ) || !user_is_enabled( $t_id ) ) {
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, drop @U%d (user disabled)', $p_bug_id, $t_id );
continue;
}
# Exclude users who have this notification type turned off
if( $t_pref_field ) {
$t_notify = user_pref_get_pref( $t_id, $t_pref_field );
if( OFF == $t_notify ) {
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, drop @U%d (pref %s off)', $p_bug_id, $t_id, $t_pref_field );
continue;
} else {
# Users can define the severity of an issue before they are emailed for
# each type of notification
$t_min_sev_pref_field = $t_pref_field . '_min_severity';
$t_min_sev_notify = user_pref_get_pref( $t_id, $t_min_sev_pref_field );
$t_bug_severity = bug_get_field( $p_bug_id, 'severity' );
if( $t_bug_severity < $t_min_sev_notify ) {
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, drop @U%d (pref threshold)', $p_bug_id, $t_id );
continue;
}
}
}
# exclude users who don't have at least viewer access to the bug,
# or who can't see bugnotes if the last update included a bugnote
if( !access_has_bug_level( config_get( 'view_bug_threshold', null, $t_id, $t_bug->project_id ), $p_bug_id, $t_id )
|| $t_bug_date == $t_bugnote_date && !access_has_bugnote_level( config_get( 'view_bug_threshold', null, $t_id, $t_bug->project_id ), $t_bugnote_id, $t_id )
) {
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, drop @U%d (access level)', $p_bug_id, $t_id );
continue;
}
# check to exclude users as specified by plugins
$t_recipient_exclude_data = event_signal( 'EVENT_NOTIFY_USER_EXCLUDE', array( $p_bug_id, $p_notify_type, $t_id ) );
$t_exclude = false;
foreach( $t_recipient_exclude_data as $t_plugin => $t_recipient_exclude_data2 ) {
foreach( $t_recipient_exclude_data2 as $t_callback => $t_recipient_excluded ) {
# exclude if any plugin returns true (excludes the user)
if( $t_recipient_excluded ) {
$t_exclude = true;
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, drop @U%d (by %s plugin)', $p_bug_id, $t_id, $t_plugin );
}
}
}
# user was excluded by a plugin
if( $t_exclude ) {
continue;
}
# Finally, let's get their emails, if they've set one
$t_email = user_get_email( $t_id );
if( is_blank( $t_email ) ) {
log_event( LOG_EMAIL_RECIPIENT, 'Issue = #%d, drop @U%d (no email address)', $p_bug_id, $t_id );
} else {
# @@@ we could check the emails for validity again but I think
# it would be too slow
$t_final_recipients[$t_id] = $t_email;
}
}
return $t_final_recipients;
}
/**
* Send password to user
* @param integer $p_user_id A valid user identifier.
* @param string $p_confirm_hash Confirmation hash.
* @param string $p_admin_name Administrator name.
* @return void
*/
function email_signup( $p_user_id, $p_confirm_hash, $p_admin_name = '' ) {
if( ( OFF == config_get( 'send_reset_password' ) ) || ( OFF == config_get( 'enable_email_notification' ) ) ) {
return;
}
# @@@ thraxisp - removed to address #6084 - user won't have any settings yet,
# use same language as display for the email
# lang_push( user_pref_get_language( $p_user_id ) );
# retrieve the username and email
$t_username = user_get_field( $p_user_id, 'username' );
$t_email = user_get_email( $p_user_id );
# Build Welcome Message
$t_subject = '[' . config_get( 'window_title' ) . '] ' . lang_get( 'new_account_subject' );
if( !empty( $p_admin_name ) ) {
$t_intro_text = sprintf( lang_get( 'new_account_greeting_admincreated' ), $p_admin_name, $t_username );
} else {
$t_intro_text = sprintf( lang_get( 'new_account_greeting' ), $t_username );
}
$t_message = $t_intro_text . "\n\n" . string_get_confirm_hash_url( $p_user_id, $p_confirm_hash ) . "\n\n" . lang_get( 'new_account_message' ) . "\n\n" . lang_get( 'new_account_do_not_reply' );
# Send signup email regardless of mail notification pref
# or else users won't be able to sign up
if( !is_blank( $t_email ) ) {
email_store( $t_email, $t_subject, $t_message, null, true );
log_event( LOG_EMAIL, 'Signup Email = %s, Hash = %s, User = @U%d', $t_email, $p_confirm_hash, $p_user_id );
}
# lang_pop(); # see above
}
/**
* Send confirm_hash URL to user forgets the password
* @param integer $p_user_id A valid user identifier.
* @param string $p_confirm_hash Confirmation hash.
* @return void
*/
function email_send_confirm_hash_url( $p_user_id, $p_confirm_hash ) {
if( OFF == config_get( 'send_reset_password' ) ) {
log_event( LOG_EMAIL_VERBOSE, 'Password reset email notifications disabled.' );
return;
}
if( OFF == config_get( 'enable_email_notification' ) ) {
log_event( LOG_EMAIL_VERBOSE, 'email notifications disabled.' );
return;
}
if( !user_is_enabled( $p_user_id ) ) {
log_event( LOG_EMAIL, 'Password reset for user @U%d not sent, user is disabled', $p_user_id );
return;
}
lang_push( user_pref_get_language( $p_user_id ) );
# retrieve the username and email
$t_username = user_get_field( $p_user_id, 'username' );
$t_email = user_get_email( $p_user_id );
$t_subject = '[' . config_get( 'window_title' ) . '] ' . lang_get( 'lost_password_subject' );
$t_message = lang_get( 'reset_request_msg' ) . " \n\n" . string_get_confirm_hash_url( $p_user_id, $p_confirm_hash ) . " \n\n" . lang_get( 'new_account_username' ) . ' ' . $t_username . " \n" . lang_get( 'new_account_IP' ) . ' ' . $_SERVER['REMOTE_ADDR'] . " \n\n" . lang_get( 'new_account_do_not_reply' );
# Send password reset regardless of mail notification preferences
# or else users won't be able to receive their reset passwords
if( !is_blank( $t_email ) ) {
email_store( $t_email, $t_subject, $t_message, null, true );
log_event( LOG_EMAIL, 'Password reset for user @U%d sent to %s', $p_user_id, $t_email );
} else {
log_event( LOG_EMAIL, 'Password reset for user @U%d not sent, email is empty', $p_user_id );
}
lang_pop();
}
/**
* notify the selected group a new user has signup
* @param string $p_username Username of new user.
* @param string $p_email Email address of new user.
* @return void
*/
function email_notify_new_account( $p_username, $p_email ) {
log_event( LOG_EMAIL, 'New account for user %s', $p_username );
$t_threshold_min = config_get( 'notify_new_user_created_threshold_min' );
$t_threshold_users = project_get_all_user_rows( ALL_PROJECTS, $t_threshold_min );
$t_user_ids = array_keys( $t_threshold_users );
user_cache_array_rows( $t_user_ids );
user_pref_cache_array_rows( $t_user_ids );
foreach( $t_threshold_users as $t_user ) {
lang_push( user_pref_get_language( $t_user['id'] ) );
$t_recipient_email = user_get_email( $t_user['id'] );
$t_subject = '[' . config_get( 'window_title' ) . '] ' . lang_get( 'new_account_subject' );
$t_message = lang_get( 'new_account_signup_msg' ) . "\n\n" . lang_get( 'new_account_username' ) . ' ' . $p_username . "\n" . lang_get( 'new_account_email' ) . ' ' . $p_email . "\n" . lang_get( 'new_account_IP' ) . ' ' . $_SERVER['REMOTE_ADDR'] . "\n" . config_get_global( 'path' ) . "\n\n" . lang_get( 'new_account_do_not_reply' );
if( !is_blank( $t_recipient_email ) ) {
email_store( $t_recipient_email, $t_subject, $t_message );
log_event( LOG_EMAIL, 'New Account Notify for email = \'%s\'', $t_recipient_email );
}
lang_pop();
}
}
/**
* send a generic email
* $p_notify_type: use check who she get notified of such event.
* $p_message_id: message id to be translated and included at the top of the email message.
* Return false if it were problems sending email
* @param integer $p_bug_id A bug identifier.
* @param string $p_notify_type Notification type.
* @param integer $p_message_id Message identifier.
* @param array $p_header_optional_params Optional Parameters (default null).
* @param array $p_extra_user_ids_to_email Array of additional users to email.
* @return void
*/
function email_generic( $p_bug_id, $p_notify_type, $p_message_id = null, array $p_header_optional_params = null, array $p_extra_user_ids_to_email = array() ) {
# @todo yarick123: email_collect_recipients(...) will be completely rewritten to provide additional information such as language, user access,..
# @todo yarick123:sort recipients list by language to reduce switches between different languages
$t_recipients = email_collect_recipients( $p_bug_id, $p_notify_type, $p_extra_user_ids_to_email );
email_generic_to_recipients( $p_bug_id, $p_notify_type, $t_recipients, $p_message_id, $p_header_optional_params );
}
/**
* Sends a generic email to the specific set of recipients.
*
* @param integer $p_bug_id A bug identifier
* @param string $p_notify_type Notification type
* @param array $p_recipients Array of recipients (key: user id, value: email address)
* @param integer $p_message_id Message identifier
* @param array $p_header_optional_params Optional Parameters (default null)
* @return void
*/
function email_generic_to_recipients( $p_bug_id, $p_notify_type, array $p_recipients, $p_message_id = null, array $p_header_optional_params = null ) {
if( OFF == config_get( 'enable_email_notification' ) ) {
return;
}
ignore_user_abort( true );
bugnote_get_all_bugnotes( $p_bug_id );
$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
if( is_array( $p_recipients ) ) {
# send email to every recipient
foreach( $p_recipients as $t_user_id => $t_user_email ) {
log_event( LOG_EMAIL_VERBOSE, 'Issue = #%d, Type = %s, Msg = \'%s\', User = @U%d, Email = \'%s\'.', $p_bug_id, $p_notify_type, $p_message_id, $t_user_id, $t_user_email );
# load (push) user language here as build_visible_bug_data assumes current language
lang_push( user_pref_get_language( $t_user_id, $t_project_id ) );
$t_visible_bug_data = email_build_visible_bug_data( $t_user_id, $p_bug_id, $p_message_id );
email_bug_info_to_one_user( $t_visible_bug_data, $p_message_id, $t_user_id, $p_header_optional_params );
lang_pop();
}
}
}
/**
* Send notices that a user is now monitoring the bug. Typically this will only be sent when the added
* user is not the logged in user. This is assuming that receive own notifications is OFF (default).
* @param integer $p_bug_id A valid bug identifier.
* @param integer $p_user_id A valid user identifier.
* @return void
*/
function email_monitor_added( $p_bug_id, $p_user_id ) {
log_event( LOG_EMAIL, 'Issue #%d monitored by user @U%d', $p_bug_id, $p_user_id );
$t_opt = array();
$t_opt[] = bug_format_id( $p_bug_id );
$t_opt[] = user_get_name( $p_user_id );
email_generic( $p_bug_id, 'monitor', 'email_notification_title_for_action_monitor', $t_opt, array( $p_user_id ) );
}
/**
* send notices when a relationship is ADDED
* @param integer $p_bug_id A bug identifier.
* @param integer $p_related_bug_id Related bug identifier.
* @param integer $p_rel_type Relationship type.
* @return void
*/
function email_relationship_added( $p_bug_id, $p_related_bug_id, $p_rel_type ) {
$t_opt = array();
$t_opt[] = bug_format_id( $p_related_bug_id );
global $g_relationships;
if( !isset( $g_relationships[$p_rel_type] ) ) {
trigger_error( ERROR_RELATIONSHIP_NOT_FOUND, ERROR );
}
log_event(
LOG_EMAIL,
'Issue #%d relationship added to issue #%d (relationship type %s)',
$p_bug_id,
$p_related_bug_id,
$g_relationships[$p_rel_type]['#description'] );
$t_recipients = email_collect_recipients( $p_bug_id, 'relation' );
# Recipient has to have access to both bugs to get the notification.
$t_recipients = email_filter_recipients_for_bug( $p_bug_id, $t_recipients );
$t_recipients = email_filter_recipients_for_bug( $p_related_bug_id, $t_recipients );
email_generic_to_recipients( $p_bug_id, 'relation', $t_recipients, $g_relationships[$p_rel_type]['#notify_added'], $t_opt );
}
/**
* Filter recipients to remove ones that don't have access to the specified bug.
*
* @param integer $p_bug_id The bug id
* @param array $p_recipients The recipients array (key: id, value: email)
* @return array The filtered list of recipients in same format
* @access private
*/
function email_filter_recipients_for_bug( $p_bug_id, array $p_recipients ) {
$t_view_bug_threshold = config_get( 'view_bug_threshold' );
$t_authorized_recipients = array();
foreach( $p_recipients as $t_recipient_id => $t_recipient_email ) {
if( access_has_bug_level( $t_view_bug_threshold, $p_bug_id, $t_recipient_id ) ) {
$t_authorized_recipients[$t_recipient_id] = $t_recipient_email;
}
}
return $t_authorized_recipients;
}
/**
* send notices when a relationship is DELETED
* @param integer $p_bug_id A bug identifier.
* @param integer $p_related_bug_id Related bug identifier.
* @param integer $p_rel_type Relationship type.
* @return void
*/
function email_relationship_deleted( $p_bug_id, $p_related_bug_id, $p_rel_type ) {
$t_opt = array();
$t_opt[] = bug_format_id( $p_related_bug_id );
global $g_relationships;
if( !isset( $g_relationships[$p_rel_type] ) ) {
trigger_error( ERROR_RELATIONSHIP_NOT_FOUND, ERROR );
}
log_event(
LOG_EMAIL,
'Issue #%d relationship to issue #%d (relationship type %s) deleted.',
$p_bug_id,
$p_related_bug_id,
$g_relationships[$p_rel_type]['#description'] );
$t_recipients = email_collect_recipients( $p_bug_id, 'relation' );
# Recipient has to have access to both bugs to get the notification.
$t_recipients = email_filter_recipients_for_bug( $p_bug_id, $t_recipients );
$t_recipients = email_filter_recipients_for_bug( $p_related_bug_id, $t_recipients );
email_generic_to_recipients( $p_bug_id, 'relation', $t_recipients, $g_relationships[$p_rel_type]['#notify_deleted'], $t_opt );
}
/**
* send notices to all the handlers of the parent bugs when a child bug is RESOLVED
* @param integer $p_bug_id A bug identifier.
* @return void
*/
function email_relationship_child_resolved( $p_bug_id ) {
email_relationship_child_resolved_closed( $p_bug_id, 'email_notification_title_for_action_relationship_child_resolved' );
}
/**
* send notices to all the handlers of the parent bugs when a child bug is CLOSED
* @param integer $p_bug_id A bug identifier.
* @return void
*/
function email_relationship_child_closed( $p_bug_id ) {
email_relationship_child_resolved_closed( $p_bug_id, 'email_notification_title_for_action_relationship_child_closed' );
}
/**
* send notices to all the handlers of the parent bugs still open when a child bug is resolved/closed
*
* @param integer $p_bug_id A bug identifier.
* @param integer $p_message_id A message identifier.
* @return void
*/
function email_relationship_child_resolved_closed( $p_bug_id, $p_message_id ) {
# retrieve all the relationships in which the bug is the destination bug
$t_relationship = relationship_get_all_dest( $p_bug_id );
$t_relationship_count = count( $t_relationship );
if( $t_relationship_count == 0 ) {
# no parent bug found
return;
}
if( $p_message_id == 'email_notification_title_for_action_relationship_child_closed' ) {
log_event( LOG_EMAIL, sprintf( 'Issue #%d child issue closed', $p_bug_id ) );
} else {
log_event( LOG_EMAIL, sprintf( 'Issue #%d child issue resolved', $p_bug_id ) );
}
for( $i = 0;$i < $t_relationship_count;$i++ ) {
if( $t_relationship[$i]->type == BUG_DEPENDANT ) {
$t_src_bug_id = $t_relationship[$i]->src_bug_id;
$t_status = bug_get_field( $t_src_bug_id, 'status' );
if( $t_status < config_get( 'bug_resolved_status_threshold' ) ) {
# sent the notification just for parent bugs not resolved/closed
$t_opt = array();
$t_opt[] = bug_format_id( $p_bug_id );
email_generic( $t_src_bug_id, 'handler', $p_message_id, $t_opt );
}
}
}
}
/**
* send notices when a bug is sponsored
* @param int $p_bug_id
* @return null
*/
function email_sponsorship_added( $p_bug_id ) {
log_event( LOG_EMAIL, sprintf( 'Issue #%d sponsorship added', $p_bug_id ) );
email_generic( $p_bug_id, 'sponsor', 'email_notification_title_for_action_sponsorship_added' );
}
/**
* send notices when a sponsorship is modified
* @param int $p_bug_id
* @return null
*/
function email_sponsorship_updated( $p_bug_id ) {
log_event( LOG_EMAIL, sprintf( 'Issue #%d sponsorship updated', $p_bug_id ) );
email_generic( $p_bug_id, 'sponsor', 'email_notification_title_for_action_sponsorship_updated' );
}
/**
* send notices when a sponsorship is deleted
* @param int $p_bug_id
* @return null
*/
function email_sponsorship_deleted( $p_bug_id ) {
log_event( LOG_EMAIL, sprintf( 'Issue #%d sponsorship removed', $p_bug_id ) );
email_generic( $p_bug_id, 'sponsor', 'email_notification_title_for_action_sponsorship_deleted' );
}
/**
* send notices when a new bug is added
* @param int $p_bug_id
* @return null
*/
function email_bug_added( $p_bug_id ) {
log_event( LOG_EMAIL, sprintf( 'Issue #%d reported', $p_bug_id ) );
email_generic( $p_bug_id, 'new', 'email_notification_title_for_action_bug_submitted' );
}
/**
* Send notifications for bug update.
* @param int $p_bug_id The bug id.
*/
function email_bug_updated( $p_bug_id ) {
log_event( LOG_EMAIL, sprintf( 'Issue #%d updated', $p_bug_id ) );
email_generic( $p_bug_id, 'updated', 'email_notification_title_for_action_bug_updated' );
}
/**
* send notices when a new bugnote
* @param int $p_bug_id
* @return null
*/
function email_bugnote_add( $p_bug_id ) {
log_event( LOG_EMAIL, sprintf( 'Note added to issue #%d', $p_bug_id ) );
email_generic( $p_bug_id, 'bugnote', 'email_notification_title_for_action_bugnote_submitted' );
}
/**
* send notices when a bug is RESOLVED
* @param int $p_bug_id
* @return null
*/
function email_resolved( $p_bug_id ) {
log_event( LOG_EMAIL, sprintf( 'Issue #%d resolved', $p_bug_id ) );
email_generic( $p_bug_id, 'resolved', 'email_notification_title_for_status_bug_resolved' );
}
/**
* send notices when a bug is CLOSED
* @param int $p_bug_id
* @return null
*/
function email_close( $p_bug_id ) {
log_event( LOG_EMAIL, sprintf( 'Issue #%d closed', $p_bug_id ) );
email_generic( $p_bug_id, 'closed', 'email_notification_title_for_status_bug_closed' );
}
/**
* send notices when a bug is REOPENED
* @param int $p_bug_id
* @return null
*/
function email_bug_reopened( $p_bug_id ) {
log_event( LOG_EMAIL, sprintf( 'Issue #%d reopened', $p_bug_id ) );
email_generic( $p_bug_id, 'reopened', 'email_notification_title_for_action_bug_reopened' );
}
/**
* Send notices when a bug handler is changed.
* @param int $p_bug_id
* @param int $p_prev_handler_id
* @param int $p_new_handler_id
* @return null
*/
function email_owner_changed($p_bug_id, $p_prev_handler_id, $p_new_handler_id ) {
if ( $p_prev_handler_id == 0 && $p_new_handler_id != 0 ) {
log_event( LOG_EMAIL, sprintf( 'Issue #%d assigned to user @U%d.', $p_bug_id, $p_new_handler_id ) );
} else if ( $p_prev_handler_id != 0 && $p_new_handler_id == 0 ) {
log_event( LOG_EMAIL, sprintf( 'Issue #%d is no longer assigned to @U%d.', $p_bug_id, $p_prev_handler_id ) );
} else {
log_event(
LOG_EMAIL,
sprintf(
'Issue #%d is assigned to @U%d instead of @U%d.',
$p_bug_id,
$p_new_handler_id,
$p_prev_handler_id )
);
}
$t_message_id = $p_new_handler_id == NO_USER ?
'email_notification_title_for_action_bug_unassigned' :
'email_notification_title_for_action_bug_assigned';
$t_extra_user_ids_to_email = array();
if ( $p_prev_handler_id !== NO_USER && $p_prev_handler_id != $p_new_handler_id ) {
if ( email_notify_flag( 'owner', 'handler' ) == ON ) {
$t_extra_user_ids_to_email[] = $p_prev_handler_id;
}
}
email_generic( $p_bug_id, 'owner', $t_message_id, /* headers */ null, $t_extra_user_ids_to_email );
}
/**
* Send notifications when bug status is changed.
* @param int $p_bug_id The bug id
* @param string $p_new_status_label The new status label.
*/
function email_bug_status_changed( $p_bug_id, $p_new_status_label ) {
log_event( LOG_EMAIL, sprintf( 'Issue #%d status changed', $p_bug_id ) );
email_generic( $p_bug_id, $p_new_status_label, 'email_notification_title_for_status_bug_' . $p_new_status_label );
}
/**
* send notices when a bug is DELETED
* @param int $p_bug_id
* @return null
*/
function email_bug_deleted( $p_bug_id ) {
log_event( LOG_EMAIL, sprintf( 'Issue #%d deleted', $p_bug_id ) );
email_generic( $p_bug_id, 'deleted', 'email_notification_title_for_action_bug_deleted' );
}
/**
* Store email in queue for sending
*
* @param string $p_recipient Email recipient address.
* @param string $p_subject Subject of email message.
* @param string $p_message Body text of email message.
* @param array $p_headers Array of additional headers to send with the email.
* @param boolean $p_force True to force sending of emails in shutdown function,
* even when using cronjob
* @return integer|null
*/
function email_store( $p_recipient, $p_subject, $p_message, array $p_headers = null, $p_force = false ) {
global $g_email_shutdown_processing;
$t_recipient = trim( $p_recipient );
$t_subject = string_email( trim( $p_subject ) );
$t_message = string_email_links( trim( $p_message ) );
# short-circuit if no recipient is defined, or email disabled
# note that this may cause signup messages not to be sent
if( is_blank( $p_recipient ) || ( OFF == config_get( 'enable_email_notification' ) ) ) {
return null;
}
$t_email_data = new EmailData;
$t_email_data->email = $t_recipient;
$t_email_data->subject = $t_subject;
$t_email_data->body = $t_message;
$t_email_data->metadata = array();
$t_email_data->metadata['headers'] = $p_headers === null ? array() : $p_headers;
# Urgent = 1, Not Urgent = 5, Disable = 0
$t_email_data->metadata['charset'] = 'utf-8';