5e80c89cb8cfc8413fce3e81dd776c5fc42b9e30
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, 9096);
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 // ignore this error from m$ exchange, it is not fatal (aka bug)
34 if (!ereg("command resulted in",$message)) {
35 set_up_language($squirrelmail_language);
36 echo "<br><b><font color=$color[2]>\n";
37 echo _("ERROR : Could not complete request.");
39 echo _("Reason Given: ");
40 echo $message . "</font><br>\n";
43 } else if ($response == "BAD") {
44 set_up_language($squirrelmail_language);
45 echo "<br><b><font color=$color[2]>\n";
46 echo _("ERROR : Bad or malformed request.");
48 echo _("Server responded: ");
49 echo $message . "</font><br>\n";
57 /******************************************************************************
58 ** Logs the user into the imap server. If $hide is set, no error messages
59 ** will be displayed. This function returns the imap connection handle.
60 ******************************************************************************/
61 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
62 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
64 $imap_stream = fsockopen ($imap_server_address, $imap_port,
65 &$error_number, &$error_string, 15);
66 $server_info = fgets ($imap_stream, 1024);
68 // Decrypt the password
69 $password = OneTimePadDecrypt($password, $onetimepad);
71 /** Do some error correction **/
74 set_up_language($squirrelmail_language, true);
75 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
76 echo "$error_number : $error_string<br>\r\n";
81 fputs ($imap_stream, "a001 LOGIN \"" . quotemeta($username) .
82 "\" \"" . quotemeta($password) . "\"\r\n");
83 $read = sqimap_read_data ($imap_stream, "a001", false, $response, $message);
85 /** If the connection was not successful, lets see why **/
86 if ($response != "OK") {
88 if ($response != "NO") {
89 // "BAD" and anything else gets reported here.
90 set_up_language($squirrelmail_language, true);
91 if ($response == "BAD")
92 printf (_("Bad request: %s")."<br>\r\n", $message);
94 printf (_("Unknown error: %s") . "<br>\n", $message);
96 echo _("Read data:") . "<br>\n";
99 foreach ($read as $line)
101 echo htmlspecialchars($line) . "<br>\n";
106 // If the user does not log in with the correct
107 // username and password it is not possible to get the
108 // correct locale from the user's preferences.
109 // Therefore, apply the same hack as on the login
112 // $squirrelmail_language is set by a cookie when
113 // the user selects language and logs out
115 set_up_language($squirrelmail_language, true);
119 <body bgcolor
=ffffff
>
122 <table width
=70% noborder bgcolor
=ffffff align
=center
>
127 <?php
echo _("ERROR") ?
>
135 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
136 <a href
="login.php" target
="_top"><?php
echo _("Click here to try again") ?
></a
>
159 /******************************************************************************
160 ** Simply logs out the imap session
161 ******************************************************************************/
162 function sqimap_logout ($imap_stream) {
163 fputs ($imap_stream, "a001 LOGOUT\r\n");
166 function sqimap_capability($imap_stream, $capability) {
167 global $sqimap_capabilities;
168 global $imap_general_debug;
170 if (!is_array($sqimap_capabilities)) {
171 fputs ($imap_stream, "a001 CAPABILITY\r\n");
172 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
174 $c = explode(' ', $read[0]);
175 for ($i=2; $i < count($c); $i++
) {
176 list($k, $v) = explode('=', $c[$i]);
177 $sqimap_capabilities[$k] = ($v)?
$v:TRUE;
180 return $sqimap_capabilities[$capability];
183 /******************************************************************************
184 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
185 ******************************************************************************/
186 function sqimap_get_delimiter ($imap_stream = false) {
187 global $imap_general_debug;
188 global $sqimap_delimiter;
189 global $optional_delimiter;
191 /* Use configured delimiter if set */
192 if((!empty($optional_delimiter)) && $optional_delimiter != "detect") {
193 return $optional_delimiter;
196 /* Do some caching here */
197 if (!$sqimap_delimiter) {
198 if (sqimap_capability($imap_stream, "NAMESPACE")) {
199 /* According to something that I can't find, this is supposed to work on all systems
200 OS: This won't work in Courier IMAP.
201 OS: According to rfc2342 response from NAMESPACE command is:
202 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
203 OS: We want to lookup all personal NAMESPACES...
205 fputs ($imap_stream, "a001 NAMESPACE\r\n");
206 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
207 if (eregi('\* NAMESPACE +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL)', $read[0], $data)) {
208 if (eregi('^\( *\((.*)\) *\)', $data[1], $data2))
210 $pna = explode(')(', $pn);
211 while (list($k, $v) = each($pna))
213 list($_, $n, $_, $d) = explode('"', $v);
216 /* OS: We don't need this code right now, it is for other_users and shared folders
217 if (eregi('^\( *\((.*)\) *\)', $data[2], $data2))
219 if (eregi('^\( *\((.*)\) *\)', $data[3], $data2))
222 $ona = explode(')(', $on);
223 while (list($k, $v) = each($ona))
225 list($_, $n, $_, $d) = explode('"', $v);
228 $sna = explode(')(', $sn);
229 while (list($k, $v) = each($sna))
231 list($_, $n, $_, $d) = explode('"', $v);
236 $sqimap_delimiter = $pn[0];
238 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
239 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
240 $quote_position = strpos ($read[0], "\"");
241 $sqimap_delimiter = substr ($read[0], $quote_position+
1, 1);
244 return $sqimap_delimiter;
248 /******************************************************************************
249 ** Gets the number of messages in the current mailbox.
250 ******************************************************************************/
251 function sqimap_get_num_messages ($imap_stream, $mailbox) {
252 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
253 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
254 for ($i = 0; $i < count($read_ary); $i++
) {
255 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
259 return "BUG! Couldn't get number of messages in $mailbox!";
263 /******************************************************************************
264 ** Returns a displayable email address
265 ******************************************************************************/
266 function sqimap_find_email ($string) {
267 /** Luke Ehresman <lehresma@css.tayloru.edu>
268 ** <lehresma@css.tayloru.edu>
269 ** lehresma@css.tayloru.edu
272 ** lehresma@css.tayloru.edu (Luke Ehresman)
275 if (ereg("<([^>]+)>", $string, $regs)) {
278 return trim($string);
282 /******************************************************************************
283 ** Takes the From: field, and creates a displayable name.
284 ** Luke Ehresman <lkehresman@yahoo.com>
285 ** becomes: Luke Ehresman
286 ** <lkehresman@yahoo.com>
287 ** becomes: lkehresman@yahoo.com
288 ******************************************************************************/
289 function sqimap_find_displayable_name ($string) {
290 $string = " ".trim($string);
291 $orig_string = $string;
292 if (strpos($string, "<") && strpos($string, ">")) {
293 if (strpos($string, "<") == 1) {
294 $string = sqimap_find_email($string);
296 $string = trim($string);
297 $string = substr($string, 0, strpos($string, "<"));
298 $string = ereg_replace ("\"", "", $string);
301 if (trim($string) == "") {
302 $string = sqimap_find_email($orig_string);
309 /******************************************************************************
310 ** Returns the number of unseen messages in this folder
311 ******************************************************************************/
312 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
313 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
314 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
315 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
316 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
321 /******************************************************************************
322 ** Saves a message to a given folder -- used for saving sent messages
323 ******************************************************************************/
324 function sqimap_append ($imap_stream, $sent_folder, $length) {
325 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
326 $tmp = fgets ($imap_stream, 1024);
329 function sqimap_append_done ($imap_stream) {
330 fputs ($imap_stream, "\r\n");
331 $tmp = fgets ($imap_stream, 1024);