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 ** Reads the output from the IMAP stream. If handle_errors is set to true,
19 ** this will also handle all errors that are received. If it is not set,
20 ** the errors will be sent back through $response and $message
21 ******************************************************************************/
23 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
24 &$response, &$message) {
25 global $color, $squirrelmail_language, $imap_general_debug;
28 $resultlist = array();
34 while (strpos($read, "\n") === false) {
35 $read .= fgets($imap_stream, 9096);
38 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
40 } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
41 // Sizeless response, probably single-line
42 // For debugging purposes
43 if ($imap_general_debug) {
44 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
49 $read = fgets($imap_stream, 9096);
54 while (strpos($read, "\n") === false) {
55 $read .= fgets($imap_stream, 9096);
57 // For debugging purposes
58 if ($imap_general_debug) {
59 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
62 // If we know the size, no need to look at the end parameters
64 if ($total_size == $size) {
65 // We've reached the end of this 'message', switch to the next one.
68 } else if ($total_size > $size) {
69 $difference = $total_size - $size;
70 $total_size = $total_size - strlen($read);
71 $data[] = substr ($read, 0, strlen($read)-$difference);
72 $read = substr ($read, strlen($read)-$difference, strlen($read));
76 $read = fgets($imap_stream, 9096);
77 while (strpos($read, "\n") === false) {
78 $read .= fgets($imap_stream, 9096);
81 $total_size +
= strlen($read);
83 if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
84 ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
88 $read = fgets ($imap_stream, 9096);
93 while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
94 !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
95 $read = fgets($imap_stream, 9096);
97 $resultlist[] = $data;
100 $message = trim($regs[2]);
102 if ($imap_general_debug) echo '--<br>';
104 if ($handle_errors == false)
107 if ($response == 'NO') {
108 // ignore this error from m$ exchange, it is not fatal (aka bug)
109 if (strstr($message, 'command resulted in') === false) {
110 set_up_language($squirrelmail_language);
111 echo "<br><b><font color=$color[2]>\n";
112 echo _("ERROR : Could not complete request.");
114 echo _("Reason Given: ");
115 echo $message . "</font><br>\n";
118 } else if ($response == 'BAD') {
119 set_up_language($squirrelmail_language);
120 echo "<br><b><font color=$color[2]>\n";
121 echo _("ERROR : Bad or malformed request.");
123 echo _("Server responded: ");
124 echo $message . "</font><br>\n";
130 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
131 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message);
135 /******************************************************************************
136 ** Logs the user into the imap server. If $hide is set, no error messages
137 ** will be displayed. This function returns the imap connection handle.
138 ******************************************************************************/
139 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
140 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
142 $imap_stream = fsockopen ($imap_server_address, $imap_port,
143 $error_number, $error_string, 15);
144 $server_info = fgets ($imap_stream, 1024);
146 // Decrypt the password
147 $password = OneTimePadDecrypt($password, $onetimepad);
149 /** Do some error correction **/
152 set_up_language($squirrelmail_language, true);
153 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
154 echo "$error_number : $error_string<br>\r\n";
159 fputs ($imap_stream, "a001 LOGIN \"" . quoteIMAP($username) .
160 '" "' . quoteIMAP($password) . "\"\r\n");
161 $read = sqimap_read_data ($imap_stream, 'a001', false, $response, $message);
163 /** If the connection was not successful, lets see why **/
164 if ($response != "OK") {
166 if ($response != 'NO') {
167 // "BAD" and anything else gets reported here.
168 set_up_language($squirrelmail_language, true);
169 if ($response == 'BAD')
170 printf (_("Bad request: %s")."<br>\r\n", $message);
172 printf (_("Unknown error: %s") . "<br>\n", $message);
174 echo _("Read data:") . "<br>\n";
177 foreach ($read as $line)
179 echo htmlspecialchars($line) . "<br>\n";
184 // If the user does not log in with the correct
185 // username and password it is not possible to get the
186 // correct locale from the user's preferences.
187 // Therefore, apply the same hack as on the login
190 // $squirrelmail_language is set by a cookie when
191 // the user selects language and logs out
193 set_up_language($squirrelmail_language, true);
197 <body bgcolor
="ffffff">
200 <table width
="70%" noborder bgcolor
="ffffff" align
="center">
202 <td bgcolor
="dcdcdc">
203 <font color
="cc0000">
205 <?php
echo _("ERROR") ?
>
213 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
214 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
237 /******************************************************************************
238 ** Simply logs out the imap session
239 ******************************************************************************/
240 function sqimap_logout ($imap_stream) {
241 fputs ($imap_stream, "a001 LOGOUT\r\n");
244 function sqimap_capability($imap_stream, $capability) {
245 global $sqimap_capabilities;
246 global $imap_general_debug;
248 if (!is_array($sqimap_capabilities)) {
249 fputs ($imap_stream, "a001 CAPABILITY\r\n");
250 $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b);
252 $c = explode(' ', $read[0]);
253 for ($i=2; $i < count($c); $i++
) {
254 $cap_list = explode('=', $c[$i]);
255 if (isset($cap_list[1]))
256 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
258 $sqimap_capabilities[$cap_list[0]] = TRUE;
261 if (! isset($sqimap_capabilities[$capability]))
263 return $sqimap_capabilities[$capability];
266 /******************************************************************************
267 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
268 ******************************************************************************/
269 function sqimap_get_delimiter ($imap_stream = false) {
270 global $imap_general_debug;
271 global $sqimap_delimiter;
272 global $optional_delimiter;
274 /* Use configured delimiter if set */
275 if((!empty($optional_delimiter)) && $optional_delimiter != "detect")
276 return $optional_delimiter;
278 /* Do some caching here */
279 if (!$sqimap_delimiter) {
280 if (sqimap_capability($imap_stream, "NAMESPACE")) {
281 /* According to something that I can't find, this is supposed to work on all systems
282 OS: This won't work in Courier IMAP.
283 OS: According to rfc2342 response from NAMESPACE command is:
284 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
285 OS: We want to lookup all personal NAMESPACES...
287 fputs ($imap_stream, "a001 NAMESPACE\r\n");
288 $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b);
289 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
290 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2))
292 $pna = explode(')(', $pn);
293 while (list($k, $v) = each($pna))
295 $lst = explode('"', $v);
296 if (isset($lst[3])) {
297 $pn[$lst[1]] = $lst[3];
303 $sqimap_delimiter = $pn[0];
305 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
306 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
307 $quote_position = strpos ($read[0], '"');
308 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
311 return $sqimap_delimiter;
315 /******************************************************************************
316 ** Gets the number of messages in the current mailbox.
317 ******************************************************************************/
318 function sqimap_get_num_messages ($imap_stream, $mailbox) {
319 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
320 $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message);
321 for ($i = 0; $i < count($read_ary); $i++
) {
322 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
326 return "BUG! Couldn't get number of messages in $mailbox!";
330 /******************************************************************************
331 ** Returns a displayable email address
332 ******************************************************************************/
333 function sqimap_find_email ($string) {
334 /** Luke Ehresman <lehresma@css.tayloru.edu>
335 ** <lehresma@css.tayloru.edu>
336 ** lehresma@css.tayloru.edu
339 ** lehresma@css.tayloru.edu (Luke Ehresman)
342 if (ereg("<([^>]+)>", $string, $regs)) {
345 return trim($string);
349 /******************************************************************************
350 ** Takes the From: field, and creates a displayable name.
351 ** Luke Ehresman <lkehresman@yahoo.com>
352 ** becomes: Luke Ehresman
353 ** <lkehresman@yahoo.com>
354 ** becomes: lkehresman@yahoo.com
355 ******************************************************************************/
356 function sqimap_find_displayable_name ($string) {
357 $string = ' '.trim($string);
358 $orig_string = $string;
359 if (strpos($string, '<') && strpos($string, '>')) {
360 if (strpos($string, '<') == 1) {
361 $string = sqimap_find_email($string);
363 $string = trim($string);
364 $string = substr($string, 0, strpos($string, '<'));
365 $string = ereg_replace ('"', '', $string);
368 if (trim($string) == '') {
369 $string = sqimap_find_email($orig_string);
376 /******************************************************************************
377 ** Returns the number of unseen messages in this folder
378 ******************************************************************************/
379 function sqimap_unseen_messages ($imap_stream, $mailbox) {
380 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
381 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
382 $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message);
383 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
388 /******************************************************************************
389 ** Saves a message to a given folder -- used for saving sent messages
390 ******************************************************************************/
391 function sqimap_append ($imap_stream, $sent_folder, $length) {
392 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
393 $tmp = fgets ($imap_stream, 1024);
396 function sqimap_append_done ($imap_stream) {
397 fputs ($imap_stream, "\r\n");
398 $tmp = fgets ($imap_stream, 1024);