59177427 |
1 | <?php |
7350889b |
2 | |
35586184 |
3 | /** |
258d61ed |
4 | * imap_messages.php |
5 | * |
6c84ba1e |
6 | * Copyright (c) 1999-2005 The SquirrelMail Project Team |
258d61ed |
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 | |
97f7ddf2 |
17 | |
7c3e0802 |
18 | /** |
8315c94c |
19 | * Copy a set of messages ($id) to another mailbox ($mailbox) |
258d61ed |
20 | * @param int $imap_stream The resource ID for the IMAP socket |
21 | * @param string $id The list of messages to copy |
22 | * @param string $mailbox The destination to copy to |
4e6e5d2d |
23 | * @param bool $handle_errors Show error messages in case of a NO, BAD or BYE response |
24 | * @return bool If the copy completed without errors |
258d61ed |
25 | */ |
4e6e5d2d |
26 | function sqimap_msgs_list_copy($imap_stream, $id, $mailbox, $handle_errors = true) { |
1c198ef7 |
27 | $msgs_id = sqimap_message_list_squisher($id); |
4e6e5d2d |
28 | $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($mailbox), $handle_errors, $response, $message, TRUE); |
324ac3c5 |
29 | if ($response == 'OK') { |
30 | return true; |
31 | } else { |
32 | return false; |
33 | } |
7c3e0802 |
34 | } |
35 | |
8315c94c |
36 | |
7c3e0802 |
37 | /** |
8315c94c |
38 | * Move a set of messages ($id) to another mailbox. Deletes the originals. |
258d61ed |
39 | * @param int $imap_stream The resource ID for the IMAP socket |
40 | * @param string $id The list of messages to move |
41 | * @param string $mailbox The destination to move to |
4e6e5d2d |
42 | * @param bool $handle_errors Show error messages in case of a NO, BAD or BYE response |
43 | * @return bool If the move completed without errors |
258d61ed |
44 | */ |
4e6e5d2d |
45 | function sqimap_msgs_list_move($imap_stream, $id, $mailbox, $handle_errors = true) { |
7c3e0802 |
46 | $msgs_id = sqimap_message_list_squisher($id); |
4e6e5d2d |
47 | if (sqimap_msgs_list_copy ($imap_stream, $id, $mailbox, $handle_errors)) { |
324ac3c5 |
48 | return sqimap_toggle_flag($imap_stream, $id, '\\Deleted', true, true); |
49 | } else { |
50 | return false; |
51 | } |
034fddf9 |
52 | } |
53 | |
54 | |
d6c32258 |
55 | /** |
258d61ed |
56 | * Deletes a message and move it to trash or expunge the mailbox |
57 | * @param resource imap connection |
58 | * @param string $mailbox mailbox, used for checking if it concerns the trash_folder |
59 | * @param array $id list with uid's |
60 | * @param bool $bypass_trash skip copy to trash |
61 | * @return array $aMessageList array with messages containing the new flags and UID @see parseFetch |
62 | */ |
8315c94c |
63 | function sqimap_msgs_list_delete($imap_stream, $mailbox, $id, $bypass_trash=false) { |
324ac3c5 |
64 | // FIX ME, remove globals by introducing an associative array with properties |
65 | // as 4th argument as replacement for the bypass_trash var |
6201339c |
66 | global $move_to_trash, $trash_folder; |
324ac3c5 |
67 | $bRes = true; |
abafb676 |
68 | if (($move_to_trash == true) && ($bypass_trash != true) && |
69 | (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder)) ) { |
324ac3c5 |
70 | $bRes = sqimap_msgs_list_copy ($imap_stream, $id, $trash_folder); |
71 | } |
72 | if ($bRes) { |
73 | return sqimap_toggle_flag($imap_stream, $id, '\\Deleted', true, true); |
74 | } else { |
75 | return false; |
034fddf9 |
76 | } |
034fddf9 |
77 | } |
78 | |
79 | |
258d61ed |
80 | /** |
81 | * Set a flag on the provided uid list |
82 | * @param resource imap connection |
83 | * @param array $id list with uid's |
84 | * @param string $flag Flags to set/unset flags can be i.e.'\Seen', '\Answered', '\Seen \Answered' |
85 | * @param bool $set add (true) or remove (false) the provided flag |
86 | * @param bool $handle_errors Show error messages in case of a NO, BAD or BYE response |
87 | * @return array $aMessageList array with messages containing the new flags and UID @see parseFetch |
88 | */ |
034fddf9 |
89 | function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) { |
034fddf9 |
90 | $msgs_id = sqimap_message_list_squisher($id); |
91 | $set_string = ($set ? '+' : '-'); |
324ac3c5 |
92 | $aResponse = sqimap_run_command_list($imap_stream, "STORE $msgs_id ".$set_string."FLAGS ($flag)", $handle_errors, $response, $message, TRUE); |
93 | // parse the fetch response |
94 | return parseFetch($aResponse); |
034fddf9 |
95 | } |
96 | |
8315c94c |
97 | |
48af4b64 |
98 | /** |
258d61ed |
99 | * Sort the message list and crunch to be as small as possible |
100 | * (overflow could happen, so make it small if possible) |
101 | */ |
97f7ddf2 |
102 | function sqimap_message_list_squisher($messages_array) { |
103 | if( !is_array( $messages_array ) ) { |
fe70ae27 |
104 | return $messages_array; |
97f7ddf2 |
105 | } |
2d34da11 |
106 | |
97f7ddf2 |
107 | sort($messages_array, SORT_NUMERIC); |
108 | $msgs_str = ''; |
109 | while ($messages_array) { |
110 | $start = array_shift($messages_array); |
111 | $end = $start; |
112 | while (isset($messages_array[0]) && $messages_array[0] == $end + 1) { |
113 | $end = array_shift($messages_array); |
114 | } |
115 | if ($msgs_str != '') { |
116 | $msgs_str .= ','; |
117 | } |
118 | $msgs_str .= $start; |
119 | if ($start != $end) { |
120 | $msgs_str .= ':' . $end; |
121 | } |
122 | } |
97f7ddf2 |
123 | return $msgs_str; |
3411d4ec |
124 | } |
97f7ddf2 |
125 | |
8315c94c |
126 | |
48af4b64 |
127 | /** |
8315c94c |
128 | * Retrieves an array with a sorted uid list. Sorting is done on the imap server |
129 | * @link http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-17.txt |
130 | * @param resource $imap_stream IMAP socket connection |
131 | * @param string $sSortField Field to sort on |
132 | * @param bool $reverse Reverse order search |
133 | * @return array $id sorted uid list |
134 | */ |
135 | function sqimap_get_sort_order($imap_stream, $sSortField, $reverse, $search='ALL') { |
ce68b76b |
136 | global $default_charset; |
2d34da11 |
137 | |
ffb776c4 |
138 | if ($sSortField) { |
139 | if ($reverse) { |
140 | $sSortField = 'REVERSE '.$sSortField; |
141 | } |
324ac3c5 |
142 | $query = "SORT ($sSortField) ".strtoupper($default_charset)." $search"; |
143 | // FIX ME sqimap_run_command should return the parsed data accessible by $aDATA['SORT'] |
144 | $aData = sqimap_run_command ($imap_stream, $query, false, $response, $message, TRUE); |
145 | /* fallback to default charset */ |
146 | if ($response == 'NO' && strpos($message,'[BADCHARSET]') !== false) { |
147 | $query = "SORT ($sSortField) US-ASCII $search"; |
148 | $aData = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE); |
cdca177a |
149 | } |
0fdc2fb6 |
150 | } |
324ac3c5 |
151 | |
152 | if ($response == 'OK') { |
153 | return parseUidList($aData,'SORT'); |
ffb776c4 |
154 | } else { |
324ac3c5 |
155 | return false; |
156 | } |
157 | } |
158 | |
258d61ed |
159 | |
160 | /** |
8315c94c |
161 | * Parses a UID list returned on a SORT or SEARCH request |
162 | * @param array $aData imap response |
163 | * @param string $sCommand issued imap command (SEARCH or SORT) |
164 | * @return array $aUid uid list |
165 | */ |
324ac3c5 |
166 | function parseUidList($aData,$sCommand) { |
167 | $aUid = array(); |
168 | if (isset($aData) && count($aData)) { |
169 | for ($i=0,$iCnt=count($aData);$i<$iCnt;++$i) { |
170 | if (preg_match("/^\* $sCommand (.+)$/", $aData[$i], $aMatch)) { |
171 | $aUid += preg_split("/ /", trim($aMatch[1])); |
172 | } |
173 | } |
cdca177a |
174 | } |
324ac3c5 |
175 | return array_unique($aUid); |
aa0da530 |
176 | } |
2d34da11 |
177 | |
26b22b20 |
178 | /** |
258d61ed |
179 | * Retrieves an array with a sorted uid list. Sorting is done by SquirrelMail |
180 | * |
181 | * @param resource $imap_stream IMAP socket connection |
182 | * @param string $sSortField Field to sort on |
183 | * @param bool $reverse Reverse order search |
184 | * @param array $aUid limit the search to the provided array with uid's default sqimap_get_small_headers uses 1:* |
185 | * @return array $aUid sorted uid list |
186 | */ |
8315c94c |
187 | function get_squirrel_sort($imap_stream, $sSortField, $reverse = false, $aUid = NULL) { |
e0e30169 |
188 | if ($sSortField != 'RFC822.SIZE' && $sSortField != 'INTERNALDATE') { |
324ac3c5 |
189 | $msgs = sqimap_get_small_header_list($imap_stream, $aUid, |
e0e30169 |
190 | array($sSortField), array()); |
ffb776c4 |
191 | } else { |
324ac3c5 |
192 | $msgs = sqimap_get_small_header_list($imap_stream, $aUid, |
e0e30169 |
193 | array(), array($sSortField)); |
ffb776c4 |
194 | } |
c2e29558 |
195 | $aUid = array(); |
76f29d49 |
196 | $walk = false; |
ffb776c4 |
197 | switch ($sSortField) { |
76f29d49 |
198 | // natcasesort section |
ffb776c4 |
199 | case 'FROM': |
ffb776c4 |
200 | case 'TO': |
76f29d49 |
201 | case 'CC': |
202 | if(!$walk) { |
203 | array_walk($msgs, create_function('&$v,&$k,$f', |
204 | '$v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
544ab9e2 |
205 | $addr = reset(parseRFC822Address($v[$f],1)); |
206 | $sPersonal = (isset($addr[SQM_ADDR_PERSONAL]) && $addr[SQM_ADDR_PERSONAL]) ? |
207 | $addr[SQM_ADDR_PERSONAL] : ""; |
208 | $sEmail = ($addr[SQM_ADDR_HOST]) ? |
204f909c |
209 | $addr[SQM_ADDR_MAILBOX] . "@".$addr[SQM_ADDR_HOST] : |
544ab9e2 |
210 | $addr[SQM_ADDR_HOST]; |
211 | $v[$f] = ($sPersonal) ? decodeHeader($sPersonal):$sEmail;'),$sSortField); |
76f29d49 |
212 | $walk = true; |
cdca177a |
213 | } |
76f29d49 |
214 | // nobreak |
ffb776c4 |
215 | case 'SUBJECT': |
76f29d49 |
216 | if(!$walk) { |
217 | array_walk($msgs, create_function('&$v,&$k,$f', |
218 | '$v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
219 | $v[$f] = strtolower(decodeHeader(trim($v[$f]))); |
220 | $v[$f] = (preg_match("/^(vedr|sv|re|aw|\[\w\]):\s*(.*)$/si", $v[$f], $matches)) ? |
221 | $matches[2] : $v[$f];'),$sSortField); |
222 | $walk = true; |
223 | } |
ffb776c4 |
224 | foreach ($msgs as $item) { |
324ac3c5 |
225 | $aUid[$item['UID']] = $item[$sSortField]; |
ffb776c4 |
226 | } |
76f29d49 |
227 | natcasesort($aUid); |
228 | $aUid = array_keys($aUid); |
ffb776c4 |
229 | if ($reverse) { |
e432a21c |
230 | $aUid = array_reverse($aUid); |
ffb776c4 |
231 | } |
232 | break; |
76f29d49 |
233 | // \natcasesort section |
234 | // sort_numeric section |
ffb776c4 |
235 | case 'DATE': |
76f29d49 |
236 | case 'INTERNALDATE': |
237 | if(!$walk) { |
238 | array_walk($msgs, create_function('&$v,$k,$f', |
239 | '$v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
240 | $v[$f] = getTimeStamp(explode(" ",$v[$f]));'),$sSortField); |
241 | $walk = true; |
ffb776c4 |
242 | } |
76f29d49 |
243 | // nobreak; |
ffb776c4 |
244 | case 'RFC822.SIZE': |
c2e29558 |
245 | if(!$walk) { |
246 | // redefine $sSortField to maintain the same namespace between |
598294a7 |
247 | // server-side sorting and SquirrelMail sorting |
c2e29558 |
248 | $sSortField = 'SIZE'; |
249 | } |
ffb776c4 |
250 | foreach ($msgs as $item) { |
324ac3c5 |
251 | $aUid[$item['UID']] = (isset($item[$sSortField])) ? $item[$sSortField] : 0; |
ffb776c4 |
252 | } |
253 | if ($reverse) { |
76f29d49 |
254 | arsort($aUid,SORT_NUMERIC); |
ffb776c4 |
255 | } else { |
76f29d49 |
256 | asort($aUid, SORT_NUMERIC); |
ffb776c4 |
257 | } |
76f29d49 |
258 | $aUid = array_keys($aUid); |
ffb776c4 |
259 | break; |
76f29d49 |
260 | // \sort_numeric section |
ffb776c4 |
261 | case 'UID': |
76f29d49 |
262 | $aUid = array_reverse($msgs); |
ffb776c4 |
263 | break; |
6201339c |
264 | } |
76f29d49 |
265 | return $aUid; |
cdca177a |
266 | } |
267 | |
8315c94c |
268 | |
48af4b64 |
269 | /** |
258d61ed |
270 | * Returns an indent array for printMessageinfo() |
271 | * This represents the amount of indent needed (value), |
272 | * for this message number (key) |
273 | */ |
76f29d49 |
274 | |
275 | /* |
276 | * Notes for future work: |
277 | * indent_array should contain: indent_level, parent and flags, |
278 | * sibling notes .. |
279 | * To achieve that we need to define the following flags: |
280 | * 0: hasnochildren |
281 | * 1: haschildren |
282 | * 2: is first |
283 | * 4: is last |
284 | * a node has sibling nodes if it's not the last node |
285 | * a node has no sibling nodes if it's the last node |
286 | * By using binary comparations we can store the flag in one var |
287 | * |
288 | * example: |
289 | * -1 par = 0, level = 0, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast) |
290 | * \-2 par = 1, level = 1, flag = 0 + 2 = 2 (hasnochildren, isfirst) |
291 | * |-3 par = 1, level = 1, flag = 1 + 4 = 5 (haschildren, islast) |
292 | * \-4 par = 3, level = 2, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast) |
293 | * \-5 par = 4, level = 3, flag = 0 + 2 + 4 = 6 (hasnochildren, isfirst, islast) |
294 | */ |
8315c94c |
295 | function get_parent_level($thread_new) { |
48af4b64 |
296 | $parent = ''; |
297 | $child = ''; |
298 | $cutoff = 0; |
cdca177a |
299 | |
76f29d49 |
300 | /* |
301 | * loop through the threads and take unwanted characters out |
302 | * of the thread string then chop it up |
303 | */ |
7c612fdd |
304 | for ($i=0;$i<count($thread_new);$i++) { |
305 | $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]); |
306 | $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]); |
307 | $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY); |
1c198ef7 |
308 | } |
7c612fdd |
309 | $indent_array = array(); |
76f29d49 |
310 | if (!$thread_new) { |
311 | $thread_new = array(); |
312 | } |
288bbce0 |
313 | /* looping through the parts of one message thread */ |
cdca177a |
314 | |
7c612fdd |
315 | for ($i=0;$i<count($thread_new);$i++) { |
76f29d49 |
316 | /* first grab the parent, it does not indent */ |
cdca177a |
317 | |
7c612fdd |
318 | if (isset($thread_new[$i][0])) { |
288bbce0 |
319 | if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) { |
320 | $parent = $regs[1]; |
321 | } |
7c612fdd |
322 | } |
323 | $indent_array[$parent] = 0; |
288bbce0 |
324 | |
76f29d49 |
325 | /* |
326 | * now the children, checking each thread portion for |
327 | * ),(, and space, adjusting the level and space values |
328 | * to get the indent level |
329 | */ |
288bbce0 |
330 | $level = 0; |
474528eb |
331 | $spaces = array(); |
332 | $spaces_total = 0; |
7c612fdd |
333 | $indent = 0; |
288bbce0 |
334 | $fake = FALSE; |
76f29d49 |
335 | for ($k=1,$iCnt=count($thread_new[$i])-1;$k<$iCnt;++$k) { |
7c612fdd |
336 | $chars = count_chars($thread_new[$i][$k], 1); |
288bbce0 |
337 | if (isset($chars['40'])) { /* testing for ( */ |
76f29d49 |
338 | $level += $chars['40']; |
7c612fdd |
339 | } |
288bbce0 |
340 | if (isset($chars['41'])) { /* testing for ) */ |
76f29d49 |
341 | $level -= $chars['41']; |
474528eb |
342 | $spaces[$level] = 0; |
288bbce0 |
343 | /* if we were faking lets stop, this portion |
76f29d49 |
344 | * of the thread is over |
345 | */ |
288bbce0 |
346 | if ($level == $cutoff) { |
347 | $fake = FALSE; |
7c612fdd |
348 | } |
349 | } |
288bbce0 |
350 | if (isset($chars['32'])) { /* testing for space */ |
474528eb |
351 | if (!isset($spaces[$level])) { |
352 | $spaces[$level] = 0; |
353 | } |
76f29d49 |
354 | $spaces[$level] += $chars['32']; |
474528eb |
355 | } |
356 | for ($x=0;$x<=$level;$x++) { |
357 | if (isset($spaces[$x])) { |
76f29d49 |
358 | $spaces_total += $spaces[$x]; |
474528eb |
359 | } |
288bbce0 |
360 | } |
474528eb |
361 | $indent = $level + $spaces_total; |
288bbce0 |
362 | /* must have run into a message that broke the thread |
76f29d49 |
363 | * so we are adjusting for that portion |
364 | */ |
288bbce0 |
365 | if ($fake == TRUE) { |
366 | $indent = $indent +1; |
7c612fdd |
367 | } |
368 | if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) { |
369 | $child = $regs[1]; |
370 | } |
288bbce0 |
371 | /* the thread must be broken if $indent == 0 |
76f29d49 |
372 | * so indent the message once and start faking it |
373 | */ |
288bbce0 |
374 | if ($indent == 0) { |
375 | $indent = 1; |
376 | $fake = TRUE; |
377 | $cutoff = $level; |
378 | } |
379 | /* dont need abs but if indent was negative |
76f29d49 |
380 | * errors would occur |
381 | */ |
382 | $indent_array[$child] = ($indent < 0) ? 0 : $indent; |
474528eb |
383 | $spaces_total = 0; |
cdca177a |
384 | } |
7c612fdd |
385 | } |
386 | return $indent_array; |
387 | } |
388 | |
389 | |
48af4b64 |
390 | /** |
258d61ed |
391 | * Returns an array with each element as a string representing one |
392 | * message-thread as returned by the IMAP server. |
393 | * @link http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-13.txt |
394 | */ |
8315c94c |
395 | function get_thread_sort($imap_stream, $search='ALL') { |
ffb776c4 |
396 | global $thread_new, $sort_by_ref, $default_charset, $server_sort_array, $indent_array; |
324ac3c5 |
397 | |
7c612fdd |
398 | $thread_temp = array (); |
399 | if ($sort_by_ref == 1) { |
400 | $sort_type = 'REFERENCES'; |
76f29d49 |
401 | } else { |
7c612fdd |
402 | $sort_type = 'ORDEREDSUBJECT'; |
403 | } |
324ac3c5 |
404 | $query = "THREAD $sort_type ".strtoupper($default_charset)." $search"; |
405 | |
406 | $thread_test = sqimap_run_command ($imap_stream, $query, false, $response, $message, TRUE); |
407 | /* fallback to default charset */ |
408 | if ($response == 'NO' && strpos($message,'[BADCHARSET]') !== false) { |
409 | $query = "THREAD $sort_type US-ASCII $search"; |
410 | $thread_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE); |
411 | } |
2728fa19 |
412 | if (isset($thread_test[0])) { |
9978560b |
413 | for ($i=0,$iCnt=count($thread_test);$i<$iCnt;++$i) { |
76f29d49 |
414 | if (preg_match("/^\* THREAD (.+)$/", $thread_test[$i], $regs)) { |
415 | $thread_list = trim($regs[1]); |
416 | break; |
417 | } |
1c198ef7 |
418 | } |
76f29d49 |
419 | } else { |
420 | $thread_list = ""; |
7c612fdd |
421 | } |
26eca02e |
422 | if (!preg_match("/OK/", $response)) { |
76f29d49 |
423 | $server_sort_array = 'no'; |
424 | return $server_sort_array; |
cdca177a |
425 | } |
474528eb |
426 | if (isset($thread_list)) { |
427 | $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY); |
428 | } |
76f29d49 |
429 | |
7c612fdd |
430 | $counter = 0; |
431 | $thread_new = array(); |
432 | $k = 0; |
433 | $thread_new[0] = ""; |
76f29d49 |
434 | /* |
435 | * parse the thread response into separate threads |
436 | * |
437 | * example: |
438 | * [0] => (540) |
439 | * [1] => (1386) |
440 | * [2] => (1599 759 959 37) |
441 | * [3] => (492 1787) |
442 | * [4] => ((933)(1891)) |
443 | * [5] => (1030 (1497)(845)(1637)) |
444 | */ |
445 | for ($i=0,$iCnt=count($thread_temp);$i<$iCnt;$i++) { |
ffb776c4 |
446 | if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') { |
8315c94c |
447 | $thread_new[$k] = $thread_new[$k] . $thread_temp[$i]; |
76f29d49 |
448 | } elseif ($thread_temp[$i] == '(') { |
8315c94c |
449 | $thread_new[$k] .= $thread_temp[$i]; |
450 | $counter++; |
76f29d49 |
451 | } elseif ($thread_temp[$i] == ')') { |
452 | if ($counter > 1) { |
453 | $thread_new[$k] .= $thread_temp[$i]; |
454 | $counter = $counter - 1; |
455 | } else { |
456 | $thread_new[$k] .= $thread_temp[$i]; |
457 | $k++; |
458 | $thread_new[$k] = ""; |
459 | $counter = $counter - 1; |
460 | } |
ffb776c4 |
461 | } |
7c612fdd |
462 | } |
324ac3c5 |
463 | |
7c612fdd |
464 | $thread_new = array_reverse($thread_new); |
76f29d49 |
465 | /* place the threads after each other in one string */ |
7c612fdd |
466 | $thread_list = implode(" ", $thread_new); |
467 | $thread_list = str_replace("(", " ", $thread_list); |
468 | $thread_list = str_replace(")", " ", $thread_list); |
469 | $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY); |
60a3e687 |
470 | $server_sort_array = $thread_list; |
ffb776c4 |
471 | |
472 | $indent_array = get_parent_level ($thread_new); |
324ac3c5 |
473 | return array($thread_list,$indent_array); |
7c612fdd |
474 | } |
475 | |
034fddf9 |
476 | |
7b07404c |
477 | function elapsedTime($start) { |
0fdc2fb6 |
478 | $stop = gettimeofday(); |
479 | $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec']; |
480 | return $timepassed; |
7b07404c |
481 | } |
7c612fdd |
482 | |
8315c94c |
483 | |
9b935fea |
484 | /** |
485 | * Normalise the different Priority headers into a uniform value, |
486 | * namely that of the X-Priority header (1, 3, 5). Supports: |
487 | * Prioirty, X-Priority, Importance. |
488 | * X-MS-Mail-Priority is not parsed because it always coincides |
489 | * with one of the other headers. |
490 | * |
491 | * DUPLICATE CODE ALERT: |
492 | * NOTE: this is actually a duplicate from the function in |
493 | * class/mime/Rfc822Header.php. |
494 | */ |
bddb3448 |
495 | function parsePriority($value) { |
496 | $value = strtolower(array_shift(split('/\w/',trim($value)))); |
497 | if ( is_numeric($value) ) { |
498 | return $value; |
499 | } |
500 | if ( $value == 'urgent' || $value == 'high' ) { |
501 | return 1; |
502 | } elseif ( $value == 'non-urgent' || $value == 'low' ) { |
503 | return 5; |
504 | } |
505 | return 3; |
506 | } |
507 | |
258d61ed |
508 | /** |
509 | * Parses a string in an imap response. String starts with " or { which means it |
510 | * can handle double quoted strings and literal strings |
511 | * |
512 | * @param string $read imap response |
513 | * @param integer $i (reference) offset in string |
514 | * @return string $s parsed string without the double quotes or literal count |
515 | */ |
a18594b2 |
516 | function parseString($read,&$i) { |
517 | $char = $read{$i}; |
518 | $s = ''; |
519 | if ($char == '"') { |
0fdc2fb6 |
520 | $iPos = ++$i; |
521 | while (true) { |
522 | $iPos = strpos($read,'"',$iPos); |
523 | if (!$iPos) break; |
8315c94c |
524 | if ($iPos && $read{$iPos -1} != '\\') { |
525 | $s = substr($read,$i,($iPos-$i)); |
526 | $i = $iPos; |
527 | break; |
528 | } |
529 | $iPos++; |
530 | if ($iPos > strlen($read)) { |
531 | break; |
532 | } |
0fdc2fb6 |
533 | } |
a18594b2 |
534 | } else if ($char == '{') { |
535 | $lit_cnt = ''; |
536 | ++$i; |
537 | $iPos = strpos($read,'}',$i); |
538 | if ($iPos) { |
8315c94c |
539 | $lit_cnt = substr($read, $i, $iPos - $i); |
540 | $i += strlen($lit_cnt) + 3; /* skip } + \r + \n */ |
541 | /* Now read the literal */ |
542 | $s = ($lit_cnt ? substr($read,$i,$lit_cnt): ''); |
543 | $i += $lit_cnt; |
544 | /* temp bugfix (SM 1.5 will have a working clean version) |
545 | too much work to implement that version right now */ |
546 | --$i; |
0fdc2fb6 |
547 | } else { /* should never happen */ |
a18594b2 |
548 | $i += 3; /* } + \r + \n */ |
549 | $s = ''; |
0fdc2fb6 |
550 | } |
a18594b2 |
551 | } else { |
0fdc2fb6 |
552 | return false; |
a18594b2 |
553 | } |
554 | ++$i; |
555 | return $s; |
556 | } |
557 | |
8315c94c |
558 | |
258d61ed |
559 | /** |
560 | * Parses a string containing an array from an imap response. String starts with ( and end with ) |
561 | * |
562 | * @param string $read imap response |
563 | * @param integer $i (reference) offset in string |
564 | * @return array $a |
565 | */ |
a18594b2 |
566 | function parseArray($read,&$i) { |
567 | $i = strpos($read,'(',$i); |
568 | $i_pos = strpos($read,')',$i); |
569 | $s = substr($read,$i+1,$i_pos - $i -1); |
570 | $a = explode(' ',$s); |
571 | if ($i_pos) { |
572 | $i = $i_pos+1; |
573 | return $a; |
574 | } else { |
575 | return false; |
576 | } |
577 | } |
8315c94c |
578 | |
579 | |
258d61ed |
580 | /** |
581 | * Retrieves a list with headers, flags, size or internaldate from the imap server |
582 | * @param resource $imap_stream imap connection |
583 | * @param array $msg_list array with id's to create a msgs set from |
584 | * @param array $aHeaderFields requested header fields |
585 | * @param array $aFetchItems requested other fetch items like FLAGS, RFC822.SIZE |
586 | * @return array $aMessages associative array with messages. Key is the UID, value is an associative array |
587 | */ |
8315c94c |
588 | function sqimap_get_small_header_list($imap_stream, $msg_list, |
bddb3448 |
589 | $aHeaderFields = array('Date', 'To', 'Cc', 'From', 'Subject', 'X-Priority', 'Importance', 'Priority', 'Content-Type'), |
8cc8ec79 |
590 | $aFetchItems = array('FLAGS', 'RFC822.SIZE', 'INTERNALDATE')) { |
ffb776c4 |
591 | |
324ac3c5 |
592 | $aMessageList = array(); |
ffb776c4 |
593 | |
c075fcfe |
594 | $bUidFetch = ! in_array('UID', $aFetchItems, true); |
8cc8ec79 |
595 | |
97f7ddf2 |
596 | /* Get the small headers for each message in $msg_list */ |
258d61ed |
597 | if ($msg_list !== NULL) { |
a18594b2 |
598 | $msgs_str = sqimap_message_list_squisher($msg_list); |
ffb776c4 |
599 | /* |
600 | * We need to return the data in the same order as the caller supplied |
601 | * in $msg_list, but IMAP servers are free to return responses in |
602 | * whatever order they wish... So we need to re-sort manually |
603 | */ |
8cc8ec79 |
604 | if ($bUidFetch) { |
605 | for ($i = 0; $i < sizeof($msg_list); $i++) { |
324ac3c5 |
606 | $aMessageList["$msg_list[$i]"] = array(); |
8cc8ec79 |
607 | } |
ffb776c4 |
608 | } |
1c198ef7 |
609 | } else { |
a18594b2 |
610 | $msgs_str = '1:*'; |
611 | } |
ffb776c4 |
612 | |
3411d4ec |
613 | /* |
ffb776c4 |
614 | * Create the query |
615 | */ |
cdca177a |
616 | |
ffb776c4 |
617 | $sFetchItems = ''; |
618 | $query = "FETCH $msgs_str ("; |
619 | if (count($aFetchItems)) { |
620 | $sFetchItems = implode(' ',$aFetchItems); |
621 | } |
622 | if (count($aHeaderFields)) { |
623 | $sHeaderFields = implode(' ',$aHeaderFields); |
624 | $sFetchItems .= ' BODY.PEEK[HEADER.FIELDS ('.$sHeaderFields.')]'; |
7b07404c |
625 | } |
ffb776c4 |
626 | $query .= trim($sFetchItems) . ')'; |
324ac3c5 |
627 | $aResponse = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, $bUidFetch); |
628 | $aMessages = parseFetch($aResponse,$aMessageList); |
629 | array_reverse($aMessages); |
630 | return $aMessages; |
631 | } |
8cc8ec79 |
632 | |
8315c94c |
633 | |
258d61ed |
634 | /** |
635 | * Parses a fetch response, currently it can hande FLAGS, HEADERS, RFC822.SIZE, INTERNALDATE and UID |
636 | * @param array $aResponse Imap response |
637 | * @param array $aMessageList Placeholder array for results. The keys of the |
638 | * placeholder array should be the UID so we can reconstruct the order. |
639 | * @return array $aMessageList associative array with messages. Key is the UID, value is an associative array |
640 | * @author Marc Groot Koerkamp |
641 | */ |
324ac3c5 |
642 | function parseFetch($aResponse,$aMessageList = array()) { |
643 | foreach ($aResponse as $r) { |
d74abf73 |
644 | $msg = array(); |
2a9b0fad |
645 | // use unset because we do isset below |
a18594b2 |
646 | $read = implode('',$r); |
647 | |
1c198ef7 |
648 | /* |
8315c94c |
649 | * #id<space>FETCH<space>( |
650 | */ |
1c198ef7 |
651 | |
a18594b2 |
652 | /* extract the message id */ |
653 | $i_space = strpos($read,' ',2); |
654 | $id = substr($read,2,$i_space-2); |
324ac3c5 |
655 | $msg['ID'] = $id; |
a18594b2 |
656 | $fetch = substr($read,$i_space+1,5); |
657 | if (!is_numeric($id) && $fetch !== 'FETCH') { |
8cc8ec79 |
658 | $msg['ERROR'] = $read; // htmlspecialchars should be done just before display. this is backend code |
659 | break; |
a18594b2 |
660 | } |
661 | $i = strpos($read,'(',$i_space+5); |
662 | $read = substr($read,$i+1); |
663 | $i_len = strlen($read); |
664 | $i = 0; |
665 | while ($i < $i_len && $i !== false) { |
666 | /* get argument */ |
667 | $read = trim(substr($read,$i)); |
668 | $i_len = strlen($read); |
669 | $i = strpos($read,' '); |
670 | $arg = substr($read,0,$i); |
671 | ++$i; |
672 | switch ($arg) |
673 | { |
674 | case 'UID': |
675 | $i_pos = strpos($read,' ',$i); |
676 | if (!$i_pos) { |
677 | $i_pos = strpos($read,')',$i); |
cdca177a |
678 | } |
a18594b2 |
679 | if ($i_pos) { |
680 | $unique_id = substr($read,$i,$i_pos-$i); |
681 | $i = $i_pos+1; |
682 | } else { |
683 | break 3; |
cdca177a |
684 | } |
a18594b2 |
685 | break; |
686 | case 'FLAGS': |
687 | $flags = parseArray($read,$i); |
688 | if (!$flags) break 3; |
ffb776c4 |
689 | $aFlags = array(); |
a18594b2 |
690 | foreach ($flags as $flag) { |
691 | $flag = strtolower($flag); |
ffb776c4 |
692 | $aFlags[$flag] = true; |
cdca177a |
693 | } |
ffb776c4 |
694 | $msg['FLAGS'] = $aFlags; |
a18594b2 |
695 | break; |
696 | case 'RFC822.SIZE': |
697 | $i_pos = strpos($read,' ',$i); |
698 | if (!$i_pos) { |
699 | $i_pos = strpos($read,')',$i); |
cdca177a |
700 | } |
a18594b2 |
701 | if ($i_pos) { |
ffb776c4 |
702 | $msg['SIZE'] = substr($read,$i,$i_pos-$i); |
a18594b2 |
703 | $i = $i_pos+1; |
704 | } else { |
705 | break 3; |
706 | } |
1c198ef7 |
707 | |
8cc8ec79 |
708 | break; |
709 | case 'ENVELOPE': |
710 | break; // to be implemented, moving imap code out of the nessages class |
711 | sqimap_parse_address($read,$i,$msg); |
712 | break; // to be implemented, moving imap code out of the nessages class |
713 | case 'BODYSTRUCTURE': |
a18594b2 |
714 | break; |
715 | case 'INTERNALDATE': |
35519cd7 |
716 | $msg['INTERNALDATE'] = trim(str_replace(' ', ' ',parseString($read,$i))); |
a18594b2 |
717 | break; |
718 | case 'BODY.PEEK[HEADER.FIELDS': |
719 | case 'BODY[HEADER.FIELDS': |
720 | $i = strpos($read,'{',$i); |
721 | $header = parseString($read,$i); |
92a52cda |
722 | if ($header === false) break 2; |
2a9b0fad |
723 | /* First we replace all \r\n by \n, and unfold the header */ |
724 | $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $header)); |
a18594b2 |
725 | /* Now we can make a new header array with */ |
726 | /* each element representing a headerline */ |
2a9b0fad |
727 | $hdr = explode("\n" , $hdr); |
2714d4ff |
728 | $aReceived = array(); |
a18594b2 |
729 | foreach ($hdr as $line) { |
730 | $pos = strpos($line, ':'); |
731 | if ($pos > 0) { |
732 | $field = strtolower(substr($line, 0, $pos)); |
733 | if (!strstr($field,' ')) { /* valid field */ |
734 | $value = trim(substr($line, $pos+1)); |
735 | switch($field) |
736 | { |
ffb776c4 |
737 | case 'to': $msg['TO'] = $value; break; |
738 | case 'cc': $msg['CC'] = $value; break; |
739 | case 'from': $msg['FROM'] = $value; break; |
740 | case 'date': |
741 | $msg['DATE'] = str_replace(' ', ' ', $value); |
cdca177a |
742 | break; |
bddb3448 |
743 | case 'x-priority': |
744 | case 'importance': |
745 | case 'priority': |
746 | $msg['PRIORITY'] = parsePriority($value); break; |
ffb776c4 |
747 | case 'subject': $msg['SUBJECT'] = $value; break; |
a18594b2 |
748 | case 'content-type': |
749 | $type = $value; |
cdca177a |
750 | if ($pos = strpos($type, ";")) { |
751 | $type = substr($type, 0, $pos); |
752 | } |
753 | $type = explode("/", $type); |
ceacbb9f |
754 | if(!is_array($type) || count($type) < 2) { |
ffb776c4 |
755 | $msg['TYPE0'] = 'text'; |
756 | $msg['TYPE1'] = 'plain'; |
ceacbb9f |
757 | } else { |
758 | $msg['TYPE0'] = strtolower($type[0]); |
759 | $msg['TYPE1'] = strtolower($type[1]); |
cef054e4 |
760 | } |
cdca177a |
761 | break; |
2714d4ff |
762 | case 'received': |
763 | $aReceived[] = $value; |
764 | break; |
a18594b2 |
765 | default: break; |
766 | } |
cdca177a |
767 | } |
768 | } |
769 | } |
2714d4ff |
770 | if (count($aReceived)) { |
771 | $msg['RECEIVED'] = $aReceived; |
772 | } |
a18594b2 |
773 | break; |
774 | default: |
775 | ++$i; |
776 | break; |
cdca177a |
777 | } |
cdca177a |
778 | } |
6201339c |
779 | $msgi ="$unique_id"; |
324ac3c5 |
780 | $msg['UID'] = $unique_id; |
ffb776c4 |
781 | |
324ac3c5 |
782 | $aMessageList[$msgi] = $msg; |
a18594b2 |
783 | ++$msgi; |
97f7ddf2 |
784 | } |
324ac3c5 |
785 | return $aMessageList; |
97f7ddf2 |
786 | } |
787 | |
8315c94c |
788 | |
258d61ed |
789 | /** |
790 | * Work in process |
791 | * @private |
792 | * @author Marc Groot Koerkamp |
793 | */ |
8cc8ec79 |
794 | function sqimap_parse_envelope($read, &$i, &$msg) { |
795 | $arg_no = 0; |
796 | $arg_a = array(); |
797 | ++$i; |
798 | for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++$i) { |
799 | $char = strtoupper($read{$i}); |
800 | switch ($char) { |
801 | case '{': |
802 | case '"': |
803 | $arg_a[] = parseString($read,$i); |
804 | ++$arg_no; |
805 | break; |
806 | case 'N': |
807 | /* probably NIL argument */ |
808 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
809 | $arg_a[] = ''; |
810 | ++$arg_no; |
811 | $i += 2; |
812 | } |
813 | break; |
814 | case '(': |
815 | /* Address structure (with group support) |
816 | * Note: Group support is useless on SMTP connections |
817 | * because the protocol doesn't support it |
818 | */ |
819 | $addr_a = array(); |
820 | $group = ''; |
821 | $a=0; |
822 | for (; $i < $cnt && $read{$i} != ')'; ++$i) { |
823 | if ($read{$i} == '(') { |
824 | $addr = sqimap_parse_address($read, $i); |
825 | if (($addr[3] == '') && ($addr[2] != '')) { |
826 | /* start of group */ |
827 | $group = $addr[2]; |
828 | $group_addr = $addr; |
829 | $j = $a; |
830 | } else if ($group && ($addr[3] == '') && ($addr[2] == '')) { |
831 | /* end group */ |
832 | if ($a == ($j+1)) { /* no group members */ |
833 | $group_addr[4] = $group; |
834 | $group_addr[2] = ''; |
835 | $group_addr[0] = "$group: Undisclosed recipients;"; |
836 | $addr_a[] = $group_addr; |
837 | $group =''; |
838 | } |
839 | } else { |
840 | $addr[4] = $group; |
841 | $addr_a[] = $addr; |
842 | } |
843 | ++$a; |
844 | } |
845 | } |
846 | $arg_a[] = $addr_a; |
847 | break; |
848 | default: break; |
849 | } |
850 | } |
851 | |
852 | if (count($arg_a) > 9) { |
853 | $d = strtr($arg_a[0], array(' ' => ' ')); |
854 | $d = explode(' ', $d); |
cf92500b |
855 | if (!$arg_a[1]) $arg_a[1] = ''; |
8cc8ec79 |
856 | $msg['DATE'] = $d; /* argument 1: date */ |
857 | $msg['SUBJECT'] = $arg_a[1]; /* argument 2: subject */ |
858 | $msg['FROM'] = is_array($arg_a[2]) ? $arg_a[2][0] : ''; /* argument 3: from */ |
859 | $msg['SENDER'] = is_array($arg_a[3]) ? $arg_a[3][0] : ''; /* argument 4: sender */ |
860 | $msg['REPLY-TO'] = is_array($arg_a[4]) ? $arg_a[4][0] : ''; /* argument 5: reply-to */ |
861 | $msg['TO'] = $arg_a[5]; /* argument 6: to */ |
862 | $msg['CC'] = $arg_a[6]; /* argument 7: cc */ |
863 | $msg['BCC'] = $arg_a[7]; /* argument 8: bcc */ |
864 | $msg['IN-REPLY-TO'] = $arg_a[8]; /* argument 9: in-reply-to */ |
865 | $msg['MESSAGE-ID'] = $arg_a[9]; /* argument 10: message-id */ |
866 | } |
867 | } |
868 | |
8315c94c |
869 | |
258d61ed |
870 | /** |
871 | * Work in process |
872 | * @private |
873 | * @author Marc Groot Koerkamp |
874 | */ |
8cc8ec79 |
875 | function sqimap_parse_address($read, &$i) { |
876 | $arg_a = array(); |
877 | for (; $read{$i} != ')'; ++$i) { |
878 | $char = strtoupper($read{$i}); |
879 | switch ($char) { |
880 | case '{': |
881 | case '"': $arg_a[] = parseString($read,$i); break; |
882 | case 'n': |
883 | case 'N': |
884 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
885 | $arg_a[] = ''; |
886 | $i += 2; |
887 | } |
888 | break; |
889 | default: break; |
890 | } |
891 | } |
892 | |
893 | if (count($arg_a) == 4) { |
894 | return $arg_a; |
895 | |
896 | // $adr = new AddressStructure(); |
897 | // $adr->personal = $arg_a[0]; |
898 | // $adr->adl = $arg_a[1]; |
899 | // $adr->mailbox = $arg_a[2]; |
900 | // $adr->host = $arg_a[3]; |
901 | } else { |
902 | $adr = ''; |
903 | } |
904 | return $adr; |
905 | } |
906 | |
8315c94c |
907 | |
48af4b64 |
908 | /** |
258d61ed |
909 | * Returns a message array with all the information about a message. |
910 | * See the documentation folder for more information about this array. |
911 | * |
912 | * @param resource $imap_stream imap connection |
913 | * @param integer $id uid of the message |
914 | * @param string $mailbox used for error handling, can be removed because we should return an error code and generate the message elsewhere |
915 | * @return Message Message object |
916 | */ |
8315c94c |
917 | function sqimap_get_message($imap_stream, $id, $mailbox) { |
461eda6c |
918 | // typecast to int to prohibit 1:* msgs sets |
919 | $id = (int) $id; |
2d34da11 |
920 | $flags = array(); |
8315c94c |
921 | $read = sqimap_run_command($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, TRUE); |
114f2a24 |
922 | if ($read) { |
b69a13a4 |
923 | if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) { |
924 | if (trim($regs[1])) { |
925 | $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY'); |
926 | } |
927 | } |
114f2a24 |
928 | } else { |
b69a13a4 |
929 | /* the message was not found, maybe the mailbox was modified? */ |
930 | global $sort, $startMessage, $color; |
931 | |
932 | $errmessage = _("The server couldn't find the message you requested.") . |
0fdc2fb6 |
933 | '<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 |
934 | /* this will include a link back to the message list */ |
461eda6c |
935 | error_message($errmessage, $mailbox, $sort, (int) $startMessage, $color); |
b69a13a4 |
936 | exit; |
1c198ef7 |
937 | } |
2d34da11 |
938 | $bodystructure = implode('',$read); |
939 | $msg = mime_structure($bodystructure,$flags); |
8315c94c |
940 | $read = sqimap_run_command($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, TRUE); |
19d470aa |
941 | $rfc822_header = new Rfc822Header(); |
767ace1f |
942 | $rfc822_header->parseHeader($read); |
943 | $msg->rfc822_header = $rfc822_header; |
2d34da11 |
944 | return $msg; |
97f7ddf2 |
945 | } |
946 | |
8315c94c |
947 | |
258d61ed |
948 | /** |
8315c94c |
949 | * Deprecated !!!!!!! DO NOT USE THIS, use sqimap_msgs_list_copy instead |
950 | */ |
951 | function sqimap_messages_copy($imap_stream, $start, $end, $mailbox) { |
258d61ed |
952 | $read = sqimap_run_command ($imap_stream, "COPY $start:$end " . sqimap_encode_mailbox_name($mailbox), true, $response, $message, TRUE); |
953 | } |
954 | |
8315c94c |
955 | |
258d61ed |
956 | /** |
8315c94c |
957 | * Deprecated !!!!!!! DO NOT USE THIS, use sqimap_msgs_list_delete instead |
958 | */ |
959 | function sqimap_messages_delete($imap_stream, $start, $end, $mailbox, $bypass_trash=false) { |
ce68b76b |
960 | global $move_to_trash, $trash_folder; |
258d61ed |
961 | |
962 | if (($move_to_trash == true) && ($bypass_trash != true) && |
963 | (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) { |
964 | sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder); |
965 | } |
966 | sqimap_messages_flag ($imap_stream, $start, $end, "Deleted", true); |
967 | } |
968 | |
8315c94c |
969 | |
258d61ed |
970 | /** |
8315c94c |
971 | * Deprecated !!!!!!! DO NOT USE THIS, use sqimap_toggle_flag instead |
972 | * Set a flag on the provided uid list |
973 | * @param resource imap connection |
974 | */ |
975 | function sqimap_messages_flag($imap_stream, $start, $end, $flag, $handle_errors) { |
258d61ed |
976 | $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", $handle_errors, $response, $message, TRUE); |
977 | } |
978 | |
8315c94c |
979 | |
980 | /** |
981 | * @deprecated |
982 | */ |
983 | function sqimap_get_small_header($imap_stream, $id, $sent) { |
258d61ed |
984 | $res = sqimap_get_small_header_list($imap_stream, $id, $sent); |
985 | return $res[0]; |
986 | } |
987 | |
9b935fea |
988 | ?> |