Use exceptions in Horde_Mime
authorMichael M Slusarz <slusarz@curecanti.org>
Sun, 22 Feb 2009 08:53:54 +0000 (01:53 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Sun, 22 Feb 2009 08:55:07 +0000 (01:55 -0700)
framework/Mime/lib/Horde/Mime.php
framework/Mime/lib/Horde/Mime/Address.php
framework/Mime/lib/Horde/Mime/Exception.php [new file with mode: 0644]
framework/Mime/lib/Horde/Mime/Headers.php
framework/Mime/lib/Horde/Mime/Mail.php
framework/Mime/lib/Horde/Mime/Mdn.php
framework/Mime/lib/Horde/Mime/Part.php
framework/Mime/package.xml

index 87f437f..0beb019 100644 (file)
@@ -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)
     {
index 7ce620f..7a31be2 100644 (file)
@@ -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.
      * </pre>
      *
-     * @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 (file)
index 0000000..8e78717
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+/**
+ * Exception handler for the Horde_Mime class.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @package  Horde_Mime
+ */
+class Horde_Mime_Exception extends Horde_Exception {}
index 1126524..47f2a01 100644 (file)
@@ -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();
     }
 
     /**
index b5f8b92..c0c0037 100644 (file)
@@ -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;
     }
 
     /**
index 688b246..a86f93f 100644 (file)
@@ -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(),
index 55109dd..c0927f6 100644 (file)
@@ -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;
index fcd8d3a..a758085 100644 (file)
@@ -89,6 +89,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
        <file name="zip.php" role="php" />
       </dir> <!-- /lib/Horde/Mime/Viewer -->
       <file name="Address.php" role="php" />
+      <file name="Exception.php" role="php" />
       <file name="Headers.php" role="php" />
       <file name="Magic.php" role="php" />
       <file name="Mail.php" role="php" />
@@ -125,6 +126,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <min>1.5.0</min>
    </pearinstaller>
    <package>
+    <name>Horde_Framework</name>
+    <channel>pear.horde.org</channel>
+   </package>
+   <package>
     <name>Mail_mimeDecode</name>
     <channel>pear.php.net</channel>
    </package>
@@ -154,10 +159,6 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <channel>pear.horde.org</channel>
    </package>
    <package>
-    <name>Horde_Framework</name>
-    <channel>pear.horde.org</channel>
-   </package>
-   <package>
     <name>iCalendar</name>
     <channel>pear.horde.org</channel>
    </package>
@@ -222,6 +223,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Mime/Viewer/wordperfect.php" as="Horde/Mime/Viewer/wordperfect.php" />
    <install name="lib/Horde/Mime/Viewer/zip.php" as="Horde/Mime/Viewer/zip.php" />
    <install name="lib/Horde/Mime/Address.php" as="Horde/Mime/Address.php" />
+   <install name="lib/Horde/Mime/Exception.php" as="Horde/Mime/Exception.php" />
    <install name="lib/Horde/Mime/Headers.php" as="Horde/Mime/Headers.php" />
    <install name="lib/Horde/Mime/Mdn.php" as="Horde/Mime/Mdn.php" />
    <install name="lib/Horde/Mime/Magic.php" as="Horde/Mime/Magic.php" />