imap_messages.php 13 KB

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