mailbox.php 20 KB

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