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