From bd81f8e1ade46b4ccfaba11a85090f1a3ec0f18b Mon Sep 17 00:00:00 2001 From: SL-Gundam Date: Wed, 29 Jan 2014 21:26:59 +0100 Subject: [PATCH] Fixed some losts changes caused by the recent merge Some minor enhancements to previous merge Fixed indentations --- EmailReporting.php | 12 +- core/Mail/Parser.php | 30 ++--- core/mail_api.php | 241 ++++++++++++++++------------------- pages/manage_config_edit.php | 2 +- 4 files changed, 134 insertions(+), 151 deletions(-) diff --git a/EmailReporting.php b/EmailReporting.php index efcbbb9..c725f75 100644 --- a/EmailReporting.php +++ b/EmailReporting.php @@ -51,6 +51,9 @@ function config() # 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 @@ -154,6 +157,9 @@ function config() # 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, @@ -170,12 +176,6 @@ function config() # 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, - - //Strip Gmail style replies from body of the message - 'mail_strip_gmail_style_replies' => OFF, - - // Add users from Cc and To field in mail header - 'mail_add_users_from_cc_to' => OFF, ); } diff --git a/core/Mail/Parser.php b/core/Mail/Parser.php index 83b7851..e1c3c4e 100644 --- a/core/Mail/Parser.php +++ b/core/Mail/Parser.php @@ -267,8 +267,8 @@ private function parseStructure( &$structure ) $this->setFrom( $structure->headers['from'] ); $this->setSubject( $structure->headers['subject'] ); - if ( isset( $structure->headers['x-priority'] ) ) - { + if ( isset( $structure->headers['x-priority'] ) ) + { $this->setPriority( $structure->headers['x-priority'] ); } @@ -289,9 +289,9 @@ private function parseStructure( &$structure ) } $this->setTo( $structure->headers['to'] ); - - if ( isset( $structure->headers['cc'] ) ) - { + + if ( isset( $structure->headers['cc'] ) ) + { $this->setCc( $structure->headers['cc'] ); } } @@ -308,20 +308,20 @@ private function setSubject( $subject ) private function setTo( $p_to ) { - $regex = '([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})'; - if( preg_match_all ("/".$regex."/is", $p_to, $matches) ) - { - $this->_to = $matches[1]; - } + $regex = '([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})'; + if( preg_match_all ( "/" . $regex . "/is", $p_to, $matches ) ) + { + $this->_to = $matches[1]; + } } private function setCc( $p_cc ) { - $regex = '([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})'; - if( preg_match_all ("/".$regex."/is", $p_cc, $matches) ) - { - $this->_cc = $matches[1]; - } + $regex = '([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})'; + if( preg_match_all ( "/" . $regex . "/is", $p_cc, $matches ) ) + { + $this->_cc = $matches[1]; + } } private function setPriority( $priority ) diff --git a/core/mail_api.php b/core/mail_api.php index fdf0303..1bfde63 100644 --- a/core/mail_api.php +++ b/core/mail_api.php @@ -43,6 +43,7 @@ class ERP_mailbox_api private $_mail_add_bug_reports; private $_mail_add_bugnotes; private $_mail_add_complete_email; + private $_mail_add_users_from_cc_to; private $_mail_auto_signup; private $_mail_block_attachments_md5; private $_mail_block_attachments_logging; @@ -61,17 +62,16 @@ class ERP_mailbox_api private $_mail_remove_mantis_email; private $_mail_remove_replies; private $_mail_remove_replies_after; - private $_mail_strip_gmail_style_replies; private $_mail_removed_reply_text; private $_mail_reporter_id; private $_mail_save_from; private $_mail_save_subject_in_note; + private $_mail_strip_gmail_style_replies; private $_mail_strip_signature; private $_mail_strip_signature_delim; private $_mail_subject_id_regex; private $_mail_use_bug_priority; private $_mail_use_reporter; - private $_mail_add_users_from_cc_to; private $_mp_options = array(); @@ -93,6 +93,7 @@ public function __construct( $p_test_only = FALSE ) $this->_mail_add_bug_reports = plugin_config_get( 'mail_add_bug_reports' ); $this->_mail_add_bugnotes = plugin_config_get( 'mail_add_bugnotes' ); $this->_mail_add_complete_email = plugin_config_get( 'mail_add_complete_email' ); + $this->_mail_add_users_from_cc_to = plugin_config_get( 'mail_add_users_from_cc_to' ); $this->_mail_auto_signup = plugin_config_get( 'mail_auto_signup' ); $this->_mail_block_attachments_md5 = plugin_config_get( 'mail_block_attachments_md5' ); $this->_mail_block_attachments_logging = plugin_config_get( 'mail_block_attachments_logging' ); @@ -111,17 +112,16 @@ public function __construct( $p_test_only = FALSE ) $this->_mail_remove_mantis_email = plugin_config_get( 'mail_remove_mantis_email' ); $this->_mail_remove_replies = plugin_config_get( 'mail_remove_replies' ); $this->_mail_remove_replies_after = plugin_config_get( 'mail_remove_replies_after' ); - $this->_mail_strip_gmail_style_replies = plugin_config_get( 'mail_strip_gmail_style_replies' ); $this->_mail_removed_reply_text = plugin_config_get( 'mail_removed_reply_text' ); $this->_mail_reporter_id = plugin_config_get( 'mail_reporter_id' ); $this->_mail_save_from = plugin_config_get( 'mail_save_from' ); $this->_mail_save_subject_in_note = plugin_config_get( 'mail_save_subject_in_note' ); + $this->_mail_strip_gmail_style_replies = plugin_config_get( 'mail_strip_gmail_style_replies' ); $this->_mail_strip_signature = plugin_config_get( 'mail_strip_signature' ); $this->_mail_strip_signature_delim = plugin_config_get( 'mail_strip_signature_delim' ); $this->_mail_subject_id_regex = plugin_config_get( 'mail_subject_id_regex' ); $this->_mail_use_bug_priority = plugin_config_get( 'mail_use_bug_priority' ); $this->_mail_use_reporter = plugin_config_get( 'mail_use_reporter' ); - $this->_mail_add_users_from_cc_to = plugin_config_get( 'mail_add_users_from_cc_to' ); $this->_mp_options[ 'add_attachments' ] = config_get( 'allow_file_upload' ); $this->_mp_options[ 'debug' ] = $this->_mail_debug; @@ -604,88 +604,88 @@ private function parse_content( &$p_msg ) # -------------------- # return the user id for the mail reporting user - private function get_user( $p_parsed_from ) - { - if ( $this->_mail_use_reporter ) - { - // Always report as mail_reporter - $t_reporter_id = $this->_mail_reporter_id; - } - else - { - // Try to get the reporting users id - $t_reporter_id = $this->get_userid_from_email( $p_parsed_from[ 'email' ] ); - - if ( !$t_reporter_id ) - { - if ( $this->_mail_auto_signup ) - { - // So, we have to sign up a new user... - $t_new_reporter_name = $this->prepare_username( $p_parsed_from ); - - if ( $t_new_reporter_name !== FALSE && $this->validate_email_address( $p_parsed_from[ 'email' ] ) ) - { - if( user_signup( $t_new_reporter_name, $p_parsed_from[ 'email' ] ) ) - { - # notify the selected group a new user has signed-up - email_notify_new_account( $t_new_reporter_name, $p_parsed_from[ 'email' ] ); - - $t_reporter_id = user_get_id_by_email( $p_parsed_from[ 'email' ] ); - $t_reporter_name = $t_new_reporter_name; - - $t_realname = $this->prepare_realname( $p_parsed_from, $t_reporter_name ); - - if ( $t_realname !== FALSE ) - { - user_set_realname( $t_reporter_id, $t_realname ); - } - } - } - - if ( !$t_reporter_id ) - { - $this->custom_error( 'Failed to create user based on: ' . $p_parsed_from[ 'From' ] ); - } - } - } - - if ( ( !$t_reporter_id || !user_is_enabled( $t_reporter_id ) ) && $this->_mail_fallback_mail_reporter ) - { - // Fall back to the default mail_reporter - $t_reporter_id = $this->_mail_reporter_id; - } - } - - if ( $t_reporter_id && user_is_enabled( $t_reporter_id ) ) - { - if ( !isset( $t_reporter_name ) ) - { - $t_reporter_name = user_get_field( $t_reporter_id, 'username' ); - } - - $t_authattemptresult = auth_attempt_script_login( $t_reporter_name ); - - # last attempt for fallback - if ( $t_authattemptresult === FALSE && $this->_mail_fallback_mail_reporter && $t_reporter_id != $this->_mail_reporter_id && user_is_enabled( $this->_mail_reporter_id ) ) - { - $t_reporter_id = $this->_mail_reporter_id; - $t_reporter_name = user_get_field( $t_reporter_id, 'username' ); - $t_authattemptresult = auth_attempt_script_login( $t_reporter_name ); - } - - if ( $t_authattemptresult === TRUE ) - { - user_update_last_visit( $t_reporter_id ); - - return( (int) $t_reporter_id ); - } - } - - // Normally this function does not get here unless all else failed - $this->custom_error( 'Could not get a valid reporter. Email will be ignored' ); - - return( FALSE ); - } + private function get_user( $p_parsed_from ) + { + if ( $this->_mail_use_reporter ) + { + // Always report as mail_reporter + $t_reporter_id = $this->_mail_reporter_id; + } + else + { + // Try to get the reporting users id + $t_reporter_id = $this->get_userid_from_email( $p_parsed_from[ 'email' ] ); + + if ( !$t_reporter_id ) + { + if ( $this->_mail_auto_signup ) + { + // So, we have to sign up a new user... + $t_new_reporter_name = $this->prepare_username( $p_parsed_from ); + + if ( $t_new_reporter_name !== FALSE && $this->validate_email_address( $p_parsed_from[ 'email' ] ) ) + { + if( user_signup( $t_new_reporter_name, $p_parsed_from[ 'email' ] ) ) + { + # notify the selected group a new user has signed-up + email_notify_new_account( $t_new_reporter_name, $p_parsed_from[ 'email' ] ); + + $t_reporter_id = user_get_id_by_email( $p_parsed_from[ 'email' ] ); + $t_reporter_name = $t_new_reporter_name; + + $t_realname = $this->prepare_realname( $p_parsed_from, $t_reporter_name ); + + if ( $t_realname !== FALSE ) + { + user_set_realname( $t_reporter_id, $t_realname ); + } + } + } + + if ( !$t_reporter_id ) + { + $this->custom_error( 'Failed to create user based on: ' . $p_parsed_from[ 'From' ] ); + } + } + } + + if ( ( !$t_reporter_id || !user_is_enabled( $t_reporter_id ) ) && $this->_mail_fallback_mail_reporter ) + { + // Fall back to the default mail_reporter + $t_reporter_id = $this->_mail_reporter_id; + } + } + + if ( $t_reporter_id && user_is_enabled( $t_reporter_id ) ) + { + if ( !isset( $t_reporter_name ) ) + { + $t_reporter_name = user_get_field( $t_reporter_id, 'username' ); + } + + $t_authattemptresult = auth_attempt_script_login( $t_reporter_name ); + + # last attempt for fallback + if ( $t_authattemptresult === FALSE && $this->_mail_fallback_mail_reporter && $t_reporter_id != $this->_mail_reporter_id && user_is_enabled( $this->_mail_reporter_id ) ) + { + $t_reporter_id = $this->_mail_reporter_id; + $t_reporter_name = user_get_field( $t_reporter_id, 'username' ); + $t_authattemptresult = auth_attempt_script_login( $t_reporter_name ); + } + + if ( $t_authattemptresult === TRUE ) + { + user_update_last_visit( $t_reporter_id ); + + return( (int) $t_reporter_id ); + } + } + + // Normally this function does not get here unless all else failed + $this->custom_error( 'Could not get a valid reporter. Email will be ignored' ); + + return( FALSE ); + } # -------------------- # Try to obtain an existing userid based on an email address @@ -695,7 +695,7 @@ private function get_userid_from_email( $p_email_address ) if ( $this->_use_ldap_email ) { - $t_username = ldap_get_username_from_email( $p_email_address ); + $t_username = $this->ldap_get_username_from_email( $p_email_address ); if ( $t_username !== NULL && user_is_name_valid( $t_username ) ) { @@ -750,7 +750,7 @@ private function add_bug( &$p_email, $p_overwrite_project_id = FALSE ) # Reopen issue and add a bug note bug_reopen( $t_bug_id, $t_description ); } - else + elseif ( !is_blank( $t_description ) ) { # Add a bug note bugnote_add( $t_bug_id, $t_description ); @@ -928,8 +928,8 @@ private function add_bug( &$p_email, $p_overwrite_project_id = FALSE ) } } - //Add the users in Cc and To list in mail header - $this->add_monitors( $t_bug_id, $p_email ); + //Add the users in Cc and To list in mail header + $this->add_monitors( $t_bug_id, $p_email ); ERP_set_temporary_overwrite( 'project_override', NULL ); @@ -1119,7 +1119,7 @@ private function prepare_username( $p_user_info ) break; case 'from_ldap': - $t_username = ldap_get_username_from_email( $p_user_info[ 'email' ] ); + $t_username = $this->ldap_get_username_from_email( $p_user_info[ 'email' ] ); break; case 'name': @@ -1360,7 +1360,7 @@ private function strip_signature( $p_description ) if ( $this->_mail_strip_signature && strlen( trim( $this->_mail_strip_signature_delim ) ) > 1 ) { - $t_parts = preg_split( '/((?:\r|\n|\n\r)' . $this->_mail_strip_signature_delim . '\s*(?:\r|\n|\n\r))/', $t_description, -1, PREG_SPLIT_DELIM_CAPTURE ); + $t_parts = preg_split( '/((?:\r|\n|\r\n)' . $this->_mail_strip_signature_delim . '\s*(?:\r|\n|\r\n))/', $t_description, -1, PREG_SPLIT_DELIM_CAPTURE ); if ( count( $t_parts ) > 2 ) // String should not start with the delimiter so that why we need at least 3 parts { @@ -1389,46 +1389,29 @@ private function show_memory_usage( $p_location ) } // -------------------- - // Add monitors from Cc and To fields in mail header - private function add_monitors( $p_bug_id, $p_email ) - { - if ( $this->_mail_add_users_from_cc_to) - { - $t_emails = array_merge($p_email[ 'To' ], $p_email[ 'Cc' ] ); - - print_r($t_emails); - foreach( $t_emails as $t_email ) - { - $t_user_id = $this->get_userid_from_email( $t_email ); - print $t_email.": ".$t_user_id. "
" ; - if( $t_user_id !== FALSE) - { - // Make sure that mail_reporter_id and reporter_id are not added as a monitors. - if( ($this->_mail_reporter_id != $t_user_id) && ($p_email['Reporter_id'] != $t_user_id) ) - { - bug_monitor( $p_bug_id, $t_user_id ); - } - } - } - } - } - - - # -------------------- - # This function formats the bytes so that they are easily readable. - # Not part of a class - function ERP_formatbytes( $p_bytes ) + // Add monitors from Cc and To fields in mail header + private function add_monitors( $p_bug_id, $p_email ) { - $t_units = array( ' B', ' KiB', ' MiB', ' GiB', ' TiB' ); + if ( $this->_mail_add_users_from_cc_to ) + { + $t_emails = array_merge( $p_email[ 'To' ], $p_email[ 'Cc' ] ); - $t_bytes = $p_bytes; + foreach( $t_emails as $t_email ) + { + $t_user_id = $this->get_userid_from_email( $t_email ); - for ( $i = 0; $t_bytes > 1024; $i++ ) - { - $t_bytes /= 1024; - } + $this->custom_error( 'Monitor: ' . $t_user_id . ' - ' . $t_email . ' --> Issue ID: #' . $p_bug_id, FALSE ); - return( round( $t_bytes, 2 ) . $t_units[ $i ] ); + if( $t_user_id !== FALSE ) + { + // Make sure that mail_reporter_id and reporter_id are not added as a monitors. + if( $this->_mail_reporter_id != $t_user_id && $p_email[ 'Reporter_id' ] != $t_user_id ) + { + bug_monitor( $p_bug_id, $t_user_id ); + } + } + } + } } /** @@ -1442,7 +1425,7 @@ function ERP_formatbytes( $p_bytes ) * * Based on ldap_get_field_from_username from MantisBT 1.2.14 */ - function ldap_get_username_from_email( $p_email_address ) + private function ldap_get_username_from_email( $p_email_address ) { if ( $this->_login_method == LDAP ) { diff --git a/pages/manage_config_edit.php b/pages/manage_config_edit.php index 521595f..db85098 100644 --- a/pages/manage_config_edit.php +++ b/pages/manage_config_edit.php @@ -23,7 +23,7 @@ 'mail_nosubject' => gpc_get_string( 'mail_nosubject' ), 'mail_parse_html' => gpc_get_int( 'mail_parse_html' ), 'mail_preferred_username' => gpc_get_string( 'mail_preferred_username' ), - 'mail_preferred_realname' => gpc_get_string( 'mail_preferred_realname' ), + 'mail_preferred_realname' => gpc_get_string( 'mail_preferred_realname' ), 'mail_remove_mantis_email' => gpc_get_int( 'mail_remove_mantis_email' ), 'mail_remove_replies' => gpc_get_int( 'mail_remove_replies' ), 'mail_strip_gmail_style_replies'=> gpc_get_int( 'mail_strip_gmail_style_replies' ),