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
53 // For debugging purposes
54 if ($imap_general_debug) {
55 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
61 $read = fgets($imap_stream, 9096);
66 while (strpos($read, "\n") === false) {
67 $read .= fgets($imap_stream, 9096);
70 // For debugging purposes
71 if ($imap_general_debug) {
72 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
76 // If we know the size, no need to look at the end parameters
78 if ($total_size == $size) {
79 // We've reached the end of this 'message', switch to the next one.
82 } else if ($total_size > $size) {
83 $difference = $total_size - $size;
84 $total_size = $total_size - strlen($read);
85 $data[] = substr ($read, 0, strlen($read)-$difference);
86 $read = substr ($read, strlen($read)-$difference, strlen($read));
90 $read = fgets($imap_stream, 9096);
91 while (strpos($read, "\n") === false) {
92 $read .= fgets($imap_stream, 9096);
95 $total_size +
= strlen($read);
97 if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
98 (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) {
102 $read = fgets ($imap_stream, 9096);
107 while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
108 !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
109 $read = fgets($imap_stream, 9096);
111 $resultlist[] = $data;
113 $response = $regs[1];
114 $message = trim($regs[2]);
116 if ($imap_general_debug) { echo '--<br>'; }
117 if ($handle_errors == false) { return $resultlist; }
119 if ($response == 'NO') {
120 // ignore this error from m$ exchange, it is not fatal (aka bug)
121 if (strstr($message, 'command resulted in') === false) {
122 set_up_language($squirrelmail_language);
123 echo "<br><b><font color=$color[2]>\n";
124 echo _("ERROR : Could not complete request.");
126 echo _("Reason Given: ");
127 echo $message . "</font><br>\n";
130 } else if ($response == 'BAD') {
131 set_up_language($squirrelmail_language);
132 echo "<br><b><font color=$color[2]>\n";
133 echo _("ERROR : Bad or malformed request.");
135 echo _("Server responded: ");
136 echo $message . "</font><br>\n";
142 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
143 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message);
147 /******************************************************************************
148 ** Logs the user into the imap server. If $hide is set, no error messages
149 ** will be displayed. This function returns the imap connection handle.
150 ******************************************************************************/
151 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
152 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
154 $imap_stream = fsockopen ($imap_server_address, $imap_port,
155 $error_number, $error_string, 15);
156 $server_info = fgets ($imap_stream, 1024);
158 // Decrypt the password
159 $password = OneTimePadDecrypt($password, $onetimepad);
161 /** Do some error correction **/
164 set_up_language($squirrelmail_language, true);
165 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
166 echo "$error_number : $error_string<br>\r\n";
171 fputs ($imap_stream, sqimap_session_id() . ' LOGIN "' . quoteIMAP($username) .
172 '" "' . quoteIMAP($password) . "\"\r\n");
173 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), false, $response, $message);
175 /** If the connection was not successful, lets see why **/
176 if ($response != "OK") {
178 if ($response != 'NO') {
179 // "BAD" and anything else gets reported here.
180 set_up_language($squirrelmail_language, true);
181 if ($response == 'BAD')
182 printf (_("Bad request: %s")."<br>\r\n", $message);
184 printf (_("Unknown error: %s") . "<br>\n", $message);
186 echo _("Read data:") . "<br>\n";
189 foreach ($read as $line)
191 echo htmlspecialchars($line) . "<br>\n";
196 // If the user does not log in with the correct
197 // username and password it is not possible to get the
198 // correct locale from the user's preferences.
199 // Therefore, apply the same hack as on the login
202 // $squirrelmail_language is set by a cookie when
203 // the user selects language and logs out
205 set_up_language($squirrelmail_language, true);
209 <body bgcolor
="ffffff">
212 <table width
="70%" noborder bgcolor
="ffffff" align
="center">
214 <td bgcolor
="dcdcdc">
215 <font color
="cc0000">
217 <?php
echo _("ERROR") ?
>
225 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
226 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
249 /******************************************************************************
250 ** Simply logs out the imap session
251 ******************************************************************************/
252 function sqimap_logout ($imap_stream) {
253 fputs ($imap_stream, sqimap_session_id() . " LOGOUT\r\n");
256 function sqimap_capability($imap_stream, $capability) {
257 global $sqimap_capabilities;
258 global $imap_general_debug;
260 if (!is_array($sqimap_capabilities)) {
261 fputs ($imap_stream, sqimap_session_id() . " CAPABILITY\r\n");
262 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
264 $c = explode(' ', $read[0]);
265 for ($i=2; $i < count($c); $i++
) {
266 $cap_list = explode('=', $c[$i]);
267 if (isset($cap_list[1]))
268 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
270 $sqimap_capabilities[$cap_list[0]] = TRUE;
273 if (! isset($sqimap_capabilities[$capability]))
275 return $sqimap_capabilities[$capability];
278 /******************************************************************************
279 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
280 ******************************************************************************/
281 function sqimap_get_delimiter ($imap_stream = false) {
282 global $imap_general_debug;
283 global $sqimap_delimiter;
284 global $optional_delimiter;
286 /* Use configured delimiter if set */
287 if((!empty($optional_delimiter)) && $optional_delimiter != "detect")
288 return $optional_delimiter;
290 /* Do some caching here */
291 if (!$sqimap_delimiter) {
292 if (sqimap_capability($imap_stream, "NAMESPACE")) {
293 /* According to something that I can't find, this is supposed to work on all systems
294 OS: This won't work in Courier IMAP.
295 OS: According to rfc2342 response from NAMESPACE command is:
296 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
297 OS: We want to lookup all personal NAMESPACES...
299 fputs ($imap_stream, sqimap_session_id() . " NAMESPACE\r\n");
300 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
301 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
302 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2))
304 $pna = explode(')(', $pn);
305 while (list($k, $v) = each($pna))
307 $lst = explode('"', $v);
308 if (isset($lst[3])) {
309 $pn[$lst[1]] = $lst[3];
315 $sqimap_delimiter = $pn[0];
317 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
318 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
319 $quote_position = strpos ($read[0], '"');
320 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
323 return $sqimap_delimiter;
327 /******************************************************************************
328 ** Gets the number of messages in the current mailbox.
329 ******************************************************************************/
330 function sqimap_get_num_messages ($imap_stream, $mailbox) {
331 fputs ($imap_stream, sqimap_session_id() . " EXAMINE \"$mailbox\"\r\n");
332 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
333 for ($i = 0; $i < count($read_ary); $i++
) {
334 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
338 return "BUG! Couldn't get number of messages in $mailbox!";
342 /******************************************************************************
343 ** Returns a displayable email address
344 ******************************************************************************/
345 function sqimap_find_email ($string) {
346 /** Luke Ehresman <lehresma@css.tayloru.edu>
347 ** <lehresma@css.tayloru.edu>
348 ** lehresma@css.tayloru.edu
351 ** lehresma@css.tayloru.edu (Luke Ehresman)
354 if (ereg("<([^>]+)>", $string, $regs)) {
357 return trim($string);
361 /******************************************************************************
362 ** Takes the From: field, and creates a displayable name.
363 ** Luke Ehresman <lkehresman@yahoo.com>
364 ** becomes: Luke Ehresman
365 ** <lkehresman@yahoo.com>
366 ** becomes: lkehresman@yahoo.com
367 ******************************************************************************/
368 function sqimap_find_displayable_name ($string) {
369 $string = ' '.trim($string);
370 $orig_string = $string;
371 if (strpos($string, '<') && strpos($string, '>')) {
372 if (strpos($string, '<') == 1) {
373 $string = sqimap_find_email($string);
375 $string = trim($string);
376 $string = substr($string, 0, strpos($string, '<'));
377 $string = ereg_replace ('"', '', $string);
380 if (trim($string) == '') {
381 $string = sqimap_find_email($orig_string);
388 /******************************************************************************
389 ** Returns the number of unseen messages in this folder
390 ******************************************************************************/
391 function sqimap_unseen_messages ($imap_stream, $mailbox) {
392 //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n");
393 fputs ($imap_stream, sqimap_session_id() . " STATUS \"$mailbox\" (UNSEEN)\r\n");
394 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
395 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
400 /******************************************************************************
401 ** Saves a message to a given folder -- used for saving sent messages
402 ******************************************************************************/
403 function sqimap_append ($imap_stream, $sent_folder, $length) {
404 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
405 $tmp = fgets ($imap_stream, 1024);
408 function sqimap_append_done ($imap_stream) {
409 fputs ($imap_stream, "\r\n");
410 $tmp = fgets ($imap_stream, 1024);