5 ** This implements all functions that do general imap functions.
8 $imap_general_debug = false;
9 //$imap_general_debug = false;
11 /******************************************************************************
12 ** Reads the output from the IMAP stream. If handle_errors is set to true,
13 ** this will also handle all errors that are received. If it is not set,
14 ** the errors will be sent back through $response and $message
15 ******************************************************************************/
16 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
17 global $color, $squirrelmail_language, $imap_general_debug;
21 $data[$counter] = $read = fgets ($imap_stream, 4096);
22 if ($imap_general_debug) { echo "<small><tt><font color=cc0000>$read</font></tt></small><br>"; flush(); }
24 } while (! ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs));
27 $message = trim($regs[2]);
29 if ($imap_general_debug) echo "--<br>";
31 if ($handle_errors == true) {
32 if ($response == "NO") {
33 set_up_language($squirrelmail_language);
34 echo "<br><b><font color=$color[2]>\n";
35 echo _("ERROR : Could not complete request.");
37 echo _("Reason Given: ");
38 echo $message . "</font><br>\n";
40 } else if ($response == "BAD") {
41 set_up_language($squirrelmail_language);
42 echo "<br><b><font color=$color[2]>\n";
43 echo _("ERROR : Bad or malformed request.");
45 echo _("Server responded: ");
46 echo $message . "</font><br>\n";
54 /******************************************************************************
55 ** Logs the user into the imap server. If $hide is set, no error messages
56 ** will be displayed. This function returns the imap connection handle.
57 ******************************************************************************/
58 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
59 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
61 $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string);
62 $server_info = fgets ($imap_stream, 1024);
64 // Decrypt the password
65 $password = OneTimePadDecrypt($password, $onetimepad);
67 /** Do some error correction **/
70 set_up_language($squirrelmail_language, true);
71 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
72 echo "$error_number : $error_string<br>\r\n";
77 fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n");
78 $read = sqimap_read_data ($imap_stream, "a001", false, $response, $message);
80 /** If the connection was not successful, lets see why **/
81 if ($response != "OK") {
83 if ($response != "NO") {
84 // "BAD" and anything else gets reported here.
85 set_up_language($squirrelmail_language, true);
86 if ($response == "BAD")
87 printf (_("Bad request: %s")."<br>\r\n", $message);
89 printf (_("Unknown error: %s") . "<br>\n", $message);
91 echo _("Read data:") . "<br>\n";
94 foreach ($read as $line)
96 echo htmlspecialchars($line) . "<br>\n";
101 // If the user does not log in with the correct
102 // username and password it is not possible to get the
103 // correct locale from the user's preferences.
104 // Therefore, apply the same hack as on the login
107 // $squirrelmail_language is set by a cookie when
108 // the user selects language and logs out
110 set_up_language($squirrelmail_language, true);
114 <body bgcolor
=ffffff
>
117 <table width
=70% noborder bgcolor
=ffffff align
=center
>
122 <?php
echo _("ERROR") ?
>
130 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
131 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
154 /******************************************************************************
155 ** Simply logs out the imap session
156 ******************************************************************************/
157 function sqimap_logout ($imap_stream) {
158 fputs ($imap_stream, "a001 LOGOUT\r\n");
161 function sqimap_capability($imap_stream, $capability) {
162 global $sqimap_capabilities;
163 global $imap_general_debug;
165 if (!is_array($sqimap_capabilities)) {
166 fputs ($imap_stream, "a001 CAPABILITY\r\n");
167 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
169 $c = explode(' ', $read[0]);
170 for ($i=2; $i < count($c); $i++
) {
171 list($k, $v) = explode('=', $c[$i]);
172 $sqimap_capabilities[$k] = ($v)?
$v:TRUE;
175 return $sqimap_capabilities[$capability];
178 /******************************************************************************
179 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
180 ******************************************************************************/
181 function sqimap_get_delimiter ($imap_stream = false) {
182 global $imap_general_debug;
183 global $sqimap_delimiter;
185 /* Do some caching here */
186 if (!$sqimap_delimiter) {
187 if (sqimap_capability($imap_stream, "NAMESPACE")) {
188 /* According to something that I can't find, this is supposed to work on all systems
189 OS: This won't work in Courier IMAP.
190 OS: According to rfc2342 response from NAMESPACE command is:
191 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
192 OS: We want to lookup all personal NAMESPACES...
194 fputs ($imap_stream, "a001 NAMESPACE\r\n");
195 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
196 if (eregi('\* NAMESPACE +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL)', $read[0], $data)) {
197 if (eregi('^\( *\((.*)\) *\)', $data[1], $data2))
199 $pna = explode(')(', $pn);
200 while (list($k, $v) = each($pna))
202 list($_, $n, $_, $d) = explode('"', $v);
205 /* OS: We don't need this code right now, it is for other_users and shared folders
206 if (eregi('^\( *\((.*)\) *\)', $data[2], $data2))
208 if (eregi('^\( *\((.*)\) *\)', $data[3], $data2))
211 $ona = explode(')(', $on);
212 while (list($k, $v) = each($ona))
214 list($_, $n, $_, $d) = explode('"', $v);
217 $sna = explode(')(', $sn);
218 while (list($k, $v) = each($sna))
220 list($_, $n, $_, $d) = explode('"', $v);
225 $sqimap_delimiter = $pn[0];
227 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
228 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
229 $quote_position = strpos ($read[0], "\"");
230 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
233 return $sqimap_delimiter;
237 /******************************************************************************
238 ** Gets the number of messages in the current mailbox.
239 ******************************************************************************/
240 function sqimap_get_num_messages ($imap_stream, $mailbox) {
241 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
242 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
243 for ($i = 0; $i < count($read_ary); $i++
) {
244 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
248 return "BUG! Couldn't get number of messages in $mailbox!";
252 /******************************************************************************
253 ** Returns a displayable email address
254 ******************************************************************************/
255 function sqimap_find_email ($string) {
256 /** Luke Ehresman <lehresma@css.tayloru.edu>
257 ** <lehresma@css.tayloru.edu>
258 ** lehresma@css.tayloru.edu
261 ** lehresma@css.tayloru.edu (Luke Ehresman)
264 if (ereg("<([^>]+)>", $string, $regs)) {
267 return trim($string);
271 /******************************************************************************
272 ** Takes the From: field, and creates a displayable name.
273 ** Luke Ehresman <lkehresman@yahoo.com>
274 ** becomes: Luke Ehresman
275 ** <lkehresman@yahoo.com>
276 ** becomes: lkehresman@yahoo.com
277 ******************************************************************************/
278 function sqimap_find_displayable_name ($string) {
279 $string = " ".trim($string);
280 $orig_string = $string;
281 if (strpos($string, "<") && strpos($string, ">")) {
282 if (strpos($string, "<") == 1) {
283 $string = sqimap_find_email($string);
285 $string = trim($string);
286 $string = substr($string, 0, strpos($string, "<"));
287 $string = ereg_replace ("\"", "", $string);
290 if (trim($string) == "") {
291 $string = sqimap_find_email($orig_string);
298 /******************************************************************************
299 ** Returns the number of unseen messages in this folder
300 ******************************************************************************/
301 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
302 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
303 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
304 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
305 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
310 /******************************************************************************
311 ** Saves a message to a given folder -- used for saving sent messages
312 ******************************************************************************/
313 function sqimap_append ($imap_stream, $sent_folder, $length) {
314 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
315 $tmp = fgets ($imap_stream, 1024);
318 function sqimap_append_done ($imap_stream) {
319 fputs ($imap_stream, "\r\n");
320 $tmp = fgets ($imap_stream, 1024);