imap_general.php 11 KB

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