imap_messages.php 14 KB

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