imap_general.php 11 KB

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