From 6fb756f440631e4363476dadb074a2df7fd14688 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Sun, 22 Feb 2009 01:53:54 -0700 Subject: [PATCH] Use exceptions in Horde_Mime --- framework/Mime/lib/Horde/Mime.php | 7 ++--- framework/Mime/lib/Horde/Mime/Address.php | 18 +++++------- framework/Mime/lib/Horde/Mime/Exception.php | 14 +++++++++ framework/Mime/lib/Horde/Mime/Headers.php | 20 +++++++++---- framework/Mime/lib/Horde/Mime/Mail.php | 45 ++++++++++++++++------------- framework/Mime/lib/Horde/Mime/Mdn.php | 16 ++++++++-- framework/Mime/lib/Horde/Mime/Part.php | 5 ++-- framework/Mime/package.xml | 10 ++++--- 8 files changed, 85 insertions(+), 50 deletions(-) create mode 100644 framework/Mime/lib/Horde/Mime/Exception.php diff --git a/framework/Mime/lib/Horde/Mime.php b/framework/Mime/lib/Horde/Mime.php index 87f437f77..0beb019a1 100644 --- a/framework/Mime/lib/Horde/Mime.php +++ b/framework/Mime/lib/Horde/Mime.php @@ -187,7 +187,8 @@ class Horde_Mime * @param string $defserver The default domain to append to mailboxes. * * @return string The text, encoded only if it contains non-ASCII - * characters, or PEAR_Error on error. + * characters. + * @throws Horde_Mime_Exception */ static public function encodeAddress($addresses, $charset = null, $defserver = null) @@ -201,9 +202,6 @@ class Horde_Mime } $addresses = Horde_Mime_Address::parseAddressList($addresses, array('defserver' => $defserver, 'nestgroups' => true)); - if (is_a($addresses, 'PEAR_Error')) { - return $addresses; - } } $text = ''; @@ -315,6 +313,7 @@ class Horde_Mime * to. * * @return string The decoded text. + * @throw Horde_Mime_Exception */ static public function decodeAddrString($string, $to_charset = null) { diff --git a/framework/Mime/lib/Horde/Mime/Address.php b/framework/Mime/lib/Horde/Mime/Address.php index 7ce620f79..7a31be2fd 100644 --- a/framework/Mime/lib/Horde/Mime/Address.php +++ b/framework/Mime/lib/Horde/Mime/Address.php @@ -333,8 +333,9 @@ class Horde_Mime_Address { $addressList = array(); - $from = self::parseAddressList($address, array('defserver' => $defserver)); - if (is_a($from, 'PEAR_Error')) { + try { + $from = self::parseAddressList($address, array('defserver' => $defserver)); + } catch (Horde_Mime_Exception $e) { return $multiple ? array() : ''; } @@ -359,16 +360,13 @@ class Horde_Mime_Address * 'nestgroups' - (boolean) Nest the groups? (Will appear under the * 'groupname' key) * DEFAULT: No. - * 'reterror' - (boolean) Return a PEAR_Error object on error? - * DEFAULT: Returns an empty array on error. * 'validate' - (boolean) Validate the address(es)? * DEFAULT: No. * * - * @return mixed If 'reterror' is true, returns a PEAR_Error object on - * error. Otherwise, a list of arrays with the possible - * keys: 'mailbox', 'host', 'personal', 'adl', 'groupname', - * and 'comment'. + * @return array A list of arrays with the possible keys: 'mailbox', + * 'host', 'personal', 'adl', 'groupname', and 'comment'. + * @throws Horde_Mime_Exception */ static public function parseAddressList($address, $options = array()) { @@ -379,19 +377,17 @@ class Horde_Mime_Address $options = array_merge(array( 'defserver' => null, 'nestgroups' => false, - 'reterror' => false, 'validate' => false ), $options); static $parser; if (!isset($parser)) { - require_once 'Mail/RFC822.php'; $parser = new Mail_RFC822(); } $ret = $parser->parseAddressList($address, $options['defserver'], $options['nestgroups'], $options['validate']); if (is_a($ret, 'PEAR_Error')) { - return empty($options['reterror']) ? array() : $ret; + throw new Horde_Mime_Exception($ret); } /* Convert objects to arrays. */ diff --git a/framework/Mime/lib/Horde/Mime/Exception.php b/framework/Mime/lib/Horde/Mime/Exception.php new file mode 100644 index 000000000..8e787170d --- /dev/null +++ b/framework/Mime/lib/Horde/Mime/Exception.php @@ -0,0 +1,14 @@ + + * @category Horde + * @package Horde_Mime + */ +class Horde_Mime_Exception extends Horde_Exception {} diff --git a/framework/Mime/lib/Horde/Mime/Headers.php b/framework/Mime/lib/Horde/Mime/Headers.php index 112652419..47f2a016f 100644 --- a/framework/Mime/lib/Horde/Mime/Headers.php +++ b/framework/Mime/lib/Horde/Mime/Headers.php @@ -74,8 +74,9 @@ class Horde_Mime_Headers foreach (array_keys($val) as $key) { if (in_array($header, $address_keys) ) { /* Address encoded headers. */ - $text = Horde_Mime::encodeAddress($val[$key], $charset, empty($options['defserver']) ? null : $options['defserver']); - if (is_a($text, 'PEAR_Error')) { + try { + $text = Horde_Mime::encodeAddress($val[$key], $charset, empty($options['defserver']) ? null : $options['defserver']); + } catch (Horde_Mime_Exception $e) { $text = $val[$key]; } } elseif (in_array($header, $mime) && !empty($ob['params'])) { @@ -267,7 +268,11 @@ class Horde_Mime_Headers if (!empty($options['decode'])) { // Fields defined in RFC 2822 that contain address information if (in_array($lcHeader, $this->addressFields())) { - $value = Horde_Mime::decodeAddrString($value); + try { + $value = Horde_Mime::decodeAddrString($value); + } catch (Horde_Mime_Exception $e) { + $value = ''; + } } else { $value = Horde_Mime::decode($value); } @@ -503,9 +508,12 @@ class Horde_Mime_Headers public function getOb($field) { $val = $this->getValue($field); - return is_null($val) - ? array() - : Horde_Mime_Address::parseAddressList($val); + if (!is_null($val)) { + try { + return Horde_Mime_Address::parseAddressList($val); + } catch (Horde_Mime_Exception $e) {} + } + return array(); } /** diff --git a/framework/Mime/lib/Horde/Mime/Mail.php b/framework/Mime/lib/Horde/Mime/Mail.php index b5f8b9219..c0c00371e 100644 --- a/framework/Mime/lib/Horde/Mime/Mail.php +++ b/framework/Mime/lib/Horde/Mime/Mail.php @@ -114,13 +114,13 @@ class Horde_Mime_Mail * @param array $header Hash with header names as keys and header * contents as values. * @param string $charset The header value's charset. + * + * @throws Horde_Mime_Exception */ public function addHeaders($headers = array(), $charset = 'iso-8859-1') { foreach ($headers as $header => $value) { - if (is_a($added = $this->addHeader($header, $value, $charset), 'PEAR_Error')) { - return $added; - } + $this->addHeader($header, $value, $charset); } } @@ -135,6 +135,8 @@ class Horde_Mime_Mail * headers are added; if null, the correct * behaviour is automatically chosen depending * on the header name. + * + * @throws Horde_Mime_Exception */ public function addHeader($header, $value, $charset = 'iso-8859-1', $overwrite = null) @@ -180,7 +182,9 @@ class Horde_Mime_Mail $value = $this->_headers->getValue($header); $this->_headers->removeHeader($header); if (in_array(String::lower($header), array('to', 'cc', 'bcc'))) { - $this->removeRecipients($value); + try { + $this->removeRecipients($value); + } catch (Horde_Mime_Exception $e) {} } } @@ -311,14 +315,12 @@ class Horde_Mime_Mail * * @param string|array List of recipients, either as a comma separated * list or as an array of email addresses. + * + * @throws Horde_Mime_Exception */ public function addRecipients($recipients) { - $recipients = $this->_buildRecipients($recipients); - if (is_a($recipients, 'PEAR_Error')) { - return $recipients; - } - $this->_recipients = array_merge($this->_recipients, $recipients); + $this->_recipients = array_merge($this->_recipients, $this->_buildRecipients($recipients)); } /** @@ -326,14 +328,12 @@ class Horde_Mime_Mail * * @param string|array List of recipients, either as a comma separated * list or as an array of email addresses. + * + * @throws Horde_Mime_Exception */ public function removeRecipients($recipients) { - $recipients = $this->_buildRecipients($recipients); - if (is_a($recipients, 'PEAR_Error')) { - return $recipients; - } - $this->_recipients = array_diff($this->_recipients, $recipients); + $this->_recipients = array_diff($this->_recipients, $this->_buildRecipients($recipients)); } /** @@ -350,7 +350,8 @@ class Horde_Mime_Mail * @param string|array List of recipients, either as a comma separated * list or as an array of email addresses. * - * @return array Normalized list of recipients or PEAR_Error on failure. + * @return array Normalized list of recipients. + * @throws Horde_Mime_Exception */ protected function _buildRecipients($recipients) { @@ -373,7 +374,7 @@ class Horde_Mime_Mail foreach (Horde_Mime_Address::bareAddress(implode(', ', $addrlist), null, true) as $val) { if (Horde_Mime::is8bit($val)) { - return PEAR::raiseError(sprintf(_("Invalid character in e-mail address: %s."), $val)); + throw new Horde_Mime_Exception(sprintf(_("Invalid character in e-mail address: %s."), $val)); } } @@ -391,10 +392,9 @@ class Horde_Mime_Mail * @param array $params Any parameters necessary for the Mail driver. * @param boolean $resend If true, the message id and date are re-used; * If false, they will be updated. - * @param boolean $flowed Send message in flowed text format. @since - * Horde 3.2.1 + * @param boolean $flowed Send message in flowed text format. * - * @return mixed True on success, PEAR_Error on error. + * @throws Horde_Mime_Exception */ public function send($driver = null, $params = array(), $resend = false, $flowed = true) @@ -457,9 +457,14 @@ class Horde_Mime_Mail } /* Send message. */ - return $basepart->send(implode(', ', $this->_recipients), + $res = $basepart->send(implode(', ', $this->_recipients), $this->_headers, $this->_mailer_driver, $this->_mailer_params); + if (is_a($res, 'PEAR_Error')) { + throw new Horde_Mime_Exception($res); + } + + return $res; } /** diff --git a/framework/Mime/lib/Horde/Mime/Mdn.php b/framework/Mime/lib/Horde/Mime/Mdn.php index 688b2462b..a86f93ff7 100644 --- a/framework/Mime/lib/Horde/Mime/Mdn.php +++ b/framework/Mime/lib/Horde/Mime/Mdn.php @@ -74,7 +74,12 @@ class Horde_Mime_Mdn /* RFC 3798 [2.1]: Explicit confirmation is needed if there is more * than one distinct address in the Disposition-Notification-To * header. */ - $addr_arr = Horde_Mime_Address::parseAddressList($this->getMdnReturnAddr()); + try { + $addr_arr = Horde_Mime_Address::parseAddressList($this->getMdnReturnAddr()); + } catch (Horde_Mime_Exception $e) { + return false; + } + if (count($addr_arr) > 1) { return true; } @@ -84,7 +89,12 @@ class Horde_Mime_Mdn * from the address in the Return-Path header." This comparison is * case-sensitive for the mailbox part and case-insensitive for the * host part. */ - $ret_arr = Horde_Mime_Address::parseAddressList($return_path); + try { + $ret_arr = Horde_Mime_Address::parseAddressList($return_path); + } catch (Horde_Mime_Exception $e) { + return false; + } + return ($addr_arr[0]['mailbox'] == $ret_arr[0]['mailbox']) && (String::lower($addr_arr[0]['host']) == String::lower($ret_arr[0]['host'])); } @@ -129,7 +139,7 @@ class Horde_Mime_Mdn * information to provide. Key is the type of * modification, value is the text. * - * @return mixed True on success, PEAR_Error object on error. + * @throws Horde_Mime_Exception */ public function generate($action, $sending, $type, $maildriver, $mailparams = array(), $mod = array(), diff --git a/framework/Mime/lib/Horde/Mime/Part.php b/framework/Mime/lib/Horde/Mime/Part.php index 55109dd7c..c0927f647 100644 --- a/framework/Mime/lib/Horde/Mime/Part.php +++ b/framework/Mime/lib/Horde/Mime/Part.php @@ -1304,7 +1304,7 @@ class Horde_Mime_Part * @param array $params Any parameters necessary for the * Mail driver. * - * @return mixed True on success, PEAR_Error on error. + * @throws Horde_Mime_Exception */ public function send($email, $headers, $driver, $params = array()) { @@ -1362,7 +1362,8 @@ class Horde_Mime_Part } else { $userinfo = $result->toString(); } - return PEAR::raiseError($error, null, null, null, $userinfo); + // TODO: userinfo + throw new Horde_Mime_Exception($error); } return $result; diff --git a/framework/Mime/package.xml b/framework/Mime/package.xml index fcd8d3a22..a75808545 100644 --- a/framework/Mime/package.xml +++ b/framework/Mime/package.xml @@ -89,6 +89,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -125,6 +126,10 @@ http://pear.php.net/dtd/package-2.0.xsd"> 1.5.0 + Horde_Framework + pear.horde.org + + Mail_mimeDecode pear.php.net @@ -154,10 +159,6 @@ http://pear.php.net/dtd/package-2.0.xsd"> pear.horde.org - Horde_Framework - pear.horde.org - - iCalendar pear.horde.org @@ -222,6 +223,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + -- 2.11.0