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; |
165e24a7 |
103 | } else { |
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 | ******************************************************************************/ |
1d2ce1c3 |
169 | function sqimap_get_delimiter ($imap_stream = false) { |
170 | fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n"); |
171 | $read = sqimap_read_data($imap_stream, ".", true, $a, $b); |
172 | $quote_position = strpos ($read[0], "\""); |
173 | $delim = substr ($read[0], $quote_position+1, 1); |
174 | |
175 | return $delim; |
176 | |
177 | /* According to something that I can't find, this is supposed to work on all systems |
178 | |
441f2d33 |
179 | fputs ($imap_stream, "a001 NAMESPACE\r\n"); |
180 | $read = sqimap_read_data($imap_stream, "a001", true, $a, $b); |
181 | eregi("\"\" \"(.)\"", $read[0], $regs); |
182 | return $regs[1]; |
1d2ce1c3 |
183 | */ |
052e0c26 |
184 | } |
185 | |
186 | |
187 | |
188 | |
189 | /****************************************************************************** |
190 | ** Gets the number of messages in the current mailbox. |
191 | ******************************************************************************/ |
192 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
193 | fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n"); |
194 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message); |
195 | for ($i = 0; $i < count($read_ary); $i++) { |
441f2d33 |
196 | if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) { |
197 | return $regs[1]; |
052e0c26 |
198 | } |
199 | } |
441f2d33 |
200 | return "BUG! Couldn't get number of messages in $mailbox!"; |
052e0c26 |
201 | } |
202 | |
203 | |
204 | /****************************************************************************** |
205 | ** Returns a displayable email address |
206 | ******************************************************************************/ |
207 | function sqimap_find_email ($string) { |
208 | /** Luke Ehresman <lehresma@css.tayloru.edu> |
209 | ** <lehresma@css.tayloru.edu> |
210 | ** lehresma@css.tayloru.edu |
1d2ce1c3 |
211 | ** |
212 | ** What about |
213 | ** lehresma@css.tayloru.edu (Luke Ehresman) |
052e0c26 |
214 | **/ |
215 | |
441f2d33 |
216 | if (ereg("<([^>]+)>", $string, $regs)) { |
217 | $string = $regs[1]; |
052e0c26 |
218 | } |
219 | return trim($string); |
220 | } |
221 | |
222 | |
223 | /****************************************************************************** |
224 | ** Takes the From: field, and creates a displayable name. |
225 | ** Luke Ehresman <lkehresman@yahoo.com> |
226 | ** becomes: Luke Ehresman |
227 | ** <lkehresman@yahoo.com> |
228 | ** becomes: lkehresman@yahoo.com |
229 | ******************************************************************************/ |
230 | function sqimap_find_displayable_name ($string) { |
1d2ce1c3 |
231 | $string = " ".trim($string); |
232 | $orig_string = $string; |
233 | if (strpos($string, "<") && strpos($string, ">")) { |
234 | if (strpos($string, "<") == 1) { |
235 | $string = sqimap_find_email($string); |
236 | } else { |
237 | $string = trim($string); |
238 | $string = substr($string, 0, strpos($string, "<")); |
239 | $string = ereg_replace ("\"", "", $string); |
240 | } |
241 | |
242 | if (trim($string) == "") { |
243 | $string = sqimap_find_email($orig_string); |
244 | } |
245 | } |
246 | return $string; |
052e0c26 |
247 | } |
248 | |
249 | |
052e0c26 |
250 | /****************************************************************************** |
251 | ** Returns the number of unseen messages in this folder |
252 | ******************************************************************************/ |
c5eb2c03 |
253 | function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) { |
254 | //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n"); |
255 | fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n"); |
052e0c26 |
256 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message); |
441f2d33 |
257 | ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs); |
258 | return $regs[1]; |
052e0c26 |
259 | } |
260 | |
261 | |
262 | /****************************************************************************** |
263 | ** Saves a message to a given folder -- used for saving sent messages |
264 | ******************************************************************************/ |
265 | function sqimap_append ($imap_stream, $sent_folder, $length) { |
266 | fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n"); |
267 | $tmp = fgets ($imap_stream, 1024); |
268 | } |
269 | |
270 | function sqimap_append_done ($imap_stream) { |
271 | fputs ($imap_stream, "\r\n"); |
272 | $tmp = fgets ($imap_stream, 1024); |
273 | } |
274 | ?> |