mailbox.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. while ($rel_start <= $end) {
  36. if ($end - $rel_start > 50) {
  37. $rel_end = $rel_start + 50;
  38. } else {
  39. $rel_end = $end;
  40. }
  41. fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n");
  42. $read = fgets($imapConnection, 1024);
  43. $from_num = $rel_start - 1;
  44. $date_num = $rel_start - 1;
  45. $subj_num = $rel_start - 1;
  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_num] = substr($read, 5, strlen($read) - 6);
  51. $from_num++;
  52. }
  53. else if (substr($read, 0, 5) == "Date:") {
  54. $read = ereg_replace("<", "[", $read);
  55. $read = ereg_replace(">", "]", $read);
  56. $date[$date_num] = substr($read, 5, strlen($read) - 6);
  57. $date_num++;
  58. }
  59. else if (substr($read, 0, 8) == "Subject:") {
  60. $read = ereg_replace("<", "[", $read);
  61. $read = ereg_replace(">", "]", $read);
  62. $subject[$subj_num] = substr($read, 8, strlen($read) - 9);
  63. $subj_num++;
  64. }
  65. $read = fgets($imapConnection, 1024);
  66. }
  67. $rel_start = $rel_start + 50;
  68. }
  69. }
  70. function setMessageFlag($imapConnection, $i, $q, $flag) {
  71. fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
  72. }
  73. /** This function gets the flags for message $j. It does only one message at a
  74. ** time, rather than doing groups of messages (like getMessageHeaders does).
  75. ** I found it one or two seconds quicker (on a box of 800 messages) to do it
  76. ** individually. I'm not sure why it happens like that, but that's what my
  77. ** testing found. Perhaps later I will be proven wrong and this will change.
  78. **/
  79. function getMessageFlags($imapConnection, $j, &$flags) {
  80. /** * 2 FETCH (FLAGS (\Answered \Seen)) */
  81. fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
  82. $read = fgets($imapConnection, 1024);
  83. $count = 0;
  84. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  85. if (strpos($read, "FLAGS")) {
  86. $read = ereg_replace("\(", "", $read);
  87. $read = ereg_replace("\)", "", $read);
  88. $read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
  89. $read = trim($read);
  90. $flags = explode(" ", $read);;
  91. $s = 0;
  92. while ($s < count($flags)) {
  93. $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
  94. $s++;
  95. }
  96. } else {
  97. $flags[0] = "None";
  98. }
  99. $count++;
  100. $read = fgets($imapConnection, 1024);
  101. }
  102. }
  103. function getEmailAddr($sender) {
  104. if (strpos($sender, "EMAILSTART--") == false)
  105. return "";
  106. $start = strpos($sender, "EMAILSTART--");
  107. $emailAddr = substr($sender, $start, strlen($sender));
  108. return $emailAddr;
  109. }
  110. function getSender($sender) {
  111. if (strpos($sender, "EMAILSTART--") == false)
  112. return "";
  113. $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
  114. $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
  115. return "$first$second";
  116. }
  117. function getSenderName($sender) {
  118. $name = getSender($sender);
  119. $emailAddr = getEmailAddr($sender);
  120. $emailStart = strpos($emailAddr, "EMAILSTART--");
  121. $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
  122. if (($emailAddr == "") && ($name == "")) {
  123. $from = $sender;
  124. }
  125. else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
  126. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  127. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  128. $from = $emailAddr;
  129. }
  130. else if (strlen($name) > 0) {
  131. $from = $name;
  132. }
  133. else if (strlen($emailAddr > 0)) {
  134. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  135. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  136. $from = $emailAddr;
  137. }
  138. $from = trim($from);
  139. // strip out any quotes if they exist
  140. if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
  141. $from = substr($from, 1, strlen($from) - 2);
  142. return $from;
  143. }
  144. /** returns "true" if the copy was completed successfully.
  145. ** returns "false" with an error message if unsuccessful.
  146. **/
  147. function copyMessages($imapConnection, $from_id, $to_id, $folder) {
  148. fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
  149. $read = fgets($imapConnection, 1024);
  150. while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
  151. $read = fgets($imapConnection, 1024);
  152. }
  153. if (substr($read, 0, 15) == "mailboxStore NO") {
  154. echo "ERROR... $read<BR>";
  155. return false;
  156. } else if (substr($read, 0, 15) == "mailboxStore OK") {
  157. return true;
  158. }
  159. echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
  160. return false;
  161. }
  162. /** expunges a mailbox **/
  163. function expungeBox($imapConnection, $mailbox) {
  164. selectMailbox($imapConnection, $mailbox, $num);
  165. fputs($imapConnection, "1 EXPUNGE\n");
  166. }
  167. function getFolderNameMinusINBOX($mailbox) {
  168. if (substr($mailbox, 0, 6) == "INBOX.")
  169. $box = substr($mailbox, 6, strlen($mailbox));
  170. else
  171. $box = $mailbox;
  172. return $box;
  173. }
  174. ?>