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);
17 // echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
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);
24 // echo "<small><tt><font color=cc0000>$read</font></tt></small><br>";
27 if (substr($read, 0, strlen("$pre OK")) == "$pre OK") {
29 $message = trim(substr($read, strlen("$pre OK"), strlen($read)));
31 else if (substr($read, 0, strlen("$pre BAD")) == "$pre BAD") {
33 $message = trim(substr($read, strlen("$pre BAD"), strlen($read)));
37 $message = trim(substr($read, strlen("$pre NO"), strlen($read)));
40 if ($handle_errors == true) {
41 if ($response == "NO") {
42 echo "<br><b><font color=$color[2]>";
43 echo _("ERROR : Could not complete request.");
45 echo _("Reason Given: ");
46 echo "$message</font><br>";
48 } else if ($response == "BAD") {
49 echo "<br><b><font color=$color[2]>";
50 echo _("ERROR : Bad or malformed request.");
52 echo _("Server responded: ");
53 echo "$message</font><br>";
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) {
70 $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string);
71 $server_info = fgets ($imap_stream, 1024);
73 /** Do some error correction **/
76 echo "Error connecting to IMAP server: $imap_server_address.<br>\r\n";
77 echo "$error_number : $error_string<br>\r\n";
82 fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n");
83 $read = fgets ($imap_stream, 1024);
85 /** If the connection was not successful, lets see why **/
86 if (substr($read, 0, 7) != "a001 OK") {
88 if (substr($read, 0, 8) == "a001 BAD") {
89 echo "Bad request: $read<br>\r\n";
91 } else if (substr($read, 0, 7) == "a001 NO") {
97 <table width
=70% noborder bgcolor
=ffffff align
=center
>
102 <?php
echo _("ERROR") ?
>
110 <?php
echo _("Unknown user or password incorrect.") ?
><br
>
111 <a href
="login.php"><?php
echo _("Click here to try again") ?
></a
>
123 echo "Unknown error: $read<br>";
137 /******************************************************************************
138 ** Simply logs out the imap session
139 ******************************************************************************/
140 function sqimap_logout ($imap_stream) {
141 fputs ($imap_stream, "a001 LOGOUT\r\n");
146 /******************************************************************************
147 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
148 ******************************************************************************/
149 function sqimap_get_delimiter ($imap_stream) {
150 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
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);
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]);
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
186 if (strpos($string, "<") && strpos($string, ">")) {
187 $string = substr($string, strpos($string, "<")+
1);
188 $string = substr($string, 0, strpos($string, ">"));
190 return trim($string);
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);
207 $string = trim($string);
208 $string = substr($string, 0, strpos($string, "<"));
209 $string = ereg_replace ("\"", "", $string);
217 /******************************************************************************
218 ** Returns the number of unseen messages in this folder
219 ******************************************************************************/
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");
223 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
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));
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);
239 function sqimap_append_done ($imap_stream) {
240 fputs ($imap_stream, "\r\n");
241 $tmp = fgets ($imap_stream, 1024);