Advertisement
Guest User

phppipe

a guest
Mar 27th, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 27.01 KB | None | 0 0
  1. <?php
  2. class Mail_mimeDecode
  3.     {
  4.     var $_input = null;
  5.     var $_header = null;
  6.     var $_body = null;
  7.     var $_error = null;
  8.     var $_include_bodies = null;
  9.     var $_decode_bodies = null;
  10.     var $_decode_headers = null;
  11.     function Mail_mimeDecode($input)
  12.         {
  13.         list($header, $body) = $this->_splitBodyHeader($input);
  14.         $this->_input          = $input;
  15.         $this->_header         = $header;
  16.         $this->_body           = $body;
  17.         $this->_decode_bodies  = true;
  18.         $this->_include_bodies = true;
  19.         }
  20.     function decode($params = null)
  21.         {
  22.         $isStatic = !(isset($this) && get_class($this) == 'Mail_mimeDecode');
  23.         if (($isStatic && isset($params['input'])))
  24.             {
  25.             $obj       = new Mail_mimeDecode($params['input']);
  26.             $structure = $obj->decode($params);
  27.             }
  28.         else
  29.             {
  30.             if ($isStatic)
  31.                 {
  32.                 return false;
  33.                 }
  34.             $this->_include_bodies = (isset($params['include_bodies']) ? $params['include_bodies'] : false);
  35.             $this->_decode_bodies  = (isset($params['decode_bodies']) ? $params['decode_bodies'] : false);
  36.             $this->_decode_headers = (isset($params['decode_headers']) ? $params['decode_headers'] : false);
  37.             $structure             = $this->_decode($this->_header, $this->_body);
  38.             }
  39.         return $structure;
  40.         }
  41.     function _decode($headers, $body, $default_ctype = 'text/plain')
  42.         {
  43.         $return          = new stdClass();
  44.         $return->headers = array();
  45.         $headers         = $this->_parseHeaders($headers);
  46.         foreach ($headers as $value)
  47.             {
  48.             if ((isset($return->headers[strtolower($value['name'])]) && !is_array($return->headers[strtolower($value['name'])])))
  49.                 {
  50.                 $return->headers[strtolower($value['name'])]   = array(
  51.                     $return->headers[strtolower($value['name'])]
  52.                 );
  53.                 $return->headers[strtolower($value['name'])][] = $value['value'];
  54.                 }
  55.             if (isset($return->headers[strtolower($value['name'])]))
  56.                 {
  57.                 $return->headers[strtolower($value['name'])][] = $value['value'];
  58.                 }
  59.             $return->headers[strtolower($value['name'])] = $value['value'];
  60.             }
  61.         reset($headers);
  62.         while (list($key, $value) = each($headers))
  63.             {
  64.             $headers[$key]['name'] = strtolower($headers[$key]['name']);
  65.             switch ($headers[$key]['name'])
  66.             {
  67.                 case 'content-type':
  68.                     {
  69.                     $content_type = $this->_parseHeaderValue($headers[$key]['value']);
  70.                     if (preg_match('/([0-9a-z+.-]+)\/([0-9a-z+.-]+)/i', $content_type['value'], $regs))
  71.                         {
  72.                         $return->ctype_primary   = $regs[1];
  73.                         $return->ctype_secondary = $regs[2];
  74.                         }
  75.                     if (isset($content_type['other']))
  76.                         {
  77.                         while (list($p_name, $p_value) = each($content_type['other']))
  78.                             {
  79.                             $return->ctype_parameters[$p_name] = $p_value;
  80.                             }
  81.                         }
  82.                     break;
  83.                     }
  84.                 case 'content-disposition':
  85.                     {
  86.                     $content_disposition = $this->_parseHeaderValue($headers[$key]['value']);
  87.                     $return->disposition = $content_disposition['value'];
  88.                     if (isset($content_disposition['other']))
  89.                         {
  90.                         while (list($p_name, $p_value) = each($content_disposition['other']))
  91.                             {
  92.                             $return->d_parameters[$p_name] = $p_value;
  93.                             }
  94.                         }
  95.                     break;
  96.                     }
  97.                 case 'content-transfer-encoding':
  98.                     {
  99.                     $content_transfer_encoding = $this->_parseHeaderValue($headers[$key]['value']);
  100.                     }
  101.             }
  102.             }
  103.         if (isset($content_type))
  104.             {
  105.             switch (strtolower($content_type['value']))
  106.             {
  107.                 case 'text/plain':
  108.                     {
  109.                     $encoding = (isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit');
  110.                     ($this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding) : $body) : null);
  111.                     break;
  112.                     }
  113.                 case 'text/html':
  114.                     {
  115.                     $encoding = (isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit');
  116.                     ($this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding) : $body) : null);
  117.                     break;
  118.                     }
  119.                 case 'multipart/parallel':
  120.                     {
  121.                     }
  122.                 case 'multipart/report':
  123.                     {
  124.                     }
  125.                 case 'multipart/signed':
  126.                     {
  127.                     }
  128.                 case 'multipart/digest':
  129.                     {
  130.                     }
  131.                 case 'multipart/alternative':
  132.                     {
  133.                     }
  134.                 case 'multipart/related':
  135.                     {
  136.                     }
  137.                 case 'multipart/mixed':
  138.                     {
  139.                     if (!isset($content_type['other']['boundary']))
  140.                         {
  141.                         $this->_error = 'No boundary found for ' . $content_type['value'] . ' part';
  142.                         return false;
  143.                         }
  144.                     $default_ctype = (strtolower($content_type['value']) === 'multipart/digest' ? 'message/rfc822' : 'text/plain');
  145.                     $parts         = $this->_boundarySplit($body, $content_type['other']['boundary']);
  146.                     $i             = 0;
  147.                     while ($i < count($parts))
  148.                         {
  149.                         list($part_header, $part_body) = $this->_splitBodyHeader($parts[$i]);
  150.                         $part            = $this->_decode($part_header, $part_body, $default_ctype);
  151.                         $return->parts[] = $part;
  152.                         ++$i;
  153.                         }
  154.                     break;
  155.                     }
  156.                 case 'message/rfc822':
  157.                     {
  158.                     $obj =& new Mail_mimeDecode($body);
  159.                     $return->parts[] = $obj->decode(array(
  160.                         'include_bodies' => $this->_include_bodies,
  161.                         'decode_bodies' => $this->_decode_bodies,
  162.                         'decode_headers' => $this->_decode_headers
  163.                     ));
  164.                     unset($obj);
  165.                     break;
  166.                     }
  167.                 default:
  168.                     {
  169.                     if (!isset($content_transfer_encoding['value']))
  170.                         {
  171.                         $content_transfer_encoding['value'] = '7bit';
  172.                         }
  173.                     ($this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $content_transfer_encoding['value']) : $body) : null);
  174.                     break;
  175.                     }
  176.             }
  177.             }
  178.         else
  179.             {
  180.             $ctype                   = explode('/', $default_ctype);
  181.             $return->ctype_primary   = $ctype[0];
  182.             $return->ctype_secondary = $ctype[1];
  183.             ($this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body) : $body) : null);
  184.             }
  185.         return $return;
  186.         }
  187.     function &getMimeNumbers(&$structure, $no_refs = false, $mime_number = '', $prepend = '')
  188.         {
  189.         $return = array();
  190.         if (!empty($structure->parts))
  191.             {
  192.             if ($mime_number != '')
  193.                 {
  194.                 $structure->mime_id = $prepend . $mime_number;
  195.                 $return[$prepend . $mime_number] =& $structure;
  196.                 }
  197.             $i = 0;
  198.             while ($i < count($structure->parts))
  199.                 {
  200.                 if ((!empty($structure->headers['content-type']) && substr(strtolower($structure->headers['content-type']), 0, 8) == 'message/'))
  201.                     {
  202.                     $prepend      = $prepend . $mime_number . '.';
  203.                     $_mime_number = '';
  204.                     }
  205.                 else
  206.                     {
  207.                     $_mime_number = ($mime_number == '' ? $i + 1 : sprintf('%s.%s', $mime_number, $i + 1));
  208.                     }
  209.                 $arr =& Mail_mimeDecode::getmimenumbers($structure->parts[$i], $no_refs, $_mime_number, $prepend);
  210.                 foreach ($arr as $key => $val)
  211.                     {
  212.                     $no_refs ? $return[$key] = '' : $return[$key] =& $arr[$key];
  213.                     }
  214.                 ++$i;
  215.                 }
  216.             }
  217.         else
  218.             {
  219.             if ($mime_number == '')
  220.                 {
  221.                 $mime_number = '1';
  222.                 }
  223.             $structure->mime_id = $prepend . $mime_number;
  224.             $no_refs ? $return[$prepend . $mime_number] = '' : $return[$prepend . $mime_number] =& $structure;
  225.             }
  226.         return $return;
  227.         }
  228.     function _splitBodyHeader($input)
  229.         {
  230.         if (preg_match('/^(.*?)??(.*)/s', $input, $match))
  231.             {
  232.             return array(
  233.                 $match[1],
  234.                 $match[2]
  235.             );
  236.             }
  237.         $this->_error = 'Could not split header and body';
  238.         return false;
  239.         }
  240.     function _parseHeaders($input)
  241.         {
  242.         if ($input !== '')
  243.             {
  244.             $input   = preg_replace('/?/', '', $input);
  245.             $input   = preg_replace('/( | )+/', ' ', $input);
  246.             $headers = explode('', trim($input));
  247.             foreach ($headers as $value)
  248.                 {
  249.                 $hdr_name  = substr($value, 0, $pos = strpos($value, ':'));
  250.                 $hdr_value = substr($value, $pos + 1);
  251.                 if ($hdr_value[0] == ' ')
  252.                     {
  253.                     $hdr_value = substr($hdr_value, 1);
  254.                     }
  255.                 $return[] = array(
  256.                     'name' => $hdr_name,
  257.                     'value' => ($this->_decode_headers ? $this->_decodeHeader($hdr_value) : $hdr_value)
  258.                 );
  259.                 }
  260.             }
  261.         else
  262.             {
  263.             $return = array();
  264.             }
  265.         return $return;
  266.         }
  267.     function _parseHeaderValue($input)
  268.         {
  269.         if ($pos = strpos($input, ';') !== false)
  270.             {
  271.             $return['value'] = trim(substr($input, 0, $pos));
  272.             $input           = trim(substr($input, $pos + 1));
  273.             if (0 < strlen($input))
  274.                 {
  275.                 $splitRegex = '/([^;\'"]*[\'"]([^\'"]*([^\'"]*)*)[\'"][^;\'"]*|([^;]+))(;|$)/';
  276.                 preg_match_all($splitRegex, $input, $matches);
  277.                 $parameters = array();
  278.                 $i          = 0;
  279.                 while ($i < count($matches[0]))
  280.                     {
  281.                     $param = $matches[0][$i];
  282.                     while (substr($param, 0 - 2) == '\;')
  283.                         {
  284.                         $param .= $matches[0][++$i];
  285.                         }
  286.                     $parameters[] = $param;
  287.                     ++$i;
  288.                     }
  289.                 $i = 0;
  290.                 while ($i < count($parameters))
  291.                     {
  292.                     $param_name  = trim(substr($parameters[$i], 0, $pos = strpos($parameters[$i], '=')), '\'";  \\ ');
  293.                     $param_value = trim(str_replace('\;', ';', substr($parameters[$i], $pos + 1)), '\'";    \\ ');
  294.                     if ($param_value[0] == '"')
  295.                         {
  296.                         $param_value = substr($param_value, 1, 0 - 1);
  297.                         }
  298.                     $return['other'][$param_name]             = $param_value;
  299.                     $return['other'][strtolower($param_name)] = $param_value;
  300.                     ++$i;
  301.                     }
  302.                 }
  303.             }
  304.         else
  305.             {
  306.             $return['value'] = trim($input);
  307.             }
  308.         return $return;
  309.         }
  310.     function _boundarySplit($input, $boundary)
  311.         {
  312.         $parts       = array();
  313.         $bs_possible = substr($boundary, 2, 0 - 2);
  314.         $bs_check    = '\"' . $bs_possible . '\"';
  315.         if ($boundary == $bs_check)
  316.             {
  317.             $boundary = $bs_possible;
  318.             }
  319.         $tmp = explode('--' . $boundary, $input);
  320.         $i   = 1;
  321.         while ($i < count($tmp) - 1)
  322.             {
  323.             $parts[] = $tmp[$i];
  324.             ++$i;
  325.             }
  326.         return $parts;
  327.         }
  328.     function _decodeHeader($input)
  329.         {
  330.         $input = preg_replace('/(=\?[^?]+\?(q|b)\?[^?]*\?=)(\s)+=\?/i', '\1=?', $input);
  331.         while (preg_match('/(=\?([^?]+)\?(q|b)\?([^?]*)\?=)/i', $input, $matches))
  332.             {
  333.             $encoded  = $matches[1];
  334.             $charset  = $matches[2];
  335.             $encoding = $matches[3];
  336.             $text     = $matches[4];
  337.             switch (strtolower($encoding))
  338.             {
  339.                 case 'b':
  340.                     {
  341.                     $text = base64_decode($text);
  342.                     break;
  343.                     }
  344.                 case 'q':
  345.                     {
  346.                     $text = str_replace('_', ' ', $text);
  347.                     preg_match_all('/=([a-f0-9]{2})/i', $text, $matches);
  348.                     foreach ($matches[1] as $value)
  349.                         {
  350.                         $text = str_replace('=' . $value, chr(hexdec($value)), $text);
  351.                         }
  352.                     }
  353.             }
  354.             $input = str_replace($encoded, $text, $input);
  355.             }
  356.         return $input;
  357.         }
  358.     function _decodeBody($input, $encoding = '7bit')
  359.         {
  360.         switch (strtolower($encoding))
  361.         {
  362.             case '7bit':
  363.                 return $input;
  364.                 break;
  365.             case 'quoted-printable':
  366.                 return $this->_quotedPrintableDecode($input);
  367.                 break;
  368.             case 'base64':
  369.                 return base64_decode($input);
  370.                 break;
  371.             default:
  372.                 return $input;
  373.         }
  374.         }
  375.     function _quotedPrintableDecode($input)
  376.         {
  377.         $input = preg_replace('/=?/', '', $input);
  378.         $input = preg_replace('/=([a-f0-9]{2})/ie', 'chr(hexdec(\'\1\'))', $input);
  379.         return $input;
  380.         }
  381.     function uudecode($input)
  382.         {
  383.         preg_match_all('/begin ([0-7]{3}) (.+)?(.+)?end/Us', $input, $matches);
  384.         $j = 0;
  385.         while ($j < count($matches[3]))
  386.             {
  387.             $str      = $matches[3][$j];
  388.             $filename = $matches[2][$j];
  389.             $fileperm = $matches[1][$j];
  390.             $file     = '';
  391.             $str      = preg_split('/?/', trim($str));
  392.             $strlen   = count($str);
  393.             $i        = 0;
  394.             while ($i < $strlen)
  395.                 {
  396.                 $pos = 1;
  397.                 $d   = 0;
  398.                 $len = (int) ord(substr($str[$i], 0, 1)) - 32 - ' ' & 63;
  399.                 while (($d + 3 <= $len && $pos + 4 <= strlen($str[$i])))
  400.                     {
  401.                     $c0 = ord(substr($str[$i], $pos, 1)) ^ 32;
  402.                     $c1 = ord(substr($str[$i], $pos + 1, 1)) ^ 32;
  403.                     $c2 = ord(substr($str[$i], $pos + 2, 1)) ^ 32;
  404.                     $c3 = ord(substr($str[$i], $pos + 3, 1)) ^ 32;
  405.                     $file .= chr(($c0 - ' ' & 63) << 2 | ($c1 - ' ' & 63) >> 4);
  406.  
  407.  
  408.                     $file .= chr(($c1 - ' ' & 63) << 4 | ($c2 - ' ' & 63) >> 2);
  409.                     $file .= chr(($c2 - ' ' & 63) << 6 | $c3 - ' ' & 63);
  410.                     $pos += 4;
  411.                     $d += 3;
  412.                     }
  413.                 if (($d + 2 <= $len && $pos + 3 <= strlen($str[$i])))
  414.                     {
  415.                     $c0 = ord(substr($str[$i], $pos, 1)) ^ 32;
  416.                     $c1 = ord(substr($str[$i], $pos + 1, 1)) ^ 32;
  417.                     $c2 = ord(substr($str[$i], $pos + 2, 1)) ^ 32;
  418.                     $file .= chr(($c0 - ' ' & 63) << 2 | ($c1 - ' ' & 63) >> 4);
  419.                     $file .= chr(($c1 - ' ' & 63) << 4 | ($c2 - ' ' & 63) >> 2);
  420.                     $pos += 3;
  421.                     $d += 2;
  422.                     }
  423.                 if (($d + 1 <= $len && $pos + 2 <= strlen($str[$i])))
  424.                     {
  425.                     $c0 = ord(substr($str[$i], $pos, 1)) ^ 32;
  426.                     $c1 = ord(substr($str[$i], $pos + 1, 1)) ^ 32;
  427.                     $file .= chr(($c0 - ' ' & 63) << 2 | ($c1 - ' ' & 63) >> 4);
  428.                     }
  429.                 ++$i;
  430.                 }
  431.             $files[] = array(
  432.                 'filename' => $filename,
  433.                 'fileperm' => $fileperm,
  434.                 'filedata' => $file
  435.             );
  436.             ++$j;
  437.             }
  438.         return $files;
  439.         }
  440.     function getSendArray()
  441.         {
  442.         $this->_decode_headers = FALSE;
  443.         $headerlist            = $this->_parseHeaders($this->_header);
  444.         $to                    = '';
  445.         if (!$headerlist)
  446.             {
  447.             return false;
  448.             }
  449.         foreach ($headerlist as $item)
  450.             {
  451.             $header[$item['name']] = $item['value'];
  452.             switch (strtolower($item['name']))
  453.             {
  454.                 case 'to':
  455.                     {
  456.                     }
  457.                 case 'cc':
  458.                     {
  459.                     }
  460.                 case 'bcc':
  461.                     {
  462.                     $to = ',' . $item['value'];
  463.                     }
  464.                 default:
  465.                     {
  466.                     }
  467.             }
  468.             break;
  469.             }
  470.         if ($to == '')
  471.             {
  472.             return false;
  473.             }
  474.         $to = substr($to, 1);
  475.         return array(
  476.             $to,
  477.             $header,
  478.             $this->_body
  479.         );
  480.         }
  481.     function getXML($input)
  482.         {
  483.         $crlf   = '';
  484.         $output = '<?xml version=\'1.0\'?>' . $crlf . '<!DOCTYPE email SYSTEM "http://www.phpguru.org/xmail/xmail.dtd">' . $crlf . '<email>' . $crlf . Mail_mimeDecode::_getxml($input) . '</email>';
  485.         return $output;
  486.         }
  487.     function _getXML($input, $indent = 1)
  488.         {
  489.         $htab    = '    ';
  490.         $crlf    = '';
  491.         $output  = '';
  492.         $headers = (array) $input->headers;
  493.         foreach ($headers as $hdr_name => $hdr_value)
  494.             {
  495.             if (is_array($headers[$hdr_name]))
  496.                 {
  497.                 $i = 0;
  498.                 while ($i < count($hdr_value))
  499.                     {
  500.                     $output .= Mail_mimeDecode::_getxml_helper($hdr_name, $hdr_value[$i], $indent);
  501.                     ++$i;
  502.                     }
  503.                 }
  504.             $output .= Mail_mimeDecode::_getxml_helper($hdr_name, $hdr_value, $indent);
  505.             }
  506.         if (!empty($input->parts))
  507.             {
  508.             $i = 0;
  509.             while ($i < count($input->parts))
  510.                 {
  511.                 $output .= $crlf . str_repeat($htab, $indent) . '<mimepart>' . $crlf . Mail_mimeDecode::_getxml($input->parts[$i], $indent + 1) . str_repeat($htab, $indent) . '</mimepart>' . $crlf;
  512.                 ++$i;
  513.                 }
  514.             }
  515.         else
  516.             {
  517.             if (isset($input->body))
  518.                 {
  519.                 $output .= $crlf . str_repeat($htab, $indent) . '<body><![CDATA[' . $input->body . ']]></body>' . $crlf;
  520.                 }
  521.             }
  522.         return $output;
  523.         }
  524.     function _getXML_helper($hdr_name, $hdr_value, $indent)
  525.         {
  526.         $htab          = '  ';
  527.         $crlf          = '';
  528.         $return        = '';
  529.         $new_hdr_value = ($hdr_name != 'received' ? Mail_mimeDecode::_parseheadervalue($hdr_value) : array(
  530.             'value' => $hdr_value
  531.         ));
  532.         $new_hdr_name  = str_replace(' ', '-', ucwords(str_replace('-', ' ', $hdr_name)));
  533.         if (!empty($new_hdr_value['other']))
  534.             {
  535.             foreach ($new_hdr_value['other'] as $paramname => $paramvalue)
  536.                 {
  537.                 $params[] = str_repeat($htab, $indent) . $htab . '<parameter>' . $crlf . str_repeat($htab, $indent) . $htab . $htab . '<paramname>' . htmlspecialchars($paramname) . '</paramname>' . $crlf . str_repeat($htab, $indent) . $htab . $htab . '<paramvalue>' . htmlspecialchars($paramvalue) . '</paramvalue>' . $crlf . str_repeat($htab, $indent) . $htab . '</parameter>' . $crlf;
  538.                 }
  539.             $params = implode('', $params);
  540.             }
  541.         else
  542.             {
  543.             $params = '';
  544.             }
  545.         $return = str_repeat($htab, $indent) . '<header>' . $crlf . str_repeat($htab, $indent) . $htab . '<headername>' . htmlspecialchars($new_hdr_name) . '</headername>' . $crlf . str_repeat($htab, $indent) . $htab . '<headervalue>' . htmlspecialchars($new_hdr_value['value']) . '</headervalue>' . $crlf . $params . str_repeat($htab, $indent) . '</header>' . $crlf;
  546.         return $return;
  547.         }
  548.     }
  549. function interpret_structure($structure)
  550.     {
  551.     global $_emailoutput;
  552.     $ctype = strtolower($structure->ctype_primary) . '/' . strtolower($structure->ctype_secondary);
  553.     if (!$ctype)
  554.         {
  555.         $ctype = 'text/plain';
  556.         }
  557.     if (($ctype == 'text/html' || $ctype == 'text/plain'))
  558.         {
  559.         $charset = 'us-ascii';
  560.         if ((!empty($structure->ctype_parameters) && isset($structure->ctype_parameters['charset'])))
  561.             {
  562.             $charset = $structure->ctype_parameters['charset'];
  563.             }
  564.         if ((!empty($structure->disposition) && $structure->disposition == 'attachment'))
  565.             {
  566.             handle_attachment($structure);
  567.             return null;
  568.             }
  569.         $var      = ($ctype == 'text/html' ? 'html' : 'text');
  570.         $bodyUtf8 = $structure->body;
  571.         if ($charset == 'UTF-8')
  572.             {
  573.             $charset = '';
  574.             }
  575.         if (($charset && function_exists('iconv')))
  576.             {
  577.             $bodyUtf8 = iconv($charset, 'utf-8', $bodyUtf8);
  578.             if (!$_emailoutput['headers']['convertedcharset'])
  579.                 {
  580.                 $_emailoutput['headers']['subject']          = iconv($charset, 'utf-8', $_emailoutput['headers']['subject']);
  581.                 $_emailoutput['headers']['convertedcharset'] = true;
  582.                 }
  583.             }
  584.         $_emailoutput['body'][$ctype] = trim($bodyUtf8);
  585.         return null;
  586.         }
  587.     if (strtolower($structure->ctype_primary) == 'multipart')
  588.         {
  589.         if (!empty($structure->parts))
  590.             {
  591.             $i = 0;
  592.             while ($i < count($structure->parts))
  593.                 {
  594.                 interpret_structure($structure->parts[$i]);
  595.                 ++$i;
  596.                 }
  597.             }
  598.         }
  599.     else
  600.         {
  601.         handle_attachment($structure);
  602.         }
  603.     }
  604. function handle_attachment($structure)
  605.     {
  606.     global $_emailoutput;
  607.     if (!empty($structure->d_parameters['filename']))
  608.         {
  609.         $filename = $structure->d_parameters['filename'];
  610.         }
  611.     else
  612.         {
  613.         if (!empty($structure->ctype_parameters['name']))
  614.             {
  615.             $filename = $structure->ctype_parameters['name'];
  616.             }
  617.         else
  618.             {
  619.             return null;
  620.             }
  621.         }
  622.     $ctype                         = strtolower($structure->ctype_primary) . '/' . strtolower($structure->ctype_secondary);
  623.     $_emailoutput['attachments'][] = array(
  624.         'data' => $structure->body,
  625.         'size' => strlen($structure->body),
  626.         'filename' => $filename,
  627.         'contenttype' => $ctype
  628.     );
  629.     }
  630. $silent       = 'true';
  631. $_emailoutput = array();
  632. $fd           = fopen('php://stdin', 'r');
  633. $input        = '';
  634. while (!feof($fd))
  635.     {
  636.     $input .= fread($fd, 1024);
  637.     }
  638. fclose($fd);
  639. $decode_params['input']          = $input;
  640. $decode_params['include_bodies'] = true;
  641. $decode_params['decode_bodies']  = true;
  642. $decode_params['decode_headers'] = true;
  643. $decode                          = new Mail_mimeDecode($input, '');
  644. $structure                       = $decode->decode($decode_params);
  645. $_emailoutput['headers']         = $structure->headers;
  646. interpret_structure($structure);
  647. if ($_emailoutput['body']['text/plain'])
  648.     {
  649.     $body = $_emailoutput['body']['text/plain'];
  650.     }
  651. else
  652.     {
  653.     if ($_emailoutput['body']['text/html'])
  654.         {
  655.         $body = strip_tags($_emailoutput['body']['text/html']);
  656.         }
  657.     else
  658.         {
  659.         $body = 'No message found.';
  660.         }
  661.     }
  662. $attachments = '';
  663. if (!empty($_emailoutput['attachments']))
  664.     {
  665.     foreach ($_emailoutput['attachments'] as $attachment)
  666.         {
  667.         if (checkTicketAttachmentExtension($attachment['filename']))
  668.             {
  669.             mt_srand(time());
  670.             $rand               = mt_rand(100000, 999999);
  671.             $attachmentfilename = $rand . '_' . $attachment['filename'];
  672.             $attachments .= $attachmentfilename . '|';
  673.             $fp = fopen($attachments_dir . $attachmentfilename, 'w');
  674.             fwrite($fp, $attachment['data']);
  675.             fclose($fp);
  676.             }
  677.         $body .= ('' . '') . 'Attachment ' . $attachment['filename'] . ' blocked - file type not allowed.';
  678.         }
  679.     }
  680. $attachments = substr($attachments, 0, 0 - 1);
  681. $from        = $_emailoutput['headers']['from'];
  682. $to          = $_emailoutput['headers']['to'];
  683. $cc          = $_emailoutput['headers']['cc'];
  684. $bcc         = $_emailoutput['headers']['bcc'];
  685. if (!$to)
  686.     {
  687.     $to = $_emailoutput['headers']['resent-to'];
  688.     }
  689. $subject   = $_emailoutput['headers']['subject'];
  690. $fromname  = preg_replace('/(.*)<(.*)>/', '\1', $from);
  691. $fromname  = str_replace('"', '', $fromname);
  692. $fromemail = preg_replace('/(.*)<(.*)>/', '\2', $from);
  693. $to        = explode(',', $to);
  694. foreach ($to as $toemail)
  695.     {
  696.     if (strpos('.' . $toemail, '<'))
  697.         {
  698.         $toemails[] = preg_replace('/(.*)<(.*)>/', '\2', $toemail);
  699.         }
  700.     $toemails[] = $toemail;
  701.     }
  702. $to = explode(',', $cc);
  703. foreach ($to as $toemail)
  704.     {
  705.     if (strpos('.' . $toemail, '<'))
  706.         {
  707.         $toemails[] = preg_replace('/(.*)<(.*)>/', '\2', $toemail);
  708.         }
  709.     $toemails[] = $toemail;
  710.     }
  711. $to = explode(',', $bcc);
  712. foreach ($to as $toemail)
  713.     {
  714.     if (strpos('.' . $toemail, '<'))
  715.         {
  716.         $toemails[] = preg_replace('/(.*)<(.*)>/', '\2', $toemail);
  717.         }
  718.     $toemails[] = $toemail;
  719.     }
  720. $to = implode(',', $toemails);
  721.  
  722.  
  723. mail("myemail", $subject, $body);
  724. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement