1: <?php
2: /**
3: * Copyright 2011-2014 Horde LLC (http://www.horde.org/)
4: *
5: * See the enclosed file COPYING for license information (GPL). If you
6: * did not receive this file, see http://www.horde.org/licenses/gpl.
7: *
8: * @category Horde
9: * @copyright 2011-2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * Method to generate MBOX data.
16: *
17: * @author Didi Rieder <adrieder@sbox.tugraz.at>
18: * @author Michael Slusarz <slusarz@horde.org>
19: * @category Horde
20: * @copyright 2011-2014 Horde LLC
21: * @license http://www.horde.org/licenses/gpl GPL
22: * @package IMP
23: */
24: class IMP_Mbox_Generate
25: {
26: /**
27: * Generates a string that can be saved out to an mbox format mailbox file
28: * for a mailbox (or set of mailboxes), optionally including all
29: * subfolders of the selected mailbox(es) as well. All mailboxes will be
30: * output in the same string.
31: *
32: * @param mixed $mboxes A mailbox name (UTF-8), or list of mailbox names,
33: * to generate a mbox file for.
34: *
35: * @return resource A stream resource containing the text of a mbox
36: * format mailbox file.
37: */
38: public function generate($mboxes)
39: {
40: $body = fopen('php://temp', 'r+');
41:
42: if (!is_array($mboxes)) {
43: if (!strlen($mboxes)) {
44: return $body;
45: }
46: $mboxes = array($mboxes);
47: }
48:
49: if (empty($mboxes)) {
50: return $body;
51: }
52:
53: $query = new Horde_Imap_Client_Fetch_Query();
54: $query->envelope();
55: $query->imapDate();
56: $query->headerText(array(
57: 'peek' => true
58: ));
59: $query->bodyText(array(
60: 'peek' => true
61: ));
62:
63: foreach (IMP_Mailbox::get($mboxes) as $val) {
64: $imp_imap = $val->imp_imap;
65: $slices = $imp_imap->getSlices(
66: $val,
67: $imp_imap->getIdsOb(Horde_Imap_Client_Ids::ALL, true)
68: );
69:
70: foreach ($slices as $slice) {
71: try {
72: $res = $imp_imap->fetch($val, $query, array(
73: 'ids' => $slice,
74: 'nocache' => true
75: ));
76: } catch (IMP_Imap_Exception $e) {
77: continue;
78: }
79:
80: foreach ($res as $ptr) {
81: $from_env = $ptr->getEnvelope()->from;
82: $from = count($from_env)
83: ? $from_env[0]->bare_address
84: : '<>';
85:
86: /* We need this long command since some MUAs (e.g. pine)
87: * require a space in front of single digit days. */
88: $imap_date = $ptr->getImapDate();
89: $date = sprintf('%s %2s %s', $imap_date->format('D M'), $imap_date->format('j'), $imap_date->format('H:i:s Y'));
90: fwrite($body, 'From ' . $from . ' ' . $date . "\r\n");
91:
92: /* Remove spurious 'From ' line in headers. */
93: $stream = $ptr->getHeaderText(0, Horde_Imap_Client_Data_Fetch::HEADER_STREAM);
94: while (!feof($stream)) {
95: $line = fgets($stream);
96: if (substr($line, 0, 5) != 'From ') {
97: fwrite($body, $line);
98: }
99: }
100:
101: fwrite($body, "\r\n");
102:
103: /* Add Body text. */
104: $stream = $ptr->getBodyText(0, true);
105: while (!feof($stream)) {
106: fwrite($body, fread($stream, 8192));
107: }
108:
109: fwrite($body, "\r\n");
110: }
111: }
112: }
113:
114: return $body;
115: }
116:
117: }
118: