imap_general.php 9.1 KB

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