00001 <?php
00027 require_once 'Zend/Mail/Storage/Folder/Maildir.php';
00028
00032 require_once 'Zend/Mail/Storage/Writable/Interface.php';
00033
00034
00042 class Zend_Mail_Storage_Writable_Maildir extends Zend_Mail_Storage_Folder_Maildir
00043 implements Zend_Mail_Storage_Writable_Interface
00044 {
00045
00046
00051 protected $_quota;
00052
00062 public static function initMaildir($dir)
00063 {
00064 if (file_exists($dir)) {
00065 if (!is_dir($dir)) {
00069 require_once 'Zend/Mail/Storage/Exception.php';
00070 throw new Zend_Mail_Storage_Exception('maildir must be a directory if already exists');
00071 }
00072 } else {
00073 if (!mkdir($dir)) {
00077 require_once 'Zend/Mail/Storage/Exception.php';
00078 $dir = dirname($dir);
00079 if (!file_exists($dir)) {
00080 throw new Zend_Mail_Storage_Exception("parent $dir not found");
00081 } else if (!is_dir($dir)) {
00082 throw new Zend_Mail_Storage_Exception("parent $dir not a directory");
00083 } else {
00084 throw new Zend_Mail_Storage_Exception('cannot create maildir');
00085 }
00086 }
00087 }
00088
00089 foreach (array('cur', 'tmp', 'new') as $subdir) {
00090 if (!@mkdir($dir . DIRECTORY_SEPARATOR . $subdir)) {
00091
00092 if (!file_exists($dir . DIRECTORY_SEPARATOR . $subdir)) {
00096 require_once 'Zend/Mail/Storage/Exception.php';
00097 throw new Zend_Mail_Storage_Exception('could not create subdir ' . $subdir);
00098 }
00099 }
00100 }
00101 }
00102
00111 public function __construct($params) {
00112 if (is_array($params)) {
00113 $params = (object)$params;
00114 }
00115
00116 if (!empty($params->create) && isset($params->dirname) && !file_exists($params->dirname . DIRECTORY_SEPARATOR . 'cur')) {
00117 self::initMaildir($params->dirname);
00118 }
00119
00120 parent::__construct($params);
00121 }
00122
00134 public function createFolder($name, $parentFolder = null)
00135 {
00136 if ($parentFolder instanceof Zend_Mail_Storage_Folder) {
00137 $folder = $parentFolder->getGlobalName() . $this->_delim . $name;
00138 } else if ($parentFolder != null) {
00139 $folder = rtrim($parentFolder, $this->_delim) . $this->_delim . $name;
00140 } else {
00141 $folder = $name;
00142 }
00143
00144 $folder = trim($folder, $this->_delim);
00145
00146
00147 $exists = null;
00148 try {
00149 $exists = $this->getFolders($folder);
00150 } catch (Zend_Mail_Exception $e) {
00151
00152 }
00153 if ($exists) {
00157 require_once 'Zend/Mail/Storage/Exception.php';
00158 throw new Zend_Mail_Storage_Exception('folder already exists');
00159 }
00160
00161 if (strpos($folder, $this->_delim . $this->_delim) !== false) {
00165 require_once 'Zend/Mail/Storage/Exception.php';
00166 throw new Zend_Mail_Storage_Exception('invalid name - folder parts may not be empty');
00167 }
00168
00169 if (strpos($folder, 'INBOX' . $this->_delim) === 0) {
00170 $folder = substr($folder, 6);
00171 }
00172
00173 $fulldir = $this->_rootdir . '.' . $folder;
00174
00175
00176 if (strpos($folder, DIRECTORY_SEPARATOR) !== false || strpos($folder, '/') !== false
00177 || dirname($fulldir) . DIRECTORY_SEPARATOR != $this->_rootdir) {
00181 require_once 'Zend/Mail/Storage/Exception.php';
00182 throw new Zend_Mail_Storage_Exception('invalid name - no directory seprator allowed in folder name');
00183 }
00184
00185
00186 $parent = null;
00187 if (strpos($folder, $this->_delim)) {
00188
00189 $parent = substr($folder, 0, strrpos($folder, $this->_delim));
00190 try {
00191 $this->getFolders($parent);
00192 } catch (Zend_Mail_Exception $e) {
00193
00194 $this->createFolder($parent);
00195 }
00196 }
00197
00198 if (!@mkdir($fulldir) || !@mkdir($fulldir . DIRECTORY_SEPARATOR . 'cur')) {
00202 require_once 'Zend/Mail/Storage/Exception.php';
00203 throw new Zend_Mail_Storage_Exception('error while creating new folder, may be created incompletly');
00204 }
00205
00206 mkdir($fulldir . DIRECTORY_SEPARATOR . 'new');
00207 mkdir($fulldir . DIRECTORY_SEPARATOR . 'tmp');
00208
00209 $localName = $parent ? substr($folder, strlen($parent) + 1) : $folder;
00210 $this->getFolders($parent)->$localName = new Zend_Mail_Storage_Folder($localName, $folder, true);
00211
00212 return $fulldir;
00213 }
00214
00222 public function removeFolder($name)
00223 {
00224
00225
00226
00227
00228
00229
00230 if ($name instanceof Zend_Mail_Storage_Folder) {
00231 $name = $name->getGlobalName();
00232 }
00233
00234 $name = trim($name, $this->_delim);
00235 if (strpos($name, 'INBOX' . $this->_delim) === 0) {
00236 $name = substr($name, 6);
00237 }
00238
00239
00240 if (!$this->getFolders($name)->isLeaf()) {
00244 require_once 'Zend/Mail/Storage/Exception.php';
00245 throw new Zend_Mail_Storage_Exception('delete children first');
00246 }
00247
00248 if ($name == 'INBOX' || $name == DIRECTORY_SEPARATOR || $name == '/') {
00252 require_once 'Zend/Mail/Storage/Exception.php';
00253 throw new Zend_Mail_Storage_Exception('wont delete INBOX');
00254 }
00255
00256 if ($name == $this->getCurrentFolder()) {
00260 require_once 'Zend/Mail/Storage/Exception.php';
00261 throw new Zend_Mail_Storage_Exception('wont delete selected folder');
00262 }
00263
00264 foreach (array('tmp', 'new', 'cur', '.') as $subdir) {
00265 $dir = $this->_rootdir . '.' . $name . DIRECTORY_SEPARATOR . $subdir;
00266 if (!file_exists($dir)) {
00267 continue;
00268 }
00269 $dh = opendir($dir);
00270 if (!$dh) {
00274 require_once 'Zend/Mail/Storage/Exception.php';
00275 throw new Zend_Mail_Storage_Exception("error opening $subdir");
00276 }
00277 while (($entry = readdir($dh)) !== false) {
00278 if ($entry == '.' || $entry == '..') {
00279 continue;
00280 }
00281 if (!unlink($dir . DIRECTORY_SEPARATOR . $entry)) {
00285 require_once 'Zend/Mail/Storage/Exception.php';
00286 throw new Zend_Mail_Storage_Exception("error cleaning $subdir");
00287 }
00288 }
00289 closedir($dh);
00290 if ($subdir !== '.') {
00291 if (!rmdir($dir)) {
00295 require_once 'Zend/Mail/Storage/Exception.php';
00296 throw new Zend_Mail_Storage_Exception("error removing $subdir");
00297 }
00298 }
00299 }
00300
00301 if (!rmdir($this->_rootdir . '.' . $name)) {
00302
00303 mkdir($this->_rootdir . '.' . $name . DIRECTORY_SEPARATOR . 'cur');
00307 require_once 'Zend/Mail/Storage/Exception.php';
00308 throw new Zend_Mail_Storage_Exception("error removing maindir");
00309 }
00310
00311 $parent = strpos($name, $this->_delim) ? substr($name, 0, strrpos($name, $this->_delim)) : null;
00312 $localName = $parent ? substr($name, strlen($parent) + 1) : $name;
00313 unset($this->getFolders($parent)->$localName);
00314 }
00315
00326 public function renameFolder($oldName, $newName)
00327 {
00328
00329
00330 if ($oldName instanceof Zend_Mail_Storage_Folder) {
00331 $oldName = $oldName->getGlobalName();
00332 }
00333
00334 $oldName = trim($oldName, $this->_delim);
00335 if (strpos($oldName, 'INBOX' . $this->_delim) === 0) {
00336 $oldName = substr($oldName, 6);
00337 }
00338
00339 $newName = trim($newName, $this->_delim);
00340 if (strpos($newName, 'INBOX' . $this->_delim) === 0) {
00341 $newName = substr($newName, 6);
00342 }
00343
00344 if (strpos($newName, $oldName . $this->_delim) === 0) {
00348 require_once 'Zend/Mail/Storage/Exception.php';
00349 throw new Zend_Mail_Storage_Exception('new folder cannot be a child of old folder');
00350 }
00351
00352
00353 $folder = $this->getFolders($oldName);
00354
00355 if ($oldName == 'INBOX' || $oldName == DIRECTORY_SEPARATOR || $oldName == '/') {
00359 require_once 'Zend/Mail/Storage/Exception.php';
00360 throw new Zend_Mail_Storage_Exception('wont rename INBOX');
00361 }
00362
00363 if ($oldName == $this->getCurrentFolder()) {
00367 require_once 'Zend/Mail/Storage/Exception.php';
00368 throw new Zend_Mail_Storage_Exception('wont rename selected folder');
00369 }
00370
00371 $newdir = $this->createFolder($newName);
00372
00373 if (!$folder->isLeaf()) {
00374 foreach ($folder as $k => $v) {
00375 $this->renameFolder($v->getGlobalName(), $newName . $this->_delim . $k);
00376 }
00377 }
00378
00379 $olddir = $this->_rootdir . '.' . $folder;
00380 foreach (array('tmp', 'new', 'cur') as $subdir) {
00381 $subdir = DIRECTORY_SEPARATOR . $subdir;
00382 if (!file_exists($olddir . $subdir)) {
00383 continue;
00384 }
00385
00386 if (!rename($olddir . $subdir, $newdir . $subdir)) {
00390 require_once 'Zend/Mail/Storage/Exception.php';
00391 throw new Zend_Mail_Storage_Exception('error while moving ' . $subdir);
00392 }
00393 }
00394
00395 mkdir($olddir . DIRECTORY_SEPARATOR . 'cur');
00396 $this->removeFolder($oldName);
00397 }
00398
00412 protected function _createUniqueId()
00413 {
00414 $id = '';
00415 $id .= function_exists('microtime') ? microtime(true) : (time() . ' ' . rand(0, 100000));
00416 $id .= '.' . (function_exists('posix_getpid') ? posix_getpid() : rand(50, 65535));
00417 $id .= '.' . php_uname('n');
00418
00419 return $id;
00420 }
00421
00433 protected function _createTmpFile($folder = 'INBOX')
00434 {
00435 if ($folder == 'INBOX') {
00436 $tmpdir = $this->_rootdir . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR;
00437 } else {
00438 $tmpdir = $this->_rootdir . '.' . $folder . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR;
00439 }
00440 if (!file_exists($tmpdir)) {
00441 if (!mkdir($tmpdir)) {
00445 require_once 'Zend/Mail/Storage/Exception.php';
00446 throw new Zend_Mail_Storage_Exception('problems creating tmp dir');
00447 }
00448 }
00449
00450
00451
00452
00453
00454
00455 $max_tries = 5;
00456 for ($i = 0; $i < $max_tries; ++$i) {
00457 $uniq = $this->_createUniqueId();
00458 if (!file_exists($tmpdir . $uniq)) {
00459
00460
00461
00462 $fh = fopen($tmpdir . $uniq, 'w');
00463 if (!$fh) {
00467 require_once 'Zend/Mail/Storage/Exception.php';
00468 throw new Zend_Mail_Storage_Exception('could not open temp file');
00469 }
00470 break;
00471 }
00472 sleep(1);
00473 }
00474
00475 if (!$fh) {
00479 require_once 'Zend/Mail/Storage/Exception.php';
00480 throw new Zend_Mail_Storage_Exception("tried $max_tries unique ids for a temp file, but all were taken"
00481 . ' - giving up');
00482 }
00483
00484 return array('dirname' => $this->_rootdir . '.' . $folder, 'uniq' => $uniq, 'filename' => $tmpdir . $uniq,
00485 'handle' => $fh);
00486 }
00487
00495 protected function _getInfoString(&$flags)
00496 {
00497
00498 $wanted_flags = array_flip($flags);
00499 if (isset($wanted_flags[Zend_Mail_Storage::FLAG_RECENT])) {
00503 require_once 'Zend/Mail/Storage/Exception.php';
00504 throw new Zend_Mail_Storage_Exception('recent flag may not be set');
00505 }
00506
00507 $info = ':2,';
00508 $flags = array();
00509 foreach (Zend_Mail_Storage_Maildir::$_knownFlags as $char => $flag) {
00510 if (!isset($wanted_flags[$flag])) {
00511 continue;
00512 }
00513 $info .= $char;
00514 $flags[$char] = $flag;
00515 unset($wanted_flags[$flag]);
00516 }
00517
00518 if (!empty($wanted_flags)) {
00519 $wanted_flags = implode(', ', array_keys($wanted_flags));
00523 require_once 'Zend/Mail/Storage/Exception.php';
00524 throw new Zend_Mail_Storage_Exception('unknown flag(s): ' . $wanted_flags);
00525 }
00526
00527 return $info;
00528 }
00529
00540
00541
00542 public function appendMessage($message, $folder = null, $flags = null, $recent = false)
00543 {
00544 if ($this->_quota && $this->checkQuota()) {
00548 require_once 'Zend/Mail/Storage/Exception.php';
00549 throw new Zend_Mail_Storage_Exception('storage is over quota!');
00550 }
00551
00552 if ($folder === null) {
00553 $folder = $this->_currentFolder;
00554 }
00555
00556 if (!($folder instanceof Zend_Mail_Storage_Folder)) {
00557 $folder = $this->getFolders($folder);
00558 }
00559
00560 if ($flags === null) {
00561 $flags = array(Zend_Mail_Storage::FLAG_SEEN);
00562 }
00563 $info = $this->_getInfoString($flags);
00564 $temp_file = $this->_createTmpFile($folder->getGlobalName());
00565
00566
00567 if (is_resource($message) && get_resource_type($message) == 'stream') {
00568 stream_copy_to_stream($message, $temp_file['handle']);
00569 } else {
00570 fputs($temp_file['handle'], $message);
00571 }
00572 fclose($temp_file['handle']);
00573
00574
00575 $size = filesize($temp_file['filename']);
00576 if ($size !== false) {
00577 $info = ',S=' . $size . $info;
00578 }
00579 $new_filename = $temp_file['dirname'] . DIRECTORY_SEPARATOR;
00580 $new_filename .= $recent ? 'new' : 'cur';
00581 $new_filename .= DIRECTORY_SEPARATOR . $temp_file['uniq'] . $info;
00582
00583
00584 $exception = null;
00585
00586 if (!link($temp_file['filename'], $new_filename)) {
00590 require_once 'Zend/Mail/Storage/Exception.php';
00591 $exception = new Zend_Mail_Storage_Exception('cannot link message file to final dir');
00592 }
00593 @unlink($temp_file['filename']);
00594
00595 if ($exception) {
00596 throw $exception;
00597 }
00598
00599 $this->_files[] = array('uniq' => $temp_file['uniq'],
00600 'flags' => $flags,
00601 'filename' => $new_filename);
00602 if ($this->_quota) {
00603 $this->_addQuotaEntry((int)$size, 1);
00604 }
00605 }
00606
00615 public function copyMessage($id, $folder)
00616 {
00617 if ($this->_quota && $this->checkQuota()) {
00621 require_once 'Zend/Mail/Storage/Exception.php';
00622 throw new Zend_Mail_Storage_Exception('storage is over quota!');
00623 }
00624
00625 if (!($folder instanceof Zend_Mail_Storage_Folder)) {
00626 $folder = $this->getFolders($folder);
00627 }
00628
00629 $filedata = $this->_getFileData($id);
00630 $old_file = $filedata['filename'];
00631 $flags = $filedata['flags'];
00632
00633
00634 while (($key = array_search(Zend_Mail_Storage::FLAG_RECENT, $flags)) !== false) {
00635 unset($flags[$key]);
00636 }
00637 $info = $this->_getInfoString($flags);
00638
00639
00640 $temp_file = $this->_createTmpFile($folder->getGlobalName());
00641
00642 fclose($temp_file['handle']);
00643
00644
00645 $size = filesize($old_file);
00646 if ($size !== false) {
00647 $info = ',S=' . $size . $info;
00648 }
00649
00650 $new_file = $temp_file['dirname'] . DIRECTORY_SEPARATOR . 'cur' . DIRECTORY_SEPARATOR . $temp_file['uniq'] . $info;
00651
00652
00653 $exception = null;
00654
00655 if (!copy($old_file, $temp_file['filename'])) {
00659 require_once 'Zend/Mail/Storage/Exception.php';
00660 $exception = new Zend_Mail_Storage_Exception('cannot copy message file');
00661 } else if (!link($temp_file['filename'], $new_file)) {
00665 require_once 'Zend/Mail/Storage/Exception.php';
00666 $exception = new Zend_Mail_Storage_Exception('cannot link message file to final dir');
00667 }
00668 @unlink($temp_file['filename']);
00669
00670 if ($exception) {
00671 throw $exception;
00672 }
00673
00674 if ($folder->getGlobalName() == $this->_currentFolder
00675 || ($this->_currentFolder == 'INBOX' && $folder->getGlobalName() == '/')) {
00676 $this->_files[] = array('uniq' => $temp_file['uniq'],
00677 'flags' => $flags,
00678 'filename' => $new_file);
00679 }
00680
00681 if ($this->_quota) {
00682 $this->_addQuotaEntry((int)$size, 1);
00683 }
00684 }
00685
00694 public function moveMessage($id, $folder) {
00695 if (!($folder instanceof Zend_Mail_Storage_Folder)) {
00696 $folder = $this->getFolders($folder);
00697 }
00698
00699 if ($folder->getGlobalName() == $this->_currentFolder
00700 || ($this->_currentFolder == 'INBOX' && $folder->getGlobalName() == '/')) {
00704 require_once 'Zend/Mail/Storage/Exception.php';
00705 throw new Zend_Mail_Storage_Exception('target is current folder');
00706 }
00707
00708 $filedata = $this->_getFileData($id);
00709 $old_file = $filedata['filename'];
00710 $flags = $filedata['flags'];
00711
00712
00713 while (($key = array_search(Zend_Mail_Storage::FLAG_RECENT, $flags)) !== false) {
00714 unset($flags[$key]);
00715 }
00716 $info = $this->_getInfoString($flags);
00717
00718
00719 $temp_file = $this->_createTmpFile($folder->getGlobalName());
00720 fclose($temp_file['handle']);
00721
00722
00723 $size = filesize($old_file);
00724 if ($size !== false) {
00725 $info = ',S=' . $size . $info;
00726 }
00727
00728 $new_file = $temp_file['dirname'] . DIRECTORY_SEPARATOR . 'cur' . DIRECTORY_SEPARATOR . $temp_file['uniq'] . $info;
00729
00730
00731 $exception = null;
00732
00733 if (!rename($old_file, $new_file)) {
00737 require_once 'Zend/Mail/Storage/Exception.php';
00738 $exception = new Zend_Mail_Storage_Exception('cannot move message file');
00739 }
00740 @unlink($temp_file['filename']);
00741
00742 if ($exception) {
00743 throw $exception;
00744 }
00745
00746 unset($this->_files[$id - 1]);
00747
00748 $this->_files = array_values($this->_files);
00749 }
00750
00751
00761 public function setFlags($id, $flags)
00762 {
00763 $info = $this->_getInfoString($flags);
00764 $filedata = $this->_getFileData($id);
00765
00766
00767 $new_filename = dirname(dirname($filedata['filename'])) . DIRECTORY_SEPARATOR . 'cur' . DIRECTORY_SEPARATOR . "$filedata[uniq]$info";
00768
00769 if (!@rename($filedata['filename'], $new_filename)) {
00773 require_once 'Zend/Mail/Storage/Exception.php';
00774 throw new Zend_Mail_Storage_Exception('cannot rename file');
00775 }
00776
00777 $filedata['flags'] = $flags;
00778 $filedata['filename'] = $new_filename;
00779
00780 $this->_files[$id - 1] = $filedata;
00781 }
00782
00783
00790 public function removeMessage($id)
00791 {
00792 $filename = $this->_getFileData($id, 'filename');
00793
00794 if ($this->_quota) {
00795 $size = filesize($filename);
00796 }
00797
00798 if (!@unlink($filename)) {
00802 require_once 'Zend/Mail/Storage/Exception.php';
00803 throw new Zend_Mail_Storage_Exception('cannot remove message');
00804 }
00805 unset($this->_files[$id - 1]);
00806
00807 $this->_files = array_values($this->_files);
00808 if ($this->_quota) {
00809 $this->_addQuotaEntry(0 - (int)$size, -1);
00810 }
00811 }
00812
00824 public function setQuota($value) {
00825 $this->_quota = $value;
00826 }
00827
00835 public function getQuota($fromStorage = false) {
00836 if ($fromStorage) {
00837 $fh = @fopen($this->_rootdir . 'maildirsize', 'r');
00838 if (!$fh) {
00842 require_once 'Zend/Mail/Storage/Exception.php';
00843 throw new Zend_Mail_Storage_Exception('cannot open maildirsize');
00844 }
00845 $definition = fgets($fh);
00846 fclose($fh);
00847 $definition = explode(',', trim($definition));
00848 $quota = array();
00849 foreach ($definition as $member) {
00850 $key = $member[strlen($member) - 1];
00851 if ($key == 'S' || $key == 'C') {
00852 $key = $key == 'C' ? 'count' : 'size';
00853 }
00854 $quota[$key] = substr($member, 0, -1);
00855 }
00856 return $quota;
00857 }
00858
00859 return $this->_quota;
00860 }
00861
00865 protected function _calculateMaildirsize() {
00866 $timestamps = array();
00867 $messages = 0;
00868 $total_size = 0;
00869
00870 if (is_array($this->_quota)) {
00871 $quota = $this->_quota;
00872 } else {
00873 try {
00874 $quota = $this->getQuota(true);
00875 } catch (Zend_Mail_Storage_Exception $e) {
00876 throw new Zend_Mail_Storage_Exception('no quota defintion found');
00877 }
00878 }
00879
00880 $folders = new RecursiveIteratorIterator($this->getFolders(), RecursiveIteratorIterator::SELF_FIRST);
00881 foreach ($folders as $folder) {
00882 $subdir = $folder->getGlobalName();
00883 if ($subdir == 'INBOX') {
00884 $subdir = '';
00885 } else {
00886 $subdir = '.' . $subdir;
00887 }
00888 if ($subdir == 'Trash') {
00889 continue;
00890 }
00891
00892 foreach (array('cur', 'new') as $subsubdir) {
00893 $dirname = $this->_rootdir . $subdir . DIRECTORY_SEPARATOR . $subsubdir . DIRECTORY_SEPARATOR;
00894 if (!file_exists($dirname)) {
00895 continue;
00896 }
00897
00898
00899 $timestamps[$dirname] = filemtime($dirname);
00900
00901 $dh = opendir($dirname);
00902
00903
00904 if (!$dh) {
00905 continue;
00906 }
00907
00908
00909 while (($entry = readdir()) !== false) {
00910 if ($entry[0] == '.' || !is_file($dirname . $entry)) {
00911 continue;
00912 }
00913
00914 if (strpos($entry, ',S=')) {
00915 strtok($entry, '=');
00916 $filesize = strtok(':');
00917 if (is_numeric($filesize)) {
00918 $total_size += $filesize;
00919 ++$messages;
00920 continue;
00921 }
00922 }
00923 $size = filesize($dirname . $entry);
00924 if ($size === false) {
00925
00926 continue;
00927 }
00928 $total_size += $size;
00929 ++$messages;
00930 }
00931 }
00932 }
00933
00934 $tmp = $this->_createTmpFile();
00935 $fh = $tmp['handle'];
00936 $definition = array();
00937 foreach ($quota as $type => $value) {
00938 if ($type == 'size' || $type == 'count') {
00939 $type = $type == 'count' ? 'C' : 'S';
00940 }
00941 $definition[] = $value . $type;
00942 }
00943 $definition = implode(',', $definition);
00944 fputs($fh, "$definition\n");
00945 fputs($fh, "$total_size $messages\n");
00946 fclose($fh);
00947 rename($tmp['filename'], $this->_rootdir . 'maildirsize');
00948 foreach ($timestamps as $dir => $timestamp) {
00949 if ($timestamp < filemtime($dir)) {
00950 unlink($this->_rootdir . 'maildirsize');
00951 break;
00952 }
00953 }
00954
00955 return array('size' => $total_size, 'count' => $messages, 'quota' => $quota);
00956 }
00957
00961 protected function _calculateQuota($forceRecalc = false) {
00962 $fh = null;
00963 $total_size = 0;
00964 $messages = 0;
00965 $maildirsize = '';
00966 if (!$forceRecalc && file_exists($this->_rootdir . 'maildirsize') && filesize($this->_rootdir . 'maildirsize') < 5120) {
00967 $fh = fopen($this->_rootdir . 'maildirsize', 'r');
00968 }
00969 if ($fh) {
00970 $maildirsize = fread($fh, 5120);
00971 if (strlen($maildirsize) >= 5120) {
00972 fclose($fh);
00973 $fh = null;
00974 $maildirsize = '';
00975 }
00976 }
00977 if (!$fh) {
00978 $result = $this->_calculateMaildirsize();
00979 $total_size = $result['size'];
00980 $messages = $result['count'];
00981 $quota = $result['quota'];
00982 } else {
00983 $maildirsize = explode("\n", $maildirsize);
00984 if (is_array($this->_quota)) {
00985 $quota = $this->_quota;
00986 } else {
00987 $definition = explode(',', $maildirsize[0]);
00988 $quota = array();
00989 foreach ($definition as $member) {
00990 $key = $member[strlen($member) - 1];
00991 if ($key == 'S' || $key == 'C') {
00992 $key = $key == 'C' ? 'count' : 'size';
00993 }
00994 $quota[$key] = substr($member, 0, -1);
00995 }
00996 }
00997 unset($maildirsize[0]);
00998 foreach ($maildirsize as $line) {
00999 list($size, $count) = explode(' ', trim($line));
01000 $total_size += $size;
01001 $messages += $count;
01002 }
01003 }
01004
01005 $over_quota = false;
01006 $over_quota = $over_quota || (isset($quota['size']) && $total_size > $quota['size']);
01007 $over_quota = $over_quota || (isset($quota['count']) && $messages > $quota['count']);
01008
01009
01010
01011
01012 if ($over_quota && ($maildirsize || filemtime($this->_rootdir . 'maildirsize') > time() - 900)) {
01013 $result = $this->_calculateMaildirsize();
01014 $total_size = $result['size'];
01015 $messages = $result['count'];
01016 $quota = $result['quota'];
01017 $over_quota = false;
01018 $over_quota = $over_quota || (isset($quota['size']) && $total_size > $quota['size']);
01019 $over_quota = $over_quota || (isset($quota['count']) && $messages > $quota['count']);
01020 }
01021
01022 if ($fh) {
01023
01024 fclose($fh);
01025 }
01026
01027 return array('size' => $total_size, 'count' => $messages, 'quota' => $quota, 'over_quota' => $over_quota);
01028 }
01029
01030 protected function _addQuotaEntry($size, $count = 1) {
01031 if (!file_exists($this->_rootdir . 'maildirsize')) {
01032
01033 }
01034 $size = (int)$size;
01035 $count = (int)$count;
01036 file_put_contents($this->_rootdir . 'maildirsize', "$size $count\n", FILE_APPEND);
01037 }
01038
01045 public function checkQuota($detailedResponse = false, $forceRecalc = false) {
01046 $result = $this->_calculateQuota($forceRecalc);
01047 return $detailedResponse ? $result : $result['over_quota'];
01048 }
01049 }