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