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