forked from mantisbt-plugins/EmailReporting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailReporting.php
527 lines (413 loc) · 14.6 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
<?php
require_once( config_get( 'class_path' ) . 'MantisPlugin.class.php' );
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',
);
$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(
'config_version' => 0,
'schema' => -1,
'path_erp' => config_get_global( 'plugin_path' ) . plugin_get_current() . '/',
'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,
# 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,
# 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',
# Delete incoming mail from POP3 server
'mail_delete' => ON,
# Should users always receive emails on actions they performed by email even though email_receive_own is OFF
'mail_email_receive_own' => OFF,
# Need to set the character encoding to which the email will be converted
# This should be the same as the character encoding used in the database system used for mantis
# values should be acceptable to the following function: http://www.php.net/mb_convert_encoding
'mail_encoding' => 'UTF-8',
# Enable fallback to mail reporter
'mail_fallback_mail_reporter' => ON,
# How many mails should be fetched at the same time
# If big mails with attachments should be received, specify only one
'mail_fetch_max' => 1,
# 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 username creations
'mail_preferred_username' => 'name',
# Preferred username for new username 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 name 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,
# Remove leading re: and fwd: occurrences from the subject before using it as issue summary
'mail_subject_remove_re_fwd' => OFF,
# Do you want to secure the EmailReporting script so that it cannot be run
# via a webserver?
'mail_secured_script' => ON,
# Which regex should be used for finding the issue id in the subject
'mail_subject_id_regex' => 'strict',
# If no issue id is found, try to match the subject to an issue summary
'mail_subject_summary_match' => OFF,
# 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,
);
}
/**
* 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' )
{
# We need to allow blank emails for a sec
config_set_cache( 'allow_blank_email', ON, CONFIG_TYPE_STRING );
config_set_global( '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, REPORTER, 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 );
plugin_config_set( 'config_version', 8 );
}
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,
);
}
/**
* 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 if you change anything to the config_version value that you
* also change the update the value in install
*/
function ERP_update_check( )
{
$t_config_version = plugin_config_get( 'config_version' );
if ( $t_config_version === 0 )
{
$t_reset_schema = plugin_config_get( 'reset_schema', 0 );
if ( $t_reset_schema === 1 )
{
plugin_config_delete( 'reset_schema' );
}
else
{
$t_username = plugin_config_get( 'mail_reporter' );
$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' )
{
# We need to allow blank emails for a sec
config_set_cache( 'allow_blank_email', ON, CONFIG_TYPE_STRING );
config_set_global( 'allow_blank_email', ON );
user_set_email( $t_user_id, '' );
}
}
plugin_config_set( 'schema', -1 );
}
plugin_config_set( 'config_version', 1 );
}
if ( $t_config_version <= 1 )
{
$t_mail_debug_directory = plugin_config_get( 'mail_debug_directory' );
$t_mail_directory = plugin_config_get( 'mail_directory', $t_mail_debug_directory );
$t_mail_reporter = plugin_config_get( 'mail_reporter' );
if ( $t_mail_directory !== $t_mail_debug_directory )
{
plugin_config_set( 'mail_debug_directory', $t_mail_directory );
}
$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' );
foreach ( $t_mailboxes AS $t_key => $t_value )
{
# Correct the hostname if it is stored in an older format
$t_hostname = $t_value[ 'mailbox_hostname' ];
if ( !is_array( $t_hostname ) )
{
$t_hostname = explode( ':', $t_hostname, 2 );
$t_hostname = array(
'hostname' => $t_hostname[ 0 ],
'port' => ( ( isset( $t_hostname[ 1 ] ) ) ? $t_hostname[ 1 ] : '' ),
);
$t_value[ 'mailbox_hostname' ] = $t_hostname;
}
# Correct index mailbox_project --> mailbox_project_id
if ( isset( $t_value[ 'mailbox_project' ] ) )
{
$t_value[ 'mailbox_project_id' ] = $t_value[ 'mailbox_project' ];
unset( $t_value[ 'mailbox_project' ] );
}
# Correct index mailbox_global_category --> mailbox_global_category_id
if ( isset( $t_value[ 'mailbox_global_category' ] ) )
{
$t_value[ 'mailbox_global_category_id' ] = $t_value[ 'mailbox_global_category' ];
unset( $t_value[ 'mailbox_global_category' ] );
}
$t_mailboxes[ $t_key ] = $t_value;
}
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' );
$t_mail_identify_reply = plugin_config_get( 'mail_identify_reply', $t_mail_remove_mantis_email );
if ( $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' );
foreach ( $t_mailboxes AS $t_key => $t_array )
{
foreach ( $t_array AS $t_index => $t_value )
{
if ( substr( $t_index, 0, 8 ) === 'mailbox_' )
{
$t_new_index = substr( $t_index, 8 );
$t_array[ $t_new_index ] = $t_value;
unset( $t_array[ $t_index ] );
}
}
$t_mailboxes[ $t_key ] = $t_array;
}
plugin_config_set( 'mailboxes', $t_mailboxes );
plugin_config_set( 'config_version', 7 );
}
if ( $t_config_version <= 7 )
{
$t_mailboxes = plugin_config_get( 'mailboxes' );
foreach ( $t_mailboxes AS $t_key => $t_value )
{
$t_hostname = $t_value[ 'hostname' ];
if ( is_array( $t_hostname ) )
{
$t_value[ 'hostname' ] = $t_hostname[ 'hostname' ];
$t_value[ 'port' ] = $t_hostname[ 'port' ];
}
$t_mailboxes[ $t_key ] = $t_value;
}
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' );
$t_indexes = array(
'type' => 'mailbox_type',
'basefolder' => 'imap_basefolder',
'createfolderstructure' => 'imap_createfolderstructure',
);
foreach ( $t_mailboxes AS $t_key => $t_array )
{
foreach ( $t_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 ] );
}
}
$t_mailboxes[ $t_key ] = $t_array;
}
plugin_config_set( 'mailboxes', $t_mailboxes );
plugin_config_set( 'config_version', 10 );
}
}
/*
* 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 );
}
}
}
}