imap_messages.php 13 KB

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