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