imap_general.php 11 KB

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