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