From 593efca07054633cb3f6f8a03a8a7e226cfd3465 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Sun, 22 Feb 2009 01:54:11 -0700 Subject: [PATCH] Use exceptions in Horde_Mime --- folks/lib/Folks.php | 12 ++++++++---- folks/lib/Report/mail.php | 10 +++++++--- folks/scripts/mail.php | 14 ++++++++------ imp/compose-dimp.php | 14 ++++++++------ imp/compose-mimp.php | 6 +++++- imp/compose.php | 23 +++++++++++++++-------- imp/lib/Compose.php | 36 +++++++++++++++++++++--------------- imp/lib/Identity/imp.php | 20 ++++++++++++-------- imp/lib/Spam.php | 6 +++++- imp/lib/UI/Message.php | 6 +++--- kronolith/attendees.php | 16 +++++++--------- kronolith/lib/Kronolith.php | 20 +++++++++++--------- kronolith/scripts/agenda.php | 11 ++++++----- news/mail.php | 10 +++++----- 14 files changed, 121 insertions(+), 83 deletions(-) diff --git a/folks/lib/Folks.php b/folks/lib/Folks.php index 11bd124ee..84fe1dca9 100644 --- a/folks/lib/Folks.php +++ b/folks/lib/Folks.php @@ -227,9 +227,11 @@ class Folks { $mail = new Horde_Mime_Mail($subject, $body, $to, $GLOBALS['conf']['support'], NLS::getCharset()); require_once FOLKS_BASE . '/lib/version.php'; - $mail->addHeader('User-Agent', 'Folks ' . FOLKS_VERSION); - $mail->addHeader('X-Originating-IP', $_SERVER['REMOTE_ADDR']); - $mail->addHeader('X-Remote-Browser', $_SERVER['HTTP_USER_AGENT']); + try { + $mail->addHeader('User-Agent', 'Folks ' . FOLKS_VERSION); + $mail->addHeader('X-Originating-IP', $_SERVER['REMOTE_ADDR']); + $mail->addHeader('X-Remote-Browser', $_SERVER['HTTP_USER_AGENT']); + } catch (Horde_Mime_Exception $e) {} foreach ($attaches as $file) { if (file_exists($file)) { @@ -239,7 +241,9 @@ class Folks { list($mail_driver, $mail_params) = Horde::getMailerConfig(); - return $mail->send($mail_driver, $mail_params); + try { + return $mail->send($mail_driver, $mail_params); + } catch (Horde_Mime_Exception $e) {} } /** diff --git a/folks/lib/Report/mail.php b/folks/lib/Report/mail.php index f5a7b951a..b5fd0eb2c 100644 --- a/folks/lib/Report/mail.php +++ b/folks/lib/Report/mail.php @@ -34,7 +34,9 @@ class Folks_Report_mail extends Folks_Report { $mail = new Horde_Mime_Mail($this->getTitle(), $this->getMessage($message), $to, $this->getUserEmail()); //FIXME: This address should be configurable - $mail->addHeader('Sender', 'horde-problem@' . $conf['report_content']['maildomain']); + try { + $mail->addHeader('Sender', 'horde-problem@' . $conf['report_content']['maildomain']); + } catch (Horde_Mime_Exception $e) {} $mail_driver = $conf['mailer']['type']; $mail_params = $conf['mailer']['params']; @@ -50,6 +52,8 @@ class Folks_Report_mail extends Folks_Report { } } - return $mail->send($mail_driver, $mail_params); + try { + return $mail->send($mail_driver, $mail_params); + } catch (Horde_Mime_Exception $e) {} } -} \ No newline at end of file +} diff --git a/folks/scripts/mail.php b/folks/scripts/mail.php index 2aed62bd0..f582da279 100644 --- a/folks/scripts/mail.php +++ b/folks/scripts/mail.php @@ -128,13 +128,15 @@ while ($row =& $res->fetchRow()) { $body2 = sprintf($body, $row[0], $registry->get('name', 'horde'), Folks::getUrlFor('user', $row[0], true, -1)); // Send mail - $mail = new MIME_Mail($subject, $body2, $row[1], $conf['support'], NLS::getCharset()); - $mail->addHeader('User-Agent', 'Folks' . FOLKS_VERSION); - $sent = $mail->send($conf['mailer']['type'], $conf['mailer']['params']); - if ($sent instanceof PEAR_Error) { - $cli->message($sent, 'cli.warning'); - } else { + $mail = new Horde_Mime_Mail($subject, $body2, $row[1], $conf['support'], NLS::getCharset()); + try { + $mail->addHeader('User-Agent', 'Folks' . FOLKS_VERSION); + } catch (Horde_Mime_Exception $e) {} + try { + $mail->send($conf['mailer']['type'], $conf['mailer']['params']); $cli->message($row[0], 'cli.success'); + } catch (Horde_Mime_Exception $e) { + $cli->message($e, 'cli.warning'); } // sleep(1); diff --git a/imp/compose-dimp.php b/imp/compose-dimp.php index ed9f9eefc..3c0fe7a0a 100644 --- a/imp/compose-dimp.php +++ b/imp/compose-dimp.php @@ -83,9 +83,10 @@ if (count($_POST)) { case 'auto_save_draft': case 'save_draft': /* Set up the From address based on the identity. */ - $from = $identity->getFromLine(null, Util::getFormData('from')); - if (is_a($from, 'PEAR_Error')) { - $notification->push($from); + try { + $from = $identity->getFromLine(null, Util::getFormData('from')); + } catch (Horde_Exception $e) { + $notification->push($e); break; } $header['from'] = $from; @@ -111,9 +112,10 @@ if (count($_POST)) { break; case 'send_message': - $from = $identity->getFromLine(null, Util::getFormData('from')); - if (is_a($from, 'PEAR_Error')) { - $notification->push($from); + try { + $from = $identity->getFromLine(null, Util::getFormData('from')); + } catch (Horde_Exception $e) { + $notification->push($e); break; } $header['from'] = $from; diff --git a/imp/compose-mimp.php b/imp/compose-mimp.php index 08d6337db..f95fe9661 100644 --- a/imp/compose-mimp.php +++ b/imp/compose-mimp.php @@ -186,7 +186,11 @@ case _("Send"): $message .= "\n" . $sig; } - $header['from'] = $identity->getFromLine(null, Util::getFormData('from')); + try { + $header['from'] = $identity->getFromLine(null, Util::getFormData('from')); + } catch (Horde_Exception $e) { + $header['from'] = ''; + } $header['replyto'] = $identity->getValue('replyto_addr'); $header['subject'] = Util::getFormData('subject'); diff --git a/imp/compose.php b/imp/compose.php index 0f0a024a0..a43f05a5b 100644 --- a/imp/compose.php +++ b/imp/compose.php @@ -401,13 +401,14 @@ case 'send_message': break; } - $from = $identity->getFromLine(null, Util::getFormData('from')); - if (is_a($from, 'PEAR_Error')) { + try { + $header['from'] = $identity->getFromLine(null, Util::getFormData('from')); + } catch (Horde_Exception $e) { + $header['from'] = ''; $get_sig = false; - $notification->push($from); + $notification->push($e); break; } - $header['from'] = $from; $header['replyto'] = $identity->getValue('replyto_addr'); $header['to'] = $imp_ui->getAddressList(Util::getFormData('to'), Util::getFormData('to_list'), Util::getFormData('to_field'), Util::getFormData('to_new')); @@ -491,10 +492,12 @@ case 'save_draft': } /* Set up the From address based on the identity. */ - $header['from'] = $identity->getFromLine(null, Util::getFormData('from')); - if (is_a($header['from'], 'PEAR_Error')) { + try { + $header['from'] = $identity->getFromLine(null, Util::getFormData('from')); + } catch (Horde_Exception $e) { + $header['from'] = ''; $get_sig = false; - $notification->push($header['from']); + $notification->push($e); break; } foreach (array('to', 'cc', 'bcc', 'subject') as $val) { @@ -955,7 +958,11 @@ if ($redirect) { if ($t->get('di_locked')) { $t->set('help_compose-from', Help::link('imp', 'compose-from')); $t->set('fromaddr_locked', $prefs->isLocked('from_addr')); - $t->set('from', htmlspecialchars($identity->getFromLine(null, Util::getFormData('from')))); + try { + $t->set('from', htmlspecialchars($identity->getFromLine(null, Util::getFormData('from')))); + } catch (Horde_Exception $e) { + $t->set('from', ''); + } if (!$t->get('fromaddr_locked')) { $t->set('fromaddr_tabindex', ++$tabindex); } diff --git a/imp/lib/Compose.php b/imp/lib/Compose.php index f235ab284..67e20ed66 100644 --- a/imp/lib/Compose.php +++ b/imp/lib/Compose.php @@ -192,9 +192,10 @@ class IMP_Compose if (!empty($headers[$k])) { $addr = $headers[$k]; if ($session) { - $addr_check = Horde_Mime::encodeAddress($this->formatAddr($addr), $charset, $_SESSION['imp']['maildomain']); - if (is_a($addr_check, 'PEAR_Error')) { - throw new IMP_Compose_Exception(sprintf(_("Saving the draft failed. The %s header contains an invalid e-mail address: %s."), $k, $addr_check->getMessage())); + try { + Horde_Mime::encodeAddress($this->formatAddr($addr), $charset, $_SESSION['imp']['maildomain']); + } catch (Horde_Mime_Exception $e) { + throw new IMP_Compose_Exception(sprintf(_("Saving the draft failed. The %s header contains an invalid e-mail address: %s."), $k, $e->getMessage()), $e->getCode()); } } $draft_headers->addHeader($v, $addr); @@ -628,14 +629,16 @@ class IMP_Compose global $conf; /* Properly encode the addresses we're sending to. */ - $email = Horde_Mime::encodeAddress($email, null, $_SESSION['imp']['maildomain']); - if (is_a($email, 'PEAR_Error')) { - throw new IMP_Compose_Exception($email); + try { + $email = Horde_Mime::encodeAddress($email, null, $_SESSION['imp']['maildomain']); + } catch (Horde_Mime_Exception $e) { + throw new IMP_Compose_Exception($e); } /* Validate the recipient addresses. */ - $result = Horde_Mime_Address::parseAddressList($email, array('defserver' => $_SESSION['imp']['maildomain'], 'validate' => true)); - if (empty($result)) { + try { + $result = Horde_Mime_Address::parseAddressList($email, array('defserver' => $_SESSION['imp']['maildomain'], 'validate' => true)); + } catch (Horde_Mime_Exception $e) { return; } @@ -719,10 +722,11 @@ class IMP_Compose return; } - $r_array = Horde_Mime::encodeAddress($recipients, null, $_SESSION['imp']['maildomain']); - if (!is_a($r_array, 'PEAR_Error')) { + try { + $r_array = Horde_Mime::encodeAddress($recipients, null, $_SESSION['imp']['maildomain']); $r_array = Horde_Mime_Address::parseAddressList($r_array, array('validate' => true)); - } + } catch (Horde_Mime_Exception $e) {} + if (empty($r_array)) { $notification->push(_("Could not save recipients.")); return; @@ -803,8 +807,9 @@ class IMP_Compose continue; } - $obs = Horde_Mime_Address::parseAddressList($email); - if (empty($obs)) { + try { + $obs = Horde_Mime_Address::parseAddressList($email); + } catch (Horde_Mime_Exception $e) { throw new IMP_Compose_Exception(sprintf(_("Invalid e-mail address: %s."), $email)); } @@ -1839,8 +1844,9 @@ class IMP_Compose /* First we'll get a comma seperated list of email addresses and a comma seperated list of personal names out of $from (there just might be more than one of each). */ - $addr_list = Horde_Mime_Address::parseAddressList($from); - if (!empty($addr_list)) { + try { + $addr_list = Horde_Mime_Address::parseAddressList($from); + } catch (Horde_Mime_Exception $e) { $addr_list = array(); } diff --git a/imp/lib/Identity/imp.php b/imp/lib/Identity/imp.php index 7d4424617..bb2e08ff8 100644 --- a/imp/lib/Identity/imp.php +++ b/imp/lib/Identity/imp.php @@ -126,6 +126,7 @@ class Identity_imp extends Identity * * @return string A full From: header in the format * 'Fullname '. + * @throws Horde_Exception */ public function getFromLine($ident = null, $from_address = '') { @@ -144,12 +145,10 @@ class Identity_imp extends Identity $name = $this->getFullname($ident); } - if (!empty($address)) { + try { $ob = Horde_Mime_Address::parseAddressList($address, array('defserver' => $_SESSION['imp']['maildomain'])); - } - if (empty($ob)) { - $ob['message'] .= ' ' . _("Your From address is not a valid email address. This can be fixed in your Personal Information options page."); - return $ob; + } catch (Horde_Mime_Exception $e) { + throw new Horde_Exception (_("Your From address is not a valid email address. This can be fixed in your Personal Information options page.")); } if (empty($name)) { @@ -346,7 +345,11 @@ class Identity_imp extends Identity if (!is_array($bcc)) { $bcc = array($bcc); } - return Horde_Mime_Address::parseAddressList(implode(', ', $bcc)); + try { + return Horde_Mime_Address::parseAddressList(implode(', ', $bcc)); + } catch (Horde_Mime_Exception $e) { + return array(); + } } } @@ -378,8 +381,9 @@ class Identity_imp extends Identity $addresses = array($addresses); } - $addr_list = Horde_Mime_Address::parseAddressList(implode(', ', $addresses)); - if (empty($addr_list)) { + try { + $addr_list = Horde_Mime_Address::parseAddressList(implode(', ', $addresses)); + } catch (Horde_Mime_Exception $e) { return null; } diff --git a/imp/lib/Spam.php b/imp/lib/Spam.php index 521463db3..bb864b01b 100644 --- a/imp/lib/Spam.php +++ b/imp/lib/Spam.php @@ -104,7 +104,11 @@ class IMP_Spam require_once 'Horde/Identity.php'; $imp_compose = &IMP_Compose::singleton(); $identity = &Identity::singleton(array('imp', 'imp')); - $from_line = $identity->getFromLine(); + try { + $from_line = $identity->getFromLine(); + } catch (Horde_Exception $e) { + $from = ''; + } } /* Build the MIME structure. */ diff --git a/imp/lib/UI/Message.php b/imp/lib/UI/Message.php index a8f8fca3d..1876a7d0f 100644 --- a/imp/lib/UI/Message.php +++ b/imp/lib/UI/Message.php @@ -82,10 +82,10 @@ class IMP_UI_Message } else { /* Send out the MDN now. */ $mail_driver = IMP_Compose::getMailDriver(); - $result = $mdn->generate(false, $confirmed, 'displayed', $mail_driver['driver'], $mail_driver['params']); - if (!is_a($result, 'PEAR_Error')) { + try { + $mdn->generate(false, $confirmed, 'displayed', $mail_driver['driver'], $mail_driver['params']); IMP_Maillog::log('mdn', $msg_id, 'displayed'); - } + } catch (Horde_Mime_Exception $e) {} if ($GLOBALS['conf']['sentmail']['driver'] != 'none') { $sentmail = IMP_Sentmail::factory(); $sentmail->log('mdn', '', $return_addr, !is_a($result, 'PEAR_Error')); diff --git a/kronolith/attendees.php b/kronolith/attendees.php index 841066d18..e0429ff0b 100644 --- a/kronolith/attendees.php +++ b/kronolith/attendees.php @@ -77,15 +77,11 @@ case 'add': $name = empty($newAttendeeParsedPart->personal) ? '' : $newAttendeeParsedPart->personal; - $newAttendeeParsedPartNew = Horde_Mime::encodeAddress( - Horde_Mime_Address::writeAddress($newAttendeeParsedPart->mailbox, - $newAttendeeParsedPart->host, $name)); - $newAttendeeParsedPartValidated = $parser->parseAddressList( - $newAttendeeParsedPartNew, '', null, true); - if (is_a($newAttendeeParsedPartValidated, 'PEAR_Error')) { - $notification->push($newAttendeeParsedPartValidated, - 'horde.error'); - } else { + + try { + $newAttendeeParsedPartNew = Horde_Mime::encodeAddress(Horde_Mime_Address::writeAddress($newAttendeeParsedPart->mailbox, $newAttendeeParsedPart->host, $name)); + $newAttendeeParsedPartValidated = $parser->parseAddressList($newAttendeeParsedPartNew, '', null, true); + $email = $newAttendeeParsedPart->mailbox . '@' . $newAttendeeParsedPart->host; // Avoid overwriting existing attendees with the default @@ -96,6 +92,8 @@ case 'add': 'response' => KRONOLITH_RESPONSE_NONE, 'name' => $name, ); + } catch (Horde_Mime_Exception $e) { + $notification->push($e, 'horde.error'); } } } diff --git a/kronolith/lib/Kronolith.php b/kronolith/lib/Kronolith.php index 3ab7dce4b..d2b15becc 100644 --- a/kronolith/lib/Kronolith.php +++ b/kronolith/lib/Kronolith.php @@ -1897,17 +1897,20 @@ class Kronolith { $recipient = empty($status['name']) ? $email : Horde_Mime_Address::trimAddress($status['name'] . ' <' . $email . '>'); $mail = new Horde_Mime_Mail($subject, $message, $recipient, $from, NLS::getCharset()); require_once KRONOLITH_BASE . '/lib/version.php'; - $mail->addHeader('User-Agent', 'Kronolith ' . KRONOLITH_VERSION); + try { + $mail->addHeader('User-Agent', 'Kronolith ' . KRONOLITH_VERSION); + } catch (Horde_Mime_Exception $e) {} $mail->addMimePart($ics); - $status = $mail->send($mail_driver, $mail_params); - if (!is_a($status, 'PEAR_Error')) { + + try { + $mail->send($mail_driver, $mail_params); $notification->push( sprintf(_("The event notification to %s was successfully sent."), $recipient), 'horde.success' ); - } else { + } catch (Horde_Mime_Exception $e) { $notification->push( - sprintf(_("There was an error sending an event notification to %s: %s"), $recipient, $status->getMessage()), + sprintf(_("There was an error sending an event notification to %s: %s"), $recipient, $e->getMessage(), $e->getCode()), 'horde.error' ); } @@ -2036,10 +2039,9 @@ class Kronolith { NLS::getCharset()); $mime_mail->setBody($message, NLS::getCharset(), true); Horde::logMessage(sprintf('Sending event notifications for %s to %s', $event->title, implode(', ', $df_recipients)), __FILE__, __LINE__, PEAR_LOG_DEBUG); - $sent = $mime_mail->send($mail_driver, $mail_params, false, false); - if (is_a($sent, 'PEAR_Error')) { - return $sent; - } + try { + $mime_mail->send($mail_driver, $mail_params, false, false); + } catch (Horde_Mime_Exception $e) {} } } } diff --git a/kronolith/scripts/agenda.php b/kronolith/scripts/agenda.php index c03c24f94..f2841f707 100755 --- a/kronolith/scripts/agenda.php +++ b/kronolith/scripts/agenda.php @@ -176,12 +176,13 @@ function send_agendas() } $mime_mail->setBody($message, NLS::getCharset(), true); - $mime_mail->addRecipients($email); + try { + $mime_mail->addRecipients($email); + } catch (Horde_Mime_Exception $e) {} Horde::logMessage(sprintf('Sending daily agenda to %s', $email), __FILE__, __LINE__, PEAR_LOG_DEBUG); - $sent = $mime_mail->send($mail_driver, $mail_params, false, false); - if (is_a($sent, 'PEAR_Error')) { - return $sent; - } + try { + $mime_mail->send($mail_driver, $mail_params, false, false); + } catch (Horde_Mime_Exception $e) {} } } diff --git a/news/mail.php b/news/mail.php index 2ae563cf7..cddd01aa4 100644 --- a/news/mail.php +++ b/news/mail.php @@ -54,12 +54,12 @@ $body = sprintf(_("%s would you like to invite you to read the news\n Title: %s\ News::getUrlFor('news', $id, true, -1)); $mail = new Horde_Mime_Mail($row['title'], $body, $to, $from, NLS::getCharset()); -$result = $mail->send($conf['mailer']['type'], $conf['mailer']['params']); -if ($result instanceof PEAR_Error) { - $notification->push($result); -} else { +try { + $mail->send($conf['mailer']['type'], $conf['mailer']['params']); $notification->push(sprintf(_("News succesfully send to %s"), $to), 'horde.success'); +} catch (Horde_Mime_Exception $e) { + $notification->push($e); } header('Location: ' . News::getUrlFor('news', $id)); -exit; \ No newline at end of file +exit; -- 2.11.0