<?php
// favourites_midair.php
// Favourites inline HTML email midair database
// November, 2020

$sofar="";
$proposedemail="";
$rval="|" . rand(19876543,39876543) . "|";
$titlebit="";

function ourgetenv($ine) {
  return getenv($ine);
}

function maybelinkvia($ih, $plnk) {
  if (strpos(strtolower($plnk), "https:") !== false || strpos(strtolower($plnk), "http:") !== false) {
    return '<a target=_blank href=' . $plnk . '>' . $ih . '</a>';
  } else if (strlen($plnk) == 11 && str_replace(' ','',$plnk) == $plnk) {
    return '<a target=_blank href=//www.youtube.com/watch?v=' . $plnk . '>' . $ih . '</a>';
  } else if ((strlen(str_replace('-','',$plnk)) == 10 || strlen(str_replace('-','',$plnk)) == 13) && str_replace(' ','',str_replace('-','',$plnk)) == str_replace('-','',$plnk) && trim(str_replace("-","",str_replace("0","",str_replace("1","",str_replace("2","",str_replace("3","",str_replace("4","",str_replace("5","",str_replace("6","",str_replace("7","",str_replace("8","",str_replace("9","",$plnk)))))))))))) == '') {
    return '<a target=_blank href=//www.abebooks.com/servlet/SearchResults?sts=t&isbn=' . $plnk . '>' . $ih . '</a>';
  }
  return $ih;
}

function maybelink($plnk) {  
  if (strpos(strtolower($plnk), "https:") !== false || strpos(strtolower($plnk), "http:") !== false) {
    return '<a target=_blank href=' . $plnk . '>' . $plnk . '</a>';
  } else if (strlen($plnk) == 11 && str_replace(' ','',$plnk) == $plnk) {  // https://www.youtube.com/watch?v=GuJQSAiODqI
    return '<a target=_blank href=//www.youtube.com/watch?v=' . $plnk . '>' . $plnk . '</a>';
  } else if ((strlen(str_replace('-','',$plnk)) == 10 || strlen(str_replace('-','',$plnk)) == 13) && str_replace(' ','',str_replace('-','',$plnk)) == str_replace('-','',$plnk) && trim(str_replace("-","",str_replace("0","",str_replace("1","",str_replace("2","",str_replace("3","",str_replace("4","",str_replace("5","",str_replace("6","",str_replace("7","",str_replace("8","",str_replace("9","",$plnk)))))))))))) == '') {
    return '<a target=_blank href=//www.abebooks.com/servlet/SearchResults?sts=t&isbn=' . $plnk . '>' . $plnk . '</a>';
  }
  return $plnk; // reDGraTP3qU
}

function maybemailto($lto, $psubj) {
  if (strpos($lto, "@") !== false) {
    return '<a title=Email href=mailto:' . trim($lto) . '?subject=' . urlencode(urlencode(str_replace(" ","\t",'Re: ' . $psubj . ' ... have moderated and accepted ...'))) . '&body=' . urlencode(urlencode('http://www.rjmprogramming.com.au/PHP/favourites_midair.php'))  . '>' . $lto . '</a>';
  } else if (trim($lto) != "" && trim(str_replace("0","",str_replace("1","",str_replace("2","",str_replace("3","",str_replace("4","",str_replace("5","",str_replace("6","",str_replace("7","",str_replace("8","",str_replace("9","",$lto))))))))))) == '') {
    return '<a title=SMS href=sms:' . trim($lto) . '&body=' . urlencode(urlencode(str_replace(" ","\t",'Re: ' . $psubj . ' ... have moderated and accepted ... ' . "\n"))) . urlencode(urlencode('http://www.rjmprogramming.com.au/PHP/favourites_midair.php')) . '>' . $lto . '</a>';
  }
  return $lto;
}


if (isset($_POST['favouritename']) && isset($_POST['favouritetype']) && isset($_POST['favouritetimestamp']) && isset($_POST['favouritevalue']) && isset($_POST['favouriteemail']) && isset($_POST['favouriteshare'])) {
  $rval="";
  $titlebit.=" post";

if (isset($_POST['htimestamp'])) {
  //$titlebit.=" 2";

$rval="|" . explode(".",str_replace("+"," ",urldecode($_POST['htimestamp'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['htimestamp']))))] . "|";
$sval=str_replace("|","",$rval) . "|";
$smval=str_replace("|","",$rval) . "|";
$sval.=str_replace("+"," ",urldecode($_POST['favouritetimestamp'])) . "|";
$sval.=str_replace("+"," ",urldecode($_POST['favouritetype'])) . "|";
$sval.=str_replace("+"," ",urldecode($_POST['favouritename'])) . "|";
$sval.=str_replace("+"," ",urldecode($_POST['favouritevalue'])) . "|";
$sval.=str_replace("+"," ",urldecode($_POST['favouriteemail'])) . "|";
$sval.=str_replace("\r"," ... ",str_replace("\n"," ... ",str_replace("\r\n"," ... ",str_replace("+"," ",urldecode($_POST['favouriteshare'])))));

$smval.=str_replace("+"," ",urldecode($_POST['favouritetimestamp'])) . "|";
$smval.=str_replace(">" . str_replace("+"," ",urldecode($_POST['favouritetype'])) . "<", " selected>" . str_replace("+"," ",urldecode($_POST['favouritetype'])) . "<", "<select><option value=Book>Book</option><option value=Movie>Movie</option><option value=Play>Play</option><option value=Song>Song</option><option value=Video>Video</option><option value=Audio>Audio</option></select>") . "|";
$smval.=maybelinkvia(str_replace("+"," ",urldecode($_POST['favouritename'])),str_replace("+"," ",urldecode($_POST['favouritevalue']))) . "|";
$smval.=maybelink(str_replace("+"," ",urldecode($_POST['favouritevalue']))) . "|";
$smval.=maybemailto(str_replace("+"," ",urldecode($_POST['favouriteemail'])),str_replace("+"," ",urldecode($_POST['favouritename']))) . "|";
$smval.=str_replace("+"," ",urldecode($_POST['favouriteshare']));

  //$titlebit.=" " . $sval;

$ssval="<h1>Favourite<a target=_blank href=//www.rjmprogramming.com.au/PHP/favourites_midair.php title=Webpage>s</a></h1>";
$ssval.="<p>Timestamp: " . str_replace("+"," ",urldecode($_POST['favouritetimestamp'])) . "</p>";
$ssval.="<p>Type: " . str_replace(">" . str_replace("+"," ",urldecode($_POST['favouritetype'])) . "<", " selected>" . str_replace("+"," ",urldecode($_POST['favouritetype'])) . "<", "<select><option value=Book>Book</option><option value=Movie>Movie</option><option value=Play>Play</option><option value=Song>Song</option><option value=Video>Video</option><option value=Audio>Audio</option></select>") . "</p>";
$ssval.="<p>Title (by Creator): " . maybelinkvia(str_replace("+"," ",urldecode($_POST['favouritename'])),str_replace("+"," ",urldecode($_POST['favouritevalue']))) . "</p>";
$ssval.="<p>Link: " . maybelink(str_replace("+"," ",urldecode($_POST['favouritevalue']))) . "</p>";
$ssval.="<p>Email: " . maybemailto(str_replace("+"," ",urldecode($_POST['favouriteemail'])),str_replace("+"," ",urldecode($_POST['favouritename']))) . "</p>";
$qssval=$ssval;
$ssval.="<p>Comments: " . str_replace("\n","<br>",str_replace("+"," ",urldecode($_POST['favouriteshare']))) . "</p>";
$qssval.="<div>Comments: <textarea>" . str_replace("\n","' + String.fromCharCode(10) + '",str_replace("+"," ",urldecode($_POST['favouriteshare']))) . "</textarea></div>";
$qssval=str_replace("\r","",str_replace("\n","",str_replace("\r\n","",$qssval)));

$sssval=str_replace("+"," ",urldecode($_POST['favouritetimestamp'])) . "|";
$sssval.=str_replace("+"," ",urldecode($_POST['favouritetype'])) . "|";
$sssval.=str_replace("+"," ",urldecode($_POST['favouritename'])) . "|";
$sssval.=str_replace("+"," ",urldecode($_POST['favouritevalue'])) . "|";
$sssval.=str_replace("+"," ",urldecode($_POST['favouriteemail'])) . "|";
$sssval.=str_replace("\n","<br>",str_replace("+"," ",urldecode($_POST['favouriteshare'])));


$ssxval="<h1>Favourites</h1>";
$ssxval.="<p>Timestamp: " . str_replace("+"," ",urldecode($_POST['favouritetimestamp'])) . "</p>";
$ssxval.="<p>Type: " . str_replace("+"," ",urldecode($_POST['favouritetype'])) . "</p>";
$ssxval.="<p>Title (by Creator): " . str_replace("+"," ",urldecode($_POST['favouritename'])) . "</p>";
$ssxval.="<p>Link: " . str_replace("+"," ",urldecode($_POST['favouritevalue'])) . "</p>";
$ssxval.="<p>Email: " . str_replace("+"," ",urldecode($_POST['favouriteemail'])) . "</p>";
$ssxval.="<p>Comments: " . str_replace("\n","<br>",str_replace("+"," ",urldecode($_POST['favouriteshare']))) . "</p>";


$proposedemail="  if (document.URL.toLowerCase().indexOf('rjmprogramming.com.au') != -1) { \n 
  zhr = new XMLHttpRequest(); \n
  zform=new FormData(); \n
  zform.append('inline', 'y'); \n
  zform.append('to', 'rmetcalfe15@gmail.com'); /" . "/ 'rmetcalfe@rjmprogramming.com.au'); \n
  zform.append('cc', 'rmetcalfe@rjmprogramming.com.au'); \n
  zform.append('subj', 'Please moderate Favourite ...'); \n
  zform.append('body', '<body>" . $qssval . "<form action=' + document.URL.split('#')[0].split('?')[0] + ' method=GET><input type=hidden name=okyes value=\"" . $sval . "\"></input><input type=submit value=Okay?></input></form></body>');
  zhr.open('post', 'http://www.rjmprogramming.com.au/HTMLCSS/emailhtml.php', true); \n
  zhr.onreadystatechange = showStuff;
  zhr.send(zform); \n 
  } else if (document.URL.toLowerCase().indexOf('/localhost') != -1) { \n 
  var moreu='" . urlencode(str_replace("</h1>"," ... ",str_replace("</p>"," ... ",str_replace("<p>","",str_replace("<h1>","",$ssxval))))) . "';
  var morev='';
  document.getElementById('aemail').href+=moreu + '&body=' + encodeURIComponent(morev + document.URL.split('#')[0].split('?')[0] + '?okyes=' + encodeURIComponent('" . $sval . "')); \n
  document.getElementById('aemail').click(); \n 
  } \n 
  \n";
}

} else if (isset($_GET['okyes'])) {
  $rval="";
  $newrow="<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>";
  //echo "here";
  //exit;
  if ($_GET['okyes'] != '') {
   $oks=explode("|", str_replace("+"," ",urldecode($_GET['okyes'])));
   //echo '' . sizeof($oks);
   //exit;
   if (sizeof($oks) >= 7 && file_exists(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html")) {
    $lastone=str_replace($oks[0] . "|" . $oks[1] . "|" . $oks[2] . "|" . $oks[3] . "|" . $oks[4] . "|" . $oks[5] . "|", "", str_replace("+"," ",urldecode($_GET['okyes'])));
    $bigsofar=file_get_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html");
    if (strpos($bigsofar, "|" . $oks[0] . "|") !== false) {
      $bigsofar=str_replace("|" . $oks[0] . "|", "", $bigsofar);
      $newrow="<tr><td>" . $oks[1] . "</td><td>" . $oks[2] . "</td><td>" . maybelinkvia($oks[3],$oks[4]) . "</td><td>" . maybelink($oks[4]) . "</td><td>" . maybemailto($oks[5],$oks[3]) . "</td><td>" . str_replace(" ... ","<br>",$lastone) . "</td></tr>";
      if (strpos($bigsofar, "<tr><td") !== false) {
        $coks=explode("<tr><td", $bigsofar);
        $bigsofar=str_replace($coks[0], $coks[0] . $newrow, $bigsofar);
        file_put_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html", $bigsofar);
      } else if (strpos($bigsofar, "</table>") !== false) {
        $coks=explode("</table>", $bigsofar);
        $bigsofar=str_replace($coks[0], $coks[0] . $newrow, $bigsofar);
        file_put_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html", $bigsofar);
      }
    }
   }
  }
} 

if (!file_exists(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html")) {
  file_put_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html", "<!doctype html>
<html>
<head>
<title>Favourites - RJM Programming - November, 2020</title>
<meta name='viewport' content='width=device-width, initial-scale=1, minimum-scale=0.25, maximum-scale=8, user-scalable=yes' />
<meta charset='utf-8' />
<style>
 input { margin-left: 2%; width:95%; }
 textarea { margin-left: 2%; width:95%; }
 select { margin-left: 2%; }
</style>
</head>
<body>
<h1>Favourites</h1>
<h3>Moderated by <a href='mailto:rmetcalfe@rjmprogramming.com.au?Subject=Favourites'>RJM Programming</a> - November, 2020</h3>
<h4 style=display:none;>" . $rval . "</h4>
<table border=1 style='width:100%;'>
<tr><th colspan=6 style='background-color:yellow;'><br><br><br><br>Past Favourites (as moderated)</th></tr>
<tr><th id='thfavouritetimestamp'>Datetime</th><th id='thfavouritetype'>Type</th><th id='thfavouritename'>Title (by Creator)</th><th id='thfavouritevalue' title='http: or https: URL or 11 character YouTube ID or 10 or 13 digit ISBN'>Link</th><th id='thfavouriteemail' title='Email or SMS number'>Email</th><th id='thfavouriteshare''>Comments</th></tr>
</table>
</body>
</html>");
} else if ($rval != "") {
    $bigsofar=file_get_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html");
    if (strpos($bigsofar, "</h4>") !== false) {
      $bigsofar=str_replace("</h4>", $rval . "</h4>", $bigsofar);
      file_put_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html", $bigsofar);
    }
}

if (file_exists(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html")) {
  $bigsofar=file_get_contents(ourgetenv("HOME") . DIRECTORY_SEPARATOR . "favourites_midair.html");
  if (strpos($bigsofar, "<table") !== false && strpos($bigsofar, "</table>") !== false) {
    $sofar="<table" . explode("<table", explode("</table>", $bigsofar)[0])[1] . "</table>";
  }
} 

echo "<!doctype html>
<html>
<head>
<title>Favourites - RJM Programming - November, 2020" . $titlebit . "</title>
<meta name='viewport' content='width=device-width, initial-scale=1, minimum-scale=0.25, maximum-scale=8, user-scalable=yes' />
<meta charset='utf-8' />
<style>
 input { margin-left: 2%; width:95%; }
 textarea { margin-left: 2%; width:95%; }
 select { margin-left: 2%; }
</style>
<script type='text/javascript'>

var alm='Sent for moderation to RJM Programming';

var zhr=null;
var zform=null;

 Date.prototype.toDateInputValue = (function() {  // thanks to https://stackoverflow.com/questions/6982692/how-to-set-input-type-dates-default-value-to-today
    var local = new Date(this);
    local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
    return local.toJSON().split('.')[0].replace('T',' '); //.slice(0,10);
 });

function showStuff(evt) {
  if (zhr != null) {
  if (zhr.readyState == 4) {
    if (zhr.status == 200) {
    alert(alm);
    }
  }
  }
 }
 
 function ol() { 
" . $proposedemail . " 
  var sv=new Date().toDateInputValue();
  //alert(sv);
  document.getElementById('favouritetimestamp').value='' + sv;
  document.getElementById('htimestamp').value='' + sv + '.' + '" . str_replace("|","",$rval) . "';
  document.getElementById('favouritename').focus();
 }
 
 function ouralert() {
  alert(alm);
 }
 
 function fs() {
  if (document.getElementById('favouritename').value.trim() == '') { 
    alm=('Not sent for moderation due to incomplete form below'); 
    setTimeout(ouralert,2000); 
    return false; 
  } else if (document.URL.toLowerCase().indexOf('rjmprogramming.com.au') != -1) {  
    alm=('Sent for moderation to RJM Programming'); 
    //setTimeout(ouralert,2000); 
    return true; 
  } 
  return true;
 }
</script>
</head>
<body onload=ol();>
<h1>Favourites</h1>
<h3>Moderated by <a href='mailto:rmetcalfe@rjmprogramming.com.au?Subject=Favourites'>RJM Programming</a> - November, 2020</h3>
<form onsubmit=\" return fs(); \" method=POST action=./favourites_midair.php>
<input id=htimestamp name=htimestamp type=hidden value=''></input>
<table border=1 style='width:100%;'>
<tr><th colspan=6><input title='Click to have your entry below be moderated by RJM Programming' style='background-color: pink;font-weight:bold;' type=submit value='Favourite'></input></th></tr>
<tr><th id='thfavouritetimestamp'>Datetime</th><th id='thfavouritetype'>Type</th><th id='thfavouritename'>Title (by Creator)</th><th id='thfavouritevalue' title='http: or https: URL or 11 character YouTube ID or 10 or 13 digit ISBN'>Link</th><th id='thfavouriteemail' title='Email or SMS number'>Email</th><th id='thfavouriteshare''>Comments</th></tr>
<tr><td id='tdfavouritetimestamp'><input id=favouritetimestamp name=favouritetimestamp type=datetime value=''></input></td><td id='tdfavouritetype'><select id=favouritetype name=favouritetype><option value=Book>Book</option><option value=Movie>Movie</option><option value=Play>Play</option><option value=Song>Song</option><option value=Video>Video</option><option value=Audio>Audio</option></select></td><td id='tdfavouritename'><input id=favouritename name=favouritename type=text value=''></input></td><td id='tdfavouritevalue'><input name=favouritevalue type=text value=''></input></td><td id='tdfavouriteemail'><input name=favouriteemail type=text value=''></input></td><td id='tdfavouriteshare''><textarea name=favouriteshare value=''></textarea></td></tr>
</table>
</form>
<a id=aemail style=display:none; href='mailto:rmetcalfe@rjmprogramming.com.au?subject='>Email</a><input type=text value='' style='position:absolute;top:-3000px;left:-3000px;'></input>
" . $sofar . "
</body>
</html>";




?>