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