mailbox.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?
  2. /**
  3. ** mailbox.php
  4. **
  5. ** This contains functions that request information about a mailbox. Including
  6. ** reading and parsing headers, getting folder information, etc.
  7. **
  8. **/
  9. function selectMailbox($imapConnection, $mailbox, &$numberOfMessages) {
  10. // select mailbox
  11. fputs($imapConnection, "mailboxSelect SELECT \"$mailbox\"\n");
  12. $read = fgets($imapConnection, 1024);
  13. while ((substr($read, 0, 16) != "mailboxSelect OK") && (substr($read, 0, 17) != "mailboxSelect BAD")) {
  14. if (substr(Chop($read), -6) == "EXISTS") {
  15. $array = explode(" ", $read);
  16. $numberOfMessages = $array[1];
  17. }
  18. $read = fgets($imapConnection, 1024);
  19. }
  20. }
  21. /** This function sends a request to the IMAP server for headers, 50 at a time
  22. ** until $end is reached. I originally had it do them all at one time, but found
  23. ** it slightly faster to do it this way.
  24. **
  25. ** Originally we had getMessageHeaders get the headers for one message at a time.
  26. ** Doing it in bunches gave us a speed increase from 9 seconds (for a box of 800
  27. ** messages) to about 3.5 seconds.
  28. **/
  29. function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) {
  30. $rel_start = $start;
  31. if (($start > $end) || ($start < 1)) {
  32. echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
  33. exit;
  34. }
  35. $from_pos = 0;
  36. $date_pos = 0;
  37. $subj_pos = 0;
  38. while ($rel_start <= $end) {
  39. if ($end - $rel_start > 50) {
  40. $rel_end = $rel_start + 50;
  41. } else {
  42. $rel_end = $end;
  43. }
  44. fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n");
  45. $read = fgets($imapConnection, 1024);
  46. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  47. if (substr($read, 0, 5) == "From:") {
  48. $read = ereg_replace("<", "EMAILSTART--", $read);
  49. $read = ereg_replace(">", "--EMAILEND", $read);
  50. $from[$from_pos] = substr($read, 5, strlen($read) - 6);
  51. $from_pos++;
  52. }
  53. else if (substr($read, 0, 5) == "Date:") {
  54. $read = ereg_replace("<", "&lt;", $read);
  55. $read = ereg_replace(">", "&gt;", $read);
  56. $date[$date_pos] = substr($read, 5, strlen($read) - 6);
  57. $date_pos++;
  58. }
  59. else if (substr($read, 0, 8) == "Subject:") {
  60. $read = ereg_replace("<", "&lt;", $read);
  61. $read = ereg_replace(">", "&gt;", $read);
  62. $subject[$subj_pos] = substr($read, 8, strlen($read) - 9);
  63. if (strlen(Chop($subject[$subj_pos])) == 0)
  64. $subject[$subj_pos] = "(no subject)";
  65. $subj_pos++;
  66. }
  67. $read = fgets($imapConnection, 1024);
  68. }
  69. $rel_start = $rel_start + 50;
  70. }
  71. }
  72. function setMessageFlag($imapConnection, $i, $q, $flag) {
  73. fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
  74. }
  75. /** This function gets the flags for message $j. It does only one message at a
  76. ** time, rather than doing groups of messages (like getMessageHeaders does).
  77. ** I found it one or two seconds quicker (on a box of 800 messages) to do it
  78. ** individually. I'm not sure why it happens like that, but that's what my
  79. ** testing found. Perhaps later I will be proven wrong and this will change.
  80. **/
  81. function getMessageFlags($imapConnection, $j, &$flags) {
  82. /** * 2 FETCH (FLAGS (\Answered \Seen)) */
  83. fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
  84. $read = fgets($imapConnection, 1024);
  85. $count = 0;
  86. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  87. if (strpos($read, "FLAGS")) {
  88. $read = ereg_replace("\(", "", $read);
  89. $read = ereg_replace("\)", "", $read);
  90. $read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
  91. $read = trim($read);
  92. $flags = explode(" ", $read);;
  93. $s = 0;
  94. while ($s < count($flags)) {
  95. $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
  96. $s++;
  97. }
  98. } else {
  99. $flags[0] = "None";
  100. }
  101. $count++;
  102. $read = fgets($imapConnection, 1024);
  103. }
  104. }
  105. function getEmailAddr($sender) {
  106. if (strpos($sender, "EMAILSTART--") == false)
  107. return "";
  108. $start = strpos($sender, "EMAILSTART--");
  109. $emailAddr = substr($sender, $start, strlen($sender));
  110. return $emailAddr;
  111. }
  112. function getSender($sender) {
  113. if (strpos($sender, "EMAILSTART--") == false)
  114. return "";
  115. $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
  116. $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
  117. return "$first$second";
  118. }
  119. function getSenderName($sender) {
  120. $name = getSender($sender);
  121. $emailAddr = getEmailAddr($sender);
  122. $emailStart = strpos($emailAddr, "EMAILSTART--");
  123. $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
  124. if (($emailAddr == "") && ($name == "")) {
  125. $from = $sender;
  126. }
  127. else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
  128. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  129. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  130. $from = $emailAddr;
  131. }
  132. else if (strlen($name) > 0) {
  133. $from = $name;
  134. }
  135. else if (strlen($emailAddr > 0)) {
  136. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  137. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  138. $from = $emailAddr;
  139. }
  140. $from = trim($from);
  141. // strip out any quotes if they exist
  142. if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
  143. $from = substr($from, 1, strlen($from) - 2);
  144. return $from;
  145. }
  146. /** returns "true" if the copy was completed successfully.
  147. ** returns "false" with an error message if unsuccessful.
  148. **/
  149. function copyMessages($imapConnection, $from_id, $to_id, $folder) {
  150. fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
  151. $read = fgets($imapConnection, 1024);
  152. while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
  153. $read = fgets($imapConnection, 1024);
  154. }
  155. if (substr($read, 0, 15) == "mailboxStore NO") {
  156. echo "ERROR... $read<BR>";
  157. return false;
  158. } else if (substr($read, 0, 15) == "mailboxStore OK") {
  159. return true;
  160. }
  161. echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
  162. return false;
  163. }
  164. /** expunges a mailbox **/
  165. function expungeBox($imapConnection, $mailbox) {
  166. selectMailbox($imapConnection, $mailbox, $num);
  167. fputs($imapConnection, "1 EXPUNGE\n");
  168. }
  169. function getFolderNameMinusINBOX($mailbox) {
  170. if (substr($mailbox, 0, 6) == "INBOX.")
  171. $box = substr($mailbox, 6, strlen($mailbox));
  172. else
  173. $box = $mailbox;
  174. return $box;
  175. }
  176. function fetchBody($imapConnection, $id) {
  177. fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
  178. $count = 0;
  179. $read[$count] = fgets($imapConnection, 1024);
  180. while ((substr($read[$count], 0, 15) != "messageFetch OK") && (substr($read[$count], 0, 16) != "messageFetch BAD")) {
  181. $count++;
  182. $read[$count] = fgets($imapConnection, 1024);
  183. }
  184. $count = 0;
  185. $useHTML= false;
  186. while ($count < count($read)) {
  187. $read[$count] = "^^$read[$count]";
  188. if (strpos($read[$count], "<html>") == true) {
  189. $useHTML = true;
  190. } else if (strpos(strtolower($read[$count]), "</html") == true) {
  191. $useHTML= false;
  192. }
  193. $read[$count] = substr($read[$count], 2, strlen($read[$count]));
  194. if ($useHTML == false) {
  195. $read[$count] = str_replace(" ", "&nbsp;", $read[$count]);
  196. $read[$count] = str_replace("\n", "", $read[$count]);
  197. $read[$count] = str_replace("\r", "", $read[$count]);
  198. $read[$count] = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;", $read[$count]);
  199. $read[$count] = "^^$read[$count]";
  200. if (strpos(trim(str_replace("&nbsp;", "", $read[$count])), ">>") == 2) {
  201. $read[$count] = substr($read[$count], 2, strlen($read[$count]));
  202. $read[$count] = "<FONT FACE=\"Fixed\" COLOR=FF0000>$read[$count]</FONT>\n";
  203. } else if (strpos(trim(str_replace("&nbsp;", "", $read[$count])), ">") == 2) {
  204. $read[$count] = substr($read[$count], 2, strlen($read[$count]));
  205. $read[$count] = "<FONT FACE=\"Fixed\" COLOR=800000>$read[$count]</FONT>\n";
  206. } else {
  207. $read[$count] = substr($read[$count], 2, strlen($read[$count]));
  208. $read[$count] = "<FONT FACE=\"Fixed\" COLOR=000000>$read[$count]</FONT>\n";
  209. }
  210. if (strpos(strtolower($read[$count]), "http://") != false) {
  211. $start = strpos(strtolower($read[$count]), "http://");
  212. $link = substr($read[$count], $start, strlen($read[$count]));
  213. if (strpos($link, "&nbsp;"))
  214. $end = strpos($link, "&nbsp;");
  215. else if (strpos($link, "<"))
  216. $end = strpos($link, "<");
  217. else
  218. $end = strlen($link);
  219. $link = substr($link, 0, $end);
  220. $read[$count] = str_replace($link, "<A HREF=\"$link\" TARGET=_top>$link</A>", $read[$count]);
  221. }
  222. }
  223. $count++;
  224. }
  225. return $read;
  226. }
  227. ?>