5 ** This implements all functions that do general imap functions.
10 if (defined ('imap_general_php'))
12 define ('imap_general_php', true);
14 global $imap_general_debug;
15 $imap_general_debug = false;
17 /******************************************************************************
18 ** Sets an unique session id in order to avoid simultanous sessions crash.
19 ******************************************************************************/
21 function sqimap_session_id() {
22 return( substr( session_id(), -4 ) );
26 /******************************************************************************
27 ** Reads the output from the IMAP stream. If handle_errors is set to true,
28 ** this will also handle all errors that are received. If it is not set,
29 ** the errors will be sent back through $response and $message
30 ******************************************************************************/
32 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
33 &$response, &$message) {
34 global $color, $squirrelmail_language;
35 global $imap_general_debug;
38 $resultlist = array();
44 while (strpos($read, "\n") === false) {
45 $read .= fgets($imap_stream, 9096);
48 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
50 } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
51 // 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";
59 $read = fgets($imap_stream, 9096);
64 while (strpos($read, "\n") === false) {
65 $read .= fgets($imap_stream, 9096);
67 // For debugging purposes
68 if ($imap_general_debug) {
69 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
72 // If we know the size, no need to look at the end parameters
74 if ($total_size == $size) {
75 // We've reached the end of this 'message', switch to the next one.
78 } else if ($total_size > $size) {
79 $difference = $total_size - $size;
80 $total_size = $total_size - strlen($read);
81 $data[] = substr ($read, 0, strlen($read)-$difference);
82 $read = substr ($read, strlen($read)-$difference, strlen($read));
86 $read = fgets($imap_stream, 9096);
87 while (strpos($read, "\n") === false) {
88 $read .= fgets($imap_stream, 9096);
91 $total_size +
= strlen($read);
93 if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
94 ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
98 $read = fgets ($imap_stream, 9096);
103 while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
104 !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
105 $read = fgets($imap_stream, 9096);
107 $resultlist[] = $data;
109 $response = $regs[1];
110 $message = trim($regs[2]);
112 if ($imap_general_debug) echo '--<br>';
114 if ($handle_errors == false)
117 if ($response == 'NO') {
118 // ignore this error from m$ exchange, it is not fatal (aka bug)
119 if (strstr($message, 'command resulted in') === false) {
120 set_up_language($squirrelmail_language);
121 echo "<br><b><font color=$color[2]>\n";
122 echo _("ERROR : Could not complete request.");
124 echo _("Reason Given: ");
125 echo $message . "</font><br>\n";
128 } else if ($response == 'BAD') {
129 set_up_language($squirrelmail_language);
130 echo "<br><b><font color=$color[2]>\n";
131 echo _("ERROR : Bad or malformed request.");
133 echo _("Server responded: ");
134 echo $message . "</font><br>\n";
140 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
141 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message);
145 /******************************************************************************
146 ** Logs the user into the imap server. If $hide is set, no error messages
147 ** will be displayed. This function returns the imap connection handle.
148 ******************************************************************************/
149 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
150 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
152 $imap_stream = fsockopen ($imap_server_address, $imap_port,
153 $error_number, $error_string, 15);
154 $server_info = fgets ($imap_stream, 1024);
156 // Decrypt the password
157 $password = OneTimePadDecrypt($password, $onetimepad);
159 /** Do some error correction **/
162 set_up_language($squirrelmail_language, true);
163 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
164 echo "$error_number : $error_string<br>\r\n";
169 fputs ($imap_stream, sqimap_session_id() . ' LOGIN "' . quoteIMAP($username) .
170 '" "' . quoteIMAP($password) . "\"\r\n");
171 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), false, $response, $message);
173 /** If the connection was not successful, lets see why **/
174 if ($response != "OK") {
176 if ($response != 'NO') {
177 // "BAD" and anything else gets reported here.
178 set_up_language($squirrelmail_language, true);
179 if ($response == 'BAD')
180 printf (_("Bad request: %s")."<br>\r\n", $message);
182 printf (_("Unknown error: %s") . "<br>\n", $message);
184 echo _("Read data:") . "<br>\n";
187 foreach ($read as $line)
189 echo htmlspecialchars($line) . "<br>\n";
194 // If the user does not log in with the correct
195 // username and password it is not possible to get the
196 // correct locale from the user's preferences.
197 // Therefore, apply the same hack as on the login
200 // $squirrelmail_language is set by a cookie when
201 // the user selects language and logs out
203 set_up_language($squirrelmail_language, true);
207 <body bgcolor
="ffffff">
210 <table width
="70%" noborder bgcolor
="ffffff" align
="center">
212 <td bgcolor
="dcdcdc">
213 <font color
="cc0000">
215 <?php
echo _("ERROR") ?
>
223 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
224 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
247 /******************************************************************************
248 ** Simply logs out the imap session
249 ******************************************************************************/
250 function sqimap_logout ($imap_stream) {
251 fputs ($imap_stream, sqimap_session_id() . " LOGOUT\r\n");
254 function sqimap_capability($imap_stream, $capability) {
255 global $sqimap_capabilities;
256 global $imap_general_debug;
258 if (!is_array($sqimap_capabilities)) {
259 fputs ($imap_stream, sqimap_session_id() . " CAPABILITY\r\n");
260 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
262 $c = explode(' ', $read[0]);
263 for ($i=2; $i < count($c); $i++
) {
264 $cap_list = explode('=', $c[$i]);
265 if (isset($cap_list[1]))
266 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
268 $sqimap_capabilities[$cap_list[0]] = TRUE;
271 if (! isset($sqimap_capabilities[$capability]))
273 return $sqimap_capabilities[$capability];
276 /******************************************************************************
277 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
278 ******************************************************************************/
279 function sqimap_get_delimiter ($imap_stream = false) {
280 global $imap_general_debug;
281 global $sqimap_delimiter;
282 global $optional_delimiter;
284 /* Use configured delimiter if set */
285 if((!empty($optional_delimiter)) && $optional_delimiter != "detect")
286 return $optional_delimiter;
288 /* Do some caching here */
289 if (!$sqimap_delimiter) {
290 if (sqimap_capability($imap_stream, "NAMESPACE")) {
291 /* According to something that I can't find, this is supposed to work on all systems
292 OS: This won't work in Courier IMAP.
293 OS: According to rfc2342 response from NAMESPACE command is:
294 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
295 OS: We want to lookup all personal NAMESPACES...
297 fputs ($imap_stream, sqimap_session_id() . " NAMESPACE\r\n");
298 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
299 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
300 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2))
302 $pna = explode(')(', $pn);
303 while (list($k, $v) = each($pna))
305 $lst = explode('"', $v);
306 if (isset($lst[3])) {
307 $pn[$lst[1]] = $lst[3];
313 $sqimap_delimiter = $pn[0];
315 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
316 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
317 $quote_position = strpos ($read[0], '"');
318 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
321 return $sqimap_delimiter;
325 /******************************************************************************
326 ** Gets the number of messages in the current mailbox.
327 ******************************************************************************/
328 function sqimap_get_num_messages ($imap_stream, $mailbox) {
329 fputs ($imap_stream, sqimap_session_id() . " EXAMINE \"$mailbox\"\r\n");
330 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
331 for ($i = 0; $i < count($read_ary); $i++
) {
332 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
336 return "BUG! Couldn't get number of messages in $mailbox!";
340 /******************************************************************************
341 ** Returns a displayable email address
342 ******************************************************************************/
343 function sqimap_find_email ($string) {
344 /** Luke Ehresman <lehresma@css.tayloru.edu>
345 ** <lehresma@css.tayloru.edu>
346 ** lehresma@css.tayloru.edu
349 ** lehresma@css.tayloru.edu (Luke Ehresman)
352 if (ereg("<([^>]+)>", $string, $regs)) {
355 return trim($string);
359 /******************************************************************************
360 ** Takes the From: field, and creates a displayable name.
361 ** Luke Ehresman <lkehresman@yahoo.com>
362 ** becomes: Luke Ehresman
363 ** <lkehresman@yahoo.com>
364 ** becomes: lkehresman@yahoo.com
365 ******************************************************************************/
366 function sqimap_find_displayable_name ($string) {
367 $string = ' '.trim($string);
368 $orig_string = $string;
369 if (strpos($string, '<') && strpos($string, '>')) {
370 if (strpos($string, '<') == 1) {
371 $string = sqimap_find_email($string);
373 $string = trim($string);
374 $string = substr($string, 0, strpos($string, '<'));
375 $string = ereg_replace ('"', '', $string);
378 if (trim($string) == '') {
379 $string = sqimap_find_email($orig_string);
386 /******************************************************************************
387 ** Returns the number of unseen messages in this folder
388 ******************************************************************************/
389 function sqimap_unseen_messages ($imap_stream, $mailbox) {
390 //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n");
391 fputs ($imap_stream, sqimap_session_id() . " STATUS \"$mailbox\" (UNSEEN)\r\n");
392 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
393 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
398 /******************************************************************************
399 ** Saves a message to a given folder -- used for saving sent messages
400 ******************************************************************************/
401 function sqimap_append ($imap_stream, $sent_folder, $length) {
402 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
403 $tmp = fgets ($imap_stream, 1024);
406 function sqimap_append_done ($imap_stream) {
407 fputs ($imap_stream, "\r\n");
408 $tmp = fgets ($imap_stream, 1024);