59177427 |
1 | <?php |
7350889b |
2 | |
35586184 |
3 | /** |
258d61ed |
4 | * imap_messages.php |
5 | * |
258d61ed |
6 | * This implements functions that manipulate messages |
7 | * NOTE: Quite a few functions in this file are obsolete |
8 | * |
c997cbe6 |
9 | * @copyright 1999-2021 The SquirrelMail Project Team |
4b4abf93 |
10 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
258d61ed |
11 | * @version $Id$ |
12 | * @package squirrelmail |
13 | * @subpackage imap |
14 | */ |
052e0c26 |
15 | |
97f7ddf2 |
16 | |
7c3e0802 |
17 | /** |
8315c94c |
18 | * Copy a set of messages ($id) to another mailbox ($mailbox) |
258d61ed |
19 | * @param int $imap_stream The resource ID for the IMAP socket |
20 | * @param string $id The list of messages to copy |
21 | * @param string $mailbox The destination to copy to |
4e6e5d2d |
22 | * @param bool $handle_errors Show error messages in case of a NO, BAD or BYE response |
91c27aee |
23 | * @return bool If the copy completed without errors |
258d61ed |
24 | */ |
4e6e5d2d |
25 | function sqimap_msgs_list_copy($imap_stream, $id, $mailbox, $handle_errors = true) { |
1c198ef7 |
26 | $msgs_id = sqimap_message_list_squisher($id); |
4e6e5d2d |
27 | $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($mailbox), $handle_errors, $response, $message, TRUE); |
324ac3c5 |
28 | if ($response == 'OK') { |
29 | return true; |
30 | } else { |
31 | return false; |
32 | } |
7c3e0802 |
33 | } |
34 | |
8315c94c |
35 | |
7c3e0802 |
36 | /** |
8315c94c |
37 | * Move a set of messages ($id) to another mailbox. Deletes the originals. |
258d61ed |
38 | * @param int $imap_stream The resource ID for the IMAP socket |
39 | * @param string $id The list of messages to move |
40 | * @param string $mailbox The destination to move to |
4e6e5d2d |
41 | * @param bool $handle_errors Show error messages in case of a NO, BAD or BYE response |
f171f05a |
42 | * @param string $source_mailbox (since 1.5.1) name of source mailbox. It is used to |
821651ff |
43 | * validate that target mailbox != source mailbox. |
4e6e5d2d |
44 | * @return bool If the move completed without errors |
258d61ed |
45 | */ |
821651ff |
46 | function sqimap_msgs_list_move($imap_stream, $id, $mailbox, $handle_errors = true, $source_mailbox = false) { |
47 | if ($source_mailbox!==false && $source_mailbox==$mailbox) { |
48 | return false; |
49 | } |
4e6e5d2d |
50 | if (sqimap_msgs_list_copy ($imap_stream, $id, $mailbox, $handle_errors)) { |
324ac3c5 |
51 | return sqimap_toggle_flag($imap_stream, $id, '\\Deleted', true, true); |
52 | } else { |
53 | return false; |
54 | } |
034fddf9 |
55 | } |
56 | |
57 | |
d6c32258 |
58 | /** |
258d61ed |
59 | * Deletes a message and move it to trash or expunge the mailbox |
60 | * @param resource imap connection |
61 | * @param string $mailbox mailbox, used for checking if it concerns the trash_folder |
62 | * @param array $id list with uid's |
83246804 |
63 | * @param bool $bypass_trash (since 1.5.0) skip copy to trash |
258d61ed |
64 | * @return array $aMessageList array with messages containing the new flags and UID @see parseFetch |
83246804 |
65 | * @since 1.4.0 |
258d61ed |
66 | */ |
8315c94c |
67 | function sqimap_msgs_list_delete($imap_stream, $mailbox, $id, $bypass_trash=false) { |
fbf11cec |
68 | // FIXME: Remove globals by introducing an associative array with properties as 4th argument as replacement for the $bypass_trash variable. |
6201339c |
69 | global $move_to_trash, $trash_folder; |
abafb676 |
70 | if (($move_to_trash == true) && ($bypass_trash != true) && |
71 | (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder)) ) { |
a2aa472a |
72 | /** |
73 | * turn off internal error handling (fourth argument = false) and |
74 | * ignore copy to trash errors (allows to delete messages when overquota) |
75 | */ |
76 | sqimap_msgs_list_copy ($imap_stream, $id, $trash_folder, false); |
034fddf9 |
77 | } |
a2aa472a |
78 | return sqimap_toggle_flag($imap_stream, $id, '\\Deleted', true, true); |
034fddf9 |
79 | } |
80 | |
81 | |
258d61ed |
82 | /** |
83 | * Set a flag on the provided uid list |
84 | * @param resource imap connection |
694205bb |
85 | * @param mixed $id Normally an array which is a list with message UIDs to be flagged |
86 | * or a string range such as "1:*" |
258d61ed |
87 | * @param string $flag Flags to set/unset flags can be i.e.'\Seen', '\Answered', '\Seen \Answered' |
88 | * @param bool $set add (true) or remove (false) the provided flag |
89 | * @param bool $handle_errors Show error messages in case of a NO, BAD or BYE response |
90 | * @return array $aMessageList array with messages containing the new flags and UID @see parseFetch |
91 | */ |
034fddf9 |
92 | function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) { |
034fddf9 |
93 | $msgs_id = sqimap_message_list_squisher($id); |
94 | $set_string = ($set ? '+' : '-'); |
f6382d6b |
95 | |
694205bb |
96 | $aMessageList = array(); |
97 | // TODO: There doesn't seem to be a reason to set up $aMessageList anyway because an empty array for each message doesn't add anything to the parseFetch() return value, so this code block could be simply deleted: |
98 | if (!is_string($id)) { |
99 | for ($i=0; $i<sizeof($id); $i++) { |
100 | $aMessageList["$id[$i]"] = array(); |
101 | } |
f6382d6b |
102 | } |
103 | |
324ac3c5 |
104 | $aResponse = sqimap_run_command_list($imap_stream, "STORE $msgs_id ".$set_string."FLAGS ($flag)", $handle_errors, $response, $message, TRUE); |
f6382d6b |
105 | |
93f04c2c |
106 | // parse the fetch response |
f6382d6b |
107 | $parseFetchResults=parseFetch($aResponse,$aMessageList); |
108 | |
f6382d6b |
109 | // some broken IMAP servers do not return UID elements on UID STORE |
110 | // if this is the case, then we need to do a UID FETCH |
694205bb |
111 | if (!empty($parseFetchResults) |
112 | && !isset(reset($parseFetchResults)['UID'])) { |
f6382d6b |
113 | $aResponse = sqimap_run_command_list($imap_stream, "FETCH $msgs_id (FLAGS)", $handle_errors, $response, $message, TRUE); |
114 | $parseFetchResults = parseFetch($aResponse,$aMessageList); |
115 | } |
116 | |
117 | return ($parseFetchResults); |
034fddf9 |
118 | } |
119 | |
8315c94c |
120 | |
48af4b64 |
121 | /** |
258d61ed |
122 | * Sort the message list and crunch to be as small as possible |
123 | * (overflow could happen, so make it small if possible) |
50d214a8 |
124 | * @param array $aUid array with uid's |
125 | * @return string $s message set string |
258d61ed |
126 | */ |
50d214a8 |
127 | function sqimap_message_list_squisher($aUid) { |
128 | if( !is_array( $aUid ) ) { |
129 | return $aUid; |
97f7ddf2 |
130 | } |
50d214a8 |
131 | sort($aUid, SORT_NUMERIC); |
132 | |
133 | if (count($aUid)) { |
134 | $s = ''; |
135 | for ($i=0,$iCnt=count($aUid);$i<$iCnt;++$i) { |
136 | $iStart = $aUid[$i]; |
137 | $iEnd = $iStart; |
138 | while ($i<($iCnt-1) && $aUid[$i+1] == $iEnd +1) { |
139 | $iEnd = $aUid[$i+1]; |
140 | ++$i; |
141 | } |
142 | if ($s) { |
143 | $s .= ','; |
144 | } |
145 | $s .= $iStart; |
146 | if ($iStart != $iEnd) { |
147 | $s .= ':' . $iEnd; |
148 | } |
97f7ddf2 |
149 | } |
150 | } |
50d214a8 |
151 | return $s; |
3411d4ec |
152 | } |
97f7ddf2 |
153 | |
8315c94c |
154 | |
48af4b64 |
155 | /** |
8315c94c |
156 | * Retrieves an array with a sorted uid list. Sorting is done on the imap server |
157 | * @link http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-17.txt |
158 | * @param resource $imap_stream IMAP socket connection |
159 | * @param string $sSortField Field to sort on |
160 | * @param bool $reverse Reverse order search |
161 | * @return array $id sorted uid list |
162 | */ |
163 | function sqimap_get_sort_order($imap_stream, $sSortField, $reverse, $search='ALL') { |
ce68b76b |
164 | global $default_charset; |
2d34da11 |
165 | |
ffb776c4 |
166 | if ($sSortField) { |
167 | if ($reverse) { |
168 | $sSortField = 'REVERSE '.$sSortField; |
169 | } |
324ac3c5 |
170 | $query = "SORT ($sSortField) ".strtoupper($default_charset)." $search"; |
fbf11cec |
171 | // FIXME: sqimap_run_command() should return the parsed data accessible by $aDATA['SORT'] |
172 | // use sqimap_run_command_list() in case of unsolicited responses. If we don't we could loose the SORT response. |
f171f05a |
173 | $aData = sqimap_run_command_list ($imap_stream, $query, false, $response, $message, TRUE); |
324ac3c5 |
174 | /* fallback to default charset */ |
4ae9beb7 |
175 | if ($response == 'NO') { |
6283bb2e |
176 | if (strpos($message,'BADCHARSET') !== false || |
9aeac85e |
177 | strpos($message,'character') !== false) { |
4ae9beb7 |
178 | sqm_trigger_imap_error('SQM_IMAP_BADCHARSET',$query, $response, $message); |
179 | $query = "SORT ($sSortField) US-ASCII $search"; |
180 | $aData = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, TRUE); |
181 | } else { |
182 | sqm_trigger_imap_error('SQM_IMAP_ERROR',$query, $response, $message); |
183 | } |
184 | } else if ($response == 'BAD') { |
185 | sqm_trigger_imap_error('SQM_IMAP_NO_SORT',$query, $response, $message); |
cdca177a |
186 | } |
0fdc2fb6 |
187 | } |
324ac3c5 |
188 | |
189 | if ($response == 'OK') { |
190 | return parseUidList($aData,'SORT'); |
ffb776c4 |
191 | } else { |
324ac3c5 |
192 | return false; |
193 | } |
194 | } |
195 | |
258d61ed |
196 | |
197 | /** |
8315c94c |
198 | * Parses a UID list returned on a SORT or SEARCH request |
f171f05a |
199 | * @param array $aData imap response (retrieved from sqimap_run_command_list) |
8315c94c |
200 | * @param string $sCommand issued imap command (SEARCH or SORT) |
201 | * @return array $aUid uid list |
202 | */ |
324ac3c5 |
203 | function parseUidList($aData,$sCommand) { |
204 | $aUid = array(); |
205 | if (isset($aData) && count($aData)) { |
206 | for ($i=0,$iCnt=count($aData);$i<$iCnt;++$i) { |
f171f05a |
207 | for ($j=0,$jCnt=count($aData[$i]);$j<$jCnt;++$j) { |
208 | if (preg_match("/^\* $sCommand (.+)$/", $aData[$i][$j], $aMatch)) { |
a895042a |
209 | $aUid += explode(' ', trim($aMatch[1])); |
f171f05a |
210 | } |
324ac3c5 |
211 | } |
212 | } |
cdca177a |
213 | } |
324ac3c5 |
214 | return array_unique($aUid); |
aa0da530 |
215 | } |
2d34da11 |
216 | |
26b22b20 |
217 | /** |
258d61ed |
218 | * Retrieves an array with a sorted uid list. Sorting is done by SquirrelMail |
219 | * |
220 | * @param resource $imap_stream IMAP socket connection |
221 | * @param string $sSortField Field to sort on |
222 | * @param bool $reverse Reverse order search |
223 | * @param array $aUid limit the search to the provided array with uid's default sqimap_get_small_headers uses 1:* |
224 | * @return array $aUid sorted uid list |
225 | */ |
8315c94c |
226 | function get_squirrel_sort($imap_stream, $sSortField, $reverse = false, $aUid = NULL) { |
e0e30169 |
227 | if ($sSortField != 'RFC822.SIZE' && $sSortField != 'INTERNALDATE') { |
324ac3c5 |
228 | $msgs = sqimap_get_small_header_list($imap_stream, $aUid, |
e0e30169 |
229 | array($sSortField), array()); |
ffb776c4 |
230 | } else { |
324ac3c5 |
231 | $msgs = sqimap_get_small_header_list($imap_stream, $aUid, |
e0e30169 |
232 | array(), array($sSortField)); |
ffb776c4 |
233 | } |
d1c87b12 |
234 | |
235 | // sqimap_get_small_header (see above) returns fields in lower case, |
236 | // but the code below uses all upper case |
c97c9b89 |
237 | foreach ($msgs as $k => $v) |
238 | if (isset($msgs[$k][strtolower($sSortField)])) |
239 | $msgs[$k][strtoupper($sSortField)] = $msgs[$k][strtolower($sSortField)]; |
d1c87b12 |
240 | |
c2e29558 |
241 | $aUid = array(); |
76f29d49 |
242 | $walk = false; |
ffb776c4 |
243 | switch ($sSortField) { |
76f29d49 |
244 | // natcasesort section |
ffb776c4 |
245 | case 'FROM': |
ffb776c4 |
246 | case 'TO': |
76f29d49 |
247 | case 'CC': |
248 | if(!$walk) { |
9cfb0b2e |
249 | if (check_php_version(5, 3, 0)) |
250 | $walk_function = function(&$v,&$k,$f) { |
251 | $v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
252 | $addr = reset(parseRFC822Address($v[$f],1)); |
253 | $sPersonal = (isset($addr[SQM_ADDR_PERSONAL]) && $addr[SQM_ADDR_PERSONAL]) ? |
254 | $addr[SQM_ADDR_PERSONAL] : ""; |
255 | $sEmail = ($addr[SQM_ADDR_HOST]) ? |
256 | $addr[SQM_ADDR_MAILBOX] . "@".$addr[SQM_ADDR_HOST] : |
257 | $addr[SQM_ADDR_HOST]; |
258 | $v[$f] = ($sPersonal) ? decodeHeader($sPersonal, true, false):$sEmail; |
259 | }; |
260 | else |
261 | $walk_function = create_function('&$v,&$k,$f', |
262 | '$v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
263 | $addr = reset(parseRFC822Address($v[$f],1)); |
264 | $sPersonal = (isset($addr[SQM_ADDR_PERSONAL]) && $addr[SQM_ADDR_PERSONAL]) ? |
265 | $addr[SQM_ADDR_PERSONAL] : ""; |
266 | $sEmail = ($addr[SQM_ADDR_HOST]) ? |
267 | $addr[SQM_ADDR_MAILBOX] . "@".$addr[SQM_ADDR_HOST] : |
268 | $addr[SQM_ADDR_HOST]; |
269 | $v[$f] = ($sPersonal) ? decodeHeader($sPersonal, true, false):$sEmail;'); |
270 | array_walk($msgs, $walk_function, $sSortField); |
76f29d49 |
271 | $walk = true; |
cdca177a |
272 | } |
76f29d49 |
273 | // nobreak |
ffb776c4 |
274 | case 'SUBJECT': |
76f29d49 |
275 | if(!$walk) { |
9cfb0b2e |
276 | if (check_php_version(5, 3, 0)) |
277 | $walk_function = function(&$v,&$k,$f) { |
278 | $v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
279 | $v[$f] = strtolower(decodeHeader(trim($v[$f]), true, false)); |
280 | $v[$f] = (preg_match("/^(?:(?:vedr|sv|re|aw|fw|fwd|\[\w\]):\s*)*\s*(.*)$/si", $v[$f], $matches)) ? |
281 | $matches[1] : $v[$f]; |
282 | }; |
283 | else |
284 | $walk_function = create_function('&$v,&$k,$f', |
285 | '$v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
286 | $v[$f] = strtolower(decodeHeader(trim($v[$f]), true, false)); |
287 | $v[$f] = (preg_match("/^(?:(?:vedr|sv|re|aw|fw|fwd|\[\w\]):\s*)*\s*(.*)$/si", $v[$f], $matches)) ? |
288 | $matches[1] : $v[$f];'); |
289 | array_walk($msgs, $walk_function, $sSortField); |
76f29d49 |
290 | $walk = true; |
291 | } |
ffb776c4 |
292 | foreach ($msgs as $item) { |
324ac3c5 |
293 | $aUid[$item['UID']] = $item[$sSortField]; |
ffb776c4 |
294 | } |
76f29d49 |
295 | natcasesort($aUid); |
296 | $aUid = array_keys($aUid); |
ffb776c4 |
297 | if ($reverse) { |
e432a21c |
298 | $aUid = array_reverse($aUid); |
ffb776c4 |
299 | } |
300 | break; |
76f29d49 |
301 | // \natcasesort section |
302 | // sort_numeric section |
ffb776c4 |
303 | case 'DATE': |
76f29d49 |
304 | case 'INTERNALDATE': |
305 | if(!$walk) { |
9cfb0b2e |
306 | if (check_php_version(5, 3, 0)) |
307 | $walk_function = function(&$v,$k,$f) { |
308 | $v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
309 | $v[$f] = getTimeStamp(explode(" ",$v[$f])); |
310 | }; |
311 | else |
312 | $walk_function = create_function('&$v,$k,$f', |
313 | '$v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
314 | $v[$f] = getTimeStamp(explode(" ",$v[$f]));'); |
315 | array_walk($msgs, $walk_function, $sSortField); |
76f29d49 |
316 | $walk = true; |
ffb776c4 |
317 | } |
76f29d49 |
318 | // nobreak; |
ffb776c4 |
319 | case 'RFC822.SIZE': |
c2e29558 |
320 | if(!$walk) { |
321 | // redefine $sSortField to maintain the same namespace between |
598294a7 |
322 | // server-side sorting and SquirrelMail sorting |
c2e29558 |
323 | $sSortField = 'SIZE'; |
324 | } |
ffb776c4 |
325 | foreach ($msgs as $item) { |
324ac3c5 |
326 | $aUid[$item['UID']] = (isset($item[$sSortField])) ? $item[$sSortField] : 0; |
ffb776c4 |
327 | } |
328 | if ($reverse) { |
76f29d49 |
329 | arsort($aUid,SORT_NUMERIC); |
ffb776c4 |
330 | } else { |
76f29d49 |
331 | asort($aUid, SORT_NUMERIC); |
ffb776c4 |
332 | } |
76f29d49 |
333 | $aUid = array_keys($aUid); |
ffb776c4 |
334 | break; |
76f29d49 |
335 | // \sort_numeric section |
ffb776c4 |
336 | case 'UID': |
76f29d49 |
337 | $aUid = array_reverse($msgs); |
ffb776c4 |
338 | break; |
6201339c |
339 | } |
76f29d49 |
340 | return $aUid; |
cdca177a |
341 | } |
342 | |
48af4b64 |
343 | /** |
258d61ed |
344 | * Returns an array with each element as a string representing one |
345 | * message-thread as returned by the IMAP server. |
9a864f82 |
346 | * @param resource $imap_stream IMAP socket connection |
347 | * @param string $search optional search string |
348 | * @return array |
258d61ed |
349 | * @link http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-13.txt |
350 | */ |
8315c94c |
351 | function get_thread_sort($imap_stream, $search='ALL') { |
9a864f82 |
352 | global $sort_by_ref, $default_charset; |
324ac3c5 |
353 | |
7c612fdd |
354 | if ($sort_by_ref == 1) { |
355 | $sort_type = 'REFERENCES'; |
76f29d49 |
356 | } else { |
7c612fdd |
357 | $sort_type = 'ORDEREDSUBJECT'; |
358 | } |
324ac3c5 |
359 | $query = "THREAD $sort_type ".strtoupper($default_charset)." $search"; |
360 | |
4ae9beb7 |
361 | // TODO use sqimap_run_command_list as we do in get_server_sort() |
9a864f82 |
362 | $sRead = sqimap_run_command ($imap_stream, $query, false, $response, $message, TRUE); |
4ae9beb7 |
363 | |
9a864f82 |
364 | /* fallback to default charset */ |
4ae9beb7 |
365 | if ($response == 'NO') { |
6283bb2e |
366 | if (strpos($message,'BADCHARSET') !== false || |
9aeac85e |
367 | strpos($message,'character') !== false) { |
4ae9beb7 |
368 | sqm_trigger_imap_error('SQM_IMAP_BADCHARSET',$query, $response, $message); |
369 | $query = "THREAD $sort_type US-ASCII $search"; |
9a864f82 |
370 | $sRead = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE); |
4ae9beb7 |
371 | } else { |
372 | sqm_trigger_imap_error('SQM_IMAP_ERROR',$query, $response, $message); |
373 | } |
374 | } elseif ($response == 'BAD') { |
375 | sqm_trigger_imap_error('SQM_IMAP_NO_THREAD',$query, $response, $message); |
324ac3c5 |
376 | } |
8ae7f0d1 |
377 | $sThreadResponse = ''; |
9a864f82 |
378 | if (isset($sRead[0])) { |
379 | for ($i=0,$iCnt=count($sRead);$i<$iCnt;++$i) { |
380 | if (preg_match("/^\* THREAD (.+)$/", $sRead[$i], $aMatch)) { |
381 | $sThreadResponse = trim($aMatch[1]); |
76f29d49 |
382 | break; |
383 | } |
1c198ef7 |
384 | } |
7c612fdd |
385 | } |
9a864f82 |
386 | unset($sRead); |
387 | |
388 | if ($response !== 'OK') { |
389 | return false; |
474528eb |
390 | } |
76f29d49 |
391 | |
9a864f82 |
392 | /* Example response |
393 | * S: * THREAD (2)(3 6 (4 23)(44 7 96)) |
394 | * -- 2 |
76f29d49 |
395 | * |
9a864f82 |
396 | * -- 3 |
397 | * \-- 6 |
398 | * |-- 4 |
399 | * | \-- 23 |
400 | * | |
401 | * \-- 44 |
402 | * \-- 7 |
403 | * \-- 96 |
76f29d49 |
404 | */ |
9a864f82 |
405 | /* |
406 | * Notes for future work: |
407 | * indent_array should contain: indent_level, parent and flags, |
408 | * sibling nodes .. |
409 | * To achieve that we need to define the following flags: |
410 | * 0: hasnochildren |
411 | * 1: haschildren |
412 | * 2: is first |
413 | * 4: is last |
414 | * a node has sibling nodes if it's not the last node |
415 | * a node has no sibling nodes if it's the last node |
416 | * By using binary comparations we can store the flag in one var |
417 | * |
418 | * example: |
419 | * -1 par = 0, level = 0, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast) |
420 | * \-2 par = 1, level = 1, flag = 0 + 2 = 2 (hasnochildren, isfirst) |
421 | * |-3 par = 1, level = 1, flag = 1 + 4 = 5 (haschildren, islast) |
422 | * \-4 par = 3, level = 2, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast) |
423 | * \-5 par = 4, level = 3, flag = 0 + 2 + 4 = 6 (hasnochildren, isfirst, islast) |
424 | */ |
425 | |
426 | $j = 0; |
427 | $k = 0; |
428 | $l = 0; |
429 | $aUidThread = array(); |
430 | $aIndent = array(); |
431 | $aUidSubThread = array(); |
432 | $aDepthStack = array(); |
433 | $sUid = ''; |
434 | |
435 | if ($sThreadResponse) { |
436 | for ($i=0,$iCnt = strlen($sThreadResponse);$i<$iCnt;++$i) { |
1710ad65 |
437 | $cChar = $sThreadResponse[$i]; |
9a864f82 |
438 | switch ($cChar) { |
439 | case '(': // new sub thread |
3b8fe16c |
440 | // correction for a subthread of a thread with no parents in thread |
441 | if (!count($aUidSubThread) && $j > 0) { |
442 | --$l; |
443 | } |
9a864f82 |
444 | $aDepthStack[$j] = $l; |
445 | ++$j; |
446 | break; |
447 | case ')': // close sub thread |
448 | if($sUid !== '') { |
449 | $aUidSubThread[] = $sUid; |
450 | $aIndent[$sUid] = $j + $l - 1; |
451 | ++$l; |
452 | $sUid = ''; |
453 | } |
454 | --$j; |
455 | if ($j === 0) { |
456 | // show message that starts the thread first. |
457 | $aUidSubThread = array_reverse($aUidSubThread); |
458 | // do not use array_merge because it's extremely slow and is causing timeouts |
459 | foreach ($aUidSubThread as $iUid) { |
460 | $aUidThread[] = $iUid; |
461 | } |
462 | $aUidSubThread = array(); |
463 | $l = 0; |
464 | $aDepthStack = array(); |
465 | } else { |
466 | $l = $aDepthStack[$j]; |
467 | } |
468 | break; |
469 | case ' ': // new child |
470 | if ($sUid !== '') { |
471 | $aUidSubThread[] = $sUid; |
472 | $aIndent[$sUid] = $j + $l - 1; |
473 | ++$l; |
474 | $sUid = ''; |
475 | } |
476 | break; |
477 | default: // part of UID |
478 | $sUid .= $cChar; |
479 | break; |
76f29d49 |
480 | } |
ffb776c4 |
481 | } |
7c612fdd |
482 | } |
9a864f82 |
483 | unset($sThreadResponse); |
484 | // show newest threads first |
485 | $aUidThread = array_reverse($aUidThread); |
486 | return array($aUidThread,$aIndent); |
7c612fdd |
487 | } |
488 | |
034fddf9 |
489 | |
7b07404c |
490 | function elapsedTime($start) { |
0fdc2fb6 |
491 | $stop = gettimeofday(); |
492 | $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec']; |
493 | return $timepassed; |
7b07404c |
494 | } |
7c612fdd |
495 | |
258d61ed |
496 | /** |
497 | * Parses a string in an imap response. String starts with " or { which means it |
498 | * can handle double quoted strings and literal strings |
499 | * |
500 | * @param string $read imap response |
501 | * @param integer $i (reference) offset in string |
502 | * @return string $s parsed string without the double quotes or literal count |
503 | */ |
a18594b2 |
504 | function parseString($read,&$i) { |
1710ad65 |
505 | $char = $read[$i]; |
a18594b2 |
506 | $s = ''; |
507 | if ($char == '"') { |
0fdc2fb6 |
508 | $iPos = ++$i; |
509 | while (true) { |
510 | $iPos = strpos($read,'"',$iPos); |
511 | if (!$iPos) break; |
1710ad65 |
512 | if ($iPos && $read[$iPos -1] != '\\') { |
8315c94c |
513 | $s = substr($read,$i,($iPos-$i)); |
514 | $i = $iPos; |
515 | break; |
516 | } |
517 | $iPos++; |
518 | if ($iPos > strlen($read)) { |
519 | break; |
520 | } |
0fdc2fb6 |
521 | } |
a18594b2 |
522 | } else if ($char == '{') { |
523 | $lit_cnt = ''; |
524 | ++$i; |
525 | $iPos = strpos($read,'}',$i); |
526 | if ($iPos) { |
8315c94c |
527 | $lit_cnt = substr($read, $i, $iPos - $i); |
528 | $i += strlen($lit_cnt) + 3; /* skip } + \r + \n */ |
529 | /* Now read the literal */ |
530 | $s = ($lit_cnt ? substr($read,$i,$lit_cnt): ''); |
531 | $i += $lit_cnt; |
532 | /* temp bugfix (SM 1.5 will have a working clean version) |
533 | too much work to implement that version right now */ |
534 | --$i; |
0fdc2fb6 |
535 | } else { /* should never happen */ |
a18594b2 |
536 | $i += 3; /* } + \r + \n */ |
537 | $s = ''; |
0fdc2fb6 |
538 | } |
a18594b2 |
539 | } else { |
0fdc2fb6 |
540 | return false; |
a18594b2 |
541 | } |
542 | ++$i; |
543 | return $s; |
544 | } |
545 | |
8315c94c |
546 | |
258d61ed |
547 | /** |
548 | * Parses a string containing an array from an imap response. String starts with ( and end with ) |
549 | * |
550 | * @param string $read imap response |
551 | * @param integer $i (reference) offset in string |
552 | * @return array $a |
553 | */ |
a18594b2 |
554 | function parseArray($read,&$i) { |
555 | $i = strpos($read,'(',$i); |
556 | $i_pos = strpos($read,')',$i); |
557 | $s = substr($read,$i+1,$i_pos - $i -1); |
558 | $a = explode(' ',$s); |
559 | if ($i_pos) { |
560 | $i = $i_pos+1; |
561 | return $a; |
562 | } else { |
563 | return false; |
564 | } |
565 | } |
8315c94c |
566 | |
567 | |
258d61ed |
568 | /** |
569 | * Retrieves a list with headers, flags, size or internaldate from the imap server |
4d7369b0 |
570 | * |
91c27aee |
571 | * WARNING: function is not portable between SquirrelMail 1.2.x, 1.4.x and 1.5.x. |
4d7369b0 |
572 | * Output format, third argument and $msg_list array format requirements differ. |
573 | * @param stream $imap_stream imap connection |
574 | * @param array $msg_list array with id's to create a msgs set from |
575 | * @param array $aHeaderFields (since 1.5.0) requested header fields |
576 | * @param array $aFetchItems (since 1.5.0) requested other fetch items like FLAGS, RFC822.SIZE |
9a3d9100 |
577 | * @return array $aMessages associative array with messages. Key is the UID, value is an associative array |
4d7369b0 |
578 | * @since 1.1.3 |
258d61ed |
579 | */ |
8315c94c |
580 | function sqimap_get_small_header_list($imap_stream, $msg_list, |
91c27aee |
581 | $aHeaderFields = array('Date', 'To', 'Cc', 'From', 'Subject', 'X-Priority', 'Content-Type'), |
8cc8ec79 |
582 | $aFetchItems = array('FLAGS', 'RFC822.SIZE', 'INTERNALDATE')) { |
ffb776c4 |
583 | |
324ac3c5 |
584 | $aMessageList = array(); |
ffb776c4 |
585 | |
91c27aee |
586 | /** |
587 | * Catch other priority headers as well |
588 | */ |
589 | if (in_array('X-Priority',$aHeaderFields,true)) { |
590 | $aHeaderFields[] = 'Importance'; |
591 | $aHeaderFields[] = 'Priority'; |
592 | } |
593 | |
c075fcfe |
594 | $bUidFetch = ! in_array('UID', $aFetchItems, true); |
8cc8ec79 |
595 | |
97f7ddf2 |
596 | /* Get the small headers for each message in $msg_list */ |
258d61ed |
597 | if ($msg_list !== NULL) { |
a18594b2 |
598 | $msgs_str = sqimap_message_list_squisher($msg_list); |
ffb776c4 |
599 | /* |
600 | * We need to return the data in the same order as the caller supplied |
601 | * in $msg_list, but IMAP servers are free to return responses in |
602 | * whatever order they wish... So we need to re-sort manually |
603 | */ |
8cc8ec79 |
604 | if ($bUidFetch) { |
605 | for ($i = 0; $i < sizeof($msg_list); $i++) { |
324ac3c5 |
606 | $aMessageList["$msg_list[$i]"] = array(); |
8cc8ec79 |
607 | } |
ffb776c4 |
608 | } |
1c198ef7 |
609 | } else { |
a18594b2 |
610 | $msgs_str = '1:*'; |
611 | } |
ffb776c4 |
612 | |
3411d4ec |
613 | /* |
ffb776c4 |
614 | * Create the query |
615 | */ |
cdca177a |
616 | |
ffb776c4 |
617 | $sFetchItems = ''; |
618 | $query = "FETCH $msgs_str ("; |
619 | if (count($aFetchItems)) { |
620 | $sFetchItems = implode(' ',$aFetchItems); |
621 | } |
622 | if (count($aHeaderFields)) { |
623 | $sHeaderFields = implode(' ',$aHeaderFields); |
624 | $sFetchItems .= ' BODY.PEEK[HEADER.FIELDS ('.$sHeaderFields.')]'; |
7b07404c |
625 | } |
ffb776c4 |
626 | $query .= trim($sFetchItems) . ')'; |
324ac3c5 |
627 | $aResponse = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, $bUidFetch); |
628 | $aMessages = parseFetch($aResponse,$aMessageList); |
629 | array_reverse($aMessages); |
630 | return $aMessages; |
631 | } |
8cc8ec79 |
632 | |
8315c94c |
633 | |
258d61ed |
634 | /** |
635 | * Parses a fetch response, currently it can hande FLAGS, HEADERS, RFC822.SIZE, INTERNALDATE and UID |
636 | * @param array $aResponse Imap response |
637 | * @param array $aMessageList Placeholder array for results. The keys of the |
638 | * placeholder array should be the UID so we can reconstruct the order. |
639 | * @return array $aMessageList associative array with messages. Key is the UID, value is an associative array |
640 | * @author Marc Groot Koerkamp |
641 | */ |
3b8fe16c |
642 | function parseFetch(&$aResponse,$aMessageList = array()) { |
91c27aee |
643 | for ($j=0,$iCnt=count($aResponse);$j<$iCnt;++$j) { |
644 | $aMsg = array(); |
a18594b2 |
645 | |
91c27aee |
646 | $read = implode('',$aResponse[$j]); |
647 | // free up memmory |
648 | unset($aResponse[$j]); /* unset does not reindex the array. the for loop is safe */ |
1c198ef7 |
649 | /* |
91c27aee |
650 | * #id<space>FETCH<space>( |
651 | */ |
1c198ef7 |
652 | |
a18594b2 |
653 | /* extract the message id */ |
91c27aee |
654 | $i_space = strpos($read,' ',2);/* position 2ed <space> */ |
655 | $id = substr($read,2/* skip "*<space>" */,$i_space -2); |
656 | $aMsg['ID'] = $id; |
a18594b2 |
657 | $fetch = substr($read,$i_space+1,5); |
658 | if (!is_numeric($id) && $fetch !== 'FETCH') { |
3047e291 |
659 | $aMsg['ERROR'] = $read; // sm_encode_html_special_chars should be done just before display. this is backend code |
8cc8ec79 |
660 | break; |
a18594b2 |
661 | } |
662 | $i = strpos($read,'(',$i_space+5); |
663 | $read = substr($read,$i+1); |
664 | $i_len = strlen($read); |
665 | $i = 0; |
666 | while ($i < $i_len && $i !== false) { |
667 | /* get argument */ |
668 | $read = trim(substr($read,$i)); |
669 | $i_len = strlen($read); |
670 | $i = strpos($read,' '); |
671 | $arg = substr($read,0,$i); |
672 | ++$i; |
91c27aee |
673 | /* |
674 | * use allcaps for imap items and lowcaps for headers as key for the $aMsg array |
675 | */ |
a18594b2 |
676 | switch ($arg) |
677 | { |
678 | case 'UID': |
679 | $i_pos = strpos($read,' ',$i); |
680 | if (!$i_pos) { |
681 | $i_pos = strpos($read,')',$i); |
cdca177a |
682 | } |
a18594b2 |
683 | if ($i_pos) { |
684 | $unique_id = substr($read,$i,$i_pos-$i); |
685 | $i = $i_pos+1; |
686 | } else { |
687 | break 3; |
cdca177a |
688 | } |
a18594b2 |
689 | break; |
690 | case 'FLAGS': |
691 | $flags = parseArray($read,$i); |
692 | if (!$flags) break 3; |
ffb776c4 |
693 | $aFlags = array(); |
a18594b2 |
694 | foreach ($flags as $flag) { |
695 | $flag = strtolower($flag); |
ffb776c4 |
696 | $aFlags[$flag] = true; |
cdca177a |
697 | } |
91c27aee |
698 | $aMsg['FLAGS'] = $aFlags; |
a18594b2 |
699 | break; |
700 | case 'RFC822.SIZE': |
701 | $i_pos = strpos($read,' ',$i); |
702 | if (!$i_pos) { |
703 | $i_pos = strpos($read,')',$i); |
cdca177a |
704 | } |
a18594b2 |
705 | if ($i_pos) { |
91c27aee |
706 | $aMsg['SIZE'] = substr($read,$i,$i_pos-$i); |
a18594b2 |
707 | $i = $i_pos+1; |
708 | } else { |
709 | break 3; |
710 | } |
8cc8ec79 |
711 | break; |
712 | case 'ENVELOPE': |
91c27aee |
713 | // sqimap_parse_address($read,$i,$aMsg); |
714 | break; // to be implemented, moving imap code out of the Message class |
8cc8ec79 |
715 | case 'BODYSTRUCTURE': |
91c27aee |
716 | break; // to be implemented, moving imap code out of the Message class |
a18594b2 |
717 | case 'INTERNALDATE': |
8af247e6 |
718 | $aMsg['INTERNALDATE'] = trim(preg_replace('/\s+/', ' ',parseString($read,$i))); |
a18594b2 |
719 | break; |
720 | case 'BODY.PEEK[HEADER.FIELDS': |
721 | case 'BODY[HEADER.FIELDS': |
91c27aee |
722 | $i = strpos($read,'{',$i); // header is always returned as literal because it contain \n characters |
a18594b2 |
723 | $header = parseString($read,$i); |
92a52cda |
724 | if ($header === false) break 2; |
2a9b0fad |
725 | /* First we replace all \r\n by \n, and unfold the header */ |
726 | $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $header)); |
91c27aee |
727 | /* Now we can make a new header array with |
728 | each element representing a headerline */ |
729 | $aHdr = explode("\n" , $hdr); |
2714d4ff |
730 | $aReceived = array(); |
91c27aee |
731 | foreach ($aHdr as $line) { |
a18594b2 |
732 | $pos = strpos($line, ':'); |
733 | if ($pos > 0) { |
734 | $field = strtolower(substr($line, 0, $pos)); |
735 | if (!strstr($field,' ')) { /* valid field */ |
736 | $value = trim(substr($line, $pos+1)); |
91c27aee |
737 | switch($field) { |
738 | case 'date': |
8af247e6 |
739 | $aMsg['date'] = trim(preg_replace('/\s+/', ' ', $value)); |
91c27aee |
740 | break; |
1710ad65 |
741 | case 'x-priority': $aMsg['x-priority'] = ($value) ? (int) $value[0] : 3; break; |
91c27aee |
742 | case 'priority': |
743 | case 'importance': |
8b08e46d |
744 | // duplicate code with Rfc822Header.cls:parsePriority() |
91c27aee |
745 | if (!isset($aMsg['x-priority'])) { |
8b08e46d |
746 | $aPrio = preg_split('/\s/',trim($value)); |
ba17b6c7 |
747 | $sPrio = strtolower(array_shift($aPrio)); |
748 | if (is_numeric($sPrio)) { |
749 | $iPrio = (int) $sPrio; |
750 | } elseif ( $sPrio == 'non-urgent' || $sPrio == 'low' ) { |
aa04b27d |
751 | $iPrio = 5; |
ba17b6c7 |
752 | } elseif ( $sPrio == 'urgent' || $sPrio == 'high' ) { |
753 | $iPrio = 1; |
91c27aee |
754 | } else { |
755 | // default is normal priority |
ba17b6c7 |
756 | $iPrio = 3; |
91c27aee |
757 | } |
ba17b6c7 |
758 | $aMsg['x-priority'] = $iPrio; |
91c27aee |
759 | } |
760 | break; |
761 | case 'content-type': |
762 | $type = $value; |
763 | if ($pos = strpos($type, ";")) { |
764 | $type = substr($type, 0, $pos); |
765 | } |
766 | $type = explode("/", $type); |
767 | if(!is_array($type) || count($type) < 2) { |
768 | $aMsg['content-type'] = array('text','plain'); |
769 | } else { |
770 | $aMsg['content-type'] = array(strtolower($type[0]),strtolower($type[1])); |
771 | } |
772 | break; |
773 | case 'received': |
774 | $aMsg['received'][] = $value; |
775 | break; |
776 | default: |
777 | $aMsg[$field] = $value; |
778 | break; |
a18594b2 |
779 | } |
cdca177a |
780 | } |
781 | } |
782 | } |
a18594b2 |
783 | break; |
784 | default: |
785 | ++$i; |
786 | break; |
cdca177a |
787 | } |
cdca177a |
788 | } |
628dba17 |
789 | if (!empty($unique_id)) { |
790 | $msgi = "$unique_id"; |
791 | $aMsg['UID'] = $unique_id; |
792 | } else { |
793 | $msgi = ''; |
794 | } |
795 | $aMessageList[$msgi] = $aMsg; |
3b8fe16c |
796 | $aResponse[$j] = NULL; |
97f7ddf2 |
797 | } |
324ac3c5 |
798 | return $aMessageList; |
97f7ddf2 |
799 | } |
800 | |
258d61ed |
801 | /** |
802 | * Work in process |
803 | * @private |
804 | * @author Marc Groot Koerkamp |
805 | */ |
8cc8ec79 |
806 | function sqimap_parse_envelope($read, &$i, &$msg) { |
807 | $arg_no = 0; |
808 | $arg_a = array(); |
809 | ++$i; |
1710ad65 |
810 | for ($cnt = strlen($read); ($i < $cnt) && ($read[$i] != ')'); ++$i) { |
811 | $char = strtoupper($read[$i]); |
8cc8ec79 |
812 | switch ($char) { |
813 | case '{': |
814 | case '"': |
815 | $arg_a[] = parseString($read,$i); |
816 | ++$arg_no; |
817 | break; |
818 | case 'N': |
819 | /* probably NIL argument */ |
820 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
821 | $arg_a[] = ''; |
822 | ++$arg_no; |
823 | $i += 2; |
824 | } |
825 | break; |
826 | case '(': |
827 | /* Address structure (with group support) |
828 | * Note: Group support is useless on SMTP connections |
829 | * because the protocol doesn't support it |
830 | */ |
831 | $addr_a = array(); |
832 | $group = ''; |
833 | $a=0; |
1710ad65 |
834 | for (; $i < $cnt && $read[$i] != ')'; ++$i) { |
835 | if ($read[$i] == '(') { |
8cc8ec79 |
836 | $addr = sqimap_parse_address($read, $i); |
837 | if (($addr[3] == '') && ($addr[2] != '')) { |
838 | /* start of group */ |
839 | $group = $addr[2]; |
840 | $group_addr = $addr; |
841 | $j = $a; |
842 | } else if ($group && ($addr[3] == '') && ($addr[2] == '')) { |
843 | /* end group */ |
844 | if ($a == ($j+1)) { /* no group members */ |
845 | $group_addr[4] = $group; |
846 | $group_addr[2] = ''; |
847 | $group_addr[0] = "$group: Undisclosed recipients;"; |
848 | $addr_a[] = $group_addr; |
849 | $group =''; |
850 | } |
851 | } else { |
852 | $addr[4] = $group; |
853 | $addr_a[] = $addr; |
854 | } |
855 | ++$a; |
856 | } |
857 | } |
858 | $arg_a[] = $addr_a; |
859 | break; |
860 | default: break; |
861 | } |
862 | } |
863 | |
864 | if (count($arg_a) > 9) { |
865 | $d = strtr($arg_a[0], array(' ' => ' ')); |
866 | $d = explode(' ', $d); |
cf92500b |
867 | if (!$arg_a[1]) $arg_a[1] = ''; |
8cc8ec79 |
868 | $msg['DATE'] = $d; /* argument 1: date */ |
869 | $msg['SUBJECT'] = $arg_a[1]; /* argument 2: subject */ |
870 | $msg['FROM'] = is_array($arg_a[2]) ? $arg_a[2][0] : ''; /* argument 3: from */ |
871 | $msg['SENDER'] = is_array($arg_a[3]) ? $arg_a[3][0] : ''; /* argument 4: sender */ |
872 | $msg['REPLY-TO'] = is_array($arg_a[4]) ? $arg_a[4][0] : ''; /* argument 5: reply-to */ |
873 | $msg['TO'] = $arg_a[5]; /* argument 6: to */ |
874 | $msg['CC'] = $arg_a[6]; /* argument 7: cc */ |
875 | $msg['BCC'] = $arg_a[7]; /* argument 8: bcc */ |
876 | $msg['IN-REPLY-TO'] = $arg_a[8]; /* argument 9: in-reply-to */ |
877 | $msg['MESSAGE-ID'] = $arg_a[9]; /* argument 10: message-id */ |
878 | } |
879 | } |
880 | |
8315c94c |
881 | |
258d61ed |
882 | /** |
883 | * Work in process |
884 | * @private |
885 | * @author Marc Groot Koerkamp |
886 | */ |
8cc8ec79 |
887 | function sqimap_parse_address($read, &$i) { |
888 | $arg_a = array(); |
1710ad65 |
889 | for (; $read[$i] != ')'; ++$i) { |
890 | $char = strtoupper($read[$i]); |
8cc8ec79 |
891 | switch ($char) { |
892 | case '{': |
893 | case '"': $arg_a[] = parseString($read,$i); break; |
894 | case 'n': |
895 | case 'N': |
896 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
897 | $arg_a[] = ''; |
898 | $i += 2; |
899 | } |
900 | break; |
901 | default: break; |
902 | } |
903 | } |
904 | |
905 | if (count($arg_a) == 4) { |
906 | return $arg_a; |
907 | |
908 | // $adr = new AddressStructure(); |
909 | // $adr->personal = $arg_a[0]; |
910 | // $adr->adl = $arg_a[1]; |
911 | // $adr->mailbox = $arg_a[2]; |
912 | // $adr->host = $arg_a[3]; |
913 | } else { |
914 | $adr = ''; |
915 | } |
916 | return $adr; |
917 | } |
918 | |
8315c94c |
919 | |
48af4b64 |
920 | /** |
258d61ed |
921 | * Returns a message array with all the information about a message. |
922 | * See the documentation folder for more information about this array. |
923 | * |
924 | * @param resource $imap_stream imap connection |
925 | * @param integer $id uid of the message |
926 | * @param string $mailbox used for error handling, can be removed because we should return an error code and generate the message elsewhere |
48d015b4 |
927 | * @param int $hide Indicates whether or not to hide any errors: 0 = don't hide, 1 = hide (just exit), 2 = hide (return FALSE), 3 = hide (return error string) (OPTIONAL; default don't hide) |
928 | * @return mixed Message object or FALSE/error string if error occurred and $hide is set to 2/3 |
258d61ed |
929 | */ |
1c9425d1 |
930 | function sqimap_get_message($imap_stream, $id, $mailbox, $hide=0) { |
461eda6c |
931 | // typecast to int to prohibit 1:* msgs sets |
51bbe8fa |
932 | // Update: $id should always be sanitized into a BIGINT so this |
933 | // is being removed; leaving this code here in case something goes |
934 | // wrong, however |
935 | //$id = (int) $id; |
2d34da11 |
936 | $flags = array(); |
8315c94c |
937 | $read = sqimap_run_command($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, TRUE); |
114f2a24 |
938 | if ($read) { |
b69a13a4 |
939 | if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) { |
940 | if (trim($regs[1])) { |
75cd948c |
941 | $flags = preg_split('/ /', $regs[1],-1,PREG_SPLIT_NO_EMPTY); |
b69a13a4 |
942 | } |
943 | } |
114f2a24 |
944 | } else { |
1c9425d1 |
945 | |
946 | if ($hide == 1) exit; |
947 | if ($hide == 2) return FALSE; |
948 | |
b69a13a4 |
949 | /* the message was not found, maybe the mailbox was modified? */ |
ce8c6f42 |
950 | global $sort, $startMessage; |
b69a13a4 |
951 | |
48d015b4 |
952 | $errmessage = _("The server couldn't find the message you requested."); |
953 | |
954 | if ($hide == 3) return $errmessage; |
955 | |
956 | $errmessage .= '<p>'._("Most probably your message list was out of date and the message has been moved away or deleted (perhaps by another program accessing the same mailbox)."); |
957 | |
b69a13a4 |
958 | /* this will include a link back to the message list */ |
ce8c6f42 |
959 | error_message($errmessage, $mailbox, $sort, (int) $startMessage); |
b69a13a4 |
960 | exit; |
1c198ef7 |
961 | } |
2d34da11 |
962 | $bodystructure = implode('',$read); |
963 | $msg = mime_structure($bodystructure,$flags); |
8315c94c |
964 | $read = sqimap_run_command($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, TRUE); |
19d470aa |
965 | $rfc822_header = new Rfc822Header(); |
767ace1f |
966 | $rfc822_header->parseHeader($read); |
967 | $msg->rfc822_header = $rfc822_header; |
a4f7d027 |
968 | |
969 | parse_message_entities($msg, $id, $imap_stream); |
2d34da11 |
970 | return $msg; |
a4f7d027 |
971 | } |
972 | |
973 | |
974 | /** |
975 | * Recursively parse embedded messages (if any) in the given |
976 | * message, building correct rfc822 headers for each one |
977 | * |
978 | * @param object $msg The message object to scan for attached messages |
979 | * NOTE: this is passed by reference! Changes made |
980 | * within will affect the caller's copy of $msg! |
981 | * @param int $id The top-level message UID on the IMAP server, even |
982 | * if the $msg being passed in is only an attached entity |
983 | * thereof. |
984 | * @param resource $imap_stream A live connection to the IMAP server. |
985 | * |
986 | * @return void |
987 | * |
988 | * @since 1.5.2 |
989 | * |
990 | */ |
991 | function parse_message_entities(&$msg, $id, $imap_stream) { |
a4f7d027 |
992 | if (!empty($msg->entities)) foreach ($msg->entities as $i => $entity) { |
e49ea4ad |
993 | if (is_object($entity) && strtolower(get_class($entity)) == 'message') { |
a4f7d027 |
994 | if (!empty($entity->rfc822_header)) { |
0331a925 |
995 | $read = sqimap_run_command($imap_stream, "FETCH $id BODY[". $entity->entity_id .".HEADER]", true, $response, $message, TRUE); |
a4f7d027 |
996 | $rfc822_header = new Rfc822Header(); |
997 | $rfc822_header->parseHeader($read); |
998 | $msg->entities[$i]->rfc822_header = $rfc822_header; |
999 | } |
1000 | parse_message_entities($msg->entities[$i], $id, $imap_stream); |
1001 | } |
1002 | } |
97f7ddf2 |
1003 | } |