示例#1
0
文件: Imap.php 项目: Maksold/platform
 /**
  * get root folder or given folder
  *
  * @param  string $rootFolder get folder structure for given folder, else root
  * @return Folder root or wanted folder
  * @throws \Zend\Mail\Storage\Exception\RuntimeException
  * @throws \Zend\Mail\Storage\Exception\InvalidArgumentException
  * @throws \Zend\Mail\Protocol\Exception\RuntimeException
  */
 public function getFolders($rootFolder = null)
 {
     $folders = $this->protocol->listMailbox((string) $rootFolder);
     if (!$folders) {
         throw new BaseException\InvalidArgumentException('folder not found');
     }
     $decodedFolders = array();
     foreach ($folders as $globalName => $data) {
         $decodedGlobalName = mb_convert_encoding($globalName, 'UTF-8', 'UTF7-IMAP');
         $decodedFolders[$decodedGlobalName] = $data;
     }
     $folders = $decodedFolders;
     ksort($folders, SORT_STRING);
     $root = new Folder('/', '/', false);
     $stack = array(null);
     $folderStack = array(null);
     $parentFolder = $root;
     $parent = '';
     foreach ($folders as $globalName => $data) {
         do {
             if (!$parent || strpos($globalName, $parent) === 0) {
                 // build local name based on global name
                 $lastDelimPosition = strrpos($globalName, $data['delim']);
                 if ($lastDelimPosition === false) {
                     $localName = $globalName;
                 } else {
                     $localName = substr($globalName, $lastDelimPosition + 1);
                 }
                 $selectable = !$data['flags'] || !in_array('\\Noselect', $data['flags']);
                 array_push($stack, $parent);
                 $parent = $globalName . $data['delim'];
                 $folder = new Folder($localName, $globalName, $selectable);
                 $folder->setFlags(!isset($data['flags']) ? array() : $data['flags']);
                 $this->postInitFolder($folder);
                 $parentFolder->{$localName} = $folder;
                 array_push($folderStack, $parentFolder);
                 $parentFolder = $folder;
                 break;
             } elseif ($stack) {
                 $parent = array_pop($stack);
                 $parentFolder = array_pop($folderStack);
             }
         } while ($stack);
         if (!$stack) {
             throw new BaseException\RuntimeException('error while constructing folder tree');
         }
     }
     return $root;
 }
 /**
  * @param string $localName
  * @param string $globalName
  * @param array  $flags
  * @param bool   $selectable
  *
  * @return Folder
  */
 protected function createRemoteFolder($localName, $globalName, array $flags = [], $selectable = true)
 {
     $folder = new Folder($localName, $globalName, $selectable);
     $folder->setFlags($flags);
     return $folder;
 }