mailbox.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. $unseen = false;
  14. while ((substr($read, 0, 16) != "mailboxSelect OK") && (substr($read, 0, 17) != "mailboxSelect BAD")) {
  15. if (substr(Chop($read), -6) == "EXISTS") {
  16. $array = explode(" ", $read);
  17. $numberOfMessages = $array[1];
  18. }
  19. $read = fgets($imapConnection, 1024);
  20. }
  21. }
  22. function unseenMessages($imapConnection, &$numUnseen) {
  23. fputs($imapConnection, "1 SEARCH UNSEEN NOT DELETED\n");
  24. $read = fgets($imapConnection, 1024);
  25. $unseen = false;
  26. if (strlen($read) > 10) {
  27. $unseen = true;
  28. $ary = explode(" ", $read);
  29. $numUnseen = count($ary) - 2;
  30. }
  31. else {
  32. $unseen = false;
  33. $numUnseen = 0;
  34. }
  35. $read = fgets($imapConnection, 1024);
  36. return $unseen;
  37. }
  38. /** This function sends a request to the IMAP server for headers, 50 at a time
  39. ** until $end is reached. I originally had it do them all at one time, but found
  40. ** it slightly faster to do it this way.
  41. **
  42. ** Originally we had getMessageHeaders get the headers for one message at a time.
  43. ** Doing it in bunches gave us a speed increase from 9 seconds (for a box of 800
  44. ** messages) to about 3.5 seconds.
  45. **/
  46. function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) {
  47. $rel_start = $start;
  48. if (($start > $end) || ($start < 1)) {
  49. echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
  50. exit;
  51. }
  52. $pos = 0;
  53. while ($rel_start <= $end) {
  54. if ($end - $rel_start > 50) {
  55. $rel_end = $rel_start + 49;
  56. } else {
  57. $rel_end = $end;
  58. }
  59. fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date To Cc)\n");
  60. $read = fgets($imapConnection, 1024);
  61. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  62. if (substr($read, 0, 5) == "From:") {
  63. $read = ereg_replace("<", "EMAILSTART--", $read);
  64. $read = ereg_replace(">", "--EMAILEND", $read);
  65. $from[$pos] = substr($read, 5, strlen($read) - 6);
  66. }
  67. else if (substr($read, 0, 5) == "Date:") {
  68. $read = ereg_replace("<", "&lt;", $read);
  69. $read = ereg_replace(">", "&gt;", $read);
  70. $date[$pos] = substr($read, 5, strlen($read) - 6);
  71. }
  72. else if (substr($read, 0, 8) == "Subject:") {
  73. $read = ereg_replace("<", "&lt;", $read);
  74. $read = ereg_replace(">", "&gt;", $read);
  75. $subject[$pos] = substr($read, 8, strlen($read) - 9);
  76. if (strlen(Chop($subject[$pos])) == 0)
  77. $subject[$pos] = "(no subject)";
  78. }
  79. else if (substr($read, 0, 1) == ")") {
  80. if ($subject[$pos] == "")
  81. $subject[$pos] = "(no subject)";
  82. else if ($from[$pos] == "")
  83. $from[$pos] = "(unknown sender)";
  84. else if ($date[$pos] == "")
  85. $from[$pos] = gettimeofday();
  86. $pos++;
  87. }
  88. $read = fgets($imapConnection, 1024);
  89. }
  90. $rel_start = $rel_start + 50;
  91. }
  92. }
  93. function setMessageFlag($imapConnection, $i, $q, $flag) {
  94. fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
  95. }
  96. /** This function gets the flags for message $j. It does only one message at a
  97. ** time, rather than doing groups of messages (like getMessageHeaders does).
  98. ** I found it one or two seconds quicker (on a box of 800 messages) to do it
  99. ** individually. I'm not sure why it happens like that, but that's what my
  100. ** testing found. Perhaps later I will be proven wrong and this will change.
  101. **/
  102. function getMessageFlags($imapConnection, $j, &$flags) {
  103. /** * 2 FETCH (FLAGS (\Answered \Seen)) */
  104. fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
  105. $read = fgets($imapConnection, 1024);
  106. $count = 0;
  107. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  108. if (strpos($read, "FLAGS")) {
  109. $read = ereg_replace("\(", "", $read);
  110. $read = ereg_replace("\)", "", $read);
  111. $read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
  112. $read = trim($read);
  113. $flags = explode(" ", $read);;
  114. $s = 0;
  115. while ($s < count($flags)) {
  116. $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
  117. $s++;
  118. }
  119. } else {
  120. $flags[0] = "None";
  121. }
  122. $count++;
  123. $read = fgets($imapConnection, 1024);
  124. }
  125. }
  126. function decodeEmailAddr($sender) {
  127. $emailAddr = getEmailAddr($sender);
  128. if (strpos($emailAddr, "EMAILSTART--")) {
  129. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  130. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  131. } else {
  132. $emailAddr = $emailAddr;
  133. }
  134. return $emailAddr;
  135. }
  136. function getEmailAddr($sender) {
  137. if (strpos($sender, "EMAILSTART--") == false)
  138. return "$sender";
  139. $emailStart = strpos($sender, "EMAILSTART--") + 12;
  140. $emailAddr = substr($sender, $emailStart, strlen($sender));
  141. $emailAddr = substr($emailAddr, 0, strpos($emailAddr, "--EMAILEND"));
  142. return $emailAddr;
  143. }
  144. function getSender($sender) {
  145. if (strpos($sender, "EMAILSTART--") == false)
  146. return "$sender";
  147. $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
  148. $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
  149. return "$first $second";
  150. }
  151. function getSenderName($sender) {
  152. $name = getSender($sender);
  153. $emailAddr = getEmailAddr($sender);
  154. $emailStart = strpos($emailAddr, "EMAILSTART--");
  155. $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
  156. if (($emailAddr == "") && ($name == "")) {
  157. $from = $sender;
  158. }
  159. else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
  160. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  161. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  162. $from = $emailAddr;
  163. }
  164. else if (strlen($name) > 0) {
  165. $from = $name;
  166. }
  167. else if (strlen($emailAddr > 0)) {
  168. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  169. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  170. $from = $emailAddr;
  171. }
  172. $from = trim($from);
  173. // strip out any quotes if they exist
  174. if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
  175. $from = substr($from, 1, strlen($from) - 2);
  176. return $from;
  177. }
  178. /** returns "true" if the copy was completed successfully.
  179. ** returns "false" with an error message if unsuccessful.
  180. **/
  181. function copyMessages($imapConnection, $from_id, $to_id, $folder) {
  182. fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
  183. $read = fgets($imapConnection, 1024);
  184. while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
  185. $read = fgets($imapConnection, 1024);
  186. }
  187. if (substr($read, 0, 15) == "mailboxStore NO") {
  188. return false;
  189. } else if (substr($read, 0, 15) == "mailboxStore OK") {
  190. return true;
  191. }
  192. echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
  193. return false;
  194. }
  195. /** expunges a mailbox **/
  196. function expungeBox($imapConnection, $mailbox) {
  197. selectMailbox($imapConnection, $mailbox, $num);
  198. fputs($imapConnection, "1 EXPUNGE\n");
  199. }
  200. function getFolderNameMinusINBOX($mailbox) {
  201. if (substr($mailbox, 0, 6) == "INBOX.")
  202. $box = substr($mailbox, 6, strlen($mailbox));
  203. else
  204. $box = $mailbox;
  205. return $box;
  206. }
  207. /** This function gets all the information about a message. Including Header and body **/
  208. function fetchMessage($imapConnection, $id) {
  209. $message["HEADER"] = fetchHeader($imapConnection, $id);
  210. $message["ENTITIES"] = fetchBody($imapConnection, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE"][0], $message["HEADER"]["TYPE"][1]);
  211. return $message;
  212. }
  213. function fetchHeader($imapConnection, $id) {
  214. fputs($imapConnection, "messageFetch FETCH $id:$id RFC822.HEADER.LINES (From Subject Date To Cc Content-Type MIME-Version)\n");
  215. $read = fgets($imapConnection, 1024);
  216. /** defaults... if the don't get overwritten, it will display text **/
  217. $header["TYPE"][0] = "text";
  218. $header["TYPE"][1] = "plain";
  219. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  220. /** MIME-VERSION **/
  221. if (substr($read, 0, 17) == "MIME-Version: 1.0") {
  222. $header["MIME"] = true;
  223. $read = fgets($imapConnection, 1024);
  224. }
  225. /** CONTENT-TYPE **/
  226. else if (substr($read, 0, 13) == "Content-Type:") {
  227. $cont = trim(substr($read, 13));
  228. $cont = substr($cont, 0, strpos($cont, ";"));
  229. $header["TYPE"][0] = substr($cont, 0, strpos($cont, "/"));
  230. $header["TYPE"][1] = substr($cont, strpos($cont, "/")+1);
  231. $read = fgets($imapConnection, 1024);
  232. if (substr(strtolower(trim($read)), 0, 9) == "boundary=") {
  233. $bound = trim($read);
  234. $bound = substr($bound, 9);
  235. $bound = str_replace("\"", "", $bound);
  236. $header["BOUNDARY"] = $bound;
  237. $read = fgets($imapConnection, 1024);
  238. }
  239. }
  240. /** FROM **/
  241. else if (substr($read, 0, 5) == "From:") {
  242. $header["FROM"] = trim(substr($read, 5, strlen($read) - 6));
  243. $read = fgets($imapConnection, 1024);
  244. }
  245. /** DATE **/
  246. else if (substr($read, 0, 5) == "Date:") {
  247. $d = substr($read, 5, strlen($read) - 6);
  248. $d = trim($d);
  249. $d = ereg_replace(" ", " ", $d);
  250. $d = explode(" ", $d);
  251. $header["DATE"] = getTimeStamp($d);
  252. $read = fgets($imapConnection, 1024);
  253. }
  254. /** SUBJECT **/
  255. else if (substr($read, 0, 8) == "Subject:") {
  256. $header["SUBJECT"] = trim(substr($read, 8, strlen($read) - 9));
  257. if (strlen(Chop($header["SUBJECT"])) == 0)
  258. $header["SUBJECT"] = "(no subject)";
  259. $read = fgets($imapConnection, 1024);
  260. }
  261. /** CC **/
  262. else if (substr($read, 0, 3) == "CC:") {
  263. $pos = 0;
  264. $header["CC"][$pos] = trim(substr($read, 4));
  265. $read = fgets($imapConnection, 1024);
  266. while ((substr($read, 0, 1) == " ") && (trim($read) != "")) {
  267. $pos++;
  268. $header["CC"][$pos] = trim($read);
  269. $read = fgets($imapConnection, 1024);
  270. }
  271. }
  272. /** TO **/
  273. else if (substr($read, 0, 3) == "To:") {
  274. $pos = 0;
  275. $header["TO"][$pos] = trim(substr($read, 4));
  276. $read = fgets($imapConnection, 1024);
  277. while ((substr($read, 0, 1) == " ") && (trim($read) != "")){
  278. $pos++;
  279. $header["TO"][$pos] = trim($read);
  280. $read = fgets($imapConnection, 1024);
  281. }
  282. }
  283. /** ERROR CORRECTION **/
  284. else if (substr($read, 0, 1) == ")") {
  285. if ($header["SUBJECT"] == "")
  286. $header["SUBJECT"] = "(no subject)";
  287. if ($header["FROM"] == "")
  288. $header["FROM"] = "(unknown sender)";
  289. if ($header["DATE"] == "")
  290. $header["DATE"] = time();
  291. $read = fgets($imapConnection, 1024);
  292. }
  293. else {
  294. $read = fgets($imapConnection, 1024);
  295. }
  296. }
  297. return $header;
  298. }
  299. function fetchBody($imapConnection, $bound, $id, $type0, $type1) {
  300. /** This first part reads in the full body of the message **/
  301. fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
  302. $read = fgets($imapConnection, 1024);
  303. $count = 0;
  304. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  305. $body[$count] = $read;
  306. $count++;
  307. $read = fgets($imapConnection, 1024);
  308. }
  309. /** this deletes the first line, and the last two (imap stuff we ignore) **/
  310. $i = 0;
  311. $j = 0;
  312. while ($i < count($body)) {
  313. if ( ($i != 0) && ($i != count($body) - 1) && ($i != count($body)) ) {
  314. $bodytmp[$j] = $body[$i];
  315. $j++;
  316. }
  317. $i++;
  318. }
  319. $body = $bodytmp;
  320. /** Now, lets work out the MIME stuff **/
  321. /** (needs mime.php included) **/
  322. return decodeMime($body, $bound, $type0, $type1);
  323. }
  324. function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound) {
  325. /** defaults... if the don't get overwritten, it will display text **/
  326. $type0 = "text";
  327. $type1 = "plain";
  328. $i = 0;
  329. while (trim($read[$i]) != "") {
  330. if (substr($read[$i], 0, 13) == "Content-Type:") {
  331. $cont = trim(substr($read[$i], 13));
  332. $cont = substr($cont, 0, strpos($cont, ";"));
  333. $type0 = substr($cont, 0, strpos($cont, "/"));
  334. $type1 = substr($cont, strpos($cont, "/")+1);
  335. if (substr(strtolower(trim($read[$i])), 0, 9) == "boundary=") {
  336. $bound = trim($read[$i]);
  337. $bound = substr($bound, 9);
  338. $bound = str_replace("\"", "", $bound);
  339. }
  340. }
  341. $i++;
  342. }
  343. /** remove the header from the entity **/
  344. $i = 0;
  345. while (trim($read[$i]) != "") {
  346. $i++;
  347. }
  348. $i++;
  349. for ($p = 0; $i < count($read); $p++) {
  350. $entity[$p] = $read[$i];
  351. $i++;
  352. }
  353. $read = $entity;
  354. }
  355. function parsePlainTextMessage($line) {
  356. $line = "^^$line";
  357. if ((strpos(strtolower($line), "<!") == false) &&
  358. (strpos(strtolower($line), "<html>") == false) &&
  359. (strpos(strtolower($line), "</html>") == false)) {
  360. $line = str_replace("<", "&lt;", $line);
  361. $line = str_replace(">", "&gt;", $line);
  362. }
  363. $wrap_at = 86; // Make this configurable int the config file some time
  364. if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning
  365. $line = wordWrap($line, $wrap_at);
  366. $line = str_replace(" ", "&nbsp;", $line);
  367. $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
  368. /** if >> or > are found at the beginning of a line, I'll assume that was
  369. replied text, so make it different colors **/
  370. if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
  371. $line = substr($line, 2, strlen($line));
  372. $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
  373. } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
  374. $line = substr($line, 2, strlen($line));
  375. $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
  376. } else {
  377. $line = substr($line, 2, strlen($line));
  378. $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
  379. }
  380. /** This translates "http://" into a link. It could be made better to accept
  381. "www" and "mailto" also. That should probably be added later. **/
  382. if (strpos(strtolower($line), "http://") != false) {
  383. $line = ereg_replace("<BR>", "", $line);
  384. $start = strpos(strtolower($line), "http://");
  385. $link = substr($line, $start, strlen($line));
  386. if (strpos($link, " ")) {
  387. $end = strpos($link, " ")-1;
  388. }
  389. else if (strpos($link, "&nbsp;")) {
  390. $end = strpos($link, "&nbsp;")-1;
  391. }
  392. else if (strpos($link, "<")) {
  393. $end = strpos($link, "<");
  394. }
  395. else if (strpos($link, ">")) {
  396. $end = strpos($link, ">");
  397. }
  398. else if (strpos($link, "(")) {
  399. $end = strpos($link, "(")-1;
  400. }
  401. else if (strpos($link, ")")) {
  402. $end = strpos($link, ")")-1;
  403. }
  404. else if (strpos($link, "{")) {
  405. $end = strpos($link, "{")-1;
  406. }
  407. else if (strpos($link, "}")) {
  408. $end = strpos($link, "}")-1;
  409. }
  410. else
  411. $end = strlen($link);
  412. $link = substr($line, $start, $end);
  413. $end = $end + $start;
  414. $before = substr($line, 0, $start);
  415. $after = substr($line, $end, strlen($line));
  416. $line = "$before<A HREF=\"$link\" TARGET=_top>$link</A>$after<BR>";
  417. }
  418. return $line;
  419. }
  420. ?>