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 | */ |
e0e30169 |
140 | function sqimap_get_sort_order ($imap_stream, $sSortField,$reverse) { |
ffb776c4 |
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) { |
149 | if ($reverse) { |
150 | $sSortField = 'REVERSE '.$sSortField; |
151 | } |
152 | $query = "SORT ($sSortField) ".strtoupper($default_charset).' ALL'; |
6201339c |
153 | $sort_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE); |
aa0da530 |
154 | } |
2728fa19 |
155 | if (isset($sort_test[0])) { |
ffb776c4 |
156 | for ($i=0,$iCnt=count($sort_test);$i<$iCnt;++$i) { |
157 | if (preg_match("/^\* SORT (.+)$/", $sort_test[$i], $regs)) { |
158 | $id = preg_split("/ /", trim($regs[1])); |
159 | break; |
160 | } |
cdca177a |
161 | } |
0fdc2fb6 |
162 | } |
26eca02e |
163 | if (!preg_match("/OK/", $response)) { |
ffb776c4 |
164 | return false; |
165 | } else { |
166 | return $id; |
cdca177a |
167 | } |
aa0da530 |
168 | } |
2d34da11 |
169 | |
26b22b20 |
170 | /** |
ffb776c4 |
171 | * Retrieves an array with a sorted uid list. Sorting is done by SquirrelMail |
0fdc2fb6 |
172 | * |
ffb776c4 |
173 | * @param resource $imap_stream IMAP socket connection |
174 | * @param string $sSortField Field to sort on |
175 | * @param bool $reverse Reverse order search |
76f29d49 |
176 | * @return array $aUid sorted uid list |
0fdc2fb6 |
177 | */ |
ffb776c4 |
178 | function get_squirrel_sort ($imap_stream, $sSortField, $reverse = false) { |
e0e30169 |
179 | if ($sSortField != 'RFC822.SIZE' && $sSortField != 'INTERNALDATE') { |
ffb776c4 |
180 | $msgs = sqimap_get_small_header_list($imap_stream, false, '*', |
e0e30169 |
181 | array($sSortField), array()); |
ffb776c4 |
182 | } else { |
183 | $msgs = sqimap_get_small_header_list($imap_stream, false, '*', |
e0e30169 |
184 | array(), array($sSortField)); |
ffb776c4 |
185 | } |
c2e29558 |
186 | $aUid = array(); |
76f29d49 |
187 | $walk = false; |
ffb776c4 |
188 | switch ($sSortField) { |
76f29d49 |
189 | // natcasesort section |
ffb776c4 |
190 | case 'FROM': |
ffb776c4 |
191 | case 'TO': |
76f29d49 |
192 | case 'CC': |
193 | if(!$walk) { |
194 | array_walk($msgs, create_function('&$v,&$k,$f', |
195 | '$v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
196 | $addr = parseAddress($v[$f]); |
197 | $v[$f] = ($addr[0][1]) ? decodeHeader($addr[0][1]):$addr[0][0];'),$sSortField); |
198 | $walk = true; |
cdca177a |
199 | } |
76f29d49 |
200 | // nobreak |
ffb776c4 |
201 | case 'SUBJECT': |
76f29d49 |
202 | if(!$walk) { |
203 | array_walk($msgs, create_function('&$v,&$k,$f', |
204 | '$v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
205 | $v[$f] = strtolower(decodeHeader(trim($v[$f]))); |
206 | $v[$f] = (preg_match("/^(vedr|sv|re|aw|\[\w\]):\s*(.*)$/si", $v[$f], $matches)) ? |
207 | $matches[2] : $v[$f];'),$sSortField); |
208 | $walk = true; |
209 | } |
ffb776c4 |
210 | foreach ($msgs as $item) { |
76f29d49 |
211 | $aUid[$item['ID']] = $item[$sSortField]; |
ffb776c4 |
212 | } |
76f29d49 |
213 | natcasesort($aUid); |
214 | $aUid = array_keys($aUid); |
ffb776c4 |
215 | if ($reverse) { |
e432a21c |
216 | $aUid = array_reverse($aUid); |
ffb776c4 |
217 | } |
218 | break; |
76f29d49 |
219 | // \natcasesort section |
220 | // sort_numeric section |
ffb776c4 |
221 | case 'DATE': |
76f29d49 |
222 | case 'INTERNALDATE': |
223 | if(!$walk) { |
224 | array_walk($msgs, create_function('&$v,$k,$f', |
225 | '$v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
226 | $v[$f] = getTimeStamp(explode(" ",$v[$f]));'),$sSortField); |
227 | $walk = true; |
ffb776c4 |
228 | } |
76f29d49 |
229 | // nobreak; |
ffb776c4 |
230 | case 'RFC822.SIZE': |
c2e29558 |
231 | if(!$walk) { |
232 | // redefine $sSortField to maintain the same namespace between |
233 | // server-side sorting and squirrelmail sorting |
234 | $sSortField = 'SIZE'; |
235 | } |
ffb776c4 |
236 | foreach ($msgs as $item) { |
c2e29558 |
237 | $aUid[$item['ID']] = (isset($item[$sSortField])) ? $item[$sSortField] : 0; |
ffb776c4 |
238 | } |
239 | if ($reverse) { |
76f29d49 |
240 | arsort($aUid,SORT_NUMERIC); |
ffb776c4 |
241 | } else { |
76f29d49 |
242 | asort($aUid, SORT_NUMERIC); |
ffb776c4 |
243 | } |
76f29d49 |
244 | $aUid = array_keys($aUid); |
ffb776c4 |
245 | break; |
76f29d49 |
246 | // \sort_numeric section |
ffb776c4 |
247 | case 'UID': |
76f29d49 |
248 | $aUid = array_reverse($msgs); |
ffb776c4 |
249 | break; |
6201339c |
250 | } |
76f29d49 |
251 | return $aUid; |
cdca177a |
252 | } |
253 | |
48af4b64 |
254 | /** |
0fdc2fb6 |
255 | * Returns an indent array for printMessageinfo() |
256 | * This represents the amount of indent needed (value), |
257 | * for this message number (key) |
258 | */ |
76f29d49 |
259 | |
260 | /* |
261 | * Notes for future work: |
262 | * indent_array should contain: indent_level, parent and flags, |
263 | * sibling notes .. |
264 | * To achieve that we need to define the following flags: |
265 | * 0: hasnochildren |
266 | * 1: haschildren |
267 | * 2: is first |
268 | * 4: is last |
269 | * a node has sibling nodes if it's not the last node |
270 | * a node has no sibling nodes if it's the last node |
271 | * By using binary comparations we can store the flag in one var |
272 | * |
273 | * example: |
274 | * -1 par = 0, level = 0, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast) |
275 | * \-2 par = 1, level = 1, flag = 0 + 2 = 2 (hasnochildren, isfirst) |
276 | * |-3 par = 1, level = 1, flag = 1 + 4 = 5 (haschildren, islast) |
277 | * \-4 par = 3, level = 2, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast) |
278 | * \-5 par = 4, level = 3, flag = 0 + 2 + 4 = 6 (hasnochildren, isfirst, islast) |
279 | */ |
ffb776c4 |
280 | function get_parent_level ($thread_new) { |
48af4b64 |
281 | $parent = ''; |
282 | $child = ''; |
283 | $cutoff = 0; |
cdca177a |
284 | |
76f29d49 |
285 | /* |
286 | * loop through the threads and take unwanted characters out |
287 | * of the thread string then chop it up |
288 | */ |
7c612fdd |
289 | for ($i=0;$i<count($thread_new);$i++) { |
290 | $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]); |
291 | $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]); |
292 | $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY); |
1c198ef7 |
293 | } |
7c612fdd |
294 | $indent_array = array(); |
76f29d49 |
295 | if (!$thread_new) { |
296 | $thread_new = array(); |
297 | } |
288bbce0 |
298 | /* looping through the parts of one message thread */ |
cdca177a |
299 | |
7c612fdd |
300 | for ($i=0;$i<count($thread_new);$i++) { |
76f29d49 |
301 | /* first grab the parent, it does not indent */ |
cdca177a |
302 | |
7c612fdd |
303 | if (isset($thread_new[$i][0])) { |
288bbce0 |
304 | if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) { |
305 | $parent = $regs[1]; |
306 | } |
7c612fdd |
307 | } |
308 | $indent_array[$parent] = 0; |
288bbce0 |
309 | |
76f29d49 |
310 | /* |
311 | * now the children, checking each thread portion for |
312 | * ),(, and space, adjusting the level and space values |
313 | * to get the indent level |
314 | */ |
288bbce0 |
315 | $level = 0; |
474528eb |
316 | $spaces = array(); |
317 | $spaces_total = 0; |
7c612fdd |
318 | $indent = 0; |
288bbce0 |
319 | $fake = FALSE; |
76f29d49 |
320 | for ($k=1,$iCnt=count($thread_new[$i])-1;$k<$iCnt;++$k) { |
7c612fdd |
321 | $chars = count_chars($thread_new[$i][$k], 1); |
288bbce0 |
322 | if (isset($chars['40'])) { /* testing for ( */ |
76f29d49 |
323 | $level += $chars['40']; |
7c612fdd |
324 | } |
288bbce0 |
325 | if (isset($chars['41'])) { /* testing for ) */ |
76f29d49 |
326 | $level -= $chars['41']; |
474528eb |
327 | $spaces[$level] = 0; |
288bbce0 |
328 | /* if we were faking lets stop, this portion |
76f29d49 |
329 | * of the thread is over |
330 | */ |
288bbce0 |
331 | if ($level == $cutoff) { |
332 | $fake = FALSE; |
7c612fdd |
333 | } |
334 | } |
288bbce0 |
335 | if (isset($chars['32'])) { /* testing for space */ |
474528eb |
336 | if (!isset($spaces[$level])) { |
337 | $spaces[$level] = 0; |
338 | } |
76f29d49 |
339 | $spaces[$level] += $chars['32']; |
474528eb |
340 | } |
341 | for ($x=0;$x<=$level;$x++) { |
342 | if (isset($spaces[$x])) { |
76f29d49 |
343 | $spaces_total += $spaces[$x]; |
474528eb |
344 | } |
288bbce0 |
345 | } |
474528eb |
346 | $indent = $level + $spaces_total; |
288bbce0 |
347 | /* must have run into a message that broke the thread |
76f29d49 |
348 | * so we are adjusting for that portion |
349 | */ |
288bbce0 |
350 | if ($fake == TRUE) { |
351 | $indent = $indent +1; |
7c612fdd |
352 | } |
353 | if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) { |
354 | $child = $regs[1]; |
355 | } |
288bbce0 |
356 | /* the thread must be broken if $indent == 0 |
76f29d49 |
357 | * so indent the message once and start faking it |
358 | */ |
288bbce0 |
359 | if ($indent == 0) { |
360 | $indent = 1; |
361 | $fake = TRUE; |
362 | $cutoff = $level; |
363 | } |
364 | /* dont need abs but if indent was negative |
76f29d49 |
365 | * errors would occur |
366 | */ |
367 | $indent_array[$child] = ($indent < 0) ? 0 : $indent; |
474528eb |
368 | $spaces_total = 0; |
cdca177a |
369 | } |
7c612fdd |
370 | } |
371 | return $indent_array; |
372 | } |
373 | |
374 | |
48af4b64 |
375 | /** |
0fdc2fb6 |
376 | * Returns an array with each element as a string representing one |
377 | * message-thread as returned by the IMAP server. |
378 | */ |
7c612fdd |
379 | function get_thread_sort ($imap_stream) { |
ffb776c4 |
380 | global $thread_new, $sort_by_ref, $default_charset, $server_sort_array, $indent_array; |
d7c82551 |
381 | if (sqsession_is_registered('thread_new')) { |
9eb0fbd4 |
382 | sqsession_unregister('thread_new'); |
7c612fdd |
383 | } |
ffb776c4 |
384 | if (sqsession_is_registered('indent_array')) { |
385 | sqsession_unregister('indent_array'); |
386 | } |
d7c82551 |
387 | if (sqsession_is_registered('server_sort_array')) { |
9eb0fbd4 |
388 | sqsession_unregister('server_sort_array'); |
60a3e687 |
389 | } |
7c612fdd |
390 | $thread_temp = array (); |
391 | if ($sort_by_ref == 1) { |
392 | $sort_type = 'REFERENCES'; |
76f29d49 |
393 | } else { |
7c612fdd |
394 | $sort_type = 'ORDEREDSUBJECT'; |
395 | } |
a18594b2 |
396 | $query = "THREAD $sort_type ".strtoupper($default_charset)." ALL"; |
6201339c |
397 | $thread_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE); |
2728fa19 |
398 | if (isset($thread_test[0])) { |
9978560b |
399 | for ($i=0,$iCnt=count($thread_test);$i<$iCnt;++$i) { |
76f29d49 |
400 | if (preg_match("/^\* THREAD (.+)$/", $thread_test[$i], $regs)) { |
401 | $thread_list = trim($regs[1]); |
402 | break; |
403 | } |
1c198ef7 |
404 | } |
76f29d49 |
405 | } else { |
406 | $thread_list = ""; |
7c612fdd |
407 | } |
26eca02e |
408 | if (!preg_match("/OK/", $response)) { |
76f29d49 |
409 | $server_sort_array = 'no'; |
410 | return $server_sort_array; |
cdca177a |
411 | } |
474528eb |
412 | if (isset($thread_list)) { |
413 | $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY); |
414 | } |
76f29d49 |
415 | |
7c612fdd |
416 | $char_count = count($thread_temp); |
417 | $counter = 0; |
418 | $thread_new = array(); |
419 | $k = 0; |
420 | $thread_new[0] = ""; |
76f29d49 |
421 | /* |
422 | * parse the thread response into separate threads |
423 | * |
424 | * example: |
425 | * [0] => (540) |
426 | * [1] => (1386) |
427 | * [2] => (1599 759 959 37) |
428 | * [3] => (492 1787) |
429 | * [4] => ((933)(1891)) |
430 | * [5] => (1030 (1497)(845)(1637)) |
431 | */ |
432 | for ($i=0,$iCnt=count($thread_temp);$i<$iCnt;$i++) { |
ffb776c4 |
433 | if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') { |
434 | $thread_new[$k] = $thread_new[$k] . $thread_temp[$i]; |
76f29d49 |
435 | } elseif ($thread_temp[$i] == '(') { |
ffb776c4 |
436 | $thread_new[$k] .= $thread_temp[$i]; |
437 | $counter++; |
76f29d49 |
438 | } elseif ($thread_temp[$i] == ')') { |
439 | if ($counter > 1) { |
440 | $thread_new[$k] .= $thread_temp[$i]; |
441 | $counter = $counter - 1; |
442 | } else { |
443 | $thread_new[$k] .= $thread_temp[$i]; |
444 | $k++; |
445 | $thread_new[$k] = ""; |
446 | $counter = $counter - 1; |
447 | } |
ffb776c4 |
448 | } |
7c612fdd |
449 | } |
9eb0fbd4 |
450 | sqsession_register($thread_new, 'thread_new'); |
7c612fdd |
451 | $thread_new = array_reverse($thread_new); |
76f29d49 |
452 | /* place the threads after each other in one string */ |
7c612fdd |
453 | $thread_list = implode(" ", $thread_new); |
454 | $thread_list = str_replace("(", " ", $thread_list); |
455 | $thread_list = str_replace(")", " ", $thread_list); |
456 | $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY); |
60a3e687 |
457 | $server_sort_array = $thread_list; |
ffb776c4 |
458 | |
459 | $indent_array = get_parent_level ($thread_new); |
460 | sqsession_register($indent_array, 'indent_array'); |
461 | |
9eb0fbd4 |
462 | sqsession_register($server_sort_array, 'server_sort_array'); |
7c612fdd |
463 | return $thread_list; |
464 | } |
465 | |
034fddf9 |
466 | |
7b07404c |
467 | function elapsedTime($start) { |
0fdc2fb6 |
468 | $stop = gettimeofday(); |
469 | $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec']; |
470 | return $timepassed; |
7b07404c |
471 | } |
7c612fdd |
472 | |
ea5fa593 |
473 | // only used in sqimap_get_small_header_list |
a18594b2 |
474 | function parseString($read,&$i) { |
475 | $char = $read{$i}; |
476 | $s = ''; |
477 | if ($char == '"') { |
0fdc2fb6 |
478 | $iPos = ++$i; |
479 | while (true) { |
480 | $iPos = strpos($read,'"',$iPos); |
481 | if (!$iPos) break; |
482 | if ($iPos && $read{$iPos -1} != '\\') { |
483 | $s = substr($read,$i,($iPos-$i)); |
484 | $i = $iPos; |
485 | break; |
486 | } |
487 | $iPos++; |
488 | if ($iPos > strlen($read)) { |
489 | break; |
490 | } |
491 | } |
a18594b2 |
492 | } else if ($char == '{') { |
493 | $lit_cnt = ''; |
494 | ++$i; |
495 | $iPos = strpos($read,'}',$i); |
496 | if ($iPos) { |
0fdc2fb6 |
497 | $lit_cnt = substr($read, $i, $iPos - $i); |
498 | $i += strlen($lit_cnt) + 3; /* skip } + \r + \n */ |
499 | /* Now read the literal */ |
500 | $s = ($lit_cnt ? substr($read,$i,$lit_cnt): ''); |
501 | $i += $lit_cnt; |
502 | /* temp bugfix (SM 1.5 will have a working clean version) |
503 | too much work to implement that version right now */ |
504 | --$i; |
505 | } else { /* should never happen */ |
a18594b2 |
506 | $i += 3; /* } + \r + \n */ |
507 | $s = ''; |
0fdc2fb6 |
508 | } |
a18594b2 |
509 | } else { |
0fdc2fb6 |
510 | return false; |
a18594b2 |
511 | } |
512 | ++$i; |
513 | return $s; |
514 | } |
515 | |
ea5fa593 |
516 | // only used in sqimap_get_small_header_list |
a18594b2 |
517 | function parseArray($read,&$i) { |
518 | $i = strpos($read,'(',$i); |
519 | $i_pos = strpos($read,')',$i); |
520 | $s = substr($read,$i+1,$i_pos - $i -1); |
521 | $a = explode(' ',$s); |
522 | if ($i_pos) { |
523 | $i = $i_pos+1; |
524 | return $a; |
525 | } else { |
526 | return false; |
527 | } |
528 | } |
529 | |
ffb776c4 |
530 | function sqimap_get_small_header_list ($imap_stream, $msg_list, $show_num=false, |
531 | $aHeaderFields = array('Date', 'To', 'Cc', 'From', 'Subject', 'X-Priority', 'Content-Type'), |
8cc8ec79 |
532 | $aFetchItems = array('FLAGS', 'RFC822.SIZE', 'INTERNALDATE')) { |
ffb776c4 |
533 | |
534 | $messages = array(); |
535 | $read_list = array(); |
536 | |
c075fcfe |
537 | $bUidFetch = ! in_array('UID', $aFetchItems, true); |
8cc8ec79 |
538 | |
97f7ddf2 |
539 | /* Get the small headers for each message in $msg_list */ |
ffb776c4 |
540 | if ($show_num != '999999' && $show_num != '*' ) { |
a18594b2 |
541 | $msgs_str = sqimap_message_list_squisher($msg_list); |
ffb776c4 |
542 | /* |
543 | * We need to return the data in the same order as the caller supplied |
544 | * in $msg_list, but IMAP servers are free to return responses in |
545 | * whatever order they wish... So we need to re-sort manually |
546 | */ |
8cc8ec79 |
547 | if ($bUidFetch) { |
548 | for ($i = 0; $i < sizeof($msg_list); $i++) { |
549 | $messages["$msg_list[$i]"] = array(); |
550 | } |
ffb776c4 |
551 | } |
1c198ef7 |
552 | } else { |
a18594b2 |
553 | $msgs_str = '1:*'; |
554 | } |
ffb776c4 |
555 | |
556 | |
a18594b2 |
557 | |
3411d4ec |
558 | /* |
ffb776c4 |
559 | * Create the query |
560 | */ |
cdca177a |
561 | |
ffb776c4 |
562 | $sFetchItems = ''; |
563 | $query = "FETCH $msgs_str ("; |
564 | if (count($aFetchItems)) { |
565 | $sFetchItems = implode(' ',$aFetchItems); |
566 | } |
567 | if (count($aHeaderFields)) { |
568 | $sHeaderFields = implode(' ',$aHeaderFields); |
569 | $sFetchItems .= ' BODY.PEEK[HEADER.FIELDS ('.$sHeaderFields.')]'; |
7b07404c |
570 | } |
ffb776c4 |
571 | $query .= trim($sFetchItems) . ')'; |
8cc8ec79 |
572 | |
573 | $read_list = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, $bUidFetch); |
2d34da11 |
574 | $i = 0; |
1c198ef7 |
575 | |
a18594b2 |
576 | foreach ($read_list as $r) { |
2a9b0fad |
577 | // use unset because we do isset below |
a18594b2 |
578 | $read = implode('',$r); |
579 | |
1c198ef7 |
580 | /* |
a18594b2 |
581 | * #id<space>FETCH<space>( |
582 | */ |
1c198ef7 |
583 | |
a18594b2 |
584 | /* extract the message id */ |
585 | $i_space = strpos($read,' ',2); |
586 | $id = substr($read,2,$i_space-2); |
587 | $fetch = substr($read,$i_space+1,5); |
588 | if (!is_numeric($id) && $fetch !== 'FETCH') { |
8cc8ec79 |
589 | $msg['ERROR'] = $read; // htmlspecialchars should be done just before display. this is backend code |
590 | break; |
a18594b2 |
591 | } |
592 | $i = strpos($read,'(',$i_space+5); |
593 | $read = substr($read,$i+1); |
594 | $i_len = strlen($read); |
595 | $i = 0; |
596 | while ($i < $i_len && $i !== false) { |
597 | /* get argument */ |
598 | $read = trim(substr($read,$i)); |
599 | $i_len = strlen($read); |
600 | $i = strpos($read,' '); |
601 | $arg = substr($read,0,$i); |
602 | ++$i; |
603 | switch ($arg) |
604 | { |
605 | case 'UID': |
606 | $i_pos = strpos($read,' ',$i); |
607 | if (!$i_pos) { |
608 | $i_pos = strpos($read,')',$i); |
cdca177a |
609 | } |
a18594b2 |
610 | if ($i_pos) { |
611 | $unique_id = substr($read,$i,$i_pos-$i); |
612 | $i = $i_pos+1; |
613 | } else { |
614 | break 3; |
cdca177a |
615 | } |
a18594b2 |
616 | break; |
617 | case 'FLAGS': |
618 | $flags = parseArray($read,$i); |
619 | if (!$flags) break 3; |
ffb776c4 |
620 | $aFlags = array(); |
a18594b2 |
621 | foreach ($flags as $flag) { |
622 | $flag = strtolower($flag); |
ffb776c4 |
623 | $aFlags[$flag] = true; |
cdca177a |
624 | } |
ffb776c4 |
625 | $msg['FLAGS'] = $aFlags; |
a18594b2 |
626 | break; |
627 | case 'RFC822.SIZE': |
628 | $i_pos = strpos($read,' ',$i); |
629 | if (!$i_pos) { |
630 | $i_pos = strpos($read,')',$i); |
cdca177a |
631 | } |
a18594b2 |
632 | if ($i_pos) { |
ffb776c4 |
633 | $msg['SIZE'] = substr($read,$i,$i_pos-$i); |
a18594b2 |
634 | $i = $i_pos+1; |
635 | } else { |
636 | break 3; |
637 | } |
1c198ef7 |
638 | |
8cc8ec79 |
639 | break; |
640 | case 'ENVELOPE': |
641 | break; // to be implemented, moving imap code out of the nessages class |
642 | sqimap_parse_address($read,$i,$msg); |
643 | break; // to be implemented, moving imap code out of the nessages class |
644 | case 'BODYSTRUCTURE': |
a18594b2 |
645 | break; |
646 | case 'INTERNALDATE': |
c2e29558 |
647 | $msg['INTERNALDATE'] = str_replace(' ', ' ',parseString($read,$i)); |
a18594b2 |
648 | break; |
649 | case 'BODY.PEEK[HEADER.FIELDS': |
650 | case 'BODY[HEADER.FIELDS': |
651 | $i = strpos($read,'{',$i); |
652 | $header = parseString($read,$i); |
92a52cda |
653 | if ($header === false) break 2; |
2a9b0fad |
654 | /* First we replace all \r\n by \n, and unfold the header */ |
655 | $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $header)); |
a18594b2 |
656 | /* Now we can make a new header array with */ |
657 | /* each element representing a headerline */ |
2a9b0fad |
658 | $hdr = explode("\n" , $hdr); |
2714d4ff |
659 | $aReceived = array(); |
a18594b2 |
660 | foreach ($hdr as $line) { |
661 | $pos = strpos($line, ':'); |
662 | if ($pos > 0) { |
663 | $field = strtolower(substr($line, 0, $pos)); |
664 | if (!strstr($field,' ')) { /* valid field */ |
665 | $value = trim(substr($line, $pos+1)); |
666 | switch($field) |
667 | { |
ffb776c4 |
668 | case 'to': $msg['TO'] = $value; break; |
669 | case 'cc': $msg['CC'] = $value; break; |
670 | case 'from': $msg['FROM'] = $value; break; |
671 | case 'date': |
672 | $msg['DATE'] = str_replace(' ', ' ', $value); |
cdca177a |
673 | break; |
ffb776c4 |
674 | case 'x-priority': $msg['PRIORITY'] = $value; break; |
675 | case 'subject': $msg['SUBJECT'] = $value; break; |
a18594b2 |
676 | case 'content-type': |
677 | $type = $value; |
cdca177a |
678 | if ($pos = strpos($type, ";")) { |
679 | $type = substr($type, 0, $pos); |
680 | } |
681 | $type = explode("/", $type); |
ceacbb9f |
682 | if(!is_array($type) || count($type) < 2) { |
ffb776c4 |
683 | $msg['TYPE0'] = 'text'; |
684 | $msg['TYPE1'] = 'plain'; |
ceacbb9f |
685 | } else { |
686 | $msg['TYPE0'] = strtolower($type[0]); |
687 | $msg['TYPE1'] = strtolower($type[1]); |
cef054e4 |
688 | } |
cdca177a |
689 | break; |
2714d4ff |
690 | case 'received': |
691 | $aReceived[] = $value; |
692 | break; |
a18594b2 |
693 | default: break; |
694 | } |
cdca177a |
695 | } |
696 | } |
697 | } |
2714d4ff |
698 | if (count($aReceived)) { |
699 | $msg['RECEIVED'] = $aReceived; |
700 | } |
a18594b2 |
701 | break; |
702 | default: |
703 | ++$i; |
704 | break; |
cdca177a |
705 | } |
cdca177a |
706 | } |
6201339c |
707 | $msgi ="$unique_id"; |
ffb776c4 |
708 | $msg['ID'] = $unique_id; |
709 | |
710 | $messages[$msgi] = $msg; |
a18594b2 |
711 | ++$msgi; |
97f7ddf2 |
712 | } |
a18594b2 |
713 | array_reverse($messages); |
1c198ef7 |
714 | return $messages; |
97f7ddf2 |
715 | } |
716 | |
8cc8ec79 |
717 | function sqimap_parse_envelope($read, &$i, &$msg) { |
718 | $arg_no = 0; |
719 | $arg_a = array(); |
720 | ++$i; |
721 | for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++$i) { |
722 | $char = strtoupper($read{$i}); |
723 | switch ($char) { |
724 | case '{': |
725 | case '"': |
726 | $arg_a[] = parseString($read,$i); |
727 | ++$arg_no; |
728 | break; |
729 | case 'N': |
730 | /* probably NIL argument */ |
731 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
732 | $arg_a[] = ''; |
733 | ++$arg_no; |
734 | $i += 2; |
735 | } |
736 | break; |
737 | case '(': |
738 | /* Address structure (with group support) |
739 | * Note: Group support is useless on SMTP connections |
740 | * because the protocol doesn't support it |
741 | */ |
742 | $addr_a = array(); |
743 | $group = ''; |
744 | $a=0; |
745 | for (; $i < $cnt && $read{$i} != ')'; ++$i) { |
746 | if ($read{$i} == '(') { |
747 | $addr = sqimap_parse_address($read, $i); |
748 | if (($addr[3] == '') && ($addr[2] != '')) { |
749 | /* start of group */ |
750 | $group = $addr[2]; |
751 | $group_addr = $addr; |
752 | $j = $a; |
753 | } else if ($group && ($addr[3] == '') && ($addr[2] == '')) { |
754 | /* end group */ |
755 | if ($a == ($j+1)) { /* no group members */ |
756 | $group_addr[4] = $group; |
757 | $group_addr[2] = ''; |
758 | $group_addr[0] = "$group: Undisclosed recipients;"; |
759 | $addr_a[] = $group_addr; |
760 | $group =''; |
761 | } |
762 | } else { |
763 | $addr[4] = $group; |
764 | $addr_a[] = $addr; |
765 | } |
766 | ++$a; |
767 | } |
768 | } |
769 | $arg_a[] = $addr_a; |
770 | break; |
771 | default: break; |
772 | } |
773 | } |
774 | |
775 | if (count($arg_a) > 9) { |
776 | $d = strtr($arg_a[0], array(' ' => ' ')); |
777 | $d = explode(' ', $d); |
778 | if (!$arg_a[1]) $arg_1[1] = ''; |
779 | $msg['DATE'] = $d; /* argument 1: date */ |
780 | $msg['SUBJECT'] = $arg_a[1]; /* argument 2: subject */ |
781 | $msg['FROM'] = is_array($arg_a[2]) ? $arg_a[2][0] : ''; /* argument 3: from */ |
782 | $msg['SENDER'] = is_array($arg_a[3]) ? $arg_a[3][0] : ''; /* argument 4: sender */ |
783 | $msg['REPLY-TO'] = is_array($arg_a[4]) ? $arg_a[4][0] : ''; /* argument 5: reply-to */ |
784 | $msg['TO'] = $arg_a[5]; /* argument 6: to */ |
785 | $msg['CC'] = $arg_a[6]; /* argument 7: cc */ |
786 | $msg['BCC'] = $arg_a[7]; /* argument 8: bcc */ |
787 | $msg['IN-REPLY-TO'] = $arg_a[8]; /* argument 9: in-reply-to */ |
788 | $msg['MESSAGE-ID'] = $arg_a[9]; /* argument 10: message-id */ |
789 | } |
790 | } |
791 | |
792 | function sqimap_parse_address($read, &$i) { |
793 | $arg_a = array(); |
794 | for (; $read{$i} != ')'; ++$i) { |
795 | $char = strtoupper($read{$i}); |
796 | switch ($char) { |
797 | case '{': |
798 | case '"': $arg_a[] = parseString($read,$i); break; |
799 | case 'n': |
800 | case 'N': |
801 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
802 | $arg_a[] = ''; |
803 | $i += 2; |
804 | } |
805 | break; |
806 | default: break; |
807 | } |
808 | } |
809 | |
810 | if (count($arg_a) == 4) { |
811 | return $arg_a; |
812 | |
813 | // $adr = new AddressStructure(); |
814 | // $adr->personal = $arg_a[0]; |
815 | // $adr->adl = $arg_a[1]; |
816 | // $adr->mailbox = $arg_a[2]; |
817 | // $adr->host = $arg_a[3]; |
818 | } else { |
819 | $adr = ''; |
820 | } |
821 | return $adr; |
822 | } |
823 | |
48af4b64 |
824 | /** |
0fdc2fb6 |
825 | * Returns a message array with all the information about a message. |
826 | * See the documentation folder for more information about this array. |
827 | */ |
97f7ddf2 |
828 | function sqimap_get_message ($imap_stream, $id, $mailbox) { |
461eda6c |
829 | // typecast to int to prohibit 1:* msgs sets |
830 | $id = (int) $id; |
2d34da11 |
831 | $flags = array(); |
6201339c |
832 | $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, TRUE); |
114f2a24 |
833 | if ($read) { |
b69a13a4 |
834 | if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) { |
835 | if (trim($regs[1])) { |
836 | $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY'); |
837 | } |
838 | } |
114f2a24 |
839 | } else { |
b69a13a4 |
840 | /* the message was not found, maybe the mailbox was modified? */ |
841 | global $sort, $startMessage, $color; |
842 | |
843 | $errmessage = _("The server couldn't find the message you requested.") . |
0fdc2fb6 |
844 | '<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 |
845 | /* this will include a link back to the message list */ |
461eda6c |
846 | error_message($errmessage, $mailbox, $sort, (int) $startMessage, $color); |
b69a13a4 |
847 | exit; |
1c198ef7 |
848 | } |
2d34da11 |
849 | $bodystructure = implode('',$read); |
850 | $msg = mime_structure($bodystructure,$flags); |
6201339c |
851 | $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, TRUE); |
19d470aa |
852 | $rfc822_header = new Rfc822Header(); |
767ace1f |
853 | $rfc822_header->parseHeader($read); |
854 | $msg->rfc822_header = $rfc822_header; |
2d34da11 |
855 | return $msg; |
97f7ddf2 |
856 | } |
857 | |
052e0c26 |
858 | ?> |