diff --git a/src/Protocol/Imap.php b/src/Protocol/Imap.php index 5947b444..23caa519 100644 --- a/src/Protocol/Imap.php +++ b/src/Protocol/Imap.php @@ -10,6 +10,8 @@ namespace Zend\Mail\Protocol; +use Zend\Stdlib\ErrorHandler; + /** * @category Zend * @package Zend_Mail @@ -76,12 +78,14 @@ public function connect($host, $port = null, $ssl = false) $port = $ssl === 'SSL' ? 993 : 143; } - $errno = 0; - $errstr = ''; - $this->socket = @fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION); + ErrorHandler::start(); + $this->socket = fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION); + $error = ErrorHandler::stop(); if (!$this->socket) { - throw new Exception\RuntimeException('cannot connect to host; error = ' . $errstr . - ' (errno = ' . $errno . ' )'); + throw new Exception\RuntimeException(sprintf( + 'cannot connect to host%s', + ($error ? sprintf('; error = %s (errno = %d )', $error->getMessage(), $error->getCode()) : '') + ), 0, $error); } if (!$this->_assumedNextLine('* OK')) { diff --git a/src/Protocol/Pop3.php b/src/Protocol/Pop3.php index 22e1cd80..92e41169 100644 --- a/src/Protocol/Pop3.php +++ b/src/Protocol/Pop3.php @@ -10,6 +10,8 @@ namespace Zend\Mail\Protocol; +use Zend\Stdlib\ErrorHandler; + /** * @category Zend * @package Zend_Mail @@ -84,12 +86,14 @@ public function connect($host, $port = null, $ssl = false) $port = $ssl == 'SSL' ? 995 : 110; } - $errno = 0; - $errstr = ''; - $this->socket = @fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION); + ErrorHandler::start(); + $this->socket = fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION); + $error = ErrorHandler::stop(); if (!$this->socket) { - throw new Exception\RuntimeException('cannot connect to host; error = ' . $errstr - . ' (errno = ' . $errno . ' )'); + throw new Exception\RuntimeException(sprintf( + 'cannot connect to host%s', + ($error ? sprintf('; error = %s (errno = %d )', $error->getMessage(), $error->getCode()) : '') + ), 0, $error); } $welcome = $this->readResponse(); @@ -122,9 +126,11 @@ public function connect($host, $port = null, $ssl = false) */ public function sendRequest($request) { - $result = @fputs($this->socket, $request . "\r\n"); + ErrorHandler::start(); + $result = fputs($this->socket, $request . "\r\n"); + $error = ErrorHandler::stop(); if (!$result) { - throw new Exception\RuntimeException('send failed - connection closed?'); + throw new Exception\RuntimeException('send failed - connection closed?', 0, $error); } } @@ -138,9 +144,11 @@ public function sendRequest($request) */ public function readResponse($multiline = false) { - $result = @fgets($this->socket); + ErrorHandler::start(); + $result = fgets($this->socket); + $error = ErrorHandler::stop(); if (!is_string($result)) { - throw new Exception\RuntimeException('read failed - connection closed?'); + throw new Exception\RuntimeException('read failed - connection closed?', 0, $error); } $result = trim($result); diff --git a/src/Storage/Folder/Maildir.php b/src/Storage/Folder/Maildir.php index d5d96560..7155bd33 100644 --- a/src/Storage/Folder/Maildir.php +++ b/src/Storage/Folder/Maildir.php @@ -89,10 +89,10 @@ protected function _buildFolderTree() $this->rootFolder->INBOX = new Storage\Folder('INBOX', 'INBOX', true); ErrorHandler::start(E_WARNING); - $dh = opendir($this->rootdir); - ErrorHandler::stop(); + $dh = opendir($this->rootdir); + $error = ErrorHandler::stop(); if (!$dh) { - throw new Exception\RuntimeException("can't read folders in maildir"); + throw new Exception\RuntimeException("can't read folders in maildir", 0, $error); } $dirs = array(); diff --git a/src/Storage/Maildir.php b/src/Storage/Maildir.php index efc56bd1..8c569c0c 100644 --- a/src/Storage/Maildir.php +++ b/src/Storage/Maildir.php @@ -267,22 +267,22 @@ protected function _openMaildir($dirname) } ErrorHandler::start(E_WARNING); - $dh = opendir($dirname . '/cur/'); - ErrorHandler::stop(); + $dh = opendir($dirname . '/cur/'); + $error = ErrorHandler::stop(); if (!$dh) { - throw new Exception\RuntimeException('cannot open maildir'); + throw new Exception\RuntimeException('cannot open maildir', 0, $error); } $this->_getMaildirFiles($dh, $dirname . '/cur/'); closedir($dh); ErrorHandler::start(E_WARNING); - $dh = opendir($dirname . '/new/'); - ErrorHandler::stop(); + $dh = opendir($dirname . '/new/'); + $error = ErrorHandler::stop(); if ($dh) { $this->_getMaildirFiles($dh, $dirname . '/new/', array(Mail\Storage::FLAG_RECENT)); closedir($dh); } elseif (file_exists($dirname . '/new/')) { - throw new Exception\RuntimeException('cannot read recent mails in maildir'); + throw new Exception\RuntimeException('cannot read recent mails in maildir', 0, $error); } } diff --git a/src/Storage/Mbox.php b/src/Storage/Mbox.php index cbdd1bdc..196da7c4 100644 --- a/src/Storage/Mbox.php +++ b/src/Storage/Mbox.php @@ -246,9 +246,11 @@ protected function openMboxFile($filename) $this->close(); } - $this->fh = @fopen($filename, 'r'); + ErrorHandler::start(); + $this->fh = fopen($filename, 'r'); + $error = ErrorHandler::stop(); if (!$this->fh) { - throw new Exception\RuntimeException('cannot open mbox file'); + throw new Exception\RuntimeException('cannot open mbox file', 0, $error); } $this->filename = $filename; $this->filemtime = filemtime($this->filename); @@ -256,8 +258,8 @@ protected function openMboxFile($filename) if (!$this->isMboxFile($this->fh, false)) { ErrorHandler::start(E_WARNING); fclose($this->fh); - ErrorHandler::stop(); - throw new Exception\InvalidArgumentException('file is not a valid mbox format'); + $error = ErrorHandler::stop(); + throw new Exception\InvalidArgumentException('file is not a valid mbox format', 0, $error); } $messagePos = array('start' => ftell($this->fh), 'separator' => 0, 'end' => 0); @@ -380,13 +382,18 @@ public function __sleep() */ public function __wakeup() { - if ($this->filemtime != @filemtime($this->filename)) { + ErrorHandler::start(); + $filemtime = filemtime($this->filename); + ErrorHandler::stop(); + if ($this->filemtime != $filemtime) { $this->close(); $this->openMboxFile($this->filename); } else { - $this->fh = @fopen($this->filename, 'r'); + ErrorHandler::start(); + $this->fh = fopen($this->filename, 'r'); + $error = ErrorHandler::stop(); if (!$this->fh) { - throw new Exception\RuntimeException('cannot open mbox file'); + throw new Exception\RuntimeException('cannot open mbox file', 0, $error); } } } diff --git a/src/Storage/Message.php b/src/Storage/Message.php index b86d92a3..85a747b6 100644 --- a/src/Storage/Message.php +++ b/src/Storage/Message.php @@ -10,6 +10,8 @@ namespace Zend\Mail\Storage; +use Zend\Stdlib\ErrorHandler; + /** * @category Zend * @package Zend_Mail @@ -36,9 +38,11 @@ public function __construct(array $params) { if (isset($params['file'])) { if (!is_resource($params['file'])) { - $params['raw'] = @file_get_contents($params['file']); + ErrorHandler::start(); + $params['raw'] = file_get_contents($params['file']); + $error = ErrorHandler::stop(); if ($params['raw'] === false) { - throw new Exception\RuntimeException('could not open file'); + throw new Exception\RuntimeException('could not open file', 0, $error); } } else { $params['raw'] = stream_get_contents($params['file']); diff --git a/src/Storage/Writable/Maildir.php b/src/Storage/Writable/Maildir.php index b0d683d6..a4b4c373 100644 --- a/src/Storage/Writable/Maildir.php +++ b/src/Storage/Writable/Maildir.php @@ -48,23 +48,29 @@ public static function initMaildir($dir) throw new StorageException\InvalidArgumentException('maildir must be a directory if already exists'); } } else { - if (!mkdir($dir)) { + ErrorHandler::start(); + $test = mkdir($dir); + $error = ErrorHandler::stop(); + if (!$test) { $dir = dirname($dir); if (!file_exists($dir)) { - throw new StorageException\InvalidArgumentException("parent $dir not found"); + throw new StorageException\InvalidArgumentException("parent $dir not found", 0, $error); } elseif (!is_dir($dir)) { - throw new StorageException\InvalidArgumentException("parent $dir not a directory"); + throw new StorageException\InvalidArgumentException("parent $dir not a directory", 0, $error); } else { - throw new StorageException\RuntimeException('cannot create maildir'); + throw new StorageException\RuntimeException('cannot create maildir', 0, $error); } } } foreach (array('cur', 'tmp', 'new') as $subdir) { - if (!@mkdir($dir . DIRECTORY_SEPARATOR . $subdir)) { + ErrorHandler::start(); + $test = mkdir($dir . DIRECTORY_SEPARATOR . $subdir); + $error = ErrorHandler::stop(); + if (!$test) { // ignore if dir exists (i.e. was already valid maildir or two processes try to create one) if (!file_exists($dir . DIRECTORY_SEPARATOR . $subdir)) { - throw new StorageException\RuntimeException('could not create subdir ' . $subdir); + throw new StorageException\RuntimeException('could not create subdir ' . $subdir, 0, $error); } } } @@ -155,9 +161,12 @@ public function createFolder($name, $parentFolder = null) } } - if (!@mkdir($fulldir) || !@mkdir($fulldir . DIRECTORY_SEPARATOR . 'cur')) { - throw new StorageException\RuntimeException('error while creating new folder, may be created incompletely'); + ErrorHandler::start(); + if (!mkdir($fulldir) || !mkdir($fulldir . DIRECTORY_SEPARATOR . 'cur')) { + $error = ErrorHandler::stop(); + throw new StorageException\RuntimeException('error while creating new folder, may be created incompletely', 0, $error); } + ErrorHandler::stop(); mkdir($fulldir . DIRECTORY_SEPARATOR . 'new'); mkdir($fulldir . DIRECTORY_SEPARATOR . 'tmp'); @@ -639,8 +648,11 @@ public function setFlags($id, $flags) // NOTE: double dirname to make sure we always move to cur. if recent flag has been set (message is in new) it will be moved to cur. $new_filename = dirname(dirname($filedata['filename'])) . DIRECTORY_SEPARATOR . 'cur' . DIRECTORY_SEPARATOR . "$filedata[uniq]$info"; - if (!@rename($filedata['filename'], $new_filename)) { - throw new StorageException\RuntimeException('cannot rename file'); + ErrorHandler::start(); + $test = rename($filedata['filename'], $new_filename); + $error = ErrorHandler::stop(); + if (!$test) { + throw new StorageException\RuntimeException('cannot rename file', 0, $error); } $filedata['flags'] = $flags; @@ -664,8 +676,11 @@ public function removeMessage($id) $size = filesize($filename); } - if (!@unlink($filename)) { - throw new StorageException\RuntimeException('cannot remove message'); + ErrorHandler::start(); + $test = unlink($filename); + $error = ErrorHandler::stop(); + if (!$test) { + throw new StorageException\RuntimeException('cannot remove message', 0, $error); } unset($this->files[$id - 1]); // remove the gap @@ -702,10 +717,10 @@ public function getQuota($fromStorage = false) { if ($fromStorage) { ErrorHandler::start(E_WARNING); - $fh = fopen($this->rootdir . 'maildirsize', 'r'); - ErrorHandler::stop(); + $fh = fopen($this->rootdir . 'maildirsize', 'r'); + $error = ErrorHandler::stop(); if (!$fh) { - throw new StorageException\RuntimeException('cannot open maildirsize'); + throw new StorageException\RuntimeException('cannot open maildirsize', 0, $error); } $definition = fgets($fh); fclose($fh);