clicking on email addr sends you to compose screen
[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;
2a32fc83 68 global $PHPSESSID;
e1469126 69 $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string);
d29aac0e 70 $server_info = fgets ($imap_stream, 1024);
71
72 /** Do some error correction **/
73 if (!$imap_stream) {
74 if (!$hide) {
d17b1a71 75 echo "Error connecting to IMAP server: $imap_server_address.<br>\r\n";
76 echo "$error_number : $error_string<br>\r\n";
d29aac0e 77 }
78 exit;
79 }
80
d17b1a71 81 fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n");
d29aac0e 82 $read = fgets ($imap_stream, 1024);
83
84 /** If the connection was not successful, lets see why **/
85 if (substr($read, 0, 7) != "a001 OK") {
86 if (!$hide) {
87 if (substr($read, 0, 8) == "a001 BAD") {
d17b1a71 88 echo "Bad request: $read<br>\r\n";
d29aac0e 89 exit;
90 } else if (substr($read, 0, 7) == "a001 NO") {
91 ?>
92 <html>
19acd99f 93 <body bgcolor=ffffff>
d29aac0e 94 <br>
19acd99f 95 <center>
96 <table width=70% noborder bgcolor=ffffff align=center>
d29aac0e 97 <tr>
19acd99f 98 <td bgcolor=dcdcdc>
99 <font color=cc0000>
d29aac0e 100 <center>
101 <? echo _("ERROR") ?>
102 </center>
103 </font>
104 </td>
105 </tr>
106 <tr>
107 <td>
d29aac0e 108 <center>
109 <? echo _("Unknown user or password incorrect.") ?><br>
110 <a href="login.php"><? echo _("Click here to try again") ?></a>
111 </center>
d29aac0e 112 </td>
113 </tr>
114 </table>
19acd99f 115 </center>
d29aac0e 116 </body>
117 </html>
118 <?
2a32fc83 119 session_destroy();
d29aac0e 120 exit;
121 } else {
122 echo "Unknown error: $read<br>";
123 exit;
124 }
125 } else {
126 exit;
127 }
128 }
129
130 return $imap_stream;
131 }
132
133
134
135
136 /******************************************************************************
137 ** Simply logs out the imap session
138 ******************************************************************************/
139 function sqimap_logout ($imap_stream) {
d17b1a71 140 fputs ($imap_stream, "a001 LOGOUT\r\n");
d29aac0e 141 }
142
143
144
145 /******************************************************************************
146 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
147 ******************************************************************************/
148 function sqimap_get_delimiter ($imap_stream) {
81a897dc 149 fputs ($imap_stream, ". LSUB \"\" *\r\n");
d29aac0e 150 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
151 $quote_position = strpos ($read[0], "\"");
152 $delim = substr ($read[0], $quote_position+1, 1);
153
154 return $delim;
155 }
156
157
158
159
160 /******************************************************************************
161 ** Gets the number of messages in the current mailbox.
162 ******************************************************************************/
163 function sqimap_get_num_messages ($imap_stream, $mailbox) {
d17b1a71 164 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
d29aac0e 165 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
166 for ($i = 0; $i < count($read_ary); $i++) {
167 if (substr(trim($read_ary[$i]), -6) == EXISTS) {
168 $array = explode (" ", $read_ary[$i]);
169 $num = $array[1];
170 }
171 }
172 return $num;
173 }
174
175
176 /******************************************************************************
177 ** Returns a displayable email address
178 ******************************************************************************/
179 function sqimap_find_email ($string) {
180 /** Luke Ehresman <lehresma@css.tayloru.edu>
181 ** <lehresma@css.tayloru.edu>
182 ** lehresma@css.tayloru.edu
183 **/
184
185 if (strpos($string, "<") && strpos($string, ">")) {
186 $string = substr($string, strpos($string, "<")+1);
187 $string = substr($string, 0, strpos($string, ">"));
188 }
f01b15e4 189 return trim($string);
d29aac0e 190 }
191
192
193 /******************************************************************************
06e55a3e 194 ** Takes the From: field, and creates a displayable name.
195 ** Luke Ehresman <lkehresman@yahoo.com>
196 ** becomes: Luke Ehresman
197 ** <lkehresman@yahoo.com>
198 ** becomes: lkehresman@yahoo.com
d29aac0e 199 ******************************************************************************/
200 function sqimap_find_displayable_name ($string) {
f01b15e4 201 $string = " ".trim($string);
d29aac0e 202 if (strpos($string, "<") && strpos($string, ">")) {
f01b15e4 203 if (strpos($string, "<") == 1) {
d29aac0e 204 $string = sqimap_find_email($string);
205 } else {
2c898a11 206 $string = trim($string);
d29aac0e 207 $string = substr($string, 0, strpos($string, "<"));
2c898a11 208 $string = ereg_replace ("\"", "", $string);
d29aac0e 209 }
210 }
211 return $string;
212 }
213
214
215
216 /******************************************************************************
217 ** Returns the number of unseen messages in this folder
218 ******************************************************************************/
219 function sqimap_unseen_messages ($imap_stream, &$num_unseen) {
d17b1a71 220 fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
d29aac0e 221 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
222 $unseen = false;
223
224 if (strlen($read_ary[0]) > 10) {
225 $unseen = true;
226 $ary = explode (" ", $read_ary[0]);
227 $num_unseen = count($ary) - 2;
228 } else {
229 $unseen = false;
230 $num_unseen = 0;
231 }
232
233 return $unseen;
234 }
d17b1a71 235
236
237 /******************************************************************************
238 ** Saves a message to a given folder -- used for saving sent messages
239 ******************************************************************************/
6441f7c6 240 function sqimap_append ($imap_stream, $sent_folder, $length) {
f8308f16 241 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
28010579 242 $tmp = fgets ($imap_stream, 1024);
d17b1a71 243 }
28010579 244
245 function sqimap_append_done ($imap_stream) {
246 fputs ($imap_stream, "\r\n");
f8308f16 247 $tmp = fgets ($imap_stream, 1024);
28010579 248 }
4ca45d7b 249?>