/**
* body parts
* @var object
*/
var $parts;
var $mid;
var $maildir;
/**
* Constructor.
*
* Sets up the object, initialise the variables, and splits and
* stores the header and body of the input.
*
* @param string The input to decode
* @access public
*/
/**
* Begins the decoding process. If called statically
* it will create an object and call the decode() method
* of it.
*
* @param array An array of various parameters that determine
* various things:
* include_bodies - Whether to include the body in the returned
* object.
* decode_bodies - Whether to decode the bodies
* of the parts. (Transfer encoding)
* decode_headers - Whether to decode headers
* input - If called statically, this will be treated
* as the input
* @return object Decoded results
* @access public
*/
function decode($params = null)
{
// Have we been called statically?
// If so, create an object and pass details to that.
if (!isset($this) AND isset($params['input']))
{
if (isset($params['crlf']))
{
$obj = new Decode_Mimemail($params['input'],$params['mid'],$params['maildir'],$params['crlf']);
}
else
{
$obj = new Decode_Mimemail($params['input'],$params['mid'],$params['maildir']);
}
$structure = $obj->decode($params);
// Called statically but no input
}
elseif (!isset($this))
{
return $this->_error="Called statically and no input given";
/**
* Performs the decoding. Decodes the body string passed to it
* If it finds certain content-types it will call itself in a
* recursive fashion
*
* @param string Header section
* @param string Body section
* @param string mid mime filename
* @return object Results of decoding process
* @access private
*/
function _decode($headers, $body, $mid, $maildir, $default_ctype = 'text/plain')
{
$return = new stdClass;
if(!is_null($headers))
{
$headers = $this->parseHeaders($headers);
}
else{
$this->_error="the mime headers is null.";
return $this->_error;
}
foreach ($headers as $value)
{
if (isset($return->headers[$value['name']]) AND !is_array($return->headers[$value['name']]))
{
$return->headers[$value['name']] = array($return->headers[$value['name']]);
$return->headers[$value['name']][] = $value['value'];
}
elseif (isset($return->headers[$value['name']]))
{
$return->headers[$value['name']][] = $value['value'];
}
else
{
$return->headers[$value['name']] = $value['value'];
}
}
reset($headers);
//rewinds array's internal pointer to the first element and returns the value of the first array element.
while (list($key, $value) = each($headers))
{
$headers[$key]['name'] = strtolower($headers[$key]['name']);
switch ($headers[$key]['name'])
{
case 'content-type':
$content_type = $this->parseHeaderValue($headers[$key]['value']);
if (preg_match('/([0-9a-z+.-]+)/([0-9a-z+.-]+)/i', $content_type['value'], $regs))
{
$return->ctype_primary = $regs[1];
$return->ctype_secondary = $regs[2];
}
if (isset($content_type['other']))
{
while (list($p_name, $p_value) = each($content_type['other']))
{
$return->ctype_parameters[$p_name] = $p_value;
}
}
break;
case 'content-disposition':
$content_disposition = $this->parseHeaderValue($headers[$key]['value']);
$return->disposition = $content_disposition['value'];
if (isset($content_disposition['other']))
{
while (list($p_name, $p_value) = each($content_disposition['other']))
{
$return->d_parameters[$p_name] = $p_value;
}
}
break;
case 'content-transfer-encoding':
if(!is_null($this->parseHeaderValue($headers[$key]['value'])))
{
$content_transfer_encoding = $this->parseHeaderValue($headers[$key]['value']);
}
else{
$content_transfer_encoding = "";
}
break;
}
}
if (isset($content_type))
{
$content_type['value'] = strtolower($content_type['value']);
switch ($content_type['value'])
{
case 'text':
case 'text/plain':
if($this->_include_bodies)
{
if($this->_decode_bodies)
{
$return->body = isset($content_transfer_encoding['value'])
?$this->decodeBody($body,$content_transfer_encoding['value'])
: $body;
}
else{
$return->body = $body;
}
/**
* Given a string containing a header and body
* section, this function will split them (at the first blank line) and return them.
*
* @param string Input to split apart
* @return array Contains header and body section
* @access private
*/
function splitBodyHeader($input)
{
$pos = strpos($input, $this->_crlf.$this->_crlf);
if ($pos === false)
{
$this->_error = 'Could not split header and body';
return false;
}