From: Andrey Kutejko Date: Mon, 29 Jul 2013 06:19:53 +0000 (+0300) Subject: exception classes X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=655df2d2eeeabb626ef29984428548b8ed6e404f;p=ipf-mail.git exception classes --- diff --git a/ipf/exception.php b/ipf/exception.php index c418db8..ef36fc7 100644 --- a/ipf/exception.php +++ b/ipf/exception.php @@ -1,3 +1,6 @@ _type = $type; @@ -225,7 +225,7 @@ class IPF_Mail extends IPF_Mime_Message $this->_from = $email; $this->_storeHeader('From', $this->_encodeHeader('"'.$name.'"').' <'.$email.'>', true); } else { - throw new IPF_Exception_Mail('From Header set twice'); + throw new IPF_Mail_Exception('From Header set twice'); } return $this; } @@ -242,7 +242,7 @@ class IPF_Mail extends IPF_Mime_Message $this->_returnPath = $email; $this->_storeHeader('Return-Path', $email, false); } else { - throw new IPF_Exception_Mail('Return-Path Header set twice'); + throw new IPF_Mail_Exception('Return-Path Header set twice'); } return $this; } @@ -266,7 +266,7 @@ class IPF_Mail extends IPF_Mime_Message $this->_subject = IPF_Mime::encodeQ($subject); $this->_storeHeader('Subject', $this->_subject); } else { - throw new IPF_Exception_Mail('Subject set twice'); + throw new IPF_Mail_Exception('Subject set twice'); } return $this; } @@ -286,17 +286,17 @@ class IPF_Mail extends IPF_Mime_Message } else if (is_string($date)) { $date = strtotime($date); if ($date === false || $date < 0) { - throw new IPF_Exception_Mail('String representations of Date Header must be ' . + throw new IPF_Mail_Exception('String representations of Date Header must be ' . 'strtotime()-compatible'); } $date = date('r', $date); } else { - throw new IPF_Exception_Mail(__METHOD__ . ' only accepts UNIX timestamps and strtotime()-compatible strings'); + throw new IPF_Mail_Exception(__METHOD__ . ' only accepts UNIX timestamps and strtotime()-compatible strings'); } $this->_date = $date; $this->_storeHeader('Date', $date); } else { - throw new IPF_Exception_Mail('Date Header set twice'); + throw new IPF_Mail_Exception('Date Header set twice'); } return $this; } @@ -309,7 +309,7 @@ class IPF_Mail extends IPF_Mime_Message public function addHeader($name, $value, $append = false) { if (in_array(strtolower($name), array('to', 'cc', 'bcc', 'from', 'subject', 'return-path', 'date'))) { - throw new IPF_Exception_Mail('Cannot set standard header from addHeader()'); + throw new IPF_Mail_Exception('Cannot set standard header from addHeader()'); } $value = strtr($value,"\r\n\t",'???'); diff --git a/ipf/mail/exception.php b/ipf/mail/exception.php new file mode 100644 index 0000000..8ec3e7a --- /dev/null +++ b/ipf/mail/exception.php @@ -0,0 +1,6 @@ +_mail = $params['handler']; @@ -47,7 +47,7 @@ class IPF_Mail_Part implements RecursiveIterator { try { return stripos($this->contentType, 'multipart/') === 0; - } catch(IPF_Exception_Mail $e) { + } catch(IPF_Mail_Exception $e) { return false; } } @@ -62,7 +62,7 @@ class IPF_Mail_Part implements RecursiveIterator if ($this->_mail) { return $this->_mail->getRawContent($this->_messageNum); } else { - throw new IPF_Exception_Mail('no content'); + throw new IPF_Mail_Exception('no content'); } } @@ -80,7 +80,7 @@ class IPF_Mail_Part implements RecursiveIterator // split content in parts $boundary = $this->getHeaderField('content-type', 'boundary'); if (!$boundary) { - throw new IPF_Exception_Mail('no boundary found in content type to split message'); + throw new IPF_Mail_Exception('no boundary found in content type to split message'); } $parts = IPF_Mime_Decode::splitMessageStruct($this->_content, $boundary); $counter = 1; @@ -96,7 +96,7 @@ class IPF_Mail_Part implements RecursiveIterator } if (!$this->_mail && $this->_content === null) { - throw new IPF_Exception_Mail('part not found'); + throw new IPF_Mail_Exception('part not found'); } if ($this->_mail && $this->_mail->hasFetchPart) { @@ -107,7 +107,7 @@ class IPF_Mail_Part implements RecursiveIterator $this->_cacheContent(); if (!isset($this->_parts[$num])) { - throw new IPF_Exception_Mail('part not found'); + throw new IPF_Mail_Exception('part not found'); } return $this->_parts[$num]; @@ -160,7 +160,7 @@ class IPF_Mail_Part implements RecursiveIterator if (!isset($this->_headers[$lowerName])) { $lowerName = strtolower(preg_replace('%([a-z])([A-Z])%', '\1-\2', $name)); if (!isset($this->_headers[$lowerName])) { - throw new IPF_Exception_Mail("no Header with Name $name found"); + throw new IPF_Mail_Exception("no Header with Name $name found"); } } $name = $lowerName; diff --git a/ipf/mail/protocol/abstract.php b/ipf/mail/protocol/abstract.php index 139884c..88eb17a 100644 --- a/ipf/mail/protocol/abstract.php +++ b/ipf/mail/protocol/abstract.php @@ -57,11 +57,11 @@ abstract class IPF_Mail_Protocol_Abstract if ($errorNum == 0) { $errorStr = 'Could not open socket'; } - throw new IPF_Exception_Mail($errorStr); + throw new IPF_Mail_Exception($errorStr); } if (($result = stream_set_timeout($this->_socket, self::TIMEOUT_CONNECTION)) === false) { - throw new IPF_Exception_Mail('Could not set stream timeout'); + throw new IPF_Mail_Exception('Could not set stream timeout'); } return $result; @@ -77,7 +77,7 @@ abstract class IPF_Mail_Protocol_Abstract protected function _send($request) { if (!is_resource($this->_socket)) { - throw new IPF_Exception_Mail('No connection has been established to ' . $this->_host); + throw new IPF_Mail_Exception('No connection has been established to ' . $this->_host); } $this->_request = $request; @@ -88,7 +88,7 @@ abstract class IPF_Mail_Protocol_Abstract $this->_log .= $request . self::EOL; if ($result === false) { - throw new IPF_Exception_Mail('Could not send request to ' . $this->_host); + throw new IPF_Mail_Exception('Could not send request to ' . $this->_host); } return $result; @@ -97,7 +97,7 @@ abstract class IPF_Mail_Protocol_Abstract protected function _receive($timeout = null) { if (!is_resource($this->_socket)) { - throw new IPF_Exception_Mail('No connection has been established to ' . $this->_host); + throw new IPF_Mail_Exception('No connection has been established to ' . $this->_host); } // Adapters may wish to supply per-commend timeouts according to appropriate RFC @@ -115,11 +115,11 @@ abstract class IPF_Mail_Protocol_Abstract $info = stream_get_meta_data($this->_socket); if (!empty($info['timed_out'])) { - throw new IPF_Exception_Mail($this->_host . ' has timed out'); + throw new IPF_Mail_Exception($this->_host . ' has timed out'); } if ($reponse === false) { - throw new IPF_Exception_Mail('Could not read from ' . $this->_host); + throw new IPF_Mail_Exception('Could not read from ' . $this->_host); } return $reponse; @@ -138,7 +138,7 @@ abstract class IPF_Mail_Protocol_Abstract sscanf($result, $this->_template, $cmd, $msg); if ($cmd === null || !in_array($cmd, $code)) { - throw new IPF_Exception_Mail($result); + throw new IPF_Mail_Exception($result); } } while (strpos($msg, '-') === 0); // The '-' message prefix indicates an information string instead of a response string. diff --git a/ipf/mail/protocol/imap.php b/ipf/mail/protocol/imap.php index 109e488..6ac14c2 100644 --- a/ipf/mail/protocol/imap.php +++ b/ipf/mail/protocol/imap.php @@ -29,18 +29,18 @@ class IPF_Mail_Protocol_Imap $this->_socket = @fsockopen($host, $port); if (!$this->_socket) { - throw new IPF_Exception_Mail('cannot connect to host'); + throw new IPF_Mail_Exception('cannot connect to host'); } if (!$this->_assumedNextLine('* OK')) { - throw new IPF_Exception_Mail('host doesn\'t allow connection'); + throw new IPF_Mail_Exception('host doesn\'t allow connection'); } if ($ssl === 'TLS') { $result = $this->requestAndResponse('STARTTLS'); $result = $result && stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT); if (!$result) { - throw new IPF_Exception_Mail('cannot enable TLS'); + throw new IPF_Mail_Exception('cannot enable TLS'); } } } @@ -49,7 +49,7 @@ class IPF_Mail_Protocol_Imap { $line = @fgets($this->_socket); if ($line === false) { - throw new IPF_Exception_Mail('cannot read - connection closed?'); + throw new IPF_Mail_Exception('cannot read - connection closed?'); } return $line; @@ -204,10 +204,10 @@ class IPF_Mail_Protocol_Imap foreach ($tokens as $token) { if (is_array($token)) { if (@fputs($this->_socket, $line . ' ' . $token[0] . "\r\n") === false) { - throw new IPF_Exception_Mail('cannot write - connection closed?'); + throw new IPF_Mail_Exception('cannot write - connection closed?'); } if (!$this->_assumedNextLine('+ ')) { - throw new IPF_Exception_Mail('cannot send literal string'); + throw new IPF_Mail_Exception('cannot send literal string'); } $line = $token[1]; } else { @@ -216,7 +216,7 @@ class IPF_Mail_Protocol_Imap } if (@fputs($this->_socket, $line . "\r\n") === false) { - throw new IPF_Exception_Mail('cannot write - connection closed?'); + throw new IPF_Mail_Exception('cannot write - connection closed?'); } } @@ -269,7 +269,7 @@ class IPF_Mail_Protocol_Imap if ($this->_socket) { try { $result = $this->requestAndResponse('LOGOUT', array(), true); - } catch (IPF_Exception_Mail $e) { + } catch (IPF_Mail_Exception $e) { // ignoring exception } fclose($this->_socket); @@ -393,7 +393,7 @@ class IPF_Mail_Protocol_Imap } if ($to === null && !is_array($from)) { - throw new IPF_Exception_Mail('the single id was not found in response'); + throw new IPF_Mail_Exception('the single id was not found in response'); } return $result; diff --git a/ipf/mail/protocol/pop3.php b/ipf/mail/protocol/pop3.php index 4648b48..cd148c9 100644 --- a/ipf/mail/protocol/pop3.php +++ b/ipf/mail/protocol/pop3.php @@ -30,7 +30,7 @@ class IPF_Mail_Protocol_Pop3 $this->_socket = @fsockopen($host, $port); if (!$this->_socket) { - throw new IPF_Exception_Mail('cannot connect to host'); + throw new IPF_Mail_Exception('cannot connect to host'); } $welcome = $this->readResponse(); @@ -47,7 +47,7 @@ class IPF_Mail_Protocol_Pop3 $this->request('STLS'); $result = stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT); if (!$result) { - throw new IPF_Exception_Mail('cannot enable TLS'); + throw new IPF_Mail_Exception('cannot enable TLS'); } } @@ -58,7 +58,7 @@ class IPF_Mail_Protocol_Pop3 { $result = @fputs($this->_socket, $request . "\r\n"); if (!$result) { - throw new IPF_Exception_Mail('send failed - connection closed?'); + throw new IPF_Mail_Exception('send failed - connection closed?'); } } @@ -66,7 +66,7 @@ class IPF_Mail_Protocol_Pop3 { $result = @fgets($this->_socket); if (!is_string($result)) { - throw new IPF_Exception_Mail('read failed - connection closed?'); + throw new IPF_Mail_Exception('read failed - connection closed?'); } $result = trim($result); @@ -78,7 +78,7 @@ class IPF_Mail_Protocol_Pop3 } if ($status != '+OK') { - throw new IPF_Exception_Mail('last request failed'); + throw new IPF_Mail_Exception('last request failed'); } if ($multiline) { @@ -107,7 +107,7 @@ class IPF_Mail_Protocol_Pop3 try { $this->request('QUIT'); - } catch (IPF_Exception_Mail $e) { + } catch (IPF_Mail_Exception $e) { // ignore error - we're closing the socket anyway } @@ -127,7 +127,7 @@ class IPF_Mail_Protocol_Pop3 try { $this->request("APOP $user " . md5($this->_timestamp . $password)); return; - } catch (IPF_Exception_Mail $e) { + } catch (IPF_Mail_Exception $e) { // ignore } } @@ -197,7 +197,7 @@ class IPF_Mail_Protocol_Pop3 if ($fallback) { return $this->retrieve($msgno); } else { - throw new IPF_Exception_Mail('top not supported and no fallback wanted'); + throw new IPF_Mail_Exception('top not supported and no fallback wanted'); } } $this->hasTop = true; @@ -206,7 +206,7 @@ class IPF_Mail_Protocol_Pop3 try { $result = $this->request("TOP $msgno $lines", true); - } catch (IPF_Exception_Mail $e) { + } catch (IPF_Mail_Exception $e) { $this->hasTop = false; if ($fallback) { $result = $this->retrieve($msgno); diff --git a/ipf/mail/protocol/smtp.php b/ipf/mail/protocol/smtp.php index c122bf7..008f26f 100644 --- a/ipf/mail/protocol/smtp.php +++ b/ipf/mail/protocol/smtp.php @@ -28,7 +28,7 @@ class IPF_Mail_Protocol_Smtp extends IPF_Mail_Protocol_Abstract break; default: - throw new IPF_Exception_Mail($config['ssl'] . ' is unsupported SSL type'); + throw new IPF_Mail_Exception($config['ssl'] . ' is unsupported SSL type'); break; } } @@ -53,12 +53,12 @@ class IPF_Mail_Protocol_Smtp extends IPF_Mail_Protocol_Abstract { // Respect RFC 2821 and disallow HELO attempts if session is already initiated. if ($this->_sess === true) { - throw new IPF_Exception_Mail('Cannot issue HELO to existing session'); + throw new IPF_Mail_Exception('Cannot issue HELO to existing session'); } // Validate client hostname if (!$this->_validHost->isValid($host)) { - throw new IPF_Exception_Mail(join(', ', $this->_validHost->getMessage())); + throw new IPF_Mail_Exception(join(', ', $this->_validHost->getMessage())); } // Initiate helo sequence @@ -70,7 +70,7 @@ class IPF_Mail_Protocol_Smtp extends IPF_Mail_Protocol_Abstract $this->_send('STARTTLS'); $this->_expect(220, 180); if (!stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) { - throw new IPF_Exception_Mail('Unable to connect via TLS'); + throw new IPF_Mail_Exception('Unable to connect via TLS'); } $this->_ehlo($host); } @@ -85,10 +85,10 @@ class IPF_Mail_Protocol_Smtp extends IPF_Mail_Protocol_Abstract try { $this->_send('EHLO ' . $host); $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2 - } catch (IPF_Exception_Mail $e) { + } catch (IPF_Mail_Exception $e) { $this->_send('HELO ' . $host); $this->_expect(250, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2 - } catch (IPF_Exception_Mail $e) { + } catch (IPF_Mail_Exception $e) { throw $e; } } @@ -96,7 +96,7 @@ class IPF_Mail_Protocol_Smtp extends IPF_Mail_Protocol_Abstract public function mail($from) { if ($this->_sess !== true) { - throw new IPF_Exception_Mail('A valid session has not been started'); + throw new IPF_Mail_Exception('A valid session has not been started'); } $this->_send('MAIL FROM:<' . $from . '>'); @@ -111,7 +111,7 @@ class IPF_Mail_Protocol_Smtp extends IPF_Mail_Protocol_Abstract public function rcpt($to) { if ($this->_mail !== true) { - throw new IPF_Exception_Mail('No sender reverse path has been supplied'); + throw new IPF_Mail_Exception('No sender reverse path has been supplied'); } // Set rcpt to true, as per 4.1.1.3 of RFC 2821 @@ -124,7 +124,7 @@ class IPF_Mail_Protocol_Smtp extends IPF_Mail_Protocol_Abstract { // Ensure recipients have been set if ($this->_rcpt !== true) { - throw new IPF_Exception_Mail('No recipient forward path has been supplied'); + throw new IPF_Mail_Exception('No recipient forward path has been supplied'); } $this->_send('DATA'); @@ -178,7 +178,7 @@ class IPF_Mail_Protocol_Smtp extends IPF_Mail_Protocol_Abstract public function auth() { if ($this->_auth === true) { - throw new IPF_Exception_Mail('Already authenticated for this session'); + throw new IPF_Mail_Exception('Already authenticated for this session'); } } diff --git a/ipf/mail/transport/abstract.php b/ipf/mail/transport/abstract.php index 68e5044..91dcfcb 100644 --- a/ipf/mail/transport/abstract.php +++ b/ipf/mail/transport/abstract.php @@ -50,7 +50,7 @@ abstract class IPF_Mail_Transport_Abstract protected function _prepareHeaders($headers) { if (!$this->_mail) { - throw new IPF_Exception_Mail('Missing IPF_Mail object in _mail property'); + throw new IPF_Mail_Exception('Missing IPF_Mail object in _mail property'); } $this->header = ''; @@ -75,7 +75,7 @@ abstract class IPF_Mail_Transport_Abstract } } if (!$sane) { - throw new IPF_Exception_Mail('At least one mail header line is too long'); + throw new IPF_Mail_Exception('At least one mail header line is too long'); } } @@ -126,7 +126,7 @@ abstract class IPF_Mail_Transport_Abstract } if (!$body) { - throw new IPF_Exception_Mail('No body specified'); + throw new IPF_Mail_Exception('No body specified'); } // Get headers @@ -153,7 +153,7 @@ abstract class IPF_Mail_Transport_Abstract $count = count($this->_parts); $boundary = null; if ($count < 1) { - throw new IPF_Exception_Mail('Empty mail cannot be sent'); + throw new IPF_Mail_Exception('Empty mail cannot be sent'); } if ($count > 1) { diff --git a/ipf/mail/transport/sendmail.php b/ipf/mail/transport/sendmail.php index 487b654..6a16fc1 100644 --- a/ipf/mail/transport/sendmail.php +++ b/ipf/mail/transport/sendmail.php @@ -28,14 +28,14 @@ class IPF_Mail_Transport_Sendmail extends IPF_Mail_Transport_Abstract $this->parameters); } if (!$result) { - throw new IPF_Exception_Mail('Unable to send mail'); + throw new IPF_Mail_Exception('Unable to send mail'); } } protected function _prepareHeaders($headers) { if (!$this->_mail) { - throw new IPF_Exception_Mail('_prepareHeaders requires a registered IPF_Mail object'); + throw new IPF_Mail_Exception('_prepareHeaders requires a registered IPF_Mail object'); } // mail() uses its $to parameter to set the To: header, and the $subject @@ -43,12 +43,12 @@ class IPF_Mail_Transport_Sendmail extends IPF_Mail_Transport_Abstract if (0 === strpos(PHP_OS, 'WIN')) { // If the current recipients list is empty, throw an error if (empty($this->recipients)) { - throw new IPF_Exception_Mail('Missing To addresses'); + throw new IPF_Mail_Exception('Missing To addresses'); } } else { // All others, simply grab the recipients and unset the To: header if (!isset($headers['To'])) { - throw new IPF_Exception_Mail('Missing To header'); + throw new IPF_Mail_Exception('Missing To header'); } unset($headers['To']['append']); diff --git a/ipf/mail/transport/smtp.php b/ipf/mail/transport/smtp.php index d37ded4..e099a4d 100644 --- a/ipf/mail/transport/smtp.php +++ b/ipf/mail/transport/smtp.php @@ -31,7 +31,7 @@ class IPF_Mail_Transport_Smtp extends IPF_Mail_Transport_Abstract if ($this->_connection instanceof IPF_Mail_Protocol_Smtp) { try { $this->_connection->quit(); - } catch (IPF_Exception_Mail $e) { + } catch (IPF_Mail_Exception $e) { // ignore } $this->_connection->disconnect(); @@ -80,7 +80,7 @@ class IPF_Mail_Transport_Smtp extends IPF_Mail_Transport_Abstract protected function _prepareHeaders($headers) { if (!$this->_mail) { - throw new IPF_Exception_Mail('_prepareHeaders requires a registered IPF_Mail object'); + throw new IPF_Mail_Exception('_prepareHeaders requires a registered IPF_Mail object'); } unset($headers['Bcc']); diff --git a/ipf/mime/decode.php b/ipf/mime/decode.php index a1e92de..e4a5d42 100644 --- a/ipf/mime/decode.php +++ b/ipf/mime/decode.php @@ -29,7 +29,7 @@ class IPF_Mime_Decode // no more parts, find end boundary $p = strpos($body, '--' . $boundary . '--', $start); if ($p===false) { - throw new IPF_Exception('Not a valid Mime Message: End Missing'); + throw new IPF_Mime_Exception('Not a valid Mime Message: End Missing'); } // the remaining part also needs to be parsed: @@ -117,7 +117,7 @@ class IPF_Mime_Decode $field = $firstName . '=' . $field; if (!preg_match_all('%([^=\s]+)\s*=("[^"]+"|[^;]+)(;\s*|$)%', $field, $matches)) { - throw new IPF_Exception('not a valid header field'); + throw new IPF_Mime_Exception('not a valid header field'); } if ($wantedPart) { diff --git a/ipf/mime/exception.php b/ipf/mime/exception.php new file mode 100644 index 0000000..54d46df --- /dev/null +++ b/ipf/mime/exception.php @@ -0,0 +1,6 @@ +description = $value; break; default: - throw new IPF_Exception('Unknown header ignored for MimePart:' . $key); + throw new IPF_Mime_Exception('Unknown header ignored for MimePart:' . $key); } } $res->addPart($newPart); diff --git a/ipf/mime/part.php b/ipf/mime/part.php index 68e9b23..1fbc583 100644 --- a/ipf/mime/part.php +++ b/ipf/mime/part.php @@ -30,7 +30,7 @@ class IPF_Mime_Part { public function getEncodedStream() { if (!$this->_isStream) { - throw new IPF_Exception('Attempt to get a stream from a string part'); + throw new IPF_Mime_Exception('Attempt to get a stream from a string part'); } //stream_filter_remove(); // ??? is that right? @@ -46,7 +46,7 @@ class IPF_Mime_Part { ) ); if (!is_resource($filter)) { - throw new IPF_Exception('Failed to append quoted-printable filter'); + throw new IPF_Mime_Exception('Failed to append quoted-printable filter'); } break; case IPF_Mime::ENCODING_BASE64: @@ -60,7 +60,7 @@ class IPF_Mime_Part { ) ); if (!is_resource($filter)) { - throw new IPF_Exception('Failed to append base64 filter'); + throw new IPF_Mime_Exception('Failed to append base64 filter'); } break; default: