Fix for bug #117438, incorrect deletion of attachments.
[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
8 /******************************************************************************
9 ** Reads the output from the IMAP stream. If handle_errors is set to true,
10 ** this will also handle all errors that are received. If it is not set,
11 ** the errors will be sent back through $response and $message
12 ******************************************************************************/
3d2504a1 13 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
441f2d33 14 global $color, $squirrelmail_language;
052e0c26 15
a3432f47 16 //$imap_general_debug = true;
17 $imap_general_debug = false;
18
052e0c26 19 $read = fgets ($imap_stream, 1024);
a3432f47 20 if ($imap_general_debug) echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
052e0c26 21 $counter = 0;
441f2d33 22 while (! ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
052e0c26 23 $data[$counter] = $read;
24 $read = fgets ($imap_stream, 1024);
a3432f47 25 if ($imap_general_debug) echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
052e0c26 26 $counter++;
3d2504a1 27 }
28
29 $response = $regs[1];
30 $message = trim($regs[2]);
31
a3432f47 32 if ($imap_general_debug) echo "--<br>";
052e0c26 33
34 if ($handle_errors == true) {
3d2504a1 35 if ($response == "NO") {
441f2d33 36 set_up_language($squirrelmail_language);
04632dbc 37 echo "<br><b><font color=$color[2]>\n";
052e0c26 38 echo _("ERROR : Could not complete request.");
04632dbc 39 echo "</b><br>\n";
052e0c26 40 echo _("Reason Given: ");
3d2504a1 41 echo $message . "</font><br>\n";
052e0c26 42 exit;
3d2504a1 43 } else if ($response == "BAD") {
441f2d33 44 set_up_language($squirrelmail_language);
04632dbc 45 echo "<br><b><font color=$color[2]>\n";
052e0c26 46 echo _("ERROR : Bad or malformed request.");
04632dbc 47 echo "</b><br>\n";
052e0c26 48 echo _("Server responded: ");
3d2504a1 49 echo $message . "</font><br>\n";
052e0c26 50 exit;
51 }
52 }
53
54 return $data;
55 }
56
57
58
59
60 /******************************************************************************
61 ** Logs the user into the imap server. If $hide is set, no error messages
62 ** will be displayed. This function returns the imap connection handle.
63 ******************************************************************************/
64 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
52eefafc 65 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
1b187352 66
052e0c26 67 $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string);
68 $server_info = fgets ($imap_stream, 1024);
69
52eefafc 70 // Decrypt the password
71 $password = OneTimePadDecrypt($password, $onetimepad);
72
052e0c26 73 /** Do some error correction **/
74 if (!$imap_stream) {
75 if (!$hide) {
441f2d33 76 set_up_language($squirrelmail_language, true);
1b187352 77 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
052e0c26 78 echo "$error_number : $error_string<br>\r\n";
79 }
80 exit;
81 }
82
83 fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n");
3d2504a1 84 $read = sqimap_read_data ($imap_stream, "a001", false, $response, $message);
052e0c26 85
86 /** If the connection was not successful, lets see why **/
3d2504a1 87 if ($response != "OK") {
052e0c26 88 if (!$hide) {
3d2504a1 89 if ($response != "NO") {
90 // "BAD" and anything else gets reported here.
441f2d33 91 set_up_language($squirrelmail_language, true);
3d2504a1 92 if ($response == "BAD")
93 printf (_("Bad request: %s")."<br>\r\n", $message);
94 else
95 printf (_("Unknown error: %s") . "<br>\n", $message);
96 echo "<br>";
97 echo _("Read data:") . "<br>\n";
98 foreach ($read as $line)
99 {
100 echo htmlspecialchars($line) . "<br>\n";
101 }
052e0c26 102 exit;
3d2504a1 103 } else if ($response == "NO") {
1b187352 104 // If the user does not log in with the correct
105 // username and password it is not possible to get the
106 // correct locale from the user's preferences.
107 // Therefore, apply the same hack as on the login
108 // screen.
109
110 // $squirrelmail_language is set by a cookie when
111 // the user selects language and logs out
112
441f2d33 113 set_up_language($squirrelmail_language, true);
1b187352 114
052e0c26 115 ?>
116 <html>
117 <body bgcolor=ffffff>
118 <br>
119 <center>
120 <table width=70% noborder bgcolor=ffffff align=center>
121 <tr>
122 <td bgcolor=dcdcdc>
123 <font color=cc0000>
124 <center>
59177427 125 <?php echo _("ERROR") ?>
052e0c26 126 </center>
127 </font>
128 </td>
129 </tr>
130 <tr>
131 <td>
132 <center>
59177427 133 <?php echo _("Unknown user or password incorrect.") ?><br>
5630c7ec 134 <a href="login.php" target="_top"><?php echo _("Click here to try again") ?></a>
052e0c26 135 </center>
136 </td>
137 </tr>
138 </table>
139 </center>
140 </body>
141 </html>
59177427 142 <?php
052e0c26 143 session_destroy();
144 exit;
052e0c26 145 }
146 } else {
147 exit;
148 }
149 }
150
151 return $imap_stream;
152 }
153
154
155
156
157 /******************************************************************************
158 ** Simply logs out the imap session
159 ******************************************************************************/
160 function sqimap_logout ($imap_stream) {
161 fputs ($imap_stream, "a001 LOGOUT\r\n");
162 }
163
164
165
166 /******************************************************************************
167 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
168 ******************************************************************************/
169 function sqimap_get_delimiter ($imap_stream) {
441f2d33 170 fputs ($imap_stream, "a001 NAMESPACE\r\n");
171 $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
172 eregi("\"\" \"(.)\"", $read[0], $regs);
173 return $regs[1];
052e0c26 174 }
175
176
177
178
179 /******************************************************************************
180 ** Gets the number of messages in the current mailbox.
181 ******************************************************************************/
182 function sqimap_get_num_messages ($imap_stream, $mailbox) {
183 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
184 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
185 for ($i = 0; $i < count($read_ary); $i++) {
441f2d33 186 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
187 return $regs[1];
052e0c26 188 }
189 }
441f2d33 190 return "BUG! Couldn't get number of messages in $mailbox!";
052e0c26 191 }
192
193
194 /******************************************************************************
195 ** Returns a displayable email address
196 ******************************************************************************/
197 function sqimap_find_email ($string) {
198 /** Luke Ehresman <lehresma@css.tayloru.edu>
199 ** <lehresma@css.tayloru.edu>
200 ** lehresma@css.tayloru.edu
201 **/
202
441f2d33 203 if (ereg("<([^>]+)>", $string, $regs)) {
204 $string = $regs[1];
052e0c26 205 }
206 return trim($string);
207 }
208
209
210 /******************************************************************************
211 ** Takes the From: field, and creates a displayable name.
212 ** Luke Ehresman <lkehresman@yahoo.com>
213 ** becomes: Luke Ehresman
214 ** <lkehresman@yahoo.com>
215 ** becomes: lkehresman@yahoo.com
216 ******************************************************************************/
217 function sqimap_find_displayable_name ($string) {
441f2d33 218 ereg("^\"?([^\"<]*)[\" <]*([^>]+)>?$", trim($string), $regs);
219 if ($regs[1] == '')
220 return $regs[2];
221 return $regs[1];
052e0c26 222 }
223
224
052e0c26 225 /******************************************************************************
226 ** Returns the number of unseen messages in this folder
227 ******************************************************************************/
c5eb2c03 228 function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
229 //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
230 fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
052e0c26 231 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
441f2d33 232 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
233 return $regs[1];
052e0c26 234 }
235
236
237 /******************************************************************************
238 ** Saves a message to a given folder -- used for saving sent messages
239 ******************************************************************************/
240 function sqimap_append ($imap_stream, $sent_folder, $length) {
241 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
242 $tmp = fgets ($imap_stream, 1024);
243 }
244
245 function sqimap_append_done ($imap_stream) {
246 fputs ($imap_stream, "\r\n");
247 $tmp = fgets ($imap_stream, 1024);
248 }
249?>