Removed line, plugin.php now handles this properly.
[squirrelmail.git] / functions / imap_general.php
1 <?php
2 /**
3 ** imap.php
4 **
5 ** This implements all functions that do general imap functions.
6 **
7 ** $Id$
8 **/
9
10 $imap_general_debug = false;
11
12 /******************************************************************************
13 ** Reads the output from the IMAP stream. If handle_errors is set to true,
14 ** this will also handle all errors that are received. If it is not set,
15 ** the errors will be sent back through $response and $message
16 ******************************************************************************/
17 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
18 global $color, $squirrelmail_language, $imap_general_debug;
19
20 $read = fgets($imap_stream, 9096);
21
22 if (ereg("^\* [0-9]+ FETCH.*{([0-9]+)}", $read, $regs)) {
23 $size = $regs[1];
24 } else {
25 $size = 0;
26 }
27
28 $data = Array();
29
30 $continue = true;
31 while ($continue) {
32 // Continue if needed for this single line
33 while (strpos($read, "\n") === false) {
34 $read .= fgets($imap_stream, 9096);
35 }
36 if ($imap_general_debug) {
37 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
38 flush();
39 }
40
41 if (ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
42 if ($size) {
43 $dt = $data;
44 $dt[0] = $dt[count($dt)-1] = "";
45 $d = implode ("", $dt);
46 if (strlen($d) >= $size) {
47 $continue = false;
48 } else {
49 $data[] = $read;
50 $read = fgets ($imap_stream, 9096);
51 }
52 } else {
53 $continue = false;
54 }
55 } else {
56 $data[] = $read;
57 $read = fgets ($imap_stream, 9096);
58 }
59 }
60
61 $response = $regs[1];
62 $message = trim($regs[2]);
63
64 if ($imap_general_debug) echo "--<br>";
65
66 if ($handle_errors == false)
67 return $data;
68
69 if ($response == "NO") {
70 // ignore this error from m$ exchange, it is not fatal (aka bug)
71 if (!ereg("command resulted in",$message)) {
72 set_up_language($squirrelmail_language);
73 echo "<br><b><font color=$color[2]>\n";
74 echo _("ERROR : Could not complete request.");
75 echo "</b><br>\n";
76 echo _("Reason Given: ");
77 echo $message . "</font><br>\n";
78 exit;
79 }
80 } else if ($response == "BAD") {
81 set_up_language($squirrelmail_language);
82 echo "<br><b><font color=$color[2]>\n";
83 echo _("ERROR : Bad or malformed request.");
84 echo "</b><br>\n";
85 echo _("Server responded: ");
86 echo $message . "</font><br>\n";
87 exit;
88 }
89
90 return $data;
91 }
92
93 /******************************************************************************
94 ** Logs the user into the imap server. If $hide is set, no error messages
95 ** will be displayed. This function returns the imap connection handle.
96 ******************************************************************************/
97 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
98 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
99
100 $imap_stream = fsockopen ($imap_server_address, $imap_port,
101 &$error_number, &$error_string, 15);
102 $server_info = fgets ($imap_stream, 1024);
103
104 // Decrypt the password
105 $password = OneTimePadDecrypt($password, $onetimepad);
106
107 /** Do some error correction **/
108 if (!$imap_stream) {
109 if (!$hide) {
110 set_up_language($squirrelmail_language, true);
111 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
112 echo "$error_number : $error_string<br>\r\n";
113 }
114 exit;
115 }
116
117 fputs ($imap_stream, "a001 LOGIN \"" . quotemeta($username) .
118 "\" \"" . quotemeta($password) . "\"\r\n");
119 $read = sqimap_read_data ($imap_stream, "a001", false, $response, $message);
120
121 /** If the connection was not successful, lets see why **/
122 if ($response != "OK") {
123 if (!$hide) {
124 if ($response != "NO") {
125 // "BAD" and anything else gets reported here.
126 set_up_language($squirrelmail_language, true);
127 if ($response == "BAD")
128 printf (_("Bad request: %s")."<br>\r\n", $message);
129 else
130 printf (_("Unknown error: %s") . "<br>\n", $message);
131 echo "<br>";
132 echo _("Read data:") . "<br>\n";
133 if (is_array($read))
134 {
135 foreach ($read as $line)
136 {
137 echo htmlspecialchars($line) . "<br>\n";
138 }
139 }
140 exit;
141 } else {
142 // If the user does not log in with the correct
143 // username and password it is not possible to get the
144 // correct locale from the user's preferences.
145 // Therefore, apply the same hack as on the login
146 // screen.
147
148 // $squirrelmail_language is set by a cookie when
149 // the user selects language and logs out
150
151 set_up_language($squirrelmail_language, true);
152
153 ?>
154 <html>
155 <body bgcolor=ffffff>
156 <br>
157 <center>
158 <table width=70% noborder bgcolor=ffffff align=center>
159 <tr>
160 <td bgcolor=dcdcdc>
161 <font color=cc0000>
162 <center>
163 <?php echo _("ERROR") ?>
164 </center>
165 </font>
166 </td>
167 </tr>
168 <tr>
169 <td>
170 <center>
171 <?php echo _("Unknown user or password incorrect.") ?><br>
172 <a href="login.php" target="_top"><?php echo _("Click here to try again") ?></a>
173 </center>
174 </td>
175 </tr>
176 </table>
177 </center>
178 </body>
179 </html>
180 <?php
181 session_destroy();
182 exit;
183 }
184 } else {
185 exit;
186 }
187 }
188
189 return $imap_stream;
190 }
191
192
193
194
195 /******************************************************************************
196 ** Simply logs out the imap session
197 ******************************************************************************/
198 function sqimap_logout ($imap_stream) {
199 fputs ($imap_stream, "a001 LOGOUT\r\n");
200 }
201
202 function sqimap_capability($imap_stream, $capability) {
203 global $sqimap_capabilities;
204 global $imap_general_debug;
205
206 if (!is_array($sqimap_capabilities)) {
207 fputs ($imap_stream, "a001 CAPABILITY\r\n");
208 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
209
210 $c = explode(' ', $read[0]);
211 for ($i=2; $i < count($c); $i++) {
212 $cap_list = explode('=', $c[$i]);
213 if (isset($cap_list[1]))
214 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
215 else
216 $sqimap_capabilities[$cap_list[0]] = TRUE;
217 }
218 }
219 return $sqimap_capabilities[$capability];
220 }
221
222 /******************************************************************************
223 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
224 ******************************************************************************/
225 function sqimap_get_delimiter ($imap_stream = false) {
226 global $imap_general_debug;
227 global $sqimap_delimiter;
228 global $optional_delimiter;
229
230 /* Use configured delimiter if set */
231 if((!empty($optional_delimiter)) && $optional_delimiter != "detect") {
232 return $optional_delimiter;
233 }
234
235 /* Do some caching here */
236 if (!$sqimap_delimiter) {
237 if (sqimap_capability($imap_stream, "NAMESPACE")) {
238 /* According to something that I can't find, this is supposed to work on all systems
239 OS: This won't work in Courier IMAP.
240 OS: According to rfc2342 response from NAMESPACE command is:
241 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
242 OS: We want to lookup all personal NAMESPACES...
243 */
244 fputs ($imap_stream, "a001 NAMESPACE\r\n");
245 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
246 if (eregi('\* NAMESPACE +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL) +(\( *\(.+\) *\)|NIL)', $read[0], $data)) {
247 if (eregi('^\( *\((.*)\) *\)', $data[1], $data2))
248 $pn = $data2[1];
249 $pna = explode(')(', $pn);
250 while (list($k, $v) = each($pna))
251 {
252 list($_, $n, $_, $d) = explode('"', $v);
253 $pn[$n] = $d;
254 }
255 /* OS: We don't need this code right now, it is for other_users and shared folders
256 if (eregi('^\( *\((.*)\) *\)', $data[2], $data2))
257 $on = $data2[1];
258 if (eregi('^\( *\((.*)\) *\)', $data[3], $data2))
259 $sn = $data2[1];
260 unset($data);
261 $ona = explode(')(', $on);
262 while (list($k, $v) = each($ona))
263 {
264 list($_, $n, $_, $d) = explode('"', $v);
265 $on[$n] = $d;
266 }
267 $sna = explode(')(', $sn);
268 while (list($k, $v) = each($sna))
269 {
270 list($_, $n, $_, $d) = explode('"', $v);
271 $sn[$n] = $d;
272 }
273 */
274 }
275 $sqimap_delimiter = $pn[0];
276 } else {
277 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
278 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
279 $quote_position = strpos ($read[0], "\"");
280 $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
281 }
282 }
283 return $sqimap_delimiter;
284 }
285
286
287 /******************************************************************************
288 ** Gets the number of messages in the current mailbox.
289 ******************************************************************************/
290 function sqimap_get_num_messages ($imap_stream, $mailbox) {
291 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
292 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
293 for ($i = 0; $i < count($read_ary); $i++) {
294 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
295 return $regs[1];
296 }
297 }
298 return "BUG! Couldn't get number of messages in $mailbox!";
299 }
300
301
302 /******************************************************************************
303 ** Returns a displayable email address
304 ******************************************************************************/
305 function sqimap_find_email ($string) {
306 /** Luke Ehresman <lehresma@css.tayloru.edu>
307 ** <lehresma@css.tayloru.edu>
308 ** lehresma@css.tayloru.edu
309 **
310 ** What about
311 ** lehresma@css.tayloru.edu (Luke Ehresman)
312 **/
313
314 if (ereg("<([^>]+)>", $string, $regs)) {
315 $string = $regs[1];
316 }
317 return trim($string);
318 }
319
320
321 /******************************************************************************
322 ** Takes the From: field, and creates a displayable name.
323 ** Luke Ehresman <lkehresman@yahoo.com>
324 ** becomes: Luke Ehresman
325 ** <lkehresman@yahoo.com>
326 ** becomes: lkehresman@yahoo.com
327 ******************************************************************************/
328 function sqimap_find_displayable_name ($string) {
329 $string = " ".trim($string);
330 $orig_string = $string;
331 if (strpos($string, "<") && strpos($string, ">")) {
332 if (strpos($string, "<") == 1) {
333 $string = sqimap_find_email($string);
334 } else {
335 $string = trim($string);
336 $string = substr($string, 0, strpos($string, "<"));
337 $string = ereg_replace ("\"", "", $string);
338 }
339
340 if (trim($string) == "") {
341 $string = sqimap_find_email($orig_string);
342 }
343 }
344 return $string;
345 }
346
347
348 /******************************************************************************
349 ** Returns the number of unseen messages in this folder
350 ******************************************************************************/
351 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
352 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
353 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
354 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
355 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
356 return $regs[1];
357 }
358
359
360 /******************************************************************************
361 ** Saves a message to a given folder -- used for saving sent messages
362 ******************************************************************************/
363 function sqimap_append ($imap_stream, $sent_folder, $length) {
364 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
365 $tmp = fgets ($imap_stream, 1024);
366 }
367
368 function sqimap_append_done ($imap_stream) {
369 fputs ($imap_stream, "\r\n");
370 $tmp = fgets ($imap_stream, 1024);
371 }
372 ?>