16f19b0ccb2bddc7526223e1846db17ab7e86fa2
5 ** Copyright (c) 1999-2001 The Squirrelmail Development Team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
8 ** This implements all functions that do general imap functions.
13 global $imap_general_debug;
14 $imap_general_debug = false;
16 /******************************************************************************
17 ** Sets an unique session id in order to avoid simultanous sessions crash.
18 ******************************************************************************/
20 function sqimap_session_id() {
21 return( substr( session_id(), -4 ) );
25 /******************************************************************************
26 ** Reads the output from the IMAP stream. If handle_errors is set to true,
27 ** this will also handle all errors that are received. If it is not set,
28 ** the errors will be sent back through $response and $message
29 ******************************************************************************/
31 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
32 &$response, &$message) {
33 global $color, $squirrelmail_language;
34 global $imap_general_debug;
37 $resultlist = array();
43 while (strpos($read, "\n") === false) {
44 $read .= fgets($imap_stream, 9096);
47 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
49 } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
50 // Sizeless response, probably single-line
52 // For debugging purposes
53 if ($imap_general_debug) {
54 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
60 $read = fgets($imap_stream, 9096);
65 while (strpos($read, "\n") === false) {
66 $read .= fgets($imap_stream, 9096);
69 // For debugging purposes
70 if ($imap_general_debug) {
71 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
75 // If we know the size, no need to look at the end parameters
77 if ($total_size == $size) {
78 // We've reached the end of this 'message', switch to the next one.
81 } else if ($total_size > $size) {
82 $difference = $total_size - $size;
83 $total_size = $total_size - strlen($read);
84 $data[] = substr ($read, 0, strlen($read)-$difference);
85 $read = substr ($read, strlen($read)-$difference, strlen($read));
89 $read = fgets($imap_stream, 9096);
90 while (strpos($read, "\n") === false) {
91 $read .= fgets($imap_stream, 9096);
94 $total_size +
= strlen($read);
96 if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
97 (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) {
101 $read = fgets ($imap_stream, 9096);
106 while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
107 !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
108 $read = fgets($imap_stream, 9096);
110 $resultlist[] = $data;
112 $response = $regs[1];
113 $message = trim($regs[2]);
115 if ($imap_general_debug) { echo '--<br>'; }
116 if ($handle_errors == false) { return $resultlist; }
118 if ($response == 'NO') {
119 // ignore this error from m$ exchange, it is not fatal (aka bug)
120 if (strstr($message, 'command resulted in') === false) {
121 set_up_language($squirrelmail_language);
122 echo "<br><b><font color=$color[2]>\n";
123 echo _("ERROR : Could not complete request.");
125 echo _("Reason Given: ");
126 echo $message . "</font><br>\n";
129 } else if ($response == 'BAD') {
130 set_up_language($squirrelmail_language);
131 echo "<br><b><font color=$color[2]>\n";
132 echo _("ERROR : Bad or malformed request.");
134 echo _("Server responded: ");
135 echo $message . "</font><br>\n";
141 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
142 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message);
146 /******************************************************************************
147 ** Logs the user into the imap server. If $hide is set, no error messages
148 ** will be displayed. This function returns the imap connection handle.
149 ******************************************************************************/
150 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
151 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
153 $imap_stream = fsockopen ($imap_server_address, $imap_port,
154 $error_number, $error_string, 15);
155 $server_info = fgets ($imap_stream, 1024);
157 // Decrypt the password
158 $password = OneTimePadDecrypt($password, $onetimepad);
160 /** Do some error correction **/
163 set_up_language($squirrelmail_language, true);
164 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
165 echo "$error_number : $error_string<br>\r\n";
170 fputs ($imap_stream, sqimap_session_id() . ' LOGIN "' . quoteIMAP($username) .
171 '" "' . quoteIMAP($password) . "\"\r\n");
172 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), false, $response, $message);
174 /** If the connection was not successful, lets see why **/
175 if ($response != "OK") {
177 if ($response != 'NO') {
178 // "BAD" and anything else gets reported here.
179 set_up_language($squirrelmail_language, true);
180 if ($response == 'BAD')
181 printf (_("Bad request: %s")."<br>\r\n", $message);
183 printf (_("Unknown error: %s") . "<br>\n", $message);
185 echo _("Read data:") . "<br>\n";
188 foreach ($read as $line)
190 echo htmlspecialchars($line) . "<br>\n";
195 // If the user does not log in with the correct
196 // username and password it is not possible to get the
197 // correct locale from the user's preferences.
198 // Therefore, apply the same hack as on the login
201 // $squirrelmail_language is set by a cookie when
202 // the user selects language and logs out
204 set_up_language($squirrelmail_language, true);
208 <body bgcolor
="ffffff">
211 <table width
="70%" noborder bgcolor
="ffffff" align
="center">
213 <td bgcolor
="dcdcdc">
214 <font color
="cc0000">
216 <?php
echo _("ERROR") ?
>
224 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
225 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
248 /******************************************************************************
249 ** Simply logs out the imap session
250 ******************************************************************************/
251 function sqimap_logout ($imap_stream) {
252 fputs ($imap_stream, sqimap_session_id() . " LOGOUT\r\n");
255 function sqimap_capability($imap_stream, $capability) {
256 global $sqimap_capabilities;
257 global $imap_general_debug;
259 if (!is_array($sqimap_capabilities)) {
260 fputs ($imap_stream, sqimap_session_id() . " CAPABILITY\r\n");
261 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
263 $c = explode(' ', $read[0]);
264 for ($i=2; $i < count($c); $i++
) {
265 $cap_list = explode('=', $c[$i]);
266 if (isset($cap_list[1]))
267 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
269 $sqimap_capabilities[$cap_list[0]] = TRUE;
272 if (! isset($sqimap_capabilities[$capability]))
274 return $sqimap_capabilities[$capability];
277 /******************************************************************************
278 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
279 ******************************************************************************/
280 function sqimap_get_delimiter ($imap_stream = false) {
281 global $imap_general_debug;
282 global $sqimap_delimiter;
283 global $optional_delimiter;
285 /* Use configured delimiter if set */
286 if((!empty($optional_delimiter)) && $optional_delimiter != "detect")
287 return $optional_delimiter;
289 /* Do some caching here */
290 if (!$sqimap_delimiter) {
291 if (sqimap_capability($imap_stream, "NAMESPACE")) {
292 /* According to something that I can't find, this is supposed to work on all systems
293 OS: This won't work in Courier IMAP.
294 OS: According to rfc2342 response from NAMESPACE command is:
295 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
296 OS: We want to lookup all personal NAMESPACES...
298 fputs ($imap_stream, sqimap_session_id() . " NAMESPACE\r\n");
299 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
300 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
301 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2))
303 $pna = explode(')(', $pn);
304 while (list($k, $v) = each($pna))
306 $lst = explode('"', $v);
307 if (isset($lst[3])) {
308 $pn[$lst[1]] = $lst[3];
314 $sqimap_delimiter = $pn[0];
316 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
317 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
318 $quote_position = strpos ($read[0], '"');
319 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
322 return $sqimap_delimiter;
326 /******************************************************************************
327 ** Gets the number of messages in the current mailbox.
328 ******************************************************************************/
329 function sqimap_get_num_messages ($imap_stream, $mailbox) {
330 fputs ($imap_stream, sqimap_session_id() . " EXAMINE \"$mailbox\"\r\n");
331 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
332 for ($i = 0; $i < count($read_ary); $i++
) {
333 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
337 return "BUG! Couldn't get number of messages in $mailbox!";
341 /******************************************************************************
342 ** Returns a displayable email address
343 ******************************************************************************/
344 function sqimap_find_email ($string) {
345 /** Luke Ehresman <lehresma@css.tayloru.edu>
346 ** <lehresma@css.tayloru.edu>
347 ** lehresma@css.tayloru.edu
350 ** lehresma@css.tayloru.edu (Luke Ehresman)
353 if (ereg("<([^>]+)>", $string, $regs)) {
356 return trim($string);
360 /******************************************************************************
361 ** Takes the From: field, and creates a displayable name.
362 ** Luke Ehresman <lkehresman@yahoo.com>
363 ** becomes: Luke Ehresman
364 ** <lkehresman@yahoo.com>
365 ** becomes: lkehresman@yahoo.com
366 ******************************************************************************/
367 function sqimap_find_displayable_name ($string) {
368 $string = ' '.trim($string);
369 $orig_string = $string;
370 if (strpos($string, '<') && strpos($string, '>')) {
371 if (strpos($string, '<') == 1) {
372 $string = sqimap_find_email($string);
374 $string = trim($string);
375 $string = substr($string, 0, strpos($string, '<'));
376 $string = ereg_replace ('"', '', $string);
379 if (trim($string) == '') {
380 $string = sqimap_find_email($orig_string);
387 /******************************************************************************
388 ** Returns the number of unseen messages in this folder
389 ******************************************************************************/
390 function sqimap_unseen_messages ($imap_stream, $mailbox) {
391 //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n");
392 fputs ($imap_stream, sqimap_session_id() . " STATUS \"$mailbox\" (UNSEEN)\r\n");
393 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
394 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
399 /******************************************************************************
400 ** Saves a message to a given folder -- used for saving sent messages
401 ******************************************************************************/
402 function sqimap_append ($imap_stream, $sent_folder, $length) {
403 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
404 $tmp = fgets ($imap_stream, 1024);
407 function sqimap_append_done ($imap_stream) {
408 fputs ($imap_stream, "\r\n");
409 $tmp = fgets ($imap_stream, 1024);