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