imap_messages.php 15 KB

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