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