mailbox.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. $pos = 0;
  36. while ($rel_start <= $end) {
  37. if ($end - $rel_start > 50) {
  38. $rel_end = $rel_start + 49;
  39. } else {
  40. $rel_end = $end;
  41. }
  42. fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n");
  43. $read = fgets($imapConnection, 1024);
  44. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  45. if (substr($read, 0, 5) == "From:") {
  46. $read = ereg_replace("<", "EMAILSTART--", $read);
  47. $read = ereg_replace(">", "--EMAILEND", $read);
  48. $from[$pos] = substr($read, 5, strlen($read) - 6);
  49. }
  50. else if (substr($read, 0, 5) == "Date:") {
  51. $read = ereg_replace("<", "&lt;", $read);
  52. $read = ereg_replace(">", "&gt;", $read);
  53. $date[$pos] = substr($read, 5, strlen($read) - 6);
  54. }
  55. else if (substr($read, 0, 8) == "Subject:") {
  56. $read = ereg_replace("<", "&lt;", $read);
  57. $read = ereg_replace(">", "&gt;", $read);
  58. $subject[$pos] = substr($read, 8, strlen($read) - 9);
  59. if (strlen(Chop($subject[$pos])) == 0)
  60. $subject[$pos] = "(no subject)";
  61. }
  62. else if (substr($read, 0, 1) == ")") {
  63. if ($subject[$pos] == "")
  64. $subject[$pos] = "(no subject)";
  65. else if ($from[$pos] == "")
  66. $from[$pos] = "(unknown sender)";
  67. else if ($date[$pos] == "")
  68. $from[$pos] = gettimeofday();
  69. $pos++;
  70. }
  71. $read = fgets($imapConnection, 1024);
  72. }
  73. $rel_start = $rel_start + 50;
  74. }
  75. }
  76. function setMessageFlag($imapConnection, $i, $q, $flag) {
  77. fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
  78. }
  79. /** This function gets the flags for message $j. It does only one message at a
  80. ** time, rather than doing groups of messages (like getMessageHeaders does).
  81. ** I found it one or two seconds quicker (on a box of 800 messages) to do it
  82. ** individually. I'm not sure why it happens like that, but that's what my
  83. ** testing found. Perhaps later I will be proven wrong and this will change.
  84. **/
  85. function getMessageFlags($imapConnection, $j, &$flags) {
  86. /** * 2 FETCH (FLAGS (\Answered \Seen)) */
  87. fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
  88. $read = fgets($imapConnection, 1024);
  89. $count = 0;
  90. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  91. if (strpos($read, "FLAGS")) {
  92. $read = ereg_replace("\(", "", $read);
  93. $read = ereg_replace("\)", "", $read);
  94. $read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
  95. $read = trim($read);
  96. $flags = explode(" ", $read);;
  97. $s = 0;
  98. while ($s < count($flags)) {
  99. $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
  100. $s++;
  101. }
  102. } else {
  103. $flags[0] = "None";
  104. }
  105. $count++;
  106. $read = fgets($imapConnection, 1024);
  107. }
  108. }
  109. function decodeEmailAddr($sender) {
  110. $emailAddr = getEmailAddr($sender);
  111. $emailStart = strpos($emailAddr, "EMAILSTART--");
  112. $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
  113. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  114. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  115. return $emailAddr;
  116. }
  117. function getEmailAddr($sender) {
  118. if (strpos($sender, "EMAILSTART--") == false)
  119. return "";
  120. $start = strpos($sender, "EMAILSTART--");
  121. $emailAddr = substr($sender, $start, strlen($sender));
  122. return $emailAddr;
  123. }
  124. function getSender($sender) {
  125. if (strpos($sender, "EMAILSTART--") == false)
  126. return "";
  127. $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
  128. $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
  129. return "$first$second";
  130. }
  131. function getSenderName($sender) {
  132. $name = getSender($sender);
  133. $emailAddr = getEmailAddr($sender);
  134. $emailStart = strpos($emailAddr, "EMAILSTART--");
  135. $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
  136. if (($emailAddr == "") && ($name == "")) {
  137. $from = $sender;
  138. }
  139. else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
  140. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  141. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  142. $from = $emailAddr;
  143. }
  144. else if (strlen($name) > 0) {
  145. $from = $name;
  146. }
  147. else if (strlen($emailAddr > 0)) {
  148. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  149. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  150. $from = $emailAddr;
  151. }
  152. $from = trim($from);
  153. // strip out any quotes if they exist
  154. if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
  155. $from = substr($from, 1, strlen($from) - 2);
  156. return $from;
  157. }
  158. /** returns "true" if the copy was completed successfully.
  159. ** returns "false" with an error message if unsuccessful.
  160. **/
  161. function copyMessages($imapConnection, $from_id, $to_id, $folder) {
  162. fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
  163. $read = fgets($imapConnection, 1024);
  164. while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
  165. $read = fgets($imapConnection, 1024);
  166. }
  167. if (substr($read, 0, 15) == "mailboxStore NO") {
  168. echo "ERROR... $read<BR>";
  169. return false;
  170. } else if (substr($read, 0, 15) == "mailboxStore OK") {
  171. return true;
  172. }
  173. echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
  174. return false;
  175. }
  176. /** expunges a mailbox **/
  177. function expungeBox($imapConnection, $mailbox) {
  178. selectMailbox($imapConnection, $mailbox, $num);
  179. fputs($imapConnection, "1 EXPUNGE\n");
  180. }
  181. function getFolderNameMinusINBOX($mailbox) {
  182. if (substr($mailbox, 0, 6) == "INBOX.")
  183. $box = substr($mailbox, 6, strlen($mailbox));
  184. else
  185. $box = $mailbox;
  186. return $box;
  187. }
  188. /** This function will fetch the body of a given message and format
  189. it into our standard format. **/
  190. function fetchBody($imapConnection, $id) {
  191. fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
  192. $count = 0;
  193. $read[$count] = fgets($imapConnection, 1024);
  194. while ((substr($read[$count], 0, 15) != "messageFetch OK") && (substr($read[$count], 0, 16) != "messageFetch BAD")) {
  195. $count++;
  196. $read[$count] = fgets($imapConnection, 1024);
  197. }
  198. /** this loop removes the first line, and the last two which
  199. are IMAP information that we don't need. **/
  200. $i = 0;
  201. $j = 0;
  202. while ($i < count($read)) {
  203. if (($i != 0) && ($i != count($read) - 1) && ($i != count($read) - 2)){
  204. $readtmp[$j] = $read[$i];
  205. $j++;
  206. }
  207. $i++;
  208. }
  209. $read = $readtmp;
  210. /** This loop formats the text, creating links out of linkable stuff too **/
  211. $count = 0;
  212. $useHTML= false;
  213. while ($count < count($read)) {
  214. $read[$count] = "^^$read[$count]";
  215. if (strpos(strtolower($read[$count]), "<html>") == true) {
  216. $useHTML = true;
  217. } else if (strpos(strtolower($read[$count]), "</html>") == true) {
  218. $useHTML = false;
  219. }
  220. $read[$count] = substr($read[$count], 2, strlen($read[$count]));
  221. if ($useHTML == false) {
  222. $read[$count] = parsePlainBodyText($read[$count]);
  223. } else {
  224. $read[$count] = parseHTMLBodyText($read[$count]);
  225. }
  226. $count++;
  227. }
  228. return $read;
  229. }
  230. function parseHTMLBodyText($line) {
  231. return $line;
  232. }
  233. function parsePlainBodyText($line) {
  234. $line = "^^$line";
  235. if ((strpos(strtolower($line), "<!") == false) &&
  236. (strpos(strtolower($line), "<html>") == false) &&
  237. (strpos(strtolower($line), "</html>") == false)) {
  238. $line = str_replace("<", "&lt;", $line);
  239. $line = str_replace(">", "&gt;", $line);
  240. }
  241. $wrap_at = 80; // Make this configurable int the config file some time
  242. if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning
  243. $line = wordWrap($line, $wrap_at);
  244. $line = str_replace(" ", "&nbsp;", $line);
  245. $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
  246. /** if >> or > are found at the beginning of a line, I'll assume that was
  247. replied text, so make it different colors **/
  248. if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
  249. $line = substr($line, 2, strlen($line));
  250. $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
  251. } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
  252. $line = substr($line, 2, strlen($line));
  253. $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
  254. } else {
  255. $line = substr($line, 2, strlen($line));
  256. $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
  257. }
  258. /** This translates "http://" into a link. It could be made better to accept
  259. "www" and "mailto" also. That should probably be added later. **/
  260. if (strpos(strtolower($line), "http://") != false) {
  261. $start = strpos(strtolower($line), "http://");
  262. $text = substr($line, $start, strlen($line));
  263. $link = ereg_replace("<BR>", "", $text);
  264. if (strpos($link, "&"))
  265. $end = strpos($link, "&");
  266. else if (strpos($link, "<"))
  267. $end = strpos($link, "<");
  268. else
  269. $end = strlen($link);
  270. $link = substr($link, 0, $end);
  271. $line = str_replace($text, "<A HREF=\"$link\" TARGET=_top>$text</A>", $line);
  272. }
  273. return $line;
  274. }
  275. ?>