home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 May / INTERNET103.ISO / pc / software / windows / building / php_nuke / html / modules / webmail / decodemessage.php < prev    next >
Encoding:
PHP Script  |  2002-09-16  |  12.8 KB  |  331 lines

  1. <?php
  2.  
  3. /**************************************************************************/
  4.  #  Mailbox 0.9.2a   by Sivaprasad R.L (http://netlogger.net)             #
  5.  #  eMailBox 0.9.3   by Don Grabowski  (http://ecomjunk.com)              #
  6.  #          --  A pop3 client addon for phpnuked websites --              #
  7.  #                                                                        #
  8.  # This program is distributed in the hope that it will be useful,        #
  9.  # but WITHOUT ANY WARRANTY; without even the implied warranty of         #
  10.  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          #
  11.  # GNU General Public License for more details.                           #
  12.  #                                                                        #
  13.  # You should have received a copy of the GNU General Public License      #
  14.  # along with this program; if not, write to the Free Software            #
  15.  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              #
  16.  #                                                                        #
  17.  #             Copyright (C) by Sivaprasad R.L                            #
  18.  #            Script completed by Ecomjunk.com 2001                       #
  19. /**************************************************************************/
  20.  
  21. #########################################################
  22. #  Class DecodeMessage: Mime message decoder            #
  23. #  by AKAN NKWEINI                                      #
  24. #  akan@p3mail.com                                      #
  25. #  http://www.p3mail.com                                #
  26. #########################################################
  27.  
  28. class DecodeMessage{
  29.   var $header;
  30.   var $body;
  31.   var $fullmessage;
  32.   var $auto_decode = true;
  33.   var $attachment_path;
  34.   var $choose_best = true;
  35.   var $best_format = "text/html";
  36.  
  37.  
  38.   function InitHeaderAndBody($header, $body) {
  39.     $this->header = $header;
  40.     $this->body = $body;
  41.     $this->fullmessage = chop($header)."\t\n\t\n".ltrim($body);
  42.   }
  43.   function Body() {
  44.     return trim($this->body);
  45.   }
  46.   function InitMessage($msg) {
  47.     global $download_dir;
  48.     $i = 0;
  49.     $m = "";
  50.     $messagebody = "";
  51.     $line = explode("\n",trim($msg));
  52.     for ($j=0;$j<count($line);$j++) {
  53.       if (chop($line[$j]) == ""  AND $i < 1) :
  54.         $this->header = $m;
  55.         $i++;
  56.       endif;
  57.       if ($i > 0)
  58.         $messagebody .= $line[$j]."\n";
  59.       $m .= $line[$j]."\n";
  60.     }
  61.     $this->body = $messagebody;
  62.     $this->fullmessage = $msg;
  63.     $this->attachment_path = $download_dir;
  64.   }
  65.  
  66.   function Headers($field="") {
  67.     if ($field == "") :
  68.       return $this->header;
  69.     else :
  70.       $hd = "";
  71.       $field = $field.":";
  72.       $start = 0;
  73.       $j=0;
  74.       $header = eregi_replace("\r", "\n", $this->header);
  75.       $p = explode("\n", $header);
  76.       do {
  77.         for ($i=$start;$i<count($p);$i++) {
  78.           if (ereg("^($field)", $p[$i]))  :
  79.               $position = $i;
  80.               $hd .= ereg_replace("$field", "",$p[$i]);
  81.               break;
  82.             endif;
  83.         }
  84.         if (ereg("^($field)", $p[$i]))  :
  85.           for ($i=$position+1;$i<count($p);$i++) {
  86.             $tok = strtok($p[$i], " ");
  87.             if (ereg(":$", $tok) AND (!(eregi("^($field)", $tok))))
  88.               break;
  89.             $hd .= ereg_replace("$field", "",$p[$i]);
  90.           }
  91.           $start=$i+1;
  92.         endif;
  93.       } while ($j++ < count($p));
  94.     return $hd;
  95.     endif;
  96.   }
  97.  
  98.   function ContentType() {
  99.     $c = $this->Headers("Content-Type");
  100.     $ct = ereg_replace("[[:space:]]", "", $c);
  101.     if (!(ereg(";", $ct))) :
  102.       $content["type"] = trim($ct);
  103.     else :
  104.       $p = explode (";", $ct);
  105.       for ($i=0;$i<count($p);$i++) {
  106.         if (eregi("^(text)", $p[$i])) :
  107.           $content["type"] = $p[$i];
  108.         elseif (eregi("^(multipart)", $p[$i])) :
  109.           $content["type"] = $p[$i];
  110.         elseif (eregi("^(application)", $p[$i])) :
  111.           $content["type"] = $p[$i];
  112.         elseif (eregi("^(message)", $p[$i])) :
  113.           $content["type"] = $p[$i];
  114.         elseif (eregi("^(image)", $p[$i])) :
  115.           $content["type"] = $p[$i];
  116.         elseif (eregi("^(audio)", $p[$i])) :
  117.           $content["type"] = $p[$i];
  118.         elseif (eregi("^(charset)", $p[$i])) :
  119.           $content["charset"] = eregi_replace("(charset=)|(\")", "", $p[$i]);
  120.         elseif (eregi("^(report-type)", $p[$i])) :
  121.           $content["report-type"] = eregi_replace("(report-type=)|(\")", "", $p[$i]);
  122.         elseif (eregi("^(type)", $p[$i])) :
  123.           $content["subtype"] = eregi_replace("(type=)|(\")", "", $p[$i]);
  124.         elseif (eregi("^(boundary)", $p[$i])) :
  125.           $content["boundary"] = eregi_replace("(boundary=)|(\")", "", $p[$i]);
  126.         elseif (eregi("^(name)", $p[$i])) :
  127.           $content["name"] = eregi_replace("(name=)|(\")", "", $p[$i]);
  128.         elseif (eregi("^(access-type)", $p[$i])) :
  129.           $content["access-type"] = eregi_replace("(access-type=)|(\")", "", $p[$i]);
  130.         elseif (eregi("^(site)", $p[$i])) :
  131.           $content["site"] = eregi_replace("(site=)|(\")", "", $p[$i]);
  132.         elseif (eregi("^(directory)", $p[$i])) :
  133.           $content["directory"] = eregi_replace("(directory=)|(\")", "", $p[$i]);
  134.         elseif (eregi("^(mode)", $p[$i])) :
  135.           $content["mode"] = eregi_replace("(mode=)|(\")", "", $p[$i]);
  136.         endif;
  137.       }
  138.     endif;
  139.     return $content;
  140.   }
  141.   function ContentDisposition() {
  142.     $c = $this->Headers("Content-Disposition");
  143.     $c = ereg_replace("[[:space:]]", "", $c);
  144.     if (!(ereg(";", $c))) :
  145.       $cd["type"] = $c;
  146.     else :
  147.       $p = explode(";", $c);
  148.       for ($i=0;$i<count($p);$i++) {
  149.         if (eregi("^(inline)", $p[$i])) :
  150.           $cd["type"] = $p[$i];
  151.         elseif (eregi("^(attachment)", $p[$i])) :
  152.           $cd["type"] = $p[$i];
  153.         elseif(eregi("^(filename)", $p[$i])) :
  154.           $cd["filename"] = eregi_replace("(filename=)|(\")", "", $p[$i]);
  155.         endif;
  156.       }
  157.     endif;
  158.     return $cd;
  159.   }
  160.   function my_array_shift(&$array) {
  161.     reset($array);
  162.     $key = key($array);
  163.     $val = current($array);
  164.     unset($array[$key]);
  165.     return $val;
  166.   }
  167.   function my_array_compact(&$array) {
  168.     while (list($key, $val) = each($array)) :
  169.         if (chop($val) == '')
  170.           unset($array[$key]);
  171.     endwhile;
  172.   }
  173.    function my_in_array($value, $array) {
  174.     while (list($key, $val) = each($array)) :
  175.         if (strcmp($value, $val) == 0)
  176.           return true;
  177.     endwhile;
  178.     return false;
  179.   }
  180.   function Result() {
  181.     global $attachments_view;
  182.     $is_multipart_alternative = false;
  183.     $is_multipart_related = false;
  184.     $found_best = false;
  185.     do {
  186.       $next_message = "";
  187.       do {
  188.         $next_multipart = "";
  189.         $content = $this->ContentType();
  190.         $cd = $this->ContentDisposition();
  191.         if ( eregi("^(multipart)", $content["type"]) ) :
  192.           if ( eregi("multipart/alternative", $content["type"]) ) :
  193.             $is_multipart_alternative = true;
  194.           endif;
  195.           if ( eregi("multipart/related", $content["type"]) ) :
  196.             $is_multipart_related = true;
  197.           endif;
  198.           $boundary = "--".$content["boundary"];
  199.           $p = explode($boundary, $this->body);
  200.           for ($i=0;$i<count($p);$i++) {
  201.             $this->InitMessage($p[$i]);
  202.             $content = $this->ContentType();
  203.             $this->ContentDisposition();
  204.  
  205.             if ($is_multipart_related AND (chop($this->Headers("Content-ID")) != '')) :
  206.               $cont["id"] = ereg_replace("[<>]","", $this->Headers("Content-ID"));
  207.               $cont["name"] = $content["name"];
  208.               $contentid[] = $cont;
  209.               unset($cont);
  210.             endif;
  211.             if (eregi("multipart", $content["type"])) :
  212.               $multiparts[] = $p[$i];
  213.             elseif (eregi("message", $content["type"])) :
  214.               $messages[] = $p[$i];
  215.             elseif ($this->choose_best AND eregi("text/plain", $content["type"]) AND $is_multipart_alternative  AND !($found_best)) :
  216.               $best = $p[$i];
  217.             elseif ($this->choose_best AND eregi($this->best_format, $content["type"]) AND $is_multipart_alternative ) :
  218.               if (eregi("[[:alpha:]]", chop($p[$i]))) :
  219.                 $best = $p[$i];
  220.                 $found_best = true;
  221.               endif;
  222.             elseif (chop($content["type"]) != '' AND chop($this->body) !='') :
  223.               $parts[] = $p[$i];
  224.             endif;
  225.             #echo "<pre>($i)###".htmlspecialchars($this->header)."</pre>--###<hr>";
  226.           }
  227.           if (chop($best) != '') :
  228.             $parts[] = $best;
  229.           endif;
  230.         else :
  231.           if (eregi("(message)", $content["type"])) :
  232.             $messages[] = $this->fullmessage;
  233.           elseif (chop($this->body) != '') :
  234.             $parts[] = $this->fullmessage;
  235.           endif;
  236.         endif;
  237.         unset($is_multipart_alternative);
  238.         unset($best);
  239.         unset($found_best);
  240.         if (count($multiparts) > 0) :
  241.           $next_multipart = $this->my_array_shift($multiparts);
  242.           $this->InitMessage($next_multipart);
  243.         endif;
  244.       } while ($next_multipart != "");
  245.         if (chop($parts) != '') :
  246.  
  247.           for ($i=0;$i<count($parts);$i++) {;
  248.             $this->InitMessage($parts[$i]);
  249.             $ct = $this->ContentType();
  250.             $cd = $this->ContentDisposition();
  251.  
  252.             if (eregi("text/html", $ct["type"]) AND count($contentid > 0)) :
  253.  
  254.               for ($k=0;$k<count($contentid);$k++) {
  255.                 if (ini_get(file_uploads) AND $attachments_view == 1) {
  256.                 $filelocation = $this->attachment_path."/".$contentid[$k]["name"];
  257.                 }
  258.             $cid = $contentid[$k]["id"];
  259.                 $cid = ereg_replace("[[:space:]]", "", $cid);
  260.                 $this->body = str_replace("cid:", "", $this->body);
  261.         if (ini_get(file_uploads) AND $attachments_view == 1) {
  262.                     $this->body = str_replace($cid, $filelocation, $this->body);
  263.         }
  264.               }
  265.             endif;
  266.             if ($this->auto_decode
  267.               AND eregi("attachment", $cd["type"])
  268.               OR eregi("base64", $this->Headers("Content-Transfer-Encoding"))
  269.               ) :
  270.                 $filename = chop($ct["name"]) ? $ct["name"] : $cd["filename"];
  271.                 if (eregi("base64", $this->Headers("Content-Transfer-Encoding"))) :
  272.                   $file = base64_decode($this->body);
  273.                 elseif (eregi("quoted-printable", $this->Headers("Content-Transfer-Encoding"))) :
  274.                   $file = quoted_printable_decode($this->body);
  275.                   $file = ereg_replace("(=\n)", "", $this->body);
  276.                   $file = $this->body;
  277.                 elseif (eregi("7bit", $this->Headers("Content-Transfer-Encoding"))) :
  278.                   $file = $this->body;
  279.                 endif;
  280.             if (ini_get(file_uploads) AND $attachments_view == 1) {    
  281.         $filepath = $this->attachment_path."/".$filename;
  282.                 @unlink($filepath);
  283.                 if (chop($filename != '')) :
  284.                   $fp = @fopen($filepath, "a") OR die("Cannot open file \"$filepath\"");
  285.                   fwrite($fp, $file);
  286.                   fclose($fp);
  287.                   if (eregi("attachment", $cd["type"]) OR eregi("inline", $cd["type"])) :
  288.                     #echo "\n<p><a href=\"$filepath\">$filename</a><p>";
  289.                     $decoded_part["attachments"] = $filename;
  290.                   endif;
  291.                 endif;
  292.         }
  293.             endif;
  294.             if (eregi("^(text)", $ct["type"] )
  295.                 AND !(eregi("text/html", $ct["type"] ))
  296.                 AND !(eregi("attachment", $cd["type"] ))
  297.                 OR (chop($ct["type"]) == "")
  298.                ) :
  299.               $decoded_part["body"]["type"] = $ct["type"];
  300.               $decoded_part["body"]["body"] = $this->body;
  301.             elseif (eregi("text/html", $ct["type"] ) AND !(eregi("attachment", $cd["type"] ))) :
  302.               $decoded_part["body"]["type"] = $ct["type"];
  303.               $decoded_part["body"]["body"] = $this->body;
  304.               #echo "<pre>($parts_count)###".htmlspecialchars($ct["type"])."</pre>--###<hr>";
  305.  
  306.             endif;
  307.              $dp[] = $decoded_part;
  308.              unset($decoded_part);
  309.            }
  310.  
  311.         endif;
  312.         $message[] = $dp;
  313.         unset($dp);
  314.         unset($is_multpart_related);
  315.         unset($contentid);
  316.         unset($parts);
  317.         if (count($messages) > 0) :
  318.           $this->my_array_compact($messages);
  319.           $next_message = $this->my_array_shift($messages);
  320.           $this->InitMessage($next_message);
  321.           $this->InitMessage($this->body);
  322.         endif;
  323.     } while ($next_message != "");
  324.     return $message;
  325.   }
  326.   function MessageID() {
  327.     return ereg_replace("[<>]","",$this->Headers("Message-ID"));
  328.   }
  329. };
  330.  
  331. ?>