imap_messages.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?
  2. /**
  3. ** imap_messages.php
  4. **
  5. ** This implements functions that manipulate messages
  6. **/
  7. /******************************************************************************
  8. ** Copies specified messages to specified folder
  9. ******************************************************************************/
  10. function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
  11. fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n");
  12. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  13. }
  14. /******************************************************************************
  15. ** Deletes specified messages and moves them to trash if possible
  16. ******************************************************************************/
  17. function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
  18. global $move_to_trash, $trash_folder, $auto_expunge;
  19. if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder))) {
  20. sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
  21. sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
  22. } else {
  23. sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
  24. }
  25. }
  26. /******************************************************************************
  27. ** Sets the specified messages with specified flag
  28. ******************************************************************************/
  29. function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
  30. fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n");
  31. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  32. }
  33. /******************************************************************************
  34. ** Returns some general header information -- FROM, DATE, and SUBJECT
  35. ******************************************************************************/
  36. function sqimap_get_small_header ($imap_stream, $id, &$from, &$subject, &$date) {
  37. fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER.FIELDS (From Subject Date)]\r\n");
  38. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  39. for ($i = 0; $i < count($read); $i++) {
  40. if (strtolower(substr($read[$i], 0, 5)) == "from:") {
  41. $from = sqimap_find_displayable_name(substr($read[$i], 5));
  42. } else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
  43. $date = substr($read[$i], 5);
  44. } else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
  45. $subject = htmlspecialchars(substr($read[$i], 8));
  46. if (strlen(trim($subject)) == 0)
  47. $subject = _("(no subject)");
  48. }
  49. }
  50. }
  51. /******************************************************************************
  52. ** Returns the flags for the specified messages
  53. ******************************************************************************/
  54. function sqimap_get_flags ($imap_stream, $start, $end) {
  55. fputs ($imap_stream, "a001 FETCH $start:$end FLAGS\r\n");
  56. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  57. $i = 0;
  58. while ($i < count($read)) {
  59. if (strpos($read[$i], "FLAGS")) {
  60. $tmp = ereg_replace("\(", "", $read[$i]);
  61. $tmp = ereg_replace("\)", "", $tmp);
  62. $tmp = str_replace("\\", "", $tmp);
  63. $tmp = substr($tmp, strpos($tmp, "FLAGS")+6, strlen($tmp));
  64. $tmp = trim($tmp);
  65. $flags[$i] = explode(" ", $tmp);
  66. } else {
  67. $flags[$i][0] = "None";
  68. }
  69. $i++;
  70. }
  71. return $flags;
  72. }
  73. /******************************************************************************
  74. ** Returns a message array with all the information about a message. See
  75. ** the documentation folder for more information about this array.
  76. ******************************************************************************/
  77. function sqimap_get_message ($imap_stream, $id, $mailbox) {
  78. $message["INFO"]["ID"] = $id;
  79. $message["INFO"]["MAILBOX"] = $mailbox;
  80. $message["HEADER"] = sqimap_get_message_header($imap_stream, $id);
  81. $message["ENTITIES"] = sqimap_get_message_body($imap_stream, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"], $message["HEADER"]["ENCODING"], $message["HEADER"]["CHARSET"]);
  82. return $message;
  83. }
  84. /******************************************************************************
  85. ** Wrapper function that reformats the header information.
  86. ******************************************************************************/
  87. function sqimap_get_message_header ($imap_stream, $id) {
  88. fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
  89. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  90. return sqimap_get_header($imap_stream, $read);
  91. }
  92. /******************************************************************************
  93. ** Wrapper function that returns entity headers for use by decodeMime
  94. ******************************************************************************/
  95. function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
  96. $header = sqimap_get_header($imap_stream, $read);
  97. $type0 = $header["TYPE0"];
  98. $type1 = $header["TYPE1"];
  99. $bound = $header["BOUNDARY"];
  100. $encoding = $header["ENCODING"];
  101. $charset = $header["CHARSET"];
  102. $filename = $header["FILENAME"];
  103. }
  104. /******************************************************************************
  105. ** Queries the IMAP server and gets all header information.
  106. ******************************************************************************/
  107. function sqimap_get_header ($imap_stream, $read) {
  108. $i = 0;
  109. // Set up some defaults
  110. $header["TYPE0"] = "text";
  111. $header["TYPE1"] = "plain";
  112. $header["CHARSET"] = "us-ascii";
  113. while ($i < count($read)) {
  114. if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
  115. $header["MIME"] = true;
  116. $i++;
  117. }
  118. /** ENCODING TYPE **/
  119. else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
  120. $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
  121. $i++;
  122. }
  123. /** CONTENT-TYPE **/
  124. else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
  125. $cont = strtolower(trim(substr($read[$i], 13)));
  126. if (strpos($cont, ";"))
  127. $cont = substr($cont, 0, strpos($cont, ";"));
  128. if (strpos($cont, "/")) {
  129. $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
  130. $header["TYPE1"] = substr($cont, strpos($cont, "/")+1);
  131. } else {
  132. $header["TYPE0"] = $cont;
  133. }
  134. $line = $read[$i];
  135. $i++;
  136. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  137. str_replace("\n", "", $line);
  138. str_replace("\n", "", $read[$i]);
  139. $line = "$line $read[$i]";
  140. $i++;
  141. }
  142. /** Detect the boundary of a multipart message **/
  143. if (strpos(strtolower(trim($line)), "boundary=")) {
  144. $pos = strpos(strtolower($line), "boundary=") + 9;
  145. $bound = trim($line);
  146. if (strpos($line, " ", $pos) > 0) {
  147. $bound = substr($bound, $pos, strpos($line, " ", $pos));
  148. } else {
  149. $bound = substr($bound, $pos);
  150. }
  151. $bound = str_replace("\"", "", $bound);
  152. $header["BOUNDARY"] = $bound;
  153. }
  154. /** Detect the charset **/
  155. if (strpos(strtolower(trim($line)), "charset=")) {
  156. $pos = strpos($line, "charset=") + 8;
  157. $charset = trim($line);
  158. if (strpos($line, " ", $pos) > 0) {
  159. $charset = substr($charset, $pos, strpos($line, " ", $pos));
  160. } else {
  161. $charset = substr($charset, $pos);
  162. }
  163. $charset = str_replace("\"", "", $charset);
  164. $header["CHARSET"] = $charset;
  165. } else {
  166. $header["CHARSET"] = "us-ascii";
  167. }
  168. /** Detects filename if any **/
  169. if (strpos(strtolower(trim($line)), "name=")) {
  170. $pos = strpos($line, "name=") + 5;
  171. $name = trim($line);
  172. if (strpos($line, " ", $pos) > 0) {
  173. $name = substr($name, $pos, strpos($line, " ", $pos));
  174. } else {
  175. $name = substr($name, $pos);
  176. }
  177. $name = str_replace("\"", "", $name);
  178. $header["FILENAME"] = $name;
  179. }
  180. }
  181. /** REPLY-TO **/
  182. else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
  183. $header["REPLYTO"] = trim(substr($read[$i], 9, strlen($read[$i])));
  184. $i++;
  185. }
  186. /** FROM **/
  187. else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
  188. $header["FROM"] = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
  189. if ($header["REPLYTO"] == "")
  190. $header["REPLYTO"] = $header["FROM"];
  191. $i++;
  192. }
  193. /** DATE **/
  194. else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
  195. $d = substr($read[$i], 5);
  196. $d = trim($d);
  197. $d = ereg_replace(" ", " ", $d);
  198. $d = explode(" ", $d);
  199. $header["DATE"] = getTimeStamp($d);
  200. $i++;
  201. }
  202. /** SUBJECT **/
  203. else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
  204. $header["SUBJECT"] = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
  205. if (strlen(Chop($header["SUBJECT"])) == 0)
  206. $header["SUBJECT"] = _("(no subject)");
  207. $i++;
  208. }
  209. /** CC **/
  210. else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
  211. $pos = 0;
  212. $header["CC"][$pos] = trim(substr($read[$i], 4));
  213. $i++;
  214. while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")) {
  215. $pos++;
  216. $header["CC"][$pos] = trim($read[$i]);
  217. $i++;
  218. }
  219. }
  220. /** TO **/
  221. else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
  222. $pos = 0;
  223. $header["TO"][$pos] = trim(substr($read[$i], 4));
  224. $i++;
  225. while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")){
  226. $pos++;
  227. $header["TO"][$pos] = trim($read[$i]);
  228. $i++;
  229. }
  230. }
  231. /** MESSAGE ID **/
  232. else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
  233. $header["MESSAGE-ID"] = trim(substr($read[$i], 11));
  234. $i++;
  235. }
  236. /** ERROR CORRECTION **/
  237. else if (substr($read[$i], 0, 1) == ")") {
  238. if ($header["SUBJECT"] == "")
  239. $header["SUBJECT"] = _("(no subject)");
  240. if ($header["FROM"] == "")
  241. $header["FROM"] = _("(unknown sender)");
  242. if ($header["DATE"] == "")
  243. $header["DATE"] = time();
  244. $i++;
  245. }
  246. else {
  247. $i++;
  248. }
  249. }
  250. return $header;
  251. }
  252. /******************************************************************************
  253. ** Returns the body of a message.
  254. ******************************************************************************/
  255. function sqimap_get_message_body ($imap_stream, $bound, $id, $type0, $type1, $encoding, $charset) {
  256. fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\r\n");
  257. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  258. $i = 0;
  259. $j = 0;
  260. while ($i < count($read)-1) {
  261. if ( ($i != 0) ) {
  262. $bodytmp[$j] = $read[$i];
  263. $j++;
  264. }
  265. $i++;
  266. }
  267. $body = $bodytmp;
  268. return decodeMime($body, $bound, $type0, $type1, $encoding, $charset);
  269. }
  270. ?>