/
email_api.php
866 lines (754 loc) · 29.9 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
<?php
# Mantis - a php based bugtracking system
# Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
# Copyright (C) 2002 - 2003 Mantis Team - mantisbt-dev@lists.sourceforge.net
# This program is distributed under the terms and conditions of the GPL
# See the README and LICENSE files for details
# --------------------------------------------------------
# $Id: email_api.php,v 1.50 2003-02-19 12:18:09 puklos Exp $
# --------------------------------------------------------
$t_core_dir = dirname( __FILE__ ).DIRECTORY_SEPARATOR;
require_once( $t_core_dir . 'current_user_api.php' );
require_once( $t_core_dir . 'bug_api.php' );
require_once( $t_core_dir . 'custom_field_api.php' );
require_once( $t_core_dir . 'string_api.php' );
require_once( $t_core_dir . 'history_api.php' );
###########################################################################
# Email API
###########################################################################
# --------------------
# Return a perl compatible regular expression that will
# match a valid email address as per RFC 822 (approximately)
#
# The regex will provide too matched groups: the first will be the
# local part (or mailbox name) and the second will be the domain
function email_get_rfc822_regex() {
# Build up basic RFC 822 BNF definitions.
# list of the special characters: ( ) < > @ , ; : \ " . [ ]
$t_specials = '\(\)\<\>\@\,\;\:\\\"\.\[\]';
# the space character
$t_space = '\040';
# valid characters in a quoted string
$t_char = '\000-\177';
# control characters
$t_ctl = '\000-\037\177';
# a chunk of quoted text (anything except " \ \r are valid)
$t_qtext_re = '[^"\\\r]+';
# match any valid character preceded by a backslash ( mostly for \" )
$t_qpair_re = "\\\\[$t_char]";
# a complete quoted string - " characters with valid characters or
# backslash-escaped characters between them
$t_quoted_string_re = "(?:\"(?:$t_qtext_re|$t_qpair_re)*\")";
# an unquoted atom (anything that isn't a control char, a space, or a
# special char)
$t_atom_re = "(?:[^$t_ctl$t_space$t_specials]+)";
# a domain ref is an atom
$t_domain_ref_re = $t_atom_re;
# the characters in a domain literal can be anything except: [ ] \ \r
$t_dtext_re = "[^\\[\\]\\\\\\r]";
# a domain-literal is a sequence of characters or escaped pairs inside
# square brackets
$t_domain_literal_re = "\\[(?:$t_dtext_re|$t_qpair_re)*\\]";
# a subdomain is a domain ref or a domain literal
$t_sub_domain_re = "(?:$t_domain_ref_re|$t_domain_literal_re)";
# a domain is at least one subdomain, with optional further subdomains
# separated by periods. eg: '[1.2.3.4]' or 'foo.bar'
$t_domain_re = "$t_sub_domain_re(?:\.$t_sub_domain_re)*";
# a word is either quoted string or an atom
$t_word_re = "(?:$t_atom_re|$t_quoted_string_re)";
# the local part of the address spec (the mailbox name)
# is one or more words separated by periods
$t_local_part_re = "$t_word_re(?:\.$t_word_re)*";
# the address spec is made up of a local part, and @ symbol,
# and a domain
$t_addr_spec_re = "/^($t_local_part_re)\@($t_domain_re)$/";
return $t_addr_spec_re;
}
# --------------------
# check to see that the format is valid and that the mx record exists
function email_is_valid( $p_email ) {
# if we don't validate then just accept
if ( OFF == config_get( 'validate_email' ) ) {
return true;
}
if ( is_blank( $p_email ) && ON == config_get( 'allow_blank_email' ) ) {
return true;
}
# Use a regular expression to check to see if the email is in valid format
# x-xx.xxx@yyy.zzz.abc etc.
if ( preg_match( email_get_rfc822_regex(), $p_email, $t_check ) ) {
$t_local = $t_check[1];
$t_domain = $t_check[2];
# see if we're limited to one domain
if ( ON == config_get( 'limit_email_domain' ) ) {
if ( 0 != strcasecmp( $t_limit_email_domain, $t_domain ) ) {
return false;
}
}
if ( preg_match( '/\\[(\d+)\.(\d+)\.(\d+)\.(\d+)\\]/', $t_domain, $t_check ) ) {
# Handle domain-literals of the form '[1.2.3.4]'
# as long as each segment is less than 255, we're ok
if ( $t_check[1] <= 255 &&
$t_check[2] <= 255 &&
$t_check[3] <= 255 &&
$t_check[4] <= 255 ) {
return true;
}
} else if ( ON == config_get( 'check_mx_record' ) ) {
# Check for valid mx records
if ( getmxrr( $t_domain, $temp ) ) {
return true;
} else {
$host = $t_domain . '.';
# for no mx record... try dns check
if (checkdnsrr ( $host, 'ANY' ))
return true;
}
} else {
# Email format was valid but did't check for valid mx records
return true;
}
}
# Everything failed. The email is invalid
return false;
}
# --------------------
# Check if the email address is valid
# return true if it is, trigger an ERROR if it isn't
function email_ensure_valid( $p_email ) {
if ( ! email_is_valid( $p_email ) ) {
trigger_error( ERROR_EMAIL_INVALID, ERROR );
}
}
# --------------------
# get_notify_flag
# 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.
function get_notify_flag( $action, $flag ) {
global $g_notify_flags, $g_default_notify_flags;
if ( isset ( $g_notify_flags[$action][$flag] ) ) {
return $g_notify_flags[$action][$flag];
} elseif ( isset ( $g_default_notify_flags[$flag] ) ) {
return $g_default_notify_flags[$flag];
}
return OFF;
}
# --------------------
# build the bcc list
# UGLY function: here's how it works:
# We store all the email addresses in an array. First we grab the reporter.
# If the handler is assigned then we add that.
# The next part is only for NEW bugs.
# We add all ADMINISTRATORs then add DEVELOPERs.
# Lastly, we do a bit of post processing and return the bcc string.
function build_bcc_list( $p_bug_id, $p_notify_type ) {
global $g_mantis_bug_table, $g_mantis_user_table, $g_mantis_bugnote_table,
$g_mantis_project_table, $g_mantis_user_pref_table,
$g_mantis_project_user_list_table,
$g_use_bcc, $g_use_phpMailer,
$g_mantis_bug_monitor_table;
$c_bug_id = (integer)$p_bug_id;
# setup the array of email entries
$send_arr = array();
# Get Reporter Email
if ( ON == get_notify_flag( $p_notify_type, 'reporter' )) {
$v_reporter_id = bug_get_field( $p_bug_id, 'reporter_id' );
if ( user_exists( $v_reporter_id ) && user_is_enabled( $v_reporter_id ) ) {
$t_pref_field = 'email_on_' . $p_notify_type;
if ( db_field_exists( $t_pref_field, $g_mantis_user_pref_table ) ) {
$t_notify_reporter = user_pref_get_pref( $v_reporter_id, $t_pref_field );
if ( ON == $t_notify_reporter ) {
$send_arr[] = user_get_email( $v_reporter_id );
}
} else {
$send_arr[] = user_get_email( $v_reporter_id );
}
}
}
# Get Handler Email
if ( ON == get_notify_flag( $p_notify_type, 'handler' )) {
$v_handler_id = bug_get_field( $p_bug_id, 'handler_id' );
if ( user_exists( $v_handler_id ) && user_is_enabled( $v_handler_id ) ) {
$t_pref_field = 'email_on_' . $p_notify_type;
if ( db_field_exists( $t_pref_field, $g_mantis_user_pref_table ) ) {
$t_notify_handler = user_pref_get_pref( $v_handler_id, $t_pref_field );
if ( ON == $t_notify_handler ) {
$send_arr[] = user_get_email( $v_handler_id );
}
} else {
$send_arr[] = user_get_email( $v_handler_id );
}
}
}
# Check if we want to broadcast to all developers on a NEW bug
$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
$t_project_view_state = project_get_field( $t_project_id, 'view_state' );
#@@@@@@@
$temp_arr = array();
# grab the administrators
$query = "SELECT id, email
FROM $g_mantis_user_table
WHERE enabled = 1
ORDER BY username";
$result = db_query( $query );
$user_count = db_num_rows( $result );
for ($i=0;$i<$user_count;$i++) {
$row = db_fetch_array( $result );
extract( $row, EXTR_PREFIX_ALL, 'v' );
$temp_arr[$v_email] = array( $v_email, $v_id );
}
foreach ( $temp_arr as $key => $val ) {
$v_id = $val[1];
$v_email = $val[0];
# add all administrators if notification flag enabled
$t_access_level = user_get_field( $v_id, 'access_level' );
if ( ($t_access_level == ADMINISTRATOR) && (ON == get_notify_flag( $p_notify_type, 'admin' ))) {
$send_arr[] = $v_email;
continue;
}
# Get authors of bug notes
if ( ON == get_notify_flag( $p_notify_type, 'bugnotes' )) {
$query = "SELECT 1 ".
"FROM $g_mantis_bugnote_table ".
"WHERE (bug_id = $p_bug_id) AND (reporter_id = $v_id) ".
"LIMIT 1";
$result = db_query( $query );
if ( db_num_rows( $result ) > 0 ) {
$send_arr[] = $v_email;
}
}
# see if users belong
$t_project_view_state = project_get_field( $t_project_id, 'view_state' );
if ( PUBLIC == $t_project_view_state ) {
$query = "SELECT l.access_level
FROM $g_mantis_project_user_list_table l,
$g_mantis_project_table p
WHERE l.project_id='$t_project_id' AND
p.id=l.project_id AND
l.user_id='$v_id'
LIMIT 1";
$result = db_query( $query );
$count = db_num_rows( $result );
if ( $count > 0 ){
$t_access_level = db_result( $result );
}
if ( $t_access_level >= get_notify_flag($p_notify_type, 'threshold')) {
$send_arr[] = $v_email;
}
} else {
$query = "SELECT 1
FROM $g_mantis_project_user_list_table l,
$g_mantis_project_table p
WHERE l.project_id='$t_project_id' AND
p.id=l.project_id AND
l.user_id='$v_id' AND
l.access_level>='" . get_notify_flag($p_notify_type, 'threshold') ."'
LIMIT 1";
$result = db_query( $query );
if ( db_num_rows( $result ) > 0 ) {
$send_arr[] = $v_email;
continue;
}
}
}
if ( ON == get_notify_flag( $p_notify_type, 'monitor' )) {
# grab all users MONITORING bug
$query = "SELECT DISTINCT m.user_id, u.email
FROM $g_mantis_bug_monitor_table m,
$g_mantis_user_table u
WHERE m.bug_id=$c_bug_id AND
m.user_id=u.id AND u.enabled = 1";
$result = db_query( $query );
$monitor_user_count = db_num_rows( $result );
for ($i=0;$i<$monitor_user_count;$i++) {
$row = db_fetch_array( $result );
# @@@ This check seem to be redundant, since the user list
# retrieved from the query result should all be enabled/exists
if ( user_exists( $row['user_id'] ) && user_is_enabled( $row['user_id'] ) ) {
$t_pref_field = 'email_on_' . $p_notify_type;
if ( db_field_exists( $t_pref_field, $g_mantis_user_pref_table ) ) {
# if the user's notification is on then add to the list
$t_notify = user_pref_get_pref( $row['user_id'], $t_pref_field );
if ( ON == $t_notify ) {
$send_arr[] = $row['email'];
}
}
}
} # end MONITORING
}
$t_bcc = ( $g_use_bcc && !$g_use_phpMailer ) ? 'Bcc: ' : '';
## win-bcc-bug
$send_arr = array_unique( $send_arr );
foreach ( $send_arr as $send_val ) {
if (strlen($send_val) != 0) {
$t_bcc .= $send_val.', ';
}
}
# chop off the last comma and add a \n
if ( strlen( $t_bcc ) > 5 ) {
return substr( $t_bcc, 0, strlen( $t_bcc )-2).(($g_use_bcc) ? "\n" : ''); ## win-bcc-bug
} else {
return '';
}
}
# --------------------
# Send password to user
function email_signup( $p_user_id, $p_password ) {
global $g_mantis_user_table, $g_path;
$c_user_id = db_prepare_int( $p_user_id );
$query = "SELECT username, email
FROM $g_mantis_user_table
WHERE id='$c_user_id'";
$result = db_query( $query );
$row = db_fetch_array( $result );
extract( $row, EXTR_PREFIX_ALL, 'v' );
# Build Welcome Message
$t_message = lang_get( 'new_account_greeting' ).
lang_get( 'new_account_url' ) . $g_path . "\n".
lang_get( 'new_account_username' ) . $v_username . "\n".
lang_get( 'new_account_password' ) . $p_password . "\n\n".
lang_get( 'new_account_message' ) .
lang_get( 'new_account_do_not_reply' );
email_send( $v_email, lang_get( 'new_account_subject' ), $t_message );
}
# --------------------
# Send new password when user forgets
function email_reset( $p_user_id, $p_password ) {
global $g_mantis_user_table, $g_path;
$c_user_id = db_prepare_int( $p_user_id );
$query = "SELECT username, email
FROM $g_mantis_user_table
WHERE id='$c_user_id'";
$result = db_query( $query );
$row = db_fetch_array( $result );
extract( $row, EXTR_PREFIX_ALL, 'v' );
# Build Welcome Message
$t_message = lang_get( 'reset_request_msg' ) . "\n\n".
lang_get( 'new_account_username' ) . $v_username."\n".
lang_get( 'new_account_password' ) . $p_password."\n\n".
$g_path."\n\n";
email_send( $v_email, lang_get( 'news_password_msg' ), $t_message );
}
# --------------------
# send notices when a new bug is added
function email_new_bug( $p_bug_id ) {
$t_bcc = build_bcc_list( $p_bug_id, 'new' );
email_bug_info( $p_bug_id, lang_get( 'new_bug_msg' ), $t_bcc );
}
# --------------------
# send notices when a new bugnote
function email_bugnote_add( $p_bug_id ) {
$t_bcc = build_bcc_list( $p_bug_id, 'bugnote' );
email_bug_info( $p_bug_id, lang_get( 'email_bugnote_msg' ), $t_bcc );
}
# --------------------
# send notices when a bug is RESOLVED
function email_resolved( $p_bug_id ) {
$t_bcc = build_bcc_list( $p_bug_id, 'resolved' );
email_bug_info( $p_bug_id, lang_get( 'email_resolved_msg' ), $t_bcc );
}
# --------------------
# send notices when a bug is CLOSED
function email_close( $p_bug_id ) {
$t_bcc = build_bcc_list( $p_bug_id, 'closed' );
email_bug_info( $p_bug_id, lang_get( 'email_close_msg' ), $t_bcc );
}
# --------------------
# send notices when a bug is set to FEEDBACK
function email_feedback( $p_bug_id ) {
$t_bcc = build_bcc_list( $p_bug_id, 'feedback' );
email_bug_info( $p_bug_id, lang_get( 'email_feedback_msg' ), $t_bcc );
}
# --------------------
# send notices when a bug is REOPENED
function email_reopen( $p_bug_id ) {
$t_bcc = build_bcc_list( $p_bug_id, 'reopened' );
email_bug_info( $p_bug_id, lang_get( 'email_reopen_msg' ), $t_bcc );
}
# --------------------
# send notices when a bug is ASSIGNED
function email_assign( $p_bug_id ) {
$t_bcc = build_bcc_list( $p_bug_id, 'assigned' );
email_bug_info( $p_bug_id, lang_get( 'email_assigned_msg' ), $t_bcc );
}
# --------------------
# send notices when a bug is DELETED
function email_bug_deleted( $p_bug_id ) {
$t_bcc = build_bcc_list( $p_bug_id, 'deleted' );
email_bug_info( $p_bug_id, lang_get( 'email_bug_deleted_msg' ), $t_bcc );
}
# --------------------
# Build the bug info part of the message
function email_build_bug_message( $p_bug_id, $p_message, &$p_category ) {
global $g_mantis_bug_table, $g_mantis_bug_text_table,
$g_mantis_project_table,
$g_complete_date_format, $g_show_view,
$g_bugnote_order, $g_path,
$g_email_separator1, $g_email_padding_length;
$c_bug_id = db_prepare_int( $p_bug_id );
$query = "SELECT *, UNIX_TIMESTAMP(date_submitted) as date_submitted,
UNIX_TIMESTAMP(last_updated) as last_updated
FROM $g_mantis_bug_table
WHERE id='$c_bug_id'
ORDER BY date_submitted $g_bugnote_order";
$result = db_query( $query );
$row = db_fetch_array( $result );
extract( $row, EXTR_PREFIX_ALL, 'v' );
$query = "SELECT *
FROM $g_mantis_bug_text_table
WHERE id='$v_bug_text_id'";
$result = db_query( $query );
$row = db_fetch_array( $result );
extract( $row, EXTR_PREFIX_ALL, 'v2' );
$query = "SELECT name
FROM $g_mantis_project_table
WHERE id='$v_project_id'";
$result = db_query( $query );
$t_project_name = db_result( $result, 0, 0 );
$t_reporter_name = user_get_name( $v_reporter_id );
if ( 0 != $v_handler_id ) {
$t_handler_name = user_get_name( $v_handler_id );
} else {
$t_handler_name = '';
}
$v_date_submitted = date( $g_complete_date_format, $v_date_submitted );
$v_last_updated = date( $g_complete_date_format, $v_last_updated );
$t_sev_str = get_enum_element( 'severity', $v_severity );
$t_pri_str = get_enum_element( 'priority', $v_priority );
$t_sta_str = get_enum_element( 'status', $v_status );
$t_rep_str = get_enum_element( 'reproducibility', $v_reproducibility );
$t_message = $g_email_separator1."\n";
if ( $p_message != lang_get( 'email_bug_deleted_msg' ) ) {
$t_message .= $g_path;
$t_message .= string_get_bug_view_url( $p_bug_id ) . "\n";
$t_message .= $g_email_separator1."\n";
}
$t_message .= str_pad( lang_get( 'email_reporter' ) . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$t_reporter_name."\n";
$t_message .= str_pad( lang_get( 'email_handler' ) . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$t_handler_name."\n";
$t_message .= $g_email_separator1."\n";
$t_message .= str_pad( lang_get( 'email_project' ) . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$t_project_name."\n";
$t_message .= str_pad( lang_get( 'email_bug' ) . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$v_id."\n";
$t_message .= str_pad( lang_get( 'email_category') . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$v_category."\n";
$t_message .= str_pad( lang_get( 'email_reproducibility' ) . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$t_rep_str."\n";
$t_message .= str_pad( lang_get( 'email_severity' ) . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$t_sev_str."\n";
$t_message .= str_pad( lang_get( 'email_priority' ) . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$t_pri_str."\n";
$t_message .= str_pad( lang_get( 'email_status' ) . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$t_sta_str."\n";
# @@@ Add support for access levels, possible only send the ones that are available for access level EVERYBODY
$t_related_custom_field_ids = custom_field_get_linked_ids( $v_project_id );
foreach( $t_related_custom_field_ids as $t_id ) {
$t_def = custom_field_get_definition( $t_id );
$t_message .= str_pad( $t_def['name'] . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT );
$t_custom_field_value = custom_field_get_value( $t_id, $p_bug_id );
if( CUSTOM_FIELD_TYPE_EMAIL == $t_def['type'] ) {
$t_message .= "mailto:$t_custom_field_value";
} else {
$t_message .= $t_custom_field_value;
}
$t_message .= "\n";
} // foreach
if ( RESOLVED == $v_status ) {
$t_res_str = get_enum_element( 'resolution', $v_resolution );
$t_message .= str_pad( lang_get( 'email_resolution' ) . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$t_res_str."\n";
if ( DUPLICATE == $v_resolution ) {
$t_message .= str_pad( lang_get( 'email_duplicate' ) . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$v_duplicate_id."\n";
}
}
$t_message .= $g_email_separator1."\n";
$t_message .= str_pad( lang_get( 'email_date_submitted' ) . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$v_date_submitted."\n";
$t_message .= str_pad( lang_get( 'email_last_modified' ) . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$v_last_updated."\n";
$t_message .= $g_email_separator1."\n";
$t_message .= str_pad( lang_get( 'email_summary' ) . ': ', ' ', $g_email_padding_length, STR_PAD_RIGHT ).$v_summary."\n";
$t_message .= lang_get( 'email_description' ) . ": \n".wordwrap( $v2_description )."\n";
$t_message .= $g_email_separator1."\n\n";
$p_category = '';
if ( OFF != config_get( 'email_set_category' ) ) {
$p_category = '[' . $t_project_name . '] ' . $v_category;
}
return $t_message;
}
# --------------------
# Build the bugnotes part of the message
function email_build_bugnote_message( $p_bug_id ) {
global $g_mantis_bugnote_table, $g_mantis_bugnote_text_table,
$g_complete_date_format,
$g_bugnote_order, $g_email_separator2;
$c_bug_id = (integer)$p_bug_id;
$t_message = '';
$t_state = PUBLIC;
$query = "SELECT *, UNIX_TIMESTAMP(last_modified) as last_modified
FROM $g_mantis_bugnote_table
WHERE bug_id='$c_bug_id' AND view_state='$t_state'
ORDER BY date_submitted $g_bugnote_order";
$result = db_query( $query );
$bugnote_count = db_num_rows( $result );
# BUILT MESSAGE
for ( $i=0; $i<$bugnote_count; $i++ ) {
$row = db_fetch_array( $result );
extract( $row, EXTR_PREFIX_ALL, 't' );
$query = "SELECT note
FROM $g_mantis_bugnote_text_table
WHERE id='$t_bugnote_text_id'";
$result2 = db_query( $query );
$t_username = user_get_name( $t_reporter_id );
$t_note = db_result( $result2, 0, 0 );
$t_last_modified = date( $g_complete_date_format, $t_last_modified );
$t_string = ' '.$t_username.' - '.$t_last_modified.' ';
$t_message = $t_message.$g_email_separator2."\n";
$t_message = $t_message.$t_string."\n";
$t_message = $t_message.$g_email_separator2."\n";
$t_message = $t_message.wordwrap( $t_note )."\n\n";
}
return $t_message;
}
# --------------------
# Builds the bug history portion of the bug e-mail
function email_build_history_message( $p_bug_id ) {
$history = history_get_events_array( $p_bug_id );
$t_message = lang_get( 'bug_history' ) . "\n";
$t_message .= str_pad( lang_get( 'date_modified' ), 20 ) .
str_pad( lang_get( 'username' ), 20 ) .
str_pad( lang_get( 'field' ), 20 ) .
str_pad( lang_get( 'change' ), 20 ). "\n";
$t_message .= config_get( 'email_separator1' ) . "\n";
for ( $i = 0; $i < count($history); $i++ ) {
$t_message .= str_pad( $history[$i]['date'], 20 ) .
str_pad( $history[$i]['username'], 20 ) .
str_pad( $history[$i]['note'], 20 ) .
str_pad( $history[$i]['change'], 20 ). "\n";
}
$t_message .= config_get( 'email_separator1' ) . "\n\n";
return ( $t_message );
}
# --------------------
# Send bug info to reporter and handler
function email_bug_info( $p_bug_id, $p_message, $p_headers='' ) {
global $g_to_email, $g_use_bcc;
# build subject
$t_subject = email_build_subject( $p_bug_id );
# build message
$t_category = '';
$t_message = $p_message."\n";
$t_message .= email_build_bug_message( $p_bug_id, $p_message, $t_category );
$t_message .= email_build_bugnote_message( $p_bug_id );
$t_message .= email_build_history_message( $p_bug_id );
# send mail
## win-bcc-bug
if ( OFF == $g_use_bcc ) {
## list of receivers
$to = $g_to_email.( ( is_blank( $p_headers ) || is_blank( $g_to_email ) ) ? '' : ', ').$p_headers;
# echo '<br />email_bug_info::Sending email to :'.$to;
$res1 = email_send( $to, $t_subject, $t_message, '', $t_category );
} else {
# Send Email
# echo '<br />email_bug_info::Sending email to : '.$g_to_email;
$res1 = email_send( $g_to_email, $t_subject, $t_message, $p_headers, $t_category );
}
}
# --------------------
# this function sends the actual email
function email_send( $p_recipient, $p_subject, $p_message, $p_header='', $p_category='' ) {
global $g_from_email, $g_enable_email_notification,
$g_return_path_email, $g_use_x_priority,
$g_use_phpMailer, $g_phpMailer_method, $g_smtp_host,
$g_smtp_username, $g_smtp_password, $g_mail_priority;
# short-circuit if no emails should be sent
if ( OFF ==$g_enable_email_notification ) {
return;
}
$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
if ( is_blank( $p_recipient ) && ( OFF == config_get('use_bcc') ) ) {
return;
}
# for debugging only
#echo $t_recipient.'<br />'.$t_subject.'<br />'.$t_message.'<br />'.$t_headers;
#exit;
#echo '<br />xxxRecipient ='.$t_recipient.'<br />';
#echo 'Headers ='.nl2br($t_headers).'<br />';
#echo $t_subject.'<br />';
#echo nl2br($t_message).'<br />';
#exit;
$t_debug_email = config_get('debug_email');
if ( ON == $g_use_phpMailer ) {
# Visit http://phpmailer.sourceforge.net
# if you have problems with phpMailer
include_once('class.phpmailer.php');
$mail = new phpmailer;
# Select the method to send mail
switch ( $g_phpMailer_method ) {
case 0: $mail->IsMail();
break;
case 1: $mail->IsSendmail();
break;
case 2: $mail->IsSMTP();
break;
}
$mail->IsHTML(false); # set email format to plain text
$mail->WordWrap = 80; # set word wrap to 50 characters
$mail->Priority = 0; # Urgent = 1, Not Urgent = 5, Disable = 0
$mail->CharSet = lang_get( 'charset' );
$mail->Host = $g_smtp_host;
$mail->From = $g_from_email;
$mail->FromName = '';
if ( ! is_blank( $g_smtp_username ) ) { # Use SMTP Authentication
$mail->SMTPAuth = true;
$mail->Username = $g_smtp_username;
$mail->Password = $g_smtp_password;
}
$t_debug_to = '';
# add to the Recipient list
$t_recipient_list = split(',', $t_recipient);
while ( list( , $t_recipient ) = each( $t_recipient_list ) ) {
if ( !is_blank( $t_recipient ) ) {
if ( OFF === $t_debug_email ) {
$mail->AddAddress( $t_recipient, '' );
} else {
$t_debug_to .= !is_blank( $t_debug_to ) ? ', ' : '';
$t_debug_to .= $t_recipient;
}
}
}
# add to the BCC list
$t_debug_bcc = '';
$t_bcc_list = split(',', $p_header);
while(list(, $t_bcc) = each($t_bcc_list)) {
if ( !is_blank( $t_bcc ) ) {
if ( OFF === $t_debug_email ) {
$mail->AddBCC( $t_bcc, '' );
} else {
$t_debug_bcc .= !is_blank( $t_debug_bcc ) ? ', ' : '';
$t_debug_bcc .= $t_bcc;
}
}
}
if ( OFF !== $t_debug_email )
{
$t_message = "\n" . $t_message;
if ( !is_blank( $t_debug_bcc ) ) {
$t_message = 'Bcc: ' . $t_debug_bcc . "\n" . $t_message;
}
if ( !is_blank( $t_debug_to ) ) {
$t_message = 'To: '. $t_debug_to . "\n" . $t_message;
}
$mail->AddAddress( $t_debug_email, '' );
}
$mail->Subject = $t_subject;
$mail->Body = make_lf_crlf( "\n".$t_message );
if ( EMAIL_CATEGORY_PROJECT_CATEGORY == config_get( 'email_set_category' ) ) {
$mail->AddCustomHeader( "Keywords: $p_category" );
}
if( !$mail->Send() ) {
PRINT "PROBLEMS SENDING MAIL TO: $t_recipient<br />";
PRINT 'Mailer Error: '.$mail->ErrorInfo.'<br />';
exit;
}
} else {
# Visit http://www.php.net/manual/function.mail.php
# if you have problems with mailing
$t_headers = "From: $g_from_email\n";
#$t_headers .= "Reply-To: $p_reply_to_email\n";
$t_headers .= "X-Sender: <$g_from_email>\n";
$t_headers .= 'X-Mailer: PHP/'.phpversion()."\n";
if ( ON == $g_use_x_priority ) {
$t_headers .= "X-Priority: $g_mail_priority\n"; # Urgent = 1, Not Urgent = 5, Disable = 0
}
$t_headers .= "Return-Path: <$g_return_path_email>\n"; # return email if error
$t_headers .= 'Content-Type: text/plain; charset=' . lang_get( 'charset' ) . "\n";
if ( EMAIL_CATEGORY_PROJECT_CATEGORY == config_get( 'email_set_category' ) ) {
$t_headers = "Keywords: $p_category\n";
}
if ( OFF === $t_debug_email ) {
$t_headers .= $p_header;
} else {
$t_message = "To: $t_recipient\n$p_header\n\n$t_message";
$t_recipient = $t_debug_email;
}
$t_recipient = make_lf_crlf( $t_recipient );
$t_subject = make_lf_crlf( $t_subject );
$t_message = make_lf_crlf( $t_message );
$t_headers = make_lf_crlf( $t_headers );
# set the SMTP host... only used on window though
ini_set( 'SMTP', config_get( 'smtp_host', 'localhost' ) );
$result = mail( $t_recipient, $t_subject, $t_message, $t_headers );
if ( TRUE != $result ) {
PRINT "PROBLEMS SENDING MAIL TO: $t_recipient<br />";
PRINT htmlspecialchars($t_recipient).'<br />';
PRINT htmlspecialchars($t_subject).'<br />';
PRINT nl2br(htmlspecialchars($t_headers)).'<br />';
PRINT nl2br(htmlspecialchars($t_message)).'<br />';
exit;
}
}
}
# --------------------
# helper function
function get_bug_summary( $p_bug_id ) {
global $g_mantis_bug_table;
$c_bug_id = (integer)$p_bug_id;
$query = "SELECT summary
FROM $g_mantis_bug_table
WHERE id='$c_bug_id'";
$result = db_query( $query );
return db_result( $result, 0, 0 );
}
# --------------------
# formats the subject correctly
# we include the project name, bug id, and summary.
function email_build_subject( $p_bug_id ) {
# grab the project name
$p_project_name = project_get_field( bug_get_field( $p_bug_id, 'project_id' ), 'name' );
# grab the subject (summary)
$p_subject = get_bug_summary( $p_bug_id );
# padd the bug id with zeros
$p_bug_id = bug_format_id( $p_bug_id );
return '['.$p_project_name.' '.$p_bug_id.']: '.$p_subject;
}
# --------------------
# clean up LF to CRLF
function make_lf_crlf( $p_string ) {
$t_string = str_replace( "\n", "\r\n", $p_string );
return str_replace( "\r\r\n", "\r\n", $t_string );
}
# --------------------
# Check limit_email_domain option and append the domain name if it is set
function email_append_domain( $p_email ) {
$t_limit_email_domain = config_get( 'limit_email_domain' );
if ( $t_limit_email_domain && !is_blank( $p_email ) ) {
$p_email = "$p_email@$t_limit_email_domain";
}
return $p_email;
}
# --------------------
# Send a bug reminder to each of the given user, or to each user if the first
# parameter is an array
# return an array of usernames to which the reminder was successfully sent
#
# @@@ I'm not sure this shouldn't return an array of user ids... more work for
# the caller but cleaner from an API point of view.
function email_bug_reminder( $p_recipients, $p_bug_id, $p_message ) {
if ( ! is_array( $p_recipients ) ) {
$p_recipients = array( $p_recipients );
}
$t_subject = email_build_subject( $p_bug_id );
$t_sender = current_user_get_field( 'username' ) . ' <' .
current_user_get_field( 'email' ) . '>' ;
$t_date = date( config_get( 'normal_date_format' ) );
$t_header = "\n" . lang_get( 'on' ) . " $t_date, $t_sender " .
lang_get( 'sent_you_this_reminder_about' ) . ":\n\n" .
config_get( 'path' );
$result = array();
foreach ( $p_recipients as $t_recipient ) {
$t_email = user_get_email( $t_recipient );
$result[] = user_get_name( $t_recipient );
$t_contents = $t_header .
string_get_bug_view_url( $p_bug_id, $t_recipient ) .
"\n\n$p_message";
email_send( $t_email, $t_subject, $t_contents );
}
return $result;
}
?>