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