imap_messages.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /**
  3. ** imap_messages.php
  4. **
  5. ** This implements functions that manipulate messages
  6. **
  7. ** $Id$
  8. **/
  9. if (defined ('imap_messages_php'))
  10. return;
  11. define ('imap_messages_php', true);
  12. /******************************************************************************
  13. ** Copies specified messages to specified folder
  14. ******************************************************************************/
  15. function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
  16. fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n");
  17. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  18. }
  19. /******************************************************************************
  20. ** Deletes specified messages and moves them to trash if possible
  21. ******************************************************************************/
  22. function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
  23. global $move_to_trash, $trash_folder, $auto_expunge;
  24. if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
  25. sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
  26. sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
  27. } else {
  28. sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
  29. }
  30. }
  31. /******************************************************************************
  32. ** Sets the specified messages with specified flag
  33. ******************************************************************************/
  34. function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
  35. fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n");
  36. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  37. }
  38. /******************************************************************************
  39. ** Remove specified flag from specified messages
  40. ******************************************************************************/
  41. function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag) {
  42. fputs ($imap_stream, "a001 STORE $start:$end -FLAGS (\\$flag)\r\n");
  43. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  44. }
  45. /******************************************************************************
  46. ** Returns some general header information -- FROM, DATE, and SUBJECT
  47. ******************************************************************************/
  48. class small_header {
  49. var $from = '', $subject = '', $date = '', $to = '',
  50. $priority = 0, $message_id = 0, $cc = '';
  51. }
  52. function sqimap_get_small_header ($imap_stream, $id, $sent) {
  53. fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n");
  54. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  55. $subject = _("(no subject)");
  56. $from = _("Unknown Sender");
  57. $priority = "0";
  58. $messageid = "<>";
  59. $cc = "";
  60. $to = "";
  61. $date = "";
  62. $type[0] = "";
  63. $type[1] = "";
  64. $g = 0;
  65. for ($i = 0; $i < count($read); $i++) {
  66. if (eregi ("^to:(.*)$", $read[$i], $regs)) {
  67. //$to = sqimap_find_displayable_name(substr($read[$i], 3));
  68. $to = $regs[1];
  69. } else if (eregi ("^from:(.*)$", $read[$i], $regs)) {
  70. //$from = sqimap_find_displayable_name(substr($read[$i], 5));
  71. $from = $regs[1];
  72. } else if (eregi ("^x-priority:(.*)$", $read[$i], $regs)) {
  73. $priority = trim($regs[1]);
  74. } else if (eregi ("^message-id:(.*)$", $read[$i], $regs)) {
  75. $messageid = trim($regs[1]);
  76. } else if (eregi ("^cc:(.*)$", $read[$i], $regs)) {
  77. $cc = $regs[1];
  78. } else if (eregi ("^date:(.*)$", $read[$i], $regs)) {
  79. $date = $regs[1];
  80. } else if (eregi ("^subject:(.*)$", $read[$i], $regs)) {
  81. $subject = htmlspecialchars(trim($regs[1]));
  82. if ($subject == "")
  83. $subject = _("(no subject)");
  84. } else if (eregi ("^content-type:(.*)$", $read[$i], $regs)) {
  85. $type = strtolower(trim($regs[1]));
  86. if ($pos = strpos($type, ";"))
  87. $type = substr($type, 0, $pos);
  88. $type = explode("/", $type);
  89. if (! isset($type[1]))
  90. $type[1] = '';
  91. }
  92. }
  93. // If there isn't a date, it takes the internal date and uses
  94. // that as the normal date.
  95. if (trim($date) == "") {
  96. fputs ($imap_stream, "a002 FETCH $id INTERNALDATE\r\n");
  97. $internal_read = sqimap_read_data ($imap_stream, "a002", true, $r, $m);
  98. // * 22 FETCH (INTERNALDATE " 8-Sep-2000 13:17:07 -0500")
  99. $date = $internal_read[0];
  100. $date = eregi_replace(".*internaldate \"", "", $date);
  101. $date = eregi_replace("\".*", "", $date);
  102. $date_ary = explode(" ", trim($date));
  103. $date_ary[0] = str_replace("-", " ", $date_ary[0]);
  104. $date = implode (" ", $date_ary);
  105. }
  106. fputs ($imap_stream, "a003 FETCH $id RFC822.SIZE\r\n");
  107. $read = sqimap_read_data($imap_stream, "a003", true, $r, $m);
  108. eregi("([0-9]+)[^0-9]*$", $read[0], $regs);
  109. $size = $regs[1];
  110. $header = new small_header;
  111. if ($sent == true)
  112. $header->from = (trim($to) != '')? $to : _("(only Cc/Bcc)");
  113. else
  114. $header->from = $from;
  115. $header->date = $date;
  116. $header->subject = $subject;
  117. $header->to = $to;
  118. $header->priority = $priority;
  119. $header->message_id = $messageid;
  120. $header->cc = $cc;
  121. $header->size = $size;
  122. $header->type0 = $type[0];
  123. $header->type1 = $type[1];
  124. return $header;
  125. }
  126. /******************************************************************************
  127. ** Returns the flags for the specified messages
  128. ******************************************************************************/
  129. function sqimap_get_flags ($imap_stream, $i) {
  130. fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
  131. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  132. if (ereg("FLAGS(.*)", $read[0], $regs))
  133. return explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
  134. return Array('None');
  135. }
  136. /******************************************************************************
  137. ** Returns a message array with all the information about a message. See
  138. ** the documentation folder for more information about this array.
  139. ******************************************************************************/
  140. function sqimap_get_message ($imap_stream, $id, $mailbox) {
  141. $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
  142. return sqimap_get_message_body($imap_stream, $header);
  143. }
  144. /******************************************************************************
  145. ** Wrapper function that reformats the header information.
  146. ******************************************************************************/
  147. function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
  148. fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
  149. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  150. $header = sqimap_get_header($imap_stream, $read);
  151. $header->id = $id;
  152. $header->mailbox = $mailbox;
  153. return $header;
  154. }
  155. /******************************************************************************
  156. ** Wrapper function that returns entity headers for use by decodeMime
  157. ******************************************************************************/
  158. /*
  159. function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
  160. $header = sqimap_get_header($imap_stream, $read);
  161. $type0 = $header["TYPE0"];
  162. $type1 = $header["TYPE1"];
  163. $bound = $header["BOUNDARY"];
  164. $encoding = $header["ENCODING"];
  165. $charset = $header["CHARSET"];
  166. $filename = $header["FILENAME"];
  167. }
  168. */
  169. /******************************************************************************
  170. ** Queries the IMAP server and gets all header information.
  171. ******************************************************************************/
  172. function sqimap_get_header ($imap_stream, $read) {
  173. global $where, $what;
  174. $hdr = new msg_header();
  175. $i = 0;
  176. // Set up some defaults
  177. $hdr->type0 = "text";
  178. $hdr->type1 = "plain";
  179. $hdr->charset = "us-ascii";
  180. while ($i < count($read)) {
  181. if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
  182. $hdr->mime = true;
  183. $i++;
  184. }
  185. /** ENCODING TYPE **/
  186. else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
  187. $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
  188. $i++;
  189. }
  190. /** CONTENT-TYPE **/
  191. else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
  192. $cont = strtolower(trim(substr($read[$i], 13)));
  193. if (strpos($cont, ";"))
  194. $cont = substr($cont, 0, strpos($cont, ";"));
  195. if (strpos($cont, "/")) {
  196. $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
  197. $hdr->type1 = substr($cont, strpos($cont, "/")+1);
  198. } else {
  199. $hdr->type0 = $cont;
  200. }
  201. $line = $read[$i];
  202. $i++;
  203. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  204. str_replace("\n", "", $line);
  205. str_replace("\n", "", $read[$i]);
  206. $line = "$line $read[$i]";
  207. $i++;
  208. }
  209. /** Detect the boundary of a multipart message **/
  210. if (eregi('boundary="([^"]+)"', $line, $regs)) {
  211. $hdr->boundary = $regs[1];
  212. }
  213. /** Detect the charset **/
  214. if (strpos(strtolower(trim($line)), "charset=")) {
  215. $pos = strpos($line, "charset=") + 8;
  216. $charset = trim($line);
  217. if (strpos($line, ";", $pos) > 0) {
  218. $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
  219. } else {
  220. $charset = substr($charset, $pos);
  221. }
  222. $charset = str_replace("\"", "", $charset);
  223. $hdr->charset = $charset;
  224. } else {
  225. $hdr->charset = "us-ascii";
  226. }
  227. }
  228. else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
  229. /** Add better dontent-disposition support **/
  230. $line = $read[$i];
  231. $i++;
  232. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  233. str_replace("\n", "", $line);
  234. str_replace("\n", "", $read[$i]);
  235. $line = "$line $read[$i]";
  236. $i++;
  237. }
  238. /** Detects filename if any **/
  239. if (strpos(strtolower(trim($line)), "filename=")) {
  240. $pos = strpos($line, "filename=") + 9;
  241. $name = trim($line);
  242. if (strpos($line, " ", $pos) > 0) {
  243. $name = substr($name, $pos, strpos($line, " ", $pos));
  244. } else {
  245. $name = substr($name, $pos);
  246. }
  247. $name = str_replace("\"", "", $name);
  248. $hdr->filename = $name;
  249. }
  250. }
  251. /** REPLY-TO **/
  252. else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
  253. $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
  254. $i++;
  255. }
  256. /** FROM **/
  257. else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
  258. $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
  259. if (! isset($hdr->replyto) || $hdr->replyto == "")
  260. $hdr->replyto = $hdr->from;
  261. $i++;
  262. }
  263. /** DATE **/
  264. else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
  265. $d = substr($read[$i], 5);
  266. $d = trim($d);
  267. $d = strtr($d, array(' ' => ' '));
  268. $d = explode(' ', $d);
  269. $hdr->date = getTimeStamp($d);
  270. $i++;
  271. }
  272. /** SUBJECT **/
  273. else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
  274. $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
  275. if (strlen(Chop($hdr->subject)) == 0)
  276. $hdr->subject = _("(no subject)");
  277. if ($where == "SUBJECT") {
  278. $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
  279. }
  280. $i++;
  281. }
  282. /** CC **/
  283. else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
  284. $pos = 0;
  285. $hdr->cc[$pos] = trim(substr($read[$i], 4));
  286. $i++;
  287. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  288. $pos++;
  289. $hdr->cc[$pos] = trim($read[$i]);
  290. $i++;
  291. }
  292. }
  293. /** TO **/
  294. else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
  295. $pos = 0;
  296. $hdr->to[$pos] = trim(substr($read[$i], 4));
  297. $i++;
  298. while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
  299. $pos++;
  300. $hdr->to[$pos] = trim($read[$i]);
  301. $i++;
  302. }
  303. }
  304. /** MESSAGE ID **/
  305. else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
  306. $hdr->message_id = trim(substr($read[$i], 11));
  307. $i++;
  308. }
  309. /** ERROR CORRECTION **/
  310. else if (substr($read[$i], 0, 1) == ")") {
  311. if (strlen(trim($hdr->subject)) == 0)
  312. $hdr->subject = _("(no subject)");
  313. if (strlen(trim($hdr->from)) == 0)
  314. $hdr->from = _("(unknown sender)");
  315. if (strlen(trim($hdr->date)) == 0)
  316. $hdr->date = time();
  317. $i++;
  318. }
  319. else {
  320. $i++;
  321. }
  322. }
  323. return $hdr;
  324. }
  325. /******************************************************************************
  326. ** Returns the body of a message.
  327. ******************************************************************************/
  328. function sqimap_get_message_body ($imap_stream, &$header) {
  329. $id = $header->id;
  330. return decodeMime($imap_stream, $header);
  331. }
  332. /******************************************************************************
  333. ** Returns an array with the body structure
  334. ******************************************************************************/
  335. ?>