Example #1
0
 /**
  * Sorts message numbers array $messages by the specified $sortCriteria.
  *
  * This method supports unique IDs instead of message numbers. See
  * {@link ezcMailImapTransportOptions} for how to enable unique IDs
  * referencing.
  *
  * $messages is an array of message numbers, for example:
  * <code>
  *   array( 1, 2, 4 );
  * </code>
  *
  * $sortCriteria is an email header like: Subject, To, From, Date, Sender.
  *
  * The sorting is done with the php function natcasesort().
  *
  * Before calling this method, a connection to the IMAP server must be
  * established and a user must be authenticated successfully, and a mailbox
  * must be selected.
  *
  * @throws ezcMailTransportException
  *         if a mailbox is not selected
  *         or if the server sent a negative response
  *         or if the array $messages is empty
  * @param array(int) $messages
  * @param string $sortCriteria
  * @param bool $reverse
  * @return array(string)
  */
 protected function sort($messages, $sortCriteria, $reverse = false)
 {
     $uid = $this->options->uidReferencing ? self::UID : self::NO_UID;
     if ($this->state != self::STATE_SELECTED && $this->state != self::STATE_SELECTED_READONLY) {
         throw new ezcMailTransportException("Can't call sort() on the IMAP transport when a mailbox is not selected.");
     }
     $result = array();
     $query = ucfirst(strtolower($sortCriteria));
     $messageNumbers = implode(',', $messages);
     $tag = $this->getNextTag();
     $this->connection->sendData("{$tag} {$uid}FETCH {$messageNumbers} (BODY.PEEK[HEADER.FIELDS ({$query})])");
     $response = trim($this->connection->getLine());
     while (strpos($response, $tag) === false) {
         if (strpos($response, ' FETCH (') !== false) {
             if ($this->options->uidReferencing) {
                 preg_match('/^\\* [0-9]+ FETCH \\(UID ([0-9]+)/', $response, $matches);
             } else {
                 preg_match('/^\\* ([0-9]+) FETCH/', $response, $matches);
             }
             $messageNumber = $matches[1];
         }
         if (strpos($response, $query) !== false) {
             $strippedResponse = trim(trim(str_replace("{$query}: ", '', $response)), '"');
             switch ($query) {
                 case 'Date':
                     $strippedResponse = strtotime($strippedResponse);
                     break;
                 case 'Subject':
                 case 'From':
                 case 'Sender':
                 case 'To':
                     $strippedResponse = ezcMailTools::mimeDecode($strippedResponse);
                     break;
                 default:
                     break;
             }
             $result[$messageNumber] = $strippedResponse;
         }
         // in case the mail doesn't have the $sortCriteria header (like junk mail missing Subject header)
         if (strpos($response, ')') !== false && !isset($result[$messageNumber])) {
             $result[$messageNumber] = '';
         }
         $response = trim($this->connection->getLine());
     }
     if ($this->responseType($response) != self::RESPONSE_OK) {
         throw new ezcMailTransportException("The IMAP server could not sort the messages: {$response}.");
     }
     if ($reverse === true) {
         natcasesort($result);
         $result = array_reverse($result, true);
     } else {
         natcasesort($result);
     }
     return $result;
 }
Example #2
0
 /**
  * Sends a NOOP command to the server, use it to keep the connection alive.
  *
  * Before calling this method, a connection to the POP3 server must be
  * established and a user must be authenticated successfully.
  *
  * @throws ezcMailTransportException
  *         if there was no connection to the server
  *         or if not authenticated
  *         or if the server sent a negative response
  */
 public function noop()
 {
     if ($this->state != self::STATE_TRANSACTION) {
         throw new ezcMailTransportException("Can't call noop() on the POP3 transport when not successfully logged in.");
     }
     // send the command
     $this->connection->sendData("NOOP");
     $response = $this->connection->getLine();
     if (!$this->isPositiveResponse($response)) {
         throw new ezcMailTransportException("The POP3 server sent a negative response to the NOOP command: {$response}.");
     }
 }