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