added saving attachments again, and removed a "click here" screen after
[squirrelmail.git] / functions / imap_general.php
1 <?
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) {
14 global $color;
15
16 $read = fgets ($imap_stream, 1024);
17 $counter = 0;
18 while ((substr($read, 0, strlen("$pre OK")) != "$pre OK") &&
19 (substr($read, 0, strlen("$pre BAD")) != "$pre BAD") &&
20 (substr($read, 0, strlen("$pre NO")) != "$pre NO")) {
21 $data[$counter] = $read;
22 $read = fgets ($imap_stream, 1024);
23 $counter++;
24 }
25 if (substr($read, 0, strlen("$pre OK")) == "$pre OK") {
26 $response = "OK";
27 $message = trim(substr($read, strlen("$pre OK"), strlen($read)));
28 }
29 else if (substr($read, 0, strlen("$pre BAD")) == "$pre BAD") {
30 $response = "BAD";
31 $message = trim(substr($read, strlen("$pre BAD"), strlen($read)));
32 }
33 else {
34 $response = "NO";
35 $message = trim(substr($read, strlen("$pre NO"), strlen($read)));
36 }
37
38 if ($handle_errors == true) {
39 if ($response == "NO") {
40 echo "<br><b><font color=$color[2]>";
41 echo _("ERROR : Could not complete request.");
42 echo "</b><br>";
43 echo _("Reason Given: ");
44 echo "$message</font><br>";
45 exit;
46 } else if ($response == "BAD") {
47 echo "<br><b><font color=$color[2]>";
48 echo _("ERROR : Bad or malformed request.");
49 echo "</b><br>";
50 echo _("Server responded: ");
51 echo "$message</font><br>";
52 exit;
53 }
54 }
55
56 return $data;
57 }
58
59
60
61
62 /******************************************************************************
63 ** Logs the user into the imap server. If $hide is set, no error messages
64 ** will be displayed. This function returns the imap connection handle.
65 ******************************************************************************/
66 function sqimap_login ($username, $password, $imap_server_address, $hide) {
67 global $color;
68 $imap_stream = fsockopen ($imap_server_address, 143, &$error_number, &$error_string);
69 $server_info = fgets ($imap_stream, 1024);
70
71 /** Do some error correction **/
72 if (!$imap_stream) {
73 if (!$hide) {
74 echo "Error connecting to IMAP server: $imap_server_address.<br>\r\n";
75 echo "$error_number : $error_string<br>\r\n";
76 }
77 exit;
78 }
79
80 fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n");
81 $read = fgets ($imap_stream, 1024);
82
83 /** If the connection was not successful, lets see why **/
84 if (substr($read, 0, 7) != "a001 OK") {
85 if (!$hide) {
86 if (substr($read, 0, 8) == "a001 BAD") {
87 echo "Bad request: $read<br>\r\n";
88 exit;
89 } else if (substr($read, 0, 7) == "a001 NO") {
90 ?>
91 <html>
92 <body bgcolor=ffffff>
93 <br>
94 <center>
95 <table width=70% noborder bgcolor=ffffff align=center>
96 <tr>
97 <td bgcolor=dcdcdc>
98 <font color=cc0000>
99 <center>
100 <? echo _("ERROR") ?>
101 </center>
102 </font>
103 </td>
104 </tr>
105 <tr>
106 <td>
107 <center>
108 <? echo _("Unknown user or password incorrect.") ?><br>
109 <a href="login.php"><? echo _("Click here to try again") ?></a>
110 </center>
111 </td>
112 </tr>
113 </table>
114 </center>
115 </body>
116 </html>
117 <?
118 exit;
119 } else {
120 echo "Unknown error: $read<br>";
121 exit;
122 }
123 } else {
124 exit;
125 }
126 }
127
128 return $imap_stream;
129 }
130
131
132
133
134 /******************************************************************************
135 ** Simply logs out the imap session
136 ******************************************************************************/
137 function sqimap_logout ($imap_stream) {
138 fputs ($imap_stream, "a001 LOGOUT\r\n");
139 }
140
141
142
143 /******************************************************************************
144 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
145 ******************************************************************************/
146 function sqimap_get_delimiter ($imap_stream) {
147 fputs ($imap_stream, ". LIST \"\" *\r\n");
148 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
149 $quote_position = strpos ($read[0], "\"");
150 $delim = substr ($read[0], $quote_position+1, 1);
151
152 return $delim;
153 }
154
155
156
157
158 /******************************************************************************
159 ** Gets the number of messages in the current mailbox.
160 ******************************************************************************/
161 function sqimap_get_num_messages ($imap_stream, $mailbox) {
162 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
163 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
164 for ($i = 0; $i < count($read_ary); $i++) {
165 if (substr(trim($read_ary[$i]), -6) == EXISTS) {
166 $array = explode (" ", $read_ary[$i]);
167 $num = $array[1];
168 }
169 }
170 return $num;
171 }
172
173
174 /******************************************************************************
175 ** Returns a displayable email address
176 ******************************************************************************/
177 function sqimap_find_email ($string) {
178 /** Luke Ehresman <lehresma@css.tayloru.edu>
179 ** <lehresma@css.tayloru.edu>
180 ** lehresma@css.tayloru.edu
181 **/
182
183 if (strpos($string, "<") && strpos($string, ">")) {
184 $string = substr($string, strpos($string, "<")+1);
185 $string = substr($string, 0, strpos($string, ">"));
186 }
187 return trim($string);
188 }
189
190
191 /******************************************************************************
192 ** Takes the From: field, and creates a displayable name.
193 ** Luke Ehresman <lkehresman@yahoo.com>
194 ** becomes: Luke Ehresman
195 ** <lkehresman@yahoo.com>
196 ** becomes: lkehresman@yahoo.com
197 ******************************************************************************/
198 function sqimap_find_displayable_name ($string) {
199 $string = " ".trim($string);
200 if (strpos($string, "<") && strpos($string, ">")) {
201 if (strpos($string, "<") == 1) {
202 $string = sqimap_find_email($string);
203 } else {
204 $string = substr($string, 0, strpos($string, "<"));
205 }
206 }
207 return $string;
208 }
209
210
211
212 /******************************************************************************
213 ** Returns the number of unseen messages in this folder
214 ******************************************************************************/
215 function sqimap_unseen_messages ($imap_stream, &$num_unseen) {
216 fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
217 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
218 $unseen = false;
219
220 if (strlen($read_ary[0]) > 10) {
221 $unseen = true;
222 $ary = explode (" ", $read_ary[0]);
223 $num_unseen = count($ary) - 2;
224 } else {
225 $unseen = false;
226 $num_unseen = 0;
227 }
228
229 return $unseen;
230 }
231
232
233 /******************************************************************************
234 ** Saves a message to a given folder -- used for saving sent messages
235 ******************************************************************************/
236 function sqimap_append ($imap_stream, $mailbox, $body, $to, $cc, $bcc, $subject, $data_dir, $username, $domain, $version) {
237 global $sent_folder, $data_dir;
238
239 $from = getPref($data_dir, $username, "full_name");
240 $from_addr = getPref($data_dir, $username, "email_address");
241 if ($from_addr == "")
242 $from_addr = "$username@$domain";
243
244 if ($from == "")
245 $from = "<$from_addr>";
246 else
247 $from = $from . " <$from_addr>";
248
249 $message = "Date: ".date("D, j M Y H:i:s ", mktime()) . timezone() . "\r\n";
250 $message .= "Subject: ". $subject."\r\n";
251 $message .= "From: ".$from."\r\n";
252 $message .= "To: ".$to."\r\n";
253 if ($cc_list) {
254 $message .= "Cc: ".$cc."\r\n"; // Who the CCs are
255 }
256 $message .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
257 $message .= "Content-Transfer-Encoding: 8bit\r\n";
258 $message .= "\r\n";
259 $message .= stripslashes($body) . "\r\n";
260 $message .= "\r\n";
261
262 $size = count_chars($message);
263 fputs ($imap_stream, "a001 APPEND $sent_folder (\\Seen) \{$size}\r\n");
264 fputs ($imap_stream, "$message");
265
266 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
267 }
268 ?>