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