imap_messages.php 15 KB

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