59177427 |
1 | <?php |
7350889b |
2 | |
35586184 |
3 | /** |
4 | * imap_messages.php |
5 | * |
82d304a0 |
6 | * Copyright (c) 1999-2004 The SquirrelMail Project Team |
35586184 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This implements functions that manipulate messages |
d6c32258 |
10 | * NOTE: Quite a few functions in this file are obsolete |
35586184 |
11 | * |
12 | * $Id$ |
d6c32258 |
13 | * @package squirrelmail |
35586184 |
14 | */ |
052e0c26 |
15 | |
d6c32258 |
16 | /** |
17 | * Copies specified messages to specified folder |
18 | * @param int $imap_stream The resource ID for the IMAP connection |
19 | * @param string $start Beginning of range to copy |
20 | * @param string $end End of the range to copy |
21 | * @param string $mailbox Which box to copy to |
22 | * @deprecated This function is obsolete and should not be used |
23 | */ |
35586184 |
24 | function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) { |
6201339c |
25 | $read = sqimap_run_command ($imap_stream, "COPY $start:$end " . sqimap_encode_mailbox_name($mailbox), true, $response, $message, TRUE); |
97f7ddf2 |
26 | } |
27 | |
7c3e0802 |
28 | /** |
29 | * copy a range of messages ($id) to another mailbox ($mailbox) |
d6c32258 |
30 | * @param int $imap_stream The resource ID for the IMAP socket |
31 | * @param string $id The list of messages to copy |
32 | * @param string $mailbox The destination to copy to |
33 | * @return void |
7c3e0802 |
34 | */ |
034fddf9 |
35 | function sqimap_msgs_list_copy ($imap_stream, $id, $mailbox) { |
034fddf9 |
36 | $msgs_id = sqimap_message_list_squisher($id); |
6201339c |
37 | $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($mailbox), true, $response, $message, TRUE); |
7c3e0802 |
38 | } |
39 | |
40 | /** |
41 | * move a range of messages ($id) to another mailbox. Deletes the originals. |
d6c32258 |
42 | * @param int $imap_stream The resource ID for the IMAP socket |
43 | * @param string $id The list of messages to move |
44 | * @param string $mailbox The destination to move to |
45 | * @return void |
7c3e0802 |
46 | */ |
47 | function sqimap_msgs_list_move ($imap_stream, $id, $mailbox) { |
7c3e0802 |
48 | $msgs_id = sqimap_message_list_squisher($id); |
6201339c |
49 | $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($mailbox), true, $response, $message, TRUE); |
50 | $read = sqimap_run_command ($imap_stream, "STORE $msgs_id +FLAGS (\\Deleted)", true, $response,$message, TRUE); |
034fddf9 |
51 | } |
52 | |
53 | |
d6c32258 |
54 | /** |
55 | * Deletes specified messages and moves them to trash if possible |
56 | * @deprecated This function is obsolete and should no longer be used |
57 | * @param int $imap_steam The resource ID for the IMAP connection |
58 | * @param string $start Start of range |
59 | * @param string $end End of range |
60 | * @param string $mailbox Mailbox messages are being deleted from |
61 | * @return void |
62 | */ |
abafb676 |
63 | function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox, $bypass_trash=false) { |
6201339c |
64 | global $move_to_trash, $trash_folder, $auto_expunge; |
97f7ddf2 |
65 | |
abafb676 |
66 | if (($move_to_trash == true) && ($bypass_trash != true) && |
67 | (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) { |
97f7ddf2 |
68 | sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder); |
97f7ddf2 |
69 | } |
d8a8203a |
70 | sqimap_messages_flag ($imap_stream, $start, $end, "Deleted", true); |
97f7ddf2 |
71 | } |
72 | |
6c540963 |
73 | function sqimap_msgs_list_delete ($imap_stream, $mailbox, $id, $bypass_trash=false) { |
6201339c |
74 | global $move_to_trash, $trash_folder; |
034fddf9 |
75 | $msgs_id = sqimap_message_list_squisher($id); |
abafb676 |
76 | if (($move_to_trash == true) && ($bypass_trash != true) && |
77 | (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder)) ) { |
6201339c |
78 | $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($trash_folder), true, $response, $message, TRUE); |
034fddf9 |
79 | } |
6201339c |
80 | $read = sqimap_run_command ($imap_stream, "STORE $msgs_id +FLAGS (\\Deleted)", true, $response, $message, TRUE); |
034fddf9 |
81 | } |
82 | |
83 | |
d6c32258 |
84 | /** |
85 | * Sets the specified messages with specified flag |
86 | */ |
d8a8203a |
87 | function sqimap_messages_flag ($imap_stream, $start, $end, $flag, $handle_errors) { |
6201339c |
88 | $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", $handle_errors, $response, $message, TRUE); |
97f7ddf2 |
89 | } |
90 | |
034fddf9 |
91 | function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) { |
034fddf9 |
92 | $msgs_id = sqimap_message_list_squisher($id); |
93 | $set_string = ($set ? '+' : '-'); |
6201339c |
94 | $read = sqimap_run_command ($imap_stream, "STORE $msgs_id ".$set_string."FLAGS ($flag)", $handle_errors, $response, $message, TRUE); |
034fddf9 |
95 | } |
96 | |
48af4b64 |
97 | /** @deprecated */ |
97f7ddf2 |
98 | function sqimap_get_small_header ($imap_stream, $id, $sent) { |
098ea084 |
99 | $res = sqimap_get_small_header_list($imap_stream, $id, $sent); |
97f7ddf2 |
100 | return $res[0]; |
101 | } |
3411d4ec |
102 | |
48af4b64 |
103 | /** |
3411d4ec |
104 | * Sort the message list and crunch to be as small as possible |
97f7ddf2 |
105 | * (overflow could happen, so make it small if possible) |
106 | */ |
107 | function sqimap_message_list_squisher($messages_array) { |
108 | if( !is_array( $messages_array ) ) { |
fe70ae27 |
109 | return $messages_array; |
97f7ddf2 |
110 | } |
2d34da11 |
111 | |
97f7ddf2 |
112 | sort($messages_array, SORT_NUMERIC); |
113 | $msgs_str = ''; |
114 | while ($messages_array) { |
115 | $start = array_shift($messages_array); |
116 | $end = $start; |
117 | while (isset($messages_array[0]) && $messages_array[0] == $end + 1) { |
118 | $end = array_shift($messages_array); |
119 | } |
120 | if ($msgs_str != '') { |
121 | $msgs_str .= ','; |
122 | } |
123 | $msgs_str .= $start; |
124 | if ($start != $end) { |
125 | $msgs_str .= ':' . $end; |
126 | } |
127 | } |
97f7ddf2 |
128 | return $msgs_str; |
3411d4ec |
129 | } |
97f7ddf2 |
130 | |
48af4b64 |
131 | /** |
132 | * Get sort order from server and return it as the $id array for mailbox_display. |
aa0da530 |
133 | */ |
2d34da11 |
134 | function sqimap_get_sort_order ($imap_stream, $sort, $mbxresponse) { |
ba80e62b |
135 | global $default_charset, $thread_sort_messages, |
136 | $internal_date_sort, $server_sort_array, |
6201339c |
137 | $sent_folder, $mailbox; |
2d34da11 |
138 | |
d7c82551 |
139 | if (sqsession_is_registered('server_sort_array')) { |
9eb0fbd4 |
140 | sqsession_unregister('server_sort_array'); |
60a3e687 |
141 | } |
2d34da11 |
142 | |
aa0da530 |
143 | $sort_on = array(); |
144 | $reverse = 0; |
145 | $server_sort_array = array(); |
146 | $sort_test = array(); |
147 | $sort_query = ''; |
2d34da11 |
148 | |
149 | if ($sort == 6) { |
6201339c |
150 | if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) { |
151 | $uidnext = $mbxresponse['UIDNEXT']-1; |
2d34da11 |
152 | } else { |
6201339c |
153 | $uidnext = '*'; |
154 | } |
155 | $query = "SEARCH UID 1:$uidnext"; |
156 | $uids = sqimap_run_command ($imap_stream, $query, true, $response, $message, true); |
157 | if (isset($uids[0])) { |
158 | if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) { |
159 | $server_sort_array = preg_split("/ /", trim($regs[1])); |
160 | } |
161 | } |
162 | if (!preg_match("/OK/", $response)) { |
163 | $server_sort_array = 'no'; |
cdca177a |
164 | } |
770f0e03 |
165 | $server_sort_array = array_reverse($server_sort_array); |
9eb0fbd4 |
166 | sqsession_register($server_sort_array, 'server_sort_array'); |
2d34da11 |
167 | return $server_sort_array; |
168 | } |
169 | |
aa0da530 |
170 | $sort_on = array (0=> 'DATE', |
171 | 1=> 'DATE', |
172 | 2=> 'FROM', |
173 | 3=> 'FROM', |
174 | 4=> 'SUBJECT', |
d11ccd82 |
175 | 5=> 'SUBJECT'); |
aa0da530 |
176 | if ($internal_date_sort == true) { |
177 | $sort_on[0] = 'ARRIVAL'; |
178 | $sort_on[1] = 'ARRIVAL'; |
179 | } |
ba80e62b |
180 | if ($sent_folder == $mailbox) { |
181 | $sort_on[2] = 'TO'; |
182 | $sort_on[3] = 'TO'; |
183 | } |
aa0da530 |
184 | if (!empty($sort_on[$sort])) { |
a18594b2 |
185 | $query = "SORT ($sort_on[$sort]) ".strtoupper($default_charset).' ALL'; |
6201339c |
186 | $sort_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE); |
aa0da530 |
187 | } |
2728fa19 |
188 | if (isset($sort_test[0])) { |
a8a70251 |
189 | for ($i=0,$iCnt=count($sort_test);$i<$iCnt;++$i) { |
190 | if (preg_match("/^\* SORT (.+)$/", $sort_test[$i], $regs)) { |
cdca177a |
191 | $server_sort_array = preg_split("/ /", trim($regs[1])); |
a8a70251 |
192 | break; |
cdca177a |
193 | } |
a8a70251 |
194 | } |
aa0da530 |
195 | } |
196 | if ($sort == 0 || $sort == 2 || $sort == 4) { |
197 | $server_sort_array = array_reverse($server_sort_array); |
198 | } |
26eca02e |
199 | if (!preg_match("/OK/", $response)) { |
cdca177a |
200 | $server_sort_array = 'no'; |
201 | } |
9eb0fbd4 |
202 | sqsession_register($server_sort_array, 'server_sort_array'); |
aa0da530 |
203 | return $server_sort_array; |
204 | } |
2d34da11 |
205 | |
26b22b20 |
206 | /** |
207 | * Get sort order from server if server does not have the SORT extension |
208 | * and return it as array for mailbox_display. |
209 | * |
210 | * @param resource $imap_stream |
211 | * @param array $mbxresponse response from a sqimap_mailbox_select |
212 | * @return array $php_sort_array |
213 | */ |
2d34da11 |
214 | function sqimap_get_php_sort_order ($imap_stream, $mbxresponse) { |
2d34da11 |
215 | |
d7c82551 |
216 | if (sqsession_is_registered('php_sort_array')) { |
9eb0fbd4 |
217 | sqsession_unregister('php_sort_array'); |
2d34da11 |
218 | } |
219 | |
2d34da11 |
220 | $php_sort_array = array(); |
221 | |
6201339c |
222 | if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) { |
223 | $uidnext = $mbxresponse['UIDNEXT']-1; |
224 | } else { |
225 | $uidnext = '*'; |
226 | } |
227 | $query = "SEARCH UID 1:$uidnext"; |
228 | $uids = sqimap_run_command ($imap_stream, $query, true, $response, $message, true); |
229 | if (isset($uids[0])) { |
230 | $php_sort_array = array(); |
231 | // EIMS workaround. EIMS returns the result as multiple untagged SEARCH responses |
232 | foreach($uids as $line) { |
233 | if (preg_match("/^\* SEARCH (.+)$/", $line, $regs)) { |
234 | $php_sort_array += preg_split("/ /", trim($regs[1])); |
cdca177a |
235 | } |
236 | } |
6201339c |
237 | } |
238 | if (!preg_match("/OK/", $response)) { |
239 | $php_sort_array = 'no'; |
2d34da11 |
240 | } |
9eb0fbd4 |
241 | sqsession_register($php_sort_array, 'php_sort_array'); |
2d34da11 |
242 | return $php_sort_array; |
cdca177a |
243 | } |
244 | |
2d34da11 |
245 | |
48af4b64 |
246 | /** |
247 | * Returns an indent array for printMessageinfo() |
248 | * This represents the amount of indent needed (value), |
249 | * for this message number (key) |
250 | */ |
7c612fdd |
251 | function get_parent_level ($imap_stream) { |
252 | global $sort_by_ref, $default_charset, $thread_new; |
48af4b64 |
253 | $parent = ''; |
254 | $child = ''; |
255 | $cutoff = 0; |
cdca177a |
256 | |
288bbce0 |
257 | /* loop through the threads and take unwanted characters out |
258 | of the thread string then chop it up |
259 | */ |
7c612fdd |
260 | for ($i=0;$i<count($thread_new);$i++) { |
261 | $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]); |
262 | $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]); |
263 | $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY); |
264 | } |
265 | $indent_array = array(); |
266 | if (!$thread_new) { |
474528eb |
267 | $thread_new = array(); |
7c612fdd |
268 | } |
288bbce0 |
269 | /* looping through the parts of one message thread */ |
cdca177a |
270 | |
7c612fdd |
271 | for ($i=0;$i<count($thread_new);$i++) { |
288bbce0 |
272 | /* first grab the parent, it does not indent */ |
cdca177a |
273 | |
7c612fdd |
274 | if (isset($thread_new[$i][0])) { |
288bbce0 |
275 | if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) { |
276 | $parent = $regs[1]; |
277 | } |
7c612fdd |
278 | } |
279 | $indent_array[$parent] = 0; |
288bbce0 |
280 | |
281 | /* now the children, checking each thread portion for |
282 | ),(, and space, adjusting the level and space values |
283 | to get the indent level |
284 | */ |
285 | $level = 0; |
474528eb |
286 | $spaces = array(); |
287 | $spaces_total = 0; |
7c612fdd |
288 | $indent = 0; |
288bbce0 |
289 | $fake = FALSE; |
7c612fdd |
290 | for ($k=1;$k<(count($thread_new[$i]))-1;$k++) { |
291 | $chars = count_chars($thread_new[$i][$k], 1); |
288bbce0 |
292 | if (isset($chars['40'])) { /* testing for ( */ |
293 | $level = $level + $chars['40']; |
7c612fdd |
294 | } |
288bbce0 |
295 | if (isset($chars['41'])) { /* testing for ) */ |
296 | $level = $level - $chars['41']; |
474528eb |
297 | $spaces[$level] = 0; |
288bbce0 |
298 | /* if we were faking lets stop, this portion |
299 | of the thread is over |
300 | */ |
301 | if ($level == $cutoff) { |
302 | $fake = FALSE; |
7c612fdd |
303 | } |
304 | } |
288bbce0 |
305 | if (isset($chars['32'])) { /* testing for space */ |
474528eb |
306 | if (!isset($spaces[$level])) { |
307 | $spaces[$level] = 0; |
308 | } |
309 | $spaces[$level] = $spaces[$level] + $chars['32']; |
310 | } |
311 | for ($x=0;$x<=$level;$x++) { |
312 | if (isset($spaces[$x])) { |
313 | $spaces_total = $spaces_total + $spaces[$x]; |
314 | } |
288bbce0 |
315 | } |
474528eb |
316 | $indent = $level + $spaces_total; |
288bbce0 |
317 | /* must have run into a message that broke the thread |
318 | so we are adjusting for that portion |
319 | */ |
320 | if ($fake == TRUE) { |
321 | $indent = $indent +1; |
7c612fdd |
322 | } |
323 | if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) { |
324 | $child = $regs[1]; |
325 | } |
288bbce0 |
326 | /* the thread must be broken if $indent == 0 |
327 | so indent the message once and start faking it |
328 | */ |
329 | if ($indent == 0) { |
330 | $indent = 1; |
331 | $fake = TRUE; |
332 | $cutoff = $level; |
333 | } |
334 | /* dont need abs but if indent was negative |
335 | errors would occur |
336 | */ |
7c612fdd |
337 | $indent_array[$child] = abs($indent); |
474528eb |
338 | $spaces_total = 0; |
cdca177a |
339 | } |
7c612fdd |
340 | } |
341 | return $indent_array; |
342 | } |
343 | |
344 | |
48af4b64 |
345 | /** |
346 | * Returns an array with each element as a string representing one |
347 | * message-thread as returned by the IMAP server. |
348 | */ |
7c612fdd |
349 | function get_thread_sort ($imap_stream) { |
6201339c |
350 | global $thread_new, $sort_by_ref, $default_charset, $server_sort_array; |
d7c82551 |
351 | if (sqsession_is_registered('thread_new')) { |
9eb0fbd4 |
352 | sqsession_unregister('thread_new'); |
7c612fdd |
353 | } |
d7c82551 |
354 | if (sqsession_is_registered('server_sort_array')) { |
9eb0fbd4 |
355 | sqsession_unregister('server_sort_array'); |
60a3e687 |
356 | } |
7c612fdd |
357 | $thread_temp = array (); |
358 | if ($sort_by_ref == 1) { |
359 | $sort_type = 'REFERENCES'; |
360 | } |
361 | else { |
362 | $sort_type = 'ORDEREDSUBJECT'; |
363 | } |
a18594b2 |
364 | $query = "THREAD $sort_type ".strtoupper($default_charset)." ALL"; |
6201339c |
365 | $thread_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE); |
2728fa19 |
366 | if (isset($thread_test[0])) { |
9978560b |
367 | for ($i=0,$iCnt=count($thread_test);$i<$iCnt;++$i) { |
297e99ab |
368 | if (preg_match("/^\* THREAD (.+)$/", $thread_test[$i], $regs)) { |
9978560b |
369 | $thread_list = trim($regs[1]); |
370 | break; |
371 | } |
372 | } |
7c612fdd |
373 | } |
374 | else { |
375 | $thread_list = ""; |
376 | } |
26eca02e |
377 | if (!preg_match("/OK/", $response)) { |
cdca177a |
378 | $server_sort_array = 'no'; |
379 | return $server_sort_array; |
380 | } |
474528eb |
381 | if (isset($thread_list)) { |
382 | $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY); |
383 | } |
7c612fdd |
384 | $char_count = count($thread_temp); |
385 | $counter = 0; |
386 | $thread_new = array(); |
387 | $k = 0; |
388 | $thread_new[0] = ""; |
389 | for ($i=0;$i<$char_count;$i++) { |
390 | if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') { |
391 | $thread_new[$k] = $thread_new[$k] . $thread_temp[$i]; |
392 | } |
393 | elseif ($thread_temp[$i] == '(') { |
394 | $thread_new[$k] .= $thread_temp[$i]; |
395 | $counter++; |
396 | } |
397 | elseif ($thread_temp[$i] == ')') { |
398 | if ($counter > 1) { |
399 | $thread_new[$k] .= $thread_temp[$i]; |
400 | $counter = $counter - 1; |
401 | } |
402 | else { |
403 | $thread_new[$k] .= $thread_temp[$i]; |
404 | $k++; |
405 | $thread_new[$k] = ""; |
406 | $counter = $counter - 1; |
407 | } |
408 | } |
409 | } |
9eb0fbd4 |
410 | sqsession_register($thread_new, 'thread_new'); |
7c612fdd |
411 | $thread_new = array_reverse($thread_new); |
412 | $thread_list = implode(" ", $thread_new); |
413 | $thread_list = str_replace("(", " ", $thread_list); |
414 | $thread_list = str_replace(")", " ", $thread_list); |
415 | $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY); |
60a3e687 |
416 | $server_sort_array = $thread_list; |
9eb0fbd4 |
417 | sqsession_register($server_sort_array, 'server_sort_array'); |
7c612fdd |
418 | return $thread_list; |
419 | } |
420 | |
034fddf9 |
421 | |
7b07404c |
422 | function elapsedTime($start) { |
423 | $stop = gettimeofday(); |
424 | $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec']; |
425 | return $timepassed; |
426 | } |
7c612fdd |
427 | |
ea5fa593 |
428 | // only used in sqimap_get_small_header_list |
a18594b2 |
429 | function parseString($read,&$i) { |
430 | $char = $read{$i}; |
431 | $s = ''; |
432 | if ($char == '"') { |
433 | $iPos = ++$i; |
434 | while (true) { |
435 | $iPos = strpos($read,'"',$iPos); |
436 | if (!$iPos) break; |
437 | if ($iPos && $read{$iPos -1} != '\\') { |
438 | $s = substr($read,$i,($iPos-$i)); |
439 | $i = $iPos; |
440 | break; |
441 | } |
442 | $iPos++; |
443 | if ($iPos > strlen($read)) { |
444 | break; |
445 | } |
446 | } |
447 | } else if ($char == '{') { |
448 | $lit_cnt = ''; |
449 | ++$i; |
450 | $iPos = strpos($read,'}',$i); |
451 | if ($iPos) { |
452 | $lit_cnt = substr($read, $i, $iPos - $i); |
453 | $i += strlen($lit_cnt) + 3; /* skip } + \r + \n */ |
454 | /* Now read the literal */ |
455 | $s = ($lit_cnt ? substr($read,$i,$lit_cnt): ''); |
456 | $i += $lit_cnt; |
457 | /* temp bugfix (SM 1.5 will have a working clean version) |
458 | too much work to implement that version right now */ |
459 | --$i; |
460 | } else { /* should never happen */ |
461 | $i += 3; /* } + \r + \n */ |
462 | $s = ''; |
463 | } |
464 | } else { |
465 | return false; |
466 | } |
467 | ++$i; |
468 | return $s; |
469 | } |
470 | |
ea5fa593 |
471 | // only used in sqimap_get_small_header_list |
a18594b2 |
472 | function parseArray($read,&$i) { |
473 | $i = strpos($read,'(',$i); |
474 | $i_pos = strpos($read,')',$i); |
475 | $s = substr($read,$i+1,$i_pos - $i -1); |
476 | $a = explode(' ',$s); |
477 | if ($i_pos) { |
478 | $i = $i_pos+1; |
479 | return $a; |
480 | } else { |
481 | return false; |
482 | } |
483 | } |
484 | |
485 | function sqimap_get_small_header_list ($imap_stream, $msg_list, $show_num=false) { |
e7be116b |
486 | global $squirrelmail_language, $color, $data_dir, $username, $imap_server_type; |
6201339c |
487 | global $allow_server_sort; |
97f7ddf2 |
488 | /* Get the small headers for each message in $msg_list */ |
97f7ddf2 |
489 | $maxmsg = sizeof($msg_list); |
a18594b2 |
490 | if ($show_num != '999999') { |
491 | $msgs_str = sqimap_message_list_squisher($msg_list); |
492 | } else { |
493 | $msgs_str = '1:*'; |
494 | } |
cef054e4 |
495 | $messages = array(); |
97f7ddf2 |
496 | $read_list = array(); |
a18594b2 |
497 | |
3411d4ec |
498 | /* |
97f7ddf2 |
499 | * We need to return the data in the same order as the caller supplied |
500 | * in $msg_list, but IMAP servers are free to return responses in |
501 | * whatever order they wish... So we need to re-sort manually |
502 | */ |
503 | for ($i = 0; $i < sizeof($msg_list); $i++) { |
a18594b2 |
504 | $messages["$msg_list[$i]"] = array(); |
97f7ddf2 |
505 | } |
cdca177a |
506 | |
7b07404c |
507 | $internaldate = getPref($data_dir, $username, 'internal_date_sort'); |
508 | if ($internaldate) { |
a18594b2 |
509 | $query = "FETCH $msgs_str (FLAGS UID RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])"; |
7b07404c |
510 | } else { |
a18594b2 |
511 | $query = "FETCH $msgs_str (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])"; |
7b07404c |
512 | } |
6201339c |
513 | $read_list = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, TRUE); |
2d34da11 |
514 | $i = 0; |
a18594b2 |
515 | |
516 | foreach ($read_list as $r) { |
2a9b0fad |
517 | /* initialize/reset vars */ |
97f7ddf2 |
518 | $subject = _("(no subject)"); |
10c50790 |
519 | $from = _("Unknown sender"); |
97f7ddf2 |
520 | $priority = 0; |
9b761dbd |
521 | $messageid = '<>'; |
2a9b0fad |
522 | $type = array('',''); |
523 | $cc = $to = $inrepto = ''; |
524 | // use unset because we do isset below |
525 | unset($date); |
cef054e4 |
526 | $flag_seen = $flag_answered = $flag_deleted = $flag_flagged = false; |
a18594b2 |
527 | |
528 | $read = implode('',$r); |
529 | |
530 | /* |
531 | * #id<space>FETCH<space>( |
532 | */ |
533 | |
534 | /* extract the message id */ |
535 | $i_space = strpos($read,' ',2); |
536 | $id = substr($read,2,$i_space-2); |
537 | $fetch = substr($read,$i_space+1,5); |
538 | if (!is_numeric($id) && $fetch !== 'FETCH') { |
539 | set_up_language($squirrelmail_language); |
540 | echo '<br><b><font color=$color[2]>' . |
541 | _("ERROR : Could not complete request.") . |
542 | '</b><br>' . |
543 | _("Unknown response from IMAP server: ") . ' 1.' . |
544 | htmlspecialchars($read) . "</font><br>\n"; |
545 | break; |
546 | } |
547 | $i = strpos($read,'(',$i_space+5); |
548 | $read = substr($read,$i+1); |
549 | $i_len = strlen($read); |
550 | $i = 0; |
551 | while ($i < $i_len && $i !== false) { |
552 | /* get argument */ |
553 | $read = trim(substr($read,$i)); |
554 | $i_len = strlen($read); |
555 | $i = strpos($read,' '); |
556 | $arg = substr($read,0,$i); |
557 | ++$i; |
558 | switch ($arg) |
559 | { |
560 | case 'UID': |
561 | $i_pos = strpos($read,' ',$i); |
562 | if (!$i_pos) { |
563 | $i_pos = strpos($read,')',$i); |
cdca177a |
564 | } |
a18594b2 |
565 | if ($i_pos) { |
566 | $unique_id = substr($read,$i,$i_pos-$i); |
567 | $i = $i_pos+1; |
568 | } else { |
569 | break 3; |
cdca177a |
570 | } |
a18594b2 |
571 | break; |
572 | case 'FLAGS': |
573 | $flags = parseArray($read,$i); |
574 | if (!$flags) break 3; |
575 | foreach ($flags as $flag) { |
576 | $flag = strtolower($flag); |
577 | switch ($flag) |
578 | { |
579 | case '\\seen': $flag_seen = true; break; |
580 | case '\\answered': $flag_answered = true; break; |
581 | case '\\deleted': $flag_deleted = true; break; |
582 | case '\\flagged': $flag_flagged = true; break; |
583 | default: break; |
cdca177a |
584 | } |
585 | } |
a18594b2 |
586 | break; |
587 | case 'RFC822.SIZE': |
588 | $i_pos = strpos($read,' ',$i); |
589 | if (!$i_pos) { |
590 | $i_pos = strpos($read,')',$i); |
cdca177a |
591 | } |
a18594b2 |
592 | if ($i_pos) { |
593 | $size = substr($read,$i,$i_pos-$i); |
594 | $i = $i_pos+1; |
595 | } else { |
596 | break 3; |
597 | } |
598 | |
599 | break; |
600 | case 'INTERNALDATE': |
27932b08 |
601 | $date = parseString($read,$i); |
602 | //if ($tmpdate === false) break 3; |
603 | //$tmpdate = str_replace(' ',' ',$tmpdate); |
604 | //$tmpdate = explode(' ',$tmpdate); |
605 | //$date = str_replace('-',' ',$tmpdate[0]) . " " . |
606 | // $tmpdate[1] . ' ' . $tmpdate[2]; |
a18594b2 |
607 | break; |
608 | case 'BODY.PEEK[HEADER.FIELDS': |
609 | case 'BODY[HEADER.FIELDS': |
610 | $i = strpos($read,'{',$i); |
611 | $header = parseString($read,$i); |
92a52cda |
612 | if ($header === false) break 2; |
2a9b0fad |
613 | /* First we replace all \r\n by \n, and unfold the header */ |
614 | $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $header)); |
a18594b2 |
615 | /* Now we can make a new header array with */ |
616 | /* each element representing a headerline */ |
2a9b0fad |
617 | $hdr = explode("\n" , $hdr); |
a18594b2 |
618 | foreach ($hdr as $line) { |
619 | $pos = strpos($line, ':'); |
620 | if ($pos > 0) { |
621 | $field = strtolower(substr($line, 0, $pos)); |
622 | if (!strstr($field,' ')) { /* valid field */ |
623 | $value = trim(substr($line, $pos+1)); |
624 | switch($field) |
625 | { |
626 | case 'to': $to = $value; break; |
627 | case 'cc': $cc = $value; break; |
628 | case 'from': $from = $value; break; |
27932b08 |
629 | case 'date': $date = $value; break; |
a18594b2 |
630 | case 'x-priority': $priority = $value; break; |
631 | case 'subject': |
632 | $subject = $value; |
cdca177a |
633 | if ($subject == "") { |
634 | $subject = _("(no subject)"); |
635 | } |
636 | break; |
a18594b2 |
637 | case 'content-type': |
638 | $type = $value; |
cdca177a |
639 | if ($pos = strpos($type, ";")) { |
640 | $type = substr($type, 0, $pos); |
641 | } |
642 | $type = explode("/", $type); |
cef054e4 |
643 | if(!is_array($type)) { |
644 | $type[0] = 'text'; |
645 | } |
cdca177a |
646 | break; |
a18594b2 |
647 | default: break; |
648 | } |
cdca177a |
649 | } |
650 | } |
651 | } |
a18594b2 |
652 | break; |
653 | default: |
654 | ++$i; |
655 | break; |
cdca177a |
656 | } |
cdca177a |
657 | } |
cef054e4 |
658 | if (isset($date)) { |
659 | $date = str_replace(' ', ' ', $date); |
660 | $tmpdate = explode(' ', trim($date)); |
661 | } else { |
2a9b0fad |
662 | $tmpdate = $date = array(); |
cef054e4 |
663 | } |
6201339c |
664 | $msgi ="$unique_id"; |
665 | $messages[$msgi]['ID'] = $unique_id; |
cef054e4 |
666 | $messages[$msgi]['TIME_STAMP'] = getTimeStamp($tmpdate); |
667 | $messages[$msgi]['DATE_STRING'] = getDateString($messages[$msgi]['TIME_STAMP']); |
0c1c4c19 |
668 | $messages[$msgi]['FROM'] = $from; //parseAddress($from); |
098ea084 |
669 | $messages[$msgi]['SUBJECT'] = $subject; |
670 | // if (handleAsSent($mailbox)) { |
0c1c4c19 |
671 | $messages[$msgi]['TO'] = $to; //parseAddress($to); |
098ea084 |
672 | // } |
cef054e4 |
673 | $messages[$msgi]['PRIORITY'] = $priority; |
0c1c4c19 |
674 | $messages[$msgi]['CC'] = $cc; //parseAddress($cc); |
cef054e4 |
675 | $messages[$msgi]['SIZE'] = $size; |
676 | $messages[$msgi]['TYPE0'] = $type[0]; |
677 | $messages[$msgi]['FLAG_DELETED'] = $flag_deleted; |
678 | $messages[$msgi]['FLAG_ANSWERED'] = $flag_answered; |
679 | $messages[$msgi]['FLAG_SEEN'] = $flag_seen; |
680 | $messages[$msgi]['FLAG_FLAGGED'] = $flag_flagged; |
681 | |
682 | /* non server sort stuff */ |
683 | if (!$allow_server_sort) { |
a18594b2 |
684 | $from = parseAddress($from); |
685 | if ($from[0][1]) { |
686 | $from = decodeHeader($from[0][1]); |
687 | } else { |
688 | $from = $from[0][0]; |
689 | } |
690 | $messages[$msgi]['FROM-SORT'] = $from; |
691 | $subject_sort = strtolower(decodeHeader($subject)); |
692 | if (preg_match("/^(vedr|sv|re|aw):\s*(.*)$/si", $subject_sort, $matches)){ |
cef054e4 |
693 | $messages[$msgi]['SUBJECT-SORT'] = $matches[2]; |
a18594b2 |
694 | } else { |
695 | $messages[$msgi]['SUBJECT-SORT'] = $subject_sort; |
696 | } |
cdca177a |
697 | } |
a18594b2 |
698 | ++$msgi; |
97f7ddf2 |
699 | } |
a18594b2 |
700 | array_reverse($messages); |
701 | $new_messages = array(); |
702 | foreach ($messages as $i =>$message) { |
703 | $new_messages[] = $message; |
704 | } |
705 | return $new_messages; |
97f7ddf2 |
706 | } |
707 | |
48af4b64 |
708 | /** |
3411d4ec |
709 | * Returns a message array with all the information about a message. |
710 | * See the documentation folder for more information about this array. |
711 | */ |
97f7ddf2 |
712 | function sqimap_get_message ($imap_stream, $id, $mailbox) { |
cdca177a |
713 | |
2d34da11 |
714 | $flags = array(); |
6201339c |
715 | $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, TRUE); |
114f2a24 |
716 | if ($read) { |
b69a13a4 |
717 | if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) { |
718 | if (trim($regs[1])) { |
719 | $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY'); |
720 | } |
721 | } |
114f2a24 |
722 | } else { |
b69a13a4 |
723 | /* the message was not found, maybe the mailbox was modified? */ |
724 | global $sort, $startMessage, $color; |
725 | |
726 | $errmessage = _("The server couldn't find the message you requested.") . |
727 | '<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)."); |
728 | /* this will include a link back to the message list */ |
729 | error_message($errmessage, $mailbox, $sort, $startMessage, $color); |
730 | exit; |
2d34da11 |
731 | } |
732 | $bodystructure = implode('',$read); |
733 | $msg = mime_structure($bodystructure,$flags); |
6201339c |
734 | $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, TRUE); |
19d470aa |
735 | $rfc822_header = new Rfc822Header(); |
767ace1f |
736 | $rfc822_header->parseHeader($read); |
737 | $msg->rfc822_header = $rfc822_header; |
2d34da11 |
738 | return $msg; |
97f7ddf2 |
739 | } |
740 | |
052e0c26 |
741 | ?> |