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