1: <?php
2:
3: class ImapMailBox {
4:
5: 6: 7: 8:
9: private $client;
10:
11: 12: 13: 14:
15: private $mailBoxName;
16:
17: public function __construct(ImapClient $client, $mailBoxName) {
18: $this->client = $client;
19: $this->mailBoxName = $mailBoxName;
20: }
21:
22: public function queryUnseenIds($sender = null) {
23: $query = new Horde_Imap_Client_Search_Query();
24: $query->flag(Horde_Imap_Client::FLAG_SEEN, false);
25: if ($sender) {
26: $query->headerText('From', $sender);
27: }
28: $results = $this->client->getHordeImapClient()->search($this->mailBoxName, $query);
29: $ids = array();
30: foreach ($results['match'] as $id) {
31: $ids[] = $id;
32: }
33: return $ids;
34: }
35:
36: public function fetchMessage($messageId) {
37: $structure = $this->_getStructure($messageId);
38: $query = new Horde_Imap_Client_Fetch_Query();
39: $query->envelope();
40: foreach (array_keys($structure->contentTypeMap()) as $typeIndex) {
41: $query->bodyPart($typeIndex, array(
42: 'peek' => true
43: ));
44: }
45: $results = $this->client->getHordeImapClient()->fetch($this->mailBoxName, $query, array(
46: 'ids' => new Horde_Imap_Client_Ids($messageId)
47: ));
48: return $this->_parseFetchData($results[$messageId], $structure);
49: }
50:
51: public function setAsSeen($messageId) {
52: $query = new Horde_Imap_Client_Fetch_Query();
53: $query->headerText(array(
54: 'peek' => false
55: ));
56: $results = $this->client->getHordeImapClient()->fetch($this->mailBoxName, $query, array(
57: 'ids' => new Horde_Imap_Client_Ids($messageId)
58: ));
59: if (!($results instanceof Horde_Imap_Client_Fetch_Results)) {
60: throw new Exception("Failed to set message (ID=$messageId) as seen");
61: }
62: }
63:
64: 65: 66: 67: 68: 69:
70: private function _getStructure($messageId) {
71: $query = new Horde_Imap_Client_Fetch_Query();
72: $query->structure();
73: $results = $this->client->getHordeImapClient()->fetch($this->mailBoxName, $query, array(
74: 'ids' => new Horde_Imap_Client_Ids($messageId)
75: ));
76: return $results[$messageId]->getStructure();
77: }
78:
79: private function _parseFetchData(Horde_Imap_Client_Data_Fetch $fetchData, Horde_Mime_Part $structure) {
80: $data = $this->_parseFetchDataHeaders($fetchData);
81: $data['bodies'] = array();
82: foreach ($structure->contentTypeMap() as $typeIndex => $typeMime) {
83: $data['bodies'][$typeMime] = $this->_parseFetchDataContents($fetchData, $structure, $typeIndex);
84: }
85: return $data;
86: }
87:
88: private function (Horde_Imap_Client_Data_Fetch $fetchData) {
89: $properties = array(
90: 'bcc', 'cc', 'date', 'from', 'in_reply_to', 'message_id',
91: 'reply_to', 'sender', 'subject', 'to'
92: );
93: $headers = array();
94: foreach ($properties as $property) {
95: $value = $fetchData->getEnvelope()->{$property};
96: if ($value instanceof Horde_Mail_Rfc822_List) {
97: $headers[$property] = $value->bare_addresses;
98: } else if ($value instanceof Horde_Imap_Client_DateTime ||
99: is_string($value)) {
100: $headers[$property] = $value;
101: } else {
102: throw new Exception('Class "' . gettype($value) . '" not mapped for property "' . $property . '"');
103: }
104: }
105: return $headers;
106: }
107:
108: private function _parseFetchDataContents(Horde_Imap_Client_Data_Fetch $fetchData, $structure, $contentsBodyPartIndex) {
109: $stream = $fetchData->getBodyPart($contentsBodyPartIndex, true);
110: $part = $structure[$contentsBodyPartIndex];
111: $part->setContents($stream, array(
112: 'usestream' => true
113: ));
114: return $part->getContents();
115: }
116:
117: }
118: