5 ** This implements all functions that do general imap functions.
8 $imap_general_debug = false;
10 /******************************************************************************
11 ** Reads the output from the IMAP stream. If handle_errors is set to true,
12 ** this will also handle all errors that are received. If it is not set,
13 ** the errors will be sent back through $response and $message
14 ******************************************************************************/
15 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
16 global $color, $squirrelmail_language, $imap_general_debug;
19 $read = fgets($imap_stream, 9096);
22 // Continue if needed for this single line
23 while (strpos($read, "\n") === false)
25 $read .= fgets($imap_stream, 9096);
28 if ($imap_general_debug)
30 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
34 if (ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs))
36 // Test if this is really the last line.
37 // Very much a hack, but what else can I do?
38 socket_set_blocking($imap_stream, false);
39 $read_next = fgets($imap_stream, 2);
40 socket_set_blocking($imap_stream, true);
49 $read_next = fgets($imap_stream, 9096);
57 $message = trim($regs[2]);
59 if ($imap_general_debug) echo "--<br>";
61 if ($handle_errors == false)
64 if ($response == "NO") {
65 // ignore this error from m$ exchange, it is not fatal (aka bug)
66 if (!ereg("command resulted in",$message)) {
67 set_up_language($squirrelmail_language);
68 echo "<br><b><font color=$color[2]>\n";
69 echo _("ERROR : Could not complete request.");
71 echo _("Reason Given: ");
72 echo $message . "</font><br>\n";
75 } else if ($response == "BAD") {
76 set_up_language($squirrelmail_language);
77 echo "<br><b><font color=$color[2]>\n";
78 echo _("ERROR : Bad or malformed request.");
80 echo _("Server responded: ");
81 echo $message . "</font><br>\n";
88 /******************************************************************************
89 ** Logs the user into the imap server. If $hide is set, no error messages
90 ** will be displayed. This function returns the imap connection handle.
91 ******************************************************************************/
92 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
93 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
95 $imap_stream = fsockopen ($imap_server_address, $imap_port,
96 &$error_number, &$error_string, 15);
97 $server_info = fgets ($imap_stream, 1024);
99 // Decrypt the password
100 $password = OneTimePadDecrypt($password, $onetimepad);
102 /** Do some error correction **/
105 set_up_language($squirrelmail_language, true);
106 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
107 echo "$error_number : $error_string<br>\r\n";
112 fputs ($imap_stream, "a001 LOGIN \"" . quotemeta($username) .
113 "\" \"" . quotemeta($password) . "\"\r\n");
114 $read = sqimap_read_data ($imap_stream, "a001", false, $response, $message);
116 /** If the connection was not successful, lets see why **/
117 if ($response != "OK") {
119 if ($response != "NO") {
120 // "BAD" and anything else gets reported here.
121 set_up_language($squirrelmail_language, true);
122 if ($response == "BAD")
123 printf (_("Bad request: %s")."<br>\r\n", $message);
125 printf (_("Unknown error: %s") . "<br>\n", $message);
127 echo _("Read data:") . "<br>\n";
130 foreach ($read as $line)
132 echo htmlspecialchars($line) . "<br>\n";
137 // If the user does not log in with the correct
138 // username and password it is not possible to get the
139 // correct locale from the user's preferences.
140 // Therefore, apply the same hack as on the login
143 // $squirrelmail_language is set by a cookie when
144 // the user selects language and logs out
146 set_up_language($squirrelmail_language, true);
150 <body bgcolor
=ffffff
>
153 <table width
=70% noborder bgcolor
=ffffff align
=center
>
158 <?php
echo _("ERROR") ?
>
166 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
167 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
190 /******************************************************************************
191 ** Simply logs out the imap session
192 ******************************************************************************/
193 function sqimap_logout ($imap_stream) {
194 fputs ($imap_stream, "a001 LOGOUT\r\n");
197 function sqimap_capability($imap_stream, $capability) {
198 global $sqimap_capabilities;
199 global $imap_general_debug;
201 if (!is_array($sqimap_capabilities)) {
202 fputs ($imap_stream, "a001 CAPABILITY\r\n");
203 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
205 $c = explode(' ', $read[0]);
206 for ($i=2; $i < count($c); $i++
) {
207 list($k, $v) = explode('=', $c[$i]);
208 $sqimap_capabilities[$k] = ($v)?
$v:TRUE;
211 return $sqimap_capabilities[$capability];
214 /******************************************************************************
215 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
216 ******************************************************************************/
217 function sqimap_get_delimiter ($imap_stream = false) {
218 global $imap_general_debug;
219 global $sqimap_delimiter;
220 global $optional_delimiter;
222 /* Use configured delimiter if set */
223 if((!empty($optional_delimiter)) && $optional_delimiter != "detect") {
224 return $optional_delimiter;
227 /* Do some caching here */
228 if (!$sqimap_delimiter) {
229 if (sqimap_capability($imap_stream, "NAMESPACE")) {
230 /* According to something that I can't find, this is supposed to work on all systems
231 OS: This won't work in Courier IMAP.
232 OS: According to rfc2342 response from NAMESPACE command is:
233 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
234 OS: We want to lookup all personal NAMESPACES...
236 fputs ($imap_stream, "a001 NAMESPACE\r\n");
237 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
238 if (eregi('\* NAMESPACE +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL)', $read[0], $data)) {
239 if (eregi('^\( *\((.*)\) *\)', $data[1], $data2))
241 $pna = explode(')(', $pn);
242 while (list($k, $v) = each($pna))
244 list($_, $n, $_, $d) = explode('"', $v);
247 /* OS: We don't need this code right now, it is for other_users and shared folders
248 if (eregi('^\( *\((.*)\) *\)', $data[2], $data2))
250 if (eregi('^\( *\((.*)\) *\)', $data[3], $data2))
253 $ona = explode(')(', $on);
254 while (list($k, $v) = each($ona))
256 list($_, $n, $_, $d) = explode('"', $v);
259 $sna = explode(')(', $sn);
260 while (list($k, $v) = each($sna))
262 list($_, $n, $_, $d) = explode('"', $v);
267 $sqimap_delimiter = $pn[0];
269 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
270 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
271 $quote_position = strpos ($read[0], "\"");
272 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
275 return $sqimap_delimiter;
279 /******************************************************************************
280 ** Gets the number of messages in the current mailbox.
281 ******************************************************************************/
282 function sqimap_get_num_messages ($imap_stream, $mailbox) {
283 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
284 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
285 for ($i = 0; $i < count($read_ary); $i++
) {
286 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
290 return "BUG! Couldn't get number of messages in $mailbox!";
294 /******************************************************************************
295 ** Returns a displayable email address
296 ******************************************************************************/
297 function sqimap_find_email ($string) {
298 /** Luke Ehresman <lehresma@css.tayloru.edu>
299 ** <lehresma@css.tayloru.edu>
300 ** lehresma@css.tayloru.edu
303 ** lehresma@css.tayloru.edu (Luke Ehresman)
306 if (ereg("<([^>]+)>", $string, $regs)) {
309 return trim($string);
313 /******************************************************************************
314 ** Takes the From: field, and creates a displayable name.
315 ** Luke Ehresman <lkehresman@yahoo.com>
316 ** becomes: Luke Ehresman
317 ** <lkehresman@yahoo.com>
318 ** becomes: lkehresman@yahoo.com
319 ******************************************************************************/
320 function sqimap_find_displayable_name ($string) {
321 $string = " ".trim($string);
322 $orig_string = $string;
323 if (strpos($string, "<") && strpos($string, ">")) {
324 if (strpos($string, "<") == 1) {
325 $string = sqimap_find_email($string);
327 $string = trim($string);
328 $string = substr($string, 0, strpos($string, "<"));
329 $string = ereg_replace ("\"", "", $string);
332 if (trim($string) == "") {
333 $string = sqimap_find_email($orig_string);
340 /******************************************************************************
341 ** Returns the number of unseen messages in this folder
342 ******************************************************************************/
343 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
344 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
345 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
346 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
347 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
352 /******************************************************************************
353 ** Saves a message to a given folder -- used for saving sent messages
354 ******************************************************************************/
355 function sqimap_append ($imap_stream, $sent_folder, $length) {
356 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
357 $tmp = fgets ($imap_stream, 1024);
360 function sqimap_append_done ($imap_stream) {
361 fputs ($imap_stream, "\r\n");
362 $tmp = fgets ($imap_stream, 1024);