imap_messages.php 13 KB

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