imap_parse.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. function sqimap_parse_RFC822Header ($read, $hdr) {
  3. $i = 0;
  4. /* Set up some defaults */
  5. $hdr->type0 = "text";
  6. $hdr->type1 = "plain";
  7. $hdr->charset = "us-ascii";
  8. $count = count($read);
  9. while ($i < $count) {
  10. /* unfold multi-line headers */
  11. while (($i + 1 < $count) && (strspn($read[$i + 1], "\t ") > 0) ) {
  12. $read[$i + 1] = substr($read[$i], 0, -2) . ' ' . ltrim($read[$i+1]);
  13. array_splice($read, $i, 1);
  14. $count--;
  15. }
  16. $line = $read[$i];
  17. $c = strtolower($line{0});
  18. switch ($c) {
  19. case 'm':
  20. $c2 = strtolower($line{1});
  21. switch ($c2) {
  22. case 'i':
  23. if (substr($line, 0, 17) == "MIME-Version: 1.0") {
  24. $hdr->mime = true;
  25. }
  26. $i++;
  27. break;
  28. case 'e':
  29. /* MESSAGE ID */
  30. if (strtolower(substr($line, 0, 11)) == "message-id:") {
  31. $hdr->message_id = trim(substr($line, 11));
  32. }
  33. $i++;
  34. break;
  35. default:
  36. $i++;
  37. break;
  38. }
  39. break;
  40. case 'c':
  41. $c2 = strtolower($line{1});
  42. switch ($c2) {
  43. case 'o':
  44. /* Content-Transfer-Encoding */
  45. if (substr(strtolower($line), 0, 26) == "content-transfer-encoding:") {
  46. $hdr->encoding = strtolower(trim(substr($line, 26)));
  47. $i++;
  48. }
  49. /* Content-Type */
  50. else if (strtolower(substr($line, 0, 13)) == "content-type:") {
  51. $cont = strtolower(trim(substr($line, 13)));
  52. if (strpos($cont, ";")) {
  53. $cont = substr($cont, 0, strpos($cont, ";"));
  54. }
  55. if (strpos($cont, "/")) {
  56. $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
  57. $hdr->type1 = substr($cont, strpos($cont, "/")+1);
  58. } else {
  59. $hdr->type0 = $cont;
  60. }
  61. $line = $read[$i];
  62. $i++;
  63. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  64. str_replace("\n", "", $line);
  65. str_replace("\n", "", $read[$i]);
  66. $line = "$line $read[$i]";
  67. $i++;
  68. }
  69. /* Detect the boundary of a multipart message */
  70. if (eregi('boundary="([^"]+)"', $line, $regs)) {
  71. $hdr->boundary = $regs[1];
  72. }
  73. /* Detect the charset */
  74. if (strpos(strtolower(trim($line)), "charset=")) {
  75. $pos = strpos($line, "charset=") + 8;
  76. $charset = trim($line);
  77. if (strpos($line, ";", $pos) > 0) {
  78. $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
  79. } else {
  80. $charset = substr($charset, $pos);
  81. }
  82. $charset = str_replace("\"", "", $charset);
  83. $hdr->charset = $charset;
  84. } else {
  85. $hdr->charset = "us-ascii";
  86. }
  87. /* Detect type in case of multipart/related */
  88. if (strpos(strtolower(trim($line)), "type=")) {
  89. $pos = strpos($line, "type=") + 6;
  90. $type = trim($line);
  91. if (strpos($line, ";", $pos) > 0) {
  92. $type = substr($type, $pos, strpos($line, ";", $pos)-$pos);
  93. } else {
  94. $type = substr($type, $pos);
  95. }
  96. $hdr->type = $type;
  97. }
  98. }
  99. else if (strtolower(substr($line, 0, 20)) == "content-disposition:") {
  100. /* Add better content-disposition support */
  101. $i++;
  102. while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
  103. str_replace("\n", "", $line);
  104. str_replace("\n", "", $read[$i]);
  105. $line = "$line $read[$i]";
  106. $i++;
  107. }
  108. /* Detects filename if any */
  109. if (strpos(strtolower(trim($line)), "filename=")) {
  110. $pos = strpos($line, "filename=") + 9;
  111. $name = trim($line);
  112. if (strpos($line, " ", $pos) > 0) {
  113. $name = substr($name, $pos, strpos($line, " ", $pos));
  114. } else {
  115. $name = substr($name, $pos);
  116. }
  117. $name = str_replace("\"", "", $name);
  118. $hdr->filename = $name;
  119. }
  120. } else $i++;
  121. break;
  122. case 'c': /* Cc */
  123. if (strtolower(substr($line, 0, 3)) == "cc:") {
  124. $hdr->cc = sqimap_parse_address(trim(substr($line, 9, strlen($line) - 10)), true);
  125. }
  126. $i++;
  127. break;
  128. default:
  129. $i++;
  130. break;
  131. }
  132. break;
  133. case 'r': /* Reply-To */
  134. if (strtolower(substr($line, 0, 9)) == "reply-to:") {
  135. $hdr->replyto = sqimap_parse_address(trim(substr($line, 9, strlen($line) - 10)), false);
  136. }
  137. $i++;
  138. break;
  139. case 'f': /* From */
  140. if (strtolower(substr($line, 0, 5)) == "from:") {
  141. $hdr->from=sqimap_parse_address(trim(substr($line, 5, strlen($line) - 6)), false);
  142. if (! isset($hdr->replyto) || $hdr->replyto == "") {
  143. $hdr->replyto = $hdr->from;
  144. }
  145. }
  146. $i++;
  147. break;
  148. case 'd':
  149. $c2 = strtolower($line{1});
  150. switch ($c2) {
  151. case 'a': /* Date */
  152. if (strtolower(substr($line, 0, 5)) == "date:") {
  153. $d = substr($read[$i], 5);
  154. $d = trim($d);
  155. $d = strtr($d, array(' ' => ' '));
  156. $d = explode(' ', $d);
  157. $hdr->date = getTimeStamp($d);
  158. }
  159. $i++;
  160. break;
  161. case 'i': /* Disposition-Notification-To */
  162. if (strtolower(substr($line, 0, 28)) == "disposition-notification-to:") {
  163. $dnt = trim(substr($read[$i], 28));
  164. $hdr->disposition = sqimap_parse_address($dnt, false);
  165. }
  166. $i++;
  167. break;
  168. default:
  169. $i++;
  170. break;
  171. }
  172. break;
  173. case 's':
  174. /* SUBJECT */
  175. if (strtolower(substr($line, 0, 8)) == "subject:") {
  176. $hdr->subject = trim(substr($line, 8, strlen($line) - 9));
  177. if (strlen(Chop($hdr->subject)) == 0) {
  178. $hdr->subject = _("(no subject)");
  179. }
  180. }
  181. $i++;
  182. break;
  183. case 'b':
  184. /* BCC */
  185. if (strtolower(substr($line, 0, 4)) == "bcc:") {
  186. $hdr->bcc = sqimap_parse_address(trim(substr($line, 4, strlen($line) - 5)), true);
  187. }
  188. $i++;
  189. break;
  190. case 't':
  191. /* TO */
  192. if (strtolower(substr($line, 0, 3)) == "to:") {
  193. $hdr->to = sqimap_parse_address(trim(substr($line, 3, strlen($line) - 4)), true);
  194. }
  195. $i++;
  196. break;
  197. case ')':
  198. /* ERROR CORRECTION */
  199. if (strlen(trim($hdr->subject)) == 0) {
  200. $hdr->subject = _("(no subject)");
  201. }
  202. if (strlen(trim($hdr->from)) == 0) {
  203. $hdr->from = _("(unknown sender)");
  204. }
  205. if (strlen(trim($hdr->date)) == 0) {
  206. $hdr->date = time();
  207. }
  208. $i++;
  209. break;
  210. case 'x':
  211. /* X-PRIORITY */
  212. if (strtolower(substr($line, 0, 11)) == "x-priority:") {
  213. $hdr->priority = trim(substr($line, 11));
  214. }
  215. $i++;
  216. break;
  217. default:
  218. $i++;
  219. break;
  220. }
  221. }
  222. return $hdr;
  223. }
  224. /**
  225. * function to process addresses.
  226. */
  227. function sqimap_parse_address($address, $ar, $addr_ar = array(), $group = '') {
  228. $pos = 0;
  229. $j = strlen( $address );
  230. $name = '';
  231. $addr = '';
  232. while ( $pos < $j ) {
  233. if ($address{$pos} == '"') { /* get the personal name */
  234. $pos++;
  235. while ( $address{$pos} != '"' &&
  236. $pos < $j ) {
  237. if (substr($address, $pos, 2) == '\\"') {
  238. $name .= $address{$pos};
  239. $pos++;
  240. } elseif (substr($address, $pos, 2) == '\\\\') {
  241. $name .= $address{$pos};
  242. $pos++;
  243. }
  244. $name .= $address{$pos};
  245. $pos++;
  246. }
  247. } elseif ($address{$pos} == '<') { /* get email address */
  248. $addr_start=$pos;
  249. $pos++;
  250. while ( $address{$pos} != '>' &&
  251. $pos < $j ) {
  252. $addr .= $address{$pos};
  253. $pos++;
  254. }
  255. } elseif ($address{$pos} == '(') { /* rip off comments */
  256. $addr_start=$pos;
  257. $pos++;
  258. while ( $address{$pos} != ')' &&
  259. $pos < $j ) {
  260. $addr .= $address{$pos};
  261. $pos++;
  262. }
  263. $address_start = substr($address,0,$addr_start);
  264. $address_end = substr($address,$pos+1);
  265. $address = $address_start . $address_end;
  266. $j = strlen( $address );
  267. $pos = $addr_start;
  268. } elseif ( $address{$pos} == ',' ) { /* we reached a delimiter */
  269. if ($addr == '') {
  270. $addr = substr($address,0,$pos);
  271. } elseif ($name == '') {
  272. $name = substr($address,0,$addr_start);
  273. }
  274. $at = strpos($addr, '@');
  275. $addr_structure = new address_structure();
  276. $addr_structure->personal = $name;
  277. $addr_structure->group = $group;
  278. if ($at) {
  279. $addr_structure->mailbox = substr($addr,0,$at);
  280. $addr_structure->host = substr($addr,$at+1);
  281. } else {
  282. $addr_structure->mailbox = $addr;
  283. }
  284. $address = substr($address,$pos+1);
  285. $j = strlen( $address );
  286. $pos = 0;
  287. $name = '';
  288. $addr = '';
  289. $addr_ar[] = $addr_structure;
  290. } elseif ( $address{$pos} == ":" ) { /* process the group addresses */
  291. /* group marker */
  292. $group = substr($address,0,$pos);
  293. $address = substr($address,$pos+1);
  294. $result = sqimap_parse_address($address, $ar, $addr_ar, $group);
  295. $addr_ar = $result[0];
  296. $pos = $result[1];
  297. $address = substr($address,$pos);
  298. $j = strlen( $address );
  299. $group = '';
  300. } elseif ($address{$pos} == ';' && $group ) {
  301. $address = substr($address, 0, $pos-1);
  302. break;
  303. }
  304. $pos++;
  305. }
  306. if ($addr == '') {
  307. $addr = substr($address,0,$pos);
  308. } elseif ($name == '') {
  309. $name = substr($address,0,$addr_start);
  310. }
  311. $at = strpos($addr, '@');
  312. $addr_structure = new address_structure();
  313. $addr_structure->group = $group;
  314. if ($at) {
  315. $addr_structure->mailbox = trim(substr($addr,0,$at));
  316. $addr_structure->host = trim(substr($addr,$at+1));
  317. } else {
  318. $addr_structure->mailbox = trim($addr);
  319. }
  320. if ($group && $addr == '') { /* no addresses found in group */
  321. $name = "$group: Undisclosed recipients;";
  322. $addr_structure->personal = $name;
  323. $addr_ar[] = $addr_structure;
  324. return (array($addr_ar,$pos+1));
  325. } else {
  326. $addr_structure->personal = $name;
  327. if ($name || $addr) {
  328. $addr_ar[] = $addr_structure;
  329. }
  330. }
  331. if ($ar) {
  332. return ($addr_ar);
  333. } else {
  334. return ($addr_ar[0]);
  335. }
  336. }
  337. ?>