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