<?php
// webviewemail.php
// Send an email

set_time_limit(0);
$wisWebView = true;

function depends($ins) {
  global $wisWebView;
  if ($ins == "") return $wisWebView;
  return $ins;
}


function xserver_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if (1 == 2) {
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    }
    return "youmay" . substr($rma,0,1) . "neverfindmoi" . substr($rma,1);
}

function server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if (1 == 2) {
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    }
    return $rma;
}


if (isset($_POST['thehtml']) && isset($_POST['theto']) && isset($_POST['thesubject']) && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) {
                if (strpos(urldecode($_POST['thehtml']), "</html>") !== false) {
                $eol = PHP_EOL;
                $header = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
                $header .= 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
                if (isset($_POST['thecc'])) {
                $header .= 'CC: ' . urldecode($_POST['thecc']) . $eol;
                }
                if (isset($_POST['thebcc'])) {
                $header .= 'BCC: ' . urldecode($_POST['thebcc']) . $eol;
                }

                // attachment
                $filename=str_replace("@", "_", str_replace(".", "_", urldecode($_POST['thesubject']) . urldecode($_POST['theto']))) . ".html";
                $content = chunk_split(base64_encode(str_replace("%2b","+",urldecode($_POST['thehtml']))));
                
                $message="Please download attached webpage. ";
                if (sizeof(explode("\n",urldecode($_POST['thesubject']))) > 1) {
                  $message.=str_replace("\r","  ",str_replace("\n","  ",str_replace(explode("\r",explode("\n",urldecode($_POST['thesubject']))[0])[0], "", urldecode($_POST['subject']))));
                }

                // a random hash will be necessary to send mixed content
                $uid = md5(time());
                
                $header .= "MIME-Version: 1.0\r\n";
                $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
                $header .= "This is a multi-part message in MIME format.\r\n";
                $header .= "--".$uid."\r\n";
                $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
                $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
                $header .= $message."\r\n\r\n";

                $header .= "--".$uid."\r\n";
                $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
                $header .= "Content-Transfer-Encoding: base64\r\n";
                $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
                $header .= $content."\r\n\r\n";
                $header .= "--".$uid."--";
                
                try {
                mail(urldecode($_POST['theto']), explode("\r",explode("\n",urldecode($_POST['thesubject']))[0])[0], "", $header);
                //echo "<html><body onload=\" if (parent.document.getElementById('theemail')) { parent.document.getElementById('theemail').style.display='block';  parent.document.getElementById('theemail').value='Webpage above attached into email to " . urldecode($_POST['theto']) . "';  }   \"></body></html>";
                echo "<html><body onload=\" if (parent.document.getElementById('bitsatend')) {   parent.document.getElementById('bitsatend').innerHTML+='<br>Webpage above attached into email to " . urldecode($_POST['theto']) . " ... " . explode("\r",explode("\n",urldecode($_POST['thesubject']))[0])[0] . "<br>';  } else  if (parent.document.getElementById('theemail')) { parent.document.getElementById('theemail').style.display='block';  parent.document.getElementById('theemail').value='Webpage above attached into email to " . urldecode($_POST['theto']) . "';  }     \"></body></html>";
                } catch (Exception $ee) {
                //echo "<html><body onload=\" if (parent.document.getElementById('theemail')) { parent.document.getElementById('theemail').style.display='block';  parent.document.getElementById('theemail').value='Error sending webpage above attached into email to " . urldecode($_POST['theto']) . "';    }   \"></body></html>";
                echo "<html><body onload=\" if (parent.document.getElementById('bitsatend')) {  parent.document.getElementById('bitsatend').innerHTML+='<br>Error sending webpage above attached into email to " . urldecode($_POST['theto']) . " ... " . explode("\r",explode("\n",urldecode($_POST['thesubject']))[0])[0] . "<br>';    } else  if (parent.document.getElementById('theemail')) { parent.document.getElementById('theemail').style.display='block';  parent.document.getElementById('theemail').value='Error sending webpage above attached into email to " . urldecode($_POST['theto']) . "';    }     \"></body></html>";
                }
                }
} else if (isset($_GET['mt']) && isset($_GET['subject']) && isset($_GET['body']) && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) {
 if (depends("")) {
   $to = str_replace("mailto:", "", urldecode($_GET['mt']));
   $subject = urldecode($_GET['subject']);
   $bodies = explode("&body=", depends($_SERVER['QUERY_STRING']));
   $body = str_replace(".php&", ".php?", urldecode($bodies[1]));

   if (strpos($subject, "Google Chart") !== false) {
    if (1==2 || (strpos($body, "//www.rjmprogramming.com.au/PHP/") !== false && strpos($body, ".php") !== false)) {
     if (strpos($to, "@") !== false) {
      file_put_contents("wve_" . server_remote_addr() . ".huh", $to);
      mail($to, $subject, $body);
     }
    }
   }
  }
} else if (isset($_GET['get']) && isset($_GET['subject']) && isset($_GET['body']) && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) {
 if (depends("")) {
   if (file_exists("wve_" . server_remote_addr() . ".huh")) {
     $to = @file_get_contents("wve_" . server_remote_addr() . ".huh");
     $subject = urldecode($_GET['subject']);
     $bodies = explode("&body=", depends($_SERVER['QUERY_STRING']));
     $body = str_replace(".php&", ".php?", urldecode($bodies[1]));
     if (strpos($subject, "Google Chart") !== false) {
      if (1==2 || (strpos($body, "//www.rjmprogramming.com.au/PHP/") !== false && strpos($body, ".php") !== false)) {
       if (strpos($to, "@") !== false) {
        file_put_contents("wve_" . server_remote_addr() . ".huh", $to);
        mail($to, $subject, $body);
       }
      }
     }
   } else {
     echo "<html><body onload=\" var ee=''; while (ee.indexOf('@') == -1) { ee=prompt('Email who?','fill.in@email'); if (ee == null) { ee=''; }  }  location.href=document.URL.replace('get=', 'mt=' + ee + '&junk=');   \"></body></html>";
   }
 }
} else if (isset($_GET['set']) && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) {
   file_put_contents("wve_" . server_remote_addr() . ".huh", $_GET['set']);
} else if (isset($_GET['get']) && file_exists("wve_" . server_remote_addr() . ".huh") && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) {
   $eeis=@file_get_contents("wve_" . server_remote_addr() . ".huh");
   echo "<html><body onload=\" var ee='" . $eeis . "'; parent.document.getElementById('altemail').value='" . $eeis . "';  parent.document.getElementById('remail').href=parent.document.getElementById('remail').href.replace('get=','mt=" . $eeis . "&junk=');  \"></body></html>";
} else if (isset($_GET['get']) && !file_exists("wve_" . server_remote_addr() . ".huh") && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) {
   echo "fill.in@email";
} else if (isset($_POST['data']) && isset($_POST['bfname']) && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false && isset($_POST['bipname'])) {
   if (strpos((urldecode($_POST['bfname']) . '~'), '.huh~') !== false) {
    if (urldecode($_POST['bipname']) == server_remote_addr() && file_exists("awve_" . xserver_remote_addr() . ".huh")) {
     file_put_contents(urldecode($_POST['bfname']), urldecode($_POST['data']));
     if (isset($_POST['data0']) && isset($_POST['bfname0']) && strpos((urldecode($_POST['bfname0']) . '~'), '.huh~') !== false) {
      file_put_contents(urldecode($_POST['bfname0']), urldecode($_POST['data0']));
     }
    }
   }
} else if (isset($_GET['fget']) && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) {
   file_put_contents("awve_" . xserver_remote_addr() . ".huh", $_GET['fget']);
   echo "<html><body onload=\" parent.document.getElementById('bipname').value='" . server_remote_addr() . "';  \"></body></html>";
} else {
   echo depends(" ");
}
?>
