imap_general.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 {
  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. global $optional_delimiter;
  144. if (!$optional_delimiter) $optional_delimiter = "detect";
  145. if (strtolower($optional_delimiter) == "detect") {
  146. fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
  147. $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
  148. $quote_position = strpos ($read[0], "\"");
  149. $delim = substr ($read[0], $quote_position+1, 1);
  150. return $delim;
  151. } else {
  152. return $optional_delimiter;
  153. }
  154. /* According to something that I can't find, this is supposed to work on all systems
  155. fputs ($imap_stream, "a001 NAMESPACE\r\n");
  156. $read = sqimap_read_data($imap_stream, "a001", true, $a, $b);
  157. eregi("\"\" \"(.)\"", $read[0], $regs);
  158. return $regs[1];
  159. */
  160. }
  161. /******************************************************************************
  162. ** Gets the number of messages in the current mailbox.
  163. ******************************************************************************/
  164. function sqimap_get_num_messages ($imap_stream, $mailbox) {
  165. fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
  166. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
  167. for ($i = 0; $i < count($read_ary); $i++) {
  168. if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
  169. return $regs[1];
  170. }
  171. }
  172. return "BUG! Couldn't get number of messages in $mailbox!";
  173. }
  174. /******************************************************************************
  175. ** Returns a displayable email address
  176. ******************************************************************************/
  177. function sqimap_find_email ($string) {
  178. /** Luke Ehresman <lehresma@css.tayloru.edu>
  179. ** <lehresma@css.tayloru.edu>
  180. ** lehresma@css.tayloru.edu
  181. **
  182. ** What about
  183. ** lehresma@css.tayloru.edu (Luke Ehresman)
  184. **/
  185. if (ereg("<([^>]+)>", $string, $regs)) {
  186. $string = $regs[1];
  187. }
  188. return trim($string);
  189. }
  190. /******************************************************************************
  191. ** Takes the From: field, and creates a displayable name.
  192. ** Luke Ehresman <lkehresman@yahoo.com>
  193. ** becomes: Luke Ehresman
  194. ** <lkehresman@yahoo.com>
  195. ** becomes: lkehresman@yahoo.com
  196. ******************************************************************************/
  197. function sqimap_find_displayable_name ($string) {
  198. $string = " ".trim($string);
  199. $orig_string = $string;
  200. if (strpos($string, "<") && strpos($string, ">")) {
  201. if (strpos($string, "<") == 1) {
  202. $string = sqimap_find_email($string);
  203. } else {
  204. $string = trim($string);
  205. $string = substr($string, 0, strpos($string, "<"));
  206. $string = ereg_replace ("\"", "", $string);
  207. }
  208. if (trim($string) == "") {
  209. $string = sqimap_find_email($orig_string);
  210. }
  211. }
  212. return $string;
  213. }
  214. /******************************************************************************
  215. ** Returns the number of unseen messages in this folder
  216. ******************************************************************************/
  217. function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
  218. //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
  219. fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
  220. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
  221. ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
  222. return $regs[1];
  223. }
  224. /******************************************************************************
  225. ** Saves a message to a given folder -- used for saving sent messages
  226. ******************************************************************************/
  227. function sqimap_append ($imap_stream, $sent_folder, $length) {
  228. fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
  229. $tmp = fgets ($imap_stream, 1024);
  230. }
  231. function sqimap_append_done ($imap_stream) {
  232. fputs ($imap_stream, "\r\n");
  233. $tmp = fgets ($imap_stream, 1024);
  234. }
  235. ?>