imap_messages.php 14 KB

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