d23f0c375944ce4e66fc1c28fa392af5b65079a3
[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 $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);
57 $continue = false;
58 } else {
59 $data[] = $read;
60 $read = fgets($imap_stream, 9096);
61 }
62 $total_size += strlen($read);
63 } else {
64 if (ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
65 $continue = false;
66 } else {
67 $data[] = $read;
68 $read = fgets ($imap_stream, 9096);
69 }
70 }
71 }
72
73 $response = $regs[1];
74 $message = trim($regs[2]);
75
76 if ($imap_general_debug) echo "--<br>";
77
78 if ($handle_errors == false)
79 return $data;
80
81 if ($response == "NO") {
82 // ignore this error from m$ exchange, it is not fatal (aka bug)
83 if (!ereg("command resulted in",$message)) {
84 set_up_language($squirrelmail_language);
85 echo "<br><b><font color=$color[2]>\n";
86 echo _("ERROR : Could not complete request.");
87 echo "</b><br>\n";
88 echo _("Reason Given: ");
89 echo $message . "</font><br>\n";
90 exit;
91 }
92 } else if ($response == "BAD") {
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;
100 }
101
102 return $data;
103 }
104
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) {
110 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
111
112 $imap_stream = fsockopen ($imap_server_address, $imap_port,
113 $error_number, $error_string, 15);
114 $server_info = fgets ($imap_stream, 1024);
115
116 // Decrypt the password
117 $password = OneTimePadDecrypt($password, $onetimepad);
118
119 /** Do some error correction **/
120 if (!$imap_stream) {
121 if (!$hide) {
122 set_up_language($squirrelmail_language, true);
123 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
124 echo "$error_number : $error_string<br>\r\n";
125 }
126 exit;
127 }
128
129 fputs ($imap_stream, "a001 LOGIN \"" . quotemeta($username) .
130 "\" \"" . quotemeta($password) . "\"\r\n");
131 $read = sqimap_read_data ($imap_stream, "a001", false, $response, $message);
132
133 /** If the connection was not successful, lets see why **/
134 if ($response != "OK") {
135 if (!$hide) {
136 if ($response != "NO") {
137 // "BAD" and anything else gets reported here.
138 set_up_language($squirrelmail_language, true);
139 if ($response == "BAD")
140 printf (_("Bad request: %s")."<br>\r\n", $message);
141 else
142 printf (_("Unknown error: %s") . "<br>\n", $message);
143 echo "<br>";
144 echo _("Read data:") . "<br>\n";
145 if (is_array($read))
146 {
147 foreach ($read as $line)
148 {
149 echo htmlspecialchars($line) . "<br>\n";
150 }
151 }
152 exit;
153 } else {
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
163 set_up_language($squirrelmail_language, true);
164
165 ?>
166 <html>
167 <body bgcolor=ffffff>
168 <br>
169 <center>
170 <table width=70% noborder bgcolor=ffffff align=center>
171 <tr>
172 <td bgcolor=dcdcdc>
173 <font color=cc0000>
174 <center>
175 <?php echo _("ERROR") ?>
176 </center>
177 </font>
178 </td>
179 </tr>
180 <tr>
181 <td>
182 <center>
183 <?php echo _("Unknown user or password incorrect.") ?><br>
184 <a href="login.php" target="_top"><?php echo _("Click here to try again") ?></a>
185 </center>
186 </td>
187 </tr>
188 </table>
189 </center>
190 </body>
191 </html>
192 <?php
193 session_destroy();
194 exit;
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
214 function sqimap_capability($imap_stream, $capability) {
215 global $sqimap_capabilities;
216 global $imap_general_debug;
217
218 if (!is_array($sqimap_capabilities)) {
219 fputs ($imap_stream, "a001 CAPABILITY\r\n");
220 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
221
222 $c = explode(' ', $read[0]);
223 for ($i=2; $i < count($c); $i++) {
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;
229 }
230 }
231 return $sqimap_capabilities[$capability];
232 }
233
234 /******************************************************************************
235 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
236 ******************************************************************************/
237 function sqimap_get_delimiter ($imap_stream = false) {
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 }
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");
257 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
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 {
264 list($_, $n, $_, $d) = explode('"', $v);
265 $pn[$n] = $d;
266 }
267 /* OS: We don't need this code right now, it is for other_users and shared folders
268 if (eregi('^\( *\((.*)\) *\)', $data[2], $data2))
269 $on = $data2[1];
270 if (eregi('^\( *\((.*)\) *\)', $data[3], $data2))
271 $sn = $data2[1];
272 unset($data);
273 $ona = explode(')(', $on);
274 while (list($k, $v) = each($ona))
275 {
276 list($_, $n, $_, $d) = explode('"', $v);
277 $on[$n] = $d;
278 }
279 $sna = explode(')(', $sn);
280 while (list($k, $v) = each($sna))
281 {
282 list($_, $n, $_, $d) = explode('"', $v);
283 $sn[$n] = $d;
284 }
285 */
286 }
287 $sqimap_delimiter = $pn[0];
288 } else {
289 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
290 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
291 $quote_position = strpos ($read[0], "\"");
292 $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
293 }
294 }
295 return $sqimap_delimiter;
296 }
297
298
299 /******************************************************************************
300 ** Gets the number of messages in the current mailbox.
301 ******************************************************************************/
302 function sqimap_get_num_messages ($imap_stream, $mailbox) {
303 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
304 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
305 for ($i = 0; $i < count($read_ary); $i++) {
306 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
307 return $regs[1];
308 }
309 }
310 return "BUG! Couldn't get number of messages in $mailbox!";
311 }
312
313
314 /******************************************************************************
315 ** Returns a displayable email address
316 ******************************************************************************/
317 function sqimap_find_email ($string) {
318 /** Luke Ehresman <lehresma@css.tayloru.edu>
319 ** <lehresma@css.tayloru.edu>
320 ** lehresma@css.tayloru.edu
321 **
322 ** What about
323 ** lehresma@css.tayloru.edu (Luke Ehresman)
324 **/
325
326 if (ereg("<([^>]+)>", $string, $regs)) {
327 $string = $regs[1];
328 }
329 return trim($string);
330 }
331
332
333 /******************************************************************************
334 ** Takes the From: field, and creates a displayable name.
335 ** Luke Ehresman <lkehresman@yahoo.com>
336 ** becomes: Luke Ehresman
337 ** <lkehresman@yahoo.com>
338 ** becomes: lkehresman@yahoo.com
339 ******************************************************************************/
340 function sqimap_find_displayable_name ($string) {
341 $string = " ".trim($string);
342 $orig_string = $string;
343 if (strpos($string, "<") && strpos($string, ">")) {
344 if (strpos($string, "<") == 1) {
345 $string = sqimap_find_email($string);
346 } else {
347 $string = trim($string);
348 $string = substr($string, 0, strpos($string, "<"));
349 $string = ereg_replace ("\"", "", $string);
350 }
351
352 if (trim($string) == "") {
353 $string = sqimap_find_email($orig_string);
354 }
355 }
356 return $string;
357 }
358
359
360 /******************************************************************************
361 ** Returns the number of unseen messages in this folder
362 ******************************************************************************/
363 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
364 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
365 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
366 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
367 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
368 return $regs[1];
369 }
370
371
372 /******************************************************************************
373 ** Saves a message to a given folder -- used for saving sent messages
374 ******************************************************************************/
375 function sqimap_append ($imap_stream, $sent_folder, $length) {
376 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
377 $tmp = fgets ($imap_stream, 1024);
378 }
379
380 function sqimap_append_done ($imap_stream) {
381 fputs ($imap_stream, "\r\n");
382 $tmp = fgets ($imap_stream, 1024);
383 }
384 ?>