mailbox.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. echo ">>> $read<BR>";
  165. while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
  166. $read = fgets($imapConnection, 1024);
  167. echo ">>> $read<BR>";
  168. }
  169. if (substr($read, 0, 15) == "mailboxStore NO") {
  170. return false;
  171. } else if (substr($read, 0, 15) == "mailboxStore OK") {
  172. return true;
  173. }
  174. echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
  175. return false;
  176. }
  177. /** expunges a mailbox **/
  178. function expungeBox($imapConnection, $mailbox) {
  179. selectMailbox($imapConnection, $mailbox, $num);
  180. fputs($imapConnection, "1 EXPUNGE\n");
  181. }
  182. function getFolderNameMinusINBOX($mailbox) {
  183. if (substr($mailbox, 0, 6) == "INBOX.")
  184. $box = substr($mailbox, 6, strlen($mailbox));
  185. else
  186. $box = $mailbox;
  187. return $box;
  188. }
  189. /** This function will fetch the body of a given message and format
  190. it into our standard format. **/
  191. function fetchBody($imapConnection, $id) {
  192. fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
  193. $count = 0;
  194. $read[$count] = fgets($imapConnection, 1024);
  195. while ((substr($read[$count], 0, 15) != "messageFetch OK") && (substr($read[$count], 0, 16) != "messageFetch BAD")) {
  196. $count++;
  197. $read[$count] = fgets($imapConnection, 1024);
  198. }
  199. /** this loop removes the first line, and the last two which
  200. are IMAP information that we don't need. **/
  201. $i = 0;
  202. $j = 0;
  203. while ($i < count($read)) {
  204. if (($i != 0) && ($i != count($read) - 1) && ($i != count($read) - 2)){
  205. $readtmp[$j] = $read[$i];
  206. $j++;
  207. }
  208. $i++;
  209. }
  210. $read = $readtmp;
  211. /** This loop formats the text, creating links out of linkable stuff too **/
  212. $count = 0;
  213. $useHTML= false;
  214. while ($count < count($read)) {
  215. $read[$count] = "^^$read[$count]";
  216. if (strpos(strtolower($read[$count]), "<html>") == true) {
  217. $useHTML = true;
  218. } else if (strpos(strtolower($read[$count]), "</html>") == true) {
  219. $useHTML = false;
  220. }
  221. $read[$count] = substr($read[$count], 2, strlen($read[$count]));
  222. if ($useHTML == false) {
  223. $read[$count] = parsePlainBodyText($read[$count]);
  224. } else {
  225. $read[$count] = parseHTMLBodyText($read[$count]);
  226. }
  227. $count++;
  228. }
  229. return $read;
  230. }
  231. function parseHTMLBodyText($line) {
  232. return $line;
  233. }
  234. function parsePlainBodyText($line) {
  235. $line = "^^$line";
  236. if ((strpos(strtolower($line), "<!") == false) &&
  237. (strpos(strtolower($line), "<html>") == false) &&
  238. (strpos(strtolower($line), "</html>") == false)) {
  239. $line = str_replace("<", "&lt;", $line);
  240. $line = str_replace(">", "&gt;", $line);
  241. }
  242. $wrap_at = 80; // Make this configurable int the config file some time
  243. if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning
  244. $line = wordWrap($line, $wrap_at);
  245. $line = str_replace(" ", "&nbsp;", $line);
  246. $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
  247. $line = str_replace("\n", "", $line);
  248. $line = str_replace("\r", "", $line);
  249. /** if >> or > are found at the beginning of a line, I'll assume that was
  250. replied text, so make it different colors **/
  251. if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
  252. $line = substr($line, 2, strlen($line));
  253. $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
  254. } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
  255. $line = substr($line, 2, strlen($line));
  256. $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
  257. } else {
  258. $line = substr($line, 2, strlen($line));
  259. $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
  260. }
  261. /** This translates "http://" into a link. It could be made better to accept
  262. "www" and "mailto" also. That should probably be added later. **/
  263. if (strpos(strtolower($line), "http://") != false) {
  264. $line = ereg_replace("<BR>", "", $line);
  265. $start = strpos(strtolower($line), "http://");
  266. $link = substr($line, $start, strlen($line));
  267. if (strpos($link, " ")) {
  268. $end = strpos($link, " ")-1;
  269. }
  270. else if (strpos($link, "&nbsp;")) {
  271. $end = strpos($link, "&nbsp;")-1;
  272. }
  273. else if (strpos($link, "<")) {
  274. $end = strpos($link, "<");
  275. }
  276. else if (strpos($link, ">")) {
  277. $end = strpos($link, ">");
  278. }
  279. else if (strpos($link, "(")) {
  280. $end = strpos($link, "(")-1;
  281. }
  282. else if (strpos($link, ")")) {
  283. $end = strpos($link, ")")-1;
  284. }
  285. else if (strpos($link, "{")) {
  286. $end = strpos($link, "{")-1;
  287. }
  288. else if (strpos($link, "}")) {
  289. $end = strpos($link, "}")-1;
  290. }
  291. else
  292. $end = strlen($link);
  293. $link = substr($line, $start, $end);
  294. $end = $end + $start;
  295. $before = substr($line, 0, $start);
  296. $after = substr($line, $end, strlen($line));
  297. $line = "$before<A HREF=\"$link\" TARGET=_top>$link</A>$after<BR>";
  298. }
  299. return $line;
  300. }
  301. /*
  302. $start = strpos(strtolower($line), "http://");
  303. $text = substr($line, $start, strlen($line));
  304. $linktext = substr($link, 0, $end);
  305. $link = trim(ereg_replace("<BR>", "", $linktext));
  306. // $line = str_replace($text, "<A HREF=\"$link\" TARGET=_top>$link</A>", $line);
  307. */
  308. function getMessageHeadersTo($imapConnection, $start, $end, &$to) {
  309. $rel_start = $start;
  310. if (($start > $end) || ($start < 1)) {
  311. echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
  312. exit;
  313. }
  314. $pos = 0;
  315. while ($rel_start <= $end) {
  316. if ($end - $rel_start > 50) {
  317. $rel_end = $rel_start + 49;
  318. } else {
  319. $rel_end = $end;
  320. }
  321. fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (To)\n");
  322. $read = fgets($imapConnection, 1024);
  323. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  324. if (substr($read, 0, 3) == "To:") {
  325. $read = ereg_replace("<", "&lt;", $read);
  326. $read = ereg_replace(">", "&gt;", $read);
  327. $to[$pos] = substr($read, 3, strlen($read));
  328. if (strlen(Chop($to[$pos])) == 0)
  329. $to[$pos] = "Unknown Recipients";
  330. }
  331. else if (substr($read, 0, 1) == ")") {
  332. if ($subject[$pos] == "")
  333. $subject[$pos] = "Unknown Recipients";
  334. $pos++;
  335. }
  336. $read = fgets($imapConnection, 1024);
  337. }
  338. $rel_start = $rel_start + 50;
  339. }
  340. }
  341. ?>