public function getMessages($from = 0, $count = 2) { $total = $this->getTotalMessages(); if ($from + $count > $total) { $count = $total - $from; } $headers = array(); $fetch_query = new \Horde_Imap_Client_Fetch_Query(); $fetch_query->envelope(); $fetch_query->flags(); $fetch_query->seq(); $fetch_query->size(); $fetch_query->uid(); $fetch_query->imapDate(); $headers = array_merge($headers, array('importance', 'list-post', 'x-priority')); $headers[] = 'content-type'; $fetch_query->headers('imp', $headers, array('cache' => true, 'peek' => true)); $opt = array('ids' => $from + 1 . ':' . ($from + 1 + $count)); $opt = array(); // $list is an array of Horde_Imap_Client_Data_Fetch objects. $headers = $this->conn->fetch($this->folder_id, $fetch_query, $opt); ob_start(); // fix for Horde warnings $messages = array(); foreach ($headers as $message_id => $header) { $message = new Message($this->conn, $this->folder_id, $message_id); $message->setInfo($header); $messages[] = $message->getListArray(); } ob_clean(); return $messages; }
private function getmsg() { $headers = array(); $fetch_query = new \Horde_Imap_Client_Fetch_Query(); $fetch_query->envelope(); // $fetch_query->fullText(); $fetch_query->bodyText(); $fetch_query->flags(); $fetch_query->seq(); $fetch_query->size(); $fetch_query->uid(); $fetch_query->imapDate(); $headers = array_merge($headers, array('importance', 'list-post', 'x-priority')); $headers[] = 'content-type'; $fetch_query->headers('imp', $headers, array('cache' => true, 'peek' => true)); // $list is an array of Horde_Imap_Client_Data_Fetch objects. $ids = new \Horde_Imap_Client_Ids($this->message_id); $headers = $this->conn->fetch($this->folder_id, $fetch_query, array('ids' => $ids)); $this->plainmsg = $headers[$this->message_id]->getBodyText(); // // // HEADER // $this->header = $this->conn->fetchHeader($this->folder_id, $this->message_id); // // // BODY // $bodystructure= $this->conn->getStructure($this->folder_id, $this->message_id); // $a= \rcube_imap_generic::getStructurePartData($bodystructure, 0); // if ($a['type'] == 'multipart') { // for ($i=0; $i < count($bodystructure); $i++) { // if (!is_array($bodystructure[$i])) // break; // $this->getpart($bodystructure[$i],$i+1); // } // } else { // // get part no 1 // $this->getpart($bodystructure,1); // } }
/** * Sort search results client side if the server does not support the SORT * IMAP extension (RFC 5256). * * @param Horde_Imap_Client_Ids $res The search results. * @param array $opts The options to _search(). * * @return array The sort results. * * @throws Horde_Imap_Client_Exception */ public function clientSort($res, $opts) { if (!count($res)) { return $res; } /* Generate the FETCH command needed. */ $query = new Horde_Imap_Client_Fetch_Query(); foreach ($opts['sort'] as $val) { switch ($val) { case Horde_Imap_Client::SORT_ARRIVAL: $query->imapDate(); break; case Horde_Imap_Client::SORT_DATE: $query->imapDate(); $query->envelope(); break; case Horde_Imap_Client::SORT_CC: case Horde_Imap_Client::SORT_DISPLAYFROM: case Horde_Imap_Client::SORT_DISPLAYTO: case Horde_Imap_Client::SORT_FROM: case Horde_Imap_Client::SORT_SUBJECT: case Horde_Imap_Client::SORT_TO: $query->envelope(); break; case Horde_Imap_Client::SORT_SEQUENCE: $query->seq(); break; case Horde_Imap_Client::SORT_SIZE: $query->size(); break; } } if (!count($query)) { return $res; } $mbox = $this->_socket->currentMailbox(); $fetch_res = $this->_socket->fetch($mbox['mailbox'], $query, array('ids' => $res)); return $this->_clientSortProcess($res->ids, $fetch_res, $opts['sort']); }
private function loadMessageBodies() { $headers = array(); $fetch_query = new \Horde_Imap_Client_Fetch_Query(); $fetch_query->envelope(); $fetch_query->structure(); $fetch_query->flags(); $fetch_query->seq(); $fetch_query->size(); $fetch_query->uid(); $fetch_query->imapDate(); $headers = array_merge($headers, array('importance', 'list-post', 'x-priority')); $headers[] = 'content-type'; $fetch_query->headers('imp', $headers, array('cache' => true, 'peek' => true)); // $list is an array of Horde_Imap_Client_Data_Fetch objects. $ids = new \Horde_Imap_Client_Ids($this->message_id); $headers = $this->conn->fetch($this->folder_id, $fetch_query, array('ids' => $ids)); $fetch = $headers[$this->message_id]; // set $this->fetch to get to, from ... $this->fetch = $fetch; // analyse the body part $structure = $fetch->getStructure(); // debugging below $structure_type = $structure->getPrimaryType(); if ($structure_type == 'multipart') { $i = 1; foreach ($structure->getParts() as $p) { $this->getpart($p, $i++); } } else { if ($structure->findBody() != null) { // get the body from the server $partId = $structure->findBody(); $this->queryBodyPart($partId); } } }
/** * Fetch message data (see RFC 3501 [6.4.5]). * * @param mixed $mailbox The mailbox to search. * Either a * Horde_Imap_Client_Mailbox * object or a string (UTF-8). * @param Horde_Imap_Client_Fetch_Query $query Fetch query object. * @param array $options Additional options: * - changedsince: (integer) Only return messages that have a * mod-sequence larger than this value. This option * requires the CONDSTORE IMAP extension (if not present, * this value is ignored). Additionally, the mailbox * must support mod-sequences or an exception will be * thrown. If valid, this option implicity adds the * mod-sequence fetch criteria to the fetch command. * DEFAULT: Mod-sequence values are ignored. * - ids: (Horde_Imap_Client_Ids) A list of messages to fetch data from. * DEFAULT: All messages in $mailbox will be fetched. * * @return Horde_Imap_Client_Fetch_Results A results object. * * @throws Horde_Imap_Client_Exception * @throws Horde_Imap_Client_Exception_NoSupportExtension */ public function fetch($mailbox, $query, array $options = array()) { $this->login(); $query = clone $query; $cache_array = $header_cache = $new_query = array(); $res_seq = null; if (empty($options['ids'])) { $options['ids'] = $this->getIdsOb(Horde_Imap_Client_Ids::ALL); } elseif ($options['ids']->isEmpty()) { return new Horde_Imap_Client_Fetch_Results($this->_fetchDataClass); } elseif ($options['ids']->search_res && !$this->queryCapability('SEARCHRES')) { /* SEARCHRES requires server support. */ throw new Horde_Imap_Client_Exception_NoSupportExtension('SEARCHRES'); } $this->openMailbox($mailbox, Horde_Imap_Client::OPEN_AUTO); $cf = $this->_initCache(true) ? $this->_cacheFields() : array(); if (!empty($cf)) { /* If using cache, we store by UID so we need to return UIDs. */ $query->uid(); } if ($query->contains(Horde_Imap_Client::FETCH_MODSEQ) && !isset($this->_init['enabled']['CONDSTORE'])) { unset($query[$k]); } /* Determine if caching is available and if anything in $query is * cacheable. * TODO: Re-add base headertext caching. */ foreach ($cf as $k => $v) { if (isset($query[$k])) { switch ($k) { case Horde_Imap_Client::FETCH_ENVELOPE: case Horde_Imap_Client::FETCH_FLAGS: case Horde_Imap_Client::FETCH_IMAPDATE: case Horde_Imap_Client::FETCH_SIZE: case Horde_Imap_Client::FETCH_STRUCTURE: $cache_array[$k] = $v; break; case Horde_Imap_Client::FETCH_HEADERS: $this->_temp['headers_caching'] = array(); foreach ($query[$k] as $key => $val) { /* Only cache if directly requested. Iterate through * requests to ensure at least one can be cached. */ if (!empty($val['cache']) && !empty($val['peek'])) { $cache_array[$k] = $v; ksort($val); $header_cache[$key] = hash('md5', serialize($val)); } } break; } } } $ret = new Horde_Imap_Client_Fetch_Results($this->_fetchDataClass, $options['ids']->sequence ? Horde_Imap_Client_Fetch_Results::SEQUENCE : Horde_Imap_Client_Fetch_Results::UID); /* If nothing is cacheable, we can do a straight search. */ if (empty($cache_array)) { $this->_fetch($ret, $query, $options); return $ret; } /* If doing a changedsince search, limit the UIDs now. */ if (!empty($options['changedsince'])) { $changed_query = new Horde_Imap_Client_Fetch_Query(); if ($options['ids']->sequence) { $changed_query->seq(); } else { $changed_query->uid(); } $this->_fetch($ret, $changed_query, array('changedsince' => $options['changedsince'], 'ids' => $options['ids'])); if (!count($ret)) { return $ret; } $options['ids'] = $this->getIdsOb($ret->ids(), $options['ids']->sequence); } /* Grab Seq -> UID lookup. */ $res_seq = $this->_getSeqUidLookup($options['ids']); $ids = $options['ids']->sequence ? array_keys($res_seq['lookup']) : $res_seq['uids']; /* Get the cached values. */ $status_res = $this->status($this->_selected, Horde_Imap_Client::STATUS_UIDVALIDITY); $data = $this->_cache->get($this->_selected, $res_seq['uids']->ids, array_values($cache_array), $status_res['uidvalidity']); /* Build a list of what we still need. */ foreach ($ids as $val) { $crit = clone $query; if ($options['ids']->sequence) { $uid = $res_seq['lookup'][$val]; unset($crit[Horde_Imap_Client::FETCH_SEQ]); } else { $uid = $val; } /* UID will be added into the results object below. */ unset($crit[Horde_Imap_Client::FETCH_UID]); foreach ($cache_array as $key => $cid) { switch ($key) { case Horde_Imap_Client::FETCH_ENVELOPE: if (isset($data[$uid][$cid]) && $data[$uid][$cid] instanceof Horde_Imap_Client_Data_Envelope) { $ret->get($val)->setEnvelope($data[$uid][$cid]); unset($crit[$key]); } break; case Horde_Imap_Client::FETCH_FLAGS: if (isset($data[$uid][$cid]) && is_array($data[$uid][$cid])) { $ret->get($val)->setFlags($data[$uid][$cid]); unset($crit[$key]); } break; case Horde_Imap_Client::FETCH_HEADERS: /* HEADERS caching. */ foreach ($header_cache as $hkey => $hval) { if (isset($data[$uid][$cid][$hval])) { /* We have found a cached entry with the same MD5 * sum. */ $ret->get($val)->setHeaders($hkey, $data[$uid][$cid][$hval]); $crit->remove($key, $hkey); } else { $this->_temp['headers_caching'][$hkey] = $hval; } } break; case Horde_Imap_Client::FETCH_IMAPDATE: if (isset($data[$uid][$cid]) && $data[$uid][$cid] instanceof Horde_Imap_Client_DateTime) { $ret->get($val)->setImapDate($data[$uid][$cid]); unset($crit[$key]); } break; case Horde_Imap_Client::FETCH_SIZE: if (isset($data[$uid][$cid])) { $ret->get($val)->setSize($data[$uid][$cid]); unset($crit[$key]); } break; case Horde_Imap_Client::FETCH_STRUCTURE: if (isset($data[$uid][$cid]) && $data[$uid][$cid] instanceof Horde_Mime_Part) { $ret->get($val)->setStructure($data[$uid][$cid]); unset($crit[$key]); } break; } } if (count($crit)) { $sig = $crit->hash(); if (isset($new_query[$sig])) { $new_query[$sig]['i']->add($val); } else { $new_query[$sig] = array('c' => $crit, 'i' => $this->getIdsOb($val, $options['ids']->sequence)); } } } foreach ($new_query as $val) { $this->_fetch($ret, $val['c'], array_merge($options, array('ids' => $val['i']))); } foreach ($ret as $key => $val) { if ($options['ids']->sequence) { $val->setSeq($key); $val->setUid($res_seq['lookup'][$key]); } else { $val->setUid($key); } } return $ret; }
/** * @static * @param $user_id * @param $account_id * @param $folder_id * @param int $from * @param int $count * @return array */ public static function getMessages($user_id, $account_id, $folder_id, $from = 0, $count = 20) { // get the account $account = App::getAccount($user_id, $account_id); if (!$account) { #TODO: i18n return array('error' => 'unknown account'); } try { // connect to the imap server $conn = App::getImapConnection($account); $messages = array(); // $mb = new \Horde_Imap_Client_Mailbox($folder_id); $status = $conn->status($folder_id, \Horde_Imap_Client::STATUS_MESSAGES); $total = $status['messages']; if ($from + $count > $total) { $count = $total - $from; } $headers = array(); $fetch_query = new \Horde_Imap_Client_Fetch_Query(); $fetch_query->envelope(); $fetch_query->flags(); $fetch_query->seq(); $fetch_query->size(); $fetch_query->uid(); $fetch_query->imapDate(); $headers = array_merge($headers, array('importance', 'list-post', 'x-priority')); $headers[] = 'content-type'; $fetch_query->headers('imp', $headers, array('cache' => true, 'peek' => true)); $opt = array('ids' => $from + 1 . ':' . ($from + 1 + $count)); // $list is an array of Horde_Imap_Client_Data_Fetch objects. $headers = $conn->fetch($folder_id, $fetch_query); foreach ($headers as $header) { $flags = array('SEEN' => True, 'ANSWERED' => False, 'FORWARDED' => False, 'DRAFT' => False, 'HAS_ATTACHMENTS' => True); // \Horde_Imap_Client_Data_Fetch::HEADER_PARSE $f = $header->getFlags(); $date = $header->getImapDate()->format('U'); $id = $header->getUid(); $e = $header->getEnvelope(); $flags = array(); $to = $e->to_decoded[0]; $to = $to['personal']; //."<".$to['mailbox']."@".$to['host'].">"; $from = $e->from_decoded[0]; $from = $from['personal']; //."<".$from['mailbox']."@".$from['host'].">"; $messages[] = array('id' => $id, 'from' => $from, 'to' => $to, 'subject' => $e->subject_decoded, 'date' => $date, 'size' => $header->getSize(), 'flags' => $flags); } return array('account_id' => $account_id, 'folder_id' => $folder_id, 'messages' => $messages); } catch (\Horde_Imap_Client_Exception $e) { return array('error' => $e->getMessage()); } }