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