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