imap_parse.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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, 3, strlen($line) - 4)), 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->dnt = 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 (!is_object($hdr->from) && 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. } else if (strtolower(substr($line,0,9)) == 'x-mailer:') {
  215. $hdr->xmailer = trim(substr($line, 9));
  216. }
  217. $i++;
  218. break;
  219. default:
  220. $i++;
  221. break;
  222. }
  223. }
  224. return $hdr;
  225. }
  226. /**
  227. * function to process addresses.
  228. */
  229. function sqimap_parse_address($address, $ar, $addr_ar = array(), $group = '') {
  230. $pos = 0;
  231. $j = strlen( $address );
  232. $name = '';
  233. $addr = '';
  234. while ( $pos < $j ) {
  235. if ($address{$pos} == '"') { /* get the personal name */
  236. $pos++;
  237. while ( $address{$pos} != '"' &&
  238. $pos < $j ) {
  239. if (substr($address, $pos, 2) == '\\"') {
  240. $name .= $address{$pos};
  241. $pos++;
  242. } elseif (substr($address, $pos, 2) == '\\\\') {
  243. $name .= $address{$pos};
  244. $pos++;
  245. }
  246. $name .= $address{$pos};
  247. $pos++;
  248. }
  249. } elseif ($address{$pos} == '<') { /* get email address */
  250. $addr_start=$pos;
  251. $pos++;
  252. while ( $address{$pos} != '>' &&
  253. $pos < $j ) {
  254. $addr .= $address{$pos};
  255. $pos++;
  256. }
  257. } elseif ($address{$pos} == '(') { /* rip off comments */
  258. $addr_start=$pos;
  259. $pos++;
  260. while ( $address{$pos} != ')' &&
  261. $pos < $j ) {
  262. $addr .= $address{$pos};
  263. $pos++;
  264. }
  265. $address_start = substr($address,0,$addr_start);
  266. $address_end = substr($address,$pos+1);
  267. $address = $address_start . $address_end;
  268. $j = strlen( $address );
  269. $pos = $addr_start;
  270. } elseif ( $address{$pos} == ',' ) { /* we reached a delimiter */
  271. if ($addr == '') {
  272. $addr = substr($address,0,$pos);
  273. } elseif ($name == '') {
  274. $name = substr($address,0,$addr_start);
  275. }
  276. $at = strpos($addr, '@');
  277. $addr_structure = new address_structure();
  278. $addr_structure->personal = $name;
  279. $addr_structure->group = $group;
  280. if ($at) {
  281. $addr_structure->mailbox = substr($addr,0,$at);
  282. $addr_structure->host = substr($addr,$at+1);
  283. } else {
  284. $addr_structure->mailbox = $addr;
  285. }
  286. $address = substr($address,$pos+1);
  287. $j = strlen( $address );
  288. $pos = 0;
  289. $name = '';
  290. $addr = '';
  291. $addr_ar[] = $addr_structure;
  292. } elseif ( $address{$pos} == ":" ) { /* process the group addresses */
  293. /* group marker */
  294. $group = substr($address,0,$pos);
  295. $address = substr($address,$pos+1);
  296. $result = sqimap_parse_address($address, $ar, $addr_ar, $group);
  297. $addr_ar = $result[0];
  298. $pos = $result[1];
  299. $address = substr($address,$pos);
  300. $j = strlen( $address );
  301. $group = '';
  302. } elseif ($address{$pos} == ';' && $group ) {
  303. $address = substr($address, 0, $pos-1);
  304. break;
  305. }
  306. $pos++;
  307. }
  308. if ($addr == '') {
  309. $addr = substr($address,0,$pos);
  310. } elseif ($name == '') {
  311. $name = substr($address,0,$addr_start);
  312. }
  313. $at = strpos($addr, '@');
  314. $addr_structure = new address_structure();
  315. $addr_structure->group = $group;
  316. if ($at) {
  317. $addr_structure->mailbox = trim(substr($addr,0,$at));
  318. $addr_structure->host = trim(substr($addr,$at+1));
  319. } else {
  320. $addr_structure->mailbox = trim($addr);
  321. }
  322. if ($group && $addr == '') { /* no addresses found in group */
  323. $name = "$group: Undisclosed recipients;";
  324. $addr_structure->personal = $name;
  325. $addr_ar[] = $addr_structure;
  326. return (array($addr_ar,$pos+1));
  327. } else {
  328. $addr_structure->personal = $name;
  329. if ($name || $addr) {
  330. $addr_ar[] = $addr_structure;
  331. }
  332. }
  333. if ($ar) {
  334. return ($addr_ar);
  335. } else {
  336. return ($addr_ar[0]);
  337. }
  338. }
  339. ?>