imap_messages.php 13 KB

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