mailbox.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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)\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. imapReadData($imapConnection, "1", true, $response, $message);
  202. }
  203. function getFolderNameMinusINBOX($mailbox, $del) {
  204. $inbox = "INBOX" . $del;
  205. if (substr($mailbox, 0, strlen($inbox)) == $inbox)
  206. $box = substr($mailbox, strlen($inbox), strlen($mailbox));
  207. else
  208. $box = $mailbox;
  209. return $box;
  210. }
  211. /** This function gets all the information about a message. Including Header and body **/
  212. function fetchMessage($imapConnection, $id, $mailbox) {
  213. $message["INFO"]["ID"] = $id;
  214. $message["INFO"]["MAILBOX"] = $mailbox;
  215. $message["HEADER"] = fetchHeader($imapConnection, $id);
  216. $message["ENTITIES"] = fetchBody($imapConnection, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"]);
  217. return $message;
  218. }
  219. function fetchHeader($imapConnection, $id) {
  220. fputs($imapConnection, "messageFetch FETCH $id:$id RFC822.HEADER\n");
  221. $read = fgets($imapConnection, 1024);
  222. /** defaults... if the don't get overwritten, it will display text **/
  223. $header["TYPE0"] = "text";
  224. $header["TYPE1"] = "plain";
  225. $header["ENCODING"] = "us-ascii";
  226. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  227. /** MIME-VERSION **/
  228. if (substr($read, 0, 17) == "MIME-Version: 1.0") {
  229. $header["MIME"] = true;
  230. $read = fgets($imapConnection, 1024);
  231. }
  232. /** ENCODING TYPE **/
  233. else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
  234. $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
  235. }
  236. /** CONTENT-TYPE **/
  237. else if (substr($read, 0, 13) == "Content-Type:") {
  238. $cont = strtolower(trim(substr($read, 13)));
  239. if (strpos($cont, ";"))
  240. $cont = substr($cont, 0, strpos($cont, ";"));
  241. if (strpos($cont, "/")) {
  242. $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
  243. $header["TYPE1"] = substr($cont, strpos($cont, "/")+1);
  244. } else {
  245. $header["TYPE0"] = $cont;
  246. }
  247. $line = $read;
  248. $read = fgets($imapConnection, 1024);
  249. while ( (substr(substr($read, 0, strpos($read, " ")), -1) != ":") && (trim($read) != "") && (trim($read) != ")")) {
  250. str_replace("\n", "", $line);
  251. str_replace("\n", "", $read);
  252. $line = "$line $read";
  253. $read = fgets($imapConnection, 1024);
  254. }
  255. /** Detect the boundary of a multipart message **/
  256. if (strpos(strtolower(trim($line)), "boundary=")) {
  257. $pos = strpos($line, "boundary=") + 9;
  258. $bound = trim($line);
  259. if (strpos($line, " ", $pos) > 0) {
  260. $bound = substr($bound, $pos, strpos($line, " ", $pos));
  261. } else {
  262. $bound = substr($bound, $pos);
  263. }
  264. $bound = str_replace("\"", "", $bound);
  265. $header["BOUNDARY"] = $bound;
  266. }
  267. /** Detect the charset **/
  268. if (strpos(strtolower(trim($line)), "charset=")) {
  269. $pos = strpos($line, "charset=") + 8;
  270. $charset = trim($line);
  271. if (strpos($line, " ", $pos) > 0) {
  272. $charset = substr($charset, $pos, strpos($line, " ", $pos));
  273. } else {
  274. $charset = substr($charset, $pos);
  275. }
  276. $charset = str_replace("\"", "", $charset);
  277. $header["CHARSET"] = $charset;
  278. } else {
  279. $header["CHARSET"] = "us-ascii";
  280. }
  281. /** Detects filename if any **/
  282. if (strpos(strtolower(trim($line)), "name=")) {
  283. $pos = strpos($line, "name=") + 5;
  284. $name = trim($line);
  285. if (strpos($line, " ", $pos) > 0) {
  286. $name = substr($name, $pos, strpos($line, " ", $pos));
  287. } else {
  288. $name = substr($name, $pos);
  289. }
  290. $name = str_replace("\"", "", $name);
  291. $header["FILENAME"] = $name;
  292. }
  293. }
  294. /** REPLY-TO **/
  295. else if (strtolower(substr($read, 0, 9)) == "reply-to:") {
  296. $header["REPLYTO"] = trim(substr($read, 9, strlen($read)));
  297. $read = fgets($imapConnection, 1024);
  298. }
  299. /** FROM **/
  300. else if (strtolower(substr($read, 0, 5)) == "from:") {
  301. $header["FROM"] = trim(substr($read, 5, strlen($read) - 6));
  302. if ($header["REPLYTO"] == "")
  303. $header["REPLYTO"] = $header["FROM"];
  304. $read = fgets($imapConnection, 1024);
  305. }
  306. /** DATE **/
  307. else if (strtolower(substr($read, 0, 5)) == "date:") {
  308. $d = substr($read, 5, strlen($read) - 6);
  309. $d = trim($d);
  310. $d = ereg_replace(" ", " ", $d);
  311. $d = explode(" ", $d);
  312. $header["DATE"] = getTimeStamp($d);
  313. $read = fgets($imapConnection, 1024);
  314. }
  315. /** SUBJECT **/
  316. else if (strtolower(substr($read, 0, 8)) == "subject:") {
  317. $header["SUBJECT"] = trim(substr($read, 8, strlen($read) - 9));
  318. if (strlen(Chop($header["SUBJECT"])) == 0)
  319. $header["SUBJECT"] = "(no subject)";
  320. $read = fgets($imapConnection, 1024);
  321. }
  322. /** CC **/
  323. else if (strtolower(substr($read, 0, 3)) == "cc:") {
  324. $pos = 0;
  325. $header["CC"][$pos] = trim(substr($read, 4));
  326. $read = fgets($imapConnection, 1024);
  327. while ((substr($read, 0, 1) == " ") && (trim($read) != "")) {
  328. $pos++;
  329. $header["CC"][$pos] = trim($read);
  330. $read = fgets($imapConnection, 1024);
  331. }
  332. }
  333. /** TO **/
  334. else if (strtolower(substr($read, 0, 3)) == "to:") {
  335. $pos = 0;
  336. $header["TO"][$pos] = trim(substr($read, 4));
  337. $read = fgets($imapConnection, 1024);
  338. while ((substr($read, 0, 1) == " ") && (trim($read) != "")){
  339. $pos++;
  340. $header["TO"][$pos] = trim($read);
  341. $read = fgets($imapConnection, 1024);
  342. }
  343. }
  344. /** ERROR CORRECTION **/
  345. else if (substr($read, 0, 1) == ")") {
  346. if ($header["SUBJECT"] == "")
  347. $header["SUBJECT"] = "(no subject)";
  348. if ($header["FROM"] == "")
  349. $header["FROM"] = "(unknown sender)";
  350. if ($header["DATE"] == "")
  351. $header["DATE"] = time();
  352. $read = fgets($imapConnection, 1024);
  353. }
  354. else {
  355. $read = fgets($imapConnection, 1024);
  356. }
  357. }
  358. return $header;
  359. }
  360. function fetchBody($imapConnection, $bound, $id, $type0, $type1) {
  361. /** This first part reads in the full body of the message **/
  362. fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
  363. $read = fgets($imapConnection, 1024);
  364. $count = 0;
  365. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  366. $body[$count] = $read;
  367. $count++;
  368. $read = fgets($imapConnection, 1024);
  369. }
  370. /** this deletes the first line, and the last two (imap stuff we ignore) **/
  371. $i = 0;
  372. $j = 0;
  373. while ($i < count($body)) {
  374. if ( ($i != 0) && ($i != count($body) - 1) && ($i != count($body)) ) {
  375. $bodytmp[$j] = $body[$i];
  376. $j++;
  377. }
  378. $i++;
  379. }
  380. $body = $bodytmp;
  381. /** Now, lets work out the MIME stuff **/
  382. /** (needs mime.php included) **/
  383. return decodeMime($body, $bound, $type0, $type1);
  384. }
  385. function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
  386. /** defaults... if the don't get overwritten, it will display text **/
  387. $type0 = "text";
  388. $type1 = "plain";
  389. $encoding = "us-ascii";
  390. $i = 0;
  391. while (trim($read[$i]) != "") {
  392. if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
  393. $encoding = strtolower(trim(substr($read[$i], 26)));
  394. } else if (substr($read[$i], 0, 13) == "Content-Type:") {
  395. $cont = strtolower(trim(substr($read[$i], 13)));
  396. if (strpos($cont, ";"))
  397. $cont = substr($cont, 0, strpos($cont, ";"));
  398. if (strpos($cont, "/")) {
  399. $type0 = substr($cont, 0, strpos($cont, "/"));
  400. $type1 = substr($cont, strpos($cont, "/")+1);
  401. } else {
  402. $type0 = $cont;
  403. }
  404. $read[$i] = trim($read[$i]);
  405. $line = $read[$i];
  406. $i++;
  407. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  408. str_replace("\n", "", $line);
  409. str_replace("\n", "", $read[$i]);
  410. $line = "$line $read[$i]";
  411. $i++;
  412. $read[$i] = trim($read[$i]);
  413. }
  414. $i--;
  415. /** Detect the boundary of a multipart message **/
  416. if (strpos(strtolower(trim($line)), "boundary=")) {
  417. $pos = strpos($line, "boundary=") + 9;
  418. $bound = trim($line);
  419. if (strpos($line, " ", $pos) > 0) {
  420. $bound = substr($bound, $pos, strpos($line, " ", $pos));
  421. } else {
  422. $bound = substr($bound, $pos);
  423. }
  424. $bound = str_replace("\"", "", $bound);
  425. }
  426. /** Detect the charset **/
  427. if (strpos(strtolower(trim($line)), "charset=")) {
  428. $pos = strpos($line, "charset=") + 8;
  429. $charset = trim($line);
  430. if (strpos($line, " ", $pos) > 0) {
  431. $charset = substr($charset, $pos, strpos($line, " ", $pos));
  432. } else {
  433. $charset = substr($charset, $pos);
  434. }
  435. $charset = str_replace("\"", "", $charset);
  436. }
  437. /** Detects filename if any **/
  438. if (strpos(strtolower(trim($line)), "name=")) {
  439. $pos = strpos($line, "name=") + 5;
  440. $name = trim($line);
  441. if (strpos($line, " ", $pos) > 0) {
  442. $name = substr($name, $pos, strpos($line, " ", $pos));
  443. } else {
  444. $name = substr($name, $pos);
  445. }
  446. $name = str_replace("\"", "", $name);
  447. $filename = $name;
  448. }
  449. }
  450. $i++;
  451. }
  452. /** remove the header from the entity **/
  453. $i = 0;
  454. while (trim($read[$i]) != "") {
  455. $i++;
  456. }
  457. $i++;
  458. for ($p = 0; $i < count($read); $p++) {
  459. $entity[$p] = $read[$i];
  460. $i++;
  461. }
  462. $read = $entity;
  463. }
  464. ?>