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