59177427 |
1 | <?php |
bccadd02 |
2 | |
35586184 |
3 | /** |
a6fd80f5 |
4 | * imap_general.php |
35586184 |
5 | * |
76911253 |
6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
15e6162e |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
35586184 |
8 | * |
15e6162e |
9 | * This implements all functions that do general imap functions. |
35586184 |
10 | * |
15e6162e |
11 | * $Id$ |
35586184 |
12 | */ |
13 | |
b68edc75 |
14 | require_once(SM_PATH . 'functions/page_header.php'); |
47a29326 |
15 | require_once(SM_PATH . 'functions/auth.php'); |
16 | |
35586184 |
17 | |
41600f7d |
18 | global $sqimap_session_id; |
19 | $sqimap_session_id = 1; |
71257f8b |
20 | |
3411d4ec |
21 | /* Sets an unique session id in order to avoid simultanous sessions crash. */ |
487daa81 |
22 | function sqimap_session_id($unique_id = false) { |
41600f7d |
23 | global $data_dir, $username, $sqimap_session_id; |
487daa81 |
24 | if (!$unique_id) { |
098ea084 |
25 | return( sprintf("A%03d", $sqimap_session_id++) ); |
487daa81 |
26 | } else { |
098ea084 |
27 | return( sprintf("A%03d", $sqimap_session_id++) . ' UID' ); |
487daa81 |
28 | } |
9c737111 |
29 | } |
30 | |
3411d4ec |
31 | /* |
32 | * Both send a command and accept the result from the command. |
33 | * This is to allow proper session number handling. |
34 | */ |
487daa81 |
35 | function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) { |
c5809184 |
36 | if ($imap_stream) { |
098ea084 |
37 | $sid = sqimap_session_id($unique_id); |
38 | fputs ($imap_stream, $sid . ' ' . $query . "\r\n"); |
bc78cc6e |
39 | $tag_uid_a = explode(' ',trim($sid)); |
40 | $tag = $tag_uid_a[0]; |
0dc05a81 |
41 | $read = sqimap_retrieve_imap_response ($imap_stream, $tag, $handle_errors, $response, $message, $query ); |
bc78cc6e |
42 | /* get the response and the message */ |
43 | $message = $message[$tag]; |
44 | $response = $response[$tag]; |
45 | return $read[$tag]; |
c5809184 |
46 | } else { |
47 | global $squirrelmail_language, $color; |
48 | set_up_language($squirrelmail_language); |
49 | require_once(SM_PATH . 'functions/display_messages.php'); |
50 | $string = "<b><font color=$color[2]>\n" . |
51 | _("ERROR : No available imapstream.") . |
52 | "</b></font>\n"; |
53 | error_box($string,$color); |
098ea084 |
54 | return false; |
c5809184 |
55 | } |
1c72b151 |
56 | } |
57 | |
7c7b74b3 |
58 | function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, |
59 | &$message, $unique_id = false,$filter=false, |
60 | $outputstream=false,$no_return=false) { |
c5809184 |
61 | if ($imap_stream) { |
62 | $sid = sqimap_session_id($unique_id); |
bc78cc6e |
63 | fputs ($imap_stream, $sid . ' ' . $query . "\r\n"); |
64 | $tag_uid_a = explode(' ',trim($sid)); |
65 | $tag = $tag_uid_a[0]; |
66 | |
67 | $read = sqimap_read_data ($imap_stream, $tag, $handle_errors, $response, |
7c7b74b3 |
68 | $message, $query,$filter,$outputstream,$no_return); |
bc78cc6e |
69 | /* retrieve the response and the message */ |
70 | $response = $response[$tag]; |
71 | $message = $message[$tag]; |
5c300c60 |
72 | |
bc78cc6e |
73 | if (!empty($read[$tag])) { |
74 | return $read[$tag][0]; |
75 | } else { |
76 | return $read[$tag]; |
77 | } |
c5809184 |
78 | } else { |
79 | global $squirrelmail_language, $color; |
80 | set_up_language($squirrelmail_language); |
81 | require_once(SM_PATH . 'functions/display_messages.php'); |
82 | $string = "<b><font color=$color[2]>\n" . |
83 | _("ERROR : No available imapstream.") . |
84 | "</b></font>\n"; |
85 | error_box($string,$color); |
098ea084 |
86 | return false; |
bc78cc6e |
87 | } |
88 | } |
89 | function sqimap_prepare_pipelined_query($new_query,&$tag,&$aQuery,$unique_id) { |
90 | $sid = sqimap_session_id($unique_id); |
91 | $tag_uid_a = explode(' ',trim($sid)); |
92 | $tag = $tag_uid_a[0]; |
0b38d197 |
93 | $query = $sid . ' '.$new_query."\r\n"; |
bc78cc6e |
94 | $aQuery[$tag] = $query; |
42a07ac1 |
95 | } |
96 | |
52c8b585 |
97 | function sqimap_run_pipelined_command ($imap_stream, $aQueryList, $handle_errors, |
bc78cc6e |
98 | &$aServerResponse, &$aServerMessage, $unique_id = false, |
5c300c60 |
99 | $filter=false,$outputstream=false,$no_return=false) { |
bc78cc6e |
100 | $aResponse = false; |
5c300c60 |
101 | |
52c8b585 |
102 | /* |
103 | Do not fire all calls at once to the imap-server but split the calls up |
104 | in portions of $iChunkSize. If we do not do that I think we misbehave as |
105 | IMAP client or should handle BYE calls if the IMAP-server drops the |
106 | connection because the number of queries is to large. This isn't tested |
107 | but a wild guess how it could work in the field. |
0b38d197 |
108 | |
c03cba15 |
109 | After testing it on Exchange 2000 we discovered that a chunksize of 32 |
0b38d197 |
110 | was quicker then when we raised it to 128. |
52c8b585 |
111 | */ |
112 | $iQueryCount = count($aQueryList); |
113 | $iChunkSize = 32; |
114 | // array_chunk would also do the job but it's supported from php > 4.2 |
115 | $aQueryChunks = array(); |
116 | $iLoops = floor($iQueryCount / $iChunkSize); |
117 | |
5c300c60 |
118 | if ($iLoops * $iChunkSize != $iQueryCount) ++$iLoops; |
52c8b585 |
119 | |
120 | if (!function_exists('array_chunk')) { // arraychunk replacement |
5c300c60 |
121 | reset($aQueryList); |
52c8b585 |
122 | for($i=0;$i<$iLoops;++$i) { |
5c300c60 |
123 | for($j=0;$j<$iChunkSize;++$j) { |
124 | $key = key($aQueryList); |
125 | $aTmp[$key] = $aQueryList[$key]; |
126 | if (next($aQueryList) === false) break; |
127 | } |
128 | $aQueryChunks[] = $aTmp; |
129 | } |
52c8b585 |
130 | } else { |
131 | $aQueryChunks = array_chunk($aQueryList,$iChunkSize,true); |
bc78cc6e |
132 | } |
52c8b585 |
133 | |
134 | for ($i=0;$i<$iLoops;++$i) { |
135 | $aQuery = $aQueryChunks[$i]; |
136 | foreach($aQuery as $tag => $query) { |
137 | fputs($imap_stream,$query); |
138 | $aResults[$tag] = false; |
139 | } |
52c8b585 |
140 | foreach($aQuery as $tag => $query) { |
5c300c60 |
141 | if ($aResults[$tag] == false) { |
0dc05a81 |
142 | $aReturnedResponse = sqimap_retrieve_imap_response ($imap_stream, $tag, |
bc78cc6e |
143 | $handle_errors, $response, $message, $query, |
144 | $filter,$outputstream,$no_return); |
52c8b585 |
145 | foreach ($aReturnedResponse as $returned_tag => $aResponse) { |
5c300c60 |
146 | if (!empty($aResponse)) { |
52c8b585 |
147 | $aResults[$returned_tag] = $aResponse[0]; |
5c300c60 |
148 | } else { |
149 | $aResults[$returned_tag] = $aResponse; |
150 | } |
52c8b585 |
151 | $aServerResponse[$returned_tag] = $response[$returned_tag]; |
152 | $aServerMessage[$returned_tag] = $message[$returned_tag]; |
153 | } |
bc78cc6e |
154 | } |
155 | } |
156 | } |
b7277e4f |
157 | return $aResults; |
bc78cc6e |
158 | } |
9c737111 |
159 | |
b8c285ab |
160 | /* |
161 | * custom fgets function. gets a line from IMAP |
162 | * no matter how big it may be |
163 | */ |
164 | |
165 | function sqimap_fgets($imap_stream) { |
166 | $read = ''; |
167 | $buffer = 4096; |
168 | $results = ''; |
c41daf03 |
169 | $offset = 0; |
170 | while (strpos($results, "\r\n", $offset) === false) { |
b8c285ab |
171 | if (!($read = fgets($imap_stream, $buffer))) { |
329a7ca5 |
172 | /* this happens in case of an error */ |
173 | /* reset $results because it's useless */ |
174 | $results = false; |
b8c285ab |
175 | break; |
176 | } |
c41daf03 |
177 | if ( $results != '' ) { |
178 | $offset = strlen($results) - 1; |
179 | } |
b8c285ab |
180 | $results .= $read; |
181 | } |
182 | return $results; |
183 | } |
184 | |
7c7b74b3 |
185 | function sqimap_fread($imap_stream,$iSize,$filter=false, |
186 | $outputstream=false, $no_return=false) { |
187 | if (!$filter || !$outputstream) { |
188 | $iBufferSize = $iSize; |
189 | } else { |
bc78cc6e |
190 | $iBufferSize = 62400; // multiple of 78 in case of base64 decoding. |
7c7b74b3 |
191 | } |
192 | $iRet = $iSize - $iBufferSize; |
5eba0be2 |
193 | $iRetrieved = 0; |
7c7b74b3 |
194 | $i = 0; |
bc78cc6e |
195 | $results = $sReadRem = ''; |
196 | $bFinished = $bBufferSizeAdapted = $bBufferIsOk = false; |
5eba0be2 |
197 | while (($iRetrieved < ($iSize - $iBufferSize))) { |
7c7b74b3 |
198 | $sRead = fread($imap_stream,$iBufferSize); |
bc78cc6e |
199 | if (!$sRead) { |
7c7b74b3 |
200 | $results = false; |
201 | break; |
202 | } |
5eba0be2 |
203 | $iRetrieved += $iBufferSize; |
7c7b74b3 |
204 | if ($filter) { |
bc78cc6e |
205 | // in case line-endings do not appear at position 78 we adapt the buffersize so we can base64 decode on the fly |
206 | if (!$bBufferSizeAdapted) { |
207 | $i = strpos($sRead,"\n"); |
208 | if ($i) { |
209 | ++$i; |
210 | $iFragments = floor($iBufferSize / $i); |
211 | $iNewBufferSize = $iFragments * $i; |
212 | $iRemainder = $iNewBufferSize + $i - $iBufferSize; |
213 | if ($iNewBufferSize == $iBufferSize) { |
214 | $bBufferIsOk = true; |
215 | $iRemainder = 0; |
216 | $iNewBufferSize = $iBufferSize; |
217 | $bBufferSizeAdapted = true; |
218 | } |
219 | if (!$bBufferIsOk && ($iRemainder + $iBufferSize) < $iSize) { |
220 | $sReadRem = fread($imap_stream,$iRemainder); |
221 | } else if (!$bBufferIsOk) { |
222 | $sReadRem = fread($imap_stream,$iSize - $iBufferSize); |
223 | $bFinished = true; |
224 | } |
225 | if (!$sReadRem && $sReadRem !== '') { |
226 | $results = false; |
227 | break; |
228 | } |
229 | $iBufferSize = $iNewBufferSize; |
230 | $bBufferSizeAdapted = true; |
231 | } else { |
232 | $sReadRem = fread($imap_stream,$iSize - $iBufferSize); |
233 | $bFinished = true; |
234 | if (!$sReadRem) { |
235 | $results = false; |
236 | break; |
237 | } |
238 | } |
239 | $sRead .= $sReadRem; |
240 | $iRetrieved += $iRemainder; |
241 | unset($sReadRem); |
242 | } |
7c7b74b3 |
243 | $filter($sRead); |
244 | } |
245 | if ($outputstream) { |
246 | if (is_resource($outputstream)) { |
247 | fwrite($outputstream,$sRead); |
248 | } else if ($outputstream == 'php://stdout') { |
249 | echo $sRead; |
250 | } |
251 | } |
252 | if ($no_return) { |
253 | $sRead = ''; |
254 | } |
255 | $results .= $sRead; |
256 | } |
bc78cc6e |
257 | if (!$results && !$bFinished) { |
5eba0be2 |
258 | $sRead = fread($imap_stream,($iSize - ($iRetrieved))); |
7c7b74b3 |
259 | if ($filter) { |
260 | $filter($sRead); |
261 | } |
262 | if ($outputstream) { |
263 | if (is_resource($outputstream)) { |
264 | fwrite($outputstream,$sRead); |
265 | } else if ($outputstream == 'php://stdout') { // FIXME |
266 | echo $sRead; |
267 | } |
268 | } |
269 | if ($no_return) { |
270 | $sRead = ''; |
271 | } |
272 | $results .= $sRead; |
273 | } |
274 | return $results; |
275 | } |
0dc05a81 |
276 | /* obsolete function, inform plugins that use it */ |
277 | function sqimap_read_data_list($imap_stream, $tag, $handle_errors, |
278 | &$response, &$message, $query = '') { |
279 | global $color, $squirrelmail_language; |
280 | set_up_language($squirrelmail_language); |
281 | require_once(SM_PATH . 'functions/display_messages.php'); |
282 | $string = "<b><font color=$color[2]>\n" . |
283 | _("ERROR : Bad function call.") . |
284 | "</b><br>\n" . |
285 | _("Reason:") . ' '. |
286 | 'There is a plugin installed which make use of the <br>' . |
287 | 'SquirrelMail internal function sqimap_read_data_list.<br>'. |
5c300c60 |
288 | 'Please adapt the installed plugin and let it use<br>'. |
289 | 'sqimap_run_command or sqimap_run_command_list instead<br><br>'. |
290 | 'The following query was issued:<br>'. |
0dc05a81 |
291 | htmlspecialchars($query) . '<br>' . "</font><br>\n"; |
292 | error_box($string,$color); |
293 | echo '</body></html>'; |
294 | exit; |
295 | } |
7c7b74b3 |
296 | |
bee165ef |
297 | /* |
3411d4ec |
298 | * Reads the output from the IMAP stream. If handle_errors is set to true, |
299 | * this will also handle all errors that are received. If it is not set, |
300 | * the errors will be sent back through $response and $message |
bee165ef |
301 | */ |
b8c285ab |
302 | |
0dc05a81 |
303 | function sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors, |
7c7b74b3 |
304 | &$response, &$message, $query = '', |
305 | $filter = false, $outputstream = false, $no_return = false) { |
9c737111 |
306 | global $color, $squirrelmail_language; |
9c737111 |
307 | $read = ''; |
bc78cc6e |
308 | if (!is_array($message)) $message = array(); |
309 | if (!is_array($response)) $response = array(); |
b8c285ab |
310 | $resultlist = array(); |
311 | $data = array(); |
312 | $read = sqimap_fgets($imap_stream); |
5c300c60 |
313 | $i = $k = 0; |
bea3eb1e |
314 | while ($read) { |
315 | $char = $read{0}; |
316 | switch ($char) |
317 | { |
15bc7f66 |
318 | case '+': |
319 | default: |
320 | $read = sqimap_fgets($imap_stream); |
321 | break; |
322 | |
323 | case $tag{0}: |
324 | { |
bea3eb1e |
325 | /* get the command */ |
326 | $arg = ''; |
327 | $i = strlen($tag)+1; |
328 | $s = substr($read,$i); |
329 | if (($j = strpos($s,' ')) || ($j = strpos($s,"\n"))) { |
330 | $arg = substr($s,0,$j); |
331 | } |
332 | $found_tag = substr($read,0,$i-1); |
5c300c60 |
333 | if ($found_tag) { |
bea3eb1e |
334 | switch ($arg) |
335 | { |
15bc7f66 |
336 | case 'OK': |
337 | case 'BAD': |
338 | case 'NO': |
339 | case 'BYE': |
340 | case 'PREAUTH': |
bc78cc6e |
341 | $response[$found_tag] = $arg; |
342 | $message[$found_tag] = trim(substr($read,$i+strlen($arg))); |
343 | if (!empty($data)) { |
344 | $resultlist[] = $data; |
345 | } |
5c300c60 |
346 | $aResponse[$found_tag] = $resultlist; |
347 | $data = $resultlist = array(); |
348 | if ($found_tag == $tag) { |
349 | break 3; /* switch switch while */ |
350 | } |
351 | break; |
329a7ca5 |
352 | default: |
bea3eb1e |
353 | /* this shouldn't happen */ |
bc78cc6e |
354 | $response[$found_tag] = $arg; |
355 | $message[$found_tag] = trim(substr($read,$i+strlen($arg))); |
356 | if (!empty($data)) { |
357 | $resultlist[] = $data; |
358 | } |
359 | $aResponse[$found_tag] = $resultlist; |
5c300c60 |
360 | $data = $resultlist = array(); |
361 | if ($found_tag == $tag) { |
362 | break 3; /* switch switch while */ |
363 | } |
bc78cc6e |
364 | } |
bea3eb1e |
365 | } |
5c300c60 |
366 | $read = sqimap_fgets($imap_stream); |
367 | if ($read === false) { /* error */ |
368 | break 3; /* switch switch while */ |
369 | } |
370 | break; |
15bc7f66 |
371 | } // end case $tag{0} |
372 | |
373 | case '*': |
374 | { |
bea3eb1e |
375 | if (preg_match('/^\*\s\d+\sFETCH/',$read)) { |
376 | /* check for literal */ |
377 | $s = substr($read,-3); |
378 | $fetch_data = array(); |
379 | do { /* outer loop, continue until next untagged fetch |
380 | or tagged reponse */ |
381 | do { /* innerloop for fetching literals. with this loop |
382 | we prohibid that literal responses appear in the |
383 | outer loop so we can trust the untagged and |
384 | tagged info provided by $read */ |
385 | if ($s === "}\r\n") { |
386 | $j = strrpos($read,'{'); |
387 | $iLit = substr($read,$j+1,-3); |
388 | $fetch_data[] = $read; |
7c7b74b3 |
389 | $sLiteral = sqimap_fread($imap_stream,$iLit,$filter,$outputstream,$no_return); |
329a7ca5 |
390 | if ($sLiteral === false) { /* error */ |
391 | break 4; /* while while switch while */ |
392 | } |
bea3eb1e |
393 | /* backwards compattibility */ |
394 | $aLiteral = explode("\n", $sLiteral); |
395 | /* release not neaded data */ |
396 | unset($sLiteral); |
397 | foreach ($aLiteral as $line) { |
398 | $fetch_data[] = $line ."\n"; |
399 | } |
400 | /* release not neaded data */ |
401 | unset($aLiteral); |
402 | /* next fgets belongs to this fetch because |
403 | we just got the exact literalsize and there |
404 | must follow data to complete the response */ |
329a7ca5 |
405 | $read = sqimap_fgets($imap_stream); |
406 | if ($read === false) { /* error */ |
407 | break 4; /* while while switch while */ |
408 | } |
c0b23345 |
409 | $fetch_data[] = $read; |
bea3eb1e |
410 | } else { |
329a7ca5 |
411 | $fetch_data[] = $read; |
bea3eb1e |
412 | } |
413 | /* retrieve next line and check in the while |
414 | statements if it belongs to this fetch response */ |
415 | $read = sqimap_fgets($imap_stream); |
329a7ca5 |
416 | if ($read === false) { /* error */ |
417 | break 4; /* while while switch while */ |
418 | } |
bea3eb1e |
419 | /* check for next untagged reponse and break */ |
420 | if ($read{0} == '*') break 2; |
421 | $s = substr($read,-3); |
422 | } while ($s === "}\r\n"); |
423 | $s = substr($read,-3); |
424 | } while ($read{0} !== '*' && |
106d4087 |
425 | substr($read,0,strlen($tag)) !== $tag); |
329a7ca5 |
426 | $resultlist[] = $fetch_data; |
bea3eb1e |
427 | /* release not neaded data */ |
428 | unset ($fetch_data); |
429 | } else { |
430 | $s = substr($read,-3); |
431 | do { |
432 | if ($s === "}\r\n") { |
433 | $j = strrpos($read,'{'); |
434 | $iLit = substr($read,$j+1,-3); |
435 | $data[] = $read; |
329a7ca5 |
436 | $sLiteral = fread($imap_stream,$iLit); |
c0b23345 |
437 | if ($sLiteral === false) { /* error */ |
329a7ca5 |
438 | $read = false; |
439 | break 3; /* while switch while */ |
440 | } |
c0b23345 |
441 | $data[] = $sLiteral; |
b7277e4f |
442 | $data[] = sqimap_fgets($imap_stream); |
bea3eb1e |
443 | } else { |
329a7ca5 |
444 | $data[] = $read; |
bea3eb1e |
445 | } |
446 | $read = sqimap_fgets($imap_stream); |
329a7ca5 |
447 | if ($read === false) { |
448 | break 3; /* while switch while */ |
449 | } else if ($read{0} == '*') { |
106d4087 |
450 | break; |
451 | } |
bea3eb1e |
452 | $s = substr($read,-3); |
453 | } while ($s === "}\r\n"); |
454 | break 1; |
5c300c60 |
455 | } |
bea3eb1e |
456 | break; |
15bc7f66 |
457 | } // end case '*' |
458 | } // end switch |
c0b23345 |
459 | } // end while |
460 | |
461 | /* error processing in case $read is false */ |
462 | if ($read === false) { |
463 | unset($data); |
464 | set_up_language($squirrelmail_language); |
465 | require_once(SM_PATH . 'functions/display_messages.php'); |
466 | $string = "<b><font color=$color[2]>\n" . |
329a7ca5 |
467 | _("ERROR : Connection dropped by imap-server.") . |
468 | "</b><br>\n" . |
469 | _("Query:") . ' '. |
470 | htmlspecialchars($query) . '<br>' . "</font><br>\n"; |
c0b23345 |
471 | error_box($string,$color); |
472 | exit; |
b8c285ab |
473 | } |
c0b23345 |
474 | |
15bc7f66 |
475 | /* Set $resultlist array */ |
b8c285ab |
476 | if (!empty($data)) { |
bc78cc6e |
477 | //$resultlist[] = $data; |
9c737111 |
478 | } |
b8c285ab |
479 | elseif (empty($resultlist)) { |
480 | $resultlist[] = array(); |
481 | } |
15bc7f66 |
482 | |
483 | /* Return result or handle errors */ |
bee165ef |
484 | if ($handle_errors == false) { |
bc78cc6e |
485 | return $aResponse; |
bee165ef |
486 | return( $resultlist ); |
dd381002 |
487 | } |
bc78cc6e |
488 | switch ($response[$tag]) |
dd381002 |
489 | { |
490 | case 'OK': |
bc78cc6e |
491 | return $aResponse; |
329a7ca5 |
492 | break; |
dd381002 |
493 | case 'NO': |
494 | /* ignore this error from M$ exchange, it is not fatal (aka bug) */ |
6b6c2e06 |
495 | if (strstr($message[$tag], 'command resulted in') === false) { |
441f2d33 |
496 | set_up_language($squirrelmail_language); |
098ea084 |
497 | require_once(SM_PATH . 'functions/display_messages.php'); |
498 | $string = "<b><font color=$color[2]>\n" . |
b8c285ab |
499 | _("ERROR : Could not complete request.") . |
500 | "</b><br>\n" . |
9b761dbd |
501 | _("Query:") . ' ' . |
502 | htmlspecialchars($query) . '<br>' . |
b8c285ab |
503 | _("Reason Given: ") . |
bc78cc6e |
504 | htmlspecialchars($message[$tag]) . "</font><br>\n"; |
098ea084 |
505 | error_box($string,$color); |
329a7ca5 |
506 | echo '</body></html>'; |
052e0c26 |
507 | exit; |
9c737111 |
508 | } |
329a7ca5 |
509 | break; |
dd381002 |
510 | case 'BAD': |
9c737111 |
511 | set_up_language($squirrelmail_language); |
098ea084 |
512 | require_once(SM_PATH . 'functions/display_messages.php'); |
bef6629c |
513 | $string = "<b><font color=$color[2]>\n" . |
b8c285ab |
514 | _("ERROR : Bad or malformed request.") . |
515 | "</b><br>\n" . |
9b761dbd |
516 | _("Query:") . ' '. |
517 | htmlspecialchars($query) . '<br>' . |
b8c285ab |
518 | _("Server responded: ") . |
bc78cc6e |
519 | htmlspecialchars($message[$tag]) . "</font><br>\n"; |
dd381002 |
520 | error_box($string,$color); |
329a7ca5 |
521 | echo '</body></html>'; |
dd381002 |
522 | exit; |
523 | case 'BYE': |
524 | set_up_language($squirrelmail_language); |
525 | require_once(SM_PATH . 'functions/display_messages.php'); |
526 | $string = "<b><font color=$color[2]>\n" . |
527 | _("ERROR : Imap server closed the connection.") . |
528 | "</b><br>\n" . |
529 | _("Query:") . ' '. |
530 | htmlspecialchars($query) . '<br>' . |
531 | _("Server responded: ") . |
bc78cc6e |
532 | htmlspecialchars($message[$tag]) . "</font><br>\n"; |
dd381002 |
533 | error_box($string,$color); |
329a7ca5 |
534 | echo '</body></html>'; |
9c737111 |
535 | exit; |
dd381002 |
536 | default: |
537 | set_up_language($squirrelmail_language); |
538 | require_once(SM_PATH . 'functions/display_messages.php'); |
539 | $string = "<b><font color=$color[2]>\n" . |
540 | _("ERROR : Unknown imap response.") . |
541 | "</b><br>\n" . |
542 | _("Query:") . ' '. |
543 | htmlspecialchars($query) . '<br>' . |
544 | _("Server responded: ") . |
bc78cc6e |
545 | htmlspecialchars($message[$tag]) . "</font><br>\n"; |
dd381002 |
546 | error_box($string,$color); |
329a7ca5 |
547 | /* the error is displayed but because we don't know the reponse we |
548 | return the result anyway */ |
bc78cc6e |
549 | return $aResponse; |
329a7ca5 |
550 | break; |
9c737111 |
551 | } |
9c737111 |
552 | } |
553 | |
7c7b74b3 |
554 | function sqimap_read_data ($imap_stream, $tag_uid, $handle_errors, |
555 | &$response, &$message, $query = '', |
556 | $filter=false,$outputstream=false,$no_return=false) { |
bc78cc6e |
557 | |
558 | $tag_uid_a = explode(' ',trim($tag_uid)); |
559 | $tag = $tag_uid_a[0]; |
560 | |
0dc05a81 |
561 | $res = sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors, |
7c7b74b3 |
562 | $response, $message, $query,$filter,$outputstream,$no_return); |
863936bb |
563 | /* sqimap_read_data should be called for one response |
0dc05a81 |
564 | but since it just calls sqimap_retrieve_imap_response which |
863936bb |
565 | handles multiple responses we need to check for that |
566 | and merge the $res array IF they are seperated and |
567 | IF it was a FETCH response. */ |
568 | |
ba8f367a |
569 | // if (isset($res[1]) && is_array($res[1]) && isset($res[1][0]) |
570 | // && preg_match('/^\* \d+ FETCH/', $res[1][0])) { |
571 | // $result = array(); |
572 | // foreach($res as $index=>$value) { |
573 | // $result = array_merge($result, $res["$index"]); |
574 | // } |
575 | // } |
863936bb |
576 | if (isset($result)) { |
bc78cc6e |
577 | return $result[$tag]; |
56afb33f |
578 | } |
56afb33f |
579 | else { |
bc78cc6e |
580 | return $res; |
56afb33f |
581 | } |
9c737111 |
582 | } |
583 | |
3411d4ec |
584 | /* |
585 | * Logs the user into the imap server. If $hide is set, no error messages |
586 | * will be displayed. This function returns the imap connection handle. |
587 | */ |
9c737111 |
588 | function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) { |
47a29326 |
589 | global $color, $squirrelmail_language, $onetimepad, $use_imap_tls, $imap_auth_mech; |
85fc999e |
590 | |
eb2f6102 |
591 | if (!isset($onetimepad) || empty($onetimepad)) { |
592 | sqgetglobalvar('onetimepad' , $onetimepad , SQ_SESSION ); |
593 | } |
bd9829d7 |
594 | $imap_server_address = sqimap_get_user_server($imap_server_address, $username); |
098ea084 |
595 | $host=$imap_server_address; |
596 | |
597 | if (($use_imap_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) { |
598 | /* Use TLS by prefixing "tls://" to the hostname */ |
599 | $imap_server_address = 'tls://' . $imap_server_address; |
600 | } |
47a29326 |
601 | |
3411d4ec |
602 | $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15); |
85fc999e |
603 | |
3411d4ec |
604 | /* Do some error correction */ |
9c737111 |
605 | if (!$imap_stream) { |
606 | if (!$hide) { |
441f2d33 |
607 | set_up_language($squirrelmail_language, true); |
098ea084 |
608 | require_once(SM_PATH . 'functions/display_messages.php'); |
609 | $string = sprintf (_("Error connecting to IMAP server: %s.") . |
610 | "<br>\r\n", $imap_server_address) . |
1f720b34 |
611 | "$error_number : $error_string<br>\r\n"; |
098ea084 |
612 | logout_error($string,$color); |
9c737111 |
613 | } |
614 | exit; |
615 | } |
052e0c26 |
616 | |
2f1f7a12 |
617 | $server_info = fgets ($imap_stream, 1024); |
618 | |
619 | /* Decrypt the password */ |
620 | $password = OneTimePadDecrypt($password, $onetimepad); |
621 | |
098ea084 |
622 | if (($imap_auth_mech == 'cram-md5') OR ($imap_auth_mech == 'digest-md5')) { |
fe0b18b3 |
623 | // We're using some sort of authentication OTHER than plain or login |
098ea084 |
624 | $tag=sqimap_session_id(false); |
625 | if ($imap_auth_mech == 'digest-md5') { |
626 | $query = $tag . " AUTHENTICATE DIGEST-MD5\r\n"; |
627 | } elseif ($imap_auth_mech == 'cram-md5') { |
628 | $query = $tag . " AUTHENTICATE CRAM-MD5\r\n"; |
629 | } |
630 | fputs($imap_stream,$query); |
631 | $answer=sqimap_fgets($imap_stream); |
632 | // Trim the "+ " off the front |
633 | $response=explode(" ",$answer,3); |
634 | if ($response[0] == '+') { |
635 | // Got a challenge back |
636 | $challenge=$response[1]; |
637 | if ($imap_auth_mech == 'digest-md5') { |
638 | $reply = digest_md5_response($username,$password,$challenge,'imap',$host); |
639 | } elseif ($imap_auth_mech == 'cram-md5') { |
640 | $reply = cram_md5_response($username,$password,$challenge); |
641 | } |
642 | fputs($imap_stream,$reply); |
643 | $read=sqimap_fgets($imap_stream); |
644 | if ($imap_auth_mech == 'digest-md5') { |
645 | // DIGEST-MD5 has an extra step.. |
646 | if (substr($read,0,1) == '+') { // OK so far.. |
647 | fputs($imap_stream,"\r\n"); |
648 | $read=sqimap_fgets($imap_stream); |
649 | } |
650 | } |
651 | $results=explode(" ",$read,3); |
652 | $response=$results[1]; |
653 | $message=$results[2]; |
47a29326 |
654 | } else { |
098ea084 |
655 | // Fake the response, so the error trap at the bottom will work |
656 | $response="BAD"; |
657 | $message='IMAP server does not appear to support the authentication method selected.'; |
658 | $message .= ' Please contact your system administrator.'; |
47a29326 |
659 | } |
fe0b18b3 |
660 | } elseif ($imap_auth_mech == 'login') { |
098ea084 |
661 | // Original IMAP login code |
fbb76d0e |
662 | $query = 'LOGIN "' . quoteimap($username) . '" "' . quoteimap($password) . '"'; |
47a29326 |
663 | $read = sqimap_run_command ($imap_stream, $query, false, $response, $message); |
1e7fc1cb |
664 | } elseif ($imap_auth_mech == 'plain') { |
098ea084 |
665 | /* Replace this with SASL PLAIN if it ever gets implemented */ |
666 | $response="BAD"; |
667 | $message='SquirrelMail does not support SASL PLAIN yet. Rerun conf.pl and use login instead.'; |
668 | } else { |
669 | $response="BAD"; |
670 | $message="Internal SquirrelMail error - unknown IMAP authentication method chosen. Please contact the developers."; |
671 | } |
47a29326 |
672 | |
098ea084 |
673 | /* If the connection was not successful, lets see why */ |
9c737111 |
674 | if ($response != 'OK') { |
675 | if (!$hide) { |
74424a43 |
676 | if ($response != 'NO') { |
3411d4ec |
677 | /* "BAD" and anything else gets reported here. */ |
098ea084 |
678 | $message = htmlspecialchars($message); |
9c737111 |
679 | set_up_language($squirrelmail_language, true); |
098ea084 |
680 | require_once(SM_PATH . 'functions/display_messages.php'); |
9c737111 |
681 | if ($response == 'BAD') { |
bea3eb1e |
682 | $string = sprintf (_("Bad request: %s")."<br>\r\n", $message); |
9c737111 |
683 | } else { |
bea3eb1e |
684 | $string = sprintf (_("Unknown error: %s") . "<br>\n", $message); |
9c737111 |
685 | } |
1e7fc1cb |
686 | if (isset($read) && is_array($read)) { |
bea3eb1e |
687 | $string .= '<br>' . _("Read data:") . "<br>\n"; |
9c737111 |
688 | foreach ($read as $line) { |
1f720b34 |
689 | $string .= htmlspecialchars($line) . "<br>\n"; |
9c737111 |
690 | } |
691 | } |
098ea084 |
692 | error_box($string,$color); |
9c737111 |
693 | exit; |
165e24a7 |
694 | } else { |
3411d4ec |
695 | /* |
696 | * If the user does not log in with the correct |
1c72b151 |
697 | * username and password it is not possible to get the |
698 | * correct locale from the user's preferences. |
699 | * Therefore, apply the same hack as on the login |
700 | * screen. |
3411d4ec |
701 | * |
702 | * $squirrelmail_language is set by a cookie when |
1c72b151 |
703 | * the user selects language and logs out |
bee165ef |
704 | */ |
9be8198d |
705 | |
9c737111 |
706 | set_up_language($squirrelmail_language, true); |
bd9c880b |
707 | include_once(SM_PATH . 'functions/display_messages.php' ); |
098ea084 |
708 | sqsession_destroy(); |
bd9c880b |
709 | logout_error( _("Unknown user or password incorrect.") ); |
9c737111 |
710 | exit; |
052e0c26 |
711 | } |
9c737111 |
712 | } else { |
052e0c26 |
713 | exit; |
9c737111 |
714 | } |
715 | } |
9c737111 |
716 | return $imap_stream; |
717 | } |
f1e6f580 |
718 | |
3411d4ec |
719 | /* Simply logs out the IMAP session */ |
9c737111 |
720 | function sqimap_logout ($imap_stream) { |
8d936b0c |
721 | /* Logout is not valid until the server returns 'BYE' |
722 | * If we don't have an imap_ stream we're already logged out */ |
26a2cc8b |
723 | if(isset($imap_stream) && $imap_stream) |
8d936b0c |
724 | sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message); |
9c737111 |
725 | } |
726 | |
487daa81 |
727 | function sqimap_capability($imap_stream, $capability='') { |
9c737111 |
728 | global $sqimap_capabilities; |
9c737111 |
729 | if (!is_array($sqimap_capabilities)) { |
1c72b151 |
730 | $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b); |
731 | |
9c737111 |
732 | $c = explode(' ', $read[0]); |
733 | for ($i=2; $i < count($c); $i++) { |
734 | $cap_list = explode('=', $c[$i]); |
3411d4ec |
735 | if (isset($cap_list[1])) { |
9c737111 |
736 | $sqimap_capabilities[$cap_list[0]] = $cap_list[1]; |
3411d4ec |
737 | } else { |
9c737111 |
738 | $sqimap_capabilities[$cap_list[0]] = TRUE; |
3411d4ec |
739 | } |
f1e6f580 |
740 | } |
9c737111 |
741 | } |
487daa81 |
742 | if ($capability) { |
098ea084 |
743 | if (isset($sqimap_capabilities[$capability])) { |
744 | return $sqimap_capabilities[$capability]; |
745 | } else { |
746 | return false; |
747 | } |
f1e6f580 |
748 | } |
487daa81 |
749 | return $sqimap_capabilities; |
9c737111 |
750 | } |
751 | |
3411d4ec |
752 | /* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */ |
9c737111 |
753 | function sqimap_get_delimiter ($imap_stream = false) { |
3411d4ec |
754 | global $sqimap_delimiter, $optional_delimiter; |
85fc999e |
755 | |
9c737111 |
756 | /* Use configured delimiter if set */ |
757 | if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') { |
758 | return $optional_delimiter; |
759 | } |
85fc999e |
760 | |
9c737111 |
761 | /* Do some caching here */ |
762 | if (!$sqimap_delimiter) { |
763 | if (sqimap_capability($imap_stream, 'NAMESPACE')) { |
3411d4ec |
764 | /* |
765 | * According to something that I can't find, this is supposed to work on all systems |
766 | * OS: This won't work in Courier IMAP. |
767 | * OS: According to rfc2342 response from NAMESPACE command is: |
768 | * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES) |
769 | * OS: We want to lookup all personal NAMESPACES... |
770 | */ |
1c72b151 |
771 | $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b); |
f1e6f580 |
772 | if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) { |
9c737111 |
773 | if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) { |
f1e6f580 |
774 | $pn = $data2[1]; |
9c737111 |
775 | } |
f1e6f580 |
776 | $pna = explode(')(', $pn); |
9c737111 |
777 | while (list($k, $v) = each($pna)) { |
862ff2d3 |
778 | $lst = explode('"', $v); |
779 | if (isset($lst[3])) { |
780 | $pn[$lst[1]] = $lst[3]; |
781 | } else { |
74424a43 |
782 | $pn[$lst[1]] = ''; |
862ff2d3 |
783 | } |
f1e6f580 |
784 | } |
785 | } |
786 | $sqimap_delimiter = $pn[0]; |
787 | } else { |
788 | fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n"); |
789 | $read = sqimap_read_data($imap_stream, '.', true, $a, $b); |
790 | $quote_position = strpos ($read[0], '"'); |
791 | $sqimap_delimiter = substr ($read[0], $quote_position+1, 1); |
792 | } |
793 | } |
794 | return $sqimap_delimiter; |
9c737111 |
795 | } |
052e0c26 |
796 | |
797 | |
b2306cbe |
798 | /* This is a straight copy of imap_mailbox.php:encode_mailbox_name() */ |
799 | function sqimap_encode_mailbox_name($what) |
800 | { |
801 | if (ereg("[\"\\\r\n]", $what)) |
802 | return '{' . strlen($what) . "}\r\n" . $what; /* 4.3 literal form */ |
803 | return '"' . $what . '"'; /* 4.3 quoted string form */ |
804 | } |
805 | |
806 | |
3411d4ec |
807 | /* Gets the number of messages in the current mailbox. */ |
9c737111 |
808 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
b2306cbe |
809 | $read_ary = sqimap_run_command ($imap_stream, 'EXAMINE ' . sqimap_encode_mailbox_name($mailbox), false, $result, $message); |
9c737111 |
810 | for ($i = 0; $i < count($read_ary); $i++) { |
85fc999e |
811 | if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) { |
812 | return $regs[1]; |
813 | } |
9c737111 |
814 | } |
1f720b34 |
815 | return false; //"BUG! Couldn't get number of messages in $mailbox!"; |
9c737111 |
816 | } |
817 | |
cfe1a81e |
818 | |
91688e5f |
819 | function parseAddress($address, $max=0) { |
820 | $aTokens = array(); |
821 | $aAddress = array(); |
822 | $iCnt = strlen($address); |
823 | $aSpecials = array('(' ,'<' ,',' ,';' ,':'); |
824 | $aReplace = array(' (',' <',' ,',' ;',' :'); |
825 | $address = str_replace($aSpecials,$aReplace,$address); |
bc78cc6e |
826 | $i = $iAddrFound = $bGroup = 0; |
91688e5f |
827 | while ($i < $iCnt) { |
828 | $cChar = $address{$i}; |
829 | switch($cChar) |
329a7ca5 |
830 | { |
831 | case '<': |
832 | $iEnd = strpos($address,'>',$i+1); |
833 | if (!$iEnd) { |
834 | $sToken = substr($address,$i); |
835 | $i = $iCnt; |
836 | } else { |
837 | $sToken = substr($address,$i,$iEnd - $i +1); |
838 | $i = $iEnd; |
839 | } |
840 | $sToken = str_replace($aReplace, $aSpecials,$sToken); |
841 | $aTokens[] = $sToken; |
842 | break; |
843 | case '"': |
844 | $iEnd = strpos($address,$cChar,$i+1); |
da1e42a1 |
845 | if ($iEnd) { |
846 | // skip escaped quotes |
847 | $prev_char = $address{$iEnd-1}; |
848 | while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') { |
849 | $iEnd = strpos($address,$cChar,$iEnd+1); |
850 | if ($iEnd) { |
851 | $prev_char = $address{$iEnd-1}; |
852 | } else { |
853 | $prev_char = false; |
854 | } |
855 | } |
856 | } |
329a7ca5 |
857 | if (!$iEnd) { |
858 | $sToken = substr($address,$i); |
859 | $i = $iCnt; |
860 | } else { |
861 | // also remove the surrounding quotes |
862 | $sToken = substr($address,$i+1,$iEnd - $i -1); |
863 | $i = $iEnd; |
864 | } |
865 | $sToken = str_replace($aReplace, $aSpecials,$sToken); |
76c3ee28 |
866 | if ($sToken) $aTokens[] = $sToken; |
329a7ca5 |
867 | break; |
868 | case '(': |
869 | $iEnd = strpos($address,')',$i); |
870 | if (!$iEnd) { |
871 | $sToken = substr($address,$i); |
872 | $i = $iCnt; |
873 | } else { |
874 | $sToken = substr($address,$i,$iEnd - $i + 1); |
875 | $i = $iEnd; |
876 | } |
877 | $sToken = str_replace($aReplace, $aSpecials,$sToken); |
878 | $aTokens[] = $sToken; |
879 | break; |
880 | case ',': |
bc78cc6e |
881 | ++$iAddrFound; |
329a7ca5 |
882 | case ';': |
bc78cc6e |
883 | if (!$bGroup) { |
884 | ++$iAddrFound; |
885 | } else { |
886 | $bGroup = false; |
887 | } |
888 | if ($max && $max == $iAddrFound) { |
889 | break 2; |
890 | } else { |
891 | $aTokens[] = $cChar; |
892 | break; |
893 | } |
894 | case ':': |
895 | $bGroup = true; |
329a7ca5 |
896 | case ' ': |
897 | $aTokens[] = $cChar; |
898 | break; |
91688e5f |
899 | default: |
329a7ca5 |
900 | $iEnd = strpos($address,' ',$i+1); |
901 | if ($iEnd) { |
902 | $sToken = trim(substr($address,$i,$iEnd - $i)); |
903 | $i = $iEnd-1; |
91688e5f |
904 | } else { |
329a7ca5 |
905 | $sToken = trim(substr($address,$i)); |
906 | $i = $iCnt; |
91688e5f |
907 | } |
329a7ca5 |
908 | if ($sToken) $aTokens[] = $sToken; |
91688e5f |
909 | } |
329a7ca5 |
910 | ++$i; |
9c737111 |
911 | } |
91688e5f |
912 | $sPersonal = $sEmail = $sComment = $sGroup = ''; |
913 | $aStack = $aComment = array(); |
914 | foreach ($aTokens as $sToken) { |
915 | if ($max && $max == count($aAddress)) { |
329a7ca5 |
916 | return $aAddress; |
092d4f2c |
917 | } |
329a7ca5 |
918 | $cChar = $sToken{0}; |
91688e5f |
919 | switch ($cChar) |
329a7ca5 |
920 | { |
921 | case '=': |
922 | case '"': |
923 | case ' ': |
924 | $aStack[] = $sToken; |
925 | break; |
926 | case '(': |
927 | $aComment[] = substr($sToken,1,-1); |
928 | break; |
929 | case ';': |
930 | if ($sGroup) { |
931 | $sEmail = trim(implode(' ',$aStack)); |
932 | $aAddress[] = array($sGroup,$sEmail); |
933 | $aStack = $aComment = array(); |
934 | $sGroup = ''; |
935 | break; |
936 | } |
937 | case ',': |
938 | if (!$sEmail) { |
939 | while (count($aStack) && !$sEmail) { |
940 | $sEmail = trim(array_pop($aStack)); |
941 | } |
942 | } |
943 | if (count($aStack)) { |
944 | $sPersonal = trim(implode('',$aStack)); |
945 | } else { |
946 | $sPersonal = ''; |
bea3eb1e |
947 | } |
329a7ca5 |
948 | if (!$sPersonal && count($aComment)) { |
949 | $sComment = implode(' ',$aComment); |
950 | $sPersonal .= $sComment; |
951 | } |
952 | $aAddress[] = array($sEmail,$sPersonal); |
953 | $sPersonal = $sComment = $sEmail = ''; |
954 | $aStack = $aComment = array(); |
955 | break; |
956 | case ':': |
957 | $sGroup = implode(' ',$aStack); break; |
958 | $aStack = array(); |
959 | break; |
960 | case '<': |
961 | $sEmail = trim(substr($sToken,1,-1)); |
962 | break; |
963 | case '>': |
964 | /* skip */ |
965 | break; |
966 | default: $aStack[] = $sToken; break; |
33565ec4 |
967 | } |
7e3de682 |
968 | } |
91688e5f |
969 | /* now do the action again for the last address */ |
970 | if (!$sEmail) { |
971 | while (count($aStack) && !$sEmail) { |
329a7ca5 |
972 | $sEmail = trim(array_pop($aStack)); |
91688e5f |
973 | } |
7e3de682 |
974 | } |
91688e5f |
975 | if (count($aStack)) { |
329a7ca5 |
976 | $sPersonal = trim(implode('',$aStack)); |
098ea084 |
977 | } else { |
91688e5f |
978 | $sPersonal = ''; |
9c737111 |
979 | } |
329a7ca5 |
980 | if (!$sPersonal && count($aComment)) { |
91688e5f |
981 | $sComment = implode(' ',$aComment); |
329a7ca5 |
982 | $sPersonal .= $sComment; |
91688e5f |
983 | } |
984 | $aAddress[] = array($sEmail,$sPersonal); |
985 | return $aAddress; |
986 | } |
987 | |
988 | |
85fc999e |
989 | |
9c737111 |
990 | /* |
3411d4ec |
991 | * Returns the number of unseen messages in this folder |
992 | */ |
9c737111 |
993 | function sqimap_unseen_messages ($imap_stream, $mailbox) { |
b2306cbe |
994 | $read_ary = sqimap_run_command ($imap_stream, 'STATUS ' . sqimap_encode_mailbox_name($mailbox) . ' (UNSEEN)', false, $result, $message); |
ea7ff111 |
995 | $i = 0; |
1f720b34 |
996 | $regs = array(false, false); |
ea7ff111 |
997 | while (isset($read_ary[$i])) { |
998 | if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) { |
999 | break; |
1000 | } |
1001 | $i++; |
1002 | } |
9c737111 |
1003 | return $regs[1]; |
1004 | } |
1005 | |
1f720b34 |
1006 | /* |
1007 | * Returns the number of unseen/total messages in this folder |
1008 | */ |
1009 | function sqimap_status_messages ($imap_stream, $mailbox) { |
b2306cbe |
1010 | $read_ary = sqimap_run_command ($imap_stream, 'STATUS ' . sqimap_encode_mailbox_name($mailbox) . ' (MESSAGES UNSEEN RECENT)', false, $result, $message); |
1f720b34 |
1011 | $i = 0; |
b8ec18ef |
1012 | $messages = $unseen = $recent = false; |
1f720b34 |
1013 | $regs = array(false,false); |
1014 | while (isset($read_ary[$i])) { |
1015 | if (preg_match('/UNSEEN\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
098ea084 |
1016 | $unseen = $regs[1]; |
1017 | } |
1f720b34 |
1018 | if (preg_match('/MESSAGES\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
098ea084 |
1019 | $messages = $regs[1]; |
1020 | } |
b8ec18ef |
1021 | if (preg_match('/RECENT\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
098ea084 |
1022 | $recent = $regs[1]; |
1023 | } |
1f720b34 |
1024 | $i++; |
1025 | } |
b8ec18ef |
1026 | return array('MESSAGES' => $messages, 'UNSEEN'=>$unseen, 'RECENT' => $recent); |
1f720b34 |
1027 | } |
1028 | |
9c737111 |
1029 | |
1030 | /* |
3411d4ec |
1031 | * Saves a message to a given folder -- used for saving sent messages |
1032 | */ |
9c737111 |
1033 | function sqimap_append ($imap_stream, $sent_folder, $length) { |
b2306cbe |
1034 | fputs ($imap_stream, sqimap_session_id() . ' APPEND ' . sqimap_encode_mailbox_name($sent_folder) . " (\\Seen) \{$length}\r\n"); |
85fc999e |
1035 | $tmp = fgets ($imap_stream, 1024); |
9c737111 |
1036 | } |
1037 | |
8813fb15 |
1038 | function sqimap_append_done ($imap_stream, $folder='') { |
1f720b34 |
1039 | global $squirrelmail_language, $color; |
9c737111 |
1040 | fputs ($imap_stream, "\r\n"); |
1041 | $tmp = fgets ($imap_stream, 1024); |
69146537 |
1042 | if (preg_match("/(.*)(BAD|NO)(.*)$/", $tmp, $regs)) { |
1043 | set_up_language($squirrelmail_language); |
1f720b34 |
1044 | require_once(SM_PATH . 'functions/display_messages.php'); |
098ea084 |
1045 | $reason = $regs[3]; |
1046 | if ($regs[2] == 'NO') { |
1047 | $string = "<b><font color=$color[2]>\n" . |
1048 | _("ERROR : Could not append message to") ." $folder." . |
1049 | "</b><br>\n" . |
1050 | _("Server responded: ") . |
1051 | $reason . "<br>\n"; |
1052 | if (preg_match("/(.*)(quota)(.*)$/i", $reason, $regs)) { |
1053 | $string .= _("Solution: ") . |
1054 | _("Remove unneccessary messages from your folder and start with your Trash folder.") |
1055 | ."<br>\n"; |
1056 | } |
1057 | $string .= "</font>\n"; |
1058 | error_box($string,$color); |
1059 | } else { |
8813fb15 |
1060 | $string = "<b><font color=$color[2]>\n" . |
098ea084 |
1061 | _("ERROR : Bad or malformed request.") . |
1062 | "</b><br>\n" . |
1063 | _("Server responded: ") . |
1064 | $tmp . "</font><br>\n"; |
1065 | error_box($string,$color); |
8813fb15 |
1066 | exit; |
098ea084 |
1067 | } |
69146537 |
1068 | } |
9c737111 |
1069 | } |
85fc999e |
1070 | |
bd9829d7 |
1071 | function sqimap_get_user_server ($imap_server, $username) { |
bd9829d7 |
1072 | if (substr($imap_server, 0, 4) != "map:") { |
1073 | return $imap_server; |
1074 | } |
bd9829d7 |
1075 | $function = substr($imap_server, 4); |
1076 | return $function($username); |
1077 | } |
1078 | |
1079 | /* This is an example that gets imapservers from yellowpages (NIS). |
1080 | * you can simple put map:map_yp_alias in your $imap_server_address |
1081 | * in config.php use your own function instead map_yp_alias to map your |
1082 | * LDAP whatever way to find the users imapserver. */ |
1083 | |
1084 | function map_yp_alias($username) { |
1085 | $yp = `ypmatch $username aliases`; |
1086 | return chop(substr($yp, strlen($username)+1)); |
1087 | } |
1088 | |
15e6162e |
1089 | ?> |