imap_messages.php 13 KB

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