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; }
/** * Given an IMAP URL, fetches the corresponding part. * * @param Horde_Imap_Client_Url_Imap $url An IMAP URL. * * @return resource The section contents in a stream. Returns null if * the part could not be found. * * @throws Horde_Imap_Client_Exception */ public function fetchFromUrl(Horde_Imap_Client_Url_Imap $url) { $ids_ob = $this->_socket->getIdsOb($url->uid); // BODY[] if (is_null($url->section)) { $query = new Horde_Imap_Client_Fetch_Query(); $query->fullText(array('peek' => true)); $fetch = $this->_socket->fetch($url->mailbox, $query, array('ids' => $ids_ob)); return $fetch[$url->uid]->getFullMsg(true); } $section = trim($url->section); // BODY[<#.>HEADER.FIELDS<.NOT>()] if (($pos = stripos($section, 'HEADER.FIELDS')) !== false) { $hdr_pos = strpos($section, '('); $cmd = substr($section, 0, $hdr_pos); $query = new Horde_Imap_Client_Fetch_Query(); $query->headers('section', explode(' ', substr($section, $hdr_pos + 1, strrpos($section, ')') - $hdr_pos)), array('id' => $pos ? substr($section, 0, $pos - 1) : 0, 'notsearch' => stripos($cmd, '.NOT') !== false, 'peek' => true)); $fetch = $this->_socket->fetch($url->mailbox, $query, array('ids' => $ids_ob)); return $fetch[$url->uid]->getHeaders('section', Horde_Imap_Client_Data_Fetch::HEADER_STREAM); } // BODY[#] if (is_numeric(substr($section, -1))) { $query = new Horde_Imap_Client_Fetch_Query(); $query->bodyPart($section, array('peek' => true)); $fetch = $this->_socket->fetch($url->mailbox, $query, array('ids' => $ids_ob)); return $fetch[$url->uid]->getBodyPart($section, true); } // BODY[<#.>HEADER] if (($pos = stripos($section, 'HEADER')) !== false) { $id = $pos ? substr($section, 0, $pos - 1) : 0; $query = new Horde_Imap_Client_Fetch_Query(); $query->headerText(array('id' => $id, 'peek' => true)); $fetch = $this->_socket->fetch($url->mailbox, $query, array('ids' => $ids_ob)); return $fetch[$url->uid]->getHeaderText($id, Horde_Imap_Client_Data_Fetch::HEADER_STREAM); } // BODY[<#.>TEXT] if (($pos = stripos($section, 'TEXT')) !== false) { $id = $pos ? substr($section, 0, $pos - 1) : 0; $query = new Horde_Imap_Client_Fetch_Query(); $query->bodyText(array('id' => $id, 'peek' => true)); $fetch = $this->_socket->fetch($url->mailbox, $query, array('ids' => $ids_ob)); return $fetch[$url->uid]->getBodyText($id, true); } // BODY[<#.>MIMEHEADER] if (($pos = stripos($section, 'MIME')) !== false) { $id = $pos ? substr($section, 0, $pos - 1) : 0; $query = new Horde_Imap_Client_Fetch_Query(); $query->mimeHeader($id, array('peek' => true)); $fetch = $this->_socket->fetch($url->mailbox, $query, array('ids' => $ids_ob)); return $fetch[$url->uid]->getMimeHeader($id, Horde_Imap_Client_Data_Fetch::HEADER_STREAM); } return null; }
private function load() { $headers = []; $fetch_query = new \Horde_Imap_Client_Fetch_Query(); $fetch_query->bodyPart($this->attachmentId); $fetch_query->mimeHeader($this->attachmentId); $headers = array_merge($headers, ['importance', 'list-post', 'x-priority']); $headers[] = 'content-type'; $fetch_query->headers('imp', $headers, ['cache' => true]); // $list is an array of Horde_Imap_Client_Data_Fetch objects. $ids = new \Horde_Imap_Client_Ids($this->messageId); $headers = $this->conn->fetch($this->mailBox, $fetch_query, ['ids' => $ids]); /** @var $fetch Horde_Imap_Client_Data_Fetch */ if (!isset($headers[$this->messageId])) { throw new DoesNotExistException('Unable to load the attachment.'); } $fetch = $headers[$this->messageId]; $mimeHeaders = $fetch->getMimeHeader($this->attachmentId, Horde_Imap_Client_Data_Fetch::HEADER_PARSE); $this->mimePart = new \Horde_Mime_Part(); // To prevent potential problems with the SOP we serve all files with the // MIME type "application/octet-stream" $this->mimePart->setType('application/octet-stream'); // Serve all files with a content-disposition of "attachment" to prevent Cross-Site Scripting $this->mimePart->setDisposition('attachment'); // Extract headers from part $contentDisposition = $mimeHeaders->getValue('content-disposition', \Horde_Mime_Headers::VALUE_PARAMS); if (!is_null($contentDisposition)) { $vars = ['filename']; foreach ($contentDisposition as $key => $val) { if (in_array($key, $vars)) { $this->mimePart->setDispositionParameter($key, $val); } } } else { $contentDisposition = $mimeHeaders->getValue('content-type', \Horde_Mime_Headers::VALUE_PARAMS); $vars = ['name']; foreach ($contentDisposition as $key => $val) { if (in_array($key, $vars)) { $this->mimePart->setContentTypeParameter($key, $val); } } } /* Content transfer encoding. */ if ($tmp = $mimeHeaders->getValue('content-transfer-encoding')) { $this->mimePart->setTransferEncoding($tmp); } $body = $fetch->getBodyPart($this->attachmentId); $this->mimePart->setContents($body); }
/** * 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_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']); }
/** * @param \Horde_Mime_Part $p * @param int $partNo * @return string * @throws DoesNotExistException * @throws \Exception */ private function loadBodyData($p, $partNo) { // DECODE DATA $fetch_query = new \Horde_Imap_Client_Fetch_Query(); $ids = new \Horde_Imap_Client_Ids($this->messageId); $fetch_query->bodyPart($partNo, ['peek' => true]); $fetch_query->bodyPartSize($partNo); $fetch_query->mimeHeader($partNo, ['peek' => true]); $headers = $this->conn->fetch($this->mailBox, $fetch_query, ['ids' => $ids]); /** @var $fetch \Horde_Imap_Client_Data_Fetch */ $fetch = $headers[$this->messageId]; if (is_null($fetch)) { throw new DoesNotExistException("Mail body for this mail({$this->messageId}) could not be loaded"); } $mimeHeaders = $fetch->getMimeHeader($partNo, Horde_Imap_Client_Data_Fetch::HEADER_PARSE); if ($enc = $mimeHeaders->getValue('content-transfer-encoding')) { $p->setTransferEncoding($enc); } $data = $fetch->getBodyPart($partNo); $p->setContents($data); $data = $p->getContents(); $data = iconv($p->getCharset(), 'utf-8//IGNORE', $data); return $data; }