forked from mantisbt-plugins/EmailReporting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailReporting.php
683 lines (546 loc) · 18.9 KB
/
EmailReporting.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
<?php
// Needed for MantisBT 1.2.x.
// Not needed for MantisBT 1.3.x
require_once( config_get( 'class_path' ) . 'MantisPlugin.class.php' );
// New function in MantisBT 1.3.x
// Included for compatibility with 1.2.x
if ( !function_exists( 'plugin_require_api' ) )
{
/**
* Allows a plugin page to require a plugin-specific API
* @param string $p_file The API to be included.
* @param string $p_basename Plugin's basename (defaults to current plugin).
* @return void
*/
function plugin_require_api( $p_file, $p_basename = null ) {
if( is_null( $p_basename ) ) {
$t_current = plugin_get_current();
} else {
$t_current = $p_basename;
}
$t_path = config_get_global( 'plugin_path' ) . $t_current . '/';
require_once( $t_path . $p_file );
}
}
// New function in MantisBT 1.3.x
// Included for compatibility with 1.2.x
if ( !function_exists( 'require_api' ) )
{
/**
* Define an API inclusion function to replace require_once
*
* @param string $p_api_name An API file name.
* @return void
*/
function require_api( $p_api_name ) {
static $s_api_included;
global $g_core_path;
if( !isset( $s_api_included[$p_api_name] ) ) {
require_once( $g_core_path . $p_api_name );
$t_new_globals = array_diff_key( get_defined_vars(), $GLOBALS, array( 't_new_globals' => 0 ) );
foreach ( $t_new_globals as $t_global_name => $t_global_value ) {
$GLOBALS[$t_global_name] = $t_global_value;
}
$s_api_included[$p_api_name] = 1;
}
}
}
class EmailReportingPlugin extends MantisPlugin
{
/**
* A method that populates the plugin information and minimum requirements.
*/
function register()
{
$this->name = plugin_lang_get( 'plugin_title' );
$this->description = plugin_lang_get( 'plugin_description' );
$this->page = 'manage_config';
$this->version = '0.9.0-DEV';
$this->requires = array(
'MantisCore' => '1.2.6, <1.3.99',
);
$this->author = plugin_lang_get( 'plugin_author' );
$this->contact = '';
$this->url = 'http://www.mantisbt.org/wiki/doku.php/mantisbt:emailreporting';
}
/**
* EmailReporting plugin configuration.
*/
function config()
{
return array(
'reset_schema' => 0,
'config_version' => 0,
'schema' => -1,
'job_users' => array(),
# --- mail reporting settings -----
# Empty default mailboxes array. This array will be used for all the mailbox
# accounts
'mailboxes' => array(),
# Empty default rules array. This array will be used for all the rules
'rules' => array(),
# Is this plugin allowed to process and create new bug reports
'mail_add_bug_reports' => ON,
# Is this plugin allowed to process and add notes to existing issues
'mail_add_bugnotes' => ON,
# Add complete email into the attachments
'mail_add_complete_email' => OFF,
// Add users from Cc and To field in mail header
'mail_add_users_from_cc_to' => OFF,
# Signup new users automatically (possible security risk!)
# Default is OFF, if mail_use_reporter is OFF and this is OFF then it will
# fallback to the mail_reporter account above
'mail_auto_signup' => OFF,
# List of md5 hashes of attachments which will be blocked.
'mail_block_attachments_md5' => array(),
# Log blocked attachments in the rejected files list
'mail_block_attachments_logging'=> ON,
# Classify bug priorities
'mail_bug_priority' => array(
'5 (lowest)' => '10',
'4 (low)' => '20',
'3 (normal)' => '30',
'2 (high)' => '40',
'1 (highest)' => '50',
5 => '20',
4 => '20',
3 => '30',
2 => '40',
1 => '50',
0 => '10',
'low' => '20',
'normal' => '30',
'high' => '40',
'' => '30',
'?' => '30',
),
# Used for debugging the system.
# Use with care
'mail_debug' => OFF,
# Save mail contents to this directory if debug mode is ON
'mail_debug_directory' => '/tmp/mantis',
# Used for debugging the system.
# Shows the memory usage in different stages of the debugging process
'mail_debug_show_memory_usage' => OFF,
# Delete incoming mail from POP3 server
'mail_delete' => ON,
# MantisBT always has the disposble email checker enabled. We needed an option to disable this in EmailReporting
'mail_disposable_email_checker' => ON,
# Should users always receive emails on actions they performed by email even though email_receive_own is OFF
'mail_email_receive_own' => OFF,
# Enable fallback to mail reporter
'mail_fallback_mail_reporter' => ON,
# Use the following text when the description is missing from the email
'mail_nodescription' => 'No description found',
# Use the following text when the subject is missing from the email
'mail_nosubject' => 'No subject found',
# Parse HTML mails
'mail_parse_html' => ON,
# Preferred username for new user creations
'mail_preferred_username' => 'name',
# Preferred realname for new user creations
'mail_preferred_realname' => 'name',
# Try to identify the original mantis email and remove it from the description
'mail_remove_mantis_email' => ON,
# Remove everything after and including the remove_replies_after text
'mail_remove_replies' => OFF,
# Text which decides after (and including) which all content needs to be removed
'mail_remove_replies_after' => '-----Original Message-----',
# Use the following text when part of the email has been removed
'mail_removed_reply_text' => '[EmailReporting -> Removed part identified as reply]',
# The account's id for mail reporting
# Also used for fallback if a user is not found in database
# Mail is just the default name which will be converted to a user id during installation
'mail_reporter_id' => 'Mail',
# Is the rule system enabled
'mail_rule_system' => OFF,
# Write the sender of the email into the issue report/note
'mail_save_from' => ON,
# Write the subject of the email in the note
'mail_save_subject_in_note' => OFF,
# Do you want to secure the EmailReporting script so that it cannot be run
# via a webserver?
'mail_secured_script' => ON,
//Strip Gmail style replies from body of the message
'mail_strip_gmail_style_replies' => OFF,
#Removes the signature that are delimited by mail_strip_signature_delim
'mail_strip_signature' => OFF,
#Removes the signature that are delimited by --
'mail_strip_signature_delim' => '--',
# Which regex should be used for finding the issue id in the subject
'mail_subject_id_regex' => 'strict',
# Looks for priority header field
'mail_use_bug_priority' => ON,
# This tells Mantis to report all the Mail with only one account
# ON = mail uses the reporter account in the setting below
# OFF = it identifies the reporter using the email address of the sender
'mail_use_reporter' => ON,
// Whether to identify notes using Message-ID in the mail header
'mail_use_message_id' => ON,
);
}
/**
* EmailReporting installation function.
*/
function install()
{
// We need to load a default value since the function config() which sets
// the defaults has not been run yet. On the other hand, configuration options
// already present in the database will be available.
$t_mail_reporter_id = plugin_config_get( 'mail_reporter_id', 'Mail' );
if ( $t_mail_reporter_id === 'Mail' )
{
// The plugin variable path_erp is not yet available. So path_erp cannot be used here
plugin_require_api( 'core/config_api.php' );
# We need to allow blank emails for a sec
ERP_set_temporary_overwrite( 'allow_blank_email', ON );
$t_rand = mt_rand( 1000, 99999 );
$t_username = $t_mail_reporter_id . $t_rand;
$t_email = '';
$t_seed = $t_email . $t_username;
# Create random password
$t_password = auth_generate_random_password( $t_seed );
# create the user
$t_result_user_create = user_create( $t_username, $t_password, $t_email, config_get_global( 'report_bug_threshold' ), FALSE, TRUE, 'Mail Reporter', plugin_lang_get( 'plugin_title' ) );
# Save these after the user has been created successfully
if ( $t_result_user_create )
{
$t_user_id = user_get_id_by_name( $t_username );
plugin_config_set( 'mail_reporter_id', $t_user_id );
}
// We need to set this here otherwise we mess up new installations with ERP_update_check
plugin_config_set( 'reset_schema', 1 );
return( $t_result_user_create );
}
return( TRUE );
}
/**
* EmailReporting uninstallation function.
*/
function uninstall()
{
# User removal from the install function will not be done
# The reason being thats its possibly connected to issues in the system
return( TRUE );
}
/**
* EmailReporting initialization function.
*/
function init()
{
$t_path = config_get_global( 'plugin_path' ) . plugin_get_current() . DIRECTORY_SEPARATOR . 'core_pear' . DIRECTORY_SEPARATOR;
if ( is_dir( $t_path ) )
{
set_include_path( get_include_path() . PATH_SEPARATOR . $t_path );
}
}
function events()
{
return array(
'EVENT_ERP_OUTPUT_MAILBOX_FIELDS' => EVENT_TYPE_OUTPUT,
// Keep in mind that this event is called BEFORE its MantisBT core counterpart
// After the MantisBT core event is not possible since that one is integrated into the bugnote_add function
'EVENT_ERP_BUGNOTE_DATA' => EVENT_TYPE_CHAIN,
// Keep in mind that this event is called AFTER its MantisBT core counterpart
'EVENT_ERP_REPORT_BUG_DATA' => EVENT_TYPE_CHAIN,
);
}
/*
* Database table to store the Message-ID to detect the replies
* This is to implement #0016719
*/
function schema()
{
return array(
array( 'CreateTableSQL', array( plugin_table( 'msgids' ), "
id I UNSIGNED NOTNULL PRIMARY AUTOINCREMENT,
issue_id I UNSIGNED NOTNULL,
msg_id C(255) NOTNULL
" )
),
array( 'CreateIndexSQL', array( 'idx_erp_msgids_msgid', plugin_table( 'msgids' ), 'msg_id', array( 'UNIQUE' ) ) ),
);
}
/**
* EmailReporting plugin hooks.
*/
function hooks( )
{
$hooks = array(
'EVENT_MENU_MANAGE' => 'ERP_manage_emailreporting_menu',
'EVENT_CORE_READY' => 'ERP_core_ready',
);
return $hooks;
}
/**
* EmailReporting plugin hooks - add emailreporting menu item.
*/
function ERP_manage_emailreporting_menu( )
{
return array( '<a href="' . plugin_page( 'manage_mailbox' ) . '">' . plugin_lang_get( 'manage' ) . ' ' . plugin_lang_get( 'plugin_title' ) . '</a>', );
}
/*
* This function will run when the mantis core is ready
*/
function ERP_core_ready( )
{
$this->ERP_update_check( );
$this->ERP_check_mantisbt_url( );
}
/*
* Since schema is not used anymore some corrections need to be applied
* Schema will be completely reset by this just once
*
* The second part updates various configuration options and performs some cleaning
* Further updates to the configuration options follow below
*
* Make sure that it is no problem if a user would delete the variable config_version
* as it would cause all the patches below to be executed all over again.
* New installations will have all of these patches executed as well.
*/
function ERP_update_check( )
{
$t_config_version = plugin_config_get( 'config_version' );
if ( $t_config_version === 0 )
{
$t_username = plugin_config_get( 'mail_reporter', '' );
if ( strlen( $t_username ) > 0 )
{
$t_user_id = user_get_id_by_name( $t_username );
if ( $t_user_id !== FALSE )
{
$t_user_email = user_get_email( $t_user_id );
if ( $t_user_email === 'nomail' )
{
plugin_require_api( 'core/config_api.php' );
# We need to allow blank emails for a sec
ERP_set_temporary_overwrite( 'allow_blank_email', ON );
user_set_email( $t_user_id, '' );
}
}
}
$t_schema = plugin_config_get( 'schema' );
$t_reset_schema = plugin_config_get( 'reset_schema' );
if ( $t_schema !== -1 && $t_reset_schema === 0 )
{
plugin_config_set( 'schema', -1 );
plugin_config_set( 'reset_schema', 1 );
}
plugin_config_set( 'config_version', 1 );
}
if ( $t_config_version <= 1 )
{
$t_mail_reporter = plugin_config_get( 'mail_reporter', '' );
if ( strlen( $t_mail_reporter ) > 0 )
{
$t_mail_reporter_id = user_get_id_by_name( $t_mail_reporter );
plugin_config_set( 'mail_reporter_id', $t_mail_reporter_id );
}
plugin_config_delete( 'mail_directory' );
plugin_config_delete( 'mail_reporter' );
plugin_config_delete( 'mail_additional' );
plugin_config_delete( 'random_user_number' );
plugin_config_delete( 'mail_bug_priority_default' );
plugin_config_set( 'config_version', 2 );
}
if ( $t_config_version <= 2 )
{
plugin_config_delete( 'mail_cronjob_present' );
plugin_config_delete( 'mail_check_timer' );
plugin_config_delete( 'mail_last_check' );
plugin_config_set( 'config_version', 3 );
}
if ( $t_config_version <= 3 )
{
$t_mailboxes = plugin_config_get( 'mailboxes', array() );
$t_indexes = array(
'mailbox_project' => 'mailbox_project_id',
'mailbox_global_category' => 'mailbox_global_category_id',
);
foreach ( $t_mailboxes AS $t_key => $t_array )
{
if ( isset( $t_array[ 'mailbox_hostname' ] ) )
{
# Correct the hostname if it is stored in an older format
$t_hostname = $t_array[ 'mailbox_hostname' ];
if ( !is_array( $t_hostname ) )
{
// ipv6 also uses : so we need to work around that
if ( substr_count( $t_hostname, ':' ) === 1 )
{
$t_hostname = explode( ':', $t_hostname, 2 );
}
else
{
$t_hostname = array( $t_hostname );
}
$t_hostname = array(
'hostname' => $t_hostname[ 0 ],
'port' => ( ( isset( $t_hostname[ 1 ] ) ) ? $t_hostname[ 1 ] : '' ),
);
$t_array[ 'mailbox_hostname' ] = $t_hostname;
}
}
$t_mailboxes[ $t_key ] = $this->ERP_update_indexes( $t_array, $t_indexes );
}
plugin_config_set( 'mailboxes', $t_mailboxes );
plugin_config_set( 'config_version', 4 );
}
if ( $t_config_version <= 4 )
{
$t_mail_remove_mantis_email = plugin_config_get( 'mail_remove_mantis_email', -1 );
$t_mail_identify_reply = plugin_config_get( 'mail_identify_reply', $t_mail_remove_mantis_email );
if ( $t_mail_remove_mantis_email !== -1 && $t_mail_identify_reply !== $t_mail_remove_mantis_email )
{
plugin_config_set( 'mail_remove_mantis_email', $t_mail_identify_reply );
}
plugin_config_delete( 'mail_identify_reply' );
plugin_config_set( 'config_version', 5 );
}
if ( $t_config_version <= 5 )
{
plugin_config_delete( 'mail_parse_mime' );
plugin_config_set( 'config_version', 6 );
}
if ( $t_config_version <= 6 )
{
$t_mailboxes = plugin_config_get( 'mailboxes', array() );
$t_indexes = array(
'mailbox_enabled' => 'enabled',
'mailbox_description' => 'description',
'mailbox_type' => 'type',
'mailbox_hostname' => 'hostname',
'mailbox_encryption' => 'encryption',
'mailbox_username' => 'username',
'mailbox_password' => 'password',
'mailbox_auth_method' => 'auth_method',
'mailbox_project_id' => 'project_id',
'mailbox_global_category_id' => 'global_category_id',
'mailbox_basefolder' => 'basefolder',
'mailbox_createfolderstructure' => 'createfolderstructure',
);
foreach ( $t_mailboxes AS $t_key => $t_array )
{
$t_mailboxes[ $t_key ] = $this->ERP_update_indexes( $t_array, $t_indexes );
}
plugin_config_set( 'mailboxes', $t_mailboxes );
plugin_config_set( 'config_version', 7 );
}
if ( $t_config_version <= 7 )
{
$t_mailboxes = plugin_config_get( 'mailboxes', array() );
foreach ( $t_mailboxes AS $t_key => $t_array )
{
if ( isset( $t_array[ 'hostname' ] ) )
{
$t_hostname = $t_array[ 'hostname' ];
if ( is_array( $t_hostname ) )
{
$t_array[ 'hostname' ] = $t_hostname[ 'hostname' ];
$t_array[ 'port' ] = $t_hostname[ 'port' ];
}
$t_mailboxes[ $t_key ] = $t_array;
}
}
plugin_config_set( 'mailboxes', $t_mailboxes );
plugin_config_set( 'config_version', 8 );
}
if ( $t_config_version <= 8 )
{
plugin_config_delete( 'mail_tmp_directory' );
plugin_config_set( 'config_version', 9 );
}
if ( $t_config_version <= 9 )
{
$t_mailboxes = plugin_config_get( 'mailboxes', array() );
$t_indexes = array(
'type' => 'mailbox_type',
'basefolder' => 'imap_basefolder',
'createfolderstructure' => 'imap_createfolderstructure',
);
foreach ( $t_mailboxes AS $t_key => $t_array )
{
$t_mailboxes[ $t_key ] = $this->ERP_update_indexes( $t_array, $t_indexes );
}
plugin_config_set( 'mailboxes', $t_mailboxes );
plugin_config_set( 'config_version', 10 );
}
if ( $t_config_version <= 10 )
{
plugin_config_delete( 'mail_rule_system' );
plugin_config_set( 'config_version', 11 );
}
if ( $t_config_version <= 11 )
{
$t_mailboxes = plugin_config_get( 'mailboxes', array() );
$t_indexes = array(
'username' => 'erp_username',
'password' => 'erp_password',
);
foreach ( $t_mailboxes AS $t_key => $t_array )
{
$t_mailboxes[ $t_key ] = $this->ERP_update_indexes( $t_array, $t_indexes );
}
plugin_config_set( 'mailboxes', $t_mailboxes );
plugin_config_delete( 'rules' );
plugin_config_delete( 'mail_encoding' );
plugin_config_set( 'config_version', 12 );
}
if ( $t_config_version <= 12 )
{
plugin_config_set( 'reset_schema', 1 );
plugin_config_set( 'config_version', 13 );
}
if ( $t_config_version <= 13 )
{
plugin_config_delete( 'mail_fetch_max' );
plugin_config_set( 'config_version', 14 );
}
if ( $t_config_version <= 14 )
{
$t_mail_reporter_id = plugin_config_get( 'mail_reporter_id', 'Mail' );
$t_report_bug_threshold = config_get_global( 'report_bug_threshold' );
if ( $t_mail_reporter_id !== 'Mail' && user_exists( $t_mail_reporter_id ) )
{
if ( !access_has_global_level( $t_report_bug_threshold, $t_mail_reporter_id ) )
{
user_set_field( $t_mail_reporter_id, 'access_level', $t_report_bug_threshold );
}
}
plugin_config_set( 'config_version', 15 );
}
}
/*
* Modifies indexes in an array based on given array
*/
function ERP_update_indexes( $p_array, $p_indexes )
{
$t_array = $p_array;
foreach ( $p_indexes AS $t_old_index => $t_new_index )
{
if ( isset( $t_array[ $t_old_index ] ) )
{
$t_array[ $t_new_index ] = $t_array[ $t_old_index ];
unset( $t_array[ $t_old_index ] );
}
}
return( $t_array );
}
/*
* Prepare mantisbt variable for use while bug_report_mail is running
* This variable fixes the problem where when EmailReporting sends emails
* that the url in the emails is incorrect
*/
function ERP_check_mantisbt_url( )
{
if ( php_sapi_name() !== 'cli' && !isset( $GLOBALS[ 't_dir_emailreporting_adjust' ] ) )
{
$t_path = config_get_global( 'path' );
$t_mail_mantisbt_url_fix = plugin_config_get( 'mail_mantisbt_url_fix', '' );
if ( strncasecmp( $t_path, 'http', 4 ) === 0 && $t_path !== $t_mail_mantisbt_url_fix )
{
plugin_config_set( 'mail_mantisbt_url_fix', $t_path );
}
}
}
}