<?php

if ( ! @include_once( './mainfile.php' ) ) { exit( '!! INCLUDE ERROR. ACCESS DENIED !!' ); }
error_reporting( _E_VALUE );


/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 RC2                                          |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: myPHPNuke Dev Team                                           |
// +----------------------------------------------------------------------+
// | Copyright: © 2001-2004 myPHPNuke Dev Team                            |
// +----------------------------------------------------------------------+
// $Id: readmail.php,v 1.8.2.4 2004/08/01 17:04:18 kirilt Exp $



/*************************************************************************/
 #  Mailbox 0.9.2a   by Sivaprasad R.L (http://netlogger.net)              #
 #                                                                        #
 #          --  A pop3 client addon for phpnuked websites --              #
 #                                                                        #
 # This program is distributed in the hope that it will be useful,        #
 # but WITHOUT ANY WARRANTY; without even the implied warranty of         #
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          #
 # GNU General Public License for more details.                           #
 #                                                                        #
 # You should have received a copy of the GNU General Public License      #
 # along with this program; if not, write to the Free Software            #
 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              #
 #                                                                        #
 #             Copyright (C) by Sivaprasad R.L                            #
 #                                                                        #
/*************************************************************************/

  if( ! _MPN_USER )
  {
      $_SERVER['F_LIB'] -> redirectRequest( 'user.php' );
      exit;
  }

  global $user, $mainfile, $op;
  global $bgcolor1, $bgcolor2, $bgcolor3, $id;



  //if(!isset($mainfile)) include "mainfile.php";
//  parse_str(base64_decode($pop3_cookie));
  require("pop3.inc.php");
  require("decodemessage.inc.php");
  include "header.php";
  include "mailheader.php";
  include "class.rc4crypt.php";

  if(!isset($id)) {
      echo "Error: Invalid Parameter<br>";
      include "mailfooter.php";
      exit();
  }

  $query = "Select * from ".$mpnTables['popsettings']." where id = $id";
  if(($res = mysql_query($query)) && (mysql_num_rows($res) > 0)) {
      $row = mysql_fetch_array($res);
      $uid = $row['uid'];

      $auser = base64_decode($user);
      $userdata = explode(":", $auser);
      $userid = $userdata[0];

      if($uid != $userid) {
          echo "<center><h2>Error: Permission denied</center>";
          include "mailfooter.php";
          exit();
      }
      $server = $row['popserver'];
      $port = $row['port'];
      $username = $row['uname'];

       $rc4 = new rc4crypt();
      $password = $rc4->encrypt($username,$row['passwd']);
  } else {
      echo "Error: POP Server not set properly<br>";
      include "mailfooter.php";
      exit();
  }

  $ms = $msgid;
  @set_time_limit(0);
  $pop3=new POP3($server,$username,$password);
  $pop3->Open();
  $message = $pop3->GetMessage($ms) ;
  $s = $pop3->Stats() ;
  $body = $message["body"];
  $header = $message["header"];
  $full = $message["full"];
  $pop3->Close();

  $d = new DecodeMessage;
  $d->InitMessage($full);
  $from_address = chop($d->Headers("From"));
  $to_address = chop($d->Headers("To"));
  $subject = $d->Headers("Subject");
  $cc = chop($d->Headers("Cc"));
  $replyto = chop($d->Headers("Reply-To:"));

  echo "<table border=0 width=100%>
       <tr bgcolor=\"$bgcolor1\">
           <td align=left>". translate("From:") . "</td>
           <td>".htmlspecialchars($from_address)."</td>
       </tr>

       <tr bgcolor=\"$bgcolor4\">
           <td align=left>" . translate("To:") . "</td>
           <td>".htmlspecialchars($to_address)."</td>
       </tr>";

  if($cc != "") {
      echo "<tr bgcolor=\"$bgcolor4\">
            <td align=left>" . translate("cc:") . "</td>
            <td>".htmlspecialchars($cc)."</td>
            </tr>";
  }

  echo "
       <tr bgcolor=\"$bgcolor1\">
           <td align=left>" . translate("Subject:") . "</td>
           <td>".htmlspecialchars($subject)."</td>
       </tr>
       <tr bgcolor=\"$bgcolor4\">
           <td align=left>" . translate("Date:") . "</td>
           <td>".htmlspecialchars($d->Headers("Date")) ."</td>
       </tr>
       <tr bgcolor=\"$bgcolor3\">
           <td colspan=2>";

  $message = $d->Result();
  $att_txt = null;
  $rtext = "";
  for ($j=0;$j<count($message);$j++) {
      for ($i=0;$i<count($message[$j]);$i++) {
//          if (chop($message[$j][$i]["attachments"]) != '') :
          if (isset( $message[$j][$i]["attachments"] ) && $message[$j][$i]["attachments"] != '') :
              $att_txt .= " <a href=\"".$d->attachment_path."/".$message[$j][$i]["attachments"]."\">".$message[$j][$i]["attachments"]."</a><br><br>";
          endif;
      }
      for ($i=0;$i<count($message[$j]);$i++) {
          if (eregi("text/html", $message[$j][$i]["body"]["type"])) {
              $res = quoted_printable_decode($message[$j][$i]["body"]["body"]);
              $res = ereg_replace("(=\n)", "", $res);
              $res = eregi_replace("(<body)", "<xbody", $res);
              $res = eregi_replace("(<meta)", "<xmeta", $res);
              echo "<br>";
              echo $res;
          } else {
              echo nl2br(htmlspecialchars($message[$j][$i]["body"]["body"]))."<br>";
          }
           $content = $rtext .= strip_tags($message[$j][$i]["body"]["body"]);
      }
  }
  echo "</td></tr></table>";

  echo"<br><br><table border=0 width=100% cellspacing=0>
                <tr bgcolor=\"$bgcolor1\">
                 <td align=center>
                  <form action=\"inbox.php\" method=\"post\">
                   <input type=hidden name=\"id\" value=\"$id\">
                   <input type=hidden name=\"op\" value=\"delete\">
                   <input type=hidden name=\"msgid\" value=\"$msgid\">
                   <input class='button' type=submit value=\"" . translate("delete") . "\">
                  </form>
                 </td>
                 <td align=center>
                  <form action=compose.php method=\"post\">
                   <input type=hidden name=to value=\"".htmlspecialchars($from_address)."\">
                   <input type=hidden name=subject value=\"".htmlspecialchars($subject)."\">
                   <input type=hidden name=body value=\"".htmlspecialchars($content)."\">
                   <input type=hidden name=op value=\"" . translate("reply") . "\">
                   <input class='button' type=submit value=reply>
                  </form>
                 </td>
                 <td align=center>
                  <form action=\"compose.php\" method=\"post\">
                   <input type=hidden name=\"to\" value=\"".htmlspecialchars($from_address)."\">
                   <input type=hidden name=\"subject\" value=\"".htmlspecialchars($subject)."\">
                   <input type=hidden name=\"body\" value=\"".htmlspecialchars($content)."\">
                   <input type=hidden name=\"op\" value=\"forward\">
                   <input class='button' type=submit value=\"" . translate("forward") . "\">
                  </form>
                 </td>
                </tr>
               </table>";

  if(isset($att_txt)) {
      echo "<br><table align=center border=1 bordercolor=$bgcolor1 width=50%><tr><td bgcolor=$bgcolor1>
            <br><b> Attachments: </b></td></tr>";
      echo "<tr><td bgcolor=$bgcolor3>$att_txt</td></tr></table><br><Br>";
  }


  include "mailfooter.php";
?>
