removed debugging information
[squirrelmail.git] / functions / imap_general.php
CommitLineData
d29aac0e 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") {
aae41ae9 40 echo "<br><b><font color=$color[2]>";
d29aac0e 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") {
aae41ae9 47 echo "<br><b><font color=$color[2]>";
d29aac0e 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 ******************************************************************************/
e1469126 66 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
d29aac0e 67 global $color;
e1469126 68 $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string);
d29aac0e 69 $server_info = fgets ($imap_stream, 1024);
70
71 /** Do some error correction **/
72 if (!$imap_stream) {
73 if (!$hide) {
d17b1a71 74 echo "Error connecting to IMAP server: $imap_server_address.<br>\r\n";
75 echo "$error_number : $error_string<br>\r\n";
d29aac0e 76 }
77 exit;
78 }
79
d17b1a71 80 fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n");
d29aac0e 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") {
d17b1a71 87 echo "Bad request: $read<br>\r\n";
d29aac0e 88 exit;
89 } else if (substr($read, 0, 7) == "a001 NO") {
90 ?>
91 <html>
19acd99f 92 <body bgcolor=ffffff>
d29aac0e 93 <br>
19acd99f 94 <center>
95 <table width=70% noborder bgcolor=ffffff align=center>
d29aac0e 96 <tr>
19acd99f 97 <td bgcolor=dcdcdc>
98 <font color=cc0000>
d29aac0e 99 <center>
100 <? echo _("ERROR") ?>
101 </center>
102 </font>
103 </td>
104 </tr>
105 <tr>
106 <td>
d29aac0e 107 <center>
108 <? echo _("Unknown user or password incorrect.") ?><br>
109 <a href="login.php"><? echo _("Click here to try again") ?></a>
110 </center>
d29aac0e 111 </td>
112 </tr>
113 </table>
19acd99f 114 </center>
d29aac0e 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) {
d17b1a71 138 fputs ($imap_stream, "a001 LOGOUT\r\n");
d29aac0e 139 }
140
141
142
143 /******************************************************************************
144 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
145 ******************************************************************************/
146 function sqimap_get_delimiter ($imap_stream) {
81a897dc 147 fputs ($imap_stream, ". LSUB \"\" *\r\n");
d29aac0e 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) {
d17b1a71 162 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
d29aac0e 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 }
f01b15e4 187 return trim($string);
d29aac0e 188 }
189
190
191 /******************************************************************************
06e55a3e 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
d29aac0e 197 ******************************************************************************/
198 function sqimap_find_displayable_name ($string) {
f01b15e4 199 $string = " ".trim($string);
d29aac0e 200 if (strpos($string, "<") && strpos($string, ">")) {
f01b15e4 201 if (strpos($string, "<") == 1) {
d29aac0e 202 $string = sqimap_find_email($string);
203 } else {
2c898a11 204 $string = trim($string);
d29aac0e 205 $string = substr($string, 0, strpos($string, "<"));
2c898a11 206 $string = ereg_replace ("\"", "", $string);
d29aac0e 207 }
208 }
209 return $string;
210 }
211
212
213
214 /******************************************************************************
215 ** Returns the number of unseen messages in this folder
216 ******************************************************************************/
217 function sqimap_unseen_messages ($imap_stream, &$num_unseen) {
d17b1a71 218 fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
d29aac0e 219 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
220 $unseen = false;
221
222 if (strlen($read_ary[0]) > 10) {
223 $unseen = true;
224 $ary = explode (" ", $read_ary[0]);
225 $num_unseen = count($ary) - 2;
226 } else {
227 $unseen = false;
228 $num_unseen = 0;
229 }
230
231 return $unseen;
232 }
d17b1a71 233
234
235 /******************************************************************************
236 ** Saves a message to a given folder -- used for saving sent messages
237 ******************************************************************************/
6441f7c6 238 function sqimap_append ($imap_stream, $sent_folder, $length) {
f8308f16 239 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
28010579 240 $tmp = fgets ($imap_stream, 1024);
d17b1a71 241 }
28010579 242
243 function sqimap_append_done ($imap_stream) {
244 fputs ($imap_stream, "\r\n");
f8308f16 245 $tmp = fgets ($imap_stream, 1024);
28010579 246 }
4ca45d7b 247?>