Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#2210 from weierophinne…
Browse files Browse the repository at this point in the history
…y/hotfix/remove-suppression-operator

Get rid of error suppression
  • Loading branch information
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 47 deletions.
14 changes: 9 additions & 5 deletions src/Protocol/Imap.php
Expand Up @@ -10,6 +10,8 @@

namespace Zend\Mail\Protocol;

use Zend\Stdlib\ErrorHandler;

/**
* @category Zend
* @package Zend_Mail
Expand Down Expand Up @@ -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')) {
Expand Down
26 changes: 17 additions & 9 deletions src/Protocol/Pop3.php
Expand Up @@ -10,6 +10,8 @@

namespace Zend\Mail\Protocol;

use Zend\Stdlib\ErrorHandler;

/**
* @category Zend
* @package Zend_Mail
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Storage/Folder/Maildir.php
Expand Up @@ -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();

Expand Down
12 changes: 6 additions & 6 deletions src/Storage/Maildir.php
Expand Up @@ -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);
}
}

Expand Down
21 changes: 14 additions & 7 deletions src/Storage/Mbox.php
Expand Up @@ -246,18 +246,20 @@ 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);

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);
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/Storage/Message.php
Expand Up @@ -10,6 +10,8 @@

namespace Zend\Mail\Storage;

use Zend\Stdlib\ErrorHandler;

/**
* @category Zend
* @package Zend_Mail
Expand All @@ -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']);
Expand Down
45 changes: 30 additions & 15 deletions src/Storage/Writable/Maildir.php
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a4b3fcc

Please sign in to comment.