added config option to specify the IMAP folder delimiter, rather than
[squirrelmail.git] / functions / imap_general.php
CommitLineData
59177427 1<?php
052e0c26 2 /**
3 ** imap.php
4 **
5 ** This implements all functions that do general imap functions.
6 **/
7
8 /******************************************************************************
9 ** Reads the output from the IMAP stream. If handle_errors is set to true,
10 ** this will also handle all errors that are received. If it is not set,
11 ** the errors will be sent back through $response and $message
12 ******************************************************************************/
3d2504a1 13 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
441f2d33 14 global $color, $squirrelmail_language;
052e0c26 15
a3432f47 16 //$imap_general_debug = true;
17 $imap_general_debug = false;
18
052e0c26 19 $read = fgets ($imap_stream, 1024);
a3432f47 20 if ($imap_general_debug) echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
052e0c26 21 $counter = 0;
441f2d33 22 while (! ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
052e0c26 23 $data[$counter] = $read;
24 $read = fgets ($imap_stream, 1024);
a3432f47 25 if ($imap_general_debug) echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
052e0c26 26 $counter++;
3d2504a1 27 }
28
29 $response = $regs[1];
30 $message = trim($regs[2]);
31
a3432f47 32 if ($imap_general_debug) echo "--<br>";
052e0c26 33
34 if ($handle_errors == true) {
3d2504a1 35 if ($response == "NO") {
441f2d33 36 set_up_language($squirrelmail_language);
04632dbc 37 echo "<br><b><font color=$color[2]>\n";
052e0c26 38 echo _("ERROR : Could not complete request.");
04632dbc 39 echo "</b><br>\n";
052e0c26 40 echo _("Reason Given: ");
3d2504a1 41 echo $message . "</font><br>\n";
052e0c26 42 exit;
3d2504a1 43 } else if ($response == "BAD") {
441f2d33 44 set_up_language($squirrelmail_language);
04632dbc 45 echo "<br><b><font color=$color[2]>\n";
052e0c26 46 echo _("ERROR : Bad or malformed request.");
04632dbc 47 echo "</b><br>\n";
052e0c26 48 echo _("Server responded: ");
3d2504a1 49 echo $message . "</font><br>\n";
052e0c26 50 exit;
51 }
52 }
53
54 return $data;
55 }
56
57
58
59
60 /******************************************************************************
61 ** Logs the user into the imap server. If $hide is set, no error messages
62 ** will be displayed. This function returns the imap connection handle.
63 ******************************************************************************/
64 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
52eefafc 65 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
1b187352 66
052e0c26 67 $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string);
68 $server_info = fgets ($imap_stream, 1024);
69
52eefafc 70 // Decrypt the password
71 $password = OneTimePadDecrypt($password, $onetimepad);
72
052e0c26 73 /** Do some error correction **/
74 if (!$imap_stream) {
75 if (!$hide) {
441f2d33 76 set_up_language($squirrelmail_language, true);
1b187352 77 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
052e0c26 78 echo "$error_number : $error_string<br>\r\n";
79 }
80 exit;
81 }
82
83 fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n");
3d2504a1 84 $read = sqimap_read_data ($imap_stream, "a001", false, $response, $message);
052e0c26 85
86 /** If the connection was not successful, lets see why **/
3d2504a1 87 if ($response != "OK") {
052e0c26 88 if (!$hide) {
3d2504a1 89 if ($response != "NO") {
90 // "BAD" and anything else gets reported here.
441f2d33 91 set_up_language($squirrelmail_language, true);
3d2504a1 92 if ($response == "BAD")
93 printf (_("Bad request: %s")."<br>\r\n", $message);
94 else
95 printf (_("Unknown error: %s") . "<br>\n", $message);
96 echo "<br>";
97 echo _("Read data:") . "<br>\n";
98 foreach ($read as $line)
99 {
100 echo htmlspecialchars($line) . "<br>\n";
101 }
052e0c26 102 exit;
165e24a7 103 } else {
1b187352 104 // If the user does not log in with the correct
105 // username and password it is not possible to get the
106 // correct locale from the user's preferences.
107 // Therefore, apply the same hack as on the login
108 // screen.
109
110 // $squirrelmail_language is set by a cookie when
111 // the user selects language and logs out
112
441f2d33 113 set_up_language($squirrelmail_language, true);
1b187352 114
052e0c26 115 ?>
116 <html>
117 <body bgcolor=ffffff>
118 <br>
119 <center>
120 <table width=70% noborder bgcolor=ffffff align=center>
121 <tr>
122 <td bgcolor=dcdcdc>
123 <font color=cc0000>
124 <center>
59177427 125 <?php echo _("ERROR") ?>
052e0c26 126 </center>
127 </font>
128 </td>
129 </tr>
130 <tr>
131 <td>
132 <center>
59177427 133 <?php echo _("Unknown user or password incorrect.") ?><br>
5630c7ec 134 <a href="login.php" target="_top"><?php echo _("Click here to try again") ?></a>
052e0c26 135 </center>
136 </td>
137 </tr>
138 </table>
139 </center>
140 </body>
141 </html>
59177427 142 <?php
052e0c26 143 session_destroy();
144 exit;
052e0c26 145 }
146 } else {
147 exit;
148 }
149 }
150
151 return $imap_stream;
152 }
153
154
155
156
157 /******************************************************************************
158 ** Simply logs out the imap session
159 ******************************************************************************/
160 function sqimap_logout ($imap_stream) {
161 fputs ($imap_stream, "a001 LOGOUT\r\n");
162 }
163
164
165
166 /******************************************************************************
167 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
168 ******************************************************************************/
1d2ce1c3 169 function sqimap_get_delimiter ($imap_stream = false) {
40ba4d36 170 global $optional_delimiter;
171 if (!$optional_delimiter) $optional_delimiter = "detect";
172
173 if (strtolower($optional_delimiter) == "detect") {
174 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
175 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
176 $quote_position = strpos ($read[0], "\"");
177 $delim = substr ($read[0], $quote_position+1, 1);
178 return $delim;
179 } else {
180 return $optional_delimiter;
181 }
1d2ce1c3 182
183 /* According to something that I can't find, this is supposed to work on all systems
184
441f2d33 185 fputs ($imap_stream, "a001 NAMESPACE\r\n");
186 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
187 eregi("\"\" \"(.)\"", $read[0], $regs);
188 return $regs[1];
1d2ce1c3 189 */
052e0c26 190 }
191
192
193
194
195 /******************************************************************************
196 ** Gets the number of messages in the current mailbox.
197 ******************************************************************************/
198 function sqimap_get_num_messages ($imap_stream, $mailbox) {
199 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
200 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
201 for ($i = 0; $i < count($read_ary); $i++) {
441f2d33 202 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
203 return $regs[1];
052e0c26 204 }
205 }
441f2d33 206 return "BUG! Couldn't get number of messages in $mailbox!";
052e0c26 207 }
208
209
210 /******************************************************************************
211 ** Returns a displayable email address
212 ******************************************************************************/
213 function sqimap_find_email ($string) {
214 /** Luke Ehresman <lehresma@css.tayloru.edu>
215 ** <lehresma@css.tayloru.edu>
216 ** lehresma@css.tayloru.edu
1d2ce1c3 217 **
218 ** What about
219 ** lehresma@css.tayloru.edu (Luke Ehresman)
052e0c26 220 **/
221
441f2d33 222 if (ereg("<([^>]+)>", $string, $regs)) {
223 $string = $regs[1];
052e0c26 224 }
225 return trim($string);
226 }
227
228
229 /******************************************************************************
230 ** Takes the From: field, and creates a displayable name.
231 ** Luke Ehresman <lkehresman@yahoo.com>
232 ** becomes: Luke Ehresman
233 ** <lkehresman@yahoo.com>
234 ** becomes: lkehresman@yahoo.com
235 ******************************************************************************/
236 function sqimap_find_displayable_name ($string) {
1d2ce1c3 237 $string = " ".trim($string);
238 $orig_string = $string;
239 if (strpos($string, "<") && strpos($string, ">")) {
240 if (strpos($string, "<") == 1) {
241 $string = sqimap_find_email($string);
242 } else {
243 $string = trim($string);
244 $string = substr($string, 0, strpos($string, "<"));
245 $string = ereg_replace ("\"", "", $string);
246 }
247
248 if (trim($string) == "") {
249 $string = sqimap_find_email($orig_string);
250 }
251 }
252 return $string;
052e0c26 253 }
254
255
052e0c26 256 /******************************************************************************
257 ** Returns the number of unseen messages in this folder
258 ******************************************************************************/
c5eb2c03 259 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
260 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
261 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
052e0c26 262 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
441f2d33 263 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
264 return $regs[1];
052e0c26 265 }
266
267
268 /******************************************************************************
269 ** Saves a message to a given folder -- used for saving sent messages
270 ******************************************************************************/
271 function sqimap_append ($imap_stream, $sent_folder, $length) {
272 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
273 $tmp = fgets ($imap_stream, 1024);
274 }
275
276 function sqimap_append_done ($imap_stream) {
277 fputs ($imap_stream, "\r\n");
278 $tmp = fgets ($imap_stream, 1024);
279 }
280?>