Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Pear Mail to v2.0.0 #77

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 43 additions & 37 deletions src/backend/imap/Mail.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
<?php
/**
* PEAR's Mail:: interface.
* PEAR's Mail:: interface.
*
* PHP versions 4 and 5
* PHP version 5
*
* LICENSE:
*
* Copyright (c) 2002-2007, Richard Heyes
* Copyright (c) 1997-2017, Chuck Hagenbuch & Richard Heyes
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* o The names of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Expand All @@ -37,9 +39,9 @@
* @category Mail
* @package Mail
* @author Chuck Hagenbuch <chuck@horde.org>
* @copyright 1997-2010 Chuck Hagenbuch
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Mail.php 307489 2011-01-14 19:06:57Z alec $
* @copyright 1997-2017 Chuck Hagenbuch
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License
* @version CVS: $Id$
* @link http://pear.php.net/package/Mail/
*/

Expand All @@ -48,10 +50,11 @@
* Z-Push changes
*
* removed PEAR dependency by implementing own raiseError()
* Remove duplicated addresses
* remove include dependancy
*
* Reference implementation used:
* http://download.pear.php.net/package/Mail-1.2.0.tgz
* SVN trunk version r333509
* https://github.com/pear/Mail/tree/v2.0.0
*
*
*/
Expand All @@ -62,8 +65,7 @@
* mailers under the PEAR hierarchy, and provides supporting functions
* useful in multiple mailer backends.
*
* @access public
* @version $Revision: 307489 $
* @version $Revision$
* @package Mail
*/
class Mail
Expand All @@ -72,25 +74,27 @@ class Mail
* Line terminator used for separating header lines.
* @var string
*/
var $sep = "\r\n";
public $sep = "\r\n";

/**
* Provides an interface for generating Mail:: objects of various
* types
*
* @param string $driver The kind of Mail:: object to instantiate.
* @param array $params The parameters to pass to the Mail:: object.
*
* @return object Mail a instance of the driver class or if fails a PEAR Error
* @access public
*/
static function &factory($driver, $params = array())
public static function factory($driver, $params = array())
{
$driver = strtolower($driver);
@include_once 'Mail/' . $driver . '.php';
$class = 'Mail_' . $driver;
if (class_exists($class)) {
$mailer = new $class($params);
return $mailer;
} else {
// Z-Push change: rasiseError dependancy
return Mail::raiseError('Unable to find class for driver ' . $driver);
}
}
Expand Down Expand Up @@ -119,16 +123,17 @@ static function &factory($driver, $params = array())
* containing a descriptive error message on
* failure.
*
* @access public
* @deprecated use Mail_mail::send instead
*/
function send($recipients, $headers, $body)
public function send($recipients, $headers, $body)
{
if (!is_array($headers)) {
// Z-Push change: rasiseError dependancy
return Mail::raiseError('$headers must be an array');
}

$result = $this->_sanitizeHeaders($headers);
// Z-Push change: rasiseError dependancy
//if (is_a($result, 'PEAR_Error')) {
if ($result === false) {
return $result;
Expand Down Expand Up @@ -159,18 +164,14 @@ function send($recipients, $headers, $body)
* filter is to prevent mail injection attacks.
*
* @param array $headers The associative array of headers to sanitize.
*
* @access private
*/
function _sanitizeHeaders(&$headers)
protected function _sanitizeHeaders(&$headers)
{
foreach ($headers as $key => $value) {
$headers[$key] =
preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i',
null, $value);
'', $value);
}

return true;
}

/**
Expand All @@ -187,17 +188,19 @@ function _sanitizeHeaders(&$headers)
* otherwise returns an array containing two
* elements: Any From: address found in the headers,
* and the plain text version of the headers.
* @access private
*/
function prepareHeaders($headers)
protected function prepareHeaders($headers)
{
$lines = array();
$from = null;

foreach ($headers as $key => $value) {
if (strcasecmp($key, 'From') === 0) {
// Z-Push change: remove include dependancy
//include_once 'Mail/RFC822.php';
$parser = new Mail_RFC822();
$addresses = $parser->parseAddressList($value, 'localhost', false);
// Z-Push change: rasiseError dependancy
//if (is_a($addresses, 'PEAR_Error')) {
if ($addresses === false) {
return $addresses;
Expand Down Expand Up @@ -249,10 +252,12 @@ function prepareHeaders($headers)
*
* @return mixed An array of forward paths (bare addresses) or a PEAR_Error
* object if the address list could not be parsed.
* @access private
*/
function parseRecipients($recipients)
protected function parseRecipients($recipients)
{
// Z-Push change: remove include dependancy
//include_once 'Mail/RFC822.php';

// if we're passed an array, assume addresses are valid and
// implode them before parsing.
if (is_array($recipients)) {
Expand All @@ -262,10 +267,11 @@ function parseRecipients($recipients)
// Parse recipients, leaving out all personal info. This is
// for smtp recipients, etc. All relevant personal information
// should already be in the headers.
$parser = new Mail_RFC822();
$addresses = $parser->parseAddressList($recipients, 'localhost', false);
$Mail_RFC822 = new Mail_RFC822();
$addresses = $Mail_RFC822->parseAddressList($recipients, 'localhost', false);

// If parseAddressList() returned a PEAR_Error object, just return it.
// Z-Push change: rasiseError dependancy
//if (is_a($addresses, 'PEAR_Error')) {
if ($addresses === false) {
return $addresses;
Expand All @@ -278,7 +284,7 @@ function parseRecipients($recipients)
}
}

// Remove duplicated
// Z-Push addition: Remove duplicated
$recipients = array_unique($recipients);

return $recipients;
Expand Down
18 changes: 5 additions & 13 deletions src/backend/imap/Mail/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* removed PEAR dependency by implementing own raiseError()
*
* Reference implementation used:
* http://download.pear.php.net/package/Mail-1.4.1.tgz
* https://github.com/pear/Mail/tree/v2.0.0
*
*
*/
Expand Down Expand Up @@ -87,18 +87,6 @@ public function __construct($params = null)
} else {
$this->_params = $params;
}

/* Because the mail() function may pass headers as command
* line arguments, we can't guarantee the use of the standard
* "\r\n" separator. Instead, we use the system's native line
* separator.
* Fixed in PHP 8.0.
*/
if (defined('PHP_EOL') && version_compare(PHP_VERSION, '8.0.0', '<')) {
$this->sep = PHP_EOL;
} elseif (version_compare(PHP_VERSION, '8.0.0', '<')) {
$this->sep = (strpos(PHP_OS, 'WIN') === false) ? "\n" : "\r\n";
}
}

/**
Expand Down Expand Up @@ -128,10 +116,12 @@ public function __construct($params = null)
public function send($recipients, $headers, $body)
{
if (!is_array($headers)) {
// Z-Push change: rasiseError dependancy
return Mail_mail::raiseError('$headers must be an array');
}

$result = $this->_sanitizeHeaders($headers);
// Z-Push change: rasiseError dependancy
//if (is_a($result, 'PEAR_Error')) {
if ($result === false) {
return $result;
Expand All @@ -156,6 +146,7 @@ public function send($recipients, $headers, $body)

// Flatten the headers out.
$headerElements = $this->prepareHeaders($headers);
// Z-Push change: rasiseError dependancy
//if (is_a($headerElements, 'PEAR_Error')) {
if ($headerElements === false) {
return $headerElements;
Expand All @@ -174,6 +165,7 @@ public function send($recipients, $headers, $body)
// If the mail() function returned failure, we need to create a
// PEAR_Error object and return it instead of the boolean result.
if ($result === false) {
// Z-Push change: rasiseError dependancy
$result = Mail_mail::raiseError('mail() returned failure');
}

Expand Down
10 changes: 9 additions & 1 deletion src/backend/imap/Mail/sendmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
* removed PEAR dependency by implementing own raiseError()
*
* Reference implementation used:
* http://download.pear.php.net/package/Mail-1.4.1.tgz
* https://github.com/pear/Mail/tree/v2.0.0
*
*
*/
Expand Down Expand Up @@ -144,23 +144,27 @@ public function __construct($params)
public function send($recipients, $headers, $body)
{
if (!is_array($headers)) {
// Z-Push change: rasiseError dependancy
return Mail_sendmail::raiseError('$headers must be an array');
}

$result = $this->_sanitizeHeaders($headers);
// Z-Push change: rasiseError dependancy
//if (is_a($result, 'PEAR_Error')) {
if ($result === false) {
return $result;
}

$recipients = $this->parseRecipients($recipients);
// Z-Push change: rasiseError dependancy
//if (is_a($recipients, 'PEAR_Error')) {
if ($recipients === false) {
return $recipients;
}
$recipients = implode(' ', array_map('escapeshellarg', $recipients));

$headerElements = $this->prepareHeaders($headers);
// Z-Push change: rasiseError dependancy
//if (is_a($headerElements, 'PEAR_Error')) {
if ($headerElements === false) {
return $headerElements;
Expand All @@ -175,18 +179,21 @@ public function send($recipients, $headers, $body)
}

if (!isset($from)) {
// Z-Push change: rasiseError dependancy
return Mail_sendmail::raiseError('No from address given.');
} elseif (strpos($from, ' ') !== false ||
strpos($from, ';') !== false ||
strpos($from, '&') !== false ||
strpos($from, '`') !== false) {
// Z-Push change: rasiseError dependancy
return Mail_sendmail::raiseError('From address specified with dangerous characters.');
}

$from = escapeshellarg($from); // Security bug #16200

$mail = @popen($this->sendmail_path . (!empty($this->sendmail_args) ? ' ' . $this->sendmail_args : '') . " -f$from -- $recipients", 'w');
if (!$mail) {
// Z-Push change: rasiseError dependancy
return Mail_sendmail::raiseError('Failed to open sendmail [' . $this->sendmail_path . '] for execution.');
}

Expand All @@ -203,6 +210,7 @@ public function send($recipients, $headers, $body)
}

if ($result != 0) {
// Z-Push change: rasiseError dependancy
return Mail_sendmail::raiseError('sendmail returned error code ' . $result,
$result);
}
Expand Down
Loading
Loading