<?php
// php_listener.php
// Listener on both command line and for web browser version
// RJM Programming http://www.rjmprogramming.com.au
// August, 2020
set_time_limit(0);
session_start();
$enduring='' . session_id();
if (isset($_GET['sid'])) {
 if (strpos($_SERVER['HTTP_REFERER'], "/php_listener.php") !== false) {
  $enduring=str_replace("+"," ",urldecode($_GET['sid']));
 }
} else if (isset($_POST['sid'])) {
 if (strpos($_SERVER['HTTP_REFERER'], "/php_listener.php") !== false) {
  $enduring=str_replace("+"," ",urldecode($_POST['sid']));
 }
}
while (strlen($enduring) < 35) {
  $wase=$enduring;
  $enduring='0' . $wase;
}
$dbit=' data-oe="" ';
$scont="";
$datait="";
$av="";
if (file_exists("x_" . $enduring . ".html")) {
  if (strpos(file_get_contents("x_" . $enduring . ".html"),  "<audio") !== false) {
    $av.="<audio" . explode("</audio>",explode("<audio", file_get_contents("x_" . $enduring . ".html"))[1])[0] . "</audio><br>"; 
  }
  if (strpos(file_get_contents("x_" . $enduring . ".html"),  "<video") !== false) {
    $av.="<video" . explode("</video>",explode("<video", file_get_contents("x_" . $enduring . ".html"))[1])[0] . "</video><br>"; 
  }
}

function server_remote_addr() {
    global $enduring;
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(opr)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(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;
    else
            $rma = '0000000'.$rma;
    return str_replace(".", "x", str_replace(":", "x", $rma . $enduring));
}

if ($argv) {  // command line ...
    $cfindings="";
    $goes=0;
    $howmanygoes="-1";
    $par=getenv("TERM"); // thanks to https://stackoverflow.com/questions/3214935/can-a-bash-script-tell-if-its-being-run-via-cron
    if ("$par" == "" || "$par" == "dummy") {  // via cron
      $cfindings1=exec("crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 1");
      $cfindings2=exec("crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 2");
      $cfindings3=exec("crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 3");
      $cfindings4=exec("crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 4");
      $cfindings5=exec("crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 5");
      if (strpos($cfindings1, "*/") !== false) {
        $howmanygoes=12 * explode("*/", $cfindings1)[1];
      } else if (strpos($cfindings2, "*/") !== false) {
        $howmanygoes=720 * explode("*/", $cfindings2)[1];
      } else if (strpos($cfindings3, "*/") !== false) {  
        $howmanygoes=17280 * explode("*/", $cfindings3)[1];
      }
    }
    while (1) {
    foreach (glob(dirname(__FILE__) . "/chat_*.rls") as $ourfilename) {      
      //rename($ourfilename, explode("__", $ourfilename)[0] . ".line");
      rename($ourfilename, explode(".rls", $ourfilename)[0] . ".line");
    }
    foreach (glob(dirname(__FILE__) . "/chat_*.RLS") as $ourfilename) {      
      //rename($ourfilename, explode("__", $ourfilename)[0] . ".line");
      rename($ourfilename, explode(".RLS", $ourfilename)[0] . ".lin2");
    }
    if ($howmanygoes >= 0) { $goes++; if ($goes >= $howmanygoes) {  exit; } }
    sleep(5);
    }
    exit;
} else if (isset($_GET['ipaddress']) && isset($_GET['ipfromaddress']) && isset($_GET['ichat'])) { 
   //file_put_contents("c_" . str_replace("+"," ",urldecode($_GET['ipaddress'])) . "__" . server_remote_addr() . ".rxec", str_replace("+"," ",urldecode($_GET['ichat'])));
   file_put_contents("c_" . str_replace("+"," ",urldecode($_GET['ipaddress'])) . "__" . server_remote_addr() . ".rec", str_replace("+"," ",urldecode($_GET['ichat'])));
   if (564 == 563) {
   $onef="";
   $spec="c_" . server_remote_addr() . "__*.rec";
   if (isset($_GET['spec'])) {
     $spec=str_replace("+"," ",urldecode($_GET['spec']));
   }
    if (1) {
    //foreach (glob("c_" . server_remote_addr() . "__*.rec") as $ourfilename) { 
    //foreach (glob("c_*" . server_remote_addr() . "*.rec") as $ourfilename) { 
    //foreach (glob("c_*__" . server_remote_addr() . ".rec") as $ourfilename) { 
    //foreach (glob("c_" . server_remote_addr() . "__*.rec") as $ourfilename) { 
    foreach (glob($spec) as $ourfilename) { 
    if (1 == 1) {
      //echo "<html><body onload=\" parent.document.getElementById('chatin').value='" . str_replace("\n", "' + String.fromCharCode(10) + '",file_get_contents($ourfilename)) . "';  parent.document.getElementById('ichat').value='';  \"></body></html>";
      echo "<html><body onload=\" if (parent.document.getElementById('ichat').value != '" . file_get_contents($ourfilename) . "') { parent.document.getElementById('tdchatin').innerHTML+=parent.mosay('" . str_replace("\n", "<br>",file_get_contents($ourfilename)) . "<br>');   parent.document.getElementById('ichat').value='';  }  \"></body></html>";
      $onef=$ourfilename;
      //unlink($ourfilename);
      //exit;
    }
      //exit;
    }
    if (1 == 4) { sleep(5); }
    }
    if ($onef != "") {
      sleep(6);
      if ($onef != "") { if (file_exists($onef)) { unlink($onef); } }
    }
    }
   exit;
} else if (isset($_POST['ipaddress']) && isset($_POST['ichat'])) { 
   $delthis="";
   $fiso="";
   //file_put_contents("c_" . str_replace("+"," ",urldecode($_POST['ipaddress'])) . "__" . server_remote_addr() . ".rxec", str_replace("+"," ",urldecode($_POST['ichat'])));
   if (isset($_POST['dbit'])) {
   file_put_contents("c_" . str_replace("+"," ",urldecode($_POST['ipaddress'])) . "__" . str_replace("+"," ",urldecode($_POST['ipfromaddress'])) . ".rec", str_replace("+"," ",urldecode($_POST['ichat'])));
   $fiso=" parent.significantother='" . substr(str_replace("+"," ",urldecode($_POST['ipaddress'])),strlen(str_replace("+"," ",urldecode($_POST['ipaddress']))) - strlen($enduring),strlen($enduring)) . "'; ";
   file_put_contents("x_" . substr(str_replace("+"," ",urldecode($_POST['ipaddress'])),strlen(str_replace("+"," ",urldecode($_POST['ipaddress']))) - strlen($enduring),strlen($enduring)) . ".htm", "<p" . $dbit . ">" . str_replace("+"," ",urldecode($_POST['ichat'])) . "</p>");
   //if (file_exists("x_" . $enduring . ".html")) {
   //$delthis="x_" . substr(str_replace("+"," ",urldecode($_POST['ipaddress'])),strlen(str_replace("+"," ",urldecode($_POST['ipaddress']))) - strlen($enduring),strlen($enduring)) . ".html";
   //file_put_contents("x_" . substr(str_replace("+"," ",urldecode($_POST['ipaddress'])),strlen(str_replace("+"," ",urldecode($_POST['ipaddress']))) - strlen($enduring),strlen($enduring)) . ".html", file_put_contents("x_" . $enduring . ".html"));
   //}
   } else {
   file_put_contents("c_" . str_replace("+"," ",urldecode($_POST['ipaddress'])) . "__" . server_remote_addr() . ".rec", str_replace("+"," ",urldecode($_POST['ichat'])));
   }
   $onef="";
   $spec="c_" . server_remote_addr() . "__*.rec";
   if (isset($_POST['spec'])) {
     $spec=str_replace("+"," ",urldecode($_POST['spec']));
   }
   //if ($delthis != "") {
   //  sleep(32);
   //  if (file_exists($delthis)) {  unlink($delthis);  }
   //}
   if ($fiso != "") {
    echo "<html><body onload=\"" . $fiso . "\"></body>";
   } else if (567 == 568) {
    if (1) {
    //foreach (glob("c_" . server_remote_addr() . "__*.rec") as $ourfilename) { 
    //foreach (glob("c_*" . server_remote_addr() . "*.rec") as $ourfilename) { 
    //foreach (glob("c_*__" . server_remote_addr() . ".rec") as $ourfilename) { 
    foreach (glob($spec) as $ourfilename) { 
    if (1 == 1) {
      //echo "<html><body onload=\" parent.document.getElementById('chatin').value='" . str_replace("\n", "' + String.fromCharCode(10) + '",file_get_contents($ourfilename)) . "';   parent.document.getElementById('ichat').value='';   \"></body></html>";
      //echo "<html><body onload=\" if (7 == 7 || parent.document.getElementById('ichat').value != '" . file_get_contents($ourfilename) . "') { parent.document.getElementById('tdchatin').innerHTML+='7:" . str_replace("\n", "<br>",file_get_contents($ourfilename)) . "<br>" . $ourfilename . "<br>found " . server_remote_addr() . " " . $spec . " this<br>';   parent.document.getElementById('ichat').value='';  }  \"></body></html>";
      echo "<html><body onload=\"" . $fiso . " if (parent.document.getElementById('ichat').value != '" . explode("\n",file_get_contents($ourfilename))[0] . "') { parent.document.getElementById('tdchatin').innerHTML+=parent.mosay('" . str_replace("\n", "<br>",file_get_contents($ourfilename)) . "<br>');   parent.document.getElementById('ichat').value='';  }  \"></body></html>";
      $onef=$ourfilename;
      //unlink($ourfilename);
      //exit;
    }
      //exit;
    }
    if (1 == 4) { sleep(5); }
    }
    if ($onef != "") {
      sleep(6);
      //if ($onef != "") { if (file_exists($onef)) { unlink($onef); } }
    }
    }
   exit;
} else if (isset($_GET['nothing'])) { 
  //if (file_exists("x_" . $enduring . ".htm")) {
  //  sleep(5);
  //  if (file_exists("x_" . $enduring . ".htm")) { unlink("x_" . $enduring . ".htm"); }
  //}
  exit;
} else if (isset($_GET['address']) && isset($_GET['ipaddress'])) { 
   if (strpos(str_replace("@","",str_replace("+"," ",urldecode($_GET['address']))), "#") !== false) {
   file_put_contents("chat_" . explode("#", str_replace("@","",str_replace("+"," ",urldecode($_GET['address']))))[0] . "__" . str_replace("+"," ",urldecode($_GET['ipaddress'])) . ".RLS", str_replace("+"," ",urldecode($_GET['ichat'])));
   } else {
   file_put_contents("chat_" . explode("#", str_replace("@","",str_replace("+"," ",urldecode($_GET['address']))))[0] . "__" . str_replace("+"," ",urldecode($_GET['ipaddress'])) . ".rls", str_replace("+"," ",urldecode($_GET['ichat'])));
   }
   exit;
} else if (isset($_POST['nomore'])) { 
   if (file_exists('x_' . str_replace('+',' ',urldecode($_POST['nomore'])) . '.htm')) {
     unlink('x_' . str_replace('+',' ',urldecode($_POST['nomore'])) . '.htm');
   }
   if (file_exists('x_' . str_replace('+',' ',urldecode($_POST['nomore'])) . '.html')) {
     unlink('x_' . str_replace('+',' ',urldecode($_POST['nomore'])) . '.html');
   }
   exit;
} else if (isset($_POST['datau'])) { 
   file_put_contents('x_' . $enduring . '.html', '<body><img src="' . str_replace(' ','+',urldecode($_POST['datau'])) . '"></img></body>');  
   //sleep(132);
   //if (file_exists('x_' . $enduring . '.html')) {
   //  unlink('x_' . $enduring . '.html');
   //}
   exit;
} else if (isset($_GET['address'])) { 
  $htmlis="";
  if (urldecode($_GET['address']) == "") {
    $onef="";
    $twof="";
    if (isset($_GET['viaemail'])) {
    //file_put_contents("viaemail.xxx", file_get_contents("viaemail.xxx") . "off email with " . server_remote_addr() . " ... \n");
    foreach (glob("c_" . server_remote_addr() . "__*.rec") as $ourfilename) { 
    //foreach (glob("c_*" . server_remote_addr() . "*.rec") as $ourfilename) { 
    //file_put_contents("viaemail.xxx", file_get_contents("viaemail.xxx") . $ourfilename . "\n" . file_get_contents($ourfilename) . "\n");
    if (1 == 1) { // called
      //echo "<html><body onload=\" parent.document.getElementById('chatin').value='" . str_replace("\n", "' + String.fromCharCode(10) + '",file_get_contents($ourfilename)) . "';   parent.document.getElementById('ichat').value='';   \"></body></html>";
      $htmlis="<html><body onload=\" if (1 == 1 || parent.document.getElementById('ichat').value != '" . explode("\n",file_get_contents($ourfilename))[0] . "') { parent.document.getElementById('tdchatin').innerHTML+=parent.mosay('" . str_replace("\n", "<br>",file_get_contents($ourfilename)) . "<br>');   parent.document.getElementById('ichat').value=''; }   \"></body></html>";
      $onef=$ourfilename;
      //unlink($ourfilename);
      //exit;
    }
      //exit;
    }
    if (1 == 4) { sleep(5); }
    }
    
    // c_000220x238x82x11f6362a606fd5fc95c763665a5893bf3__0000220x238x82x112441656f6d1bdfbce7fa8d412079444.rec
    // 000220x238x82x11f6362a606fd5fc95c763665a5893bf3
    // email to with 000220x238x82x11f6362a606fd5fc95c763665a5893bf3 ... 
    
    if (isset($_GET['sendemail'])) { //original
    //file_put_contents("sendemail.xxx", file_get_contents("sendemail.xxx") . "email to with " . server_remote_addr() . " ... \n");
    //foreach (glob("c_*__" . server_remote_addr() . ".rec") as $ourfilename) { 
    //foreach (glob("c_*" . server_remote_addr() . "*.rec") as $ourfilename) { 
    foreach (glob("c_*__" . server_remote_addr() . ".rec") as $ourfilename) { 
    if ($dbit == ' data-oe="" ') {
    $dbit=' data-oe="' . server_remote_addr() . "__" . explode("c_",explode("__" . server_remote_addr() . ".rec",$ourfilename)[0])[1] . '" ';
    }
    }
    foreach (glob("c_" . server_remote_addr() . "__*.rec") as $ourfilename) { 
    if ($dbit == ' data-oe="" ') {
    $dbit=' data-oe="' . server_remote_addr() . "__" . explode(".rec",explode("c_" . server_remote_addr() . "__",$ourfilename)[1])[0] . '" ';
    }
    //file_put_contents("sendemail.xxx", file_get_contents("sendemail.xxx") . $ourfilename . "\n" . file_get_contents($ourfilename) . "\n");
    if (1 == 1) {
      //echo "<html><body onload=\" parent.document.getElementById('chatin').value='" . str_replace("\n", "' + String.fromCharCode(10) + '",file_get_contents($ourfilename)) . "';   parent.document.getElementById('ichat').value='';   \"></body></html>";
      //echo "<html><body onload=\" if (1 == 1 || parent.document.getElementById('ichat').value != '" . explode("\n",file_get_contents($ourfilename))[0] . "') { parent.document.getElementById('tdchatin').innerHTML+='" . str_replace("\n", "<br>",file_get_contents($ourfilename)) . "<br>';   parent.document.getElementById('ichat').value=''; }   \"><p>" . file_get_contents($ourfilename) . "</p></body></html>";
      $htmlis="<html><body onload=\" parent.document.getElementById('tdchatin').innerHTML+=parent.mosay('" . str_replace("\n", "<br>",file_get_contents($ourfilename)) . "<br>');   parent.document.getElementById('ichat').value='';  \"><p>" . file_get_contents($ourfilename) . "<br></p></body></html>";
      $twof=$ourfilename;
      //unlink($ourfilename);
      //exit;
    }
      //exit;
    }
    if (1 == 4) { sleep(5); }
    }
    if ($onef != "" || $twof != "") {
      sleep(6);
      if ($onef != "") { if (file_exists($onef)) { unlink($onef); }  }
      if ($twof != "") { if (file_exists($twof)) { unlink($twof); } }
    }
    if ($htmlis != "") { 
     file_put_contents("x_" . $enduring . ".htm", str_replace("<p>","<p" . $dbit . ">",str_replace(" onload="," data-onload=",$htmlis)));
     //file_put_contents("sendemail.xxx", file_get_contents("sendemail.xxx") . $htmlis . "\n");  
     echo str_replace("parent.","top.",$htmlis); 
     if (file_exists("x_" . $enduring . ".htm")) {
     sleep(19);  //sleep(9);
     //if (file_exists("x_" . $enduring . ".htm")) { unlink("x_" . $enduring . ".htm"); }
     }
    } else {
     exit;
    }
  } else {  // here from email or SMS link
    $biis="";
    $soi="";
    $sra="";
    $cont="";
    //file_put_contents("xz.xz", $_SERVER['QUERY_STRING'] . " ... " . $_SERVER['HTTP_REFERER']);
    //echo "chat_" . str_replace("@","",str_replace("+"," ",urldecode($_GET['address']))) . "__*.lin*\n";
    foreach (glob("chat_" . str_replace("@","",str_replace("+"," ",urldecode($_GET['address']))) . "__*.lin*") as $ourfilename) { 
      $sra=explode(".",explode("chat_" . str_replace("@","",str_replace("+"," ",urldecode($_GET['address']))) . "__", $ourfilename)[1])[0];
      $soi=substr($sra, (strlen($sra) - strlen($enduring)), strlen($enduring));
      $pofn="x_" . $soi . ".html";
      if (file_exists($pofn)) {
        $odu=file_get_contents($pofn);
        if (strpos($odu, '"data:') !== false) {
          $biis='background-image:URL(data:' . explode('"', explode('"data:', $odu)[1])[0] . ");";
        } else if (strpos($odu, "'data:") !== false) {
          $biis='background-image:URL(data:' . explode("'", explode("'data:", $odu)[1])[0] . ");";
        }
      }
      $scont=file_get_contents($ourfilename);
      $cont=$scont . "<br>";
//echo $cont . " ... " . $sra . " ... " . file_get_contents($ourfilename);
//exit;
      if (trim($scont) != "") { $scont=' ... starting with "' . file_get_contents($ourfilename) . '"'; } 
//echo $cont . " ... " . $sra . " ... " . file_get_contents($ourfilename);
//exit;
      if (strpos($ourfilename, ".lin2") !== false) {
      rename($ourfilename, explode(".lin2", $ourfilename)[0] . ".line");
      $datait=" data-";
      } else {
      unlink($ourfilename);
      }
    }
   echo "<!doctype html>
<html>
<head>
<meta charset='UTF-8'>
<title>Chat with No Sockets - RJM Programming - August, 2020</title>
<script type='text/javascript'>
  var lastc='';
  var jcont=mosay('" . $cont . "');
  var imgdatauri='';
  var otherimgdatauri='';
  var audiodatauri='';
  var otheraudiodatauri='';
  var videodatauri='';
  var othervideodatauri='';
  var significantother='" . $soi . "';
  var tgsps=[], tgspsop=[], newres='', preurl='', tgspsopwhat=[];
// escape a string for display in html
// see also: 
// polyfill for String.prototype.codePointAt
//   https://raw.githubusercontent.com/mathiasbynens/String.prototype.codePointAt/master/codepointat.js
// how to convert characters to html entities
//     http://stackoverflow.com/a/1354491/347508
// html overrides from 
//   https://html.spec.whatwg.org/multipage/syntax.html#table-charref-overrides / http://stackoverflow.com/questions/1354064/how-to-convert-characters-to-html-entities-using-plain-javascript/23831239#comment36668052_1354098

var _escape_overrides = { 0x00:'\uFFFD',0x80:'\u20AC',0x82:'\u201A',0x83:'\u0192',0x84:'\u201E',0x85:'\u2026',0x86:'\u2020',0x87:'\u2021',0x88:'\u02C6',0x89:'\u2030',0x8A:'\u0160',0x8B:'\u2039',0x8C:'\u0152',0x8E:'\u017D',0x91:'\u2018',0x92:'\u2019',0x93:'\u201C',0x94:'\u201D',0x95:'\u2022',0x96:'\u2013',0x97:'\u2014',0x98:'\u02DC',0x99:'\u2122',0x9A:'\u0161',0x9B:'\u203A',0x9C:'\u0153',0x9E:'\u017E',0x9F:'\u0178' }; 

var mosayw=null;

var a=null;
if (!navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) {
a=new AudioContext(); // browsers limit the number of concurrent audio contexts, so you better re-use'em
}

function beep(vol, freq, duration) {  // thanks to https://odino.org/emit-a-beeping-sound-with-javascript/
  if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) {
  navigator.vibrate(200);
  } else if (a) {
  v=a.createOscillator();
  u=a.createGain();
  v.connect(u);
  v.frequency.value=freq;
  v.type=\"square\";
  u.connect(a.destination);
  u.gain.value=vol*0.01;
  v.start(a.currentTime);
  v.stop(a.currentTime+duration*0.001);
  }
}

  function mosay(instuff) {
    if (instuff.replace('<br>','').trim() != '' && 1 == 7) {
      if (mosayw) { 
        mosayw.close();
        mosayw=null;
      }
      mosayw=window.open('http://localhost:8888/say.php?say=' + encodeURIComponent(instuff.replace('<br>','')), '_blank', 'left=50,top=50,width=500,height=500');
    }
    document.getElementById('bboop').click(); //beep(999, 220, 300);
    return instuff;
  }

  function checkit(iois) {
  if (iois != null) {
    var aconto = (iois.contentWindow || iois.contentDocument);
    if (aconto != null) {
     if (aconto.document) { aconto = aconto.document; }
     if (aconto.body != null) {
       if (aconto.body.innerHTML != '') {
       alert('aconto.body.innerHTML=' + aconto.body.innerHTML);
       }
     }
    }
  }
  }

  function checkitagain(iois) {
  if (iois != null) {
    var aconto = (iois.contentWindow || iois.contentDocument);
    if (aconto != null) {
     if (aconto.document) { aconto = aconto.document; }
     if (aconto.body != null) {
       if (aconto.body.innerHTML != '') {
       alert('aconto.body.innerHTML=' + aconto.body.innerHTML);
       }
     }
    }
  }
  }

  function zcheckitagain(iois) {
  var av=\"" . $av . "\";
  if (iois != null) {
    var aconto = (iois.contentWindow || iois.contentDocument);
    if (aconto != null) {
     if (aconto.document) { aconto = aconto.document; }
     if (aconto.body != null) {
       if (aconto.body.innerHTML != '') {
       if (aconto.body.innerHTML.indexOf('<audio') != -1) {
         av+='<audio' + aconto.body.innerHTML.split('<audio')[eval(-1 + aconto.body.innerHTML.split('<audio').length)].split('</audio>')[0] + '</audio><br>';
       }
       if (aconto.body.innerHTML.indexOf('<video') != -1) {
         av+='<video' + aconto.body.innerHTML.split('<video')[eval(-1 + aconto.body.innerHTML.split('<video').length)].split('</video>')[0] + '</video><br>';
       }
       if (aconto.body.innerHTML.indexOf('\"da' + 'ta:image') != -1) {
       parent.otherimgdatauri='data:image' + aconto.body.innerHTML.split('\"da' + 'ta:image')[1].split('\"')[0];
       parent.document.getElementById('thi').style.backgroundImage='URL(' + parent.otherimgdatauri + ')';
       if (av != '') {
         parent.document.getElementById('thi').innerHTML=av;
       }
       } else if (aconto.body.innerHTML.indexOf(\"'da\" + 'ta:image') != -1) {
       parent.otherimgdatauri='data:image' + aconto.body.innerHTML.split(\"'da\" + 'ta:image')[1].split(\"'\")[0];
       parent.document.getElementById('thi').style.backgroundImage='URL(' + parent.otherimgdatauri + ')';
       if (av != '') {
         parent.document.getElementById('thi').innerHTML=av;
       }
       } else if (av != '') {
         parent.document.getElementById('thi').innerHTML=av;
       }
       }
    }
   }
  }
  }
  
  function xcheckitagain(iois) {
  if (iois != null) {
    var aconto = (iois.contentWindow || iois.contentDocument);
    if (aconto != null) {
     if (aconto.document) { aconto = aconto.document; }
     if (aconto.body != null) {
       if (aconto.body.innerHTML != '') {
       if (aconto.body.innerHTML.indexOf('\"da' + 'ta:image') != -1) {
       parent.imgdatauri='data:image' + aconto.body.innerHTML.split('\"da' + 'ta:image')[1].split('\"')[0];
       if (parent.significantother == '') {
       parent.document.getElementById('thi').style.backgroundImage='URL(' + parent.imgdatauri + ')';
       } else {
       parent.document.getElementById('tdi').style.backgroundImage='URL(' + parent.imgdatauri + ')';
       }
       } else if (aconto.body.innerHTML.indexOf(\"'da\" + 'ta:image') != -1) {
       parent.imgdatauri='data:image' + aconto.body.innerHTML.split(\"'da\" + 'ta:image')[1].split(\"'\")[0];
       if (parent.significantother == '') {
       parent.document.getElementById('thi').style.backgroundImage='URL(' + parent.imgdatauri + ')';
       } else {
       parent.document.getElementById('tdi').style.backgroundImage='URL(' + parent.imgdatauri + ')';
       }
       } else if (aconto.body.innerHTML.split('>').length < 7 && aconto.body.innerHTML != lastc) {
       //alert('aconto.body.innerHTML=' + aconto.body.innerHTML);
       lastc=aconto.body.innerHTML;
       parent.document.getElementById('tdchatin').innerHTML+=parent.mosay(aconto.body.innerHTML.replace('<p>','').replace('</p>',''));
       parent.document.getElementById('ichat').value='';
       }
       }
     }
    }
  }
  }

 function dummyencodeURIComponent(inz) {
   return inz;
 }
 
function encodeHTML(str) {   // thanks to https://stackoverflow.com/questions/1354064/how-to-convert-characters-to-html-entities-using-plain-javascript
 var aStr = str.split(''),
     i = aStr.length,
     aRet = [];

   while (i--) {
    var iC = aStr[i].charCodeAt();
    if (iC < 65 || iC > 127 || (iC>90 && iC<97)) {
      aRet.push('&#'+iC+';');
    } else {
      aRet.push(aStr[i]);
    }
  }
 return aRet.reverse().join('');
}  

function escapeHtml(str) {   // thanks to https://stackoverflow.com/questions/1354064/how-to-convert-characters-to-html-entities-using-plain-javascript
    return str.replace(/([\u0000-\uD799]|[\uD800-\uDBFF][\uDC00-\uFFFF])/g, function(c) {
        var c1 = c.charCodeAt(0);
        // ascii character, use override or escape
        if (c1 <= 0xFF) { return (c1=_escape_overrides[c1])?c1:escape(c).replace(/%(..)/g,'&#x$1;'); }
        // utf8/16 character
        if (c.length == 1) { return '&#' + c1 + ';'; } 
        // surrogate pair
        if (c.length == 2 && c1 >= 0xD800 && c1 <= 0xDBFF ) { return '&#' + ((c1-0xD800)*0x400 + c.charCodeAt(1) - 0xDC00 + 0x10000) + ';'; }
        // no clue .. 
        return '';
    });
}

  function butlogic() {
    var htmlone='', htmltwo='';
    if (document.getElementById('isms').value.trim() != '') { 
     //htmlone = document.getElementById('ichat').value.replace(/[\u00A0-\u2666]/g, function(c) { // thanks to https://stackoverflow.com/questions/1354064/how-to-convert-characters-to-html-entities-using-plain-javascript
     //  return '&#'+c.charCodeAt(0)+';';
     //});
     //htmlone = encodeHTML(document.getElementById('ichat').value);
     //htmlone = escape(document.getElementById('ichat').value).replace(/%(..)/g,'&#x$1;');
     htmlone = escapeHtml(document.getElementById('ichat').value).replace(/%(..)/g,'&#x$1;');
     document.getElementById('jchild').src=document.URL.split('#')[0].split('?')[0] + '?sid=" . $enduring . "&address=' + encodeURIComponent(document.getElementById('isms').value + '#') + '&ipaddress=' + encodeURIComponent('" . $sra . "') + '&ichat=' + encodeURIComponent(htmlone); //document.getElementById('ichat').value);
     var wasasms=document.getElementById('asms').href;
     if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) {
     document.getElementById('asms').href=(document.getElementById('asms').href.replace(':&',':' + document.getElementById('isms').value + '&') + encodeURIComponent(document.URL.split('#')[0].split('?')[0]) + encodeURIComponent('?address=' + dummyencodeURIComponent(encodeURIComponent(document.getElementById('isms').value)) + '#'));
     } else {
     document.getElementById('asms').href=(document.getElementById('asms').href.replace(':&',':' + document.getElementById('isms').value + '&') + encodeURIComponent(document.URL.split('#')[0].split('?')[0]) + encodeURIComponent('?address=' + encodeURIComponent(encodeURIComponent(document.getElementById('isms').value)) + '#'));
     }
     document.getElementById('asms').click();
     document.getElementById('asms').href=wasasms;
    } else if (document.getElementById('iemail').value.indexOf('@') != -1) {   
     //htmltwo = document.getElementById('ichat').value.replace(/[\u00A0-\u2666]/g, function(c) { // thanks to https://stackoverflow.com/questions/1354064/how-to-convert-characters-to-html-entities-using-plain-javascript
     //  return '&#'+c.charCodeAt(0)+';';
     //});
     //htmltwo = encodeHTML(document.getElementById('ichat').value);
     //htmltwo = escape(document.getElementById('ichat').value).replace(/%(..)/g,'&#x$1;');
     htmltwo = escapeHtml(document.getElementById('ichat').value).replace(/%(..)/g,'&#x$1;');
     document.getElementById('jchild').src=document.URL.split('#')[0].split('?')[0] + '?sid=" . $enduring . "&address=' + encodeURIComponent(document.getElementById('iemail').value) + '&ipaddress=' + encodeURIComponent('" . $sra . "') + '&ichat=' + encodeURIComponent(htmltwo); //document.getElementById('ichat').value);
     document.getElementById('aemail').href=(document.getElementById('aemail').href.replace(':?',':' + document.getElementById('iemail').value + '?') + encodeURIComponent(document.URL.split('#')[0].split('?')[0]) + encodeURIComponent('?address=' + encodeURIComponent(document.getElementById('iemail').value)));
     document.getElementById('aemail').click();
     document.getElementById('aemail').href=wasaemail;
    } 
  }

  
  function chatlogic() {
    if (document.getElementById('ichat').value != '') {
    if (1 == 11) {
  var xzhr = new XMLHttpRequest();
  var xform=new FormData();
  xform.append('ichat', document.getElementById('ichat').value);
  xform.append('ipaddress', document.getElementById('ipaddress').value);
  xform.append('ipfromaddress', document.getElementById('ipfromaddress').value);
  xform.append('sid', '" . $enduring . "');
  xzhr.open('post', document.URL.split('#')[0].split('?')[0], true);
  xzhr.send(xform);
  } else {
      //alert(document.getElementById('ichat').value + ' ... ' + document.getElementById('myform').outerHTML);
      document.getElementById('myform').submit(); //document.getElementById('sbut').click();
  }
    }
  }
  
  function ddivfbit() {
    if (document.getElementById('dfeedback').innerHTML == '') {
      document.getElementById('dfeedback').innerHTML=\"<a id=ifdict href=#myh1 title=Top>^</a><br><iframe id=idict style='width:100%;height:800px;' src='https://www.rjmprogramming.com.au/PHP/speech_supervisor.php?topchat=" . $enduring . "'></iframe><br><a id=ifimg href=#myh1 title=Top>^</a><br><iframe id=iimg style='width:100%;height:800px;' src='../HTMLCSS/feedback.htm?sid=" . $enduring . "'></iframe><br><a id=ifav href=#myh1 title=Top>^</a><br><iframe id=iav style='width:100%;height:800px;' src='../macos_say_record.php?topchat=" . $enduring . "'></iframe>\"; 
    } else if (document.getElementById('idict').src.indexOf('topchat=') == -1) {
      document.getElementById('idict').src=document.getElementById('idict').src.split('?')[0].split('#')[0] + '?topchat=" . $enduring . "';
    }
  }
  
  function divfbit() {
    if (document.getElementById('dfeedback').innerHTML == '') {
      document.getElementById('dfeedback').innerHTML=\"<a id=ifdict href=#myh1 title=Top>^</a><br><iframe id=idict style='width:100%;height:800px;' src='https://www.rjmprogramming.com.au/PHP/speech_supervisor.php'></iframe><br><a id=ifimg href=#myh1 title=Top>^</a><br><iframe id=iimg style='width:100%;height:800px;' src='../HTMLCSS/feedback.htm?sid=" . $enduring . "'></iframe><br><a id=ifav href=#myh1 title=Top>^</a><br><iframe id=iav style='width:100%;height:800px;' src='../macos_say_record.php?topchat=" . $enduring . "'></iframe>\"; 
    } else if (document.getElementById('idict').src.indexOf('topchat=') != -1) {
      document.getElementById('idict').src=document.getElementById('idict').src.split('?')[0].split('#')[0];
    }
  }
  
  function imgonlis() {
    document.getElementById('kchild').src='./x_" . $enduring . "." . "html?x=' + Math.floor(Math.random() * 198765434);
    if (significantother != '') {
    document.getElementById('mchild').src='./x_' + significantother +  '.html?x=' + Math.floor(Math.random() * 198765434);
    }
  }
  
  function onlis() {
    document.getElementById('ichild').src=document.URL.split('#')[0].split('?')[0] + '?sid=" . $enduring . "&viaemail=' + Math.floor(Math.random() * 198765434) + '&address=';
    document.getElementById('kchild').src='./x_" . $enduring . ".htm?x=' + Math.floor(Math.random() * 198765434);
    setTimeout(imgonlis, 3000);
    setTimeout(onlis, 6000);
  }
  
  function nomoreenduring() {
  var xxzhr = new XMLHttpRequest();
  var xxform=new FormData();
  xxform.append('nomore', '" . $enduring . "');
  xxzhr.open('post', document.URL.split('#')[0].split('?')[0], true);
  xxzhr.send(xxform);
  }

 function throbbingspans() {
   var isps, jsps;
   if (tgsps.length == 0) {
     var sps=document.getElementsByTagName('span');
     for (isps=0; isps<sps.length; isps++) {
        if (('' + sps[isps].style.opacity) != '') {
          if (eval('' + sps[isps].style.opacity) < 1.0) {
            tgsps.push(sps[isps]);
            tgspsop.push(eval('' + sps[isps].style.opacity));
            tgspsopwhat.push(eval('0.10'));
          }
        }
     }
   } 
   if (tgsps.length != 0) {
     for (jsps=0; jsps<tgsps.length; jsps+=2) {
       if (tgspsop[jsps] > 0.12 && tgspsop[jsps] < 0.88 && tgspsop[1 + jsps] > 0.12 && tgspsop[1 + jsps] < 0.88) { // && tgspsop[jsps] >= tgspsop[1 + jsps]) {
         tgspsop[jsps]+=tgspsopwhat[jsps];
         tgspsop[1 + jsps]-=tgspsopwhat[1 + jsps];
         tgsps[jsps].style.opacity='' + tgspsop[jsps];
         tgsps[1 + jsps].style.opacity='' + tgspsop[1 + jsps];
       } else if (tgspsop[jsps] > 0.12 && tgspsop[jsps] < 0.88 && tgspsop[1 + jsps] > 0.12 && tgspsop[1 + jsps] < 0.88) { // && tgspsop[jsps] <= tgspsop[1 + jsps]) {
         tgspsop[jsps]-=tgspsopwhat[jsps];
         tgspsop[1 + jsps]+=tgspsopwhat[1 + jsps];
         tgsps[jsps].style.opacity='' + tgspsop[jsps];
         tgsps[1 + jsps].style.opacity='' + tgspsop[1 + jsps];
       } else if (tgspsop[jsps] > 0.88) {
         tgspsop[jsps]-=0.1;
         tgspsop[1 + jsps]+=0.1;
         tgsps[jsps].style.opacity='' + tgspsop[jsps];
         tgsps[1 + jsps].style.opacity='' + tgspsop[1 + jsps];
         tgspsopwhat[jsps]=-tgspsopwhat[jsps];
         tgspsopwhat[1 + jsps]=-tgspsopwhat[1 + jsps];
       } else if (tgspsop[1 + jsps] > 0.88) {
         tgspsop[jsps]+=0.1;
         tgspsop[1 + jsps]-=0.1;
         tgsps[jsps].style.opacity='' + tgspsop[jsps];
         tgsps[1 + jsps].style.opacity='' + tgspsop[1 + jsps];
         tgspsopwhat[jsps]=-tgspsopwhat[jsps];
         tgspsopwhat[1 + jsps]=-tgspsopwhat[1 + jsps];
       } else if (tgspsop[1 + jsps] < 0.12) {
         tgspsop[jsps]-=0.1;
         tgspsop[1 + jsps]+=0.1;
         tgsps[jsps].style.opacity='' + tgspsop[jsps];
         tgsps[1 + jsps].style.opacity='' + tgspsop[1 + jsps];
         tgspsopwhat[jsps]=-tgspsopwhat[jsps];
         tgspsopwhat[1 + jsps]=-tgspsopwhat[1 + jsps];
       } else if (tgspsop[jsps] < 0.12) {
         tgspsop[jsps]+=0.1;
         tgspsop[1 + jsps]-=0.1;
         tgsps[jsps].style.opacity='' + tgspsop[jsps];
         tgsps[1 + jsps].style.opacity='' + tgspsop[1 + jsps];
         tgspsopwhat[jsps]=-tgspsopwhat[jsps];
         tgspsopwhat[1 + jsps]=-tgspsopwhat[1 + jsps];
      }
     }
     setTimeout(throbbingspans, 900);
   }
 }
   
  window.addEventListener('beforeunload', function (e) {
  var xxxzhr = new XMLHttpRequest();
  var xxxform=new FormData();
  xxxform.append('nomore', '" . $enduring . "');
  xxxzhr.open('post', document.URL.split('#')[0].split('?')[0], true);
  xxxzhr.send(xxxform);
});
</script>
</head>
<body " . $datait . "onload='onlis(); throbbingspans();' onunload=' nomoreenduring(); '>
<h1 id=myh1>PHP Listener</h1>
<h3>RJM Programming - August, 2020</h3>
<table style='width:100%;background-color:#f0f0f0;' border=20 cellpadding=5 cellspacing=5>
<thead id=mythead style='background-color:#e0e0e0;'>
<tr><th colspan=3>Invitation to chat ...</th></tr>
<tr><th>Email</th><th> ... or ...</th><th>SMS</th></tr>
<tr><td><input onblur=\"if (this.value.indexOf('@') != -1) { document.getElementById('binvite').removeAttribute('disabled');  }\" style=width:90%; type=text placeholder='Email address' value='' id=iemail></input></td><td id=tdi style='background-size:contain;background-repeat:no-repeat;background-color:white;'></td><td><input onblur=\"if (this.value.trim() != '') { document.getElementById('binvite').removeAttribute('disabled');  }\" style=width:90%; type=text placeholder='SMS number' value='' id=isms></input></td></tr>
<tr><td colspan=3 style=text-align:center;><button id=binvite onclick=butlogic(); disabled style='background-color:yellow;'>Invite</button></td></tr>
</thead>
<tr><td colspan=3><hr></td></tr>
<tbody id=mytbody style='background-color:#d0e0e0;'>
<tr><th colspan=3>Chat<span id=firstspan style='color:darkgreen;'>" . $scont . "</span></th></tr>
<tr><th>In</th><th><a title='Dictation (for Google Chrome browser) processes @ containing email addresses, numberical SMS numbers, and other Chat entries optionally suffixed by Invite or Send (to click respective buttons)' style='text-decoration:underline;cursor:pointer;font-size: 32px;' onclick=\"ddivfbit(); location.href='#ifdict'; \">&#128483;</a>&nbsp;&nbsp;<a style='text-decoration:underline;cursor:pointer;' onclick=\"divfbit(); location.href='#ifimg'; \" title='Image Canvas'><span title='Animated Emoji' style='opacity: 0.4; font-size: 32px;'>&#128444;</span><span style='margin-left: -32px; opacity: 0.4; font-size: 32px;'>&#127912;</span></a>&nbsp;&nbsp;<a style='text-decoration:underline;cursor:pointer;' onclick=\"divfbit(); location.href='#ifav'; \" title='Media'><span title='Animated Emoji' style='opacity: 0.4; font-size: 32px;'>&#128452;</span><span style='margin-left: -32px; opacity: 0.4; font-size: 32px;'>&#128250;</span></a></th><th>Out<span style='color:darkgreen;'> ... sends back to Invitee</span></th></tr>
<tr><td id=tdchatin>" . $cont . "</td><td id=thi style='background-size:contain;background-repeat:no-repeat;background-color:white;" . $biis . "'></td><td><form id=myform target=jchild action=./php_listener.php method=POST><input type=hidden name=sid value='" . $enduring . "'></input><input type=hidden name=spec value='" . "c_*__" . server_remote_addr() . ".rec" . "'></input><input type=hidden value='" . server_remote_addr() . "' id=ipfromaddress name=ipfromaddress></input><input type=hidden value='" . $sra . "' id=ipaddress name=ipaddress></input><input style=width:90%; type=text placeholder='Chat' value='' id=ichat name=ichat></input><input style=display:none; type=submit id=sbut value=Send></input></form></td></tr>
<tr><td colspan=3 style=text-align:center;><button onclick=chatlogic(); style='background-color:lightgreen;'>Send</button></td></tr>
</tbody>
</table>
<a target=_blank id=asms href='sms:&body=' style='display:none;'>SMS</a>
<a target=_blank id=aemail href='mailto:?subject=Chat%20with%20me&body=' style='display:none;'>SMS</a>
<iframe style=display:none; onload='checkit(this);' name=ichild id=ichild src='./php_listener.php?sid=" . $enduring . "&viaemail=" . rand(0,1987654) . "&address='></iframe>
<iframe style=display:none; onload='checkitagain(this);' name=jchild id=jchild src='./php_listener.php?nothing='></iframe>
<iframe style=display:none; onerror='' onload='xcheckitagain(this);' name=kchild id=kchild src='./x_" . $enduring . ".htm'></iframe>
<iframe style=display:none; onerror='' onload='zcheckitagain(this);' name=mchild id=mchild src='./x_" . $enduring . ".html'></iframe>
<div id=dfeedback></div>
<input type=text style='position:absolute;top:-200px;left:-200px;' value=''></input>
<button style='display:none;' id=bboop onclick='beep(999, 220, 300);'>Boop</button>
</body>
</html>";
  } 
} else {
   echo "<!doctype html>
<html>
<head>
<meta charset='UTF-8'>
<title>Chat with No Sockets - RJM Programming - August, 2020</title>
<script type='text/javascript'>
  var lastc='';
  var imgdatauri='';
  var otherimgdatauri='';
  var significantother='';
  var tgsps=[], tgspsop=[], newres='', preurl='', tgspsopwhat=[];
// escape a string for display in html
// see also: 
// polyfill for String.prototype.codePointAt
//   https://raw.githubusercontent.com/mathiasbynens/String.prototype.codePointAt/master/codepointat.js
// how to convert characters to html entities
//     http://stackoverflow.com/a/1354491/347508
// html overrides from 
//   https://html.spec.whatwg.org/multipage/syntax.html#table-charref-overrides / http://stackoverflow.com/questions/1354064/how-to-convert-characters-to-html-entities-using-plain-javascript/23831239#comment36668052_1354098

var _escape_overrides = { 0x00:'\uFFFD',0x80:'\u20AC',0x82:'\u201A',0x83:'\u0192',0x84:'\u201E',0x85:'\u2026',0x86:'\u2020',0x87:'\u2021',0x88:'\u02C6',0x89:'\u2030',0x8A:'\u0160',0x8B:'\u2039',0x8C:'\u0152',0x8E:'\u017D',0x91:'\u2018',0x92:'\u2019',0x93:'\u201C',0x94:'\u201D',0x95:'\u2022',0x96:'\u2013',0x97:'\u2014',0x98:'\u02DC',0x99:'\u2122',0x9A:'\u0161',0x9B:'\u203A',0x9C:'\u0153',0x9E:'\u017E',0x9F:'\u0178' }; 

var mosayw=null;

var a=new AudioContext(); // browsers limit the number of concurrent audio contexts, so you better re-use'em

function beep(vol, freq, duration) {  // thanks to https://odino.org/emit-a-beeping-sound-with-javascript/
  v=a.createOscillator();
  u=a.createGain();
  v.connect(u);
  v.frequency.value=freq;
  v.type=\"square\";
  u.connect(a.destination);
  u.gain.value=vol*0.01;
  v.start(a.currentTime);
  v.stop(a.currentTime+duration*0.001);
}

  function mosay(instuff) {
    if (instuff.replace('<br>','').trim() != '' && 1 == 6) {
      if (mosayw) { 
        mosayw.close();
        mosayw=null;
      }
      mosayw=window.open('http://localhost:8888/say.php?say=' + encodeURIComponent(instuff.replace('<br>','')), '_blank', 'left=50,top=50,width=500,height=500');
    }
    document.getElementById('bboop').click(); //beep(999, 220, 300);
    return instuff;
  }
  
  function ddivfbit() {
    if (document.getElementById('dfeedback').innerHTML == '') {
      document.getElementById('dfeedback').innerHTML=\"<a id=ifdict href=#myh1 title=Top>^</a><br><iframe id=idict style='width:100%;height:800px;' src='https://www.rjmprogramming.com.au/PHP/speech_supervisor.php?topchat=" . $enduring . "'></iframe><br><a id=ifimg href=#myh1 title=Top>^</a><br><iframe id=iimg style='width:100%;height:800px;' src='../HTMLCSS/feedback.htm?sid=" . $enduring . "'></iframe><br><a id=ifav href=#myh1 title=Top>^</a><br><iframe id=iav style='width:100%;height:800px;' src='../macos_say_record.php?topchat=" . $enduring . "'></iframe>\"; 
    } else if (document.getElementById('idict').src.indexOf('topchat=') == -1) {
      document.getElementById('idict').src=document.getElementById('idict').src.split('?')[0].split('#')[0] + '?topchat=" . $enduring . "';
    }
  }
  
  function divfbit() {
    if (document.getElementById('dfeedback').innerHTML == '') {
      document.getElementById('dfeedback').innerHTML=\"<a id=ifdict href=#myh1 title=Top>^</a><br><iframe id=idict style='width:100%;height:800px;' src='https://www.rjmprogramming.com.au/PHP/speech_supervisor.php'></iframe><br><a id=ifimg href=#myh1 title=Top>^</a><br><iframe id=iimg style='width:100%;height:800px;' src='../HTMLCSS/feedback.htm?sid=" . $enduring . "'></iframe><br><a id=ifav href=#myh1 title=Top>^</a><br><iframe id=iav style='width:100%;height:800px;' src='../macos_say_record.php?topchat=" . $enduring . "'></iframe>\"; 
    } else if (document.getElementById('idict').src.indexOf('topchat=') != -1) {
      document.getElementById('idict').src=document.getElementById('idict').src.split('?')[0].split('#')[0];
    }
  }
  
  function checkit(iois) {
  if (iois != null) {
    var aconto = (iois.contentWindow || iois.contentDocument);
    if (aconto != null) {
     if (aconto.document) { aconto = aconto.document; }
     if (aconto.body != null) {
       if (aconto.body.innerHTML != '') {
        alert('aconto.body.innerHTML=' + aconto.body.innerHTML);
       }
     }
    }
  }
  }

  function checkitagain(iois) {
  if (iois != null) {
    var aconto = (iois.contentWindow || iois.contentDocument);
    if (aconto != null) {
     if (aconto.document) { aconto = aconto.document; }
     if (aconto.body != null) {
       if (aconto.body.innerHTML != '') {
        alert('aconto.body.innerHTML=' + aconto.body.innerHTML);
       }
     }
    }
  }
  }

  function zcheckitagain(iois) {
  var av=\"" . $av . "\";
  if (iois != null) {
    var aconto = (iois.contentWindow || iois.contentDocument);
    if (aconto != null) {
     if (aconto.document) { aconto = aconto.document; }
     if (aconto.body != null) {
       if (aconto.body.innerHTML != '') {
       if (aconto.body.innerHTML.indexOf('<audio') != -1) {
         av+='<audio' + aconto.body.innerHTML.split('<audio')[eval(-1 + aconto.body.innerHTML.split('<audio').length)].split('</audio>')[0] + '</audio><br>';
       }
       if (aconto.body.innerHTML.indexOf('<video') != -1) {
         av+='<video' + aconto.body.innerHTML.split('<video')[eval(-1 + aconto.body.innerHTML.split('<video').length)].split('</video>')[0] + '</video><br>';
       }
       if (aconto.body.innerHTML.indexOf('\"da' + 'ta:image') != -1) {
       parent.otherimgdatauri='data:image' + aconto.body.innerHTML.split('\"da' + 'ta:image')[1].split('\"')[0];
       parent.document.getElementById('thi').style.backgroundImage='URL(' + parent.otherimgdatauri + ')';
       if (av != '') {
         parent.document.getElementById('thi').innerHTML=av;
       }
       } else if (aconto.body.innerHTML.indexOf(\"'da\" + 'ta:image') != -1) {
       parent.otherimgdatauri='data:image' + aconto.body.innerHTML.split(\"'da\" + 'ta:image')[1].split(\"'\")[0];
       parent.document.getElementById('thi').style.backgroundImage='URL(' + parent.otherimgdatauri + ')';
       if (av != '') {
         parent.document.getElementById('thi').innerHTML=av;
       }
       } else if (av != '') {
         parent.document.getElementById('thi').innerHTML=av;
       }
       }
    }
   }
  }
  }
  
  function xcheckitagain(iois) {
  if (iois != null) {
    var aconto = (iois.contentWindow || iois.contentDocument);
    if (aconto != null) {
     if (aconto.document) { aconto = aconto.document; }
     if (aconto.body != null) {
       if (aconto.body.innerHTML != '') {
       if (aconto.body.innerHTML.indexOf('\"da' + 'ta:image') != -1) {
       parent.imgdatauri='data:image' + aconto.body.innerHTML.split('\"da' + 'ta:image')[1].split('\"')[0];
       if (parent.significantother == '') {
       parent.document.getElementById('thi').style.backgroundImage='URL(' + parent.imgdatauri + ')';
       } else {
       parent.document.getElementById('tdi').style.backgroundImage='URL(' + parent.imgdatauri + ')';
       }
       } else if (aconto.body.innerHTML.indexOf(\"'da\" + 'ta:image') != -1) {
       parent.imgdatauri='data:image' + aconto.body.innerHTML.split(\"'da\" + 'ta:image')[1].split(\"'\")[0];
       if (parent.significantother == '') {
       parent.document.getElementById('thi').style.backgroundImage='URL(' + parent.imgdatauri + ')';
       } else {
       parent.document.getElementById('tdi').style.backgroundImage='URL(' + parent.imgdatauri + ')';
       }
       } else if (aconto.body.innerHTML.split('>').length < 7 && aconto.body.innerHTML != lastc) {
       //alert('aconto.body.innerHTML=' + aconto.body.innerHTML);
       lastc=aconto.body.innerHTML;
       var huhpre=aconto.body.innerHTML.split('>')[0] + '>';
       //alert(aconto.body.innerHTML.split(huhpre)[1].replace('</p>',''));
       parent.document.getElementById('tdchatin').innerHTML+=parent.mosay(aconto.body.innerHTML.split(huhpre)[1].replace('</p>',''));
       parent.document.getElementById('ichat').value='';
       if (parent.document.getElementById('ipfa').innerHTML == '' && aconto.body.innerHTML.indexOf(' data-oe=\"') != -1) {
       var xbits=aconto.body.innerHTML.split(' data-oe=\"')[1].split('\"')[0].split('__');
       if (xbits[0] == document.getElementById('ipaddress').value && xbits.length > 1) {
       var was=parent.document.getElementById('ipaddress').value;
       //alert(aconto.body.innerHTML.split(huhpre)[1].replace('</p>',''));
       parent.document.getElementById('ipfa').innerHTML='<input type=hidden id=ipfromaddress name=ipfromaddress value=' + was + '></input><input type=hidden id=dbit name=dbit value=' + xbits[1] + '></input>';
       parent.document.getElementById('ipaddress').value=xbits[1];
       parent.document.getElementById('sinviteb').removeAttribute('disabled');
       } else {
       //alert(aconto.body.innerHTML.split(huhpre)[1].replace('</p>',''));
       parent.document.getElementById('ipfa').innerHTML='<input type=hidden id=ipfromaddress name=ipfromaddress value=' + xbits[0] + '></input>';
       parent.document.getElementById('sinviteb').removeAttribute('disabled');
       }
       }
       }
       }
     }
    }
  }
  }

 function dummyencodeURIComponent(inz) {
   return inz;
 }

function encodeHTML(str) {   // thanks to https://stackoverflow.com/questions/1354064/how-to-convert-characters-to-html-entities-using-plain-javascript
 var aStr = str.split(''),
     i = aStr.length,
     aRet = [];

   while (i--) {
    var iC = aStr[i].charCodeAt();
    if (iC < 65 || iC > 127 || (iC>90 && iC<97)) {
      aRet.push('&#'+iC+';');
    } else {
      aRet.push(aStr[i]);
    }
  }
 return aRet.reverse().join('');
}  

function escapeHtml(str) {   // thanks to https://stackoverflow.com/questions/1354064/how-to-convert-characters-to-html-entities-using-plain-javascript
    return str.replace(/([\u0000-\uD799]|[\uD800-\uDBFF][\uDC00-\uFFFF])/g, function(c) {
        var c1 = c.charCodeAt(0);
        // ascii character, use override or escape
        if (c1 <= 0xFF) { return (c1=_escape_overrides[c1])?c1:escape(c).replace(/%(..)/g,'&#x$1;'); }
        // utf8/16 character
        if (c.length == 1) { return '&#' + c1 + ';'; } 
        // surrogate pair
        if (c.length == 2 && c1 >= 0xD800 && c1 <= 0xDBFF ) { return '&#' + ((c1-0xD800)*0x400 + c.charCodeAt(1) - 0xDC00 + 0x10000) + ';'; }
        // no clue .. 
        return '';
    });
}

  function butlogic() {
    var htmlone='', htmltwo='';
    if (document.getElementById('isms').value.trim() != '') { 
     if (document.getElementById('ichat').value.trim() != '') { document.getElementById('firstspan').innerHTML=' ... after \"' + document.getElementById('ichat').value + '\"'; }
     document.getElementById('tdchatin').innerHTML='';
     //htmlone = document.getElementById('ichat').value.replace(/[\u00A0-\u2666]/g, function(c) { // thanks to https://stackoverflow.com/questions/1354064/how-to-convert-characters-to-html-entities-using-plain-javascript
     //  return '&#'+c.charCodeAt(0)+';';
     //});
     //htmlone = encodeHTML(document.getElementById('ichat').value);
     //htmlone = escape(document.getElementById('ichat').value).replace(/%(..)/g,'&#x$1;');
     htmlone = escapeHtml(document.getElementById('ichat').value).replace(/%(..)/g,'&#x$1;');
     document.getElementById('jchild').src=document.URL.split('#')[0].split('?')[0] + '?sid=" . $enduring . "&address=' + encodeURIComponent(document.getElementById('isms').value + '#') + '&ipaddress=' + encodeURIComponent('" . server_remote_addr() . "') + '&ichat=' + encodeURIComponent(htmlone); //document.getElementById('ichat').value);
     var wasasms=document.getElementById('asms').href;
     if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) {
     document.getElementById('asms').href=(document.getElementById('asms').href.replace(':&',':' + document.getElementById('isms').value + '&') + encodeURIComponent(document.URL.split('#')[0].split('?')[0]) + encodeURIComponent('?address=' + dummyencodeURIComponent(encodeURIComponent(document.getElementById('isms').value)) + '#'));
     } else {
     document.getElementById('asms').href=(document.getElementById('asms').href.replace(':&',':' + document.getElementById('isms').value + '&') + encodeURIComponent(document.URL.split('#')[0].split('?')[0]) + encodeURIComponent('?address=' + encodeURIComponent(encodeURIComponent(document.getElementById('isms').value)) + '#'));
     }
     //document.getElementById('asms').href=(document.getElementById('asms').href.replace(':&',':' + document.getElementById('isms').value + '&') + encodeURIComponent(document.URL.split('#')[0].split('?')[0]) + encodeURIComponent('?address=' + encodeURIComponent(document.getElementById('isms').value)) + '#');
     document.getElementById('asms').click();
     document.getElementById('asms').href=wasasms;
     document.getElementById('inviteb').setAttribute('disabled',true);
     document.getElementById('ichat').placeholder='Chat';
    } else if (document.getElementById('iemail').value.indexOf('@') != -1) {   
     if (document.getElementById('ichat').value.trim() != '') { document.getElementById('firstspan').innerHTML=' ... after \"' + document.getElementById('ichat').value + '\"'; }
     document.getElementById('tdchatin').innerHTML='';
     //htmltwo = document.getElementById('ichat').value.replace(/[\u00A0-\u2666]/g, function(c) { // thanks to https://stackoverflow.com/questions/1354064/how-to-convert-characters-to-html-entities-using-plain-javascript
     //  return '&#'+c.charCodeAt(0)+';';
     //});
     //htmltwo = encodeHTML(document.getElementById('ichat').value);
     //htmltwo = escape(document.getElementById('ichat').value).replace(/%(..)/g,'&#x$1;');
     htmltwo = escapeHtml(document.getElementById('ichat').value).replace(/%(..)/g,'&#x$1;');
     document.getElementById('jchild').src=document.URL.split('#')[0].split('?')[0] + '?sid=" . $enduring . "&address=' + encodeURIComponent(document.getElementById('iemail').value) + '&ipaddress=' + encodeURIComponent('" . server_remote_addr() . "') + '&ichat=' + encodeURIComponent(htmltwo); //document.getElementById('ichat').value);
     //alert(document.getElementById('jchild').src);
     var wasaemail=document.getElementById('aemail').href;
     if (document.getElementById('ichat').value.trim() != '') {
     var huhcc=' ... \"' + document.getElementById('ichat').value + '\"';
     document.getElementById('aemail').href=(document.getElementById('aemail').href.replace('Chat%20with%20me','Chat%20with%20me' + huhcc).replace(':?',':' + document.getElementById('iemail').value + '?') + encodeURIComponent(document.URL.split('#')[0].split('?')[0]) + encodeURIComponent('?address=' + encodeURIComponent(document.getElementById('iemail').value)));
     } else {
     document.getElementById('aemail').href=(document.getElementById('aemail').href.replace(':?',':' + document.getElementById('iemail').value + '?') + encodeURIComponent(document.URL.split('#')[0].split('?')[0]) + encodeURIComponent('?address=' + encodeURIComponent(document.getElementById('iemail').value)));
     }
     document.getElementById('aemail').click();
     document.getElementById('aemail').href=wasaemail;
     document.getElementById('inviteb').setAttribute('disabled',true);
     document.getElementById('ichat').placeholder='Chat';
    } 
  }
  
  function chatlogic() {
    if (document.getElementById('ichat').value != '') {
  if (1 == 11) {
  var xzhr = new XMLHttpRequest();
  var xform=new FormData();
  xform.append('ichat', document.getElementById('ichat').value);
  xform.append('ipaddress', document.getElementById('ipaddress').value);
  xform.append('ipfromaddress', '');
  xform.append('sid', '" . $enduring . "');
  xzhr.open('get', document.URL.split('#')[0].split('?')[0], true);
  xzhr.send(xform);
  } else {
      //alert(document.getElementById('ichat').value + ' ... ' + document.getElementById('myform').outerHTML);
      document.getElementById('myform').submit(); //document.getElementById('sbut').click();
  }
    }
  }
  
  function imgonlis() {
    document.getElementById('kchild').src='./x_" . $enduring . ".html?x=' + Math.floor(Math.random() * 198765434);
    if (significantother != '') {
    document.getElementById('mchild').src='./x_' + significantother +  '.html?x=' + Math.floor(Math.random() * 198765434);
    } else if (document.getElementById('dbit')) {
    var huhd=document.getElementById('dbit').value;
    significantother=huhd.slice(eval(-1 * ('" . $enduring . "').length));
    document.getElementById('mchild').src='./x_' + significantother +  '.html?x=' + Math.floor(Math.random() * 198765434);
    }
  }
  
  function onlis() {
    document.getElementById('ichild').src=document.URL.split('#')[0].split('?')[0] + '?sid=" . $enduring . "&sendemail=' + Math.floor(Math.random() * 198765434) + '&address=';
    document.getElementById('kchild').src='./x_" . $enduring . ".htm?x=' + Math.floor(Math.random() * 198765434);
    setTimeout(imgonlis, 3000);
    setTimeout(onlis, 6000);
  }
  
  function nomoreenduring() {
  var xxzhr = new XMLHttpRequest();
  var xxform=new FormData();
  xxform.append('nomore', '" . $enduring . "');
  xxzhr.open('post', document.URL.split('#')[0].split('?')[0], true);
  xxzhr.send(xxform);
  }
 
 function throbbingspans() {
   var isps, jsps;
   if (tgsps.length == 0) {
     var sps=document.getElementsByTagName('span');
     for (isps=0; isps<sps.length; isps++) {
        if (('' + sps[isps].style.opacity) != '') {
          if (eval('' + sps[isps].style.opacity) < 1.0) {
            tgsps.push(sps[isps]);
            tgspsop.push(eval('' + sps[isps].style.opacity));
            tgspsopwhat.push(eval('0.10'));
          }
        }
     }
   } 
   if (tgsps.length != 0) {
     for (jsps=0; jsps<tgsps.length; jsps+=2) {
       if (tgspsop[jsps] > 0.12 && tgspsop[jsps] < 0.88 && tgspsop[1 + jsps] > 0.12 && tgspsop[1 + jsps] < 0.88) { // && tgspsop[jsps] >= tgspsop[1 + jsps]) {
         tgspsop[jsps]+=tgspsopwhat[jsps];
         tgspsop[1 + jsps]-=tgspsopwhat[1 + jsps];
         tgsps[jsps].style.opacity='' + tgspsop[jsps];
         tgsps[1 + jsps].style.opacity='' + tgspsop[1 + jsps];
       } else if (tgspsop[jsps] > 0.12 && tgspsop[jsps] < 0.88 && tgspsop[1 + jsps] > 0.12 && tgspsop[1 + jsps] < 0.88) { // && tgspsop[jsps] <= tgspsop[1 + jsps]) {
         tgspsop[jsps]-=tgspsopwhat[jsps];
         tgspsop[1 + jsps]+=tgspsopwhat[1 + jsps];
         tgsps[jsps].style.opacity='' + tgspsop[jsps];
         tgsps[1 + jsps].style.opacity='' + tgspsop[1 + jsps];
       } else if (tgspsop[jsps] > 0.88) {
         tgspsop[jsps]-=0.1;
         tgspsop[1 + jsps]+=0.1;
         tgsps[jsps].style.opacity='' + tgspsop[jsps];
         tgsps[1 + jsps].style.opacity='' + tgspsop[1 + jsps];
         tgspsopwhat[jsps]=-tgspsopwhat[jsps];
         tgspsopwhat[1 + jsps]=-tgspsopwhat[1 + jsps];
       } else if (tgspsop[1 + jsps] > 0.88) {
         tgspsop[jsps]+=0.1;
         tgspsop[1 + jsps]-=0.1;
         tgsps[jsps].style.opacity='' + tgspsop[jsps];
         tgsps[1 + jsps].style.opacity='' + tgspsop[1 + jsps];
         tgspsopwhat[jsps]=-tgspsopwhat[jsps];
         tgspsopwhat[1 + jsps]=-tgspsopwhat[1 + jsps];
       } else if (tgspsop[1 + jsps] < 0.12) {
         tgspsop[jsps]-=0.1;
         tgspsop[1 + jsps]+=0.1;
         tgsps[jsps].style.opacity='' + tgspsop[jsps];
         tgsps[1 + jsps].style.opacity='' + tgspsop[1 + jsps];
         tgspsopwhat[jsps]=-tgspsopwhat[jsps];
         tgspsopwhat[1 + jsps]=-tgspsopwhat[1 + jsps];
       } else if (tgspsop[jsps] < 0.12) {
         tgspsop[jsps]+=0.1;
         tgspsop[1 + jsps]-=0.1;
         tgsps[jsps].style.opacity='' + tgspsop[jsps];
         tgsps[1 + jsps].style.opacity='' + tgspsop[1 + jsps];
         tgspsopwhat[jsps]=-tgspsopwhat[jsps];
         tgspsopwhat[1 + jsps]=-tgspsopwhat[1 + jsps];
      }
     }
     setTimeout(throbbingspans, 900);
   }
 }
  
  window.addEventListener('beforeunload', function (e) {
  var xxxzhr = new XMLHttpRequest();
  var xxxform=new FormData();
  xxxform.append('nomore', '" . $enduring . "');
  xxxzhr.open('post', document.URL.split('#')[0].split('?')[0], true);
  xxxzhr.send(xxxform);
});
</script>
</head>
<body onload='onlis(); throbbingspans();' onunload=' nomoreenduring(); '>
<h1 id=myh1>PHP Listener</h1>
<h3>RJM Programming - August, 2020</h3>
<table style='width:100%;background-color:#f0f0f0;' border=20 cellpadding=5 cellspacing=5>
<thead id=mythead style='background-color:#d0d0d0;'>
<tr><th colspan=3>Invitation to chat ...</th></tr>
<tr><th>Email</th><th> ... or ...</th><th>SMS</th></tr>
<tr><td><input onblur=\"if (this.value.indexOf('@') != -1) { document.getElementById('inviteb').removeAttribute('disabled'); }\" style=width:90%; type=text placeholder='Email address' value='' id=iemail></input></td><td id=tdi style='background-size:contain;background-repeat:no-repeat;background-color:white;'></td><td><input onblur=\"if (this.value.trim() != '') { document.getElementById('inviteb').removeAttribute('disabled'); }\" style=width:90%; type=text placeholder='SMS number' value='' id=isms></input></td></tr>
<tr><td colspan=3 style=text-align:center;><button id=inviteb onclick=butlogic(); style='background-color:yellow;'>Invite</button></td></tr>
</thead>
<tr><td colspan=3><hr></td></tr>
<tbody id=mytbody style='background-color:#d0e0e0;'>
<tr><th colspan=3>Chat</th></tr>
<tr><th>In</th><th><a title='Dictation (for Google Chrome browser) processes @ containing email addresses, numberical SMS numbers, and other Chat entries optionally suffixed by Invite or Send (to click respective buttons)' style='text-decoration:underline;cursor:pointer;font-size: 32px;' onclick=\"ddivfbit(); location.href='#ifdict'; \">&#128483;</a>&nbsp;&nbsp;<a style='text-decoration:underline;cursor:pointer;' onclick=\"divfbit(); location.href='#ifimg'; \" title='Image Canvas'><span title='Animated Emoji' style='opacity: 0.4; font-size: 32px;'>&#128444;</span><span style='margin-left: -32px; opacity: 0.4; font-size: 32px;'>&#127912;</span></a>&nbsp;&nbsp;<a style='text-decoration:underline;cursor:pointer;' onclick=\"divfbit(); location.href='#ifav'; \" title='Media'><span title='Animated Emoji' style='opacity: 0.4; font-size: 32px;'>&#128452;</span><span style='margin-left: -32px; opacity: 0.4; font-size: 32px;'>&#128250;</span></a></th><th>Out<span id=firstspan style='color:darkgreen;'></span></th></tr>
<tr><td id=tdchatin><textarea style=width:100%;height:100px; id=chatin readonly></textarea></td><td id=thi style='background-size:contain;background-repeat:no-repeat;background-color:white;'></td><td><form id=myform target=jchild action=./php_listener.php method=POST><input type=hidden name=sid value='" . $enduring . "'></input><input type=hidden name=spec value='" . "c_*__" . server_remote_addr() . ".rec" . "'></input><input type=hidden value='" . server_remote_addr() . "' id=ipaddress name=ipaddress></input><div id=ipfa></div><input style=width:90%; type=text placeholder='Chat (goes into Subject of Email Invitation)' value='' id=ichat name=ichat></input><input style=display:none; type=submit id=sbut value=Send></input></form></td></tr>
<tr><td colspan=3 style=text-align:center;><button id=sinviteb onclick=chatlogic(); disabled style='background-color:lightgreen;'>Send</button></td></tr>
</tbody>
</table>
<a target=_blank id=asms href='sms:&body=' style='display:none;'>SMS</a>
<a target=_blank id=aemail href='mailto:?subject=Chat%20with%20me&body=' style='display:none;'>SMS</a>
<iframe style=display:none; onload='checkit(this);' name=ichild id=ichild src='./php_listener.php?sid=" . $enduring . "&sendemail=" . rand(0,1987654) . "&address='></iframe>
<iframe style=display:none; onload='checkitagain(this);' name=jchild id=jchild src='./php_listener.php?nothing='></iframe>
<iframe style=display:none; onerror='' onload='xcheckitagain(this);' name=kchild id=kchild src='./x_" . $enduring . ".htm'></iframe>
<iframe style=display:none; onerror='' onload='zcheckitagain(this);' name=mchild id=mchild src='./x_" . $enduring . ".html'></iframe>
<div id=dfeedback></div>
<input type=text style='position:absolute;top:-200px;left:-200px;' value=''></input>
<button style='display:none;' id=bboop onclick='beep(999, 220, 300);'>Boop</button>
</body>
</html>";
}

//exit;
?>