imap_messages.php 13 KB

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