imap_general.php 9.0 KB

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