- function parseStructure($read, $i=0)
- {
- $arg_no = 0;
- $arg_a = array();
- $cnt = strlen($read);
- while ($i < $cnt)
- {
- $char = strtoupper($read{$i});
- switch ($char)
- {
- case '(':
- if ($arg_no == 0 )
- {
- if (!isset($msg))
- {
- $msg = new message();
- $hdr = new msg_header();
- $hdr->type0 = 'text';
- $hdr->type1 = 'plain';
- $hdr->encoding = 'us-ascii';
- $msg->entity_id = $this->calcEntity($msg);
- } else
- {
- $msg->header->type0 = 'multipart';
- $msg->type0 = 'multipart';
- while ($read{$i} == '(')
- {
- $res = $msg->parseStructure($read,$i);
- $i = $res[1];
- $msg->addEntity($res[0]);
- }
- }
- } else
- {
- switch ($arg_no)
- {
- case 1:
- /* multipart properties */
- $i++;
- $res = $this->parseProperties($read,$i);
- $arg_a[] = $res[0];
- $i = $res[1];
- $arg_no++;
- break;
- case 2:
- if (isset($msg->type0) && $msg->type0 == 'multipart')
- {
- $i++;
- $res = $msg->parseDisposition($read,$i);
- $arg_a[] = $res[0];
- $i = $res[1];
- } else /* properties */
- {
- $res = $msg->parseProperties($read,$i);
- $arg_a[] = $res[0];
- $i = $res[1];
- }
- $arg_no++;
- break;
- case 3:
- if (isset($msg->type0) && $msg->type0 == 'multipart')
- {
- $i++;
- $res= $msg->parseLanguage($read,$i);
- $arg_a[] = $res[0];
- $i = $res[1];
- }
- case 7:
- if ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')
- {
- $msg->header->type0 = $arg_a[0];
- $msg->type0 = $arg_a[0];
- $msg->header->type1 = $arg_a[1];
- $msg->type1 = $arg_a[1];
- $rfc822_hdr = new rfc822_header();
- $res = $msg->parseEnvelope($read,$i,$rfc822_hdr);
- $i = $res[1];
- $msg->rfc822_header = $res[0];
- $i++;
- while ($i < $cnt && $read{$i} != '(')
- {
- $i++;
- }
- $res = $msg->parseStructure($read,$i);
- $i = $res[1];
- $msg->addEntity($res[0]);
- }
- break;
- case 8:
- $i++;
- $res = $msg->parseDisposition($read,$i);
- $arg_a[] = $res[0];
- $i = $res[1];
- $arg_no++;
- break;
- case 9:
- if ($arg_a[0] == 'text' ||
- ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822'))
- {
- $i++;
- $res = $msg->parseDisposition($read,$i);
- $arg_a[] = $res[0];
- $i = $res[1];
- } else
- {
- $i++;
- $res = $msg->parseLanguage($read,$i);
- $arg_a[] = $res[0];
- $i = $res[1];
- }
- $arg_no++;
- break;
- case 10:
- if ($arg_a[0] == 'text' ||
- ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822'))
- {
- $i++;
- $res = $msg->parseLanguage($read,$i);
- $arg_a[] = $res[0];
- $i = $res[1];
- } else
- {
- $i = $msg->parseParenthesis($read,$i);
- $arg_a[] = ''; /* not yet desribed in rfc2060 */
- }
- $arg_no++;
- break;
- default:
- /* unknown argument, skip this part */
- $i = $msg->parseParenthesis($read,$i);
- $arg_a[] = '';
- $arg_no++;
- break;
- } /* switch */
- }
- break;
- case '"':
- /* inside an entity -> start processing */
- $debug = substr($read,$i,20);
- $res = $msg->parseQuote($read,$i);
- $arg_s = $res[0];
- $i = $res[1];
- $arg_no++;
- if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */
- $arg_a[] = $arg_s;
- break;
- case 'n':
- case 'N':
- /* probably NIL argument */
- if (strtoupper(substr($read,$i,4)) == 'NIL ' ||
- strtoupper(substr($read,$i,4)) == 'NIL)')
- {
- $arg_a[] = '';
- $arg_no++;
- $i = $i+2;
- }
- break;
- case '{':
- /* process the literal value */
- $res = $msg->parseLiteral($read,$i);
- $arg_s = $res[0];
- $i = $res[1];
- $arg_no++;
- break;
- case (is_numeric($read{$i}) ):
- /* process integers */
- if ($read{$i} == ' ') break;
- $arg_s = $read{$i};;
- $i++;
- while (preg_match('/^[0-9]{1}$/',$read{$i}))
- {
- $arg_s .= $read{$i};
- $i++;
- }
- $arg_no++;
- $arg_a[] = $arg_s;
- break;
- case ')':
- if (isset($msg->type0) && $msg->type0 == 'multipart')
- {
- $multipart = true;
- } else
- {
- $multipart = false;
- }
- if (!$multipart)
- {
- if ($arg_a[0] == 'text' ||
- ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822'))
- {
- $shifted_args = true;
- } else
- {
- $shifted_args = false;
- }
- $hdr->type0 = $arg_a[0];
- $hdr->type1 = $arg_a[1];
-
- $msg->type0 = $arg_a[0];
- $msg->type1 = $arg_a[1];
-
- $arr = $arg_a[2];
- if (is_array($arr))
- {
- $hdr->parameters = $arg_a[2];
- }
- $hdr->id = str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) );
- $hdr->description = $arg_a[4];
- $hdr->encoding = strtolower($arg_a[5]);
- $hdr->entity_id = $msg->entity_id;
- $hdr->size = $arg_a[6];
- if ($shifted_args)
- {
- $hdr->lines = $arg_a[7];
- if (isset($arg_a[8]))
- {
- $hdr->md5 = $arg_a[8];
- }
- if (isset($arg_a[9]))
- {
- $hdr->disposition = $arg_a[9];
- }
- if (isset($arg_a[10]))
- {
- $hdr->language = $arg_a[10];
- }
- } else
- {
- if (isset($arg_a[7]))
- {
- $hdr->md5 = $arg_a[7];
- }
- if (isset($arg_a[8]))
- {
- $hdr->disposition = $arg_a[8];
- }
- if (isset($arg_a[9]))
- {
- $hdr->language = $arg_a[9];
- }
- }
- $msg->header = $hdr;
- $arg_no = 0;
- $i++;
- if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart')
- {
- $msg->entity_id++;
- }
- return (array($msg, $i));
- } else
- {
- $hdr->type0 = 'multipart';
- $hdr->type1 = $arg_a[0];
- $msg->type0 = 'multipart';
- $msg->type1 = $arg_a[0];
- if (is_array($arg_a[1]))
- {
- $hdr->parameters = $arg_a[1];
- }
- if (isset($arg_a[2]))
- {
- $hdr->disposition = $arg_a[2];
- }
- if (isset($arg_a[3]))
- {
- $hdr->language = $arg_a[3];
- }
- $msg->header = $hdr;
- return (array($msg, $i));
- }
- default:
- break;
- } /* switch */
- $i++;
- } /* while */
+ function parseStructure($read, $i = 0) {
+ $arg_no = 0;
+ $arg_a = array();
+ $cnt = strlen($read);
+
+ for (; $i < $cnt; ++$i) {
+ $char = strtoupper($read{$i});
+ switch ($char) {
+ case '(':
+ switch($arg_no) {
+ case 0:
+ if (!isset($msg)) {
+ $msg = new message();
+ $hdr = new msg_header();
+ $hdr->type0 = 'text';
+ $hdr->type1 = 'plain';
+ $hdr->encoding = 'us-ascii';
+ $msg->entity_id = $this->calcEntity($msg);
+ } else {
+ $msg->header->type0 = 'multipart';
+ $msg->type0 = 'multipart';
+ while ($read{$i} == '(') {
+ $res = $msg->parseStructure($read, $i);
+ $i = $res[1];
+ $msg->addEntity($res[0]);
+ }
+ }
+ break;
+ case 1:
+ /* multipart properties */
+ ++$i;
+ $res = $this->parseProperties($read, $i);
+ $arg_a[] = $res[0];
+ $i = $res[1];
+ ++$arg_no;
+ break;
+ case 2:
+ if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
+ ++$i;
+ $res = $msg->parseDisposition($read, $i);
+ } else { /* properties */
+ $res = $msg->parseProperties($read, $i);
+ }
+ $arg_a[] = $res[0];
+ $i = $res[1];
+ ++$arg_no;
+ break;
+ case 3:
+ if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
+ ++$i;
+ $res= $msg->parseLanguage($read, $i);
+ $arg_a[] = $res[0];
+ $i = $res[1];
+ }
+ case 7:
+ if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
+ $msg->header->type0 = $arg_a[0];
+ $msg->header->type1 = $arg_a[1];
+ $msg->type0 = $arg_a[0];
+ $msg->type1 = $arg_a[1];
+ $rfc822_hdr = new rfc822_header();
+ $res = $msg->parseEnvelope($read, $i, $rfc822_hdr);
+ $msg->rfc822_header = $res[0];
+ $i = $res[1] + 1;
+ while (($i < $cnt) && ($read{$i} != '(')) {
+ ++$i;
+ }
+ $res = $msg->parseStructure($read, $i);
+ $i = $res[1];
+ $msg->addEntity($res[0]);
+ }
+ break;
+ case 8:
+ ++$i;
+ $res = $msg->parseDisposition($read, $i);
+ $arg_a[] = $res[0];
+ $i = $res[1];
+ ++$arg_no;
+ break;
+ case 9:
+ ++$i;
+ if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
+ $res = $msg->parseDisposition($read, $i);
+ } else {
+ $res = $msg->parseLanguage($read, $i);
+ }
+ $arg_a[] = $res[0];
+ $i = $res[1];
+ ++$arg_no;
+ break;
+ case 10:
+ if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
+ ++$i;
+ $res = $msg->parseLanguage($read, $i);
+ $arg_a[] = $res[0];
+ $i = $res[1];
+ } else {
+ $i = $msg->parseParenthesis($read, $i);
+ $arg_a[] = ''; /* not yet described in rfc2060 */
+ }
+ ++$arg_no;
+ break;
+ default:
+ /* unknown argument, skip this part */
+ $i = $msg->parseParenthesis($read, $i);
+ $arg_a[] = '';
+ ++$arg_no;
+ break;
+ } /* switch */
+ break;
+ case '"':
+ /* inside an entity -> start processing */
+ $debug = substr($read, $i, 20);
+ $res = $msg->parseQuote($read, $i);
+ $arg_s = $res[0];
+ $i = $res[1];
+ ++$arg_no;
+ if ($arg_no < 3) {
+ $arg_s = strtolower($arg_s); /* type0 and type1 */
+ }
+ $arg_a[] = $arg_s;
+ break;
+ case 'n':
+ case 'N':
+ /* probably NIL argument */
+ if (strtoupper(substr($read, $i, 4)) == 'NIL ') {
+ $arg_a[] = '';
+ ++$arg_no;
+ $i += 2;
+ }
+ break;
+ case '{':
+ /* process the literal value */
+ $res = $msg->parseLiteral($read, $i);
+ $arg_s = $res[0];
+ $i = $res[1];
+ ++$arg_no;
+ break;
+ case is_numeric($read{$i}):
+ /* process integers */
+ if ($read{$i} == ' ') { break; }
+ $arg_s = $read{$i};;
+ for (++$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++$i) {
+ $arg_s .= $read{$i};
+ }
+ ++$arg_no;
+ $arg_a[] = $arg_s;
+ break;
+ case ')':
+ $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart'));
+ if (!$multipart) {
+ $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
+ $hdr->type0 = $arg_a[0];
+ $hdr->type1 = $arg_a[1];
+
+ $msg->type0 = $arg_a[0];
+ $msg->type1 = $arg_a[1];
+ $arr = $arg_a[2];
+ if (is_array($arr)) {
+ $hdr->parameters = $arg_a[2];
+ }
+ $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3]));
+ $hdr->description = $arg_a[4];
+ $hdr->encoding = strtolower($arg_a[5]);
+ $hdr->entity_id = $msg->entity_id;
+ $hdr->size = $arg_a[6];
+ if ($shifted_args) {
+ $hdr->lines = $arg_a[7];
+ $s = 1;
+ } else {
+ $s = 0;
+ }
+ $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5);
+ $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition);
+ $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language);
+ $msg->header = $hdr;
+ if ((strrchr($msg->entity_id, '.') == '.0') && ($msg->type0 !='multipart')) {
+ $msg->entity_id = $this->entity_id . '.1';
+ }
+ } else {
+ $hdr->type0 = 'multipart';
+ $hdr->type1 = $arg_a[0];
+ $msg->type0 = 'multipart';
+ $msg->type1 = $arg_a[0];
+ $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters);
+ $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition);
+ $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language);
+ $msg->header = $hdr;
+ }
+ ++$i;
+ return (array($msg, $i));
+ default: break;
+ } /* switch */
+
+ } /* for */