Skip to content

Solution to display only subscribed Folders #539

Open
@Kollos2k

Description

@Kollos2k

Hey there I want only to list subscribed Folders. I do this by manipulation of the Package but maybe you can add the code anywhere.

My solution is:

Goto ProtocolInterface.php and add after folders function:


    /**
     * Get a list of available subscribed folders
     * @param string $reference mailbox reference for list
     * @param string $folder mailbox / folder name match with wildcards
     *
     * @return Response containing mailboxes that matched $folder as array(globalName => array('delim' => .., 'flags' => ..))
     * @throws RuntimeException
     */    
    public function foldersSubscribed(string $reference = '', string $folder = '*'): Response;

after that goto: ImapProtocol.php and add after folders Function:

/**
     * Get a list of available subscribed folders
     *
     * @param string $reference mailbox reference for list
     * @param string $folder mailbox name match with wildcards
     *
     * @return Response folders that matched $folder as array(name => array('delimiter' => .., 'flags' => ..))
     *
     * @throws ImapBadRequestException
     * @throws ImapServerErrorException
     * @throws RuntimeException
     */
    public function foldersSubscribed(string $reference = '', string $folder = '*'): Response {
        $response = $this->requestAndResponse('LSUB', $this->escapeString($reference, $folder))->setCanBeEmpty(true);
        $list = $response->data();

        $result = [];
        if ($list[0] !== true) {
            foreach ($list as $item) {
                if (count($item) != 4 || $item[0] != 'LSUB') {
                    continue;
                }
                $item[3] = str_replace("\\\\", "\\", str_replace("\\\"", "\"", $item[3]));
                $result[$item[3]] = ['delimiter' => $item[2], 'flags' => $item[1]];
            }
        }

        return $response->setResult($result);
    }

finaly goto Client.php and add after getFolders function:

/**
     * Get folders list.
     * If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array.
     *
     * @param boolean $hierarchical
     * @param string|null $parent_folder
     * @param bool $soft_fail If true, it will return an empty collection instead of throwing an exception
     *
     * @return FolderCollection
     * @throws AuthFailedException
     * @throws ConnectionFailedException
     * @throws FolderFetchingException
     * @throws ImapBadRequestException
     * @throws ImapServerErrorException
     * @throws ResponseException
     * @throws RuntimeException
     */
    public function getFoldersSubscribed(bool $hierarchical = true, string $parent_folder = null, bool $soft_fail = false): FolderCollection {
        $this->checkConnection();
        $folders = FolderCollection::make([]);

        $pattern = $parent_folder.($hierarchical ? '%' : '*');
        $sub = $this->connection->foldersSubscribed('', $pattern)->validatedData();
        $items = $this->connection->folders('', $pattern)->validatedData();

        if(!empty($items)){
            foreach ($items as $folder_name => $item) {
                if(!isset($sub[$folder_name]))continue;
                $folder = new Folder($this, $folder_name, $item["delimiter"], $item["flags"]);

                if ($hierarchical && $folder->hasChildren()) {
                    $pattern = $folder->full_name.$folder->delimiter.'%';

                    $children = $this->getFoldersSubscribed(true, $pattern, $soft_fail);
                    $folder->setChildren($children);
                }

                $folders->push($folder);
            }

            return $folders;
        }else if (!$soft_fail){
            throw new FolderFetchingException("failed to fetch any folders");
        }

        return $folders;
    }

From now on you can get the subscribed Folders by:

$cm = new ClientManager('YOUR OPTIONS ARRAY');
$client = $cm->make('YOUR CONNECTION ARRAY');
$client->getFoldersSubscribed();

Have a nice Day
Florian

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions