imap_messages.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. ** imap_messages.php
  4. **
  5. ** This implements functions that manipulate messages
  6. **/
  7. /******************************************************************************
  8. ** Copies specified messages to specified folder
  9. ******************************************************************************/
  10. function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
  11. fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n");
  12. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  13. }
  14. /******************************************************************************
  15. ** Deletes specified messages and moves them to trash if possible
  16. ******************************************************************************/
  17. function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
  18. global $move_to_trash, $trash_folder, $auto_expunge;
  19. if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder))) {
  20. sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
  21. sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
  22. } else {
  23. sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
  24. }
  25. }
  26. /******************************************************************************
  27. ** Sets the specified messages with specified flag
  28. ******************************************************************************/
  29. function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
  30. fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n");
  31. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  32. }
  33. /******************************************************************************
  34. ** Returns some general header information -- FROM, DATE, and SUBJECT
  35. ******************************************************************************/
  36. class small_header {
  37. var $from, $subject, $date;
  38. }
  39. function sqimap_get_small_header ($imap_stream, $start, $end, $sent) {
  40. //fputs ($imap_stream, "a001 FETCH $id BODY[HEADER.FIELDS (DATE FROM SUBJECT)]\r\n");
  41. //fputs ($imap_stream, "a001 FETCH $start:$end RFC822.HEADER\r\n");
  42. fputs ($imap_stream, "a001 FETCH $start:$end BODY.PEEK[HEADER.FIELDS (Date To From Subject)]\r\n");
  43. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  44. $subject = _("(no subject)");
  45. $from = _("Unknown Sender");
  46. for ($i = 0; $i < count($read); $i++) {
  47. while (substr($read[$i], 0, 1) != ")") {
  48. if ($sent == true) {
  49. if (eregi ("^to:", $read[$i])) {
  50. $from = sqimap_find_displayable_name(substr($read[$i], 3));
  51. }
  52. } else {
  53. if (eregi ("^from:", $read[$i])) {
  54. $from = sqimap_find_displayable_name(substr($read[$i], 5));
  55. }
  56. }
  57. if (eregi ("^date:", $read[$i])) {
  58. $date = substr($read[$i], 5);
  59. } else if (eregi ("^subject:", $read[$i])) {
  60. $subject = htmlspecialchars(eregi_replace ("^subject: ", "", $read[$i]));
  61. if (strlen($subject) == 0)
  62. $subject = _("(no subject)");
  63. }
  64. $i++;
  65. }
  66. $header = new small_header;
  67. $header->from = $from;
  68. $header->date = $date;
  69. $header->subject = $subject;
  70. $ary[$g] = $header;
  71. $g++;
  72. }
  73. return $ary;
  74. }
  75. /******************************************************************************
  76. ** Returns the flags for the specified messages
  77. ******************************************************************************/
  78. function sqimap_get_flags ($imap_stream, $i) {
  79. fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
  80. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  81. if (strpos($read[0], "FLAGS")) {
  82. $tmp = ereg_replace("\(", "", $read[0]);
  83. $tmp = ereg_replace("\)", "", $tmp);
  84. $tmp = str_replace("\\", "", $tmp);
  85. $tmp = substr($tmp, strpos($tmp, "FLAGS")+6, strlen($tmp));
  86. $tmp = trim($tmp);
  87. $flags = explode(" ", $tmp);
  88. } else {
  89. $flags[0] = "None";
  90. }
  91. return $flags;
  92. }
  93. /******************************************************************************
  94. ** Returns a message array with all the information about a message. See
  95. ** the documentation folder for more information about this array.
  96. ******************************************************************************/
  97. function sqimap_get_message ($imap_stream, $id, $mailbox) {
  98. $message["INFO"]["ID"] = $id;
  99. $message["INFO"]["MAILBOX"] = $mailbox;
  100. $message["HEADER"] = sqimap_get_message_header($imap_stream, $id);
  101. $message["ENTITIES"] = sqimap_get_message_body($imap_stream, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"], $message["HEADER"]["ENCODING"], $message["HEADER"]["CHARSET"]);
  102. return $message;
  103. }
  104. /******************************************************************************
  105. ** Wrapper function that reformats the header information.
  106. ******************************************************************************/
  107. function sqimap_get_message_header ($imap_stream, $id) {
  108. fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
  109. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  110. return sqimap_get_header($imap_stream, $read);
  111. }
  112. /******************************************************************************
  113. ** Wrapper function that returns entity headers for use by decodeMime
  114. ******************************************************************************/
  115. function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
  116. $header = sqimap_get_header($imap_stream, $read);
  117. $type0 = $header["TYPE0"];
  118. $type1 = $header["TYPE1"];
  119. $bound = $header["BOUNDARY"];
  120. $encoding = $header["ENCODING"];
  121. $charset = $header["CHARSET"];
  122. $filename = $header["FILENAME"];
  123. }
  124. /******************************************************************************
  125. ** Queries the IMAP server and gets all header information.
  126. ******************************************************************************/
  127. function sqimap_get_header ($imap_stream, $read) {
  128. $i = 0;
  129. // Set up some defaults
  130. $header["TYPE0"] = "text";
  131. $header["TYPE1"] = "plain";
  132. $header["CHARSET"] = "us-ascii";
  133. while ($i < count($read)) {
  134. if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
  135. $header["MIME"] = true;
  136. $i++;
  137. }
  138. /** ENCODING TYPE **/
  139. else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
  140. $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
  141. $i++;
  142. }
  143. /** CONTENT-TYPE **/
  144. else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
  145. $cont = strtolower(trim(substr($read[$i], 13)));
  146. if (strpos($cont, ";"))
  147. $cont = substr($cont, 0, strpos($cont, ";"));
  148. if (strpos($cont, "/")) {
  149. $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
  150. $header["TYPE1"] = substr($cont, strpos($cont, "/")+1);
  151. } else {
  152. $header["TYPE0"] = $cont;
  153. }
  154. $line = $read[$i];
  155. $i++;
  156. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  157. str_replace("\n", "", $line);
  158. str_replace("\n", "", $read[$i]);
  159. $line = "$line $read[$i]";
  160. $i++;
  161. }
  162. /** Detect the boundary of a multipart message **/
  163. if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
  164. $header["BOUNDARY"] = $regs[1];
  165. }
  166. /** Detect the charset **/
  167. if (strpos(strtolower(trim($line)), "charset=")) {
  168. $pos = strpos($line, "charset=") + 8;
  169. $charset = trim($line);
  170. if (strpos($line, " ", $pos) > 0) {
  171. $charset = substr($charset, $pos, strpos($line, " ", $pos));
  172. } else {
  173. $charset = substr($charset, $pos);
  174. }
  175. $charset = str_replace("\"", "", $charset);
  176. $header["CHARSET"] = $charset;
  177. } else {
  178. $header["CHARSET"] = "us-ascii";
  179. }
  180. }
  181. else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
  182. /** Add better dontent-disposition support **/
  183. $line = $read[$i];
  184. $i++;
  185. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  186. str_replace("\n", "", $line);
  187. str_replace("\n", "", $read[$i]);
  188. $line = "$line $read[$i]";
  189. $i++;
  190. }
  191. /** Detects filename if any **/
  192. if (strpos(strtolower(trim($line)), "filename=")) {
  193. $pos = strpos($line, "filename=") + 9;
  194. $name = trim($line);
  195. if (strpos($line, " ", $pos) > 0) {
  196. $name = substr($name, $pos, strpos($line, " ", $pos));
  197. } else {
  198. $name = substr($name, $pos);
  199. }
  200. $name = str_replace("\"", "", $name);
  201. $header["FILENAME"] = $name;
  202. }
  203. }
  204. /** REPLY-TO **/
  205. else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
  206. $header["REPLYTO"] = trim(substr($read[$i], 9, strlen($read[$i])));
  207. $i++;
  208. }
  209. /** FROM **/
  210. else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
  211. $header["FROM"] = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
  212. if ($header["REPLYTO"] == "")
  213. $header["REPLYTO"] = $header["FROM"];
  214. $i++;
  215. }
  216. /** DATE **/
  217. else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
  218. $d = substr($read[$i], 5);
  219. $d = trim($d);
  220. $d = ereg_replace(" ", " ", $d);
  221. $d = explode(" ", $d);
  222. $header["DATE"] = getTimeStamp($d);
  223. $i++;
  224. }
  225. /** SUBJECT **/
  226. else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
  227. $header["SUBJECT"] = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
  228. if (strlen(Chop($header["SUBJECT"])) == 0)
  229. $header["SUBJECT"] = _("(no subject)");
  230. $i++;
  231. }
  232. /** CC **/
  233. else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
  234. $pos = 0;
  235. $header["CC"][$pos] = trim(substr($read[$i], 4));
  236. $i++;
  237. while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")) {
  238. $pos++;
  239. $header["CC"][$pos] = trim($read[$i]);
  240. $i++;
  241. }
  242. }
  243. /** TO **/
  244. else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
  245. $pos = 0;
  246. $header["TO"][$pos] = trim(substr($read[$i], 4));
  247. $i++;
  248. while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")){
  249. $pos++;
  250. $header["TO"][$pos] = trim($read[$i]);
  251. $i++;
  252. }
  253. }
  254. /** MESSAGE ID **/
  255. else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
  256. $header["MESSAGE-ID"] = trim(substr($read[$i], 11));
  257. $i++;
  258. }
  259. /** ERROR CORRECTION **/
  260. else if (substr($read[$i], 0, 1) == ")") {
  261. if ($header["SUBJECT"] == "")
  262. $header["SUBJECT"] = _("(no subject)");
  263. if ($header["FROM"] == "")
  264. $header["FROM"] = _("(unknown sender)");
  265. if ($header["DATE"] == "")
  266. $header["DATE"] = time();
  267. $i++;
  268. }
  269. else {
  270. $i++;
  271. }
  272. }
  273. return $header;
  274. }
  275. /******************************************************************************
  276. ** Returns the body of a message.
  277. ******************************************************************************/
  278. function sqimap_get_message_body ($imap_stream, $bound, $id, $type0, $type1, $encoding, $charset) {
  279. fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\r\n");
  280. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  281. $i = 0;
  282. $j = 0;
  283. while ($i < count($read)-1) {
  284. if ( ($i != 0) ) {
  285. $bodytmp[$j] = $read[$i];
  286. $j++;
  287. }
  288. $i++;
  289. }
  290. $body = $bodytmp;
  291. return decodeMime($body, $bound, $type0, $type1, $encoding, $charset);
  292. }
  293. ?>