mailbox.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 BODY[HEADER.FIELDS (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, $low, $high, &$flags) {
  105. /** * 2 FETCH (FLAGS (\Answered \Seen)) */
  106. fputs($imapConnection, "messageFetch FETCH $low:$high 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 = str_replace("\\", "", $read);
  114. $read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
  115. $read = trim($read);
  116. $flags[$count] = explode(" ", $read);;
  117. } else {
  118. $flags[$count][0] = "None";
  119. }
  120. $count++;
  121. $read = fgets($imapConnection, 1024);
  122. }
  123. }
  124. function decodeEmailAddr($sender) {
  125. $emailAddr = getEmailAddr($sender);
  126. if (strpos($emailAddr, "EMAILSTART--")) {
  127. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  128. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  129. } else {
  130. $emailAddr = $emailAddr;
  131. }
  132. return $emailAddr;
  133. }
  134. function getEmailAddr($sender) {
  135. if (strpos($sender, "EMAILSTART--") == false)
  136. return "$sender";
  137. $emailStart = strpos($sender, "EMAILSTART--") + 12;
  138. $emailAddr = substr($sender, $emailStart, strlen($sender));
  139. $emailAddr = substr($emailAddr, 0, strpos($emailAddr, "--EMAILEND"));
  140. return $emailAddr;
  141. }
  142. function getSender($sender) {
  143. if (strpos($sender, "EMAILSTART--") == false)
  144. return "$sender";
  145. $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
  146. $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
  147. return "$first $second";
  148. }
  149. function getSenderName($sender) {
  150. $name = getSender($sender);
  151. $emailAddr = getEmailAddr($sender);
  152. $emailStart = strpos($emailAddr, "EMAILSTART--");
  153. $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
  154. if (($emailAddr == "") && ($name == "")) {
  155. $from = $sender;
  156. }
  157. else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
  158. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  159. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  160. $from = $emailAddr;
  161. }
  162. else if (strlen(trim($name)) > 0) {
  163. $from = $name;
  164. }
  165. else if (strlen($emailAddr > 0)) {
  166. $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
  167. $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
  168. $from = $emailAddr;
  169. }
  170. $from = trim($from);
  171. // strip out any quotes if they exist
  172. if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
  173. $from = substr($from, 1, strlen($from) - 2);
  174. return $from;
  175. }
  176. /** returns "true" if the copy was completed successfully.
  177. ** returns "false" with an error message if unsuccessful.
  178. **/
  179. function copyMessages($imapConnection, $from_id, $to_id, $folder) {
  180. fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
  181. $read = fgets($imapConnection, 1024);
  182. while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
  183. $read = fgets($imapConnection, 1024);
  184. }
  185. if (substr($read, 0, 15) == "mailboxStore NO") {
  186. return false;
  187. } else if (substr($read, 0, 15) == "mailboxStore OK") {
  188. return true;
  189. }
  190. echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
  191. return false;
  192. }
  193. /** expunges a mailbox **/
  194. function expungeBox($imapConnection, $mailbox) {
  195. selectMailbox($imapConnection, $mailbox, $num);
  196. fputs($imapConnection, "1 EXPUNGE\n");
  197. imapReadData($imapConnection, "1", true, $response, $message);
  198. }
  199. function getFolderNameMinusINBOX($mailbox, $del) {
  200. $inbox = "INBOX" . $del;
  201. if (substr($mailbox, 0, strlen($inbox)) == $inbox)
  202. $box = substr($mailbox, strlen($inbox), 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, $mailbox) {
  209. $message["INFO"]["ID"] = $id;
  210. $message["INFO"]["MAILBOX"] = $mailbox;
  211. $message["HEADER"] = fetchHeader($imapConnection, $id);
  212. $message["ENTITIES"] = fetchBody($imapConnection, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"]);
  213. return $message;
  214. }
  215. function fetchHeader($imapConnection, $id) {
  216. fputs($imapConnection, "messageFetch FETCH $id:$id RFC822.HEADER\n");
  217. $read = fgets($imapConnection, 1024);
  218. /** defaults... if the don't get overwritten, it will display text **/
  219. $header["TYPE0"] = "text";
  220. $header["TYPE1"] = "plain";
  221. $header["ENCODING"] = "us-ascii";
  222. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  223. /** MIME-VERSION **/
  224. if (substr($read, 0, 17) == "MIME-Version: 1.0") {
  225. $header["MIME"] = true;
  226. $read = fgets($imapConnection, 1024);
  227. }
  228. /** ENCODING TYPE **/
  229. else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
  230. $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
  231. }
  232. /** CONTENT-TYPE **/
  233. else if (substr($read, 0, 13) == "Content-Type:") {
  234. $cont = strtolower(trim(substr($read, 13)));
  235. if (strpos($cont, ";"))
  236. $cont = substr($cont, 0, strpos($cont, ";"));
  237. if (strpos($cont, "/")) {
  238. $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
  239. $header["TYPE1"] = substr($cont, strpos($cont, "/")+1);
  240. } else {
  241. $header["TYPE0"] = $cont;
  242. }
  243. $line = $read;
  244. $read = fgets($imapConnection, 1024);
  245. while ( (substr(substr($read, 0, strpos($read, " ")), -1) != ":") && (trim($read) != "") && (trim($read) != ")")) {
  246. str_replace("\n", "", $line);
  247. str_replace("\n", "", $read);
  248. $line = "$line $read";
  249. $read = fgets($imapConnection, 1024);
  250. }
  251. /** Detect the boundary of a multipart message **/
  252. if (strpos(strtolower(trim($line)), "boundary=")) {
  253. $pos = strpos($line, "boundary=") + 9;
  254. $bound = trim($line);
  255. if (strpos($line, " ", $pos) > 0) {
  256. $bound = substr($bound, $pos, strpos($line, " ", $pos));
  257. } else {
  258. $bound = substr($bound, $pos);
  259. }
  260. $bound = str_replace("\"", "", $bound);
  261. $header["BOUNDARY"] = $bound;
  262. }
  263. /** Detect the charset **/
  264. if (strpos(strtolower(trim($line)), "charset=")) {
  265. $pos = strpos($line, "charset=") + 8;
  266. $charset = trim($line);
  267. if (strpos($line, " ", $pos) > 0) {
  268. $charset = substr($charset, $pos, strpos($line, " ", $pos));
  269. } else {
  270. $charset = substr($charset, $pos);
  271. }
  272. $charset = str_replace("\"", "", $charset);
  273. $header["CHARSET"] = $charset;
  274. } else {
  275. $header["CHARSET"] = "us-ascii";
  276. }
  277. /** Detects filename if any **/
  278. if (strpos(strtolower(trim($line)), "name=")) {
  279. $pos = strpos($line, "name=") + 5;
  280. $name = trim($line);
  281. if (strpos($line, " ", $pos) > 0) {
  282. $name = substr($name, $pos, strpos($line, " ", $pos));
  283. } else {
  284. $name = substr($name, $pos);
  285. }
  286. $name = str_replace("\"", "", $name);
  287. $header["FILENAME"] = $name;
  288. }
  289. }
  290. /** REPLY-TO **/
  291. else if (strtolower(substr($read, 0, 9)) == "reply-to:") {
  292. $header["REPLYTO"] = trim(substr($read, 9, strlen($read)));
  293. $read = fgets($imapConnection, 1024);
  294. }
  295. /** FROM **/
  296. else if (strtolower(substr($read, 0, 5)) == "from:") {
  297. $header["FROM"] = trim(substr($read, 5, strlen($read) - 6));
  298. if ($header["REPLYTO"] == "")
  299. $header["REPLYTO"] = $header["FROM"];
  300. $read = fgets($imapConnection, 1024);
  301. }
  302. /** DATE **/
  303. else if (strtolower(substr($read, 0, 5)) == "date:") {
  304. $d = substr($read, 5, strlen($read) - 6);
  305. $d = trim($d);
  306. $d = ereg_replace(" ", " ", $d);
  307. $d = explode(" ", $d);
  308. $header["DATE"] = getTimeStamp($d);
  309. $read = fgets($imapConnection, 1024);
  310. }
  311. /** SUBJECT **/
  312. else if (strtolower(substr($read, 0, 8)) == "subject:") {
  313. $header["SUBJECT"] = trim(substr($read, 8, strlen($read) - 9));
  314. if (strlen(Chop($header["SUBJECT"])) == 0)
  315. $header["SUBJECT"] = "(no subject)";
  316. $read = fgets($imapConnection, 1024);
  317. }
  318. /** CC **/
  319. else if (strtolower(substr($read, 0, 3)) == "cc:") {
  320. $pos = 0;
  321. $header["CC"][$pos] = trim(substr($read, 4));
  322. $read = fgets($imapConnection, 1024);
  323. while ((substr($read, 0, 1) == " ") && (trim($read) != "")) {
  324. $pos++;
  325. $header["CC"][$pos] = trim($read);
  326. $read = fgets($imapConnection, 1024);
  327. }
  328. }
  329. /** TO **/
  330. else if (strtolower(substr($read, 0, 3)) == "to:") {
  331. $pos = 0;
  332. $header["TO"][$pos] = trim(substr($read, 4));
  333. $read = fgets($imapConnection, 1024);
  334. while ((substr($read, 0, 1) == " ") && (trim($read) != "")){
  335. $pos++;
  336. $header["TO"][$pos] = trim($read);
  337. $read = fgets($imapConnection, 1024);
  338. }
  339. }
  340. /** ERROR CORRECTION **/
  341. else if (substr($read, 0, 1) == ")") {
  342. if ($header["SUBJECT"] == "")
  343. $header["SUBJECT"] = "(no subject)";
  344. if ($header["FROM"] == "")
  345. $header["FROM"] = "(unknown sender)";
  346. if ($header["DATE"] == "")
  347. $header["DATE"] = time();
  348. $read = fgets($imapConnection, 1024);
  349. }
  350. else {
  351. $read = fgets($imapConnection, 1024);
  352. }
  353. }
  354. return $header;
  355. }
  356. function fetchBody($imapConnection, $bound, $id, $type0, $type1) {
  357. /** This first part reads in the full body of the message **/
  358. fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
  359. $read = fgets($imapConnection, 1024);
  360. $count = 0;
  361. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  362. $body[$count] = $read;
  363. $count++;
  364. $read = fgets($imapConnection, 1024);
  365. }
  366. /** this deletes the first line, and the last two (imap stuff we ignore) **/
  367. $i = 0;
  368. $j = 0;
  369. while ($i < count($body)) {
  370. if ( ($i != 0) && ($i != count($body) - 1) && ($i != count($body)) ) {
  371. $bodytmp[$j] = $body[$i];
  372. $j++;
  373. }
  374. $i++;
  375. }
  376. $body = $bodytmp;
  377. /** Now, lets work out the MIME stuff **/
  378. /** (needs mime.php included) **/
  379. return decodeMime($body, $bound, $type0, $type1);
  380. }
  381. function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
  382. /** defaults... if the don't get overwritten, it will display text **/
  383. $type0 = "text";
  384. $type1 = "plain";
  385. $encoding = "us-ascii";
  386. $i = 0;
  387. while (trim($read[$i]) != "") {
  388. if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
  389. $encoding = strtolower(trim(substr($read[$i], 26)));
  390. } else if (substr($read[$i], 0, 13) == "Content-Type:") {
  391. $cont = strtolower(trim(substr($read[$i], 13)));
  392. if (strpos($cont, ";"))
  393. $cont = substr($cont, 0, strpos($cont, ";"));
  394. if (strpos($cont, "/")) {
  395. $type0 = substr($cont, 0, strpos($cont, "/"));
  396. $type1 = substr($cont, strpos($cont, "/")+1);
  397. } else {
  398. $type0 = $cont;
  399. }
  400. $read[$i] = trim($read[$i]);
  401. $line = $read[$i];
  402. $i++;
  403. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  404. str_replace("\n", "", $line);
  405. str_replace("\n", "", $read[$i]);
  406. $line = "$line $read[$i]";
  407. $i++;
  408. $read[$i] = trim($read[$i]);
  409. }
  410. $i--;
  411. /** Detect the boundary of a multipart message **/
  412. if (strpos(strtolower(trim($line)), "boundary=")) {
  413. $pos = strpos($line, "boundary=") + 9;
  414. $bound = trim($line);
  415. if (strpos($line, " ", $pos) > 0) {
  416. $bound = substr($bound, $pos, strpos($line, " ", $pos));
  417. } else {
  418. $bound = substr($bound, $pos);
  419. }
  420. $bound = str_replace("\"", "", $bound);
  421. }
  422. /** Detect the charset **/
  423. if (strpos(strtolower(trim($line)), "charset=")) {
  424. $pos = strpos($line, "charset=") + 8;
  425. $charset = trim($line);
  426. if (strpos($line, " ", $pos) > 0) {
  427. $charset = substr($charset, $pos, strpos($line, " ", $pos));
  428. } else {
  429. $charset = substr($charset, $pos);
  430. }
  431. $charset = str_replace("\"", "", $charset);
  432. }
  433. /** Detects filename if any **/
  434. if (strpos(strtolower(trim($line)), "name=")) {
  435. $pos = strpos($line, "name=") + 5;
  436. $name = trim($line);
  437. if (strpos($line, " ", $pos) > 0) {
  438. $name = substr($name, $pos, strpos($line, " ", $pos));
  439. } else {
  440. $name = substr($name, $pos);
  441. }
  442. $name = str_replace("\"", "", $name);
  443. $filename = $name;
  444. }
  445. }
  446. $i++;
  447. }
  448. /** remove the header from the entity **/
  449. $i = 0;
  450. while (trim($read[$i]) != "") {
  451. $i++;
  452. }
  453. $i++;
  454. for ($p = 0; $i < count($read); $p++) {
  455. $entity[$p] = $read[$i];
  456. $i++;
  457. }
  458. $read = $entity;
  459. }
  460. ?>