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