mailbox.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) {
  22. $rel_start = $start;
  23. if (($start > $end) || ($start < 1)) {
  24. echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
  25. exit;
  26. }
  27. while ($rel_start <= $end) {
  28. if ($end - $rel_start > 50) {
  29. $rel_end = $rel_start + 50;
  30. } else {
  31. $rel_end = $end;
  32. }
  33. fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n");
  34. $read = fgets($imapConnection, 1024);
  35. $from_num = $rel_start - 1;
  36. $date_num = $rel_start - 1;
  37. $subj_num = $rel_start - 1;
  38. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  39. if (substr($read, 0, 5) == "From:") {
  40. $read = ereg_replace("<", "EMAILSTART--", $read);
  41. $read = ereg_replace(">", "--EMAILEND", $read);
  42. $from[$from_num] = substr($read, 5, strlen($read) - 6);
  43. $from_num++;
  44. }
  45. else if (substr($read, 0, 5) == "Date:") {
  46. $read = ereg_replace("<", "[", $read);
  47. $read = ereg_replace(">", "]", $read);
  48. $date[$date_num] = substr($read, 5, strlen($read) - 6);
  49. $date_num++;
  50. }
  51. else if (substr($read, 0, 8) == "Subject:") {
  52. $read = ereg_replace("<", "[", $read);
  53. $read = ereg_replace(">", "]", $read);
  54. $subject[$subj_num] = substr($read, 8, strlen($read) - 9);
  55. $subj_num++;
  56. }
  57. $read = fgets($imapConnection, 1024);
  58. }
  59. $rel_start = $rel_start + 50;
  60. }
  61. }
  62. function setMessageFlag($imapConnection, $i, $q, $flag) {
  63. fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
  64. }
  65. function getMessageFlags($imapConnection, $j, &$flags) {
  66. /** * 2 FETCH (FLAGS (\Answered \Seen)) */
  67. fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
  68. $read = fgets($imapConnection, 1024);
  69. $count = 0;
  70. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  71. if (strpos($read, "FLAGS")) {
  72. $read = ereg_replace("\(", "", $read);
  73. $read = ereg_replace("\)", "", $read);
  74. $read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
  75. $read = trim($read);
  76. $flags = explode(" ", $read);;
  77. $s = 0;
  78. while ($s < count($flags)) {
  79. $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
  80. $s++;
  81. }
  82. } else {
  83. $flags[0] = "None";
  84. }
  85. $count++;
  86. $read = fgets($imapConnection, 1024);
  87. }
  88. }
  89. function getEmailAddr($sender) {
  90. if (strpos($sender, "EMAILSTART--") == false)
  91. return "";
  92. $start = strpos($sender, "EMAILSTART--");
  93. $emailAddr = substr($sender, $start, strlen($sender));
  94. return $emailAddr;
  95. }
  96. function getSender($sender) {
  97. if (strpos($sender, "EMAILSTART--") == false)
  98. return "";
  99. $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
  100. $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
  101. return "$first$second";
  102. }
  103. function getSenderName($sender) {
  104. $name = getSender($sender);
  105. $emailAddr = getEmailAddr($sender);
  106. $emailStart = strpos($emailAddr, "EMAILSTART--");
  107. $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
  108. if (($emailAddr == "") && ($name == "")) {
  109. $from = $sender;
  110. }
  111. else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
  112. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  113. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  114. $from = $emailAddr;
  115. }
  116. else if (strlen($name) > 0) {
  117. $from = $name;
  118. }
  119. else if (strlen($emailAddr > 0)) {
  120. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  121. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  122. $from = $emailAddr;
  123. }
  124. $from = trim($from);
  125. // strip out any quotes if they exist
  126. if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
  127. $from = substr($from, 1, strlen($from) - 2);
  128. return $from;
  129. }
  130. /** returns "true" if the copy was completed successfully.
  131. ** returns "false" with an error message if unsuccessful.
  132. **/
  133. function copyMessages($imapConnection, $from_id, $to_id, $folder) {
  134. fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
  135. $read = fgets($imapConnection, 1024);
  136. while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
  137. $read = fgets($imapConnection, 1024);
  138. }
  139. if (substr($read, 0, 15) == "mailboxStore NO") {
  140. echo "ERROR... $read<BR>";
  141. return false;
  142. } else if (substr($read, 0, 15) == "mailboxStore OK") {
  143. return true;
  144. }
  145. echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
  146. return false;
  147. }
  148. /** expunges a mailbox **/
  149. function expungeBox($imapConnection, $mailbox) {
  150. selectMailbox($imapConnection, $mailbox, $num);
  151. fputs($imapConnection, "1 EXPUNGE\n");
  152. }
  153. function getFolderNameMinusINBOX($mailbox) {
  154. if (substr($mailbox, 0, 6) == "INBOX.")
  155. $box = substr($mailbox, 6, strlen($mailbox));
  156. else
  157. $box = $mailbox;
  158. return $box;
  159. }
  160. ?>