mailbox.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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)\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 will fetch the body of a given message and format
  208. it into our standard format. **/
  209. function fetchBody($imapConnection, $id) {
  210. fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
  211. $count = 0;
  212. $read[$count] = fgets($imapConnection, 1024);
  213. while ((substr($read[$count], 0, 15) != "messageFetch OK") && (substr($read[$count], 0, 16) != "messageFetch BAD")) {
  214. $count++;
  215. $read[$count] = fgets($imapConnection, 1024);
  216. }
  217. /** this loop removes the first line, and the last two which
  218. are IMAP information that we don't need. **/
  219. $i = 0;
  220. $j = 0;
  221. while ($i < count($read)) {
  222. if (($i != 0) && ($i != count($read) - 1) && ($i != count($read) - 2)){
  223. $readtmp[$j] = $read[$i];
  224. $j++;
  225. }
  226. $i++;
  227. }
  228. $read = $readtmp;
  229. /** This loop formats the text, creating links out of linkable stuff too **/
  230. $count = 0;
  231. $useHTML= false;
  232. while ($count < count($read)) {
  233. $read[$count] = "^^$read[$count]";
  234. if (strpos(strtolower($read[$count]), "<html") == true) {
  235. $useHTML = true;
  236. } else if (strpos(strtolower($read[$count]), "</html>") == true) {
  237. $useHTML = false;
  238. }
  239. $read[$count] = substr($read[$count], 2, strlen($read[$count]));
  240. if ($useHTML == false) {
  241. $read[$count] = parsePlainBodyText($read[$count]);
  242. } else {
  243. $read[$count] = parseHTMLBodyText($read[$count]);
  244. }
  245. $count++;
  246. }
  247. return $read;
  248. }
  249. function parseHTMLBodyText($line) {
  250. return $line;
  251. }
  252. function parsePlainBodyText($line) {
  253. $line = "^^$line";
  254. if ((strpos(strtolower($line), "<!") == false) &&
  255. (strpos(strtolower($line), "<html>") == false) &&
  256. (strpos(strtolower($line), "</html>") == false)) {
  257. $line = str_replace("<", "&lt;", $line);
  258. $line = str_replace(">", "&gt;", $line);
  259. }
  260. $wrap_at = 86; // Make this configurable int the config file some time
  261. if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning
  262. $line = wordWrap($line, $wrap_at);
  263. $line = str_replace(" ", "&nbsp;", $line);
  264. $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
  265. /** if >> or > are found at the beginning of a line, I'll assume that was
  266. replied text, so make it different colors **/
  267. if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
  268. $line = substr($line, 2, strlen($line));
  269. $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
  270. } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
  271. $line = substr($line, 2, strlen($line));
  272. $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
  273. } else {
  274. $line = substr($line, 2, strlen($line));
  275. $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
  276. }
  277. /** This translates "http://" into a link. It could be made better to accept
  278. "www" and "mailto" also. That should probably be added later. **/
  279. if (strpos(strtolower($line), "http://") != false) {
  280. $line = ereg_replace("<BR>", "", $line);
  281. $start = strpos(strtolower($line), "http://");
  282. $link = substr($line, $start, strlen($line));
  283. if (strpos($link, " ")) {
  284. $end = strpos($link, " ")-1;
  285. }
  286. else if (strpos($link, "&nbsp;")) {
  287. $end = strpos($link, "&nbsp;")-1;
  288. }
  289. else if (strpos($link, "<")) {
  290. $end = strpos($link, "<");
  291. }
  292. else if (strpos($link, ">")) {
  293. $end = strpos($link, ">");
  294. }
  295. else if (strpos($link, "(")) {
  296. $end = strpos($link, "(")-1;
  297. }
  298. else if (strpos($link, ")")) {
  299. $end = strpos($link, ")")-1;
  300. }
  301. else if (strpos($link, "{")) {
  302. $end = strpos($link, "{")-1;
  303. }
  304. else if (strpos($link, "}")) {
  305. $end = strpos($link, "}")-1;
  306. }
  307. else
  308. $end = strlen($link);
  309. $link = substr($line, $start, $end);
  310. $end = $end + $start;
  311. $before = substr($line, 0, $start);
  312. $after = substr($line, $end, strlen($line));
  313. $line = "$before<A HREF=\"$link\" TARGET=_top>$link</A>$after<BR>";
  314. }
  315. return $line;
  316. }
  317. function getMessageHeadersTo($imapConnection, $i, &$to) {
  318. $pos = 0;
  319. fputs($imapConnection, "messageFetch FETCH $i:$i RFC822.HEADER.LINES (To)\n");
  320. $read = fgets($imapConnection, 1024);
  321. $firstline = true;
  322. while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  323. if ($firstline == true) {
  324. $firstline = false;
  325. $read = fgets($imapConnection, 1024);
  326. } else if (strlen(trim($read)) <= 1) {
  327. $firstline = false;
  328. $read = fgets($imapConnection, 1024);
  329. } else if ($read == ")") {
  330. $firstline = false;
  331. $read = fgets($imapConnection, 1024);
  332. } else {
  333. $firstline = false;
  334. $read = ereg_replace("<", "&lt;", $read);
  335. $read = ereg_replace(">", "&gt;", $read);
  336. if (strlen(trim($read)) != 0)
  337. $to[$pos] = substr($read, 3, strlen($read));
  338. $pos++;
  339. $read = fgets($imapConnection, 1024);
  340. }
  341. }
  342. }
  343. function getMessageHeadersCc($imapConnection, $i, &$cc) {
  344. $pos = 0;
  345. fputs($imapConnection, "messageFetch FETCH $i:$i RFC822.HEADER.LINES (Cc)\n");
  346. $read = fgets($imapConnection, 1024);
  347. $firstline = true;
  348. while ((strlen(trim($read)) > 0) && (substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
  349. if ($firstline == true) {
  350. $firstline = false;
  351. $read = fgets($imapConnection, 1024);
  352. } else if (strlen(trim($read)) <= 1) {
  353. $firstline = false;
  354. $read = fgets($imapConnection, 1024);
  355. } else if ($read == ")") {
  356. $firstline = false;
  357. $read = fgets($imapConnection, 1024);
  358. } else {
  359. $read = ereg_replace("<", "&lt;", $read);
  360. $read = ereg_replace(">", "&gt;", $read);
  361. if (strlen(trim($read)) != 0)
  362. $cc[$pos] = substr($read, 3, strlen($read));
  363. $pos++;
  364. $read = fgets($imapConnection, 1024);
  365. }
  366. }
  367. $read = fgets($imapConnection, 1024); // get rid of the last line
  368. }
  369. ?>