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