imap_general.php 10 KB

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