<?php
// target_emailer.php
// Send an email for Target game - RJM Programming - March, 2015


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['mt']) && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) {
   $mtm = urldecode(str_replace("mt=","",$_SERVER['QUERY_STRING']));
   $tos = explode("&", $mtm);
   $to = str_replace("mailto:", "", $tos[0]);
   //$subjects = explode("&", $tos[1]);
   $subject = $_GET['subject']; //str_replace("subject=", "", $subjects[0]);
   //$bodys = explode("body=", $mtm);
   $body = $_GET['body']; //$bodys[1];
   //file_put_contents("tzsxc_" . server_remote_addr() . ".huh", $mtm);
   if (strpos($subject, "Target") !== false && strpos($subject, "Share") !== false && strpos($subject, "Challenge") !== false) {
    if (strpos($body, "http://www.rjmprogramming.com.au/PHP/target_share_challenge.php") !== false || strpos($body, "http://www.rjmprogramming.com.au:80/PHP/target_share_challenge.php") !== false) { // && strpos($body, "&ts=") !== false && strpos($body, "%20") === false && strpos($body, " ") === false) {
   //file_put_contents("txsxc_" . server_remote_addr() . ".huh", $body);
     mail($to, $subject, $body);
     echo "<html><head><script type='text/javascript'> function onld2() { var aev=parent.document.getElementById('altemail').value; if (aev == null) { aev='fill.in@email'; } var xh1=parent.document.getElementById('myh1').innerHTML;  if (xh1 != null) { if (xh1.indexOf(' ... Email ') != -1) {  parent.document.getElementById('myh1').innerHTML=xh1.replace(' ... Email ', ' ... Sent Email ').replace('fill.in@email', aev).replace('fill.in@email', aev); } else if (xh1.indexOf(' ... Sent Email ') != -1) { parent.document.getElementById('myh1').innerHTML=xh1.replace(' ... Sent Email ', ' ... Sent Emails ').replace('fill.in@email', aev).replace('fill.in@email', aev);   }   }   } </script></head><body onload=' onld2(); '></body></html>";  
    } 
   }
} else if (isset($_GET['get']) && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) {
   if (file_exists("tsc_" . server_remote_addr() . ".huh")) {
     $bit = @file_get_contents("tsc_" . server_remote_addr() . ".huh");
     echo "<html><head><script type='text/javascript'> function onld() { parent.document.getElementById('altemail').value='" . $bit . "';   } </script></head><body onload=' onld(); '></body></html>";  
   } else {
     echo "fill.in@email";
   }
} else if (isset($_GET['set']) && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) {
   file_put_contents("tsc_" . server_remote_addr() . ".huh", $_GET['set']);
}
?>