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