imap_messages.php 13 KB

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