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