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