imap_general.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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, $hide) {
  59. global $color;
  60. $imap_stream = fsockopen ($imap_server_address, 143, &$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>\n";
  66. echo "$error_number : $error_string<br>\n";
  67. }
  68. exit;
  69. }
  70. fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\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>\n";
  77. exit;
  78. } else if (substr($read, 0, 7) == "a001 NO") {
  79. ?>
  80. <html>
  81. <body bgcolor=<? echo $color[4] ?>>
  82. <br>
  83. <table width=70% noborder bgcolor=<? echo $color[4] ?> align=center>
  84. <tr>
  85. <td bgcolor=<? echo $color[0] ?>>
  86. <font color=<? echo $color[2] ?>>
  87. <center>
  88. <? echo _("ERROR") ?>
  89. </center>
  90. </font>
  91. </td>
  92. </tr>
  93. <tr>
  94. <td>
  95. <font color=<? echo $color[2] ?>>
  96. <center>
  97. <? echo _("Unknown user or password incorrect.") ?><br>
  98. <a href="login.php"><? echo _("Click here to try again") ?></a>
  99. </center>
  100. </font>
  101. </td>
  102. </tr>
  103. </table>
  104. </body>
  105. </html>
  106. <?
  107. exit;
  108. } else {
  109. echo "Unknown error: $read<br>";
  110. exit;
  111. }
  112. } else {
  113. exit;
  114. }
  115. }
  116. return $imap_stream;
  117. }
  118. /******************************************************************************
  119. ** Simply logs out the imap session
  120. ******************************************************************************/
  121. function sqimap_logout ($imap_stream) {
  122. fputs ($imap_stream, "a001 LOGOUT\n");
  123. }
  124. /******************************************************************************
  125. ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
  126. ******************************************************************************/
  127. function sqimap_get_delimiter ($imap_stream) {
  128. fputs ($imap_stream, ". LIST \"\" *\n");
  129. $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
  130. $quote_position = strpos ($read[0], "\"");
  131. $delim = substr ($read[0], $quote_position+1, 1);
  132. return $delim;
  133. }
  134. /******************************************************************************
  135. ** Gets the number of messages in the current mailbox.
  136. ******************************************************************************/
  137. function sqimap_get_num_messages ($imap_stream, $mailbox) {
  138. fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\n");
  139. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
  140. for ($i = 0; $i < count($read_ary); $i++) {
  141. if (substr(trim($read_ary[$i]), -6) == EXISTS) {
  142. $array = explode (" ", $read_ary[$i]);
  143. $num = $array[1];
  144. }
  145. }
  146. return $num;
  147. }
  148. /******************************************************************************
  149. ** Returns a displayable email address
  150. ******************************************************************************/
  151. function sqimap_find_email ($string) {
  152. /** Luke Ehresman <lehresma@css.tayloru.edu>
  153. ** <lehresma@css.tayloru.edu>
  154. ** lehresma@css.tayloru.edu
  155. **/
  156. if (strpos($string, "<") && strpos($string, ">")) {
  157. $string = substr($string, strpos($string, "<")+1);
  158. $string = substr($string, 0, strpos($string, ">"));
  159. }
  160. return $string;
  161. }
  162. /******************************************************************************
  163. ** Takes the From: field, and creates a displayable name.
  164. ** Luke Ehresman <lkehresman@yahoo.com>
  165. ** becomes: Luke Ehresman
  166. ** <lkehresman@yahoo.com>
  167. ** becomes: lkehresman@yahoo.com
  168. ******************************************************************************/
  169. function sqimap_find_displayable_name ($string) {
  170. if (strpos($string, "<") && strpos($string, ">")) {
  171. if (strpos($string, "<") == 0) {
  172. $string = sqimap_find_email($string);
  173. } else {
  174. $string = substr($string, 0, strpos($string, "<"));
  175. }
  176. }
  177. return $string;
  178. }
  179. /******************************************************************************
  180. ** Returns the number of unseen messages in this folder
  181. ******************************************************************************/
  182. function sqimap_unseen_messages ($imap_stream, &$num_unseen) {
  183. fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\n");
  184. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
  185. $unseen = false;
  186. if (strlen($read_ary[0]) > 10) {
  187. $unseen = true;
  188. $ary = explode (" ", $read_ary[0]);
  189. $num_unseen = count($ary) - 2;
  190. } else {
  191. $unseen = false;
  192. $num_unseen = 0;
  193. }
  194. return $unseen;
  195. }
  196. ?>