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