/** * 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\Folder $parentFolder parent folder for new folder, else root folder is parent * @return string only used internally (new created maildir) * @throws \Zend\Mail\Storage\Exception */ public function createFolder($name, $parentFolder = null) { if ($parentFolder instanceof Folder\Folder) { $folder = $parentFolder->getGlobalName() . $this->_delim . $name; } else { if ($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 (Mail\Exception $e) { // ok } if ($exists) { throw new Storage\Exception('folder already exists'); } if (strpos($folder, $this->_delim . $this->_delim) !== false) { throw new Storage\Exception('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 Storage\Exception('invalid name - no directory seprator 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 (Mail\Exception $e) { // does not - create parent folder $this->createFolder($parent); } } if (!@mkdir($fulldir) || !@mkdir($fulldir . DIRECTORY_SEPARATOR . 'cur')) { throw new Storage\Exception('error while creating new folder, may be created incompletly'); } mkdir($fulldir . DIRECTORY_SEPARATOR . 'new'); mkdir($fulldir . DIRECTORY_SEPARATOR . 'tmp'); $localName = $parent ? substr($folder, strlen($parent) + 1) : $folder; $this->getFolders($parent)->{$localName} = new Folder\Folder($localName, $folder, true); return $fulldir; }
/** * 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\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) { $folder = $parentFolder->getGlobalName() . '/' . $name; } else { if ($parentFolder != null) { $folder = $parentFolder . '/' . $name; } else { $folder = $name; } } if (!$this->_protocol->create($folder)) { throw new Exception('cannot create folder'); } }