/** * create a new folder * * This method also creates parent folders if necessary. Some mail storages may restrict, which folder * may be used as parent or which chars may be used in the folder name * * @param string $name global name of folder, local name if $parentFolder is set * @param string|\Zend\Mail\Storage\Folder $parentFolder parent folder for new folder, else root folder is parent * @return null * @throws \Zend\Mail\Storage\Exception */ public function createFolder($name, $parentFolder = null) { // TODO: we assume / as the hierarchy delim - need to get that from the folder class! if ($parentFolder instanceof Folder) { $folder = $parentFolder->getGlobalName() . '/' . $name; } else { if ($parentFolder != null) { $folder = $parentFolder . '/' . $name; } else { $folder = $name; } } if (!$this->_protocol->create($folder)) { throw new Exception\RuntimeException('cannot create folder'); } }
/** * create a new folder * * This method also creates parent folders if necessary. Some mail storages may restrict, which folder * may be used as parent or which chars may be used in the folder name * * @param string $name global name of folder, local name if $parentFolder is set * @param string|\Zend\Mail\Storage\Folder $parentFolder parent folder for new folder, else root folder is parent * @throws \Zend\Mail\Storage\Exception\RuntimeException * @return string only used internally (new created maildir) */ public function createFolder($name, $parentFolder = null) { if ($parentFolder instanceof Folder) { $folder = $parentFolder->getGlobalName() . $this->delim . $name; } elseif ($parentFolder != null) { $folder = rtrim($parentFolder, $this->delim) . $this->delim . $name; } else { $folder = $name; } $folder = trim($folder, $this->delim); // first we check if we try to create a folder that does exist $exists = null; try { $exists = $this->getFolders($folder); } catch (MailException\ExceptionInterface $e) { // ok } if ($exists) { throw new StorageException\RuntimeException('folder already exists'); } if (strpos($folder, $this->delim . $this->delim) !== false) { throw new StorageException\RuntimeException('invalid name - folder parts may not be empty'); } if (strpos($folder, 'INBOX' . $this->delim) === 0) { $folder = substr($folder, 6); } $fulldir = $this->rootdir . '.' . $folder; // check if we got tricked and would create a dir outside of the rootdir or not as direct child if (strpos($folder, DIRECTORY_SEPARATOR) !== false || strpos($folder, '/') !== false || dirname($fulldir) . DIRECTORY_SEPARATOR != $this->rootdir) { throw new StorageException\RuntimeException('invalid name - no directory separator allowed in folder name'); } // has a parent folder? $parent = null; if (strpos($folder, $this->delim)) { // let's see if the parent folder exists $parent = substr($folder, 0, strrpos($folder, $this->delim)); try { $this->getFolders($parent); } catch (MailException\ExceptionInterface $e) { // does not - create parent folder $this->createFolder($parent); } } 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'); $localName = $parent ? substr($folder, strlen($parent) + 1) : $folder; $this->getFolders($parent)->{$localName} = new Folder($localName, $folder, true); return $fulldir; }