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

Commit

Permalink
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 72 deletions.
8 changes: 4 additions & 4 deletions src/Protocol/Smtp/Auth/Crammd5.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ protected function _hmacMd5($key, $data, $block = 64)
$key = str_pad($key, $block, "\0");
}

$k_ipad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
$k_opad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);
$kIpad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
$kOpad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);

$inner = pack('H32', md5($k_ipad . $data));
$digest = md5($k_opad . $inner);
$inner = pack('H32', md5($kIpad . $data));
$digest = md5($kOpad . $inner);

return $digest;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Storage/Maildir.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ protected function _openMaildir($dirname)
*
* @param resource $dh dir handle used for search
* @param string $dirname dirname of dir in $dh
* @param array $default_flags default flags for given dir
* @param array $defaultFlags default flags for given dir
*/
protected function _getMaildirFiles($dh, $dirname, $default_flags = array())
protected function _getMaildirFiles($dh, $dirname, $defaultFlags = array())
{
while (($entry = readdir($dh)) !== false) {
if ($entry[0] == '.' || !is_file($dirname . $entry)) {
Expand All @@ -318,16 +318,16 @@ protected function _getMaildirFiles($dh, $dirname, $default_flags = array())
$flags = '';
}

$named_flags = $default_flags;
$namedFlags = $defaultFlags;
$length = strlen($flags);
for ($i = 0; $i < $length; ++$i) {
$flag = $flags[$i];
$named_flags[$flag] = isset(static::$knownFlags[$flag]) ? static::$knownFlags[$flag] : $flag;
$namedFlags[$flag] = isset(static::$knownFlags[$flag]) ? static::$knownFlags[$flag] : $flag;
}

$data = array('uniq' => $uniq,
'flags' => $named_flags,
'flaglookup' => array_flip($named_flags),
'flags' => $namedFlags,
'flaglookup' => array_flip($namedFlags),
'filename' => $dirname . $entry);
if ($size !== null) {
$data['size'] = (int) $size;
Expand Down
124 changes: 62 additions & 62 deletions src/Storage/Writable/Maildir.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,10 @@ protected function _createTmpFile($folder = 'INBOX')
// we should retry to create a unique id if a file with the same name exists
// to avoid a script timeout we only wait 1 second (instead of 2) and stop
// after a defined retry count
// if you change this variable take into account that it can take up to $max_tries seconds
// if you change this variable take into account that it can take up to $maxTries seconds
// normally we should have a valid unique name after the first try, we're just following the "standard" here
$max_tries = 5;
for ($i = 0; $i < $max_tries; ++$i) {
$maxTries = 5;
for ($i = 0; $i < $maxTries; ++$i) {
$uniq = $this->_createUniqueId();
if (!file_exists($tmpdir . $uniq)) {
// here is the race condition! - as defined in the standard
Expand All @@ -384,7 +384,7 @@ protected function _createTmpFile($folder = 'INBOX')
}

if (!$fh) {
throw new StorageException\RuntimeException("tried $max_tries unique ids for a temp file, but all were taken"
throw new StorageException\RuntimeException("tried $maxTries unique ids for a temp file, but all were taken"
. ' - giving up');
}

Expand All @@ -404,25 +404,25 @@ protected function _createTmpFile($folder = 'INBOX')
protected function _getInfoString(&$flags)
{
// accessing keys is easier, faster and it removes duplicated flags
$wanted_flags = array_flip($flags);
if (isset($wanted_flags[Storage::FLAG_RECENT])) {
$wantedFlags = array_flip($flags);
if (isset($wantedFlags[Storage::FLAG_RECENT])) {
throw new StorageException\InvalidArgumentException('recent flag may not be set');
}

$info = ':2,';
$flags = array();
foreach (Storage\Maildir::$knownFlags as $char => $flag) {
if (!isset($wanted_flags[$flag])) {
if (!isset($wantedFlags[$flag])) {
continue;
}
$info .= $char;
$flags[$char] = $flag;
unset($wanted_flags[$flag]);
unset($wantedFlags[$flag]);
}

if (!empty($wanted_flags)) {
$wanted_flags = implode(', ', array_keys($wanted_flags));
throw new StorageException\InvalidArgumentException('unknown flag(s): ' . $wanted_flags);
if (!empty($wantedFlags)) {
$wantedFlags = implode(', ', array_keys($wantedFlags));
throw new StorageException\InvalidArgumentException('unknown flag(s): ' . $wantedFlags);
}

return $info;
Expand Down Expand Up @@ -456,44 +456,44 @@ public function appendMessage($message, $folder = null, $flags = null, $recent =
if ($flags === null) {
$flags = array(Storage::FLAG_SEEN);
}
$info = $this->_getInfoString($flags);
$temp_file = $this->_createTmpFile($folder->getGlobalName());
$info = $this->_getInfoString($flags);
$tempFile = $this->_createTmpFile($folder->getGlobalName());

// TODO: handle class instances for $message
if (is_resource($message) && get_resource_type($message) == 'stream') {
stream_copy_to_stream($message, $temp_file['handle']);
stream_copy_to_stream($message, $tempFile['handle']);
} else {
fwrite($temp_file['handle'], $message);
fwrite($tempFile['handle'], $message);
}
fclose($temp_file['handle']);
fclose($tempFile['handle']);

// we're adding the size to the filename for maildir++
$size = filesize($temp_file['filename']);
$size = filesize($tempFile['filename']);
if ($size !== false) {
$info = ',S=' . $size . $info;
}
$new_filename = $temp_file['dirname'] . DIRECTORY_SEPARATOR;
$new_filename .= $recent ? 'new' : 'cur';
$new_filename .= DIRECTORY_SEPARATOR . $temp_file['uniq'] . $info;
$newFilename = $tempFile['dirname'] . DIRECTORY_SEPARATOR;
$newFilename .= $recent ? 'new' : 'cur';
$newFilename .= DIRECTORY_SEPARATOR . $tempFile['uniq'] . $info;

// we're throwing any exception after removing our temp file and saving it to this variable instead
$exception = null;

if (!link($temp_file['filename'], $new_filename)) {
if (!link($tempFile['filename'], $newFilename)) {
$exception = new StorageException\RuntimeException('cannot link message file to final dir');
}

ErrorHandler::start(E_WARNING);
unlink($temp_file['filename']);
unlink($tempFile['filename']);
ErrorHandler::stop();

if ($exception) {
throw $exception;
}

$this->files[] = array('uniq' => $temp_file['uniq'],
$this->files[] = array('uniq' => $tempFile['uniq'],
'flags' => $flags,
'filename' => $new_filename);
'filename' => $newFilename);
if ($this->quota) {
$this->_addQuotaEntry((int) $size, 1);
}
Expand All @@ -517,7 +517,7 @@ public function copyMessage($id, $folder)
}

$filedata = $this->_getFileData($id);
$old_file = $filedata['filename'];
$oldFile = $filedata['filename'];
$flags = $filedata['flags'];

// copied message can't be recent
Expand All @@ -527,29 +527,29 @@ public function copyMessage($id, $folder)
$info = $this->_getInfoString($flags);

// we're creating the copy as temp file before moving to cur/
$temp_file = $this->_createTmpFile($folder->getGlobalName());
$tempFile = $this->_createTmpFile($folder->getGlobalName());
// we don't write directly to the file
fclose($temp_file['handle']);
fclose($tempFile['handle']);

// we're adding the size to the filename for maildir++
$size = filesize($old_file);
$size = filesize($oldFile);
if ($size !== false) {
$info = ',S=' . $size . $info;
}

$new_file = $temp_file['dirname'] . DIRECTORY_SEPARATOR . 'cur' . DIRECTORY_SEPARATOR . $temp_file['uniq'] . $info;
$newFile = $tempFile['dirname'] . DIRECTORY_SEPARATOR . 'cur' . DIRECTORY_SEPARATOR . $tempFile['uniq'] . $info;

// we're throwing any exception after removing our temp file and saving it to this variable instead
$exception = null;

if (!copy($old_file, $temp_file['filename'])) {
if (!copy($oldFile, $tempFile['filename'])) {
$exception = new StorageException\RuntimeException('cannot copy message file');
} elseif (!link($temp_file['filename'], $new_file)) {
} elseif (!link($tempFile['filename'], $newFile)) {
$exception = new StorageException\RuntimeException('cannot link message file to final dir');
}

ErrorHandler::start(E_WARNING);
unlink($temp_file['filename']);
unlink($tempFile['filename']);
ErrorHandler::stop();

if ($exception) {
Expand All @@ -559,9 +559,9 @@ public function copyMessage($id, $folder)
if ($folder->getGlobalName() == $this->currentFolder
|| ($this->currentFolder == 'INBOX' && $folder->getGlobalName() == '/')
) {
$this->files[] = array('uniq' => $temp_file['uniq'],
$this->files[] = array('uniq' => $tempFile['uniq'],
'flags' => $flags,
'filename' => $new_file);
'filename' => $newFile);
}

if ($this->quota) {
Expand Down Expand Up @@ -589,7 +589,7 @@ public function moveMessage($id, $folder)
}

$filedata = $this->_getFileData($id);
$old_file = $filedata['filename'];
$oldFile = $filedata['filename'];
$flags = $filedata['flags'];

// moved message can't be recent
Expand All @@ -599,26 +599,26 @@ public function moveMessage($id, $folder)
$info = $this->_getInfoString($flags);

// reserving a new name
$temp_file = $this->_createTmpFile($folder->getGlobalName());
fclose($temp_file['handle']);
$tempFile = $this->_createTmpFile($folder->getGlobalName());
fclose($tempFile['handle']);

// we're adding the size to the filename for maildir++
$size = filesize($old_file);
$size = filesize($oldFile);
if ($size !== false) {
$info = ',S=' . $size . $info;
}

$new_file = $temp_file['dirname'] . DIRECTORY_SEPARATOR . 'cur' . DIRECTORY_SEPARATOR . $temp_file['uniq'] . $info;
$newFile = $tempFile['dirname'] . DIRECTORY_SEPARATOR . 'cur' . DIRECTORY_SEPARATOR . $tempFile['uniq'] . $info;

// we're throwing any exception after removing our temp file and saving it to this variable instead
$exception = null;

if (!rename($old_file, $new_file)) {
if (!rename($oldFile, $newFile)) {
$exception = new StorageException\RuntimeException('cannot move message file');
}

ErrorHandler::start(E_WARNING);
unlink($temp_file['filename']);
unlink($tempFile['filename']);
ErrorHandler::stop();

if ($exception) {
Expand Down Expand Up @@ -646,17 +646,17 @@ public function setFlags($id, $flags)
$filedata = $this->_getFileData($id);

// 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";
$newFilename = dirname(dirname($filedata['filename'])) . DIRECTORY_SEPARATOR . 'cur' . DIRECTORY_SEPARATOR . "$filedata[uniq]$info";

ErrorHandler::start();
$test = rename($filedata['filename'], $new_filename);
$test = rename($filedata['filename'], $newFilename);
$error = ErrorHandler::stop();
if (!$test) {
throw new StorageException\RuntimeException('cannot rename file', 0, $error);
}

$filedata['flags'] = $flags;
$filedata['filename'] = $new_filename;
$filedata['filename'] = $newFilename;

$this->files[$id - 1] = $filedata;
}
Expand Down Expand Up @@ -748,7 +748,7 @@ protected function _calculateMaildirsize()
{
$timestamps = array();
$messages = 0;
$total_size = 0;
$totalSize = 0;

if (is_array($this->quota)) {
$quota = $this->quota;
Expand Down Expand Up @@ -798,7 +798,7 @@ protected function _calculateMaildirsize()
strtok($entry, '=');
$filesize = strtok(':');
if (is_numeric($filesize)) {
$total_size += $filesize;
$totalSize += $filesize;
++$messages;
continue;
}
Expand All @@ -808,7 +808,7 @@ protected function _calculateMaildirsize()
// ignore, as we assume file got removed
continue;
}
$total_size += $size;
$totalSize += $size;
++$messages;
}
}
Expand All @@ -825,7 +825,7 @@ protected function _calculateMaildirsize()
}
$definition = implode(',', $definition);
fwrite($fh, "$definition\n");
fwrite($fh, "$total_size $messages\n");
fwrite($fh, "$totalSize $messages\n");
fclose($fh);
rename($tmp['filename'], $this->rootdir . 'maildirsize');
foreach ($timestamps as $dir => $timestamp) {
Expand All @@ -835,7 +835,7 @@ protected function _calculateMaildirsize()
}
}

return array('size' => $total_size,
return array('size' => $totalSize,
'count' => $messages,
'quota' => $quota);
}
Expand All @@ -848,7 +848,7 @@ protected function _calculateMaildirsize()
protected function _calculateQuota($forceRecalc = false)
{
$fh = null;
$total_size = 0;
$totalSize = 0;
$messages = 0;
$maildirsize = '';
if (!$forceRecalc && file_exists($this->rootdir . 'maildirsize') && filesize($this->rootdir . 'maildirsize') < 5120) {
Expand All @@ -864,7 +864,7 @@ protected function _calculateQuota($forceRecalc = false)
}
if (!$fh) {
$result = $this->_calculateMaildirsize();
$total_size = $result['size'];
$totalSize = $result['size'];
$messages = $result['count'];
$quota = $result['quota'];
} else {
Expand All @@ -885,37 +885,37 @@ protected function _calculateQuota($forceRecalc = false)
unset($maildirsize[0]);
foreach ($maildirsize as $line) {
list($size, $count) = explode(' ', trim($line));
$total_size += $size;
$totalSize += $size;
$messages += $count;
}
}

$over_quota = false;
$over_quota = $over_quota || (isset($quota['size']) && $total_size > $quota['size']);
$over_quota = $over_quota || (isset($quota['count']) && $messages > $quota['count']);
$overQuota = false;
$overQuota = $overQuota || (isset($quota['size']) && $totalSize > $quota['size']);
$overQuota = $overQuota || (isset($quota['count']) && $messages > $quota['count']);
// NOTE: $maildirsize equals false if it wasn't set (AKA we recalculated) or it's only
// one line, because $maildirsize[0] gets unsetted.
// Also we're using local time to calculate the 15 minute offset. Touching a file just for known the
// local time of the file storage isn't worth the hassle.
if ($over_quota && ($maildirsize || filemtime($this->rootdir . 'maildirsize') > time() - 900)) {
if ($overQuota && ($maildirsize || filemtime($this->rootdir . 'maildirsize') > time() - 900)) {
$result = $this->_calculateMaildirsize();
$total_size = $result['size'];
$totalSize = $result['size'];
$messages = $result['count'];
$quota = $result['quota'];
$over_quota = false;
$over_quota = $over_quota || (isset($quota['size']) && $total_size > $quota['size']);
$over_quota = $over_quota || (isset($quota['count']) && $messages > $quota['count']);
$overQuota = false;
$overQuota = $overQuota || (isset($quota['size']) && $totalSize > $quota['size']);
$overQuota = $overQuota || (isset($quota['count']) && $messages > $quota['count']);
}

if ($fh) {
// TODO is there a safe way to keep the handle open for writing?
fclose($fh);
}

return array('size' => $total_size,
return array('size' => $totalSize,
'count' => $messages,
'quota' => $quota,
'over_quota' => $over_quota);
'over_quota' => $overQuota);
}

protected function _addQuotaEntry($size, $count = 1)
Expand Down

0 comments on commit dc02847

Please sign in to comment.