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