<?php
// whatdowestandfor.php
// Email helper for whatdowestandfor.html
// RJM Programming
// April, 2016


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($_GET['get']) ) { // && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) {
   if (file_exists("wes_" . server_remote_addr() . ".huh")) {
     $bit = @file_get_contents("wes_" . server_remote_addr() . ".huh");
     echo "<html><head><script type='text/javascript'> function onld() { parent.document.getElementById('mylemail').value='" . $bit . "';   } </script></head><body onload=' onld(); '></body></html>";
   } else {
     echo "fill.in.email@address";
   }
} else if (isset($_GET['set']) ) { // && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) {
   file_put_contents("wes_" . server_remote_addr() . ".huh", $_GET['set']);
}

if (isset($_GET['mode']) ) { // && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) {
  if ($_GET['mode'] != '' && isset($_POST['mydurl']) && isset($_POST['myfname'])) {
   if (strlen(str_replace(".png*", "", ($_POST['myfname'] . "*"))) != strlen(($_POST['myfname'] . "*")) && strlen(str_replace("*whatdowestandfor_", "", ("*" . $_POST['myfname']))) != strlen(("*" . $_POST['myfname']))) {
    $idata = base64_decode($_POST['mydurl']);
    file_put_contents($_POST['myfname'], $idata);
    if (isset($_POST['mymobile'])) {
     if ($_POST['mymobile'] != "") {
      $mtm = urldecode($_POST['mymobile']);
      $tos = explode("?", $mtm);
      $to = str_replace("mailto:", "", $tos[0]);
      $subjects = explode("&", $tos[1]);
      $subject = str_replace("subject=", "", $subjects[0]);
      $bodys = explode("body=", $tos[1]);
      $body = $bodys[1];
      if ($body == "") {
        $prebody="http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
        $bodys = explode("?", $prebody);
        $body=str_replace("whatdowestandfor.php", $_POST['myfname'], $bodys[0]);
      }
      if (strpos(str_replace("fill.in.email@address", "", $to), "@") !== false) {
        file_put_contents("wes_" . server_remote_addr() . ".huh", $to);
        if (strpos($subject, "...") !== false) mail($to, $subject, $body);
      }
     }
    }
   }
  }
} 

?>
