imap_messages.php 13 KB

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