imap_general.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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;
  62. $imap_stream = fsockopen ($imap_server_address, $imap_port, &$error_number, &$error_string);
  63. $server_info = fgets ($imap_stream, 1024);
  64. /** Do some error correction **/
  65. if (!$imap_stream) {
  66. if (!$hide) {
  67. echo "Error connecting to IMAP server: $imap_server_address.<br>\r\n";
  68. echo "$error_number : $error_string<br>\r\n";
  69. }
  70. exit;
  71. }
  72. fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\r\n");
  73. $read = fgets ($imap_stream, 1024);
  74. /** If the connection was not successful, lets see why **/
  75. if (substr($read, 0, 7) != "a001 OK") {
  76. if (!$hide) {
  77. if (substr($read, 0, 8) == "a001 BAD") {
  78. echo "Bad request: $read<br>\r\n";
  79. exit;
  80. } else if (substr($read, 0, 7) == "a001 NO") {
  81. ?>
  82. <html>
  83. <body bgcolor=ffffff>
  84. <br>
  85. <center>
  86. <table width=70% noborder bgcolor=ffffff align=center>
  87. <tr>
  88. <td bgcolor=dcdcdc>
  89. <font color=cc0000>
  90. <center>
  91. <?php echo _("ERROR") ?>
  92. </center>
  93. </font>
  94. </td>
  95. </tr>
  96. <tr>
  97. <td>
  98. <center>
  99. <?php echo _("Unknown user or password incorrect.") ?><br>
  100. <a href="login.php"><?php echo _("Click here to try again") ?></a>
  101. </center>
  102. </td>
  103. </tr>
  104. </table>
  105. </center>
  106. </body>
  107. </html>
  108. <?php
  109. session_destroy();
  110. exit;
  111. } else {
  112. echo "Unknown error: $read<br>";
  113. exit;
  114. }
  115. } else {
  116. exit;
  117. }
  118. }
  119. return $imap_stream;
  120. }
  121. /******************************************************************************
  122. ** Simply logs out the imap session
  123. ******************************************************************************/
  124. function sqimap_logout ($imap_stream) {
  125. fputs ($imap_stream, "a001 LOGOUT\r\n");
  126. }
  127. /******************************************************************************
  128. ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
  129. ******************************************************************************/
  130. function sqimap_get_delimiter ($imap_stream) {
  131. fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
  132. $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
  133. $quote_position = strpos ($read[0], "\"");
  134. $delim = substr ($read[0], $quote_position+1, 1);
  135. return $delim;
  136. }
  137. /******************************************************************************
  138. ** Gets the number of messages in the current mailbox.
  139. ******************************************************************************/
  140. function sqimap_get_num_messages ($imap_stream, $mailbox) {
  141. fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n");
  142. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
  143. for ($i = 0; $i < count($read_ary); $i++) {
  144. if (substr(trim($read_ary[$i]), -6) == EXISTS) {
  145. $array = explode (" ", $read_ary[$i]);
  146. $num = $array[1];
  147. }
  148. }
  149. return $num;
  150. }
  151. /******************************************************************************
  152. ** Returns a displayable email address
  153. ******************************************************************************/
  154. function sqimap_find_email ($string) {
  155. /** Luke Ehresman <lehresma@css.tayloru.edu>
  156. ** <lehresma@css.tayloru.edu>
  157. ** lehresma@css.tayloru.edu
  158. **/
  159. if (strpos($string, "<") && strpos($string, ">")) {
  160. $string = substr($string, strpos($string, "<")+1);
  161. $string = substr($string, 0, strpos($string, ">"));
  162. }
  163. return trim($string);
  164. }
  165. /******************************************************************************
  166. ** Takes the From: field, and creates a displayable name.
  167. ** Luke Ehresman <lkehresman@yahoo.com>
  168. ** becomes: Luke Ehresman
  169. ** <lkehresman@yahoo.com>
  170. ** becomes: lkehresman@yahoo.com
  171. ******************************************************************************/
  172. function sqimap_find_displayable_name ($string) {
  173. $string = " ".trim($string);
  174. if (strpos($string, "<") && strpos($string, ">")) {
  175. if (strpos($string, "<") == 1) {
  176. $string = sqimap_find_email($string);
  177. } else {
  178. $string = trim($string);
  179. $string = substr($string, 0, strpos($string, "<"));
  180. $string = ereg_replace ("\"", "", $string);
  181. }
  182. }
  183. return $string;
  184. }
  185. /******************************************************************************
  186. ** Returns the number of unseen messages in this folder
  187. ******************************************************************************/
  188. function sqimap_unseen_messages ($imap_stream, &$num_unseen, $mailbox) {
  189. //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n");
  190. fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n");
  191. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
  192. $unseen = false;
  193. $read_ary[0] = trim($read_ary[0]);
  194. return substr($read_ary[0], strrpos($read_ary[0], " ")+1, (strlen($read_ary[0]) - strrpos($read_ary[0], " ") - 2));
  195. }
  196. /******************************************************************************
  197. ** Saves a message to a given folder -- used for saving sent messages
  198. ******************************************************************************/
  199. function sqimap_append ($imap_stream, $sent_folder, $length) {
  200. fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
  201. $tmp = fgets ($imap_stream, 1024);
  202. }
  203. function sqimap_append_done ($imap_stream) {
  204. fputs ($imap_stream, "\r\n");
  205. $tmp = fgets ($imap_stream, 1024);
  206. }
  207. ?>