59177427 |
1 | <?php |
bccadd02 |
2 | |
35586184 |
3 | /** |
a6fd80f5 |
4 | * imap_general.php |
35586184 |
5 | * |
0e1a248b |
6 | * This implements all functions that do general IMAP functions. |
35586184 |
7 | * |
701e7bee |
8 | * @copyright 1999-2014 The SquirrelMail Project Team |
4b4abf93 |
9 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
eb19bc67 |
10 | * @version $Id$ |
d6c32258 |
11 | * @package squirrelmail |
eb19bc67 |
12 | * @subpackage imap |
35586184 |
13 | */ |
14 | |
d6c32258 |
15 | /** Includes.. */ |
202bcbcc |
16 | |
17 | require_once(SM_PATH . 'functions/rfc822address.php'); |
47a29326 |
18 | |
35586184 |
19 | |
48af4b64 |
20 | /** |
21 | * Generates a new session ID by incrementing the last one used; |
22 | * this ensures that each command has a unique ID. |
0e1a248b |
23 | * @param bool $unique_id (since 1.3.0) controls use of unique |
b35a5862 |
24 | * identifiers/message sequence numbers in IMAP commands. See IMAP |
25 | * rfc 'UID command' chapter. |
48af4b64 |
26 | * @return string IMAP session id of the form 'A000'. |
b35a5862 |
27 | * @since 1.2.0 |
48af4b64 |
28 | */ |
2aca19a1 |
29 | function sqimap_session_id($unique_id = FALSE) { |
30 | static $sqimap_session_id = 1; |
31 | |
487daa81 |
32 | if (!$unique_id) { |
098ea084 |
33 | return( sprintf("A%03d", $sqimap_session_id++) ); |
487daa81 |
34 | } else { |
098ea084 |
35 | return( sprintf("A%03d", $sqimap_session_id++) . ' UID' ); |
487daa81 |
36 | } |
9c737111 |
37 | } |
38 | |
48af4b64 |
39 | /** |
3411d4ec |
40 | * Both send a command and accept the result from the command. |
41 | * This is to allow proper session number handling. |
95e3afc1 |
42 | * @param stream $imap_stream imap connection resource |
b35a5862 |
43 | * @param string $query imap command |
44 | * @param boolean $handle_errors see sqimap_retrieve_imap_response() |
95e3afc1 |
45 | * @param array $response |
46 | * @param array $message |
47 | * @param boolean $unique_id (since 1.3.0) see sqimap_session_id(). |
0e1a248b |
48 | * @return mixed returns false on imap error. displays error message |
b35a5862 |
49 | * if imap stream is not available. |
50 | * @since 1.2.3 |
3411d4ec |
51 | */ |
487daa81 |
52 | function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) { |
c5809184 |
53 | if ($imap_stream) { |
098ea084 |
54 | $sid = sqimap_session_id($unique_id); |
55 | fputs ($imap_stream, $sid . ' ' . $query . "\r\n"); |
bc78cc6e |
56 | $tag_uid_a = explode(' ',trim($sid)); |
57 | $tag = $tag_uid_a[0]; |
0dc05a81 |
58 | $read = sqimap_retrieve_imap_response ($imap_stream, $tag, $handle_errors, $response, $message, $query ); |
bc78cc6e |
59 | /* get the response and the message */ |
60 | $message = $message[$tag]; |
1e0a2f0e |
61 | $response = $response[$tag]; |
bc78cc6e |
62 | return $read[$tag]; |
426815a3 |
63 | //FIXME: obey $handle_errors below! |
c5809184 |
64 | } else { |
65 | global $squirrelmail_language, $color; |
66 | set_up_language($squirrelmail_language); |
b729dba8 |
67 | //FIXME: NO HTML IN CORE! |
6fd95361 |
68 | $string = "<b><font color=\"$color[2]\">\n" . |
0e1a248b |
69 | _("ERROR: No available IMAP stream.") . |
b729dba8 |
70 | //FIXME: NO HTML IN CORE! |
c5809184 |
71 | "</b></font>\n"; |
1b858d86 |
72 | error_box($string); |
098ea084 |
73 | return false; |
c5809184 |
74 | } |
1c72b151 |
75 | } |
76 | |
b35a5862 |
77 | /** |
95e3afc1 |
78 | * @param stream $imap_stream imap connection resource |
b35a5862 |
79 | * @param string $query imap command |
80 | * @param boolean $handle_errors see sqimap_retrieve_imap_response() |
95e3afc1 |
81 | * @param array $response empty string, if return = false |
82 | * @param array $message empty string, if return = false |
83 | * @param boolean $unique_id (since 1.3.0) see sqimap_session_id() |
84 | * @param boolean $filter (since 1.4.1 and 1.5.0) see sqimap_fread() |
85 | * @param mixed $outputstream (since 1.4.1 and 1.5.0) see sqimap_fread() |
86 | * @param boolean $no_return (since 1.4.1 and 1.5.0) see sqimap_fread() |
0e1a248b |
87 | * @return mixed returns false on imap error. displays error message |
b35a5862 |
88 | * if imap stream is not available. |
89 | * @since 1.2.3 |
90 | */ |
1e0a2f0e |
91 | function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, |
7c7b74b3 |
92 | &$message, $unique_id = false,$filter=false, |
93 | $outputstream=false,$no_return=false) { |
c5809184 |
94 | if ($imap_stream) { |
95 | $sid = sqimap_session_id($unique_id); |
1e0a2f0e |
96 | fputs ($imap_stream, $sid . ' ' . $query . "\r\n"); |
bc78cc6e |
97 | $tag_uid_a = explode(' ',trim($sid)); |
98 | $tag = $tag_uid_a[0]; |
1e0a2f0e |
99 | |
bc78cc6e |
100 | $read = sqimap_read_data ($imap_stream, $tag, $handle_errors, $response, |
7c7b74b3 |
101 | $message, $query,$filter,$outputstream,$no_return); |
0e1a248b |
102 | if (empty($read)) { //IMAP server dropped its connection |
ec73f243 |
103 | $response = ''; |
104 | $message = ''; |
105 | return false; |
106 | } |
bc78cc6e |
107 | /* retrieve the response and the message */ |
108 | $response = $response[$tag]; |
109 | $message = $message[$tag]; |
1e0a2f0e |
110 | |
bc78cc6e |
111 | if (!empty($read[$tag])) { |
8439f61d |
112 | /* sqimap_read_data should be called for one response |
113 | but since it just calls sqimap_retrieve_imap_response |
114 | which handles multiple responses we need to check for |
115 | that and merge the $read[$tag] array IF they are |
116 | separated and IF it was a FETCH response. */ |
117 | |
118 | if (isset($read[$tag][1]) && is_array($read[$tag][1]) && isset($read[$tag][1][0]) |
119 | && preg_match('/^\* \d+ FETCH/', $read[$tag][1][0])) { |
120 | $result = array(); |
121 | foreach($read[$tag] as $index => $value) { |
122 | $result = array_merge($result, $read[$tag]["$index"]); |
123 | } |
124 | return $result; |
125 | } |
126 | |
bc78cc6e |
127 | return $read[$tag][0]; |
128 | } else { |
129 | return $read[$tag]; |
130 | } |
5adca076 |
131 | //FIXME: obey $handle_errors below! |
c5809184 |
132 | } else { |
133 | global $squirrelmail_language, $color; |
134 | set_up_language($squirrelmail_language); |
b729dba8 |
135 | //FIXME: NO HTML IN CORE! |
6fd95361 |
136 | $string = "<b><font color=\"$color[2]\">\n" . |
0e1a248b |
137 | _("ERROR: No available IMAP stream.") . |
b729dba8 |
138 | //FIXME: NO HTML IN CORE! |
c5809184 |
139 | "</b></font>\n"; |
1b858d86 |
140 | error_box($string); |
098ea084 |
141 | return false; |
1e0a2f0e |
142 | } |
bc78cc6e |
143 | } |
48af4b64 |
144 | |
0022f6db |
145 | /** |
146 | * @param mixed $new_query |
147 | * @param string $tag |
148 | * @param array $aQuery |
95e3afc1 |
149 | * @param boolean $unique_id see sqimap_session_id() |
0022f6db |
150 | * @since 1.5.0 |
151 | */ |
bc78cc6e |
152 | function sqimap_prepare_pipelined_query($new_query,&$tag,&$aQuery,$unique_id) { |
153 | $sid = sqimap_session_id($unique_id); |
154 | $tag_uid_a = explode(' ',trim($sid)); |
155 | $tag = $tag_uid_a[0]; |
0b38d197 |
156 | $query = $sid . ' '.$new_query."\r\n"; |
bc78cc6e |
157 | $aQuery[$tag] = $query; |
42a07ac1 |
158 | } |
159 | |
0022f6db |
160 | /** |
95e3afc1 |
161 | * @param stream $imap_stream imap stream |
0022f6db |
162 | * @param array $aQueryList |
163 | * @param boolean $handle_errors |
164 | * @param array $aServerResponse |
165 | * @param array $aServerMessage |
95e3afc1 |
166 | * @param boolean $unique_id see sqimap_session_id() |
167 | * @param boolean $filter see sqimap_fread() |
168 | * @param mixed $outputstream see sqimap_fread() |
169 | * @param boolean $no_return see sqimap_fread() |
0022f6db |
170 | * @since 1.5.0 |
171 | */ |
1e0a2f0e |
172 | function sqimap_run_pipelined_command ($imap_stream, $aQueryList, $handle_errors, |
bc78cc6e |
173 | &$aServerResponse, &$aServerMessage, $unique_id = false, |
1e0a2f0e |
174 | $filter=false,$outputstream=false,$no_return=false) { |
bc78cc6e |
175 | $aResponse = false; |
5c300c60 |
176 | |
1e0a2f0e |
177 | /* |
0e1a248b |
178 | Do not fire all calls at once to the IMAP server but split the calls up |
1e0a2f0e |
179 | in portions of $iChunkSize. If we do not do that I think we misbehave as |
0e1a248b |
180 | IMAP client or should handle BYE calls if the IMAP server drops the |
52c8b585 |
181 | connection because the number of queries is to large. This isn't tested |
182 | but a wild guess how it could work in the field. |
1e0a2f0e |
183 | |
184 | After testing it on Exchange 2000 we discovered that a chunksize of 32 |
0b38d197 |
185 | was quicker then when we raised it to 128. |
52c8b585 |
186 | */ |
187 | $iQueryCount = count($aQueryList); |
188 | $iChunkSize = 32; |
189 | // array_chunk would also do the job but it's supported from php > 4.2 |
190 | $aQueryChunks = array(); |
191 | $iLoops = floor($iQueryCount / $iChunkSize); |
192 | |
5c300c60 |
193 | if ($iLoops * $iChunkSize != $iQueryCount) ++$iLoops; |
52c8b585 |
194 | |
195 | if (!function_exists('array_chunk')) { // arraychunk replacement |
5c300c60 |
196 | reset($aQueryList); |
52c8b585 |
197 | for($i=0;$i<$iLoops;++$i) { |
5c300c60 |
198 | for($j=0;$j<$iChunkSize;++$j) { |
199 | $key = key($aQueryList); |
200 | $aTmp[$key] = $aQueryList[$key]; |
201 | if (next($aQueryList) === false) break; |
202 | } |
203 | $aQueryChunks[] = $aTmp; |
1e0a2f0e |
204 | } |
52c8b585 |
205 | } else { |
206 | $aQueryChunks = array_chunk($aQueryList,$iChunkSize,true); |
bc78cc6e |
207 | } |
1e0a2f0e |
208 | |
52c8b585 |
209 | for ($i=0;$i<$iLoops;++$i) { |
210 | $aQuery = $aQueryChunks[$i]; |
211 | foreach($aQuery as $tag => $query) { |
212 | fputs($imap_stream,$query); |
213 | $aResults[$tag] = false; |
214 | } |
52c8b585 |
215 | foreach($aQuery as $tag => $query) { |
5c300c60 |
216 | if ($aResults[$tag] == false) { |
1e0a2f0e |
217 | $aReturnedResponse = sqimap_retrieve_imap_response ($imap_stream, $tag, |
bc78cc6e |
218 | $handle_errors, $response, $message, $query, |
219 | $filter,$outputstream,$no_return); |
52c8b585 |
220 | foreach ($aReturnedResponse as $returned_tag => $aResponse) { |
5c300c60 |
221 | if (!empty($aResponse)) { |
52c8b585 |
222 | $aResults[$returned_tag] = $aResponse[0]; |
5c300c60 |
223 | } else { |
224 | $aResults[$returned_tag] = $aResponse; |
225 | } |
52c8b585 |
226 | $aServerResponse[$returned_tag] = $response[$returned_tag]; |
227 | $aServerMessage[$returned_tag] = $message[$returned_tag]; |
228 | } |
bc78cc6e |
229 | } |
230 | } |
231 | } |
b7277e4f |
232 | return $aResults; |
bc78cc6e |
233 | } |
9c737111 |
234 | |
1e0a2f0e |
235 | /** |
0e1a248b |
236 | * Custom fgets function: gets a line from the IMAP server, |
48af4b64 |
237 | * no matter how big it may be. |
95e3afc1 |
238 | * @param stream $imap_stream the stream to read from |
48af4b64 |
239 | * @return string a line |
0022f6db |
240 | * @since 1.2.8 |
b8c285ab |
241 | */ |
b8c285ab |
242 | function sqimap_fgets($imap_stream) { |
243 | $read = ''; |
244 | $buffer = 4096; |
245 | $results = ''; |
c41daf03 |
246 | $offset = 0; |
247 | while (strpos($results, "\r\n", $offset) === false) { |
b8c285ab |
248 | if (!($read = fgets($imap_stream, $buffer))) { |
329a7ca5 |
249 | /* this happens in case of an error */ |
250 | /* reset $results because it's useless */ |
251 | $results = false; |
b8c285ab |
252 | break; |
253 | } |
c41daf03 |
254 | if ( $results != '' ) { |
255 | $offset = strlen($results) - 1; |
256 | } |
b8c285ab |
257 | $results .= $read; |
258 | } |
259 | return $results; |
260 | } |
261 | |
0022f6db |
262 | /** |
263 | * @param stream $imap_stream |
264 | * @param integer $iSize |
95e3afc1 |
265 | * @param boolean $filter |
266 | * @param mixed $outputstream stream or 'php://stdout' string |
0e1a248b |
267 | * @param boolean $no_return controls data returned by function |
0022f6db |
268 | * @return string |
269 | * @since 1.4.1 |
270 | */ |
7c7b74b3 |
271 | function sqimap_fread($imap_stream,$iSize,$filter=false, |
272 | $outputstream=false, $no_return=false) { |
273 | if (!$filter || !$outputstream) { |
274 | $iBufferSize = $iSize; |
275 | } else { |
811318c5 |
276 | // see php bug 24033. They changed fread behaviour %$^&$% |
977a6085 |
277 | $iBufferSize = 7800; // multiple of 78 in case of base64 decoding. |
278 | } |
279 | if ($iSize < $iBufferSize) { |
280 | $iBufferSize = $iSize; |
7c7b74b3 |
281 | } |
7c0ec1d8 |
282 | |
5eba0be2 |
283 | $iRetrieved = 0; |
977a6085 |
284 | $results = ''; |
285 | $sRead = $sReadRem = ''; |
1e0a2f0e |
286 | // NB: fread can also stop at end of a packet on sockets. |
977a6085 |
287 | while ($iRetrieved < $iSize) { |
7c7b74b3 |
288 | $sRead = fread($imap_stream,$iBufferSize); |
977a6085 |
289 | $iLength = strlen($sRead); |
290 | $iRetrieved += $iLength ; |
291 | $iRemaining = $iSize - $iRetrieved; |
292 | if ($iRemaining < $iBufferSize) { |
293 | $iBufferSize = $iRemaining; |
294 | } |
13aabbcf |
295 | if ($sRead == '') { |
7c7b74b3 |
296 | $results = false; |
297 | break; |
298 | } |
13aabbcf |
299 | if ($sReadRem != '') { |
977a6085 |
300 | $sRead = $sReadRem . $sRead; |
301 | $sReadRem = ''; |
302 | } |
7c0ec1d8 |
303 | |
13aabbcf |
304 | if ($filter && $sRead != '') { |
1e0a2f0e |
305 | // in case the filter is base64 decoding we return a remainder |
7c0ec1d8 |
306 | $sReadRem = $filter($sRead); |
7c7b74b3 |
307 | } |
13aabbcf |
308 | if ($outputstream && $sRead != '') { |
7c7b74b3 |
309 | if (is_resource($outputstream)) { |
310 | fwrite($outputstream,$sRead); |
311 | } else if ($outputstream == 'php://stdout') { |
312 | echo $sRead; |
313 | } |
314 | } |
315 | if ($no_return) { |
316 | $sRead = ''; |
1e0a2f0e |
317 | } else { |
977a6085 |
318 | $results .= $sRead; |
7c7b74b3 |
319 | } |
7c7b74b3 |
320 | } |
1e0a2f0e |
321 | return $results; |
322 | } |
977a6085 |
323 | |
7c0ec1d8 |
324 | |
48af4b64 |
325 | /** |
326 | * Obsolete function, inform plugins that use it |
95e3afc1 |
327 | * @param stream $imap_stream |
328 | * @param string $tag |
329 | * @param boolean $handle_errors |
330 | * @param array $response |
331 | * @param array $message |
332 | * @param string $query |
b35a5862 |
333 | * @since 1.1.3 |
334 | * @deprecated (since 1.5.0) use sqimap_run_command or sqimap_run_command_list instead |
48af4b64 |
335 | */ |
1e0a2f0e |
336 | function sqimap_read_data_list($imap_stream, $tag, $handle_errors, |
0dc05a81 |
337 | &$response, &$message, $query = '') { |
1b858d86 |
338 | global $color, $oTemplate, $squirrelmail_language; |
0dc05a81 |
339 | set_up_language($squirrelmail_language); |
b729dba8 |
340 | //FIXME: NO HTML IN CORE! |
6fd95361 |
341 | $string = "<b><font color=\"$color[2]\">\n" . |
0e1a248b |
342 | _("ERROR: Bad function call.") . |
b729dba8 |
343 | //FIXME: NO HTML IN CORE! |
6fd95361 |
344 | "</b><br />\n" . |
0dc05a81 |
345 | _("Reason:") . ' '. |
6fd95361 |
346 | 'There is a plugin installed which make use of the <br />' . |
347 | 'SquirrelMail internal function sqimap_read_data_list.<br />'. |
348 | 'Please adapt the installed plugin and let it use<br />'. |
349 | 'sqimap_run_command or sqimap_run_command_list instead<br /><br />'. |
350 | 'The following query was issued:<br />'. |
b729dba8 |
351 | //FIXME: NO HTML IN CORE! |
3047e291 |
352 | sm_encode_html_special_chars($query) . '<br />' . "</font><br />\n"; |
1b858d86 |
353 | error_box($string); |
354 | $oTemplate->display('footer.tpl'); |
1e0a2f0e |
355 | exit; |
0dc05a81 |
356 | } |
7c7b74b3 |
357 | |
48af4b64 |
358 | /** |
0e1a248b |
359 | * Function to display an error related to an IMAP query. |
48af4b64 |
360 | * @param string title the caption of the error box |
361 | * @param string query the query that went wrong |
ec73f243 |
362 | * @param string message_title optional message title |
363 | * @param string message optional error message |
364 | * @param string $link an optional link to try again |
48af4b64 |
365 | * @return void |
0022f6db |
366 | * @since 1.5.0 |
48af4b64 |
367 | */ |
ec73f243 |
368 | function sqimap_error_box($title, $query = '', $message_title = '', $message = '', $link = '') |
4974c2a0 |
369 | { |
370 | global $color, $squirrelmail_language; |
371 | |
372 | set_up_language($squirrelmail_language); |
b729dba8 |
373 | //FIXME: NO HTML IN CORE! |
6fd95361 |
374 | $string = "<font color=\"$color[2]\"><b>\n" . $title . "</b><br />\n"; |
bf15f116 |
375 | $cmd = explode(' ',$query); |
376 | $cmd= strtolower($cmd[0]); |
377 | |
378 | if ($query != '' && $cmd != 'login') |
3047e291 |
379 | $string .= _("Query:") . ' ' . sm_encode_html_special_chars($query) . '<br />'; |
4974c2a0 |
380 | if ($message_title != '') |
381 | $string .= $message_title; |
382 | if ($message != '') |
3047e291 |
383 | $string .= sm_encode_html_special_chars($message); |
b729dba8 |
384 | //FIXME: NO HTML IN CORE! |
6fd95361 |
385 | $string .= "</font><br />\n"; |
ec73f243 |
386 | if ($link != '') |
387 | $string .= $link; |
1b858d86 |
388 | error_box($string); |
4974c2a0 |
389 | } |
390 | |
48af4b64 |
391 | /** |
3411d4ec |
392 | * Reads the output from the IMAP stream. If handle_errors is set to true, |
393 | * this will also handle all errors that are received. If it is not set, |
48af4b64 |
394 | * the errors will be sent back through $response and $message. |
95e3afc1 |
395 | * @param stream $imap_stream imap stream |
396 | * @param string $tag |
397 | * @param boolean $handle_errors handle errors internally or send them in $response and $message. |
0022f6db |
398 | * @param array $response |
399 | * @param array $message |
95e3afc1 |
400 | * @param string $query command that can be printed if something fails |
401 | * @param boolean $filter see sqimap_fread() |
402 | * @param mixed $outputstream see sqimap_fread() |
403 | * @param boolean $no_return see sqimap_fread() |
0022f6db |
404 | * @since 1.5.0 |
bee165ef |
405 | */ |
1e0a2f0e |
406 | function sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors, |
7c7b74b3 |
407 | &$response, &$message, $query = '', |
408 | $filter = false, $outputstream = false, $no_return = false) { |
9c737111 |
409 | global $color, $squirrelmail_language; |
9c737111 |
410 | $read = ''; |
bc78cc6e |
411 | if (!is_array($message)) $message = array(); |
412 | if (!is_array($response)) $response = array(); |
ec73f243 |
413 | $aResponse = ''; |
b8c285ab |
414 | $resultlist = array(); |
415 | $data = array(); |
ee231c8b |
416 | $sCommand = ''; |
417 | if (preg_match("/^(\w+)\s*/",$query,$aMatch)) { |
418 | $sCommand = strtoupper($aMatch[1]); |
419 | } else { |
420 | // error reporting (shouldn't happen) |
421 | } |
b8c285ab |
422 | $read = sqimap_fgets($imap_stream); |
8d8da447 |
423 | $i = 0; |
bea3eb1e |
424 | while ($read) { |
425 | $char = $read{0}; |
426 | switch ($char) |
427 | { |
15bc7f66 |
428 | case '+': |
429 | default: |
430 | $read = sqimap_fgets($imap_stream); |
431 | break; |
432 | |
433 | case $tag{0}: |
434 | { |
bea3eb1e |
435 | /* get the command */ |
436 | $arg = ''; |
437 | $i = strlen($tag)+1; |
438 | $s = substr($read,$i); |
439 | if (($j = strpos($s,' ')) || ($j = strpos($s,"\n"))) { |
440 | $arg = substr($s,0,$j); |
441 | } |
442 | $found_tag = substr($read,0,$i-1); |
5c300c60 |
443 | if ($found_tag) { |
bea3eb1e |
444 | switch ($arg) |
445 | { |
15bc7f66 |
446 | case 'OK': |
447 | case 'BAD': |
448 | case 'NO': |
449 | case 'BYE': |
450 | case 'PREAUTH': |
bc78cc6e |
451 | $response[$found_tag] = $arg; |
452 | $message[$found_tag] = trim(substr($read,$i+strlen($arg))); |
453 | if (!empty($data)) { |
454 | $resultlist[] = $data; |
455 | } |
5c300c60 |
456 | $aResponse[$found_tag] = $resultlist; |
457 | $data = $resultlist = array(); |
458 | if ($found_tag == $tag) { |
459 | break 3; /* switch switch while */ |
460 | } |
461 | break; |
1e0a2f0e |
462 | default: |
bea3eb1e |
463 | /* this shouldn't happen */ |
bc78cc6e |
464 | $response[$found_tag] = $arg; |
465 | $message[$found_tag] = trim(substr($read,$i+strlen($arg))); |
466 | if (!empty($data)) { |
467 | $resultlist[] = $data; |
468 | } |
469 | $aResponse[$found_tag] = $resultlist; |
5c300c60 |
470 | $data = $resultlist = array(); |
471 | if ($found_tag == $tag) { |
472 | break 3; /* switch switch while */ |
473 | } |
bc78cc6e |
474 | } |
bea3eb1e |
475 | } |
5c300c60 |
476 | $read = sqimap_fgets($imap_stream); |
477 | if ($read === false) { /* error */ |
4920e1e0 |
478 | break 2; /* switch while */ |
5c300c60 |
479 | } |
480 | break; |
15bc7f66 |
481 | } // end case $tag{0} |
482 | |
483 | case '*': |
484 | { |
202bcbcc |
485 | if (($sCommand == "FETCH" || $sCommand == "STORE") && preg_match('/^\*\s\d+\sFETCH/',$read)) { |
bea3eb1e |
486 | /* check for literal */ |
487 | $s = substr($read,-3); |
488 | $fetch_data = array(); |
489 | do { /* outer loop, continue until next untagged fetch |
490 | or tagged reponse */ |
491 | do { /* innerloop for fetching literals. with this loop |
492 | we prohibid that literal responses appear in the |
493 | outer loop so we can trust the untagged and |
494 | tagged info provided by $read */ |
935d09e1 |
495 | $read_literal = false; |
bea3eb1e |
496 | if ($s === "}\r\n") { |
497 | $j = strrpos($read,'{'); |
498 | $iLit = substr($read,$j+1,-3); |
499 | $fetch_data[] = $read; |
7c7b74b3 |
500 | $sLiteral = sqimap_fread($imap_stream,$iLit,$filter,$outputstream,$no_return); |
329a7ca5 |
501 | if ($sLiteral === false) { /* error */ |
502 | break 4; /* while while switch while */ |
503 | } |
bea3eb1e |
504 | /* backwards compattibility */ |
505 | $aLiteral = explode("\n", $sLiteral); |
506 | /* release not neaded data */ |
507 | unset($sLiteral); |
508 | foreach ($aLiteral as $line) { |
509 | $fetch_data[] = $line ."\n"; |
510 | } |
511 | /* release not neaded data */ |
1e0a2f0e |
512 | unset($aLiteral); |
bea3eb1e |
513 | /* next fgets belongs to this fetch because |
514 | we just got the exact literalsize and there |
515 | must follow data to complete the response */ |
329a7ca5 |
516 | $read = sqimap_fgets($imap_stream); |
517 | if ($read === false) { /* error */ |
518 | break 4; /* while while switch while */ |
519 | } |
935d09e1 |
520 | $s = substr($read,-3); |
521 | $read_literal = true; |
522 | continue; |
bea3eb1e |
523 | } else { |
329a7ca5 |
524 | $fetch_data[] = $read; |
bea3eb1e |
525 | } |
526 | /* retrieve next line and check in the while |
527 | statements if it belongs to this fetch response */ |
528 | $read = sqimap_fgets($imap_stream); |
329a7ca5 |
529 | if ($read === false) { /* error */ |
530 | break 4; /* while while switch while */ |
531 | } |
bea3eb1e |
532 | /* check for next untagged reponse and break */ |
533 | if ($read{0} == '*') break 2; |
534 | $s = substr($read,-3); |
935d09e1 |
535 | } while ($s === "}\r\n" || $read_literal); |
bea3eb1e |
536 | $s = substr($read,-3); |
537 | } while ($read{0} !== '*' && |
106d4087 |
538 | substr($read,0,strlen($tag)) !== $tag); |
329a7ca5 |
539 | $resultlist[] = $fetch_data; |
bea3eb1e |
540 | /* release not neaded data */ |
541 | unset ($fetch_data); |
542 | } else { |
543 | $s = substr($read,-3); |
544 | do { |
545 | if ($s === "}\r\n") { |
546 | $j = strrpos($read,'{'); |
547 | $iLit = substr($read,$j+1,-3); |
9c093ecf |
548 | // check for numeric value to avoid that untagged responses like: |
549 | // * OK [PARSE] Unexpected characters at end of address: {SET:debug=51} |
550 | // will trigger literal fetching ({SET:debug=51} !== int ) |
551 | if (is_numeric($iLit)) { |
552 | $data[] = $read; |
553 | $sLiteral = fread($imap_stream,$iLit); |
554 | if ($sLiteral === false) { /* error */ |
555 | $read = false; |
556 | break 3; /* while switch while */ |
557 | } |
558 | $data[] = $sLiteral; |
559 | $data[] = sqimap_fgets($imap_stream); |
560 | } else { |
561 | $data[] = $read; |
329a7ca5 |
562 | } |
bea3eb1e |
563 | } else { |
329a7ca5 |
564 | $data[] = $read; |
bea3eb1e |
565 | } |
566 | $read = sqimap_fgets($imap_stream); |
329a7ca5 |
567 | if ($read === false) { |
568 | break 3; /* while switch while */ |
569 | } else if ($read{0} == '*') { |
106d4087 |
570 | break; |
571 | } |
bea3eb1e |
572 | $s = substr($read,-3); |
573 | } while ($s === "}\r\n"); |
574 | break 1; |
1e0a2f0e |
575 | } |
bea3eb1e |
576 | break; |
15bc7f66 |
577 | } // end case '*' |
578 | } // end switch |
c0b23345 |
579 | } // end while |
1e0a2f0e |
580 | |
c0b23345 |
581 | /* error processing in case $read is false */ |
d7542838 |
582 | if ($read === false) { |
d3d9ef2d |
583 | // try to retrieve an untagged bye respons from the results |
584 | $sResponse = array_pop($data); |
d7542838 |
585 | if ($sResponse !== NULL && strpos($sResponse,'* BYE') !== false) { |
586 | if (!$handle_errors) { |
587 | $query = ''; |
588 | } |
0e1a248b |
589 | sqimap_error_box(_("ERROR: IMAP server closed the connection."), $query, _("Server responded:"),$sResponse); |
b729dba8 |
590 | //FIXME: NO HTML IN CORE! |
d7542838 |
591 | echo '</body></html>'; |
592 | exit; |
593 | } else if ($handle_errors) { |
1e0a2f0e |
594 | unset($data); |
0e1a248b |
595 | sqimap_error_box(_("ERROR: Connection dropped by IMAP server."), $query); |
ec73f243 |
596 | exit; |
597 | } |
b8c285ab |
598 | } |
1e0a2f0e |
599 | |
15bc7f66 |
600 | /* Set $resultlist array */ |
b8c285ab |
601 | if (!empty($data)) { |
bc78cc6e |
602 | //$resultlist[] = $data; |
9c737111 |
603 | } |
b8c285ab |
604 | elseif (empty($resultlist)) { |
1e0a2f0e |
605 | $resultlist[] = array(); |
b8c285ab |
606 | } |
15bc7f66 |
607 | |
608 | /* Return result or handle errors */ |
bee165ef |
609 | if ($handle_errors == false) { |
bc78cc6e |
610 | return $aResponse; |
dd381002 |
611 | } |
4974c2a0 |
612 | switch ($response[$tag]) { |
dd381002 |
613 | case 'OK': |
bc78cc6e |
614 | return $aResponse; |
329a7ca5 |
615 | break; |
1e0a2f0e |
616 | case 'NO': |
dd381002 |
617 | /* ignore this error from M$ exchange, it is not fatal (aka bug) */ |
6b6c2e06 |
618 | if (strstr($message[$tag], 'command resulted in') === false) { |
0e1a248b |
619 | sqimap_error_box(_("ERROR: Could not complete request."), $query, _("Reason Given:") . ' ', $message[$tag]); |
329a7ca5 |
620 | echo '</body></html>'; |
052e0c26 |
621 | exit; |
9c737111 |
622 | } |
329a7ca5 |
623 | break; |
1e0a2f0e |
624 | case 'BAD': |
0e1a248b |
625 | sqimap_error_box(_("ERROR: Bad or malformed request."), $query, _("Server responded:") . ' ', $message[$tag]); |
b729dba8 |
626 | //FIXME: NO HTML IN CORE! |
1e0a2f0e |
627 | echo '</body></html>'; |
628 | exit; |
629 | case 'BYE': |
0e1a248b |
630 | sqimap_error_box(_("ERROR: IMAP server closed the connection."), $query, _("Server responded:") . ' ', $message[$tag]); |
b729dba8 |
631 | //FIXME: NO HTML IN CORE! |
1e0a2f0e |
632 | echo '</body></html>'; |
9c737111 |
633 | exit; |
1e0a2f0e |
634 | default: |
0e1a248b |
635 | sqimap_error_box(_("ERROR: Unknown IMAP response."), $query, _("Server responded:") . ' ', $message[$tag]); |
329a7ca5 |
636 | /* the error is displayed but because we don't know the reponse we |
637 | return the result anyway */ |
1e0a2f0e |
638 | return $aResponse; |
329a7ca5 |
639 | break; |
9c737111 |
640 | } |
9c737111 |
641 | } |
642 | |
0022f6db |
643 | /** |
95e3afc1 |
644 | * @param stream $imap_stream imap string |
0022f6db |
645 | * @param string $tag_uid |
646 | * @param boolean $handle_errors |
647 | * @param array $response |
648 | * @param array $message |
95e3afc1 |
649 | * @param string $query (since 1.2.5) |
650 | * @param boolean $filter (since 1.4.1) see sqimap_fread() |
651 | * @param mixed $outputstream (since 1.4.1) see sqimap_fread() |
652 | * @param boolean $no_return (since 1.4.1) see sqimap_fread() |
0022f6db |
653 | */ |
1e0a2f0e |
654 | function sqimap_read_data ($imap_stream, $tag_uid, $handle_errors, |
7c7b74b3 |
655 | &$response, &$message, $query = '', |
656 | $filter=false,$outputstream=false,$no_return=false) { |
bc78cc6e |
657 | |
658 | $tag_uid_a = explode(' ',trim($tag_uid)); |
659 | $tag = $tag_uid_a[0]; |
660 | |
1e0a2f0e |
661 | $res = sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors, |
662 | $response, $message, $query,$filter,$outputstream,$no_return); |
663 | return $res; |
9c737111 |
664 | } |
665 | |
48af4b64 |
666 | /** |
b98732e8 |
667 | * Connects to the IMAP server and returns a resource identifier for use with |
a15f9d93 |
668 | * the other SquirrelMail IMAP functions. Does NOT login! |
48af4b64 |
669 | * @param string server hostname of IMAP server |
670 | * @param int port port number to connect to |
a15f9d93 |
671 | * @param integer $tls whether to use plain text(0), TLS(1) or STARTTLS(2) when connecting. |
672 | * Argument was boolean before 1.5.1. |
9aaa9ae2 |
673 | * @param array $ssl_options SSL context options, see config_local.php |
674 | * for more details (OPTIONAL) |
48af4b64 |
675 | * @return imap-stream resource identifier |
0022f6db |
676 | * @since 1.5.0 (usable only in 1.5.1 or later) |
b98732e8 |
677 | */ |
9aaa9ae2 |
678 | function sqimap_create_stream($server,$port,$tls=0,$ssl_options=array()) { |
ce68b76b |
679 | global $squirrelmail_language; |
b98732e8 |
680 | |
abc4a428 |
681 | if (strstr($server,':') && ! preg_match("/^\[.*\]$/",$server)) { |
682 | // numerical IPv6 address must be enclosed in square brackets |
683 | $server = '['.$server.']'; |
684 | } |
a15f9d93 |
685 | |
9aaa9ae2 |
686 | // NB: Using "ssl://" ensures the highest possible TLS version |
687 | // will be negotiated with the server (whereas "tls://" only |
688 | // uses TLS version 1.0) |
689 | // |
a15f9d93 |
690 | if ($tls == 1) { |
b98732e8 |
691 | if ((check_php_version(4,3)) and (extension_loaded('openssl'))) { |
9aaa9ae2 |
692 | if (function_exists('stream_socket_client')) { |
693 | $server_address = 'ssl://' . $server . ':' . $port; |
694 | if (!empty($ssl_options)) |
695 | $ssl_options = array('ssl' => $ssl_options); |
696 | $ssl_context = @stream_context_create($ssl_options); |
697 | $connect_timeout = ini_get('default_socket_timeout'); |
698 | // null timeout is broken |
699 | if ($connect_timeout == 0) |
700 | $connect_timeout = 15; |
701 | $imap_stream = @stream_socket_client($server_address, $error_number, $error_string, $connect_timeout, STREAM_CLIENT_CONNECT, $ssl_context); |
702 | } else { |
703 | $imap_stream = @fsockopen('ssl://' . $server, $port, $error_number, $error_string, 15); |
704 | } |
b98732e8 |
705 | } else { |
706 | require_once(SM_PATH . 'functions/display_messages.php'); |
3b151851 |
707 | logout_error( sprintf(_("Error connecting to IMAP server: %s."), $server). |
708 | '<br />'. |
709 | _("TLS is enabled, but this version of PHP does not support TLS sockets, or is missing the openssl extension."). |
710 | '<br /><br />'. |
0a6be7d1 |
711 | _("Please contact your system administrator and report this error."), |
abc4a428 |
712 | sprintf(_("Error connecting to IMAP server: %s."), $server)); |
b98732e8 |
713 | } |
9aaa9ae2 |
714 | } else { |
715 | $imap_stream = @fsockopen($server, $port, $error_number, $error_string, 15); |
b98732e8 |
716 | } |
717 | |
b98732e8 |
718 | |
719 | /* Do some error correction */ |
720 | if (!$imap_stream) { |
721 | set_up_language($squirrelmail_language, true); |
722 | require_once(SM_PATH . 'functions/display_messages.php'); |
3b151851 |
723 | logout_error( sprintf(_("Error connecting to IMAP server: %s."), $server). |
b729dba8 |
724 | //FIXME: NO HTML IN CORE! |
0a6be7d1 |
725 | "<br />\r\n$error_number : $error_string<br />\r\n", |
abc4a428 |
726 | sprintf(_("Error connecting to IMAP server: %s."), $server) ); |
b98732e8 |
727 | exit; |
728 | } |
729 | $server_info = fgets ($imap_stream, 1024); |
a15f9d93 |
730 | |
731 | /** |
732 | * Implementing IMAP STARTTLS (rfc2595) in php 5.1.0+ |
733 | * http://www.php.net/stream-socket-enable-crypto |
734 | */ |
c4de9863 |
735 | if ($tls === 2) { |
a15f9d93 |
736 | if (function_exists('stream_socket_enable_crypto')) { |
737 | // check starttls capability, don't use cached capability version |
738 | if (! sqimap_capability($imap_stream, 'STARTTLS', false)) { |
739 | // imap server does not declare starttls support |
740 | sqimap_error_box(sprintf(_("Error connecting to IMAP server: %s."), $server), |
741 | '','', |
742 | _("IMAP STARTTLS is enabled in SquirrelMail configuration, but used IMAP server does not support STARTTLS.")); |
743 | exit; |
744 | } |
745 | |
746 | // issue starttls command and check response |
747 | sqimap_run_command($imap_stream, 'STARTTLS', false, $starttls_response, $starttls_message); |
748 | // check response |
749 | if ($starttls_response!='OK') { |
750 | // starttls command failed |
751 | sqimap_error_box(sprintf(_("Error connecting to IMAP server: %s."), $server), |
752 | 'STARTTLS', |
3c021d16 |
753 | _("Server replied:") . ' ', |
a15f9d93 |
754 | $starttls_message); |
755 | exit(); |
756 | } |
757 | |
758 | // start crypto on connection. suppress function errors. |
759 | if (@stream_socket_enable_crypto($imap_stream,true,STREAM_CRYPTO_METHOD_TLS_CLIENT)) { |
760 | // starttls was successful |
761 | |
762 | /** |
4ae9beb7 |
763 | * RFC 2595 requires to discard CAPABILITY information after successful |
764 | * STARTTLS command. We don't follow RFC, because SquirrelMail stores CAPABILITY |
765 | * information only after successful login (src/redirect.php) and cached information |
766 | * is used only in other php script connections after successful STARTTLS. If script |
767 | * issues sqimap_capability() call before sqimap_login() and wants to get initial |
768 | * capability response, script should set third sqimap_capability() argument to false. |
a15f9d93 |
769 | */ |
4ae9beb7 |
770 | //sqsession_unregister('sqimap_capabilities'); |
a15f9d93 |
771 | } else { |
772 | /** |
773 | * stream_socket_enable_crypto() call failed. Possible issues: |
774 | * - broken ssl certificate (uw drops connection, error is in syslog mail facility) |
4ae9beb7 |
775 | * - some ssl error (can reproduce with STREAM_CRYPTO_METHOD_SSLv3_CLIENT, PHP E_WARNING |
a15f9d93 |
776 | * suppressed in stream_socket_enable_crypto() call) |
777 | */ |
778 | sqimap_error_box(sprintf(_("Error connecting to IMAP server: %s."), $server), |
779 | '','', |
780 | _("Unable to start TLS.")); |
781 | /** |
4ae9beb7 |
782 | * Bug: stream_socket_enable_crypto() does not register SSL errors in |
783 | * openssl_error_string() or stream notification wrapper and displays |
784 | * them in E_WARNING level message. It is impossible to retrieve error |
a15f9d93 |
785 | * message without own error handler. |
786 | */ |
787 | exit; |
788 | } |
789 | } else { |
790 | // php install does not support stream_socket_enable_crypto() function |
791 | sqimap_error_box(sprintf(_("Error connecting to IMAP server: %s."), $server), |
792 | '','', |
793 | _("IMAP STARTTLS is enabled in SquirrelMail configuration, but used PHP version does not support functions that allow to enable encryption on open socket.")); |
794 | exit; |
795 | } |
796 | } |
b98732e8 |
797 | return $imap_stream; |
798 | } |
799 | |
48af4b64 |
800 | /** |
0e1a248b |
801 | * Logs the user into the IMAP server. If $hide is set, no error messages |
b86f98e4 |
802 | * will be displayed (if set to 1, just exits, if set to 2, returns FALSE). |
41d66087 |
803 | * This function returns the IMAP connection handle. |
0022f6db |
804 | * @param string $username user name |
9c093ecf |
805 | * @param string $password password encrypted with onetimepad. Since 1.5.2 |
806 | * function can use internal password functions, if parameter is set to |
807 | * boolean false. |
0022f6db |
808 | * @param string $imap_server_address address of imap server |
809 | * @param integer $imap_port port of imap server |
41d66087 |
810 | * @param int $hide controls display connection errors: |
5a908046 |
811 | * 0 = do not hide |
812 | * 1 = show no errors (just exit) |
813 | * 2 = show no errors (return FALSE) |
b4e0d365 |
814 | * 3 = show no errors (return error string) |
9aaa9ae2 |
815 | * @param array $ssl_options SSL context options, see config_local.php |
816 | * for more details (OPTIONAL) |
b4e0d365 |
817 | * @return mixed The IMAP connection stream, or if the connection fails, |
b86f98e4 |
818 | * FALSE if $hide is set to 2 or an error string if $hide |
b4e0d365 |
819 | * is set to 3. |
3411d4ec |
820 | */ |
9aaa9ae2 |
821 | function sqimap_login ($username, $password, $imap_server_address, |
822 | $imap_port, $hide, $ssl_options=array()) { |
fe55c7c7 |
823 | global $color, $squirrelmail_language, $onetimepad, $use_imap_tls, |
824 | $imap_auth_mech, $sqimap_capabilities; |
85fc999e |
825 | |
b0434236 |
826 | // Note/TODO: This hack grabs the $authz argument from the session. In the short future, |
827 | // a new argument in function sqimap_login() will be used instead. |
828 | $authz = ''; |
829 | global $authz; |
830 | sqgetglobalvar('authz' , $authz , SQ_SESSION); |
831 | |
832 | if(!empty($authz)) { |
833 | /* authz plugin - specific: |
b86f98e4 |
834 | * Get proxy login parameters from authz plugin configuration. If they |
b0434236 |
835 | * exist, they will override the current ones. |
836 | * This is useful if we want to use different SASL authentication mechanism |
837 | * and/or different TLS settings for proxy logins. */ |
b86f98e4 |
838 | global $authz_imap_auth_mech, $authz_use_imap_tls, $authz_imapPort_tls; |
b0434236 |
839 | $imap_auth_mech = !empty($authz_imap_auth_mech) ? strtolower($authz_imap_auth_mech) : $imap_auth_mech; |
6a645613 |
840 | $use_imap_tls = !empty($authz_use_imap_tls)? $authz_use_imap_tls : $use_imap_tls; |
841 | $imap_port = !empty($authz_use_imap_tls)? $authz_imapPort_tls : $imap_port; |
b0434236 |
842 | |
843 | if($imap_auth_mech == 'login' || $imap_auth_mech == 'cram-md5') { |
844 | logout_error("Misconfigured Plugin (authz or equivalent):<br/>". |
845 | "The LOGIN and CRAM-MD5 authentication mechanisms cannot be used when attempting proxy login."); |
846 | exit; |
847 | } |
848 | } |
849 | |
045ec1a1 |
850 | /* get imap login password */ |
851 | if ($password===false) { |
852 | /* standard functions */ |
853 | $password = sqauth_read_password(); |
854 | } else { |
855 | /* old way. $key must be extracted from cookie */ |
856 | if (!isset($onetimepad) || empty($onetimepad)) { |
857 | sqgetglobalvar('onetimepad' , $onetimepad , SQ_SESSION ); |
858 | } |
859 | /* Decrypt the password */ |
860 | $password = OneTimePadDecrypt($password, $onetimepad); |
eb2f6102 |
861 | } |
045ec1a1 |
862 | |
fe55c7c7 |
863 | if (!isset($sqimap_capabilities)) { |
72bc549b |
864 | sqgetglobalvar('sqimap_capabilities' , $sqimap_capabilities , SQ_SESSION ); |
fe55c7c7 |
865 | } |
866 | |
b98732e8 |
867 | $host = $imap_server_address; |
bd9829d7 |
868 | $imap_server_address = sqimap_get_user_server($imap_server_address, $username); |
fe55c7c7 |
869 | |
9aaa9ae2 |
870 | $imap_stream = sqimap_create_stream($imap_server_address,$imap_port,$use_imap_tls,$ssl_options); |
fe55c7c7 |
871 | |
b98732e8 |
872 | if (($imap_auth_mech == 'cram-md5') OR ($imap_auth_mech == 'digest-md5')) { |
3b151851 |
873 | // We're using some sort of authentication OTHER than plain or login |
b98732e8 |
874 | $tag=sqimap_session_id(false); |
875 | if ($imap_auth_mech == 'digest-md5') { |
098ea084 |
876 | $query = $tag . " AUTHENTICATE DIGEST-MD5\r\n"; |
b98732e8 |
877 | } elseif ($imap_auth_mech == 'cram-md5') { |
098ea084 |
878 | $query = $tag . " AUTHENTICATE CRAM-MD5\r\n"; |
b98732e8 |
879 | } |
880 | fputs($imap_stream,$query); |
881 | $answer=sqimap_fgets($imap_stream); |
882 | // Trim the "+ " off the front |
883 | $response=explode(" ",$answer,3); |
884 | if ($response[0] == '+') { |
098ea084 |
885 | // Got a challenge back |
b98732e8 |
886 | $challenge=$response[1]; |
887 | if ($imap_auth_mech == 'digest-md5') { |
b0434236 |
888 | $reply = digest_md5_response($username,$password,$challenge,'imap',$host,$authz); |
b98732e8 |
889 | } elseif ($imap_auth_mech == 'cram-md5') { |
890 | $reply = cram_md5_response($username,$password,$challenge); |
891 | } |
892 | fputs($imap_stream,$reply); |
893 | $read=sqimap_fgets($imap_stream); |
894 | if ($imap_auth_mech == 'digest-md5') { |
3b151851 |
895 | // DIGEST-MD5 has an extra step.. |
b98732e8 |
896 | if (substr($read,0,1) == '+') { // OK so far.. |
098ea084 |
897 | fputs($imap_stream,"\r\n"); |
898 | $read=sqimap_fgets($imap_stream); |
098ea084 |
899 | } |
b98732e8 |
900 | } |
901 | $results=explode(" ",$read,3); |
902 | $response=$results[1]; |
903 | $message=$results[2]; |
904 | } else { |
3b151851 |
905 | // Fake the response, so the error trap at the bottom will work |
b98732e8 |
906 | $response="BAD"; |
907 | $message='IMAP server does not appear to support the authentication method selected.'; |
908 | $message .= ' Please contact your system administrator.'; |
909 | } |
fe0b18b3 |
910 | } elseif ($imap_auth_mech == 'login') { |
b98732e8 |
911 | // Original IMAP login code |
912 | $query = 'LOGIN "' . quoteimap($username) . '" "' . quoteimap($password) . '"'; |
913 | $read = sqimap_run_command ($imap_stream, $query, false, $response, $message); |
1e7fc1cb |
914 | } elseif ($imap_auth_mech == 'plain') { |
fe55c7c7 |
915 | /*** |
b0434236 |
916 | * SASL PLAIN, RFC 4616 (updates 2595) |
fe55c7c7 |
917 | * |
b0434236 |
918 | * The mechanism consists of a single message, a string of [UTF-8] |
919 | * encoded [Unicode] characters, from the client to the server. The |
920 | * client presents the authorization identity (identity to act as), |
921 | * followed by a NUL (U+0000) character, followed by the authentication |
922 | * identity (identity whose password will be used), followed by a NUL |
923 | * (U+0000) character, followed by the clear-text password. As with |
924 | * other SASL mechanisms, the client does not provide an authorization |
925 | * identity when it wishes the server to derive an identity from the |
926 | * credentials and use that as the authorization identity. |
927 | */ |
b98732e8 |
928 | $tag=sqimap_session_id(false); |
72bc549b |
929 | $sasl = (isset($sqimap_capabilities['SASL-IR']) && $sqimap_capabilities['SASL-IR']) ? true : false; |
b0434236 |
930 | if(!empty($authz)) { |
931 | $auth = base64_encode("$username\0$authz\0$password"); |
932 | } else { |
933 | $auth = base64_encode("$username\0$username\0$password"); |
934 | } |
fe55c7c7 |
935 | if ($sasl) { |
936 | // IMAP Extension for SASL Initial Client Response |
2bd6b461 |
937 | // <draft-siemborski-imap-sasl-initial-response-01b.txt> |
fe55c7c7 |
938 | $query = $tag . " AUTHENTICATE PLAIN $auth\r\n"; |
939 | fputs($imap_stream, $query); |
b98732e8 |
940 | $read = sqimap_fgets($imap_stream); |
fe55c7c7 |
941 | } else { |
942 | $query = $tag . " AUTHENTICATE PLAIN\r\n"; |
943 | fputs($imap_stream, $query); |
944 | $read=sqimap_fgets($imap_stream); |
945 | if (substr($read,0,1) == '+') { // OK so far.. |
946 | fputs($imap_stream, "$auth\r\n"); |
947 | $read = sqimap_fgets($imap_stream); |
948 | } |
098ea084 |
949 | } |
b98732e8 |
950 | $results=explode(" ",$read,3); |
951 | $response=$results[1]; |
952 | $message=$results[2]; |
b0434236 |
953 | |
b98732e8 |
954 | } else { |
955 | $response="BAD"; |
956 | $message="Internal SquirrelMail error - unknown IMAP authentication method chosen. Please contact the developers."; |
957 | } |
fe55c7c7 |
958 | |
b98732e8 |
959 | /* If the connection was not successful, lets see why */ |
9c737111 |
960 | if ($response != 'OK') { |
b4e0d365 |
961 | if (!$hide || $hide == 3) { |
962 | //FIXME: UUURG... We don't want HTML in error messages, should also do html sanitizing of error messages elsewhere; should't assume output is destined for an HTML browser here |
74424a43 |
963 | if ($response != 'NO') { |
3411d4ec |
964 | /* "BAD" and anything else gets reported here. */ |
3047e291 |
965 | $message = sm_encode_html_special_chars($message); |
9c737111 |
966 | set_up_language($squirrelmail_language, true); |
967 | if ($response == 'BAD') { |
b4e0d365 |
968 | if ($hide == 3) return sprintf(_("Bad request: %s"), $message); |
6fd95361 |
969 | $string = sprintf (_("Bad request: %s")."<br />\r\n", $message); |
9c737111 |
970 | } else { |
b4e0d365 |
971 | if ($hide == 3) return sprintf(_("Unknown error: %s"), $message); |
6fd95361 |
972 | $string = sprintf (_("Unknown error: %s") . "<br />\n", $message); |
9c737111 |
973 | } |
1e7fc1cb |
974 | if (isset($read) && is_array($read)) { |
6fd95361 |
975 | $string .= '<br />' . _("Read data:") . "<br />\n"; |
9c737111 |
976 | foreach ($read as $line) { |
3047e291 |
977 | $string .= sm_encode_html_special_chars($line) . "<br />\n"; |
9c737111 |
978 | } |
979 | } |
1b858d86 |
980 | error_box($string); |
9c737111 |
981 | exit; |
165e24a7 |
982 | } else { |
3411d4ec |
983 | /* |
984 | * If the user does not log in with the correct |
1c72b151 |
985 | * username and password it is not possible to get the |
986 | * correct locale from the user's preferences. |
987 | * Therefore, apply the same hack as on the login |
988 | * screen. |
3411d4ec |
989 | * |
990 | * $squirrelmail_language is set by a cookie when |
1c72b151 |
991 | * the user selects language and logs out |
bee165ef |
992 | */ |
fe55c7c7 |
993 | |
9c737111 |
994 | set_up_language($squirrelmail_language, true); |
098ea084 |
995 | sqsession_destroy(); |
b86f98e4 |
996 | |
d5c472f1 |
997 | /* terminate the session nicely */ |
998 | sqimap_logout($imap_stream); |
b4e0d365 |
999 | if ($hide == 3) return _("Unknown user or password incorrect."); |
bd9c880b |
1000 | logout_error( _("Unknown user or password incorrect.") ); |
9c737111 |
1001 | exit; |
052e0c26 |
1002 | } |
9c737111 |
1003 | } else { |
41d66087 |
1004 | if ($hide == 2) return FALSE; |
052e0c26 |
1005 | exit; |
9c737111 |
1006 | } |
1007 | } |
1d20443c |
1008 | |
1009 | /* Special error case: |
1010 | * Login referrals. The server returns: |
0e1a248b |
1011 | * ? OK [REFERRAL <imap url>] |
1012 | * Check RFC 2221 for details. Since we do not support login referrals yet |
1013 | * we log the user out. |
1014 | */ |
2da1524c |
1015 | if ( stristr($message, 'REFERRAL imap') === TRUE ) { |
0e1a248b |
1016 | sqimap_logout($imap_stream); |
1d20443c |
1017 | set_up_language($squirrelmail_language, true); |
1d20443c |
1018 | sqsession_destroy(); |
edde1f5c |
1019 | logout_error( _("Your mailbox is not located at this server. Try a different server or consult your system administrator") ); |
1d20443c |
1020 | exit; |
1021 | } |
0e1a248b |
1022 | |
9c737111 |
1023 | return $imap_stream; |
1024 | } |
f1e6f580 |
1025 | |
48af4b64 |
1026 | /** |
1027 | * Simply logs out the IMAP session |
95e3afc1 |
1028 | * @param stream $imap_stream the IMAP connection to log out. |
48af4b64 |
1029 | * @return void |
1030 | */ |
9c737111 |
1031 | function sqimap_logout ($imap_stream) { |
8d936b0c |
1032 | /* Logout is not valid until the server returns 'BYE' |
1033 | * If we don't have an imap_ stream we're already logged out */ |
26a2cc8b |
1034 | if(isset($imap_stream) && $imap_stream) |
8d936b0c |
1035 | sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message); |
9c737111 |
1036 | } |
1037 | |
48af4b64 |
1038 | /** |
95e3afc1 |
1039 | * Retrieve the CAPABILITY string from the IMAP server. |
48af4b64 |
1040 | * If capability is set, returns only that specific capability, |
1041 | * else returns array of all capabilities. |
95e3afc1 |
1042 | * @param stream $imap_stream |
a15f9d93 |
1043 | * @param string $capability (since 1.3.0) |
4ae9beb7 |
1044 | * @param boolean $bUseCache (since 1.5.1) Controls use of capability data stored in session |
0e1a248b |
1045 | * @return mixed (string if $capability is set and found, |
0022f6db |
1046 | * false, if $capability is set and not found, |
1047 | * array if $capability not set) |
48af4b64 |
1048 | */ |
a15f9d93 |
1049 | function sqimap_capability($imap_stream, $capability='', $bUseCache=true) { |
1050 | // sqgetGlobalVar('sqimap_capabilities', $sqimap_capabilities, SQ_SESSION); |
0493ed11 |
1051 | |
a15f9d93 |
1052 | if (!$bUseCache || ! sqgetGlobalVar('sqimap_capabilities', $sqimap_capabilities, SQ_SESSION)) { |
1c72b151 |
1053 | $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b); |
9c737111 |
1054 | $c = explode(' ', $read[0]); |
1055 | for ($i=2; $i < count($c); $i++) { |
1056 | $cap_list = explode('=', $c[$i]); |
3411d4ec |
1057 | if (isset($cap_list[1])) { |
045dee91 |
1058 | if(isset($sqimap_capabilities[trim($cap_list[0])]) && |
1059 | !is_array($sqimap_capabilities[trim($cap_list[0])])) { |
1060 | // Remove array key that was added in 'else' block below |
1061 | // This is to accomodate for capabilities like: |
1062 | // SORT SORT=MODSEQ |
1063 | unset($sqimap_capabilities[trim($cap_list[0])]); |
1064 | } |
3cfa833d |
1065 | $sqimap_capabilities[trim($cap_list[0])][] = $cap_list[1]; |
3411d4ec |
1066 | } else { |
045dee91 |
1067 | if(!isset($sqimap_capabilities[trim($cap_list[0])])) { |
1068 | $sqimap_capabilities[trim($cap_list[0])] = TRUE; |
1069 | } |
3411d4ec |
1070 | } |
f1e6f580 |
1071 | } |
9c737111 |
1072 | } |
487daa81 |
1073 | if ($capability) { |
098ea084 |
1074 | if (isset($sqimap_capabilities[$capability])) { |
1075 | return $sqimap_capabilities[$capability]; |
1076 | } else { |
1077 | return false; |
1078 | } |
f1e6f580 |
1079 | } |
487daa81 |
1080 | return $sqimap_capabilities; |
9c737111 |
1081 | } |
1082 | |
48af4b64 |
1083 | /** |
95e3afc1 |
1084 | * Returns the delimiter between mailboxes: INBOX/Test, or INBOX.Test |
0022f6db |
1085 | * @param stream $imap_stream |
1086 | * @return string |
48af4b64 |
1087 | */ |
9c737111 |
1088 | function sqimap_get_delimiter ($imap_stream = false) { |
3411d4ec |
1089 | global $sqimap_delimiter, $optional_delimiter; |
85fc999e |
1090 | |
9c737111 |
1091 | /* Use configured delimiter if set */ |
1092 | if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') { |
1093 | return $optional_delimiter; |
1094 | } |
85fc999e |
1095 | |
7eb6261e |
1096 | /* Delimiter is stored in the session from redirect. Try fetching from there first */ |
cc54580f |
1097 | if (empty($sqimap_delimiter)) { |
7eb6261e |
1098 | sqgetGlobalVar('delimiter',$sqimap_delimiter,SQ_SESSION); |
1099 | } |
1100 | |
9c737111 |
1101 | /* Do some caching here */ |
1102 | if (!$sqimap_delimiter) { |
ad2f0b54 |
1103 | if (sqimap_capability($imap_stream, 'NAMESPACE') |
3411d4ec |
1104 | /* |
1105 | * According to something that I can't find, this is supposed to work on all systems |
1106 | * OS: This won't work in Courier IMAP. |
1107 | * OS: According to rfc2342 response from NAMESPACE command is: |
1108 | * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES) |
1109 | * OS: We want to lookup all personal NAMESPACES... |
b86f98e4 |
1110 | * |
6a645613 |
1111 | * TODO: remove this in favour of the information from sqimap_get_namespace() |
3411d4ec |
1112 | */ |
ad2f0b54 |
1113 | && ($read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b)) |
1114 | && preg_match('/\* NAMESPACE +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL)/i', $read[0], $data) |
1115 | && preg_match('/^\( *\((.*)\) *\)/', $data[1], $data2)) { |
1116 | $pn = $data2[1]; |
1117 | $pna = explode(')(', $pn); |
1118 | $delnew = array(); |
1119 | while (list($k, $v) = each($pna)) { |
1120 | $lst = explode('"', $v); |
1121 | if (isset($lst[3])) { |
1122 | $delnew[$lst[1]] = $lst[3]; |
1123 | } else { |
1124 | $delnew[$lst[1]] = ''; |
f1e6f580 |
1125 | } |
1126 | } |
ad2f0b54 |
1127 | $sqimap_delimiter = array_shift($delnew); |
f1e6f580 |
1128 | } else { |
1129 | fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n"); |
1130 | $read = sqimap_read_data($imap_stream, '.', true, $a, $b); |
91e0dccc |
1131 | $read = $read['.'][0]; //sqimap_read_data() now returns a tag array of response array |
f1e6f580 |
1132 | $quote_position = strpos ($read[0], '"'); |
1133 | $sqimap_delimiter = substr ($read[0], $quote_position+1, 1); |
1134 | } |
1135 | } |
1136 | return $sqimap_delimiter; |
9c737111 |
1137 | } |
052e0c26 |
1138 | |
6a645613 |
1139 | /** |
1140 | * Retrieves the namespaces from the IMAP server. |
1141 | * NAMESPACE is an IMAP extension defined in RFC 2342. |
1142 | * |
1143 | * @param stream $imap_stream |
1144 | * @return array |
1145 | */ |
1146 | function sqimap_get_namespace($imap_stream) { |
1147 | $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b); |
1148 | return sqimap_parse_namespace($read[0]); |
1149 | } |
b86f98e4 |
1150 | |
6a645613 |
1151 | /** |
1152 | * Parses a NAMESPACE response and returns an array with the available |
1153 | * personal, users and shared namespaces. |
1154 | * |
1155 | * @param string $input |
1156 | * @return array The returned array has the following format: |
1157 | * <pre> |
1158 | * array( |
1159 | * 'personal' => array( |
1160 | * 0 => array('prefix'=>'INBOX.','delimiter' =>'.'), |
1161 | * 1 => ... |
1162 | * ), |
1163 | * 'users' => array(.. |
1164 | * ), |
1165 | * 'shared' => array( .. |
1166 | * ) |
1167 | * ) |
1168 | * </pre> |
1169 | * Note that if a namespace is not defined in the server, then the corresponding |
1170 | * array will be empty. |
1171 | */ |
1172 | function sqimap_parse_namespace(&$input) { |
1173 | $ns_strings = array(1=>'personal', 2=>'users', 3=>'shared'); |
1174 | $namespace = array(); |
1175 | |
b7910e12 |
1176 | if (preg_match('/NAMESPACE (\(\(.*\)\)|NIL) (\(\(.*\)\)|NIL) (\(\(.*\)\)|NIL)/', $input, $regs)) { |
6a645613 |
1177 | for($i=1; $i<=3; $i++) { |
1178 | if($regs[$i] == 'NIL') { |
1179 | $namespace[$ns_strings[$i]] = array(); |
1180 | } else { |
1181 | // Pop-out the first ( and last ) for easier parsing |
1182 | $ns = substr($regs[$i], 1, sizeof($regs[$i])-2); |
1183 | if($c = preg_match_all('/\((?:(.*?)\s*?)\)/', $ns, $regs2)) { |
1184 | $namespace[$ns_strings[$i]] = array(); |
1185 | for($j=0; $j<sizeof($regs2[1]); $j++) { |
61d8f26d |
1186 | preg_match('/"(.*)"\s+("(.*)"|NIL)/', $regs2[1][$j], $regs3); |
6a645613 |
1187 | $namespace[$ns_strings[$i]][$j]['prefix'] = $regs3[1]; |
61d8f26d |
1188 | if($regs3[2] == 'NIL') { |
1189 | $namespace[$ns_strings[$i]][$j]['delimiter'] = null; |
1190 | } else { |
1191 | // $regs[3] is $regs[2] without the quotes |
1192 | $namespace[$ns_strings[$i]][$j]['delimiter'] = $regs3[3]; |
1193 | } |
6a645613 |
1194 | unset($regs3); |
1195 | } |
1196 | } |
1197 | unset($ns); |
1198 | } |
1199 | } |
1200 | } |
1201 | return($namespace); |
1202 | } |
1203 | |
48af4b64 |
1204 | /** |
1205 | * This encodes a mailbox name for use in IMAP commands. |
95e3afc1 |
1206 | * @param string $what the mailbox to encode |
48af4b64 |
1207 | * @return string the encoded mailbox string |
0022f6db |
1208 | * @since 1.5.0 |
48af4b64 |
1209 | */ |
b2306cbe |
1210 | function sqimap_encode_mailbox_name($what) |
1211 | { |
b7910e12 |
1212 | if (preg_match('/["\\\r\n]/', $what)) |
91e0dccc |
1213 | return '{' . strlen($what) . "}\r\n" . $what; /* 4.3 literal form */ |
1214 | return '"' . $what . '"'; /* 4.3 quoted string form */ |
b2306cbe |
1215 | } |
1216 | |
48af4b64 |
1217 | /** |
1218 | * Gets the number of messages in the current mailbox. |
1e0a2f0e |
1219 | * |
1220 | * OBSOLETE use sqimap_status_messages instead. |
95e3afc1 |
1221 | * @param stream $imap_stream imap stream |
1222 | * @param string $mailbox |
1223 | * @deprecated |
48af4b64 |
1224 | */ |
9c737111 |
1225 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
48879ef0 |
1226 | $aStatus = sqimap_status_messages($imap_stream,$mailbox,array('MESSAGES')); |
1227 | return $aStatus['MESSAGES']; |
9c737111 |
1228 | } |
95e3afc1 |
1229 | |
1e0a2f0e |
1230 | /** |
1231 | * OBSOLETE FUNCTION should be removed after mailbox_display, |
1232 | * printMessage function is adapted |
95e3afc1 |
1233 | * $addr_ar = array(), $group = '' and $host='' arguments are used in 1.4.0 |
1234 | * @param string $address |
1235 | * @param integer $max |
1236 | * @since 1.4.0 |
1237 | * @deprecated See Rfc822Address.php |
1e0a2f0e |
1238 | */ |
91688e5f |
1239 | function parseAddress($address, $max=0) { |
1e0a2f0e |
1240 | $aAddress = parseRFC822Address($address,array('limit'=> $max)); |
1241 | /* |
1242 | * Because the expected format of the array element is changed we adapt it now. |
1243 | * This also implies that this function is obsolete and should be removed after the |
1244 | * rest of the source is adapted. See Rfc822Address.php for the new function. |
1245 | */ |
1246 | array_walk($aAddress, '_adaptAddress'); |
1247 | return $aAddress; |
1248 | } |
91688e5f |
1249 | |
1e0a2f0e |
1250 | /** |
1251 | * OBSOLETE FUNCTION should be removed after mailbox_display, |
1252 | * printMessage function is adapted |
95e3afc1 |
1253 | * |
0e1a248b |
1254 | * callback function used for formating of addresses array in |
95e3afc1 |
1255 | * parseAddress() function |
1256 | * @param array $aAddr |
1257 | * @param integer $k array key |
1258 | * @since 1.5.1 |
1259 | * @deprecated |
1e0a2f0e |
1260 | */ |
1261 | function _adaptAddress(&$aAddr,$k) { |
1262 | $sPersonal = (isset($aAddr[SQM_ADDR_PERSONAL]) && $aAddr[SQM_ADDR_PERSONAL]) ? |
1263 | $aAddr[SQM_ADDR_PERSONAL] : ''; |
1264 | $sEmail = ($aAddr[SQM_ADDR_HOST]) ? |
3cd52f38 |
1265 | $aAddr[SQM_ADDR_MAILBOX] . '@'.$aAddr[SQM_ADDR_HOST] : |
1266 | $aAddr[SQM_ADDR_MAILBOX]; |
1e0a2f0e |
1267 | $aAddr = array($sEmail,$sPersonal); |
1268 | } |
91688e5f |
1269 | |
48af4b64 |
1270 | /** |
1271 | * Returns the number of unseen messages in this folder. |
26e90c74 |
1272 | * obsoleted by sqimap_status_messages ! |
95e3afc1 |
1273 | * Arguments differ in 1.0.x |
1274 | * @param stream $imap_stream |
1275 | * @param string $mailbox |
1276 | * @return integer |
1277 | * @deprecated |
3411d4ec |
1278 | */ |
9c737111 |
1279 | function sqimap_unseen_messages ($imap_stream, $mailbox) { |
26e90c74 |
1280 | $aStatus = sqimap_status_messages($imap_stream,$mailbox,array('UNSEEN')); |
1281 | return $aStatus['UNSEEN']; |
9c737111 |
1282 | } |
1283 | |
48af4b64 |
1284 | /** |
26e90c74 |
1285 | * Returns the status items of a mailbox. |
1286 | * Default it returns MESSAGES,UNSEEN and RECENT |
0e1a248b |
1287 | * Supported status items are MESSAGES, UNSEEN, RECENT (since 1.4.0), |
95e3afc1 |
1288 | * UIDNEXT (since 1.5.1) and UIDVALIDITY (since 1.5.1) |
1289 | * @param stream $imap_stream imap stream |
1290 | * @param string $mailbox mail folder |
1291 | * @param array $aStatusItems status items |
1292 | * @return array |
1293 | * @since 1.3.2 |
1f720b34 |
1294 | */ |
1e0a2f0e |
1295 | function sqimap_status_messages ($imap_stream, $mailbox, |
26e90c74 |
1296 | $aStatusItems = array('MESSAGES','UNSEEN','RECENT')) { |
1297 | |
3fedd15b |
1298 | $aStatusItems = implode(' ',$aStatusItems); |
26e90c74 |
1299 | $read_ary = sqimap_run_command ($imap_stream, 'STATUS ' . sqimap_encode_mailbox_name($mailbox) . |
1300 | " ($aStatusItems)", false, $result, $message); |
1f720b34 |
1301 | $i = 0; |
26e90c74 |
1302 | $messages = $unseen = $recent = $uidnext = $uidvalidity = false; |
1f720b34 |
1303 | $regs = array(false,false); |
1304 | while (isset($read_ary[$i])) { |
1305 | if (preg_match('/UNSEEN\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
098ea084 |
1306 | $unseen = $regs[1]; |
1307 | } |
1f720b34 |
1308 | if (preg_match('/MESSAGES\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
098ea084 |
1309 | $messages = $regs[1]; |
1310 | } |
b8ec18ef |
1311 | if (preg_match('/RECENT\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
098ea084 |
1312 | $recent = $regs[1]; |
1e0a2f0e |
1313 | } |
26e90c74 |
1314 | if (preg_match('/UIDNEXT\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
1315 | $uidnext = $regs[1]; |
1e0a2f0e |
1316 | } |
26e90c74 |
1317 | if (preg_match('/UIDVALIDITY\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
1318 | $uidvalidity = $regs[1]; |
1e0a2f0e |
1319 | } |
1f720b34 |
1320 | $i++; |
1321 | } |
48879ef0 |
1322 | |
1323 | $status=array('MESSAGES' => $messages, |
1e0a2f0e |
1324 | 'UNSEEN'=>$unseen, |
26e90c74 |
1325 | 'RECENT' => $recent, |
1326 | 'UIDNEXT' => $uidnext, |
1327 | 'UIDVALIDITY' => $uidvalidity); |
48879ef0 |
1328 | |
1329 | if (!empty($messages)) { $hook_status['MESSAGES']=$messages; } |
1330 | if (!empty($unseen)) { $hook_status['UNSEEN']=$unseen; } |
1331 | if (!empty($recent)) { $hook_status['RECENT']=$recent; } |
81de00c0 |
1332 | if (!empty($hook_status)) { |
48879ef0 |
1333 | $hook_status['MAILBOX']=$mailbox; |
1334 | $hook_status['CALLER']='sqimap_status_messages'; |
d849b570 |
1335 | do_hook('folder_status', $hook_status); |
48879ef0 |
1336 | } |
1337 | return $status; |
1f720b34 |
1338 | } |
1339 | |
9c737111 |
1340 | |
48af4b64 |
1341 | /** |
1342 | * Saves a message to a given folder -- used for saving sent messages |
95e3afc1 |
1343 | * @param stream $imap_stream |
1344 | * @param string $sent_folder |
1345 | * @param $length |
81de00c0 |
1346 | * @return string $sid |
3411d4ec |
1347 | */ |
81de00c0 |
1348 | function sqimap_append ($imap_stream, $sMailbox, $length) { |
1349 | $sid = sqimap_session_id(); |
1350 | $query = $sid . ' APPEND ' . sqimap_encode_mailbox_name($sMailbox) . " (\\Seen) {".$length."}"; |
1351 | fputs ($imap_stream, "$query\r\n"); |
85fc999e |
1352 | $tmp = fgets ($imap_stream, 1024); |
81de00c0 |
1353 | sqimap_append_checkresponse($tmp, $sMailbox,$sid, $query); |
1354 | return $sid; |
9c737111 |
1355 | } |
1356 | |
95e3afc1 |
1357 | /** |
1358 | * @param stream imap_stream |
1359 | * @param string $folder (since 1.3.2) |
1360 | */ |
81de00c0 |
1361 | function sqimap_append_done ($imap_stream, $sMailbox='') { |
9c737111 |
1362 | fputs ($imap_stream, "\r\n"); |
1363 | $tmp = fgets ($imap_stream, 1024); |
81de00c0 |
1364 | while (!sqimap_append_checkresponse($tmp, $sMailbox)) { |
1365 | $tmp = fgets ($imap_stream, 1024); |
1366 | } |
7ec3a6b9 |
1367 | } |
1368 | |
95e3afc1 |
1369 | /** |
0e1a248b |
1370 | * Displays error messages, if there are errors in responses to |
95e3afc1 |
1371 | * commands issues by sqimap_append() and sqimap_append_done() functions. |
1372 | * @param string $response |
81de00c0 |
1373 | * @param string $sMailbox |
1374 | * @return bool $bDone |
9948500c |
1375 | * @since 1.5.1 and 1.4.5 |
95e3afc1 |
1376 | */ |
81de00c0 |
1377 | function sqimap_append_checkresponse($response, $sMailbox, $sid='', $query='') { |
81de00c0 |
1378 | // static vars to keep them available when sqimap_append_done calls this function. |
1379 | static $imapquery, $imapsid; |
7ec3a6b9 |
1380 | |
81de00c0 |
1381 | $bDone = false; |
1382 | |
1383 | if ($query) { |
1384 | $imapquery = $query; |
1385 | } |
1386 | if ($sid) { |
1387 | $imapsid = $sid; |
1388 | } |
1389 | if ($response{0} == '+') { |
1390 | // continuation request triggerd by sqimap_append() |
1391 | $bDone = true; |
1392 | } else { |
1393 | $i = strpos($response, ' '); |
1394 | $sRsp = substr($response,0,$i); |
1395 | $sMsg = substr($response,$i+1); |
1396 | $aExtra = array('MAILBOX' => $sMailbox); |
1397 | switch ($sRsp) { |
1398 | case '*': //untagged response |
1399 | $i = strpos($sMsg, ' '); |
1400 | $sRsp = strtoupper(substr($sMsg,0,$i)); |
1401 | $sMsg = substr($sMsg,$i+1); |
1402 | if ($sRsp == 'NO' || $sRsp == 'BAD') { |
1403 | // for the moment disabled. Enable after 1.5.1 release. |
1404 | // Notices could give valueable information about the mailbox |
4bc49df6 |
1405 | // Update: seems this was forgotten, but now it is finally enabled |
1406 | sqm_trigger_imap_error('SQM_IMAP_APPEND_NOTICE',$imapquery,$sRsp,$sMsg); |
81de00c0 |
1407 | } |
1408 | $bDone = false; |
1409 | case $imapsid: |
1410 | // A001 OK message |
1411 | // $imapsid<space>$sRsp<space>$sMsg |
1412 | $bDone = true; |
1413 | $i = strpos($sMsg, ' '); |
1414 | $sRsp = strtoupper(substr($sMsg,0,$i)); |
1415 | $sMsg = substr($sMsg,$i+1); |
1416 | switch ($sRsp) { |
1417 | case 'NO': |
1418 | if (preg_match("/(.*)(quota)(.*)$/i", $sMsg, $aMatch)) { |
1419 | sqm_trigger_imap_error('SQM_IMAP_APPEND_QUOTA_ERROR',$imapquery,$sRsp,$sMsg,$aExtra); |
1420 | } else { |
1421 | sqm_trigger_imap_error('SQM_IMAP_APPEND_ERROR',$imapquery,$sRsp,$sMsg,$aExtra); |
1422 | } |
1423 | break; |
1424 | case 'BAD': |
1425 | sqm_trigger_imap_error('SQM_IMAP_ERROR',$imapquery,$sRsp,$sMsg,$aExtra); |
1426 | break; |
1427 | case 'BYE': |
1428 | sqm_trigger_imap_error('SQM_IMAP_BYE',$imapquery,$sRsp,$sMsg,$aExtra); |
1429 | break; |
1430 | case 'OK': |
1431 | break; |
1432 | default: |
1433 | break; |
1434 | } |
1435 | break; |
1436 | default: |
1437 | // should be false because of the unexpected response but i'm not sure if |
1438 | // that will cause an endless loop in sqimap_append_done |
1439 | $bDone = true; |
098ea084 |
1440 | } |
69146537 |
1441 | } |
81de00c0 |
1442 | return $bDone; |
9c737111 |
1443 | } |
85fc999e |
1444 | |
95e3afc1 |
1445 | /** |
0e1a248b |
1446 | * Allows mapping of IMAP server address with custom function |
95e3afc1 |
1447 | * see map_yp_alias() |
1448 | * @param string $imap_server imap server address or mapping |
1449 | * @param string $username |
1450 | * @return string |
1451 | * @since 1.3.0 |
1452 | */ |
bd9829d7 |
1453 | function sqimap_get_user_server ($imap_server, $username) { |
bd9829d7 |
1454 | if (substr($imap_server, 0, 4) != "map:") { |
1455 | return $imap_server; |
1456 | } |
bd9829d7 |
1457 | $function = substr($imap_server, 4); |
1458 | return $function($username); |
1459 | } |
1460 | |
48af4b64 |
1461 | /** |
0e1a248b |
1462 | * This is an example that gets IMAP servers from yellowpages (NIS). |
1e0a2f0e |
1463 | * you can simple put map:map_yp_alias in your $imap_server_address |
bd9829d7 |
1464 | * in config.php use your own function instead map_yp_alias to map your |
0e1a248b |
1465 | * LDAP whatever way to find the users IMAP server. |
1466 | * |
95e3afc1 |
1467 | * Requires access to external ypmatch program |
1468 | * FIXME: it can be implemented in php yp extension or pecl (since php 5.1.0) |
1469 | * @param string $username |
1470 | * @return string |
1471 | * @since 1.3.0 |
48af4b64 |
1472 | */ |
bd9829d7 |
1473 | function map_yp_alias($username) { |
a28de419 |
1474 | $safe_username = escapeshellarg($username); |
1475 | $yp = `ypmatch $safe_username aliases`; |
bd9829d7 |
1476 | return chop(substr($yp, strlen($username)+1)); |
1e0a2f0e |
1477 | } |