<?php
// at_poc.php
// RJM Programming
// June, 2017

$localtime = localtime();
$localtimestamp = (0 + $localtime[3]) . "/" . (1 + $localtime[4]) . "/" . (1900 + $localtime[5]) . "-" . $localtime[2] . ":" .  $localtime[1] . ":" .  $localtime[0]; 
$proposedfn=dirname(__FILE__) . '/my_at_poc_' . apserver_remote_addr() . ".htm";
$proposedun="http://" . $_SERVER['SERVER_NAME'] . ":" . str_replace('/' . '/', '/',$_SERVER['SERVER_PORT'] . str_replace('at_poc.php','',str_replace(explode('at_poc.php',$_SERVER['REQUEST_URI'])[1],'',$_SERVER['REQUEST_URI'])) . '/my_at_poc_' . apserver_remote_addr() . ".htm");

function apserver_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 str_replace("/","_",str_replace(":","_",$rma));
}


if (!isset($_GET['content'])) {
echo "<!doctype html>
<html>
<head>
<script type='text/javascript'>
var timestamp=new Date().toDateString() + '-' + new Date().toTimeString();
function randbit() {
document.getElementById(\"mya\").href=document.getElementById(\"mya\").href.replace(\"?rand=x\",\"?rand=\" + Math.random());
}
function ask() {
var subcont=prompt('Please, optionally, enter your own content on webpage that will disappear after 2 to 3 minutes from when you click the OK button.','');
var newtimestamp=new Date().toDateString() + '-' + new Date().toTimeString();
if (subcont != null) {
    location.href=document.URL.split('#')[0].split('?')[0] + '?content=' + encodeURIComponent(document.getElementById(\"myp\").innerHTML.replace(timestamp, newtimestamp) + subcont);
}
}
</script>
</head>
<body onload=' randbit(); document.getElementById(\"myp\").innerHTML=\"You are seeing a page that will disappear 2 to 3 minutes after \" + timestamp + \".  This is your default web page wording, and whatever follows is your <a id=mya target=_blank href=" . $proposedun . "?rand=x>own content</a>. \"; ask();'>
<p id=myp>You are seeing a page that will disappear 2 to 3 minutes after " . $localtimestamp . ".  This is your default web page wording, and whatever follows is your <a id=mya target=_blank href=" . $proposedun . "?rand=x>own content</a>.  </p>
</body>
</html>";
} else {
echo "<!doctype html>
<html>
<head>
<script type='text/javascript'>
function randbit() {
document.getElementById(\"mya\").href=document.getElementById(\"mya\").href.replace(\"?rand=x\",\"?rand=\" + Math.random());
}
</script>
</head>
<body onload=' randbit(); '>
<p id=myp>" . urldecode($_GET['content']) . " </p>
 </body>
 </html>";
if (!file_exists($proposedfn)) {
  file_put_contents($proposedfn, "<!doctype html>
<html>
<head>
<script type='text/javascript'>
function randbit() {
document.getElementById(\"mya\").href=document.getElementById(\"mya\").href.replace(\"?rand=x\",\"?rand=\" + Math.random());
}
</script>
</head>
<body onload=' randbit(); '>
<p id=myp>" . urldecode($_GET['content']) . " </p>
 </body>
 </html>");
 chmod($proposedfn, 0666);
 file_put_contents($proposedfn . ".ksh", "rm -f " . $proposedfn . "\n" . "rm -f " . $proposedfn . ".ksh" . "\n");
 chmod($proposedfn . ".ksh", 0777);
 exec("at now + 2 minutes < " . $proposedfn . ".ksh");
}
 }
 ?>
 