59177427 |
1 | <?php |
bccadd02 |
2 | |
35586184 |
3 | /** |
a6fd80f5 |
4 | * imap_general.php |
35586184 |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
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) { |
25 | return( sprintf("A%03d", $sqimap_session_id++) ); |
26 | } else { |
27 | return( sprintf("A%03d", $sqimap_session_id++) . ' UID' ); |
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) { |
37 | $sid = sqimap_session_id($unique_id); |
38 | fputs ($imap_stream, $sid . ' ' . $query . "\r\n"); |
39 | $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query ); |
40 | return $read; |
41 | } else { |
42 | global $squirrelmail_language, $color; |
43 | set_up_language($squirrelmail_language); |
44 | require_once(SM_PATH . 'functions/display_messages.php'); |
45 | $string = "<b><font color=$color[2]>\n" . |
46 | _("ERROR : No available imapstream.") . |
47 | "</b></font>\n"; |
48 | error_box($string,$color); |
49 | return false; |
50 | } |
51 | |
1c72b151 |
52 | } |
53 | |
487daa81 |
54 | function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) { |
c5809184 |
55 | if ($imap_stream) { |
56 | $sid = sqimap_session_id($unique_id); |
57 | fputs ($imap_stream, $sid . ' ' . $query . "\r\n"); |
58 | $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query); |
59 | return $read; |
60 | } else { |
61 | global $squirrelmail_language, $color; |
62 | set_up_language($squirrelmail_language); |
63 | require_once(SM_PATH . 'functions/display_messages.php'); |
64 | $string = "<b><font color=$color[2]>\n" . |
65 | _("ERROR : No available imapstream.") . |
66 | "</b></font>\n"; |
67 | error_box($string,$color); |
68 | return false; |
69 | } |
70 | |
42a07ac1 |
71 | } |
72 | |
9c737111 |
73 | |
b8c285ab |
74 | /* |
75 | * custom fgets function. gets a line from IMAP |
76 | * no matter how big it may be |
77 | */ |
78 | |
79 | function sqimap_fgets($imap_stream) { |
80 | $read = ''; |
81 | $buffer = 4096; |
82 | $results = ''; |
83 | while (strpos($read, "\n") === false) { |
84 | if (!($read = fgets($imap_stream, $buffer))) { |
85 | break; |
86 | } |
87 | $results .= $read; |
88 | } |
89 | return $results; |
90 | } |
91 | |
bee165ef |
92 | /* |
3411d4ec |
93 | * Reads the output from the IMAP stream. If handle_errors is set to true, |
94 | * this will also handle all errors that are received. If it is not set, |
95 | * the errors will be sent back through $response and $message |
bee165ef |
96 | */ |
b8c285ab |
97 | |
3411d4ec |
98 | function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') { |
9c737111 |
99 | global $color, $squirrelmail_language; |
9c737111 |
100 | $read = ''; |
487daa81 |
101 | $pre_a = explode(' ',trim($pre)); |
102 | $pre = $pre_a[0]; |
b8c285ab |
103 | $resultlist = array(); |
104 | $data = array(); |
105 | $read = sqimap_fgets($imap_stream); |
106 | while (1) { |
107 | switch (true) { |
108 | case preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs): |
109 | case preg_match('/^\* (BYE \[ALERT\])(.*)$/', $read, $regs): |
110 | $response = $regs[1]; |
111 | $message = trim($regs[2]); |
112 | break 2; |
113 | case preg_match("/^\* (OK \[PARSE\])(.*)$/", $read): |
114 | $read = sqimap_fgets($imap_stream); |
115 | break 1; |
56afb33f |
116 | case preg_match('/^\* ([0-9]+) FETCH.*/', $read, $regs): |
b8c285ab |
117 | $fetch_data = array(); |
118 | $fetch_data[] = $read; |
119 | $read = sqimap_fgets($imap_stream); |
863936bb |
120 | while (!preg_match('/^\* [0-9]+ FETCH.*/', $read) && |
b8c285ab |
121 | !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read)) { |
122 | $fetch_data[] = $read; |
863936bb |
123 | $last = $read; |
b8c285ab |
124 | $read = sqimap_fgets($imap_stream); |
9c737111 |
125 | } |
863936bb |
126 | if (isset($last) && preg_match('/^\)/', $last)) { |
127 | array_pop($fetch_data); |
128 | } |
b8c285ab |
129 | $resultlist[] = $fetch_data; |
130 | break 1; |
131 | default: |
132 | $data[] = $read; |
133 | $read = sqimap_fgets($imap_stream); |
134 | break 1; |
9c737111 |
135 | } |
b8c285ab |
136 | } |
137 | if (!empty($data)) { |
9c737111 |
138 | $resultlist[] = $data; |
139 | } |
b8c285ab |
140 | elseif (empty($resultlist)) { |
141 | $resultlist[] = array(); |
142 | } |
bee165ef |
143 | if ($handle_errors == false) { |
144 | return( $resultlist ); |
b8c285ab |
145 | } |
146 | elseif ($response == 'NO') { |
147 | /* ignore this error from M$ exchange, it is not fatal (aka bug) */ |
9c737111 |
148 | if (strstr($message, 'command resulted in') === false) { |
441f2d33 |
149 | set_up_language($squirrelmail_language); |
1f720b34 |
150 | require_once(SM_PATH . 'functions/display_messages.php'); |
151 | $string = "<b><font color=$color[2]>\n" . |
b8c285ab |
152 | _("ERROR : Could not complete request.") . |
153 | "</b><br>\n" . |
9b761dbd |
154 | _("Query:") . ' ' . |
155 | htmlspecialchars($query) . '<br>' . |
b8c285ab |
156 | _("Reason Given: ") . |
9b761dbd |
157 | htmlspecialchars($message) . "</font><br>\n"; |
1f720b34 |
158 | error_box($string,$color); |
052e0c26 |
159 | exit; |
9c737111 |
160 | } |
b8c285ab |
161 | } |
162 | elseif ($response == 'BAD') { |
9c737111 |
163 | set_up_language($squirrelmail_language); |
1f720b34 |
164 | require_once(SM_PATH . 'functions/display_messages.php'); |
bef6629c |
165 | $string = "<b><font color=$color[2]>\n" . |
b8c285ab |
166 | _("ERROR : Bad or malformed request.") . |
167 | "</b><br>\n" . |
9b761dbd |
168 | _("Query:") . ' '. |
169 | htmlspecialchars($query) . '<br>' . |
b8c285ab |
170 | _("Server responded: ") . |
9b761dbd |
171 | htmlspecialchars($message) . "</font><br>\n"; |
1f720b34 |
172 | error_box($string,$color); |
9c737111 |
173 | exit; |
b8c285ab |
174 | } |
175 | else { |
3411d4ec |
176 | return $resultlist; |
9c737111 |
177 | } |
9c737111 |
178 | } |
179 | |
b3837b0f |
180 | function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') { |
863936bb |
181 | $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message, $query); |
182 | |
183 | /* sqimap_read_data should be called for one response |
184 | but since it just calls sqimap_read_data_list which |
185 | handles multiple responses we need to check for that |
186 | and merge the $res array IF they are seperated and |
187 | IF it was a FETCH response. */ |
188 | |
189 | if (isset($res[1]) && is_array($res[1]) && isset($res[1][0]) |
190 | && preg_match('/^\* \d+ FETCH/', $res[1][0])) { |
191 | $result = array(); |
192 | foreach($res as $index=>$value) { |
193 | $result = array_merge($result, $res["$index"]); |
56afb33f |
194 | } |
195 | } |
863936bb |
196 | if (isset($result)) { |
197 | return $result; |
56afb33f |
198 | } |
56afb33f |
199 | else { |
863936bb |
200 | return $res[0]; |
56afb33f |
201 | } |
202 | |
9c737111 |
203 | } |
204 | |
3411d4ec |
205 | /* |
206 | * Logs the user into the imap server. If $hide is set, no error messages |
207 | * will be displayed. This function returns the imap connection handle. |
208 | */ |
9c737111 |
209 | function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) { |
47a29326 |
210 | global $color, $squirrelmail_language, $onetimepad, $use_imap_tls, $imap_auth_mech; |
85fc999e |
211 | |
bd9829d7 |
212 | $imap_server_address = sqimap_get_user_server($imap_server_address, $username); |
47a29326 |
213 | $host=$imap_server_address; |
214 | |
215 | if (($use_imap_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) { |
216 | /* Use TLS by prefixing "tls://" to the hostname */ |
217 | $imap_server_address = 'tls://' . $imap_server_address; |
218 | } |
219 | |
3411d4ec |
220 | $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15); |
85fc999e |
221 | |
3411d4ec |
222 | /* Do some error correction */ |
9c737111 |
223 | if (!$imap_stream) { |
224 | if (!$hide) { |
441f2d33 |
225 | set_up_language($squirrelmail_language, true); |
1f720b34 |
226 | require_once(SM_PATH . 'functions/display_messages.php'); |
227 | $string = sprintf (_("Error connecting to IMAP server: %s.") . |
228 | "<br>\r\n", $imap_server_address) . |
229 | "$error_number : $error_string<br>\r\n"; |
2f1f7a12 |
230 | logout_error($string,$color); |
9c737111 |
231 | } |
232 | exit; |
233 | } |
052e0c26 |
234 | |
2f1f7a12 |
235 | $server_info = fgets ($imap_stream, 1024); |
236 | |
237 | /* Decrypt the password */ |
238 | $password = OneTimePadDecrypt($password, $onetimepad); |
239 | |
639c7164 |
240 | if (($imap_auth_mech == 'cram-md5') OR ($imap_auth_mech == 'digest-md5')) { |
47a29326 |
241 | // We're using some sort of authentication OTHER than plain |
242 | $tag=sqimap_session_id(false); |
243 | if ($imap_auth_mech == 'digest-md5') { |
244 | $query = $tag . " AUTHENTICATE DIGEST-MD5\r\n"; |
245 | } elseif ($imap_auth_mech == 'cram-md5') { |
246 | $query = $tag . " AUTHENTICATE CRAM-MD5\r\n"; |
247 | } |
248 | fputs($imap_stream,$query); |
249 | $answer=sqimap_fgets($imap_stream); |
250 | // Trim the "+ " off the front |
251 | $response=explode(" ",$answer,3); |
252 | if ($response[0] == '+') { |
253 | // Got a challenge back |
254 | $challenge=$response[1]; |
255 | if ($imap_auth_mech == 'digest-md5') { |
256 | $reply = digest_md5_response($username,$password,$challenge,'imap',$host); |
257 | } elseif ($imap_auth_mech == 'cram-md5') { |
258 | $reply = cram_md5_response($username,$password,$challenge); |
259 | } |
260 | fputs($imap_stream,$reply); |
261 | $read=sqimap_fgets($imap_stream); |
262 | if ($imap_auth_mech == 'digest-md5') { |
263 | // DIGEST-MD5 has an extra step.. |
264 | if (substr($read,0,1) == '+') { // OK so far.. |
265 | fputs($imap_stream,"\r\n"); |
266 | $read=sqimap_fgets($imap_stream); |
267 | } |
268 | } |
269 | $results=explode(" ",$read,3); |
270 | $response=$results[1]; |
271 | $message=$results[2]; |
272 | } else { |
273 | // Fake the response, so the error trap at the bottom will work |
274 | $response="BAD"; |
275 | $message='IMAP server does not appear to support the authentication method selected.'; |
276 | $message .= ' Please contact your system administrator.'; |
277 | } |
278 | } else { |
279 | // Original PLAIN login code |
280 | $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"'; |
281 | $read = sqimap_run_command ($imap_stream, $query, false, $response, $message); |
282 | } |
283 | |
284 | /* If the connection was not successful, lets see why */ |
9c737111 |
285 | if ($response != 'OK') { |
286 | if (!$hide) { |
74424a43 |
287 | if ($response != 'NO') { |
3411d4ec |
288 | /* "BAD" and anything else gets reported here. */ |
9b761dbd |
289 | $message = htmlspecialchars($message); |
9c737111 |
290 | set_up_language($squirrelmail_language, true); |
1f720b34 |
291 | require_once(SM_PATH . 'functions/display_messages.php'); |
9c737111 |
292 | if ($response == 'BAD') { |
1f720b34 |
293 | $string = sprintf (_("Bad request: %s")."<br>\r\n", $message); |
9c737111 |
294 | } else { |
1f720b34 |
295 | $string = sprintf (_("Unknown error: %s") . "<br>\n", $message); |
9c737111 |
296 | } |
1f720b34 |
297 | $string .= '<br>' . _("Read data:") . "<br>\n"; |
9c737111 |
298 | if (is_array($read)) { |
299 | foreach ($read as $line) { |
1f720b34 |
300 | $string .= htmlspecialchars($line) . "<br>\n"; |
9c737111 |
301 | } |
302 | } |
1f720b34 |
303 | error_box($string,$color); |
9c737111 |
304 | exit; |
165e24a7 |
305 | } else { |
3411d4ec |
306 | /* |
307 | * If the user does not log in with the correct |
1c72b151 |
308 | * username and password it is not possible to get the |
309 | * correct locale from the user's preferences. |
310 | * Therefore, apply the same hack as on the login |
311 | * screen. |
3411d4ec |
312 | * |
313 | * $squirrelmail_language is set by a cookie when |
1c72b151 |
314 | * the user selects language and logs out |
bee165ef |
315 | */ |
9be8198d |
316 | |
9c737111 |
317 | set_up_language($squirrelmail_language, true); |
bd9c880b |
318 | include_once(SM_PATH . 'functions/display_messages.php' ); |
69146537 |
319 | sqsession_destroy(); |
bd9c880b |
320 | logout_error( _("Unknown user or password incorrect.") ); |
9c737111 |
321 | exit; |
052e0c26 |
322 | } |
9c737111 |
323 | } else { |
052e0c26 |
324 | exit; |
9c737111 |
325 | } |
326 | } |
9c737111 |
327 | return $imap_stream; |
328 | } |
f1e6f580 |
329 | |
3411d4ec |
330 | /* Simply logs out the IMAP session */ |
9c737111 |
331 | function sqimap_logout ($imap_stream) { |
8d936b0c |
332 | /* Logout is not valid until the server returns 'BYE' |
333 | * If we don't have an imap_ stream we're already logged out */ |
26a2cc8b |
334 | if(isset($imap_stream) && $imap_stream) |
8d936b0c |
335 | sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message); |
9c737111 |
336 | } |
337 | |
487daa81 |
338 | function sqimap_capability($imap_stream, $capability='') { |
9c737111 |
339 | global $sqimap_capabilities; |
9c737111 |
340 | if (!is_array($sqimap_capabilities)) { |
1c72b151 |
341 | $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b); |
342 | |
9c737111 |
343 | $c = explode(' ', $read[0]); |
344 | for ($i=2; $i < count($c); $i++) { |
345 | $cap_list = explode('=', $c[$i]); |
3411d4ec |
346 | if (isset($cap_list[1])) { |
9c737111 |
347 | $sqimap_capabilities[$cap_list[0]] = $cap_list[1]; |
3411d4ec |
348 | } else { |
9c737111 |
349 | $sqimap_capabilities[$cap_list[0]] = TRUE; |
3411d4ec |
350 | } |
f1e6f580 |
351 | } |
9c737111 |
352 | } |
487daa81 |
353 | if ($capability) { |
354 | if (isset($sqimap_capabilities[$capability])) { |
355 | return $sqimap_capabilities[$capability]; |
356 | } else { |
357 | return false; |
358 | } |
f1e6f580 |
359 | } |
487daa81 |
360 | return $sqimap_capabilities; |
9c737111 |
361 | } |
362 | |
3411d4ec |
363 | /* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */ |
9c737111 |
364 | function sqimap_get_delimiter ($imap_stream = false) { |
3411d4ec |
365 | global $sqimap_delimiter, $optional_delimiter; |
85fc999e |
366 | |
9c737111 |
367 | /* Use configured delimiter if set */ |
368 | if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') { |
369 | return $optional_delimiter; |
370 | } |
85fc999e |
371 | |
9c737111 |
372 | /* Do some caching here */ |
373 | if (!$sqimap_delimiter) { |
374 | if (sqimap_capability($imap_stream, 'NAMESPACE')) { |
3411d4ec |
375 | /* |
376 | * According to something that I can't find, this is supposed to work on all systems |
377 | * OS: This won't work in Courier IMAP. |
378 | * OS: According to rfc2342 response from NAMESPACE command is: |
379 | * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES) |
380 | * OS: We want to lookup all personal NAMESPACES... |
381 | */ |
1c72b151 |
382 | $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b); |
f1e6f580 |
383 | if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) { |
9c737111 |
384 | if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) { |
f1e6f580 |
385 | $pn = $data2[1]; |
9c737111 |
386 | } |
f1e6f580 |
387 | $pna = explode(')(', $pn); |
9c737111 |
388 | while (list($k, $v) = each($pna)) { |
862ff2d3 |
389 | $lst = explode('"', $v); |
390 | if (isset($lst[3])) { |
391 | $pn[$lst[1]] = $lst[3]; |
392 | } else { |
74424a43 |
393 | $pn[$lst[1]] = ''; |
862ff2d3 |
394 | } |
f1e6f580 |
395 | } |
396 | } |
397 | $sqimap_delimiter = $pn[0]; |
398 | } else { |
399 | fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n"); |
400 | $read = sqimap_read_data($imap_stream, '.', true, $a, $b); |
401 | $quote_position = strpos ($read[0], '"'); |
402 | $sqimap_delimiter = substr ($read[0], $quote_position+1, 1); |
403 | } |
404 | } |
405 | return $sqimap_delimiter; |
9c737111 |
406 | } |
052e0c26 |
407 | |
408 | |
3411d4ec |
409 | /* Gets the number of messages in the current mailbox. */ |
9c737111 |
410 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
1f720b34 |
411 | $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", false, $result, $message); |
9c737111 |
412 | for ($i = 0; $i < count($read_ary); $i++) { |
85fc999e |
413 | if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) { |
414 | return $regs[1]; |
415 | } |
9c737111 |
416 | } |
1f720b34 |
417 | return false; //"BUG! Couldn't get number of messages in $mailbox!"; |
9c737111 |
418 | } |
419 | |
420 | |
3411d4ec |
421 | /* Returns a displayable email address. |
422 | * Luke Ehresman <lehresma@css.tayloru.edu> |
423 | * "Luke Ehresman" <lehresma@css.tayloru.edu> |
424 | * <lehresma@css.tayloru.edu> |
425 | * lehresma@css.tayloru.edu (Luke Ehresman) |
426 | * lehresma@css.tayloru.edu |
427 | * becomes: lehresma@css.tayloru.edu |
428 | */ |
9c737111 |
429 | function sqimap_find_email ($string) { |
9c737111 |
430 | if (ereg("<([^>]+)>", $string, $regs)) { |
431 | $string = $regs[1]; |
3411d4ec |
432 | } else if (ereg("([^ ]+@[^ ]+)", $string, $regs)) { |
433 | $string = $regs[1]; |
9c737111 |
434 | } |
435 | return trim($string); |
436 | } |
437 | |
438 | |
439 | /* |
3411d4ec |
440 | * Takes the From: field and creates a displayable name. |
441 | * Luke Ehresman <lkehresman@yahoo.com> |
442 | * "Luke Ehresman" <lkehresman@yahoo.com> |
443 | * lkehresman@yahoo.com (Luke Ehresman) |
444 | * becomes: Luke Ehresman |
445 | * <lkehresman@yahoo.com> |
446 | * becomes: lkehresman@yahoo.com |
6282af09 |
447 | */ |
9c737111 |
448 | function sqimap_find_displayable_name ($string) { |
6282af09 |
449 | $string = trim($string); |
2f7bda0a |
450 | |
7e3de682 |
451 | if ( ereg('^(.+)<.*>', $string, $regs) ) { |
092d4f2c |
452 | $orig_string = $string; |
2f7bda0a |
453 | $string = str_replace ('"', '', $regs[1] ); |
092d4f2c |
454 | if (trim($string) == '') { |
455 | $string = sqimap_find_email($orig_string); |
456 | } |
33565ec4 |
457 | if( $string == '' || $string == ' ' ){ |
1ba8cd6b |
458 | $string = ' '; |
33565ec4 |
459 | } |
7e3de682 |
460 | } |
461 | elseif ( ereg('\((.*)\)', $string, $regs) ) { |
9e9c63e4 |
462 | if( ( $regs[1] == '' ) || ( $regs[1] == ' ' ) ){ |
463 | if ( ereg('^(.+) \(', $string, $regs) ) { |
464 | $string = ereg_replace( ' \(\)$', '', $string ); |
465 | } else { |
1ba8cd6b |
466 | $string = ' '; |
9e9c63e4 |
467 | } |
468 | } else { |
469 | $string = $regs[1]; |
470 | } |
7e3de682 |
471 | } |
472 | else { |
2f7bda0a |
473 | $string = str_replace ('"', '', sqimap_find_email($string)); |
9c737111 |
474 | } |
9c737111 |
475 | |
7e3de682 |
476 | return trim($string); |
477 | } |
85fc999e |
478 | |
9c737111 |
479 | /* |
3411d4ec |
480 | * Returns the number of unseen messages in this folder |
481 | */ |
9c737111 |
482 | function sqimap_unseen_messages ($imap_stream, $mailbox) { |
1f720b34 |
483 | $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", false, $result, $message); |
ea7ff111 |
484 | $i = 0; |
1f720b34 |
485 | $regs = array(false, false); |
ea7ff111 |
486 | while (isset($read_ary[$i])) { |
487 | if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) { |
488 | break; |
489 | } |
490 | $i++; |
491 | } |
9c737111 |
492 | return $regs[1]; |
493 | } |
494 | |
1f720b34 |
495 | /* |
496 | * Returns the number of unseen/total messages in this folder |
497 | */ |
498 | function sqimap_status_messages ($imap_stream, $mailbox) { |
499 | $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (MESSAGES UNSEEN)", false, $result, $message); |
500 | $i = 0; |
501 | $messages = $unseen = false; |
502 | $regs = array(false,false); |
503 | while (isset($read_ary[$i])) { |
504 | if (preg_match('/UNSEEN\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
505 | $unseen = $regs[1]; |
506 | } |
507 | if (preg_match('/MESSAGES\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
508 | $messages = $regs[1]; |
509 | } |
510 | $i++; |
511 | } |
512 | return array('MESSAGES' => $messages, 'UNSEEN'=>$unseen); |
513 | } |
514 | |
9c737111 |
515 | |
516 | /* |
3411d4ec |
517 | * Saves a message to a given folder -- used for saving sent messages |
518 | */ |
9c737111 |
519 | function sqimap_append ($imap_stream, $sent_folder, $length) { |
520 | fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n"); |
85fc999e |
521 | $tmp = fgets ($imap_stream, 1024); |
9c737111 |
522 | } |
523 | |
8813fb15 |
524 | function sqimap_append_done ($imap_stream, $folder='') { |
1f720b34 |
525 | global $squirrelmail_language, $color; |
9c737111 |
526 | fputs ($imap_stream, "\r\n"); |
527 | $tmp = fgets ($imap_stream, 1024); |
69146537 |
528 | if (preg_match("/(.*)(BAD|NO)(.*)$/", $tmp, $regs)) { |
529 | set_up_language($squirrelmail_language); |
1f720b34 |
530 | require_once(SM_PATH . 'functions/display_messages.php'); |
8813fb15 |
531 | $reason = $regs[3]; |
532 | if ($regs[2] == 'NO') { |
533 | $string = "<b><font color=$color[2]>\n" . |
534 | _("ERROR : Could not append message to") ." $folder." . |
535 | "</b><br>\n" . |
536 | _("Server responded: ") . |
537 | $reason . "<br>\n"; |
538 | if (preg_match("/(.*)(quota)(.*)$/i", $reason, $regs)) { |
5296b8ef |
539 | $string .= _("Solution: ") . |
540 | _("Remove unneccessary messages from your folder and start with your Trash folder.") |
8813fb15 |
541 | ."<br>\n"; |
542 | } |
543 | $string .= "</font>\n"; |
544 | error_box($string,$color); |
545 | } else { |
546 | $string = "<b><font color=$color[2]>\n" . |
1f720b34 |
547 | _("ERROR : Bad or malformed request.") . |
548 | "</b><br>\n" . |
549 | _("Server responded: ") . |
550 | $tmp . "</font><br>\n"; |
8813fb15 |
551 | error_box($string,$color); |
552 | exit; |
553 | } |
69146537 |
554 | } |
9c737111 |
555 | } |
85fc999e |
556 | |
bd9829d7 |
557 | function sqimap_get_user_server ($imap_server, $username) { |
bd9829d7 |
558 | if (substr($imap_server, 0, 4) != "map:") { |
559 | return $imap_server; |
560 | } |
bd9829d7 |
561 | $function = substr($imap_server, 4); |
562 | return $function($username); |
563 | } |
564 | |
565 | /* This is an example that gets imapservers from yellowpages (NIS). |
566 | * you can simple put map:map_yp_alias in your $imap_server_address |
567 | * in config.php use your own function instead map_yp_alias to map your |
568 | * LDAP whatever way to find the users imapserver. */ |
569 | |
570 | function map_yp_alias($username) { |
571 | $yp = `ypmatch $username aliases`; |
572 | return chop(substr($yp, strlen($username)+1)); |
573 | } |
574 | |
15e6162e |
575 | ?> |