5 ** This implements all functions that do general imap functions.
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) {
16 $read = fgets ($imap_stream, 1024);
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);
25 if (substr($read, 0, strlen("$pre OK")) == "$pre OK") {
27 $message = trim(substr($read, strlen("$pre OK"), strlen($read)));
29 else if (substr($read, 0, strlen("$pre BAD")) == "$pre BAD") {
31 $message = trim(substr($read, strlen("$pre BAD"), strlen($read)));
35 $message = trim(substr($read, strlen("$pre NO"), strlen($read)));
38 if ($handle_errors == true) {
39 if ($response == "NO") {
40 echo "<br><b><font color=$color[2]>";
41 echo _("ERROR : Could not complete request.");
43 echo _("Reason Given: ");
44 echo "$message</font><br>";
46 } else if ($response == "BAD") {
47 echo "<br><b><font color=$color[2]>";
48 echo _("ERROR : Bad or malformed request.");
50 echo _("Server responded: ");
51 echo "$message</font><br>";
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, $imap_port, $hide) {
68 $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string);
69 $server_info = fgets ($imap_stream, 1024);
71 /** Do some error correction **/
74 echo "Error connecting to IMAP server: $imap_server_address.<br>\r\n";
75 echo "$error_number : $error_string<br>\r\n";
80 fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n");
81 $read = fgets ($imap_stream, 1024);
83 /** If the connection was not successful, lets see why **/
84 if (substr($read, 0, 7) != "a001 OK") {
86 if (substr($read, 0, 8) == "a001 BAD") {
87 echo "Bad request: $read<br>\r\n";
89 } else if (substr($read, 0, 7) == "a001 NO") {
95 <table width
=70% noborder bgcolor
=ffffff align
=center
>
100 <?
echo _("ERROR") ?
>
108 <?
echo _("Unknown user or password incorrect.") ?
><br
>
109 <a href
="login.php"><?
echo _("Click here to try again") ?
></a
>
120 echo "Unknown error: $read<br>";
134 /******************************************************************************
135 ** Simply logs out the imap session
136 ******************************************************************************/
137 function sqimap_logout ($imap_stream) {
138 fputs ($imap_stream, "a001 LOGOUT\r\n");
143 /******************************************************************************
144 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
145 ******************************************************************************/
146 function sqimap_get_delimiter ($imap_stream) {
147 fputs ($imap_stream, ". LSUB \"\" *\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);
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]);
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
183 if (strpos($string, "<") && strpos($string, ">")) {
184 $string = substr($string, strpos($string, "<")+
1);
185 $string = substr($string, 0, strpos($string, ">"));
187 return trim($string);
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);
204 $string = trim($string);
205 $string = substr($string, 0, strpos($string, "<"));
206 $string = ereg_replace ("\"", "", $string);
214 /******************************************************************************
215 ** Returns the number of unseen messages in this folder
216 ******************************************************************************/
217 function sqimap_unseen_messages ($imap_stream, &$num_unseen) {
218 fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
219 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
222 if (strlen($read_ary[0]) > 10) {
224 $ary = explode (" ", $read_ary[0]);
225 $num_unseen = count($ary) - 2;
235 /******************************************************************************
236 ** Saves a message to a given folder -- used for saving sent messages
237 ******************************************************************************/
238 function sqimap_append ($imap_stream, $sent_folder, $length) {
239 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
240 $tmp = fgets ($imap_stream, 1024);
244 function sqimap_append_done ($imap_stream) {
245 fputs ($imap_stream, "\r\n");
246 $tmp = fgets ($imap_stream, 1024);