From 22a5fbb4b3f6def4a112e5c4e7ca101a32237f73 Mon Sep 17 00:00:00 2001 From: Klaus Leithoff Date: Tue, 14 Aug 2012 13:56:02 +0000 Subject: [PATCH] fix bug regarding certain russian filenames when forwarding/compose as new, as AddStringAttachment is using basename to make sure only a filename will be used for the namepart; some fixes backported from phpmailer5.2.1 --- phpgwapi/inc/class.egw_mailer.inc.php | 45 ++++++++++++++++++++++----- phpgwapi/inc/class.phpmailer.inc.php | 22 ++++++------- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/phpgwapi/inc/class.egw_mailer.inc.php b/phpgwapi/inc/class.egw_mailer.inc.php index 0af036ba06..01b4f60a46 100644 --- a/phpgwapi/inc/class.egw_mailer.inc.php +++ b/phpgwapi/inc/class.egw_mailer.inc.php @@ -15,7 +15,7 @@ require_once(EGW_API_INC.'/class.phpmailer.inc.php'); /** * Log mails to log file specified in $GLOBALS['egw_info']['server']['log_mail'] * or regular error_log for true (can be set either in DB or header.inc.php). - * + * * This class does NOT use anything EGroupware specific, it acts like PHPMail, but logs. */ class egw_mailer extends PHPMailer @@ -26,9 +26,9 @@ class egw_mailer extends PHPMailer function __construct() { parent::__construct(true); // throw exceptions instead of echoing errors - + // setting EGroupware specific path for PHPMailer lang files - list($lang,$nation) = explode('-',$GLOBALS['egw_info']['user']['preferences']['common']['lang']); + if (!empty($GLOBALS['egw_info']['user']['preferences']['common']['lang'])) list($lang,$nation) = explode('-',$GLOBALS['egw_info']['user']['preferences']['common']['lang']); $lang_path = EGW_SERVER_ROOT.'/phpgwapi/lang/'; if ($nation && file_exists($lang_path."phpmailer.lang-$nation.php")) // atm. only for pt-br => br { @@ -43,10 +43,10 @@ class egw_mailer extends PHPMailer /** * Log mails to log file specified in $GLOBALS['egw_info']['server']['log_mail'] * or regular error_log for true (can be set either in DB or header.inc.php). - * + * * We can NOT supply this method as callback to phpMailer, as phpMailer only accepts * functions (not methods) and from a function we can NOT access $this->ErrorInfo. - * + * * @param boolean $isSent * @param string $to * @param string $cc @@ -89,21 +89,21 @@ class egw_mailer extends PHPMailer * Initiates a connection to an SMTP server. * Returns false if the operation failed. * - * Overwriting this method from phpmailer, to make sure we set SMTPSecure to ssl or tls if the standardports for ssl or tls + * Overwriting this method from phpmailer, to make sure we set SMTPSecure to ssl or tls if the standardports for ssl or tls * are configured for the given profile * * @uses SMTP * @access public * @return bool */ - public function SmtpConnect() + public function SmtpConnect() { $port = $this->Port; $hosts = explode(';',$this->Host); foreach ($hosts as $k => &$host) { $host = trim($host); // make sure there is no whitespace leading or trailling the host string - if (in_array($port,array(465,587)) && strpos($host,'://')===false) + if (in_array($port,array(465,587)) && strpos($host,'://')===false) { //$host = ($port==587?'tls://':'ssl://').trim($host); $this->SMTPSecure = ($port==587?'tls':'ssl'); @@ -176,4 +176,33 @@ class egw_mailer extends PHPMailer return parent::AddBCC($address, $name); } + + /** + * Adds a string or binary attachment (non-filesystem) to the list. + * This method can be used to attach ascii or binary data, + * such as a BLOB record from a database. + * @param string $string String attachment data. + * @param string $filename Name of the attachment. We assume that this is NOT a path + * @param string $encoding File encoding (see $Encoding). + * @param string $type File extension (MIME) type. + * @return void + */ + public function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') + { + // Append to $attachment array + //already encoded? + //TODO: maybe add an parameter to AddStringAttachment to avoid using the basename + $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $filename, $matches); + $this->attachment[] = array( + 0 => $string, + 1 => $filename, + 2 => ($x?basename($filename):$filename), + 3 => $encoding, + 4 => $type, + 5 => true, // isStringAttachment + 6 => 'attachment', + 7 => 0 + ); + } + } diff --git a/phpgwapi/inc/class.phpmailer.inc.php b/phpgwapi/inc/class.phpmailer.inc.php index 06c4b322ec..db29c1439c 100644 --- a/phpgwapi/inc/class.phpmailer.inc.php +++ b/phpgwapi/inc/class.phpmailer.inc.php @@ -312,7 +312,7 @@ class PHPMailer { private $bcc = array(); private $ReplyTo = array(); private $all_recipients = array(); - private $attachment = array(); + protected $attachment = array(); //this way extended classes may use this variable private $CustomHeader = array(); private $message_type = ''; private $boundary = array(); @@ -1199,7 +1199,7 @@ class PHPMailer { $body = ''; if ($this->sign_key_file) { - $body .= $this->GetMailMIME(); + $body .= $this->GetMailMIME()."\n"; } $this->SetWordWrap(); @@ -1245,7 +1245,7 @@ class PHPMailer { $body .= $this->EncodeString($this->Body, $this->Encoding); $body .= $this->LE.$this->LE; // Create the extended body for an attached text/calendar - $body .= $this->GetBoundary($this->boundary[2], '', $this->attachment[0][4], '') . $this->LE; + $body .= $this->GetBoundary($this->boundary[2], '', $this->attachment[0][4], '') . $this->LE; $body .= $this->EncodeString($this->attachment[0][0], $this->attachment[0][3]); $body .= $this->LE.$this->LE; $body .= $this->EndBoundary($this->boundary[2]); @@ -1262,8 +1262,8 @@ class PHPMailer { $signed = tempnam("", "signed"); if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_cert_file, array("file://".$this->sign_key_file, $this->sign_key_pass), NULL)) { @unlink($file); - @unlink($signed); $body = file_get_contents($signed); + @unlink($signed); } else { @unlink($file); @unlink($signed); @@ -1491,16 +1491,16 @@ class PHPMailer { * @access private * @return string */ - private function &EncodeFile($path, $encoding = 'base64') + private function &EncodeFile($path, $encoding = 'base64') { - if (function_exists('get_magic_quotes')) + if (function_exists('get_magic_quotes')) { - function get_magic_quotes() + function get_magic_quotes() { return false; } } - if (PHP_VERSION < 6) + if (PHP_VERSION < 6) { if (function_exists('get_magic_quotes_runtime') && ($magic_quotes = get_magic_quotes_runtime())) set_magic_quotes_runtime(0); } @@ -1510,13 +1510,13 @@ class PHPMailer { throw new phpmailerException($this->Lang('file_open') . $path, self::STOP_CONTINUE); } $file_buffer = $this->EncodeString($file_buffer, $encoding); - if (PHP_VERSION < 6) + if (PHP_VERSION < 6) { if ($magic_quotes) set_magic_quotes_runtime($magic_quotes); - } + } return $file_buffer; } catch (Exception $e) { - if (PHP_VERSION < 6) + if (PHP_VERSION < 6) { if ($magic_quotes) set_magic_quotes_runtime($magic_quotes); }