| 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") { |
| 40 | echo "<br><b><font color=$color[2]>"; |
| 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") { |
| 47 | echo "<br><b><font color=$color[2]>"; |
| 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 | ******************************************************************************/ |
| 66 | function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) { |
| 67 | global $color; |
| 68 | global $PHPSESSID; |
| 69 | $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string); |
| 70 | $server_info = fgets ($imap_stream, 1024); |
| 71 | |
| 72 | /** Do some error correction **/ |
| 73 | if (!$imap_stream) { |
| 74 | if (!$hide) { |
| 75 | echo "Error connecting to IMAP server: $imap_server_address.<br>\r\n"; |
| 76 | echo "$error_number : $error_string<br>\r\n"; |
| 77 | } |
| 78 | exit; |
| 79 | } |
| 80 | |
| 81 | fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n"); |
| 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") { |
| 88 | echo "Bad request: $read<br>\r\n"; |
| 89 | exit; |
| 90 | } else if (substr($read, 0, 7) == "a001 NO") { |
| 91 | ?> |
| 92 | <html> |
| 93 | <body bgcolor=ffffff> |
| 94 | <br> |
| 95 | <center> |
| 96 | <table width=70% noborder bgcolor=ffffff align=center> |
| 97 | <tr> |
| 98 | <td bgcolor=dcdcdc> |
| 99 | <font color=cc0000> |
| 100 | <center> |
| 101 | <? echo _("ERROR") ?> |
| 102 | </center> |
| 103 | </font> |
| 104 | </td> |
| 105 | </tr> |
| 106 | <tr> |
| 107 | <td> |
| 108 | <center> |
| 109 | <? echo _("Unknown user or password incorrect.") ?><br> |
| 110 | <a href="login.php"><? echo _("Click here to try again") ?></a> |
| 111 | </center> |
| 112 | </td> |
| 113 | </tr> |
| 114 | </table> |
| 115 | </center> |
| 116 | </body> |
| 117 | </html> |
| 118 | <? |
| 119 | session_destroy(); |
| 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) { |
| 140 | fputs ($imap_stream, "a001 LOGOUT\r\n"); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | /****************************************************************************** |
| 146 | ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test... |
| 147 | ******************************************************************************/ |
| 148 | function sqimap_get_delimiter ($imap_stream) { |
| 149 | fputs ($imap_stream, ". LSUB \"\" *\r\n"); |
| 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) { |
| 164 | fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n"); |
| 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 | } |
| 189 | return trim($string); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /****************************************************************************** |
| 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 |
| 199 | ******************************************************************************/ |
| 200 | function sqimap_find_displayable_name ($string) { |
| 201 | $string = " ".trim($string); |
| 202 | if (strpos($string, "<") && strpos($string, ">")) { |
| 203 | if (strpos($string, "<") == 1) { |
| 204 | $string = sqimap_find_email($string); |
| 205 | } else { |
| 206 | $string = trim($string); |
| 207 | $string = substr($string, 0, strpos($string, "<")); |
| 208 | $string = ereg_replace ("\"", "", $string); |
| 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) { |
| 220 | fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n"); |
| 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 | } |
| 235 | |
| 236 | |
| 237 | /****************************************************************************** |
| 238 | ** Saves a message to a given folder -- used for saving sent messages |
| 239 | ******************************************************************************/ |
| 240 | function sqimap_append ($imap_stream, $sent_folder, $length) { |
| 241 | fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n"); |
| 242 | $tmp = fgets ($imap_stream, 1024); |
| 243 | } |
| 244 | |
| 245 | function sqimap_append_done ($imap_stream) { |
| 246 | fputs ($imap_stream, "\r\n"); |
| 247 | $tmp = fgets ($imap_stream, 1024); |
| 248 | } |
| 249 | ?> |