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