Author: Michael Slusarz (slusarz@horde.org)
Inheritance: implements ArrayAccess, implements Countable, implements IteratorAggregate
Beispiel #1
0
 public function testFirst()
 {
     $this->assertNull($this->ob->first());
     $ob = new Horde_Imap_Client_Fetch_Results($this->ob_class);
     $fetch = $ob->get('1');
     $this->assertEquals($fetch, $ob->first());
 }
Beispiel #2
0
 /**
  * Fetch data for a given fetch query.
  *
  * @param Horde_Imap_Client_Fetch_Results $results  Fetch results.
  * @param array $options                            Fetch query options.
  */
 protected function _fetchCmd(Horde_Imap_Client_Fetch_Results $results, $options)
 {
     // Grab sequence IDs - IDs will always be the message number for
     // POP3 fetch commands.
     $seq_ids = $this->_getSeqIds($options['ids']);
     if (empty($seq_ids)) {
         return;
     }
     $lookup = $options['ids']->sequence ? array_combine($seq_ids, $seq_ids) : $this->_pop3Cache('uidl');
     foreach ($options['_query'] as $type => $c_val) {
         switch ($type) {
             case Horde_Imap_Client::FETCH_FULLMSG:
                 foreach ($seq_ids as $id) {
                     $tmp = $this->_pop3Cache('msg', $id);
                     if (empty($c_val['start']) && empty($c_val['length'])) {
                         $tmp2 = fopen('php://temp', 'r+');
                         stream_copy_to_stream($tmp, $tmp2, empty($c_val['length']) ? -1 : $c_val['length'], empty($c_val['start']) ? 0 : $c_val['start']);
                         $results->get($lookup[$id])->setFullMsg($tmp2);
                     } else {
                         $results->get($lookup[$id])->setFullMsg($tmp);
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_HEADERTEXT:
                 // Ignore 'peek' option
                 foreach ($c_val as $key => $val) {
                     foreach ($seq_ids as $id) {
                         /* Message header can be retrieved via TOP, if the
                          * command is available. */
                         try {
                             $tmp = $key == 0 ? $this->_pop3Cache('hdr', $id) : Horde_Mime_Part::getRawPartText(stream_get_contents($this->_pop3Cache('msg', $id)), 'header', $key);
                             $results->get($lookup[$id])->setHeaderText($key, $this->_processString($tmp, $c_val));
                         } catch (Horde_Mime_Exception $e) {
                         }
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_BODYTEXT:
                 // Ignore 'peek' option
                 foreach ($c_val as $key => $val) {
                     foreach ($seq_ids as $id) {
                         try {
                             $results->get($lookup[$id])->setBodyText($key, $this->_processString(Horde_Mime_Part::getRawPartText(stream_get_contents($this->_pop3Cache('msg', $id)), 'body', $key), $val));
                         } catch (Horde_Mime_Exception $e) {
                         }
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_MIMEHEADER:
                 // Ignore 'peek' option
                 foreach ($c_val as $key => $val) {
                     foreach ($seq_ids as $id) {
                         try {
                             $results->get($lookup[$id])->setMimeHeader($key, $this->_processString(Horde_Mime_Part::getRawPartText(stream_get_contents($this->_pop3Cache('msg', $id)), 'header', $key), $val));
                         } catch (Horde_Mime_Exception $e) {
                         }
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_BODYPART:
                 // Ignore 'decode', 'peek'
                 foreach ($c_val as $key => $val) {
                     foreach ($seq_ids as $id) {
                         try {
                             $results->get($lookup[$id])->setBodyPart($key, $this->_processString(Horde_Mime_Part::getRawPartText(stream_get_contents($this->_pop3Cache('msg', $id)), 'body', $key), $val));
                         } catch (Horde_Mime_Exception $e) {
                         }
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_HEADERS:
                 // Ignore 'length', 'peek'
                 foreach ($seq_ids as $id) {
                     $ob = $this->_pop3Cache('hdrob', $id);
                     foreach ($c_val as $key => $val) {
                         $tmp = $ob;
                         if (empty($val['notsearch'])) {
                             $tmp2 = $tmp->toArray(array('nowrap' => true));
                             foreach (array_keys($tmp2) as $hdr) {
                                 if (!in_array($hdr, $val['headers'])) {
                                     unset($tmp[$hdr]);
                                 }
                             }
                         } else {
                             foreach ($val['headers'] as $hdr) {
                                 unset($tmp[$hdr]);
                             }
                         }
                         $results->get($lookup[$id])->setHeaders($key, $tmp);
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_STRUCTURE:
                 foreach ($seq_ids as $id) {
                     if ($ptr = $this->_pop3Cache('msg', $id)) {
                         try {
                             $results->get($lookup[$id])->setStructure(Horde_Mime_Part::parseMessage(stream_get_contents($ptr), array('no_body' => true)));
                         } catch (Horde_Exception $e) {
                         }
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_ENVELOPE:
                 foreach ($seq_ids as $id) {
                     $tmp = $this->_pop3Cache('hdrob', $id);
                     $results->get($lookup[$id])->setEnvelope(array('date' => $tmp['Date'], 'subject' => $tmp['Subject'], 'from' => ($h = $tmp['From']) ? $h->getAddressList(true) : null, 'sender' => ($h = $tmp['Sender']) ? $h->getAddressList(true) : null, 'reply_to' => ($h = $tmp['Reply-to']) ? $h->getAddressList(true) : null, 'to' => ($h = $tmp['To']) ? $h->getAddressList(true) : null, 'cc' => ($h = $tmp['Cc']) ? $h->getAddressList(true) : null, 'bcc' => ($h = $tmp['Bcc']) ? $h->getAddressList(true) : null, 'in_reply_to' => $tmp['In-Reply-To'], 'message_id' => $tmp['Message-ID']));
                 }
                 break;
             case Horde_Imap_Client::FETCH_IMAPDATE:
                 foreach ($seq_ids as $id) {
                     $tmp = $this->_pop3Cache('hdrob', $id);
                     $results->get($lookup[$id])->setImapDate($tmp['Date']);
                 }
                 break;
             case Horde_Imap_Client::FETCH_SIZE:
                 $sizelist = $this->_pop3Cache('size');
                 foreach ($seq_ids as $id) {
                     $results->get($lookup[$id])->setSize($sizelist[$id]);
                 }
                 break;
             case Horde_Imap_Client::FETCH_SEQ:
                 foreach ($seq_ids as $id) {
                     $results->get($lookup[$id])->setSeq($id);
                 }
                 break;
             case Horde_Imap_Client::FETCH_UID:
                 $uidllist = $this->_pop3Cache('uidl');
                 foreach ($seq_ids as $id) {
                     if (isset($uidllist[$id])) {
                         $results->get($lookup[$id])->setUid($uidllist[$id]);
                     }
                 }
                 break;
         }
     }
 }
Beispiel #3
0
 /**
  */
 protected function _fetch(Horde_Imap_Client_Fetch_Results $results, $queries)
 {
     $pipeline = $this->_pipeline();
     $pipeline->data['fetch_lookup'] = array();
     $pipeline->data['fetch_followup'] = array();
     foreach ($queries as $options) {
         $this->_fetchCmd($pipeline, $options);
         $sequence = $options['ids']->sequence;
     }
     try {
         $resp = $this->_sendCmd($pipeline);
         /* Check for EXPUNGEISSUED (RFC 2180 [4.1]/RFC 5530 [3]). */
         if (!empty($resp->data['expungeissued'])) {
             $this->noop();
         }
         foreach ($resp->fetch as $k => $v) {
             $results->get($sequence ? $k : $v->getUid())->merge($v);
         }
     } catch (Horde_Imap_Client_Exception_ServerResponse $e) {
         if ($e->status === Horde_Imap_Client_Interaction_Server::NO) {
             if ($e->getCode() === $e::UNKNOWNCTE) {
                 /* UNKNOWN-CTE error. Redo the query without the BINARY
                  * elements. */
                 $bq = $pipeline->data['binaryquery'];
                 foreach ($queries as $val) {
                     foreach ($bq as $key2 => $val2) {
                         unset($val2['decode']);
                         $val['_query']->bodyPart($key2, $val2);
                         $val['_query']->remove(Horde_Imap_Client::FETCH_BODYPARTSIZE, $key2);
                     }
                     $pipeline->data['fetch_followup'][] = $val;
                 }
             } elseif ($sequence) {
                 /* A NO response, when coupled with a sequence FETCH, most
                  * likely means that messages were expunged. (RFC 2180
                  * [4.1]) */
                 $this->noop();
             }
         }
     } catch (Exception $e) {
         // For any other error, ignore the Exception - fetch() is nice in
         // that the return value explicitly handles missing data for any
         // given message.
     }
     if (!empty($pipeline->data['fetch_followup'])) {
         $this->_fetch($results, $pipeline->data['fetch_followup']);
     }
 }
Beispiel #4
0
 /**
  * Populates the changes, flags, and categories arrays with data from
  * any messages added/changed on the IMAP server since the last poll.
  *
  * @param array &$changes                             Changes array.
  * @param array &$flags                               Flags array.
  * @param array &$categories                          Categories array.
  * @param Horde_Imap_Client_Fetch_Results $fetch_ret  Fetch results.
  * @param array $options                              Options array.
  * @param integer $modseq                             Current MODSEQ.
  */
 protected function _buildModSeqChanges(&$changes, &$flags, &$categories, $fetch_ret, $options, $modseq)
 {
     // Get custom flags to use as categories.
     $msgFlags = $this->_getMsgFlags();
     // Filter out any changes that we already know about.
     $fetch_keys = $fetch_ret->ids();
     $result_set = array_diff($fetch_keys, $changes);
     foreach ($result_set as $uid) {
         $data = $fetch_ret[$uid];
         // Ensure no changes after the current modseq as reported by the
         // server status have been returned.
         if ($data->getModSeq() <= $modseq) {
             $changes[] = $uid;
             $flags[$uid] = array('read' => array_search(Horde_Imap_Client::FLAG_SEEN, $data->getFlags()) !== false ? 1 : 0);
             if ($options['protocolversion'] > Horde_ActiveSync::VERSION_TWOFIVE) {
                 $flags[$uid]['flagged'] = array_search(Horde_Imap_Client::FLAG_FLAGGED, $data->getFlags()) !== false ? 1 : 0;
             }
             if ($options['protocolversion'] > Horde_ActiveSync::VERSION_TWELVEONE) {
                 $categories[$uid] = array();
                 foreach ($data->getFlags() as $flag) {
                     if (!empty($msgFlags[Horde_String::lower($flag)])) {
                         $categories[$uid][] = $msgFlags[Horde_String::lower($flag)];
                     }
                 }
             }
         }
     }
 }
Beispiel #5
0
 /**
  * @throws Horde_Imap_Client_Exception_NoSupportPop3
  */
 protected function _fetch(Horde_Imap_Client_Fetch_Results $results, Horde_Imap_Client_Fetch_Query $query, $options)
 {
     // These options are not supported by this driver.
     if (!empty($options['changedsince']) || !empty($options['vanished'])) {
         throw new Horde_Imap_Client_Exception_NoSupportPop3('Fetch options');
     }
     // Grab sequence IDs - IDs will always be the message number for
     // POP3 fetch commands.
     $seq_ids = $this->_getSeqIds($options['ids']);
     if (empty($seq_ids)) {
         return;
     }
     $lookup = $options['ids']->sequence ? array_combine($seq_ids, $seq_ids) : $this->_pop3Cache('uidl');
     foreach ($query as $type => $c_val) {
         switch ($type) {
             case Horde_Imap_Client::FETCH_FULLMSG:
                 foreach ($seq_ids as $id) {
                     $tmp = $this->_pop3Cache('msg', $id);
                     if (empty($c_val['start']) && empty($c_val['length'])) {
                         $tmp2 = fopen('php://temp', 'r+');
                         stream_copy_to_stream($tmp, $tmp2, empty($c_val['length']) ? -1 : $c_val['length'], empty($c_val['start']) ? 0 : $c_val['start']);
                         $results->get($lookup[$id])->setFullMsg($tmp2);
                     } else {
                         $results->get($lookup[$id])->setFullMsg($tmp);
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_HEADERTEXT:
                 // Ignore 'peek' option
                 foreach ($c_val as $key => $val) {
                     foreach ($seq_ids as $id) {
                         /* Message header can be retrieved via TOP, if the
                          * command is available. */
                         try {
                             $tmp = $key == 0 ? $this->_pop3Cache('hdr', $id) : Horde_Mime_Part::getRawPartText(stream_get_contents($this->_pop3Cache('msg', $id)), 'header', $key);
                             $results->get($lookup[$id])->setHeaderText($key, $this->_processString($tmp, $c_val));
                         } catch (Horde_Mime_Exception $e) {
                         }
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_BODYTEXT:
                 // Ignore 'peek' option
                 foreach ($c_val as $key => $val) {
                     foreach ($seq_ids as $id) {
                         try {
                             $results->get($lookup[$id])->setBodyText($key, $this->_processString(Horde_Mime_Part::getRawPartText(stream_get_contents($this->_pop3Cache('msg', $id)), 'body', $key), $val));
                         } catch (Horde_Mime_Exception $e) {
                         }
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_MIMEHEADER:
                 // Ignore 'peek' option
                 foreach ($c_val as $key => $val) {
                     foreach ($seq_ids as $id) {
                         try {
                             $results->get($lookup[$id])->setMimeHeader($key, $this->_processString(Horde_Mime_Part::getRawPartText(stream_get_contents($this->_pop3Cache('msg', $id)), 'header', $key), $val));
                         } catch (Horde_Mime_Exception $e) {
                         }
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_BODYPART:
                 // Ignore 'decode', 'peek'
                 foreach ($c_val as $key => $val) {
                     foreach ($seq_ids as $id) {
                         try {
                             $results->get($lookup[$id])->setBodyPart($key, $this->_processString(Horde_Mime_Part::getRawPartText(stream_get_contents($this->_pop3Cache('msg', $id)), 'body', $key), $val));
                         } catch (Horde_Mime_Exception $e) {
                         }
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_HEADERS:
                 // Ignore 'length', 'peek'
                 foreach ($seq_ids as $id) {
                     $ob = $this->_pop3Cache('hdrob', $id);
                     foreach ($c_val as $key => $val) {
                         $tmp = $ob;
                         if (empty($val['notsearch'])) {
                             $tmp2 = $tmp->toArray(array('nowrap' => true));
                             foreach (array_keys($tmp2) as $hdr) {
                                 if (!in_array($hdr, $val['headers'])) {
                                     $tmp->removeHeader($hdr);
                                 }
                             }
                         } else {
                             foreach ($val['headers'] as $hdr) {
                                 $tmp->removeHeader($hdr);
                             }
                         }
                         $results->get($lookup[$id])->setHeaders($key, $tmp);
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_STRUCTURE:
                 foreach ($seq_ids as $id) {
                     if ($ptr = $this->_pop3Cache('msg', $id)) {
                         try {
                             $results->get($lookup[$id])->setStructure(Horde_Mime_Part::parseMessage(stream_get_contents($ptr)));
                         } catch (Horde_Exception $e) {
                         }
                     }
                 }
                 break;
             case Horde_Imap_Client::FETCH_ENVELOPE:
                 foreach ($seq_ids as $id) {
                     $tmp = $this->_pop3Cache('hdrob', $id);
                     $results->get($lookup[$id])->setEnvelope(array('date' => $tmp->getValue('date'), 'subject' => $tmp->getValue('subject'), 'from' => $tmp->getOb('from'), 'sender' => $tmp->getOb('sender'), 'reply_to' => $tmp->getOb('reply-to'), 'to' => $tmp->getOb('to'), 'cc' => $tmp->getOb('cc'), 'bcc' => $tmp->getOb('bcc'), 'in_reply_to' => $tmp->getValue('in-reply-to'), 'message_id' => $tmp->getValue('message-id')));
                 }
                 break;
             case Horde_Imap_Client::FETCH_IMAPDATE:
                 foreach ($seq_ids as $id) {
                     $tmp = $this->_pop3Cache('hdrob', $id);
                     $results->get($lookup[$id])->setImapDate($tmp->getValue('date'));
                 }
                 break;
             case Horde_Imap_Client::FETCH_SIZE:
                 $sizelist = $this->_pop3Cache('size');
                 foreach ($seq_ids as $id) {
                     $results->get($lookup[$id])->setSize($sizelist[$id]);
                 }
                 break;
             case Horde_Imap_Client::FETCH_SEQ:
                 foreach ($seq_ids as $id) {
                     $results->get($lookup[$id])->setSeq($id);
                 }
                 break;
             case Horde_Imap_Client::FETCH_UID:
                 $uidllist = $this->_pop3Cache('uidl');
                 foreach ($seq_ids as $id) {
                     if (isset($uidllist[$id])) {
                         $results->get($lookup[$id])->setUid($uidllist[$id]);
                     }
                 }
                 break;
         }
     }
     $this->_updateCache($results, array('seq' => $options['ids']->sequence));
 }
Beispiel #6
0
 public function testBug11946()
 {
     $test = array('* 1 FETCH (FLAGS (\\Seen \\Flagged))', '* 1 FETCH (UID 9)');
     $res = new Horde_Imap_Client_Fetch_Results();
     $this->test_ob->parseFetch($test[0], array('results' => $res));
     $this->test_ob->parseFetch($test[1], array('results' => $res));
     $this->assertEquals(array('\\seen', '\\flagged'), $res->first()->getFlags());
     $this->assertEquals(9, $res->first()->getUid());
     $test[1] = '* 1 FETCH (UID 9 FLAGS (\\Seen \\Flagged))';
     $res = new Horde_Imap_Client_Fetch_Results();
     $this->test_ob->parseFetch($test[0], array('results' => $res));
     $this->test_ob->parseFetch($test[1], array('results' => $res));
     $this->assertEquals(array('\\seen', '\\flagged'), $res->first()->getFlags());
 }
Beispiel #7
0
 /**
  * Store FETCH data in cache.
  *
  * @param Horde_Imap_Client_Fetch_Results $data  The fetch results.
  * @param array $options                         Additional options:
  *   - fields: (array) Only update these cache fields.
  *             DEFAULT: Update all cache fields.
  *   - mailbox: (Horde_Imap_Client_Mailbox) The mailbox to update.
  *              DEFAULT: The selected mailbox.
  *   - uidvalid: (integer) The UID validity number.
  *               DEFAULT: UIDVALIDITY discovered via a status() call.
  *
  * @throws Horde_Imap_Client_Exception
  */
 protected function _updateCache(Horde_Imap_Client_Fetch_Results $data, array $options = array())
 {
     $mailbox = empty($options['mailbox']) ? $this->_selected : $options['mailbox'];
     if (!$this->_initCache(empty($options['mailbox']))) {
         return;
     }
     if (in_array(strval($mailbox), $this->_params['cache']['fetch_ignore'])) {
         $this->writeDebug(sprintf("IGNORING cached FETCH data (mailbox: %s)\n", $mailbox), Horde_Imap_Client::DEBUG_INFO);
         return;
     }
     $seq_res = $data->key_type == $data::UID ? null : $this->_getSeqUidLookup($this->getIdsOb($data->ids(), true));
     $tocache = array();
     $status_flags = 0;
     if (isset($this->_init['enabled']['CONDSTORE'])) {
         $status_flags |= Horde_Imap_Client::STATUS_HIGHESTMODSEQ;
     }
     if (empty($options['uidvalid'])) {
         $status_flags |= Horde_Imap_Client::STATUS_UIDVALIDITY;
     }
     $modseq = null;
     $status_res = $this->status($mailbox, $status_flags);
     $uidvalid = isset($status_res['uidvalidity']) ? $status_res['uidvalidity'] : $options['uidvalid'];
     if (count($data)) {
         $cf = empty($options['fields']) ? $this->_params['cache']['fields'] : $this->_cacheFields();
     }
     foreach ($data as $k => $v) {
         $tmp = array();
         foreach ($cf as $key => $val) {
             if ($v->exists($key)) {
                 switch ($key) {
                     case Horde_Imap_Client::FETCH_ENVELOPE:
                         $tmp[$val] = $v->getEnvelope();
                         break;
                     case Horde_Imap_Client::FETCH_FLAGS:
                         /* A FLAGS FETCH can only occur if we are in the
                          * mailbox. So HIGHESTMODSEQ has already been updated.
                          * Ignore flag caching if MODSEQs not available. */
                         if ($modseq = $status_res['highestmodseq']) {
                             $tmp[$val] = $v->getFlags();
                         }
                         break;
                     case Horde_Imap_Client::FETCH_HEADERS:
                         foreach ($this->_temp['headers_caching'] as $label => $hash) {
                             if ($hdr = $v->getHeaders($label)) {
                                 $tmp[$val][$hash] = $hdr;
                             }
                         }
                         break;
                     case Horde_Imap_Client::FETCH_IMAPDATE:
                         $tmp[$val] = $v->getImapDate();
                         break;
                     case Horde_Imap_Client::FETCH_SIZE:
                         $tmp[$val] = $v->getSize();
                         break;
                     case Horde_Imap_Client::FETCH_STRUCTURE:
                         $tmp[$val] = clone $v->getStructure();
                         break;
                 }
             }
         }
         if (!empty($tmp)) {
             $tocache[is_null($seq_res) ? $k : $seq_res['lookup'][$k]] = $tmp;
         }
     }
     $this->_cache->set($mailbox, $tocache, $uidvalid);
     if ($modseq) {
         $metadata = $this->_cache->getMetaData($mailbox, $uidvalid, array(self::CACHE_MODSEQ));
         if (!isset($metadata[self::CACHE_MODSEQ]) || $metadata[self::CACHE_MODSEQ] != $modseq) {
             $this->_updateMetaData($mailbox, array(self::CACHE_MODSEQ => $modseq), $uidvalid);
         }
     }
 }