mailbox.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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"]["TYPE0"], $message["HEADER"]["TYPE1"]);
  211. return $message;
  212. }
  213. function fetchHeader($imapConnection, $id) {
  214. fputs($imapConnection, "messageFetch FETCH $id:$id RFC822.HEADER\n");
  215. $read = fgets($imapConnection, 1024);
  216. /** defaults... if the don't get overwritten, it will display text **/
  217. $header["TYPE0"] = "text";
  218. $header["TYPE1"] = "plain";
  219. $header["ENCODING"] = "us-ascii";
  220. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  221. /** MIME-VERSION **/
  222. if (substr($read, 0, 17) == "MIME-Version: 1.0") {
  223. $header["MIME"] = true;
  224. $read = fgets($imapConnection, 1024);
  225. }
  226. /** ENCODING TYPE **/
  227. else if (substr($read[$i], 0, 26) == "Content-Transfer-Encoding:") {
  228. $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
  229. }
  230. /** CONTENT-TYPE **/
  231. else if (substr($read, 0, 13) == "Content-Type:") {
  232. $cont = strtolower(trim(substr($read, 13)));
  233. if (strpos($cont, ";"))
  234. $cont = substr($cont, 0, strpos($cont, ";"));
  235. $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
  236. $header["TYPE1"] = substr($cont, strpos($cont, "/")+1);
  237. $line = $read;
  238. $read = fgets($imapConnection, 1024);
  239. while ( (substr(substr($read, 0, strpos($read, " ")), -1) != ":") && (trim($read) != "") && (trim($read) != ")")) {
  240. str_replace("\n", "", $line);
  241. str_replace("\n", "", $read);
  242. $line = "$line $read";
  243. $read = fgets($imapConnection, 1024);
  244. }
  245. /** Detect the boundary of a multipart message **/
  246. if (strpos(strtolower(trim($line)), "boundary=")) {
  247. $pos = strpos($line, "boundary=") + 9;
  248. $bound = trim($line);
  249. if (strpos($line, " ", $pos) > 0) {
  250. $bound = substr($bound, $pos, strpos($line, " ", $pos));
  251. } else {
  252. $bound = substr($bound, $pos);
  253. }
  254. $bound = str_replace("\"", "", $bound);
  255. $header["BOUNDARY"] = $bound;
  256. }
  257. /** Detect the charset **/
  258. if (strpos(strtolower(trim($line)), "charset=")) {
  259. $pos = strpos($line, "charset=") + 8;
  260. $charset = trim($line);
  261. if (strpos($line, " ", $pos) > 0) {
  262. $charset = substr($charset, $pos, strpos($line, " ", $pos));
  263. } else {
  264. $charset = substr($charset, $pos);
  265. }
  266. $charset = str_replace("\"", "", $charset);
  267. $header["CHARSET"] = $charset;
  268. } else {
  269. $header["CHARSET"] = "us-ascii";
  270. }
  271. }
  272. /** REPLY-TO **/
  273. else if (substr($read, 0, 9) == "Reply-To:") {
  274. $header["REPLYTO"] = trim(substr($read, 9, strlen($read)));
  275. $read = fgets($imapConnection, 1024);
  276. }
  277. /** FROM **/
  278. else if (substr($read, 0, 5) == "From:") {
  279. $header["FROM"] = trim(substr($read, 5, strlen($read) - 6));
  280. if ($header["REPLYTO"] == "")
  281. $header["REPLYTO"] = $header["FROM"];
  282. $read = fgets($imapConnection, 1024);
  283. }
  284. /** DATE **/
  285. else if (substr($read, 0, 5) == "Date:") {
  286. $d = substr($read, 5, strlen($read) - 6);
  287. $d = trim($d);
  288. $d = ereg_replace(" ", " ", $d);
  289. $d = explode(" ", $d);
  290. $header["DATE"] = getTimeStamp($d);
  291. $read = fgets($imapConnection, 1024);
  292. }
  293. /** SUBJECT **/
  294. else if (substr($read, 0, 8) == "Subject:") {
  295. $header["SUBJECT"] = trim(substr($read, 8, strlen($read) - 9));
  296. if (strlen(Chop($header["SUBJECT"])) == 0)
  297. $header["SUBJECT"] = "(no subject)";
  298. $read = fgets($imapConnection, 1024);
  299. }
  300. /** CC **/
  301. else if (substr($read, 0, 3) == "CC:") {
  302. $pos = 0;
  303. $header["CC"][$pos] = trim(substr($read, 4));
  304. $read = fgets($imapConnection, 1024);
  305. while ((substr($read, 0, 1) == " ") && (trim($read) != "")) {
  306. $pos++;
  307. $header["CC"][$pos] = trim($read);
  308. $read = fgets($imapConnection, 1024);
  309. }
  310. }
  311. /** TO **/
  312. else if (substr($read, 0, 3) == "To:") {
  313. $pos = 0;
  314. $header["TO"][$pos] = trim(substr($read, 4));
  315. $read = fgets($imapConnection, 1024);
  316. while ((substr($read, 0, 1) == " ") && (trim($read) != "")){
  317. $pos++;
  318. $header["TO"][$pos] = trim($read);
  319. $read = fgets($imapConnection, 1024);
  320. }
  321. }
  322. /** ERROR CORRECTION **/
  323. else if (substr($read, 0, 1) == ")") {
  324. if ($header["SUBJECT"] == "")
  325. $header["SUBJECT"] = "(no subject)";
  326. if ($header["FROM"] == "")
  327. $header["FROM"] = "(unknown sender)";
  328. if ($header["DATE"] == "")
  329. $header["DATE"] = time();
  330. $read = fgets($imapConnection, 1024);
  331. }
  332. else {
  333. $read = fgets($imapConnection, 1024);
  334. }
  335. }
  336. return $header;
  337. }
  338. function fetchBody($imapConnection, $bound, $id, $type0, $type1) {
  339. /** This first part reads in the full body of the message **/
  340. fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
  341. $read = fgets($imapConnection, 1024);
  342. $count = 0;
  343. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  344. $body[$count] = $read;
  345. $count++;
  346. $read = fgets($imapConnection, 1024);
  347. }
  348. /** this deletes the first line, and the last two (imap stuff we ignore) **/
  349. $i = 0;
  350. $j = 0;
  351. while ($i < count($body)) {
  352. if ( ($i != 0) && ($i != count($body) - 1) && ($i != count($body)) ) {
  353. $bodytmp[$j] = $body[$i];
  354. $j++;
  355. }
  356. $i++;
  357. }
  358. $body = $bodytmp;
  359. /** Now, lets work out the MIME stuff **/
  360. /** (needs mime.php included) **/
  361. return decodeMime($body, $bound, $type0, $type1);
  362. }
  363. function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset) {
  364. /** defaults... if the don't get overwritten, it will display text **/
  365. $type0 = "text";
  366. $type1 = "plain";
  367. $encoding = "us-ascii";
  368. $i = 0;
  369. while (trim($read[$i]) != "") {
  370. if (substr($read[$i], 0, 26) == "Content-Transfer-Encoding:") {
  371. $encoding = strtolower(trim(substr($read[$i], 26)));
  372. } else if (substr($read[$i], 0, 13) == "Content-Type:") {
  373. $cont = strtolower(trim(substr($read[$i], 13)));
  374. if (strpos($cont, ";"))
  375. $cont = substr($cont, 0, strpos($cont, ";"));
  376. $type0 = substr($cont, 0, strpos($cont, "/"));
  377. $type1 = substr($cont, strpos($cont, "/")+1);
  378. $line = $read[$i];
  379. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  380. str_replace("\n", "", $line);
  381. str_replace("\n", "", $read[$i]);
  382. $line = "$line $read[$i]";
  383. $i++;
  384. }
  385. /** Detect the boundary of a multipart message **/
  386. if (strpos(strtolower(trim($line)), "boundary=")) {
  387. $pos = strpos($line, "boundary=") + 9;
  388. $bound = trim($line);
  389. if (strpos($line, " ", $pos) > 0) {
  390. $bound = substr($bound, $pos, strpos($line, " ", $pos));
  391. } else {
  392. $bound = substr($bound, $pos);
  393. }
  394. $bound = str_replace("\"", "", $bound);
  395. }
  396. /** Detect the charset **/
  397. if (strpos(strtolower(trim($line)), "charset=")) {
  398. $pos = strpos($line, "charset=") + 8;
  399. $charset = trim($line);
  400. if (strpos($line, " ", $pos) > 0) {
  401. $charset = substr($charset, $pos, strpos($line, " ", $pos));
  402. } else {
  403. $charset = substr($charset, $pos);
  404. }
  405. $charset = str_replace("\"", "", $charset);
  406. }
  407. }
  408. $i++;
  409. }
  410. /** remove the header from the entity **/
  411. $i = 0;
  412. while (trim($read[$i]) != "") {
  413. $i++;
  414. }
  415. $i++;
  416. for ($p = 0; $i < count($read); $p++) {
  417. $entity[$p] = $read[$i];
  418. $i++;
  419. }
  420. $read = $entity;
  421. }
  422. function parsePlainTextMessage($line) {
  423. $line = "^^$line";
  424. if ((strpos(strtolower($line), "<!") == false) &&
  425. (strpos(strtolower($line), "<html>") == false) &&
  426. (strpos(strtolower($line), "</html>") == false)) {
  427. $line = str_replace("<", "&lt;", $line);
  428. $line = str_replace(">", "&gt;", $line);
  429. }
  430. $wrap_at = 86; // Make this configurable int the config file some time
  431. if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning
  432. $line = wordWrap($line, $wrap_at);
  433. $line = str_replace(" ", "&nbsp;", $line);
  434. $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
  435. /** if >> or > are found at the beginning of a line, I'll assume that was
  436. replied text, so make it different colors **/
  437. if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
  438. $line = substr($line, 2, strlen($line));
  439. $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
  440. } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
  441. $line = substr($line, 2, strlen($line));
  442. $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
  443. } else {
  444. $line = substr($line, 2, strlen($line));
  445. $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
  446. }
  447. /** This translates "http://" into a link. It could be made better to accept
  448. "www" and "mailto" also. That should probably be added later. **/
  449. if (strpos(strtolower($line), "http://") != false) {
  450. $line = ereg_replace("<BR>", "", $line);
  451. $start = strpos(strtolower($line), "http://");
  452. $link = substr($line, $start, strlen($line));
  453. if (strpos($link, " ")) {
  454. $end = strpos($link, " ")-1;
  455. }
  456. else if (strpos($link, "&nbsp;")) {
  457. $end = strpos($link, "&nbsp;")-1;
  458. }
  459. else if (strpos($link, "<")) {
  460. $end = strpos($link, "<");
  461. }
  462. else if (strpos($link, ">")) {
  463. $end = strpos($link, ">");
  464. }
  465. else if (strpos($link, "(")) {
  466. $end = strpos($link, "(")-1;
  467. }
  468. else if (strpos($link, ")")) {
  469. $end = strpos($link, ")")-1;
  470. }
  471. else if (strpos($link, "{")) {
  472. $end = strpos($link, "{")-1;
  473. }
  474. else if (strpos($link, "}")) {
  475. $end = strpos($link, "}")-1;
  476. }
  477. else
  478. $end = strlen($link);
  479. $link = substr($line, $start, $end);
  480. $end = $end + $start;
  481. $before = substr($line, 0, $start);
  482. $after = substr($line, $end, strlen($line));
  483. $line = "$before<A HREF=\"$link\" TARGET=_top>$link</A>$after<BR>";
  484. }
  485. return $line;
  486. }
  487. ?>