8bd2485aee551001430052b15421889c12bb489e
6 * Copyright (c) 1999-2001 The Squirrelmail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This implements all functions that do general imap functions.
17 * Sets an unique session id in order to avoid simultanous sessions crash.
19 * @return string a 4 chars unique string
21 function sqimap_session_id() {
23 $IMAPSessionID = substr(session_id(), -4);
24 if( $IMAPSessionID == '' ) {
25 $IMAPSessionID = GenerateRandomString(4, '', 7);
28 return( $IMAPSessionID );
32 /******************************************************************************
33 ** Reads the output from the IMAP stream. If handle_errors is set to true,
34 ** this will also handle all errors that are received. If it is not set,
35 ** the errors will be sent back through $response and $message
36 ******************************************************************************/
38 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
39 &$response, &$message) {
40 global $color, $squirrelmail_language;
43 $resultlist = array();
49 while (strpos($read, "\n") === false) {
50 $read .= fgets($imap_stream, 9096);
53 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
55 } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
56 // Sizeless response, probably single-line
59 $read = fgets($imap_stream, 9096);
64 while (strpos($read, "\n") === false) {
65 $read .= fgets($imap_stream, 9096);
67 // If we know the size, no need to look at the end parameters
69 if ($total_size == $size) {
70 // We've reached the end of this 'message', switch to the next one.
73 } else if ($total_size > $size) {
74 $difference = $total_size - $size;
75 $total_size = $total_size - strlen($read);
76 $data[] = substr ($read, 0, strlen($read)-$difference);
77 $read = substr ($read, strlen($read)-$difference, strlen($read));
81 $read = fgets($imap_stream, 9096);
82 while (strpos($read, "\n") === false) {
83 $read .= fgets($imap_stream, 9096);
86 $total_size +
= strlen($read);
88 if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
89 (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) {
93 $read = fgets ($imap_stream, 9096);
98 while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
99 !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
100 $read = fgets($imap_stream, 9096);
102 $resultlist[] = $data;
104 $response = $regs[1];
105 $message = trim($regs[2]);
107 if ($handle_errors == false) { return $resultlist; }
109 if ($response == 'NO') {
110 // ignore this error from m$ exchange, it is not fatal (aka bug)
111 if (strstr($message, 'command resulted in') === false) {
112 set_up_language($squirrelmail_language);
113 echo "<br><b><font color=$color[2]>\n";
114 echo _("ERROR : Could not complete request.");
116 echo _("Reason Given: ");
117 echo $message . "</font><br>\n";
120 } else if ($response == 'BAD') {
121 set_up_language($squirrelmail_language);
122 echo "<br><b><font color=$color[2]>\n";
123 echo _("ERROR : Bad or malformed request.");
125 echo _("Server responded: ");
126 echo $message . "</font><br>\n";
132 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
133 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message);
137 /******************************************************************************
138 ** Logs the user into the imap server. If $hide is set, no error messages
139 ** will be displayed. This function returns the imap connection handle.
140 ******************************************************************************/
141 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
143 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
145 $imap_stream = fsockopen ( $imap_server_address, $imap_port,
146 $error_number, $error_string, 15);
147 $server_info = fgets ($imap_stream, 1024);
149 // Decrypt the password
150 $password = OneTimePadDecrypt($password, $onetimepad);
152 /** Do some error correction **/
155 set_up_language($squirrelmail_language, true);
156 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
157 echo "$error_number : $error_string<br>\r\n";
162 fputs ($imap_stream, sqimap_session_id() . ' LOGIN "' . quoteIMAP($username) .
163 '" "' . quoteIMAP($password) . "\"\r\n");
164 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), false, $response, $message);
166 /** If the connection was not successful, lets see why **/
167 if ($response != "OK") {
169 if ($response != 'NO') {
170 // "BAD" and anything else gets reported here.
171 set_up_language($squirrelmail_language, true);
172 if ($response == 'BAD')
173 printf (_("Bad request: %s")."<br>\r\n", $message);
175 printf (_("Unknown error: %s") . "<br>\n", $message);
177 echo _("Read data:") . "<br>\n";
180 foreach ($read as $line)
182 echo htmlspecialchars($line) . "<br>\n";
187 // If the user does not log in with the correct
188 // username and password it is not possible to get the
189 // correct locale from the user's preferences.
190 // Therefore, apply the same hack as on the login
193 // $squirrelmail_language is set by a cookie when
194 // the user selects language and logs out
196 set_up_language($squirrelmail_language, true);
200 <body bgcolor
="ffffff">
203 <table width
="70%" noborder bgcolor
="ffffff" align
="center">
205 <td bgcolor
="dcdcdc">
206 <font color
="cc0000">
208 <?php
echo _("ERROR") ?
>
216 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
217 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
240 /******************************************************************************
241 ** Simply logs out the imap session
242 ******************************************************************************/
243 function sqimap_logout ($imap_stream) {
244 fputs ($imap_stream, sqimap_session_id() . " LOGOUT\r\n");
247 function sqimap_capability($imap_stream, $capability) {
248 global $sqimap_capabilities;
250 if (!is_array($sqimap_capabilities)) {
251 fputs ($imap_stream, sqimap_session_id() . " CAPABILITY\r\n");
252 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
254 $c = explode(' ', $read[0]);
255 for ($i=2; $i < count($c); $i++
) {
256 $cap_list = explode('=', $c[$i]);
257 if (isset($cap_list[1]))
258 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
260 $sqimap_capabilities[$cap_list[0]] = TRUE;
263 if (! isset($sqimap_capabilities[$capability]))
265 return $sqimap_capabilities[$capability];
268 /******************************************************************************
269 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
270 ******************************************************************************/
271 function sqimap_get_delimiter ($imap_stream = false) {
273 global $sqimap_delimiter;
274 global $optional_delimiter;
276 /* Use configured delimiter if set */
277 if((!empty($optional_delimiter)) && $optional_delimiter != "detect")
278 return $optional_delimiter;
280 /* Do some caching here */
281 if (!$sqimap_delimiter) {
282 if (sqimap_capability($imap_stream, "NAMESPACE")) {
283 /* According to something that I can't find, this is supposed to work on all systems
284 OS: This won't work in Courier IMAP.
285 OS: According to rfc2342 response from NAMESPACE command is:
286 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
287 OS: We want to lookup all personal NAMESPACES...
289 fputs ($imap_stream, sqimap_session_id() . " NAMESPACE\r\n");
290 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
291 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
292 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2))
294 $pna = explode(')(', $pn);
295 while (list($k, $v) = each($pna))
297 $lst = explode('"', $v);
298 if (isset($lst[3])) {
299 $pn[$lst[1]] = $lst[3];
305 $sqimap_delimiter = $pn[0];
307 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
308 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
309 $quote_position = strpos ($read[0], '"');
310 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
313 return $sqimap_delimiter;
317 /******************************************************************************
318 ** Gets the number of messages in the current mailbox.
319 ******************************************************************************/
320 function sqimap_get_num_messages ($imap_stream, $mailbox) {
321 fputs ($imap_stream, sqimap_session_id() . " EXAMINE \"$mailbox\"\r\n");
322 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
323 for ($i = 0; $i < count($read_ary); $i++
) {
324 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
328 return "BUG! Couldn't get number of messages in $mailbox!";
332 /******************************************************************************
333 ** Returns a displayable email address
334 ******************************************************************************/
335 function sqimap_find_email ($string) {
336 /** Luke Ehresman <lehresma@css.tayloru.edu>
337 ** <lehresma@css.tayloru.edu>
338 ** lehresma@css.tayloru.edu
341 ** lehresma@css.tayloru.edu (Luke Ehresman)
344 if (ereg("<([^>]+)>", $string, $regs)) {
347 return trim($string);
351 /******************************************************************************
352 ** Takes the From: field, and creates a displayable name.
353 ** Luke Ehresman <lkehresman@yahoo.com>
354 ** becomes: Luke Ehresman
355 ** <lkehresman@yahoo.com>
356 ** becomes: lkehresman@yahoo.com
357 ******************************************************************************/
358 function sqimap_find_displayable_name ($string) {
359 $string = ' '.trim($string);
360 $orig_string = $string;
361 if (strpos($string, '<') && strpos($string, '>')) {
362 if (strpos($string, '<') == 1) {
363 $string = sqimap_find_email($string);
365 $string = trim($string);
366 $string = substr($string, 0, strpos($string, '<'));
367 $string = ereg_replace ('"', '', $string);
370 if (trim($string) == '') {
371 $string = sqimap_find_email($orig_string);
378 /******************************************************************************
379 ** Returns the number of unseen messages in this folder
380 ******************************************************************************/
381 function sqimap_unseen_messages ($imap_stream, $mailbox) {
382 //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n");
383 fputs ($imap_stream, sqimap_session_id() . " STATUS \"$mailbox\" (UNSEEN)\r\n");
384 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
385 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
390 /******************************************************************************
391 ** Saves a message to a given folder -- used for saving sent messages
392 ******************************************************************************/
393 function sqimap_append ($imap_stream, $sent_folder, $length) {
394 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
395 $tmp = fgets ($imap_stream, 1024);
398 function sqimap_append_done ($imap_stream) {
399 fputs ($imap_stream, "\r\n");
400 $tmp = fgets ($imap_stream, 1024);