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()); }
/** * 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; } } }
/** */ 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']); } }
/** * @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)); }