imap_general.php 8.9 KB

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