b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (CVS HEAD) [ class tree: main ] [ index: main ] [ all elements ]

Source for file mime_parser.php

Documentation is available at mime_parser.php

  1. <?php
  2. /*
  3.  * mime_parser.php
  4.  *
  5.  * @(#) $Id: mime_parser.php,v 1.1 2008/12/31 16:04:04 tblue246 Exp $
  6.  *
  7.  */
  8.  
  9. define('MIME_PARSER_START',        1);
  10. define('MIME_PARSER_HEADER',       2);
  11. define('MIME_PARSER_HEADER_VALUE'3);
  12. define('MIME_PARSER_BODY',         4);
  13. define('MIME_PARSER_BODY_START',   5);
  14. define('MIME_PARSER_BODY_DATA',    6);
  15. define('MIME_PARSER_BODY_DONE',    7);
  16. define('MIME_PARSER_END',          8);
  17.  
  18. define('MIME_MESSAGE_START',            1);
  19. define('MIME_MESSAGE_GET_HEADER_NAME',  2);
  20. define('MIME_MESSAGE_GET_HEADER_VALUE'3);
  21. define('MIME_MESSAGE_GET_BODY',         4);
  22. define('MIME_MESSAGE_GET_BODY_PART',    5);
  23.  
  24. define('MIME_ADDRESS_START',            1);
  25. define('MIME_ADDRESS_FIRST',            2);
  26.  
  27. /*
  28. {metadocument}<?xml version="1.0" encoding="ISO-8859-1" ?>
  29. <class>
  30.  
  31.     <package>net.manuellemos.mimeparser</package>
  32.  
  33.     <version>@(#) $Id: mime_parser.php,v 1.1 2008/12/31 16:04:04 tblue246 Exp $</version>
  34.     <copyright>Copyright © (C) Manuel Lemos 2006</copyright>
  35.     <title>MIME parser</title>
  36.     <author>Manuel Lemos</author>
  37.     <authoraddress>mlemos-at-acm.org</authoraddress>
  38.  
  39.     <documentation>
  40.         <idiom>en</idiom>
  41.         <purpose>Parse MIME encapsulated e-mail message data compliant with
  42.             the RFC 2822 or aggregated in mbox format.</purpose>
  43.         <usage>Use the function <functionlink>Decode</functionlink> function
  44.             to retrieve the structure of the messages to be parsed. Adjust its
  45.             parameters to tell how to return the decoded body data.
  46.             Use the <tt>SaveBody</tt> parameter to make the body parts be saved
  47.             to files when the message is larger than the available memory. Use
  48.             the <tt>SkipBody</tt> parameter to just retrieve the message
  49.             structure without returning the body data.<paragraphbreak />
  50.             If the message data is an archive that may contain multiple messages
  51.             aggregated in the mbox format, set the variable
  52.             <variablelink>mbox</variablelink> to <booleanvalue>1</booleanvalue>.</usage>
  53.     </documentation>
  54.  
  55. {/metadocument}
  56. */
  57.  
  58. {
  59. /*
  60. {metadocument}
  61.     <variable>
  62.         <name>error</name>
  63.         <type>STRING</type>
  64.         <value></value>
  65.         <documentation>
  66.             <purpose>Store the message that is returned when an error
  67.                 occurs.</purpose>
  68.             <usage>Check this variable to understand what happened when a call to
  69.                 any of the class functions has failed.<paragraphbreak />
  70.                 This class uses cumulative error handling. This means that if one
  71.                 class functions that may fail is called and this variable was
  72.                 already set to an error message due to a failure in a previous call
  73.                 to the same or other function, the function will also fail and does
  74.                 not do anything.<paragraphbreak />
  75.                 This allows programs using this class to safely call several
  76.                 functions that may fail and only check the failure condition after
  77.                 the last function call.<paragraphbreak />
  78.                 Just set this variable to an empty string to clear the error
  79.                 condition.</usage>
  80.         </documentation>
  81.     </variable>
  82. {/metadocument}
  83. */
  84.     var $error='';
  85.  
  86. /*
  87. {metadocument}
  88.     <variable>
  89.         <name>error_position</name>
  90.         <type>INTEGER</type>
  91.         <value>-1</value>
  92.         <documentation>
  93.             <purpose>Point to the position of the message data or file that
  94.                 refers to the last error that occurred.</purpose>
  95.             <usage>Check this variable to determine the relevant position of the
  96.                 message when a parsing error occurs.</usage>
  97.         </documentation>
  98.     </variable>
  99. {/metadocument}
  100. */
  101.     var $error_position = -1;
  102.  
  103. /*
  104. {metadocument}
  105.     <variable>
  106.         <name>mbox</name>
  107.         <type>BOOLEAN</type>
  108.         <value>0</value>
  109.         <documentation>
  110.             <purpose>Specify whether the message data to parse is a single RFC
  111.                 2822 message or it is an archive that contain multiple messages in
  112.                 the mbox format.</purpose>
  113.             <usage>Set this variable to <booleanvalue>1</booleanvalue> if it is
  114.                 it is intended to parse an mbox message archive.<br />
  115.                 mbox archives may contain multiple messages. Each message starts
  116.                 with the header <tt>From</tt>. Since all valid RFC 2822 headers
  117.                 must with a colon, the class will fail to parse a mbox archive if
  118.                 this variable is set to <booleanvalue>0</booleanvalue>.</usage>
  119.         </documentation>
  120.     </variable>
  121. {/metadocument}
  122. */
  123.     var $mbox = 0;
  124.  
  125. /*
  126. {metadocument}
  127.     <variable>
  128.         <name>decode_headers</name>
  129.         <type>BOOLEAN</type>
  130.         <value>1</value>
  131.         <documentation>
  132.             <purpose>Specify whether the message headers should be decoded.</purpose>
  133.             <usage>Set this variable to <booleanvalue>1</booleanvalue> if it is
  134.                 necessary to decode message headers that may have non-ASCII
  135.                 characters and use other character set encodings.</usage>
  136.         </documentation>
  137.     </variable>
  138. {/metadocument}
  139. */
  140.     var $decode_headers = 1;
  141.  
  142. /*
  143. {metadocument}
  144.     <variable>
  145.         <name>decode_bodies</name>
  146.         <type>BOOLEAN</type>
  147.         <value>1</value>
  148.         <documentation>
  149.             <purpose>Specify whether the message body parts should be decoded.</purpose>
  150.             <usage>Set this variable to <booleanvalue>1</booleanvalue> if it is
  151.                 necessary to parse the message bodies and extract its part
  152.                 structure.</usage>
  153.         </documentation>
  154.     </variable>
  155. {/metadocument}
  156. */
  157.     var $decode_bodies = 1;
  158.  
  159. /*
  160. {metadocument}
  161.     <variable>
  162.         <name>extract_addresses</name>
  163.         <type>BOOLEAN</type>
  164.         <value>1</value>
  165.         <documentation>
  166.             <purpose>Specify whether the message headers that usually contain
  167.                 e-mail addresses should be parsed and the addresses should be
  168.                 extracted by the <functionlink>Decode</functionlink> function.</purpose>
  169.             <usage>Set this variable to <booleanvalue>1</booleanvalue> if it is
  170.                 necessary to extract the e-mail addresses contained in certain
  171.                 message headers.<paragraphbreak />
  172.                 The headers to be parsed are defined by the
  173.                 <variablelink>address_headers</variablelink> variable.<paragraphbreak />
  174.                 The parsed addresses are returned by the
  175.                 <tt>ExtractedAddresses</tt> entry of the <argumentlink>
  176.                     <function>Decode</function>
  177.                     <argument>decoded</argument>
  178.                 </argumentlink> argument of the
  179.                 <functionlink>Decode</functionlink> function.</usage>
  180.         </documentation>
  181.     </variable>
  182. {/metadocument}
  183. */
  184.     var $extract_addresses = 1;
  185.  
  186. /*
  187. {metadocument}
  188.     <variable>
  189.         <name>address_headers</name>
  190.         <type>HASH</type>
  191.         <value></value>
  192.         <documentation>
  193.             <purpose>Specify which headers contain addresses that should be
  194.                 parsed and extracted.</purpose>
  195.             <usage>Change this variable if you need to extract e-mail addresses
  196.                 from a different list of message headers.<paragraphbreak />
  197.                 It must be set to an associative array with keys set to the names
  198.                 of the headers to be parsed including the colon. The array values
  199.                 must be set to a boolean flag to tell whether the headers with the
  200.                 respective name should be parsed. The header names must be in lower
  201.                 case.<paragraphbreak />
  202.                 By default the class addresses from the headers:
  203.                 <stringvalue>from:</stringvalue>, <stringvalue>to:</stringvalue>,
  204.                 <stringvalue>cc:</stringvalue>, <stringvalue>bcc:</stringvalue>,
  205.                 <stringvalue>return-path:</stringvalue>,
  206.                 <stringvalue>reply-to:</stringvalue> and
  207.                 <stringvalue>disposition-notification-to:</stringvalue>.</usage>
  208.         </documentation>
  209.     </variable>
  210. {/metadocument}
  211. */
  212.     var $address_headers = array(
  213.         'from:' => 1,
  214.         'to:' => 1,
  215.         'cc:' => 1,
  216.         'bcc:' => 1,
  217.         'return-path:'=>1,
  218.         'reply-to:'=>1,
  219.         'disposition-notification-to:'=>1
  220.     );
  221.  
  222. /*
  223. {metadocument}
  224.     <variable>
  225.         <name>ignore_syntax_errors</name>
  226.         <type>BOOLEAN</type>
  227.         <value>1</value>
  228.         <documentation>
  229.             <purpose>Specify whether the class should ignore syntax errors in
  230.                 malformed messages.</purpose>
  231.             <usage>Set this variable to <booleanvalue>0</booleanvalue> if it is
  232.                 necessary to verify whether message data may be corrupted due to
  233.                 to eventual bugs in the program that generated the
  234.                 message.<paragraphbreak />
  235.                 Currently the class only ignores some types of syntax errors.
  236.                 Other syntax errors may still cause the
  237.                 <functionlink>Decode</functionlink> to fail.</usage>
  238.         </documentation>
  239.     </variable>
  240. {/metadocument}
  241. */
  242.     var $ignore_syntax_errors=1;
  243.  
  244. /*
  245. {metadocument}
  246.     <variable>
  247.         <name>warnings</name>
  248.         <type>HASH</type>
  249.         <value></value>
  250.         <documentation>
  251.             <purpose>Return a list of positions of the original message that
  252.                 contain syntax errors.</purpose>
  253.             <usage>Check this variable to retrieve eventual message syntax
  254.                 errors that were ignored when the
  255.                 <variablelink>ignore_syntax_errors</variablelink> is set to
  256.                 <booleanvalue>1</booleanvalue>.<paragraphbreak />
  257.                 The indexes of this array are the positions of the errors. The
  258.                 array values are the corresponding syntax error messages.</usage>
  259.         </documentation>
  260.     </variable>
  261. {/metadocument}
  262. */
  263.     var $warnings=array();
  264.  
  265.     /* Private variables */
  266.     var $state = MIME_PARSER_START;
  267.     var $buffer = '';
  268.     var $buffer_position = 0;
  269.     var $offset = 0;
  270.     var $parts = array();
  271.     var $part_position = 0;
  272.     var $headers = array();
  273.     var $body_parser;
  274.     var $body_parser_state = MIME_PARSER_BODY_DONE;
  275.     var $body_buffer = '';
  276.     var $body_buffer_position = 0;
  277.     var $body_offset = 0;
  278.     var $current_header = '';
  279.     var $file;
  280.     var $body_file;
  281.     var $position = 0;
  282.     var $body_part_number = 1;
  283.     var $next_token = '';
  284.  
  285.     /* Private functions */
  286.  
  287.     Function SetError($error)
  288.     {
  289.         $this->error = $error;
  290.         return(0);
  291.     }
  292.  
  293.     Function SetErrorWithContact($error)
  294.     {
  295.         return($this->SetError($error.'. Please contact the author Manuel Lemos <mlemos@acm.org> and send a copy of this message to let him add support for this kind of messages'));
  296.     }
  297.  
  298.     Function SetPositionedError($error$position)
  299.     {
  300.         $this->error_position = $position;
  301.         return($this->SetError($error));
  302.     }
  303.  
  304.     Function SetPositionedWarning($error$position)
  305.     {
  306.         if(!$this->ignore_syntax_errors)
  307.             return($this->SetPositionedError($error$position));
  308.         $this->warnings[$position]=$error;
  309.         return(1);
  310.     }
  311.  
  312.     Function SetPHPError($error&$php_error_message)
  313.     {
  314.         if(IsSet($php_error_message)
  315.         && strlen($php_error_message))
  316.             $error .= ': '.$php_error_message;
  317.         return($this->SetError($error));
  318.     }
  319.  
  320.     Function ResetParserState()
  321.     {
  322.         $this->error='';
  323.         $this->error_position = -1;
  324.         $this->state = MIME_PARSER_START;
  325.         $this->buffer = '';
  326.         $this->buffer_position = 0;
  327.         $this->offset = 0;
  328.         $this->parts = array();
  329.         $this->part_position = 0;
  330.         $this->headers = array();
  331.         $this->body_parser_state = MIME_PARSER_BODY_DONE;
  332.         $this->body_buffer = '';
  333.         $this->body_buffer_position = 0;
  334.         $this->body_offset = 0;
  335.         $this->current_header = '';
  336.         $this->position = 0;
  337.         $this->body_part_number = 1;
  338.         $this->next_token = '';
  339.     }
  340.  
  341.     Function Tokenize($string,$separator="")
  342.     {
  343.         if(!strcmp($separator,""))
  344.         {
  345.             $separator=$string;
  346.             $string=$this->next_token;
  347.         }
  348.         for($character=0;$character<strlen($separator);$character++)
  349.         {
  350.             if(GetType($position=strpos($string,$separator[$character]))=='integer')
  351.                 $found=(IsSet($foundmin($found,$position$position);
  352.         }
  353.         if(IsSet($found))
  354.         {
  355.             $this->next_token=substr($string,$found+1);
  356.             return(substr($string,0,$found));
  357.         }
  358.         else
  359.         {
  360.             $this->next_token='';
  361.             return($string);
  362.         }
  363.     }
  364.  
  365.     Function ParseStructuredHeader($value&$type&$parameters&$character_sets&$languages)
  366.     {
  367.         $type strtolower(trim($this->Tokenize($value';')));
  368.         $p trim($this->Tokenize(''));
  369.         $parameters $character_sets $languages array();
  370.         while(strlen($p))
  371.         {
  372.             $parameter trim(strtolower($this->Tokenize($p'=')));
  373.             $value trim($this->Tokenize(';'));
  374.             if(!strcmp($value[0]'"')
  375.             && !strcmp($value[strlen($value1]'"'))
  376.                 $value substr($value1strlen($value2);
  377.             $p trim($this->Tokenize(''));
  378.             if(($l=strlen($parameter))
  379.             && !strcmp($parameter[$l 1],'*'))
  380.             {
  381.                 $parameter=$this->Tokenize($parameter'*');
  382.                 if(IsSet($parameters[$parameter])
  383.                 && IsSet($character_sets[$parameter]))
  384.                     $value $parameters[$parameterUrlDecode($value);
  385.                 else
  386.                 {
  387.                     $character_sets[$parameterstrtolower($this->Tokenize($value'\''));
  388.                     $languages[$parameter$this->Tokenize('\'');
  389.                     $value UrlDecode($this->Tokenize(''));
  390.                 }
  391.             }
  392.             $parameters[$parameter$value;
  393.         }
  394.     }
  395.  
  396.     Function FindStringLineBreak($string$position&$break&$line_break)
  397.     {
  398.         if(GetType($line_break=strpos($string$break="\n"$position))=='integer')
  399.         {
  400.             if($line_break>$position
  401.             && $string[$line_break-1]=="\r")
  402.             {
  403.                 $line_break--;
  404.                 $break="\r\n";
  405.             }
  406.             return(1);
  407.         }
  408.         return(GetType($line_break=strpos($string$break="\r"$position))=='integer');
  409.     }
  410.  
  411.     Function FindLineBreak($position&$break&$line_break)
  412.     {
  413.         if(GetType($line_break=strpos($this->buffer$break="\r"$position))=='integer')
  414.         {
  415.             if(($n $line_break 1strlen($this->buffer)
  416.             && $this->buffer[$n]=="\n")
  417.                 $break="\r\n";
  418.             return(1);
  419.         }
  420.         return(GetType($line_break=strpos($this->buffer$break="\n"$position))=='integer');
  421.     }
  422.  
  423.     Function FindBodyLineBreak($position&$break&$line_break)
  424.     {
  425.         if(GetType($line_break=strpos($this->body_buffer$break="\r"$position))=='integer')
  426.         {
  427.             if(($n $line_break 1strlen($this->body_buffer)
  428.             && $this->body_buffer[$n]=="\n")
  429.                 $break="\r\n";
  430.             return(1);
  431.         }
  432.         return(GetType($line_break=strpos($this->body_buffer$break="\n"$position))=='integer');
  433.     }
  434.  
  435.     Function ParseHeaderString($body&$position&$headers)
  436.     {
  437.         $l strlen($body);
  438.         $headers array();
  439.         for(;$position $l;)
  440.         {
  441.             if($this->FindStringLineBreak($body$position$break$line_break))
  442.             {
  443.                 $line substr($body$position$line_break $position);
  444.                 $position $line_break strlen($break);
  445.             }
  446.             else
  447.             {
  448.                 $line substr($body$position);
  449.                 $position $l;
  450.             }
  451.             if(strlen($line)==0)
  452.                 break;
  453.             $h strtolower(strtok($line,':'));
  454.             $headers[$htrim(strtok(''));
  455.         }
  456.     }
  457.  
  458.     Function ParsePart($end&$part&$need_more_data)
  459.     {
  460.         $need_more_data 0;
  461.         switch($this->state)
  462.         {
  463.             case MIME_PARSER_START:
  464.                 $part=array(
  465.                     'Type'=>'MessageStart',
  466.                     'Position'=>$this->offset + $this->buffer_position
  467.                 );
  468.                 $this->state = MIME_PARSER_HEADER;
  469.                 break;
  470.             case MIME_PARSER_HEADER:
  471.                 if($this->FindLineBreak($this->buffer_position$break$line_break))
  472.                 {
  473.                     $next $line_break strlen($break);
  474.                     if(!strcmp($break,"\r")
  475.                     && strlen($this->buffer== $next
  476.                     && !$end)
  477.                     {
  478.                         $need_more_data 1;
  479.                         break;
  480.                     }
  481.                     if($line_break==$this->buffer_position)
  482.                     {
  483.                         $part=array(
  484.                             'Type'=>'BodyStart',
  485.                             'Position'=>$this->offset + $this->buffer_position
  486.                         );
  487.                         $this->buffer_position = $next;
  488.                         $this->state = MIME_PARSER_BODY;
  489.                         break;
  490.                     }
  491.                 }
  492.                 if(GetType($colon=strpos($this->buffer':'$this->buffer_position))=='integer')
  493.                 {
  494.                     if(GetType($space=strpos(substr($this->buffer$this->buffer_position$colon $this->buffer_position)' '))=='integer')
  495.                     {
  496.                         if((!$this->mbox
  497.                         || strcmp(strtolower(substr($this->buffer$this->buffer_position$space))'from'))
  498.                         && !$this->SetPositionedWarning('invalid header name line'$this->buffer_position))
  499.                             return(0);
  500.                         $next $this->buffer_position + $space 1;
  501.                     }
  502.                     else
  503.                         $next $colon+1;
  504.                 }
  505.                 else
  506.                 {
  507.                     $need_more_data 1;
  508.                     break;
  509.                 }
  510.                 $part=array(
  511.                     'Type'=>'HeaderName',
  512.                     'Name'=>substr($this->buffer$this->buffer_position$next $this->buffer_position),
  513.                     'Position'=>$this->offset + $this->buffer_position
  514.                 );
  515.                 $this->buffer_position = $next;
  516.                 $this->state = MIME_PARSER_HEADER_VALUE;
  517.                 break;
  518.             case MIME_PARSER_HEADER_VALUE:
  519.                 $position $this->buffer_position;
  520.                 $value '';
  521.                 for(;;)
  522.                 {
  523.                     if($this->FindLineBreak($position$break$line_break))
  524.                     {
  525.                         $next $line_break strlen($break);
  526.                         $line substr($this->buffer$position$line_break $position);
  527.                         if(strlen($this->buffer== $next)
  528.                         {
  529.                             if(!$end)
  530.                             {
  531.                                 $need_more_data 1;
  532.                                 break 2;
  533.                             }
  534.                             $value .= $line;
  535.                             $part=array(
  536.                                 'Type'=>'HeaderValue',
  537.                                 'Value'=>$value,
  538.                                 'Position'=>$this->offset + $this->buffer_position
  539.                             );
  540.                             $this->buffer_position = $next;
  541.                             $this->state = MIME_PARSER_END;
  542.                             break ;
  543.                         }
  544.                         else
  545.                         {
  546.                             $character $this->buffer[$next];
  547.                             if(!strcmp($character' ')
  548.                             || !strcmp($character"\t"))
  549.                             {
  550.                                 $value .= $line;
  551.                                 $position $next;
  552.                             }
  553.                             else
  554.                             {
  555.                                 $value .= $line;
  556.                                 $part=array(
  557.                                     'Type'=>'HeaderValue',
  558.                                     'Value'=>$value,
  559.                                     'Position'=>$this->offset + $this->buffer_position
  560.                                 );
  561.                                 $this->buffer_position = $next;
  562.                                 $this->state = MIME_PARSER_HEADER;
  563.                                 break 2;
  564.                             }
  565.                         }
  566.                     }
  567.                     else
  568.                     {
  569.                         if(!$end)
  570.                         {
  571.                             $need_more_data 1;
  572.                             break;
  573.                         }
  574.                         else
  575.                         {
  576.                             $value .= substr($this->buffer$position);
  577.                             $part=array(
  578.                                 'Type'=>'HeaderValue',
  579.                                 'Value'=>$value,
  580.                                 'Position'=>$this->offset + $this->buffer_position
  581.                             );
  582.                             $this->buffer_position = strlen($this->buffer);
  583.                             $this->state = MIME_PARSER_END;
  584.                             break;
  585.                         }
  586.                     }
  587.                 }
  588.                 break;
  589.             case MIME_PARSER_BODY:
  590.                 if($this->mbox)
  591.                 {
  592.                     $add 0;
  593.                     $append='';
  594.                     if($this->FindLineBreak($this->buffer_position$break$line_break))
  595.                     {
  596.                         $next $line_break strlen($break);
  597.                         $following $next strlen($break);
  598.                         if($following >= strlen($this->buffer)
  599.                         || GetType($line=strpos($this->buffer$break$following))!='integer')
  600.                         {
  601.                             if(!$end)
  602.                             {
  603.                                 $need_more_data 1;
  604.                                 break;
  605.                             }
  606.                         }
  607.                         $start strtolower(substr($this->buffer$nextstrlen($break.'from ')));
  608.                         if(!strcmp($break.'from '$start))
  609.                         {
  610.                             if($line_break == $this->buffer_position)
  611.                             {
  612.                                 $part=array(
  613.                                     'Type'=>'MessageEnd',
  614.                                     'Position'=>$this->offset + $this->buffer_position
  615.                                 );
  616.                                 $this->buffer_position = $following;
  617.                                 $this->state = MIME_PARSER_START;
  618.                                 break;
  619.                             }
  620.                             else
  621.                                 $add strlen($break);
  622.                             $next $line_break;
  623.                         }
  624.                         else
  625.                         {
  626.                             $start strtolower(substr($this->buffer$nextstrlen('>from ')));
  627.                             if(!strcmp('>from '$start))
  628.                             {
  629.                                 $part=array(
  630.                                     'Type'=>'BodyData',
  631.                                     'Data'=>substr($this->buffer$this->buffer_position$next $this->buffer_position),
  632.                                     'Position'=>$this->offset + $this->buffer_position
  633.                                 );
  634.                                 $this->buffer_position = $next 1;
  635.                                 break;
  636.                             }
  637.                         }
  638.                     }
  639.                     else
  640.                     {
  641.                         if(!$end)
  642.                         {
  643.                             $need_more_data 1;
  644.                             break;
  645.                         }
  646.                         $next strlen($this->buffer);
  647.                         $append="\r\n";
  648.                     }
  649.                     if($next $this->buffer_position)
  650.                     {
  651.                         $part=array(
  652.                             'Type'=>'BodyData',
  653.                             'Data'=>substr($this->buffer$this->buffer_position$next $add $this->buffer_position).$append,
  654.                             'Position'=>$this->offset + $this->buffer_position
  655.                         );
  656.                     }
  657.                     elseif($end)
  658.                     {
  659.                         $part=array(
  660.                             'Type'=>'MessageEnd',
  661.                             'Position'=>$this->offset + $this->buffer_position
  662.                         );
  663.                         $this->state = MIME_PARSER_END;
  664.                     }
  665.                     $this->buffer_position = $next;
  666.                 }
  667.                 else
  668.                 {
  669.                     if(strlen($this->buffer)-$this->buffer_position)
  670.                     {
  671.                         $data=substr($this->buffer$this->buffer_positionstrlen($this->buffer$this->buffer_position);
  672.                         $end_line (!strcmp(substr($data,-1),"\n"|| !strcmp(substr($data,-1),"\r"));
  673.                         if($end
  674.                         && !$end_line)
  675.                         {
  676.                             $data.="\n";
  677.                             $end_line 1;
  678.                         }
  679.                         $offset $this->offset + $this->buffer_position;
  680.                         $this->buffer_position = strlen($this->buffer);
  681.                         $need_more_data !$end;
  682.                         if(!$end_line)
  683.                         {
  684.                             if(GetType($line_break=strrpos($data"\n"))=='integer'
  685.                             || GetType($line_break=strrpos($data"\r"))=='integer')
  686.                             {
  687.                                 $line_break++;
  688.                                 $this->buffer_position -= strlen($data$line_break;
  689.                                 $data substr($data0$line_break);
  690.                             }
  691.                         }
  692.                         $part=array(
  693.                             'Type'=>'BodyData',
  694.                             'Data'=>$data,
  695.                             'Position'=>$offset
  696.                         );
  697.                     }
  698.                     else
  699.                     {
  700.                         if($end)
  701.                         {
  702.                             $part=array(
  703.                                 'Type'=>'MessageEnd',
  704.                                 'Position'=>$this->offset + $this->buffer_position
  705.                             );
  706.                             $this->state = MIME_PARSER_END;
  707.                         }
  708.                         else
  709.                             $need_more_data 1;
  710.                     }
  711.                 }
  712.                 break;
  713.             default:
  714.                 return($this->SetPositionedError($this->state.' is not a valid parser state'$this->buffer_position));
  715.         }
  716.         return(1);
  717.     }
  718.  
  719.     Function QueueBodyParts()
  720.     {
  721.         for(;;)
  722.         {
  723.             if(!$this->body_parser->GetPart($part,$end))
  724.                 return($this->SetError($this->body_parser->error));
  725.             if($end)
  726.                 return(1);
  727.             if(!IsSet($part['Part']))
  728.                 $part['Part']=$this->headers['Boundary'];
  729.             $this->parts[]=$part;
  730.         }
  731.     }
  732.  
  733.     Function ParseParameters($value&$first&$parameters$return)
  734.     {
  735.         $first strtolower(trim(strtok($value';')));
  736.         $values trim(strtok(''));
  737.         $parameters array();
  738.         $return_value '';
  739.         while(strlen($values))
  740.         {
  741.             $parameter trim(strtolower(strtok($values'=')));
  742.             $value trim(strtok(';'));
  743.             if(!strcmp($value[0]'"')
  744.             && !strcmp($value[strlen($value1]'"'))
  745.                 $value substr($value1strlen($value2);
  746.             $parameters[$parameter$value;
  747.             if(!strcmp($parameter$return))
  748.                 $return_value $value;
  749.             $values trim(strtok(''));
  750.         }
  751.         return($return_value);
  752.     }
  753.  
  754.     Function DecodePart($part)
  755.     {
  756.         switch($part['Type'])
  757.         {
  758.             case 'MessageStart':
  759.                 $this->headers=array();
  760.                 break;
  761.             case 'HeaderName':
  762.                 if($this->decode_bodies)
  763.                     $this->current_header = strtolower($part['Name']);
  764.                 break;
  765.             case 'HeaderValue':
  766.                 if($this->decode_headers)
  767.                 {
  768.                     $value $part['Value'];
  769.                     $error '';
  770.                     for($decoded_header array()$position 0$position<strlen($value))
  771.                     {
  772.                         if(GetType($encoded=strpos($value,'=?'$position))!='integer')
  773.                         {
  774.                             if($position<strlen($value))
  775.                             {
  776.                                 if(count($decoded_header))
  777.                                     $decoded_header[count($decoded_header)-1]['Value'].=substr($value$position);
  778.                                 else
  779.                                 {
  780.                                     $decoded_header[]=array(
  781.                                         'Value'=>substr($value$position),
  782.                                         'Encoding'=>'ASCII'
  783.                                     );
  784.                                 }
  785.                             }
  786.                             break;
  787.                         }
  788.                         $set $encoded 2;
  789.                         if(GetType($method=strpos($value,'?'$set))!='integer')
  790.                         {
  791.                             $error 'invalid header encoding syntax '.$part['Value'];
  792.                             $error_position $part['Position'$set;
  793.                             break;
  794.                         }
  795.                         $encoding=strtoupper(substr($value$set$method $set));
  796.                         $method += 1;
  797.                         if(GetType($data=strpos($value,'?'$method))!='integer')
  798.                         {
  799.                             $error 'invalid header encoding syntax '.$part['Value'];
  800.                             $error_position $part['Position'$set;
  801.                             break;
  802.                         }
  803.                         $start $data 1;
  804.                         if(GetType($end=strpos($value,'?='$start))!='integer')
  805.                         {
  806.                             $error 'invalid header encoding syntax '.$part['Value'];
  807.                             $error_position $part['Position'$start;
  808.                             break;
  809.                         }
  810.                         if($encoded $position)
  811.                         {
  812.                             if(count($decoded_header))
  813.                                 $decoded_header[count($decoded_header)-1]['Value'].=substr($value$position$encoded $position);
  814.                             else
  815.                             {
  816.                                 $decoded_header[]=array(
  817.                                     'Value'=>substr($value$position$encoded $position),
  818.                                     'Encoding'=>'ASCII'
  819.                                 );
  820.                             }
  821.                         }
  822.                         switch(strtolower(substr($value$method$data $method)))
  823.                         {
  824.                             case 'q':
  825.                                 if($end>$start)
  826.                                 {
  827.                                     for($decoded ''$position $start$position $end )
  828.                                     {
  829.                                         switch($value[$position])
  830.                                         {
  831.                                             case '=':
  832.                                                 $h HexDec($hex strtolower(substr($value$position+12)));
  833.                                                 if($end $position 3
  834.                                                 || strcmp(sprintf('%02x'$h)$hex))
  835.                                                 {
  836.                                                     $warning 'the header specified an invalid encoded character';
  837.                                                     $warning_position $part['Position'$position 1;
  838.                                                     if($this->ignore_syntax_errors)
  839.                                                     {
  840.                                                         $this->SetPositionedWarning($warning$warning_position);
  841.                                                         $decoded .= '=';
  842.                                                         $position ++;
  843.                                                     }
  844.                                                     else
  845.                                                     {
  846.                                                         $error $warning;
  847.                                                         $error_position $warning_position;
  848.                                                         break 4;
  849.                                                     }
  850.                                                 }
  851.                                                 else
  852.                                                 {
  853.                                                     $decoded .= Chr($h);
  854.                                                     $position += 3;
  855.                                                 }
  856.                                                 break;
  857.                                             case '_':
  858.                                                 $decoded .= ' ';
  859.                                                 $position++;
  860.                                                 break;
  861.                                             default:
  862.                                                 $decoded .= $value[$position];
  863.                                                 $position++;
  864.                                                 break;
  865.                                         }
  866.                                     }
  867.                                     if(count($decoded_header)
  868.                                     && (!strcmp($decoded_header[$last count($decoded_header)-1]['Encoding']'ASCII'))
  869.                                     || !strcmp($decoded_header[$last]['Encoding']$encoding))
  870.                                     {
  871.                                         $decoded_header[$last]['Value'].= $decoded;
  872.                                         $decoded_header[$last]['Encoding']$encoding;
  873.                                     }
  874.                                     else
  875.                                     {
  876.                                         $decoded_header[]=array(
  877.                                             'Value'=>$decoded,
  878.                                             'Encoding'=>$encoding
  879.                                         );
  880.                                     }
  881.                                 }
  882.                                 break;
  883.                             case 'b':
  884.                                 $decoded=base64_decode(substr($value$start$end $start));
  885.                                 if($end <= $start
  886.                                 || GetType($decoded!= 'string'
  887.                                 || strlen($decoded== 0)
  888.                                 {
  889.                                     $warning 'the header specified an invalid base64 encoded text';
  890.                                     $warning_position $part['Position'$start;
  891.                                     if($this->ignore_syntax_errors)
  892.                                         $this->SetPositionedWarning($warning$warning_position);
  893.                                     else
  894.                                     {
  895.                                         $error $warning;
  896.                                         $error_position $warning_position;
  897.                                         break 2;
  898.                                     }
  899.                                 }
  900.                                 if(count($decoded_header)
  901.                                 && (!strcmp($decoded_header[$last count($decoded_header)-1]['Encoding']'ASCII'))
  902.                                 || !strcmp($decoded_header[$last]['Encoding']$encoding))
  903.                                 {
  904.                                     $decoded_header[$last]['Value'].= $decoded;
  905.                                     $decoded_header[$last]['Encoding']$encoding;
  906.                                 }
  907.                                 else
  908.                                 {
  909.                                     $decoded_header[]=array(
  910.                                         'Value'=>$decoded,
  911.                                         'Encoding'=>$encoding
  912.                                     );
  913.                                 }
  914.                                 break;
  915.                             default:
  916.                                 $error 'the header specified an unsupported encoding method';
  917.                                 $error_position $part['Position'$method;
  918.                                 break 2;
  919.                         }
  920.                         $position $end 2;
  921.                     }
  922.                     if(strlen($error)==0
  923.                     && count($decoded_header))
  924.                         $part['Decoded']=$decoded_header;
  925.                 }
  926.                 if($this->decode_bodies
  927.                 || $this->decode_headers)
  928.                 {
  929.                     switch($this->current_header)
  930.                     {
  931.                         case 'content-type:':
  932.                             $boundary $this->ParseParameters($part['Value']$type$parameters'boundary');
  933.                             $this->headers['Type'$type;
  934.                             if($this->decode_headers)
  935.                             {
  936.                                 $part['MainValue'$type;
  937.                                 $part['Parameters'$parameters;
  938.                             }
  939.                             if(!strcmp(strtok($type'/')'multipart'))
  940.                             {
  941.                                 $this->headers['Multipart'1;
  942.                                 if(strlen($boundary))
  943.                                     $this->headers['Boundary'$boundary;
  944.                                 else
  945.                                     return($this->SetPositionedError('multipart content-type header does not specify the boundary parameter'$part['Position']));
  946.                             }
  947.                             break;
  948.                         case 'content-transfer-encoding:':
  949.                             switch($this->headers['Encoding']=strtolower(trim($part['Value'])))
  950.                             {
  951.                                 case 'quoted-printable':
  952.                                     $this->headers['QuotedPrintable'1;
  953.                                     break;
  954.                                 case '7 bit':
  955.                                 case '8 bit':
  956.                                     if(!$this->SetPositionedWarning('"'.$this->headers['Encoding'].'" is an incorrect content transfer encoding type'$part['Position']))
  957.                                         return(0);
  958.                                 case '7bit':
  959.                                 case '8bit':
  960.                                 case 'binary':
  961.                                     break;
  962.                                 case 'base64':
  963.                                     $this->headers['Base64']=1;
  964.                                     break;
  965.                                 default:
  966.                                     if(!$this->SetPositionedWarning('decoding '.$this->headers['Encoding'].' encoded bodies is not yet supported'$part['Position']))
  967.                                         return(0);
  968.                             }
  969.                             break;
  970.                     }
  971.                 }
  972.                 break;
  973.             case 'BodyStart':
  974.                 if($this->decode_bodies
  975.                 && IsSet($this->headers['Multipart']))
  976.                 {
  977.                     $this->body_parser_state = MIME_PARSER_BODY_START;
  978.                     $this->body_buffer = '';
  979.                     $this->body_buffer_position = 0;
  980.                 }
  981.                 break;
  982.             case 'MessageEnd':
  983.                 if($this->decode_bodies
  984.                 && IsSet($this->headers['Multipart'])
  985.                 && $this->body_parser_state != MIME_PARSER_BODY_DONE)
  986.                     return($this->SetPositionedError('incomplete message body part'$part['Position']));
  987.                 break;
  988.             case 'BodyData':
  989.                 if($this->decode_bodies)
  990.                 {
  991.                     if(strlen($this->body_buffer)==0)
  992.                     {
  993.                         $this->body_buffer = $part['Data'];
  994.                         $this->body_offset = $part['Position'];
  995.                     }
  996.                     else
  997.                         $this->body_buffer .= $part['Data'];
  998.                     if(IsSet($this->headers['Multipart']))
  999.                     {
  1000.                         $boundary '--'.$this->headers['Boundary'];
  1001.                         switch($this->body_parser_state)
  1002.                         {
  1003.                             case MIME_PARSER_BODY_START:
  1004.                                 for($position $this->body_buffer_position; ;)
  1005.                                 {
  1006.                                     if(!$this->FindBodyLineBreak($position$break$line_break))
  1007.                                         return(1);
  1008.                                     $next $line_break strlen($break);
  1009.                                     if(!strcmp(substr($this->body_buffer$position$line_break $position)$boundary))
  1010.                                     {
  1011.                                         $part=array(
  1012.                                             'Type'=>'StartPart',
  1013.                                             'Part'=>$this->headers['Boundary'],
  1014.                                             'Position'=>$this->body_offset + $next
  1015.                                         );
  1016.                                         $this->parts[]=$part;
  1017.                                         UnSet($this->body_parser);
  1018.                                         $this->body_parser = new mime_parser_class;
  1019.                                         $this->body_parser->decode_bodies 1;
  1020.                                         $this->body_parser->decode_headers $this->decode_headers;
  1021.                                         $this->body_parser->mbox 0;
  1022.                                         $this->body_parser_state = MIME_PARSER_BODY_DATA;
  1023.                                         $this->body_buffer = substr($this->body_buffer$next);
  1024.                                         $this->body_offset += $next;
  1025.                                         $this->body_buffer_position = 0;
  1026.                                         break;
  1027.                                     }
  1028.                                     else
  1029.                                         $position $next;
  1030.                                 }
  1031.                             case MIME_PARSER_BODY_DATA:
  1032.                                 for($position $this->body_buffer_position; ;)
  1033.                                 {
  1034.                                     if(!$this->FindBodyLineBreak($position$break$line_break))
  1035.                                     {
  1036.                                         if($position 0)
  1037.                                         {
  1038.                                             if(!$this->body_parser->Parse(substr($this->body_buffer0$position)0))
  1039.                                                 return($this->SetError($this->body_parser->error));
  1040.                                             if(!$this->QueueBodyParts())
  1041.                                                 return(0);
  1042.                                         }
  1043.                                         $this->body_buffer = substr($this->body_buffer$position);
  1044.                                         $this->body_buffer_position = 0;
  1045.                                         $this->body_offset += $position;
  1046.                                         return(1);
  1047.                                     }
  1048.                                     $next $line_break strlen($break);
  1049.                                     $line substr($this->body_buffer$position$line_break $position);
  1050.                                     if(!strcmp($line$boundary))
  1051.                                     {
  1052.                                         if(!$this->body_parser->Parse(substr($this->body_buffer0$position)1))
  1053.                                             return($this->SetError($this->body_parser->error));
  1054.                                         if(!$this->QueueBodyParts())
  1055.                                             return(0);
  1056.                                         $part=array(
  1057.                                             'Type'=>'EndPart',
  1058.                                             'Part'=>$this->headers['Boundary'],
  1059.                                             'Position'=>$this->body_offset + $position
  1060.                                         );
  1061.                                         $this->parts[$part;
  1062.                                         $part=array(
  1063.                                             'Type'=>'StartPart',
  1064.                                             'Part'=>$this->headers['Boundary'],
  1065.                                             'Position'=>$this->body_offset + $next
  1066.                                         );
  1067.                                         $this->parts[$part;
  1068.                                         UnSet($this->body_parser);
  1069.                                         $this->body_parser = new mime_parser_class;
  1070.                                         $this->body_parser->decode_bodies 1;
  1071.                                         $this->body_parser->decode_headers $this->decode_headers;
  1072.                                         $this->body_parser->mbox 0;
  1073.                                         $this->body_buffer = substr($this->body_buffer$next);
  1074.                                         $this->body_buffer_position = 0;
  1075.                                         $this->body_offset += $next;
  1076.                                         $position=0;
  1077.                                         continue;
  1078.                                     }
  1079.                                     elseif(!strcmp($line$boundary.'--'))
  1080.                                     {
  1081.                                         if(!$this->body_parser->Parse(substr($this->body_buffer0$position)1))
  1082.                                             return($this->SetError($this->body_parser->error));
  1083.                                         if(!$this->QueueBodyParts())
  1084.                                             return(0);
  1085.                                         $part=array(
  1086.                                             'Type'=>'EndPart',
  1087.                                             'Part'=>$this->headers['Boundary'],
  1088.                                             'Position'=>$this->body_offset + $position
  1089.                                         );
  1090.                                         $this->body_buffer = substr($this->body_buffer$next);
  1091.                                         $this->body_buffer_position = 0;
  1092.                                         $this->body_offset += $next;
  1093.                                         $this->body_parser_state = MIME_PARSER_BODY_DONE;
  1094.                                         break 2;
  1095.                                     }
  1096.                                     $position $next;
  1097.                                 }
  1098.                                 break;
  1099.                             case MIME_PARSER_BODY_DONE:
  1100.                                 return(1);
  1101.                             default:
  1102.                                 return($this->SetPositionedError($this->state.' is not a valid body parser state'$this->body_buffer_position));
  1103.                         }
  1104.                     }
  1105.                     elseif(IsSet($this->headers['QuotedPrintable']))
  1106.                     {
  1107.                         for($end strlen($this->body_buffer)$decoded ''$position $this->body_buffer_position$position $end)
  1108.                         {
  1109.                             if(GetType($equal strpos($this->body_buffer'='$position))!='integer')
  1110.                             {
  1111.                                 $decoded .= substr($this->body_buffer$position);
  1112.                                 $position $end;
  1113.                                 break;
  1114.                             }
  1115.                             $next $equal 1;
  1116.                             switch($end $equal)
  1117.                             {
  1118.                                 case 1:
  1119.                                     $decoded .= substr($this->body_buffer$position$equal $position);
  1120.                                     $position $equal;
  1121.                                     break 2;
  1122.                                 case 2:
  1123.                                     $decoded .= substr($this->body_buffer$position$equal $position);
  1124.                                     if(!strcmp($this->body_buffer[$next],"\n"))
  1125.                                         $position $end;
  1126.                                     else
  1127.                                         $position $equal;
  1128.                                     break 2;
  1129.                             }
  1130.                             if(!strcmp(substr($this->body_buffer$next2)$break="\r\n")
  1131.                             || !strcmp($this->body_buffer[$next]$break="\n")
  1132.                             || !strcmp($this->body_buffer[$next]$break="\r"))
  1133.                             {
  1134.                                 $decoded .= substr($this->body_buffer$position$equal $position);
  1135.                                 $position $next strlen($break);
  1136.                                 continue;
  1137.                             }
  1138.                             $decoded .= substr($this->body_buffer$position$equal $position);
  1139.                             $h HexDec($hex=strtolower(substr($this->body_buffer$next2)));
  1140.                             if(strcmp(sprintf('%02x'$h)$hex))
  1141.                             {
  1142.                                 if(!$this->SetPositionedWarning('the body specified an invalid quoted-printable encoded character'$this->body_offset + $next))
  1143.                                     return(0);
  1144.                                 $decoded.='=';
  1145.                                 $position=$next;
  1146.                             }
  1147.                             else
  1148.                             {
  1149.                                 $decoded .= Chr($h);
  1150.                                 $position $equal 3;
  1151.                             }
  1152.                         }
  1153.                         if(strlen($decoded)==0)
  1154.                         {
  1155.                             $this->body_buffer_position = $position;
  1156.                             return(1);
  1157.                         }
  1158.                         $part['Data'$decoded;
  1159.                         $this->body_buffer = substr($this->body_buffer$position);
  1160.                         $this->body_buffer_position = 0;
  1161.                         $this->body_offset += $position;
  1162.                     }
  1163.                     elseif(IsSet($this->headers['Base64']))
  1164.                     {
  1165.                         $part['Data'base64_decode($this->body_buffer_position ? substr($this->body_buffer,$this->body_buffer_position$this->body_buffer);
  1166.                         $this->body_offset += strlen($this->body_buffer$this->body_buffer_position;
  1167.                         $this->body_buffer_position = 0;
  1168.                         $this->body_buffer = '';
  1169.                     }
  1170.                     else
  1171.                     {
  1172.                         $part['Data'substr($this->body_buffer$this->body_buffer_position);
  1173.                         $this->body_buffer_position = 0;
  1174.                         $this->body_buffer = '';
  1175.                     }
  1176.                 }
  1177.                 break;
  1178.         }
  1179.         $this->parts[]=$part;
  1180.         return(1);
  1181.     }
  1182.  
  1183.     Function DecodeStream($parameters&$end_of_message&$decoded)
  1184.     {
  1185.         $end_of_message 1;
  1186.         $state MIME_MESSAGE_START;
  1187.         for(;;)
  1188.         {
  1189.             if(!$this->GetPart($part$end))
  1190.                 return(0);
  1191.             if($end)
  1192.             {
  1193.                 if(IsSet($parameters['File']))
  1194.                 {
  1195.                     $end_of_data feof($this->file);
  1196.                     if($end_of_data)
  1197.                         break;
  1198.                     $data @fread($this->file8000);
  1199.                     if(GetType($data)!='string')
  1200.                         return($this->SetPHPError('could not read the message file'$php_errormsg));
  1201.                     $end_of_data feof($this->file);
  1202.                 }
  1203.                 else
  1204.                 {
  1205.                     $end_of_data=($this->position>=strlen($parameters['Data']));
  1206.                     if($end_of_data)
  1207.                         break;
  1208.                     $data substr($parameters['Data']$this->position);
  1209.                     $end_of_data 1;
  1210.                     $this->position = strlen($parameters['Data']);
  1211.                 }
  1212.                 if(!$this->Parse($data$end_of_data))
  1213.                     return(0);
  1214.                 continue;
  1215.             }
  1216.             $type $part['Type'];
  1217.             switch($state)
  1218.             {
  1219.                 case MIME_MESSAGE_START:
  1220.                     switch($type)
  1221.                     {
  1222.                         case 'MessageStart':
  1223.                             $decoded=array(
  1224.                                 'Headers'=>array(),
  1225.                                 'Parts'=>array()
  1226.                             );
  1227.                             $end_of_message 0;
  1228.                             $state MIME_MESSAGE_GET_HEADER_NAME;
  1229.                             continue 3;
  1230.                     }
  1231.                     break;
  1232.  
  1233.                 case MIME_MESSAGE_GET_HEADER_NAME:
  1234.                     switch($type)
  1235.                     {
  1236.                         case 'HeaderName':
  1237.                             $header strtolower($part['Name']);
  1238.                             $state MIME_MESSAGE_GET_HEADER_VALUE;
  1239.                             continue 3;
  1240.                         case 'BodyStart':
  1241.                             $state MIME_MESSAGE_GET_BODY;
  1242.                             $part_number 0;
  1243.                             continue 3;
  1244.                     }
  1245.                     break;
  1246.  
  1247.                 case MIME_MESSAGE_GET_HEADER_VALUE:
  1248.                     switch($type)
  1249.                     {
  1250.                         case 'HeaderValue':
  1251.                             $value trim($part['Value']);
  1252.                             if(!IsSet($decoded['Headers'][$header]))
  1253.                             {
  1254.                                 $h 0;
  1255.                                 $decoded['Headers'][$header]=$value;
  1256.                                 if($this->extract_addresses
  1257.                                 && IsSet($this->address_headers[$header]))
  1258.                                     $decoded['HeaderPositions'][$header$part['Position'];
  1259.                             }
  1260.                             elseif(GetType($decoded['Headers'][$header])=='string')
  1261.                             {
  1262.                                 $h 1;
  1263.                                 $decoded['Headers'][$header]=array($decoded['Headers'][$header]$value);
  1264.                             }
  1265.                             else
  1266.                             {
  1267.                                 $h count($decoded['Headers'][$header]);
  1268.                                 $decoded['Headers'][$header][]=$value;
  1269.                             }
  1270.                             if(IsSet($part['Decoded'])
  1271.                             && (count($part['Decoded'])>1
  1272.                             || strcmp($part['Decoded'][0]['Encoding'],'ASCII')
  1273.                             || strcmp($valuetrim($part['Decoded'][0]['Value']))))
  1274.                             {
  1275.                                 $p=$part['Decoded'];
  1276.                                 $p[0]['Value']=ltrim($p[0]['Value']);
  1277.                                 $last=count($p)-1;
  1278.                                 $p[$last]['Value']=rtrim($p[$last]['Value']);
  1279.                                 $decoded['DecodedHeaders'][$header][$h]=$p;
  1280.                             }
  1281.                             switch($header)
  1282.                             {
  1283.                                 case 'content-disposition:':
  1284.                                     $filename='filename';
  1285.                                     break;
  1286.                                 case 'content-type:':
  1287.                                     if(!IsSet($decoded['FileName']))
  1288.                                     {
  1289.                                         $filename='name';
  1290.                                         break;
  1291.                                     }
  1292.                                 default:
  1293.                                     $filename='';
  1294.                                     break;
  1295.                             }
  1296.                             if(strlen($filename))
  1297.                             {
  1298.                                 $this->ParseStructuredHeader($value$type$header_parameters$character_sets$languages);
  1299.                                 if(IsSet($header_parameters[$filename]))
  1300.                                 {
  1301.                                     $decoded['FileName']=$header_parameters[$filename];
  1302.                                     if(IsSet($character_sets[$filename])
  1303.                                     && strlen($character_sets[$filename]))
  1304.                                         $decoded['FileNameCharacterSet']=$character_sets[$filename];
  1305.                                     if(IsSet($character_sets['language'])
  1306.                                     && strlen($character_sets['language']))
  1307.                                         $decoded['FileNameCharacterSet']=$character_sets[$filename];
  1308.                                     if(!strcmp($header'content-disposition:'))
  1309.                                         $decoded['FileDisposition']=$type;
  1310.                                 }
  1311.                             }
  1312.                             $state MIME_MESSAGE_GET_HEADER_NAME;
  1313.                             continue 3;
  1314.                     }
  1315.                     break;
  1316.  
  1317.                 case MIME_MESSAGE_GET_BODY:
  1318.                     switch($type)
  1319.                     {
  1320.                         case 'BodyData':
  1321.                             if(IsSet($parameters['SaveBody']))
  1322.                             {
  1323.                                 if(!IsSet($decoded['BodyFile']))
  1324.                                 {
  1325.                                     $directory_separator=(defined('DIRECTORY_SEPARATOR'DIRECTORY_SEPARATOR '/');
  1326.                                     $path (strlen($parameters['SaveBody']($parameters['SaveBody'].(strcmp($parameters['SaveBody'][strlen($parameters['SaveBody'])-1]$directory_separator$directory_separator '')) '').strval($this->body_part_number);
  1327.                                     if(!($this->body_file = fopen($path'wb')))
  1328.                                         return($this->SetPHPError('could not create file '.$path.' to save the message body part'$php_errormsg));
  1329.                                     $decoded['BodyFile'$path;
  1330.                                     $decoded['BodyPart'$this->body_part_number;
  1331.                                     $decoded['BodyLength'0;
  1332.                                     $this->body_part_number++;
  1333.                                 }
  1334.                                 if(strlen($part['Data'])
  1335.                                 && !fwrite($this->body_file$part['Data']))
  1336.                                 {
  1337.                                     $this->SetPHPError('could not save the message body part to file '.$decoded['BodyFile']$php_errormsg);
  1338.                                     fclose($this->body_file);
  1339.                                     @unlink($decoded['BodyFile']);
  1340.                                     return(0);
  1341.                                 }
  1342.                             }
  1343.                             elseif(IsSet($parameters['SkipBody']))
  1344.                             {
  1345.                                 if(!IsSet($decoded['BodyPart']))
  1346.                                 {
  1347.                                     $decoded['BodyPart'$this->body_part_number;
  1348.                                     $decoded['BodyLength'0;
  1349.                                     $this->body_part_number++;
  1350.                                 }
  1351.                             }
  1352.                             else
  1353.                             {
  1354.                                 if(IsSet($decoded['Body']))
  1355.                                     $decoded['Body'].=$part['Data'];
  1356.                                 else
  1357.                                 {
  1358.                                     $decoded['Body']=$part['Data'];
  1359.                                     $decoded['BodyPart'$this->body_part_number;
  1360.                                     $decoded['BodyLength'0;
  1361.                                     $this->body_part_number++;
  1362.                                 }
  1363.                             }
  1364.                             $decoded['BodyLength'+= strlen($part['Data']);
  1365.                             continue 3;
  1366.                         case 'StartPart':
  1367.                             if(!$this->DecodeStream($parameters$end_of_part$decoded_part))
  1368.                                 return(0);
  1369.                             $decoded['Parts'][$part_number]=$decoded_part;
  1370.                             $part_number++;
  1371.                             $state MIME_MESSAGE_GET_BODY_PART;
  1372.                             continue 3;
  1373.                         case 'MessageEnd':
  1374.                             if(IsSet($decoded['BodyFile']))
  1375.                                 fclose($this->body_file);
  1376.                             return(1);
  1377.                     }
  1378.                     break;
  1379.  
  1380.                 case MIME_MESSAGE_GET_BODY_PART:
  1381.                     switch($type)
  1382.                     {
  1383.                         case 'EndPart':
  1384.                             $state MIME_MESSAGE_GET_BODY;
  1385.                             continue 3;
  1386.                     }
  1387.                     break;
  1388.             }
  1389.             return($this->SetError('unexpected decoded message part type '.$type.' in state '.$state));
  1390.         }
  1391.         return(1);
  1392.     }
  1393.  
  1394.     /* Public functions */
  1395.  
  1396.     Function Parse($data$end)
  1397.     {
  1398.         if(strlen($this->error))
  1399.             return(0);
  1400.         if($this->state==MIME_PARSER_END)
  1401.             return($this->SetError('the parser already reached the end'));
  1402.         $this->buffer .= $data;
  1403.         do
  1404.         {
  1405.             Unset($part);
  1406.             if(!$this->ParsePart($end$part$need_more_data))
  1407.                 return(0);
  1408.             if(IsSet($part)
  1409.             && !$this->DecodePart($part))
  1410.                 return(0);
  1411.         }
  1412.         while(!$need_more_data
  1413.         && $this->state!=MIME_PARSER_END);
  1414.         if($end
  1415.         && $this->state!=MIME_PARSER_END)
  1416.             return($this->SetError('reached a premature end of data'));
  1417.         if($this->buffer_position>0)
  1418.         {
  1419.             $this->offset += $this->buffer_position;
  1420.             $this->buffer = substr($this->buffer$this->buffer_position);
  1421.             $this->buffer_position = 0;
  1422.         }
  1423.         return(1);
  1424.     }
  1425.  
  1426.     Function ParseFile($file)
  1427.     {
  1428.         if(strlen($this->error))
  1429.             return(0);
  1430.         if(!($stream @fopen($file'r')))
  1431.             return($this->SetPHPError('Could not open the file '.$file$php_errormsg));
  1432.         for($end 0;!$end;)
  1433.         {
  1434.             if(!($data @fread($stream8000)))
  1435.             {
  1436.                 $this->SetPHPError('Could not open the file '.$file$php_errormsg);
  1437.                 fclose($stream);
  1438.                 return(0);
  1439.             }
  1440.             $end=feof($stream);
  1441.             if(!$this->Parse($data$end))
  1442.             {
  1443.                 fclose($stream);
  1444.                 return(0);
  1445.             }
  1446.         }
  1447.         fclose($stream);
  1448.         return(1);
  1449.     }
  1450.  
  1451.     Function GetPart(&$part&$end)
  1452.     {
  1453.         $end ($this->part_position >= count($this->parts));
  1454.         if($end)
  1455.         {
  1456.             if($this->part_position)
  1457.             {
  1458.                 $this->part_position = 0;
  1459.                 $this->parts = array();
  1460.             }
  1461.         }
  1462.         else
  1463.         {
  1464.             $part $this->parts[$this->part_position];
  1465.             $this->part_position ++;
  1466.         }
  1467.         return(1);
  1468.     }
  1469.  
  1470. /*
  1471. {metadocument}
  1472.     <function>
  1473.         <name>Decode</name>
  1474.         <type>BOOLEAN</type>
  1475.         <documentation>
  1476.             <purpose>Parse and decode message data and retrieve its structure.</purpose>
  1477.             <usage>Pass an array to the <argumentlink>
  1478.                     <function>Decode</function>
  1479.                     <argument>parameters</argument>
  1480.                 </argumentlink>
  1481.                 parameter to define whether the message data should be read and
  1482.                 parsed from a file or a data string, as well additional parsing
  1483.                 options. The <argumentlink>
  1484.                     <function>Decode</function>
  1485.                     <argument>decoded</argument>
  1486.                 </argumentlink> returns the
  1487.                 data structure of the parsed messages.</usage>
  1488.             <returnvalue>This function returns <booleanvalue>1</booleanvalue> if
  1489.                 the specified message data is parsed successfully. Otherwise,
  1490.                 check the variables <variablelink>error</variablelink> and
  1491.                 <variablelink>error_position</variablelink> to determine what
  1492.                 error occurred and the relevant message position.</returnvalue>
  1493.         </documentation>
  1494.         <argument>
  1495.             <name>parameters</name>
  1496.             <type>HASH</type>
  1497.             <documentation>
  1498.                 <purpose>Associative array to specify parameters for the message
  1499.                     data parsing and decoding operation. Here follows the list of
  1500.                     supported parameters that should be used as indexes of the
  1501.                     array:<paragraphbreak />
  1502.                     <tt>File</tt><paragraphbreak />
  1503.                     Name of the file from which the message data will be read. It
  1504.                     may be the name of a file stream or a remote URL, as long as
  1505.                     your PHP installation is configured to allow accessing remote
  1506.                     files with the <tt>fopen()</tt> function.<paragraphbreak />
  1507.                     <tt>Data</tt><paragraphbreak />
  1508.                     String that specifies the message data. This should be used
  1509.                     as alternative data source for passing data available in memory,
  1510.                     like for instance messages stored in a database that was queried
  1511.                     dynamically and the message data was fetched into a string
  1512.                     variable.<paragraphbreak />
  1513.                     <tt>SaveBody</tt><paragraphbreak />
  1514.                     If this parameter is specified, the message body parts are saved
  1515.                     to files. The path of the directory where the files are saved is
  1516.                     defined by this parameter value. The information about the
  1517.                     message body part structure is returned by the <argumentlink>
  1518.                         <function>Decode</function>
  1519.                         <argument>decoded</argument>
  1520.                     </argumentlink> argument, but it just returns the body data part
  1521.                     file name instead of the actual body data. It is recommended for
  1522.                     retrieving messages larger than the available memory. The names
  1523.                     of the body part files are numbers starting from
  1524.                     <stringvalue>1</stringvalue>.<paragraphbreak />
  1525.                     <tt>SkipBody</tt><paragraphbreak />
  1526.                     If this parameter is specified, the message body parts are
  1527.                     skipped. This means the information about the message body part
  1528.                     structure is returned by the <argumentlink>
  1529.                         <function>Decode</function>
  1530.                         <argument>decoded</argument>
  1531.                     </argumentlink> but it does not return any body data. It is
  1532.                     recommended just for parsing messages without the need to
  1533.                     retrieve the message body part data.</purpose>
  1534.             </documentation>
  1535.         </argument>
  1536.         <argument>
  1537.             <name>decoded</name>
  1538.             <type>ARRAY</type>
  1539.             <out />
  1540.             <documentation>
  1541.                 <purpose>Retrieve the structure of the parsed message headers and
  1542.                     body data.<paragraphbreak />
  1543.                     The argument is used to return by reference an array of message
  1544.                     structure definitions. Each array entry refers to the structure
  1545.                     of each message that is found and parsed successfully.<paragraphbreak />
  1546.                     Each message entry consists of an associative array with several
  1547.                     entries that describe the message structure. Here follows the
  1548.                     list of message structure entries names and the meaning of the
  1549.                     respective values:<paragraphbreak />
  1550.                     <tt>Headers</tt><paragraphbreak />
  1551.                     Associative array that returns the list of all the message
  1552.                     headers. The array entries are the header names mapped to
  1553.                     lower case, including the end colon. The array values are the
  1554.                     respective header raw values without any start or trailing white
  1555.                     spaces. Long header values split between multiple message lines
  1556.                     are gathered in single string without line breaks. If an header
  1557.                     with the same name appears more than once in the message, the
  1558.                     respective value is an array with the values of all of the
  1559.                     header occurrences.<paragraphbreak />
  1560.                     <tt>DecodedHeaders</tt><paragraphbreak />
  1561.                     Associative array that returns the list of all the encoded
  1562.                     message headers when the
  1563.                     <variablelink>decode_headers</variablelink> variable is set. The
  1564.                     array entries are the header names mapped to lower case,
  1565.                     including the end colon. The array values are also arrays that
  1566.                     list only the occurrences of the header that originally were
  1567.                     encoded. Each entry of the decoded header array contains more
  1568.                     associative arrays that describe each part of the decoded
  1569.                     header. Each of those associative arrays have an entry named
  1570.                     <tt>Value</tt> that contains the decoded header part value, and
  1571.                     another entry named <tt>Encoding</tt> that specifies the
  1572.                     character set encoding of the value in upper case.<paragraphbreak />
  1573.                     <tt>ExtractedAddresses</tt><paragraphbreak />
  1574.                     If the <variablelink>extract_addresses</variablelink> variable
  1575.                     is set to <booleanvalue>1</booleanvalue>, this entry is set to an
  1576.                     associative array with the addresses found in the headers
  1577.                     specified by the <variablelink>address_headers</variablelink>
  1578.                     variable.<paragraphbreak />
  1579.                     The parsed addresses found on each header are returned as an
  1580.                     array with the format of the <link>
  1581.                         <data>addresses</data>
  1582.                         <url>rfc822_addresses_class.html#argument_ParseAddressList_addresses</url>
  1583.                     </link> argument of the <link>
  1584.                         <data>ParseAddressList</data>
  1585.                         <url>rfc822_addresses_class.html#function_ParseAddressList</url>
  1586.                     </link> function of the <link>
  1587.                         <data>RFC 822 addresses</data>
  1588.                         <url>rfc822_addresses_class.html</url>
  1589.                     </link> class.<paragraphbreak />
  1590.                     <tt>Parts</tt><paragraphbreak />
  1591.                     If this message content type is multipart, this entry is an
  1592.                     array that describes each of the parts contained in the message
  1593.                     body. Each message part is described by an associative array
  1594.                     with the same structure of a complete message
  1595.                     definition.<paragraphbreak />
  1596.                     <tt>Body</tt><paragraphbreak />
  1597.                     String with the decoded data contained in the message body. If
  1598.                     the <tt>SaveBody</tt> or <tt>SkipBody</tt> parameters are
  1599.                     defined, the <tt>Body</tt> entry is not set.<paragraphbreak />
  1600.                     <tt>BodyFile</tt><paragraphbreak />
  1601.                     Name of the file to which the message body data was saved when
  1602.                     the <tt>SaveBody</tt> parameter is defined.<paragraphbreak />
  1603.                     <tt>BodyLength</tt><paragraphbreak />
  1604.                     Length of the current decoded body part.<paragraphbreak />
  1605.                     <tt>BodyPart</tt><paragraphbreak />
  1606.                     Number of the current message body part.<paragraphbreak />
  1607.                     <tt>FileName</tt><paragraphbreak />
  1608.                     Name of the file for body parts composed from
  1609.                     files.<paragraphbreak />
  1610.                     <tt>FileNameCharacterSet</tt><paragraphbreak />
  1611.                     Character set encoding for file parts with names that may
  1612.                     include non-ASCII characters.<paragraphbreak />
  1613.                     <tt>FileNameLanguage</tt><paragraphbreak />
  1614.                     Language of file parts with names that may include non-ASCII
  1615.                     characters.<paragraphbreak />
  1616.                     <tt>FileDisposition</tt><paragraphbreak />
  1617.                     Disposition of parts that files. It may be either
  1618.                     <tt><stringvalue>inline</stringvalue></tt> for file parts to be
  1619.                     displayed with the message, or
  1620.                     <tt><stringvalue>attachment</stringvalue></tt> otherwise.</purpose>
  1621.             </documentation>
  1622.         </argument>
  1623.         <do>
  1624. {/metadocument}
  1625. */
  1626.     Function Decode($parameters&$decoded)
  1627.     {
  1628.         if(IsSet($parameters['File']))
  1629.         {
  1630.             if(!($this->file = @fopen($parameters['File']'r')))
  1631.                 return($this->SetPHPError('could not open the message file to decode '.$parameters['File']$php_errormsg));
  1632.         }
  1633.         elseif(IsSet($parameters['Data']))
  1634.             $this->position = 0;
  1635.         else
  1636.             return($this->SetError('it was not specified a valid message to decode'));
  1637.         $this->warnings = $decoded array();
  1638.         $this->ResetParserState();
  1639.         $addresses new rfc822_addresses_class;
  1640.         $addresses->ignore_syntax_errors $this->ignore_syntax_errors;
  1641.         for($message 0($success $this->DecodeStream($parameters$end_of_message$decoded_message)) && !$end_of_message$message++)
  1642.         {
  1643.             if($this->extract_addresses)
  1644.             {
  1645.                 $headers $decoded_message['Headers'];
  1646.                 $positions $decoded_message['HeaderPositions'];
  1647.                 $th count($headers);
  1648.                 for(Reset($headers)$h 0$h<$thNext($headers)++$h)
  1649.                 {
  1650.                     $header Key($headers);
  1651.                     if(IsSet($this->address_headers[$header])
  1652.                     && $this->address_headers[$header])
  1653.                     {
  1654.                         $values (GetType($headers[$header]== 'array' $headers[$headerarray($headers[$header]));
  1655.                         $p (GetType($positions[$header]== 'array' $positions[$headerarray($positions[$header]));
  1656.                         $tv count($values);
  1657.                         for($v 0$v<$tv++$v)
  1658.                         {
  1659.                             if($addresses->ParseAddressList($values[$v]$a))
  1660.                             {
  1661.                                 if($v==0)
  1662.                                     $decoded_message['ExtractedAddresses'][$header$a;
  1663.                                 else
  1664.                                 {
  1665.                                     $tl count($a);
  1666.                                     for($l 0$l<$tl++$l)
  1667.                                         $decoded_message['ExtractedAddresses'][$header][$a[$l];
  1668.                                 }
  1669.                                 $tw count($addresses->warnings);
  1670.                                 for($w 0Reset($addresses->warnings)$w $twNext($addresses->warnings)$w++)
  1671.                                 {
  1672.                                     $warning Key($addresses->warnings);
  1673.                                     if(!$this->SetPositionedWarning('Address extraction warning from header '.$header.' '.$addresses->warnings[$warning]$warning $p[$v]))
  1674.                                         return(0);
  1675.                                 }
  1676.                             }
  1677.                             elseif(!$this->SetPositionedWarning('Address extraction error from header '.$header.' '.$addresses->error$addresses->error_position $p[$v]))
  1678.                                 return(0);
  1679.                         }
  1680.                     }
  1681.                 }
  1682.                 UnSet($decoded_message['HeaderPositions']);
  1683.             }
  1684.             $decoded[$message]=$decoded_message;
  1685.         }
  1686.         if(IsSet($parameters['File']))
  1687.             fclose($this->file);
  1688.         return($success);
  1689.     }
  1690. /*
  1691. {metadocument}
  1692.         </do>
  1693.     </function>
  1694. {/metadocument}
  1695. */
  1696.  
  1697.     Function CopyAddresses($message&$results$header)
  1698.     {
  1699.         if(!IsSet($message['Headers'][$header]))
  1700.             return;
  1701.         if(!IsSet($message['ExtractedAddresses'][$header]))
  1702.         {
  1703.             $parser new rfc822_addresses_class;
  1704.             $parser->ignore_syntax_errors $this->ignore_syntax_errors;
  1705.             $values (GetType($message['Headers'][$header]== 'array' $message['Headers'][$headerarray($message['Headers'][$header]));
  1706.             $tv count($values);
  1707.             $addresses array();
  1708.             for($v 0$v<$tv++$v)
  1709.             {
  1710.                 if($parser->ParseAddressList($values[$v]$a))
  1711.                 {
  1712.                     if($v==0)
  1713.                         $addresses $a;
  1714.                     else
  1715.                     {
  1716.                         $tl count($a);
  1717.                         for($l 0$l<$tl++$l)
  1718.                             $addresses[$a[$l];
  1719.                     }
  1720.                 }
  1721.             }
  1722.         }
  1723.         else
  1724.             $addresses $message['ExtractedAddresses'][$header];
  1725.         if(count($addresses))
  1726.             $results[ucfirst(substr($header0strlen($header-1))$addresses;
  1727.     }
  1728.  
  1729.     Function ReadMessageBody($message&$body$prefix)
  1730.     {
  1731.         if(IsSet($message[$prefix]))
  1732.             $body $message[$prefix];
  1733.         elseif(IsSet($message[$prefix.'File']))
  1734.         {
  1735.             $path $message[$prefix.'File'];
  1736.             if(!($file @fopen($path'rb')))
  1737.                 return($this->SetPHPError('could not open the message body file '.$path$php_errormsg));
  1738.             for($body ''$end 0;!$end;)
  1739.             {
  1740.                 if(!($data @fread($file8000)))
  1741.                 {
  1742.                     $this->SetPHPError('Could not open the message body file '.$path$php_errormsg);
  1743.                     fclose($stream);
  1744.                     return(0);
  1745.                 }
  1746.                 $end=feof($file);
  1747.                 $body.=$data;
  1748.             }
  1749.             fclose($file);
  1750.         }
  1751.         else
  1752.             $body '';
  1753.         return(1);
  1754.     }
  1755. /*
  1756. {metadocument}
  1757.     <function>
  1758.         <name>Analyze</name>
  1759.         <type>BOOLEAN</type>
  1760.         <documentation>
  1761.             <purpose>Analyze a parsed message to describe its contents.</purpose>
  1762.             <usage>Pass an array to the <argumentlink>
  1763.                     <function>Analyze</function>
  1764.                     <argument>message</argument>
  1765.                 </argumentlink>
  1766.                 parameter with the decoded message array structure returned by the
  1767.                 <functionlink>Decode</functionlink> function. The <argumentlink>
  1768.                     <function>Analyze</function>
  1769.                     <argument>results</argument>
  1770.                 </argumentlink> returns details about the type of message that was
  1771.                 analyzed and its contents.</usage>
  1772.             <returnvalue>This function returns <booleanvalue>1</booleanvalue> if
  1773.                 the specified message is analyzed successfully. Otherwise,
  1774.                 check the variables <variablelink>error</variablelink> and
  1775.                 <variablelink>error_position</variablelink> to determine what
  1776.                 error occurred.</returnvalue>
  1777.         </documentation>
  1778.         <argument>
  1779.             <name>message</name>
  1780.             <type>HASH</type>
  1781.             <documentation>
  1782.                 <purpose>Pass an associative array with the definition of an
  1783.                     individual message returned by the <argumentlink>
  1784.                     <function>Decode</function>
  1785.                     <argument>decoded</argument>
  1786.                 </argumentlink> argument of the
  1787.                 <functionlink>Decode</functionlink> function..</purpose>
  1788.             </documentation>
  1789.         </argument>
  1790.         <argument>
  1791.             <name>results</name>
  1792.             <type>HASH</type>
  1793.             <out />
  1794.             <documentation>
  1795.                 <purpose>Returns an associative array with the results of the
  1796.                     analysis. Some types of entries are returned for all types of
  1797.                     analyzed messages. Other entries are specific to each type of
  1798.                     message.<paragraphbreak />
  1799.                     <tt>Type</tt><paragraphbreak />
  1800.                     Type of message that was analyzed. Currently it supports the
  1801.                     types: <tt>binary</tt>, <tt>text</tt>, <tt>html</tt>,
  1802.                     <tt>video</tt>, <tt>image</tt>, <tt>audio</tt>, <tt>zip</tt>,
  1803.                     <tt>pdf</tt>, <tt>postscript</tt>, <tt>ms-word</tt>,
  1804.                     <tt>ms-excel</tt>, <tt>ms-powerpoint</tt>, <tt>ms-tnef</tt>,
  1805.                     <tt>odf-writer</tt>, <tt>signature</tt>, <tt>report-type</tt>,
  1806.                     <tt>delivery-status</tt> and <tt>message</tt>.<paragraphbreak />
  1807.                     <tt>SubType</tt><paragraphbreak />
  1808.                     Name of the variant of the message type format.<paragraphbreak />
  1809.                     <tt>Description</tt><paragraphbreak />
  1810.                     Human readable description in English of the message type.<paragraphbreak />
  1811.                     <paragraphbreak />
  1812.                     <paragraphbreak />
  1813.                     <paragraphbreak />
  1814.                     <b>From message headers:</b><paragraphbreak />
  1815.                     <tt>Encoding</tt><paragraphbreak />
  1816.                     Character set encoding of the message part.<paragraphbreak />
  1817.                     <tt>Subject</tt><paragraphbreak />
  1818.                     The message subject.<paragraphbreak />
  1819.                     <tt>SubjectEncoding</tt><paragraphbreak />
  1820.                     Character set encoding of the message subject.<paragraphbreak />
  1821.                     <tt>Date</tt><paragraphbreak />
  1822.                     The message date.<paragraphbreak />
  1823.                     <tt>From</tt><paragraphbreak />
  1824.                     <tt>To</tt><paragraphbreak />
  1825.                     <tt>Cc</tt><paragraphbreak />
  1826.                     <tt>Bcc</tt><paragraphbreak />
  1827.                     Array of e-mail addresses found in the <tt>From</tt>,
  1828.                     <tt>To</tt>, <tt>Cc</tt>, <tt>Bcc</tt>.<paragraphbreak />
  1829.                     Each of the entries consists of an associative array with an
  1830.                     entry named <tt>address</tt> with the e-mail address and
  1831.                     optionally another named <tt>name</tt> with the associated
  1832.                     name.<paragraphbreak />
  1833.                     <paragraphbreak />
  1834.                     <paragraphbreak />
  1835.                     <b>For content message parts:</b><paragraphbreak />
  1836.                     <paragraphbreak />
  1837.                     <tt>Data</tt><paragraphbreak />
  1838.                     String of data of the message part.<paragraphbreak />
  1839.                     <tt>DataFile</tt><paragraphbreak />
  1840.                     File with data of the message part.<paragraphbreak />
  1841.                     <tt>DataLength</tt><paragraphbreak />
  1842.                     Length of the data of the message part.<paragraphbreak />
  1843.                     <paragraphbreak />
  1844.                     <paragraphbreak />
  1845.                     <paragraphbreak />
  1846.                     <b>For message with embedded files:</b><paragraphbreak />
  1847.                     <paragraphbreak />
  1848.                     <tt>FileName</tt><paragraphbreak />
  1849.                     Original name of the file.<paragraphbreak />
  1850.                     <tt>ContentID</tt><paragraphbreak />
  1851.                     Content identifier of the file to be used in references from
  1852.                     other message parts.<paragraphbreak />
  1853.                     For instance, an HTML message may reference images embedded in
  1854.                     the message using URLs that start with the
  1855.                     <stringvalue>cid:</stringvalue> followed by the content
  1856.                     identifier of the embedded image file part.<paragraphbreak />
  1857.                     <tt>Disposition</tt><paragraphbreak />
  1858.                     Information of whether the embedded file should be displayed
  1859.                     inline when the message is presented, or it is an attachment
  1860.                     file.<paragraphbreak />
  1861.                     <paragraphbreak />
  1862.                     <paragraphbreak />
  1863.                     <b>For composite message:</b><paragraphbreak />
  1864.                     <paragraphbreak />
  1865.                     <tt>Attachments</tt><paragraphbreak />
  1866.                     List of files attached to the message.<paragraphbreak />
  1867.                     <tt>Alternative</tt><paragraphbreak />
  1868.                     List of alternative message parts that can be displayed if the
  1869.                     main message type is not supported by the program displaying
  1870.                     the message.<paragraphbreak />
  1871.                     <tt>Related</tt><paragraphbreak />
  1872.                     List of message parts related with the main message type.<paragraphbreak />
  1873.                     It may list for instance embedded images or CSS files related
  1874.                     with an HTML message type.<paragraphbreak />
  1875.                     <paragraphbreak />
  1876.                     <paragraphbreak />
  1877.                     <b>For bounced messages or other types of delivery status report
  1878.                     messages:</b><paragraphbreak />
  1879.                     <paragraphbreak />
  1880.                     <tt>Recipients</tt><paragraphbreak />
  1881.                     List of recipients of the original message.<paragraphbreak />
  1882.                     Each entry contains an associative array that may have the
  1883.                     entries: <tt>Recipient</tt> with the original recipient address,
  1884.                     <tt>Action</tt> with the name action that triggered the delivery
  1885.                     status report, <tt>Status</tt> with the code of the status of
  1886.                     the message delivery.<paragraphbreak />
  1887.                     <tt>Response</tt><paragraphbreak />
  1888.                     Human readable response sent by the server the originated the
  1889.                     report.<paragraphbreak />
  1890.                     </purpose>
  1891.             </documentation>
  1892.         </argument>
  1893.         <do>
  1894. {/metadocument}
  1895. */
  1896.     Function Analyze($message&$results)
  1897.     {
  1898.         $results array();
  1899.         $content_type (IsSet($message['Headers']['content-type:']$message['Headers']['content-type:''text/plain');
  1900.         $disposition $this->ParseParameters($content_type$content_type$parameters'disposition');
  1901.         $type $this->Tokenize($content_type'/');
  1902.         $sub_type $this->Tokenize(';');
  1903.         $copy_body 1;
  1904.         $tolerate_unrecognized 1;
  1905.         switch($type)
  1906.         {
  1907.             case 'multipart':
  1908.                 $tolerate_unrecognized 0;
  1909.                 $copy_body 0;
  1910.                 $lp count($message['Parts']);
  1911.                 if($lp == 0)
  1912.                     return($this->SetError($this->decode_bodies ? 'No parts were found in the '.$content_type.' part message' 'It is not possible to analyze multipart messages without parsing the contained message parts. Please set the decode_bodies variable to 1 before parsing the message'));
  1913.                 $parts array();
  1914.                 for($p 0$p $lp++$p)
  1915.                 {
  1916.                     if(!$this->Analyze($message['Parts'][$p]$parts[$p]))
  1917.                         return(0);
  1918.                 }
  1919.                 switch($sub_type)
  1920.                 {
  1921.                     case 'alternative':
  1922.                         $p $lp;
  1923.                         $results $parts[--$p];
  1924.                         for(--$p $p >=--$p)
  1925.                             $results['Alternative'][$parts[$p];
  1926.                         break;
  1927.  
  1928.                     case 'related':
  1929.                         $results $parts[0];
  1930.                         for($p 1$p $lp++$p)
  1931.                             $results['Related'][$parts[$p];
  1932.                         break;
  1933.  
  1934.                     case 'mixed':
  1935.                         $results $parts[0];
  1936.                         for($p 1$p $lp++$p)
  1937.                             $results['Attachments'][$parts[$p];
  1938.                         break;
  1939.  
  1940.                     case 'report':
  1941.                         if(IsSet($parameters['report-type']))
  1942.                         {
  1943.                             switch($parameters['report-type'])
  1944.                             {
  1945.                                 case 'delivery-status':
  1946.                                     for($p 1$p $lp++$p)
  1947.                                     {
  1948.                                         if(!strcmp($parts[$p]['Type']$parameters['report-type']))
  1949.                                         {
  1950.                                             $results $parts[$p];
  1951.                                             break;
  1952.                                         }
  1953.                                     }
  1954.                                     if(!$this->ReadMessageBody($parts[0]$body'Data'))
  1955.                                         return(0);
  1956.                                     if(strlen($body))
  1957.                                         $results['Response'$body;
  1958.                                     break;
  1959.                             }
  1960.                         }
  1961.                         $results['Type'$parameters['report-type'];
  1962.                         break;
  1963.  
  1964.                     case 'signed':
  1965.                         if($lp != 2)
  1966.                             return($this->SetError('this '.$content_type.' message does not have just 2 parts'));
  1967.                         if(strcmp($parts[1]['Type']'signature'))
  1968.                         {
  1969.                             $this->SetErrorWithContact('this '.$content_type.' message does not contain a signature');
  1970.                             $this->error = '';
  1971.                         }
  1972.                         $results $parts[0];
  1973.                         $results['Signature'$parts[1];
  1974.                         break;
  1975.                 }
  1976.                 break;
  1977.             case 'text':
  1978.                 switch($sub_type)
  1979.                 {
  1980.                     case 'plain':
  1981.                         $results['Type''text';
  1982.                         $results['Description''Text message';
  1983.                         break;
  1984.                     case 'html':
  1985.                         $results['Type''html';
  1986.                         $results['Description''HTML message';
  1987.                         break;
  1988.                     default:
  1989.                         $results['Type'$type;
  1990.                         $results['SubType'$sub_type;
  1991.                         $results['Description''Text file in the '.strtoupper($sub_type).' format';
  1992.                         break;
  1993.                 }
  1994.                 break;
  1995.             case 'video':
  1996.                 $results['Type'$type;
  1997.                 $results['SubType'$sub_type;
  1998.                 $results['Description''Video file in the '.strtoupper($sub_type).' format';
  1999.                 break;
  2000.             case 'image':
  2001.                 $results['Type'$type;
  2002.                 $results['SubType'$sub_type;
  2003.                 $results['Description''Image file in the '.strtoupper($sub_type).' format';
  2004.                 break;
  2005.             case 'audio':
  2006.                 $results['Type'$type;
  2007.                 $results['SubType'$sub_type;
  2008.                 $results['Description''Audio file in the '.strtoupper($sub_type).' format';
  2009.                 break;
  2010.             case 'application':
  2011.                 switch($sub_type)
  2012.                 {
  2013.                     case 'octet-stream':
  2014.                     case 'x-msdownload':
  2015.                         $results['Type''binary';
  2016.                         $results['Description''Binary file';
  2017.                         break;
  2018.                     case 'pdf':
  2019.                         $results['Type'$sub_type;
  2020.                         $results['Description''Document in PDF format';
  2021.                         break;
  2022.                     case 'postscript':
  2023.                         $results['Type'$sub_type;
  2024.                         $results['Description''Document in Postscript format';
  2025.                         break;
  2026.                     case 'msword':
  2027.                         $results['Type''ms-word';
  2028.                         $results['Description''Word processing document in Microsoft Word format';
  2029.                         break;
  2030.                     case 'vnd.ms-powerpoint':
  2031.                         $results['Type''ms-powerpoint';
  2032.                         $results['Description''Presentation in Microsoft PowerPoint format';
  2033.                         break;
  2034.                     case 'vnd.ms-excel':
  2035.                         $results['Type''ms-excel';
  2036.                         $results['Description''Spreadsheet in Microsoft Excel format';
  2037.                         break;
  2038.                     case 'zip':
  2039.                     case 'x-zip':
  2040.                     case 'x-zip-compressed':
  2041.                         $results['Type''zip';
  2042.                         $results['Description''ZIP archive with compressed files';
  2043.                         break;
  2044.                     case 'ms-tnef':
  2045.                         $results['Type'$sub_type;
  2046.                         $results['Description''Microsoft Exchange data usually sent by Microsoft Outlook';
  2047.                         break;
  2048.                     case 'pgp-signature':
  2049.                         $results['Type''signature';
  2050.                         $results['SubType'$sub_type;
  2051.                         $results['Description''Message signature for PGP';
  2052.                         break;
  2053.                     case 'x-pkcs7-signature':
  2054.                     case 'pkcs7-signature':
  2055.                         $results['Type''signature';
  2056.                         $results['SubType'$sub_type;
  2057.                         $results['Description''PKCS message signature';
  2058.                         break;
  2059.                     case 'vnd.oasis.opendocument.text':
  2060.                         $results['Type''odf-writer';
  2061.                         $results['Description''Word processing document in ODF text format used by OpenOffice Writer';
  2062.                         break;
  2063.                 }
  2064.                 break;
  2065.             case 'message':
  2066.                 $tolerate_unrecognized 0;
  2067.                 switch($sub_type)
  2068.                 {
  2069.                     case 'delivery-status':
  2070.                         $results['Type'$sub_type;
  2071.                         $results['Description''Notification of the status of delivery of a message';
  2072.                         if(!$this->ReadMessageBody($message$body'Body'))
  2073.                             return(0);
  2074.                         if(($l strlen($body)))
  2075.                         {
  2076.                             $position 0;
  2077.                             $this->ParseHeaderString($body$position$headers);
  2078.                             $recipients array();
  2079.                             for(;$position<$l;)
  2080.                             {
  2081.                                 $this->ParseHeaderString($body$position$headers);
  2082.                                 if(count($headers))
  2083.                                 {
  2084.                                     $r count($recipients);
  2085.                                     if(IsSet($headers['action']))
  2086.                                         $recipients[$r]['Action'$headers['action'];
  2087.                                     if(IsSet($headers['status']))
  2088.                                         $recipients[$r]['Status'$headers['status'];
  2089.                                     if(IsSet($headers['original-recipient']))
  2090.                                     {
  2091.                                         strtok($headers['original-recipient']';');
  2092.                                         $recipients[$r]['Address'trim(strtok(''));
  2093.                                     }
  2094.                                     elseif(IsSet($headers['final-recipient']))
  2095.                                     {
  2096.                                         strtok($headers['final-recipient']';');
  2097.                                         $recipients[$r]['Address'trim(strtok(''));
  2098.                                     }
  2099.                                 }
  2100.                             }
  2101.                             $results['Recipients'$recipients;
  2102.                         }
  2103.                         $copy_body 0;
  2104.                         break;
  2105.                     case 'rfc822':
  2106.                         $results['Type''message';
  2107.                         $results['Description''E-mail message';
  2108.                         break;
  2109.                 }
  2110.                 break;
  2111.             default:
  2112.                 $tolerate_unrecognized 0;
  2113.                 break;
  2114.         }
  2115.         if(!IsSet($results['Type']))
  2116.         {
  2117.             $this->SetErrorWithContact($content_type.' message parts are not yet recognized');
  2118.             $results['Type'$this->error;
  2119.             $this->error = '';
  2120.         }
  2121.         if(IsSet($parameters['charset']))
  2122.             $results['Encoding'strtolower($parameters['charset']);
  2123.         if(IsSet($message['Headers']['subject:']))
  2124.         {
  2125.             if(IsSet($message['DecodedHeaders']['subject:'])
  2126.             && count($message['DecodedHeaders']['subject:']== 1
  2127.             && count($message['DecodedHeaders']['subject:'][0]== 1)
  2128.             {
  2129.                 $results['Subject'$message['DecodedHeaders']['subject:'][0][0]['Value'];
  2130.                 $results['SubjectEncoding'strtolower($message['DecodedHeaders']['subject:'][0][0]['Encoding']);
  2131.             }
  2132.             else
  2133.                 $results['Subject'$message['Headers']['subject:'];
  2134.         }
  2135.         if(IsSet($message['Headers']['date:']))
  2136.         {
  2137.             if(IsSet($message['DecodedHeaders']['date:'])
  2138.             && count($message['DecodedHeaders']['date:']== 1
  2139.             && count($message['DecodedHeaders']['date:'][0]== 1)
  2140.                 $results['Date'$message['DecodedHeaders']['date:'][0][0]['Value'];
  2141.             else
  2142.                 $results['Date'$message['Headers']['date:'];
  2143.         }
  2144.         $l count($this->address_headers);
  2145.         for(Reset($this->address_headers)$h 0$h<$lNext($this->address_headers)++$h)
  2146.             $this->CopyAddresses($message$resultsKey($this->address_headers));
  2147.         if($copy_body)
  2148.         {
  2149.             if(IsSet($message['Body']))
  2150.                 $results['Data'$message['Body'];
  2151.             elseif(IsSet($message['BodyFile']))
  2152.                 $results['DataFile'$message['BodyFile'];
  2153.             elseif(IsSet($message['BodyLength']))
  2154.                 $results['DataLength'$message['BodyLength'];
  2155.             if(IsSet($message['FileName']))
  2156.                 $results['FileName'$message['FileName'];
  2157.             if(IsSet($message['FileDisposition']))
  2158.                 $results['FileDisposition'$message['FileDisposition'];
  2159.             if(IsSet($message['Headers']['content-id:']))
  2160.             {
  2161.                 $content_id trim($message['Headers']['content-id:']);
  2162.                 $l strlen($content_id);
  2163.                 if(!strcmp($content_id[0]'<')
  2164.                 && !strcmp($content_id[$l 1]'>'))
  2165.                     $results['ContentID'substr($content_id1$l 2);
  2166.             }
  2167.         }
  2168.         return(1);
  2169.     }
  2170. /*
  2171. {metadocument}
  2172.         </do>
  2173.     </function>
  2174. {/metadocument}
  2175. */
  2176.  
  2177. };
  2178.  
  2179. /*
  2180.  
  2181. {metadocument}
  2182. </class>
  2183. {/metadocument}
  2184.  
  2185. */
  2186.  
  2187. ?>

Documentation generated on Sat, 06 Mar 2010 03:55:11 +0100 by phpDocumentor 1.4.2. This site is hosted and maintained by Daniel HAHLER (Contact).