Вход Регистрация
Файл: concrete5.7.5.6/concrete/vendor/zendframework/zend-mail/src/Storage/Folder/Maildir.php
Строк: 215
<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace ZendMailStorageFolder;

use 
ZendMailStorage;
use 
ZendMailStorageException;
use 
ZendStdlibErrorHandler;

class 
Maildir extends StorageMaildir implements FolderInterface
{
    
/**
     * root folder for folder structure
     * @var ZendMailStorageFolder
     */
    
protected $rootFolder;

    
/**
     * rootdir of folder structure
     * @var string
     */
    
protected $rootdir;

    
/**
     * name of current folder
     * @var string
     */
    
protected $currentFolder;

    
/**
     * delim char for subfolders
     * @var string
     */
    
protected $delim;

    
/**
     * Create instance with parameters
     * Supported parameters are:
     *   - dirname rootdir of maildir structure
     *   - delim   delim char for folder structure, default is '.'
     *   - folder initial selected folder, default is 'INBOX'
     *
     * @param  $params array mail reader specific parameters
     * @throws ZendMailStorageExceptionInvalidArgumentException
     */
    
public function __construct($params)
    {
        if (
is_array($params)) {
            
$params = (object) $params;
        }

        if (!isset(
$params->dirname) || !is_dir($params->dirname)) {
            throw new 
ExceptionInvalidArgumentException('no valid dirname given in params');
        }

        
$this->rootdir rtrim($params->dirnameDIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;

        
$this->delim = isset($params->delim) ? $params->delim '.';

        
$this->_buildFolderTree();
        
$this->selectFolder(!empty($params->folder) ? $params->folder 'INBOX');
        
$this->has['top'] = true;
        
$this->has['flags'] = true;
    }

    
/**
     * find all subfolders and mbox files for folder structure
     *
     * Result is save in ZendMailStorageFolder instances with the root in $this->rootFolder.
     * $parentFolder and $parentGlobalName are only used internally for recursion.
     *
     * @throws ZendMailStorageExceptionRuntimeException
     */
    
protected function _buildFolderTree()
    {
        
$this->rootFolder = new StorageFolder('/''/'false);
        
$this->rootFolder->INBOX = new StorageFolder('INBOX''INBOX'true);

        
ErrorHandler::start(E_WARNING);
        
$dh    opendir($this->rootdir);
        
$error ErrorHandler::stop();
        if (!
$dh) {
            throw new 
ExceptionRuntimeException("can't read folders in maildir"0$error);
        }
        
$dirs = array();

        while ((
$entry readdir($dh)) !== false) {

            
// maildir++ defines folders must start with .
            
if ($entry[0] != '.' || $entry == '.' || $entry == '..') {
                continue;
            }

            if (
$this->_isMaildir($this->rootdir $entry)) {
                
$dirs[] = $entry;
            }
        }
        
closedir($dh);

        
sort($dirs);
        
$stack = array(null);
        
$folderStack = array(null);
        
$parentFolder $this->rootFolder;
        
$parent '.';

        foreach (
$dirs as $dir) {
            do {
                if (
strpos($dir$parent) === 0) {
                    
$local substr($dirstrlen($parent));
                    if (
strpos($local$this->delim) !== false) {
                        throw new 
ExceptionRuntimeException('error while reading maildir');
                    }
                    
array_push($stack$parent);
                    
$parent $dir $this->delim;
                    
$folder = new StorageFolder($localsubstr($dir1), true);
                    
$parentFolder->$local $folder;
                    
array_push($folderStack$parentFolder);
                    
$parentFolder $folder;
                    break;
                } elseif (
$stack) {
                    
$parent array_pop($stack);
                    
$parentFolder array_pop($folderStack);
                }
            } while (
$stack);
            if (!
$stack) {
                throw new 
ExceptionRuntimeException('error while reading maildir');
            }
        }
    }

    
/**
     * get root folder or given folder
     *
     * @param string $rootFolder get folder structure for given folder, else root
     * @throws ZendMailStorageExceptionInvalidArgumentException
     * @return ZendMailStorageFolder root or wanted folder
     */
    
public function getFolders($rootFolder null)
    {
        if (!
$rootFolder || $rootFolder == 'INBOX') {
            return 
$this->rootFolder;
        }

        
// rootdir is same as INBOX in maildir
        
if (strpos($rootFolder'INBOX' $this->delim) === 0) {
            
$rootFolder substr($rootFolder6);
        }
        
$currentFolder $this->rootFolder;
        
$subname trim($rootFolder$this->delim);

        while (
$currentFolder) {
            
ErrorHandler::start(E_NOTICE);
            list(
$entry$subname) = explode($this->delim$subname2);
            
ErrorHandler::stop();
            
$currentFolder $currentFolder->$entry;
            if (!
$subname) {
                break;
            }
        }

        if (
$currentFolder->getGlobalName() != rtrim($rootFolder$this->delim)) {
            throw new 
ExceptionInvalidArgumentException("folder $rootFolder not found");
        }
        return 
$currentFolder;
    }

    
/**
     * select given folder
     *
     * folder must be selectable!
     *
     * @param ZendMailStorageFolder|string $globalName global name of folder or instance for subfolder
     * @throws ZendMailStorageExceptionRuntimeException
     */
    
public function selectFolder($globalName)
    {
        
$this->currentFolder = (string) $globalName;

        
// getting folder from folder tree for validation
        
$folder $this->getFolders($this->currentFolder);

        try {
            
$this->_openMaildir($this->rootdir '.' $folder->getGlobalName());
        } catch (
ExceptionExceptionInterface $e) {
            
// check what went wrong
            
if (!$folder->isSelectable()) {
                throw new 
ExceptionRuntimeException("{$this->currentFolder} is not selectable"0$e);
            }
            
// seems like file has vanished; rebuilding folder tree - but it's still an exception
            
$this->_buildFolderTree();
            throw new 
ExceptionRuntimeException('seems like the maildir has vanished, I've rebuild the ' .
                                                         '
folder treesearch for an other folder and try again', 0, $e);
        }
    }

    /**
     * get ZendMailStorageFolder instance for current folder
     *
     * @return ZendMailStorageFolder instance of current folder
     */
    public function getCurrentFolder()
    {
        return $this->currentFolder;
    }
}
Онлайн: 1
Реклама