1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class IMP_Ui_Folder
17: {
18: 19: 20: 21: 22: 23: 24: 25:
26: public function downloadMbox($flist, $zip = false)
27: {
28: global $browser, $injector;
29:
30: $mbox = $this->generateMbox($flist);
31:
32: if ($zip) {
33: $horde_compress = Horde_Compress::factory('zip');
34: try {
35: $data = $horde_compress->compress(array(array(
36: 'data' => $mbox,
37: 'name' => reset($flist) . '.mbox'
38: )), array(
39: 'stream' => true
40: ));
41: fclose($mbox);
42: } catch (Horde_Exception $e) {
43: fclose($mbox);
44: throw $e;
45: }
46:
47: $suffix = '.zip';
48: $type = 'application/zip';
49: } else {
50: $data = $mbox;
51: $suffix = '.mbox';
52: $type = null;
53: }
54:
55: fseek($data, 0, SEEK_END);
56: $browser->downloadHeaders(reset($flist) . $suffix, $type, false, ftell($data));
57:
58: rewind($data);
59: while (!feof($data)) {
60: echo fread($data, 8192);
61: }
62: fclose($data);
63: exit;
64: }
65:
66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79:
80: public function generateMbox($folder_list)
81: {
82: $body = fopen('php://temp', 'r+');
83:
84: if (empty($folder_list)) {
85: return $body;
86: }
87:
88: $imp_imap = $GLOBALS['injector']->getInstance('IMP_Factory_Imap')->create();
89:
90: foreach ($folder_list as $folder) {
91: try {
92: $status = $imp_imap->status($folder, Horde_Imap_Client::STATUS_MESSAGES);
93: } catch (IMP_Imap_Exception $e) {
94: continue;
95: }
96:
97: $query = new Horde_Imap_Client_Fetch_Query();
98: $query->size();
99:
100: try {
101: $size = $imp_imap->fetch($folder, $query, array(
102: 'ids' => $imp_imap->getIdsOb(Horde_Imap_Client_Ids::ALL, true)
103: ));
104: } catch (IMP_Imap_Exception $e) {
105: continue;
106: }
107:
108: $curr_size = 0;
109: $start = 1;
110: $slices = array();
111:
112:
113: for ($i = 1; $i <= $status['messages']; ++$i) {
114: $curr_size += $size[$i]->getSize();
115: if ($curr_size > 5242880) {
116: $slices[] = $imp_imap->getIdsOb(range($start, $i), true);
117: $curr_size = 0;
118: $start = $i + 1;
119: }
120: }
121:
122: if ($start <= $status['messages']) {
123: $slices[] = $imp_imap->getIdsOb(range($start, $status['messages']), true);
124: }
125:
126: unset($size);
127:
128: $query = new Horde_Imap_Client_Fetch_Query();
129: $query->envelope();
130: $query->imapDate();
131: $query->fullText(array(
132: 'peek' => true
133: ));
134:
135: foreach ($slices as $slice) {
136: try {
137: $res = $imp_imap->fetch($folder, $query, array(
138: 'ids' => $slice
139: ));
140: } catch (IMP_Imap_Exception $e) {
141: continue;
142: }
143:
144: reset($res);
145: while (list(,$ptr) = each($res)) {
146: $from = '<>';
147: if ($from_env = $ptr->getEnvelope()->from) {
148: $ptr2 = reset($from_env);
149: if (!empty($ptr2['mailbox']) && !empty($ptr2['host'])) {
150: $from = $ptr2['mailbox']. '@' . $ptr2['host'];
151: }
152: }
153:
154: 155:
156: $imap_date = $ptr->getImapDate();
157: $date = sprintf('%s %2s %s', $imap_date->format('D M'), $imap_date->format('j'), $imap_date->format('H:i:s Y'));
158: fwrite($body, 'From ' . $from . ' ' . $date . "\r\n");
159: stream_copy_to_stream($ptr->getFullMsg(true), $body);
160: fwrite($body, "\r\n");
161: }
162: }
163: }
164:
165: return $body;
166: }
167:
168: 169: 170: 171: 172: 173: 174: 175: 176: 177:
178: public function importMbox($mbox, $form_name)
179: {
180: $GLOBALS['browser']->wasFileUploaded($form_name, _("mailbox file"));
181:
182: $mbox = IMP_Mailbox::get(Horde_String::convertCharset($mbox, 'UTF-8', 'UTF7-IMAP'));
183: $res = $mbox->importMbox($_FILES[$form_name]['tmp_name'], $_FILES[$form_name]['type']);
184: $mbox_name = basename(Horde_Util::dispelMagicQuotes($_FILES[$form_name]['name']));
185:
186: if ($res === false) {
187: throw new IMP_Exception(sprintf(_("There was an error importing %s."), $mbox_name));
188: }
189:
190: return sprintf(ngettext('Imported %d message from %s.', 'Imported %d messages from %s', $res), $res, $mbox_name);
191: }
192:
193: }
194: