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