1: <?php
2: /**
3: * Object containing data returned by the Horde_Imap_Client_Base#fetch()
4: * command.
5: *
6: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (LGPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
10: *
11: * @author Michael Slusarz <slusarz@horde.org>
12: * @category Horde
13: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
14: * @package Imap_Client
15: */
16: class Horde_Imap_Client_Data_Fetch
17: {
18: /* Constants. */
19: const HEADER_PARSE = 1;
20: const HEADER_STREAM = 2;
21:
22: /**
23: * Internal data array.
24: *
25: * @var array
26: */
27: protected $_data = array();
28:
29: /**
30: * Set the full message property.
31: *
32: * @param mixed $msg The full message text, as either a string or stream
33: * resource.
34: */
35: public function setFullMsg($msg)
36: {
37: $this->_data[Horde_Imap_Client::FETCH_FULLMSG] = $msg;
38: }
39:
40: /**
41: * Returns the full message.
42: *
43: * @param boolean $stream Return as a stream?
44: *
45: * @return mixed The full text of the entire message.
46: */
47: public function getFullMsg($stream = false)
48: {
49: return $this->_msgText($stream, isset($this->_data[Horde_Imap_Client::FETCH_FULLMSG]) ? $this->_data[Horde_Imap_Client::FETCH_FULLMSG] : null);
50: }
51:
52: /**
53: * Set the message structure.
54: *
55: * @param Horde_Mime_Part $structure The base MIME part of the message.
56: */
57: public function setStructure(Horde_Mime_Part $structure)
58: {
59: $this->_data[Horde_Imap_Client::FETCH_STRUCTURE] = $structure;
60: }
61:
62: /**
63: * Get the message structure.
64: *
65: * @return Horde_Mime_Part $structure The base MIME part of the message.
66: */
67: public function getStructure()
68: {
69: return isset($this->_data[Horde_Imap_Client::FETCH_STRUCTURE])
70: ? clone $this->_data[Horde_Imap_Client::FETCH_STRUCTURE]
71: : new Horde_Mime_Part();
72: }
73:
74: /**
75: * Set a header entry.
76: *
77: * @param string $label The search label.
78: * @param mixed $data Either a Horde_Mime_Headers object or the raw
79: * header text.
80: */
81: public function setHeaders($label, $data)
82: {
83: $this->_data[Horde_Imap_Client::FETCH_HEADERS][$label] = $data;
84: }
85:
86: /**
87: * Get a header entry.
88: *
89: * @param string $label The search label.
90: * @param integer $format The return format. If self::HEADER_PARSE,
91: * returns a Horde_Mime_Headers object. If
92: * self::HEADER_STREAM, returns a stream.
93: * Otherwise, returns header text.
94: *
95: * @return mixed See $format.
96: */
97: public function getHeaders($label, $format = 0)
98: {
99: return $this->_getHeaders($label, $format, Horde_Imap_Client::FETCH_HEADERS);
100: }
101:
102: /**
103: * Set a header text entry.
104: *
105: * @param string $id The MIME ID.
106: * @param string $text The header text.
107: */
108: public function setHeaderText($id, $text)
109: {
110: $this->_data[Horde_Imap_Client::FETCH_HEADERTEXT][$id] = $text;
111: }
112:
113: /**
114: * Get a header text entry.
115: *
116: * @param string $id The MIME ID.
117: * @param integer $format The return format. If self::HEADER_PARSE,
118: * returns a Horde_Mime_Headers object. If
119: * self::HEADER_STREAM, returns a stream.
120: * Otherwise, returns header text.
121: *
122: * @return mixed See $format.
123: */
124: public function getHeaderText($id = 0, $format = 0)
125: {
126: return $this->_getHeaders($id, $format, Horde_Imap_Client::FETCH_HEADERTEXT);
127: }
128:
129: /**
130: * Set a MIME header entry.
131: *
132: * @param string $id The MIME ID.
133: * @param string $text The header text.
134: */
135: public function setMimeHeader($id, $text)
136: {
137: $this->_data[Horde_Imap_Client::FETCH_MIMEHEADER][$id] = $text;
138: }
139:
140: /**
141: * Get a MIME header entry.
142: *
143: * @param string $id The MIME ID.
144: * @param integer $format The return format. If self::HEADER_PARSE,
145: * returns a Horde_Mime_Headers object. If
146: * self::HEADER_STREAM, returns a stream.
147: * Otherwise, returns header text.
148: *
149: * @return mixed See $format.
150: */
151: public function getMimeHeader($id, $format = 0)
152: {
153: return $this->_getHeaders($id, $format, Horde_Imap_Client::FETCH_MIMEHEADER);
154: }
155:
156: /**
157: * Set a body part entry.
158: *
159: * @param string $id The MIME ID.
160: * @param mixed $text The body part text, as either a string or stream
161: * resource.
162: * @param string $decode Either '8bit', 'binary', or null.
163: */
164: public function setBodyPart($id, $text, $decode = null)
165: {
166: $this->_data[Horde_Imap_Client::FETCH_BODYPART][$id] = array(
167: 'd' => $decode,
168: 't' => $text
169: );
170: }
171:
172: /**
173: * Set the body part size for a body part.
174: *
175: * @param string $id The MIME ID.
176: * @param integer $size The size (in bytes).
177: */
178: public function setBodyPartSize($id, $size)
179: {
180: $this->_data[Horde_Imap_Client::FETCH_BODYPARTSIZE][$id] = intval($size);
181: }
182:
183: /**
184: * Get a body part entry.
185: *
186: * @param string $id The MIME ID.
187: * @param boolean $stream Return as a stream?
188: *
189: * @return mixed The full text of the body part.
190: */
191: public function getBodyPart($id, $stream = false)
192: {
193: return $this->_msgText($stream, isset($this->_data[Horde_Imap_Client::FETCH_BODYPART][$id]) ? $this->_data[Horde_Imap_Client::FETCH_BODYPART][$id]['t'] : null);
194: }
195:
196: /**
197: * Determines if/how a body part was MIME decoded on the server.
198: *
199: * @param string $id The MIME ID.
200: *
201: * @return string Either '8bit', 'binary', or null.
202: */
203: public function getBodyPartDecode($id)
204: {
205: return isset($this->_data[Horde_Imap_Client::FETCH_BODYPART][$id])
206: ? $this->_data[Horde_Imap_Client::FETCH_BODYPART][$id]['d']
207: : null;
208: }
209:
210: /**
211: * Returns the body part size, if returned by the server.
212: *
213: * @param string $id The MIME ID.
214: *
215: * @return integer The body part size, in bytes.
216: */
217: public function getBodyPartSize($id)
218: {
219: return isset($this->_data[Horde_Imap_Client::FETCH_BODYPARTSIZE][$id])
220: ? $this->_data[Horde_Imap_Client::FETCH_BODYPARTSIZE][$id]
221: : null;
222: }
223:
224: /**
225: * Set a body text entry.
226: *
227: * @param string $id The MIME ID.
228: * @param mixed $text The body part text, as either a string or stream
229: * resource.
230: */
231: public function setBodyText($id, $text)
232: {
233: $this->_data[Horde_Imap_Client::FETCH_BODYTEXT][$id] = $text;
234: }
235:
236: /**
237: * Get a body text entry.
238: *
239: * @param string $id The MIME ID.
240: * @param boolean $stream Return as a stream?
241: *
242: * @return mixed The full text of the body text.
243: */
244: public function getBodyText($id = 0, $stream = false)
245: {
246: return $this->_msgText($stream, isset($this->_data[Horde_Imap_Client::FETCH_BODYTEXT][$id]) ? $this->_data[Horde_Imap_Client::FETCH_BODYTEXT][$id] : null);
247: }
248:
249: /**
250: * Set envelope data.
251: *
252: * @param array $data The envelope data to pass to the Envelope object
253: * constructor, or an Envelope object.
254: */
255: public function setEnvelope($data)
256: {
257: $this->_data[Horde_Imap_Client::FETCH_ENVELOPE] = is_array($data)
258: ? new Horde_Imap_Client_Data_Envelope($data)
259: : $data;
260: }
261:
262: /**
263: * Get envelope data.
264: *
265: * @return Horde_Imap_Client_Data_Envelope An envelope object.
266: */
267: public function getEnvelope()
268: {
269: return isset($this->_data[Horde_Imap_Client::FETCH_ENVELOPE])
270: ? clone $this->_data[Horde_Imap_Client::FETCH_ENVELOPE]
271: : new Horde_Imap_Client_Data_Envelope();
272: }
273:
274: /**
275: * Set IMAP flags.
276: *
277: * @param array $flags An array of IMAP flags.
278: */
279: public function setFlags(array $flags)
280: {
281: $this->_data[Horde_Imap_Client::FETCH_FLAGS] = array_map('strtolower', $flags);
282: }
283:
284: /**
285: * Get IMAP flags.
286: *
287: * @return array An array of IMAP flags (all flags in lowercase).
288: */
289: public function getFlags()
290: {
291: return isset($this->_data[Horde_Imap_Client::FETCH_FLAGS])
292: ? $this->_data[Horde_Imap_Client::FETCH_FLAGS]
293: : array();
294: }
295:
296: /**
297: * Set IMAP internal date.
298: *
299: * @param mixed $date Either a Horde_Imap_Client_DateTime object or a
300: * date string.
301: */
302: public function setImapDate($date)
303: {
304: $this->_data[Horde_Imap_Client::FETCH_IMAPDATE] = is_object($date)
305: ? $date
306: : new Horde_Imap_Client_DateTime($date);
307: }
308:
309: /**
310: * Get internal IMAP date.
311: *
312: * @return Horde_Imap_Client_DateTime A date object.
313: */
314: public function getImapDate()
315: {
316: return isset($this->_data[Horde_Imap_Client::FETCH_IMAPDATE])
317: ? clone $this->_data[Horde_Imap_Client::FETCH_IMAPDATE]
318: : new Horde_Imap_Client_DateTime();
319: }
320:
321: /**
322: * Set message size.
323: *
324: * @param integer $size The size of the message, in bytes.
325: */
326: public function setSize($size)
327: {
328: $this->_data[Horde_Imap_Client::FETCH_SIZE] = intval($size);
329: }
330:
331: /**
332: * Get message size.
333: *
334: * @return integer The size of the message, in bytes.
335: */
336: public function getSize()
337: {
338: return isset($this->_data[Horde_Imap_Client::FETCH_SIZE])
339: ? $this->_data[Horde_Imap_Client::FETCH_SIZE]
340: : 0;
341: }
342:
343: /**
344: * Set UID.
345: *
346: * @param integer $uid The message UID.
347: */
348: public function setUid($uid)
349: {
350: $this->_data[Horde_Imap_Client::FETCH_UID] = intval($uid);
351: }
352:
353: /**
354: * Get UID.
355: *
356: * @return integer The message UID.
357: */
358: public function getUid()
359: {
360: return isset($this->_data[Horde_Imap_Client::FETCH_UID])
361: ? $this->_data[Horde_Imap_Client::FETCH_UID]
362: : null;
363: }
364:
365: /**
366: * Set message sequence number.
367: *
368: * @param integer $seq The message sequence number.
369: */
370: public function setSeq($seq)
371: {
372: $this->_data[Horde_Imap_Client::FETCH_SEQ] = intval($seq);
373: }
374:
375: /**
376: * Get message sequence number.
377: *
378: * @return integer The message sequence number.
379: */
380: public function getSeq()
381: {
382: return isset($this->_data[Horde_Imap_Client::FETCH_SEQ])
383: ? $this->_data[Horde_Imap_Client::FETCH_SEQ]
384: : null;
385: }
386:
387: /**
388: * Set the modified sequence value for the message.
389: *
390: * @param integer $modseq The modseq value.
391: */
392: public function setModSeq($modseq)
393: {
394: $this->_data[Horde_Imap_Client::FETCH_MODSEQ] = intval($modseq);
395: }
396:
397: /**
398: * Get the modified sequence value for the message.
399: *
400: * @return integer The modseq value.
401: */
402: public function getModSeq()
403: {
404: return isset($this->_data[Horde_Imap_Client::FETCH_MODSEQ])
405: ? $this->_data[Horde_Imap_Client::FETCH_MODSEQ]
406: : null;
407: }
408:
409: /**
410: * Return the internal representation of the data.
411: *
412: * @return array The data array.
413: */
414: public function getRawData()
415: {
416: return $this->_data;
417: }
418:
419: /**
420: * Merge a fetch object into this one.
421: *
422: * @param Horde_Imap_Client_Data_Fetch $data A fetch object.
423: */
424: public function merge(Horde_Imap_Client_Data_Fetch $data)
425: {
426: $this->_data = Horde_Array::replaceRecursive($this->_data, $data->getRawData());
427: }
428:
429: /**
430: * Does this object containing cacheable data of the given type?
431: *
432: * @param integer $type The type to query.
433: *
434: * @return boolean True if the type is cacheable.
435: */
436: public function exists($type)
437: {
438: return isset($this->_data[$type]);
439: }
440:
441: /**
442: * Does this object contain only default values for all fields?
443: *
444: * @since 1.2.0
445: *
446: * @return boolean True if object contains default data.
447: */
448: public function isDefault()
449: {
450: return empty($this->_data);
451: }
452:
453: /**
454: * Return text representation of a field.
455: *
456: * @param boolean $stream Return as a stream?
457: * @param mixed $data The field data (string or resource) or null if
458: * field does not exist.
459: *
460: * @return mixed Requested text representation.
461: */
462: protected function _msgText($stream, $data)
463: {
464: if ($stream) {
465: if (is_resource($data)) {
466: rewind($data);
467: return $data;
468: }
469:
470: $tmp = fopen('php://temp', 'w+');
471:
472: if (!is_null($data)) {
473: fwrite($tmp, $data);
474: rewind($tmp);
475: }
476:
477: return $tmp;
478: }
479:
480: if (is_resource($data)) {
481: rewind($data);
482: return stream_get_contents($data);
483: }
484:
485: return strval($data);
486: }
487:
488: /**
489: * Return representation of a header field.
490: *
491: * @param string $id The header id.
492: * @param integer $format The return format. If self::HEADER_PARSE,
493: * returns a Horde_Mime_Headers object. If
494: * self::HEADER_STREAM, returns a stream.
495: * Otherwise, returns header text.
496: * @param integer $key The array key where the data is stored in the
497: * internal array.
498: *
499: * @return mixed The data in the format specified by $format.
500: */
501: protected function _getHeaders($id, $format, $key)
502: {
503: switch ($format) {
504: case self::HEADER_STREAM:
505: if (!isset($this->_data[$key][$id])) {
506: return $this->_msgText(true, null);
507: } elseif (is_object($this->_data[$key][$id])) {
508: return $this->_getHeaders($id, 0, $key);
509: }
510: return $this->_msgText(true, $this->_data[$key][$id]);
511:
512: case self::HEADER_PARSE:
513: if (!isset($this->_data[$key][$id])) {
514: return new Horde_Mime_Headers();
515: } elseif (is_object($this->_data[$key][$id])) {
516: return clone $this->_data[$key][$id];
517: }
518: return Horde_Mime_Headers::parseHeaders($this->_getHeaders($id, 0, $key));
519: }
520:
521: if (!isset($this->_data[$key][$id])) {
522: return '';
523: }
524:
525: return is_object($this->_data[$key][$id])
526: ? $this->_data[$key][$id]->toString(array('nowrap' => true))
527: : $this->_msgText(false, $this->_data[$key][$id]);
528: }
529:
530: }
531: