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