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