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 <?php
echo _("ERROR") ?
>
108 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
109 <a href
="login.php"><?php
echo _("Click here to try again") ?
></a
>
121 echo "Unknown error: $read<br>";
135 /******************************************************************************
136 ** Simply logs out the imap session
137 ******************************************************************************/
138 function sqimap_logout ($imap_stream) {
139 fputs ($imap_stream, "a001 LOGOUT\r\n");
144 /******************************************************************************
145 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
146 ******************************************************************************/
147 function sqimap_get_delimiter ($imap_stream) {
148 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
149 $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
150 $quote_position = strpos ($read[0], "\"");
151 $delim = substr ($read[0], $quote_position+
1, 1);
159 /******************************************************************************
160 ** Gets the number of messages in the current mailbox.
161 ******************************************************************************/
162 function sqimap_get_num_messages ($imap_stream, $mailbox) {
163 fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
164 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
165 for ($i = 0; $i < count($read_ary); $i++
) {
166 if (substr(trim($read_ary[$i]), -6) == EXISTS
) {
167 $array = explode (" ", $read_ary[$i]);
175 /******************************************************************************
176 ** Returns a displayable email address
177 ******************************************************************************/
178 function sqimap_find_email ($string) {
179 /** Luke Ehresman <lehresma@css.tayloru.edu>
180 ** <lehresma@css.tayloru.edu>
181 ** lehresma@css.tayloru.edu
184 if (strpos($string, "<") && strpos($string, ">")) {
185 $string = substr($string, strpos($string, "<")+
1);
186 $string = substr($string, 0, strpos($string, ">"));
188 return trim($string);
192 /******************************************************************************
193 ** Takes the From: field, and creates a displayable name.
194 ** Luke Ehresman <lkehresman@yahoo.com>
195 ** becomes: Luke Ehresman
196 ** <lkehresman@yahoo.com>
197 ** becomes: lkehresman@yahoo.com
198 ******************************************************************************/
199 function sqimap_find_displayable_name ($string) {
200 $string = " ".trim($string);
201 if (strpos($string, "<") && strpos($string, ">")) {
202 if (strpos($string, "<") == 1) {
203 $string = sqimap_find_email($string);
205 $string = trim($string);
206 $string = substr($string, 0, strpos($string, "<"));
207 $string = ereg_replace ("\"", "", $string);
215 /******************************************************************************
216 ** Returns the number of unseen messages in this folder
217 ******************************************************************************/
218 function sqimap_unseen_messages ($imap_stream, &$num_unseen) {
219 fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
220 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
223 if (strlen($read_ary[0]) > 10) {
225 $ary = explode (" ", $read_ary[0]);
226 $num_unseen = count($ary) - 2;
236 /******************************************************************************
237 ** Saves a message to a given folder -- used for saving sent messages
238 ******************************************************************************/
239 function sqimap_append ($imap_stream, $sent_folder, $length) {
240 fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
241 $tmp = fgets ($imap_stream, 1024);
244 function sqimap_append_done ($imap_stream) {
245 fputs ($imap_stream, "\r\n");
246 $tmp = fgets ($imap_stream, 1024);