| 1 | <?php |
| 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, $squirrelmail_language; |
| 15 | |
| 16 | //$imap_general_debug = true; |
| 17 | $imap_general_debug = false; |
| 18 | |
| 19 | $read = fgets ($imap_stream, 1024); |
| 20 | if ($imap_general_debug) echo "<small><tt><font color=cc0000>$read</font></tt></small><br>"; |
| 21 | $counter = 0; |
| 22 | while (! ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) { |
| 23 | $data[$counter] = $read; |
| 24 | $read = fgets ($imap_stream, 1024); |
| 25 | if ($imap_general_debug) echo "<small><tt><font color=cc0000>$read</font></tt></small><br>"; |
| 26 | $counter++; |
| 27 | } |
| 28 | |
| 29 | $response = $regs[1]; |
| 30 | $message = trim($regs[2]); |
| 31 | |
| 32 | if ($imap_general_debug) echo "--<br>"; |
| 33 | |
| 34 | if ($handle_errors == true) { |
| 35 | if ($response == "NO") { |
| 36 | set_up_language($squirrelmail_language); |
| 37 | echo "<br><b><font color=$color[2]>\n"; |
| 38 | echo _("ERROR : Could not complete request."); |
| 39 | echo "</b><br>\n"; |
| 40 | echo _("Reason Given: "); |
| 41 | echo $message . "</font><br>\n"; |
| 42 | exit; |
| 43 | } else if ($response == "BAD") { |
| 44 | set_up_language($squirrelmail_language); |
| 45 | echo "<br><b><font color=$color[2]>\n"; |
| 46 | echo _("ERROR : Bad or malformed request."); |
| 47 | echo "</b><br>\n"; |
| 48 | echo _("Server responded: "); |
| 49 | echo $message . "</font><br>\n"; |
| 50 | exit; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return $data; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | /****************************************************************************** |
| 61 | ** Logs the user into the imap server. If $hide is set, no error messages |
| 62 | ** will be displayed. This function returns the imap connection handle. |
| 63 | ******************************************************************************/ |
| 64 | function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) { |
| 65 | global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad; |
| 66 | |
| 67 | $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string); |
| 68 | $server_info = fgets ($imap_stream, 1024); |
| 69 | |
| 70 | // Decrypt the password |
| 71 | $password = OneTimePadDecrypt($password, $onetimepad); |
| 72 | |
| 73 | /** Do some error correction **/ |
| 74 | if (!$imap_stream) { |
| 75 | if (!$hide) { |
| 76 | set_up_language($squirrelmail_language, true); |
| 77 | printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address); |
| 78 | echo "$error_number : $error_string<br>\r\n"; |
| 79 | } |
| 80 | exit; |
| 81 | } |
| 82 | |
| 83 | fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n"); |
| 84 | $read = sqimap_read_data ($imap_stream, "a001", false, $response, $message); |
| 85 | |
| 86 | /** If the connection was not successful, lets see why **/ |
| 87 | if ($response != "OK") { |
| 88 | if (!$hide) { |
| 89 | if ($response != "NO") { |
| 90 | // "BAD" and anything else gets reported here. |
| 91 | set_up_language($squirrelmail_language, true); |
| 92 | if ($response == "BAD") |
| 93 | printf (_("Bad request: %s")."<br>\r\n", $message); |
| 94 | else |
| 95 | printf (_("Unknown error: %s") . "<br>\n", $message); |
| 96 | echo "<br>"; |
| 97 | echo _("Read data:") . "<br>\n"; |
| 98 | foreach ($read as $line) |
| 99 | { |
| 100 | echo htmlspecialchars($line) . "<br>\n"; |
| 101 | } |
| 102 | exit; |
| 103 | } else { |
| 104 | // If the user does not log in with the correct |
| 105 | // username and password it is not possible to get the |
| 106 | // correct locale from the user's preferences. |
| 107 | // Therefore, apply the same hack as on the login |
| 108 | // screen. |
| 109 | |
| 110 | // $squirrelmail_language is set by a cookie when |
| 111 | // the user selects language and logs out |
| 112 | |
| 113 | set_up_language($squirrelmail_language, true); |
| 114 | |
| 115 | ?> |
| 116 | <html> |
| 117 | <body bgcolor=ffffff> |
| 118 | <br> |
| 119 | <center> |
| 120 | <table width=70% noborder bgcolor=ffffff align=center> |
| 121 | <tr> |
| 122 | <td bgcolor=dcdcdc> |
| 123 | <font color=cc0000> |
| 124 | <center> |
| 125 | <?php echo _("ERROR") ?> |
| 126 | </center> |
| 127 | </font> |
| 128 | </td> |
| 129 | </tr> |
| 130 | <tr> |
| 131 | <td> |
| 132 | <center> |
| 133 | <?php echo _("Unknown user or password incorrect.") ?><br> |
| 134 | <a href="login.php" target="_top"><?php echo _("Click here to try again") ?></a> |
| 135 | </center> |
| 136 | </td> |
| 137 | </tr> |
| 138 | </table> |
| 139 | </center> |
| 140 | </body> |
| 141 | </html> |
| 142 | <?php |
| 143 | session_destroy(); |
| 144 | exit; |
| 145 | } |
| 146 | } else { |
| 147 | exit; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return $imap_stream; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | /****************************************************************************** |
| 158 | ** Simply logs out the imap session |
| 159 | ******************************************************************************/ |
| 160 | function sqimap_logout ($imap_stream) { |
| 161 | fputs ($imap_stream, "a001 LOGOUT\r\n"); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | |
| 166 | /****************************************************************************** |
| 167 | ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test... |
| 168 | ******************************************************************************/ |
| 169 | function sqimap_get_delimiter ($imap_stream = false) { |
| 170 | fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n"); |
| 171 | $read = sqimap_read_data($imap_stream, ".", true, $a, $b); |
| 172 | $quote_position = strpos ($read[0], "\""); |
| 173 | $delim = substr ($read[0], $quote_position+1, 1); |
| 174 | |
| 175 | return $delim; |
| 176 | |
| 177 | /* According to something that I can't find, this is supposed to work on all systems |
| 178 | |
| 179 | fputs ($imap_stream, "a001 NAMESPACE\r\n"); |
| 180 | $read = sqimap_read_data($imap_stream, "a001", true, $a, $b); |
| 181 | eregi("\"\" \"(.)\"", $read[0], $regs); |
| 182 | return $regs[1]; |
| 183 | */ |
| 184 | } |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | /****************************************************************************** |
| 190 | ** Gets the number of messages in the current mailbox. |
| 191 | ******************************************************************************/ |
| 192 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
| 193 | fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n"); |
| 194 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message); |
| 195 | for ($i = 0; $i < count($read_ary); $i++) { |
| 196 | if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) { |
| 197 | return $regs[1]; |
| 198 | } |
| 199 | } |
| 200 | return "BUG! Couldn't get number of messages in $mailbox!"; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /****************************************************************************** |
| 205 | ** Returns a displayable email address |
| 206 | ******************************************************************************/ |
| 207 | function sqimap_find_email ($string) { |
| 208 | /** Luke Ehresman <lehresma@css.tayloru.edu> |
| 209 | ** <lehresma@css.tayloru.edu> |
| 210 | ** lehresma@css.tayloru.edu |
| 211 | ** |
| 212 | ** What about |
| 213 | ** lehresma@css.tayloru.edu (Luke Ehresman) |
| 214 | **/ |
| 215 | |
| 216 | if (ereg("<([^>]+)>", $string, $regs)) { |
| 217 | $string = $regs[1]; |
| 218 | } |
| 219 | return trim($string); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /****************************************************************************** |
| 224 | ** Takes the From: field, and creates a displayable name. |
| 225 | ** Luke Ehresman <lkehresman@yahoo.com> |
| 226 | ** becomes: Luke Ehresman |
| 227 | ** <lkehresman@yahoo.com> |
| 228 | ** becomes: lkehresman@yahoo.com |
| 229 | ******************************************************************************/ |
| 230 | function sqimap_find_displayable_name ($string) { |
| 231 | $string = " ".trim($string); |
| 232 | $orig_string = $string; |
| 233 | if (strpos($string, "<") && strpos($string, ">")) { |
| 234 | if (strpos($string, "<") == 1) { |
| 235 | $string = sqimap_find_email($string); |
| 236 | } else { |
| 237 | $string = trim($string); |
| 238 | $string = substr($string, 0, strpos($string, "<")); |
| 239 | $string = ereg_replace ("\"", "", $string); |
| 240 | } |
| 241 | |
| 242 | if (trim($string) == "") { |
| 243 | $string = sqimap_find_email($orig_string); |
| 244 | } |
| 245 | } |
| 246 | return $string; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | /****************************************************************************** |
| 251 | ** Returns the number of unseen messages in this folder |
| 252 | ******************************************************************************/ |
| 253 | function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) { |
| 254 | //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n"); |
| 255 | fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n"); |
| 256 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message); |
| 257 | ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs); |
| 258 | return $regs[1]; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /****************************************************************************** |
| 263 | ** Saves a message to a given folder -- used for saving sent messages |
| 264 | ******************************************************************************/ |
| 265 | function sqimap_append ($imap_stream, $sent_folder, $length) { |
| 266 | fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n"); |
| 267 | $tmp = fgets ($imap_stream, 1024); |
| 268 | } |
| 269 | |
| 270 | function sqimap_append_done ($imap_stream) { |
| 271 | fputs ($imap_stream, "\r\n"); |
| 272 | $tmp = fgets ($imap_stream, 1024); |
| 273 | } |
| 274 | ?> |