public function listMessages($contentType = null)
{
if ($this->state != self::STATE_SELECTED && $this->state != self::STATE_SELECTED_READONLY) {
throw new ezcMailTransportException("Can't call listMessages() on the IMAP transport when a mailbox is not selected.");
}
$messageList = array();
$messages = array();
// get the numbers of the existing messages
$tag = $this->getNextTag();
$command = "{$tag} SEARCH UNDELETED";
if (!is_null($contentType)) {
$command .= " HEADER \"Content-Type\" \"{$contentType}\"";
}
$this->connection->sendData($command);
$response = trim($this->getResponse('* SEARCH'));
if (strpos($response, '* SEARCH') !== false) {
$ids = trim(substr($response, 9));
if ($ids !== "") {
$messageList = explode(' ', $ids);
}
}
// skip the OK response ("{$tag} OK Search completed.")
$response = trim($this->getResponse($tag, $response));
if ($this->responseType($response) != self::RESPONSE_OK) {
throw new ezcMailTransportException("The IMAP server could not list messages: {$response}.");
}
if (!empty($messageList)) {
// get the sizes of the messages
$tag = $this->getNextTag();
$query = trim(implode(',', $messageList));
$this->connection->sendData("{$tag} FETCH {$query} RFC822.SIZE");
$response = $this->getResponse('FETCH (');
$currentMessage = trim(reset($messageList));
while (strpos($response, 'FETCH (') !== false) {
$line = $response;
$line = explode(' ', $line);
$line = trim($line[count($line) - 1]);
$line = substr($line, 0, strlen($line) - 1);
$messages[$currentMessage] = intval($line);
$currentMessage = next($messageList);
$response = $this->connection->getLine();
}
// skip the OK response ("{$tag} OK Fetch completed.")
$response = trim($this->getResponse($tag, $response));
if ($this->responseType($response) != self::RESPONSE_OK) {
throw new ezcMailTransportException("The IMAP server could not list messages: {$response}.");
}
}
return $messages;
}