<?php
  $jmsg="";
  $hdate='' . date('A h:i:s l jS \of F Y');
  
  if (isset($_GET['bcn']) && isset($_GET['msg'])) {
    $phpis=file_get_contents(dirname(__FILE__) . "/" . "broadcast_api_test.php");
    $parts=explode('/' . '/ ' . 'bcdata', $phpis);
    $parte=explode('/' . '/ end of ' . 'bcdata', $phpis);
    if (sizeof($parts) == 2 && sizeof($parte) == 2) {
      $zparte=explode(' bcn=' . str_replace('+',' ',urldecode($_GET['bcn'])) . ' msg=', $phpis);
      $zparts=explode(date('l jS \of F Y') . ': bcn=' . str_replace('+',' ',urldecode($_GET['bcn'])) . ' msg=', $phpis);
      if (sizeof($zparte) > 1) {
      $prehdate="/" . "/ " . explode("/" . "/ ", ($zparte[-2 + sizeof($zparte)]))[-1 + sizeof(explode("/" . "/ ", ($zparte[-2 + sizeof($zparte)])))];
      $midhdate=' bcn=' . str_replace('+',' ',urldecode($_GET['bcn'])) . ' msg=';
      $posthdate=explode("\n", $zparte[-1 + sizeof($zparte)])[0] . "\n";
      file_put_contents(dirname(__FILE__) . "/" . "broadcast_api_test.php", str_replace($prehdate . $midhdate . $posthdate, "/" . "/ " . $hdate . ": bcn=" . str_replace('+',' ',urldecode($_GET['bcn'])) . ' msg=' . base64_encode(str_replace('+',' ',urldecode($_GET['msg']))) . "\n", $phpis));
      } else {
      file_put_contents(dirname(__FILE__) . "/" . "broadcast_api_test.php", $parte[0] . "\n/" . "/ " . $hdate . ": bcn=" . str_replace('+',' ',urldecode($_GET['bcn'])) . ' msg=' . base64_encode(str_replace('+',' ',urldecode($_GET['msg']))) . "\n" . '/' . '/ end of ' . 'bcdata' . $parte[1]);
      }
      exit;
    }
  } else if (isset($_GET['bcn']) && isset($_GET['what'])) {
    $phpis=file_get_contents(dirname(__FILE__) . "/" . "broadcast_api_test.php");
    $parte=explode(' bcn=' . str_replace('+',' ',urldecode($_GET['bcn'])) . ' msg=', $phpis);
    $parts=explode(date('l jS \of F Y') . ': bcn=' . str_replace('+',' ',urldecode($_GET['bcn'])) . ' msg=', $phpis);
    if (sizeof($parts) >= 2) {
      $prehdate='' . date('A');
      $midhdate=explode(" ", trim($parts[-2 + sizeof($parts)]))[-1 + sizeof(explode(" ", trim($parts[-2 + sizeof($parts)])))];
      $posthdate='' . date('l jS \of F Y');
      $jmsg=base64_decode(explode("\n", $parts[-1 + sizeof($parts)])[0]);
    }
  } else if (isset($_GET['tidy'])) {
    $pregm='#^//[ ][AP]M[ ][0-9][0-9][:][0-9][0-9][:][0-9][0-9][ ]' . str_replace(' ','[ ]', '' . date('l jS \of F Y')) . '[:][ ]bcn[=][^\ ]+[ ]msg[=][A-Za-z0-9+/= ]+$#m';
    $phpis=file_get_contents(dirname(__FILE__) . "/" . "broadcast_api_test.php");
    $recs=file(dirname(__FILE__) . "/" . "broadcast_api_test.php");
    $parts=explode('/' . '/ ' . 'bcdata', $phpis);
    $parte=explode('/' . '/ end of ' . 'bcdata', $phpis);
    if (sizeof($parts) == 2 && sizeof($parte) == 2) {
     $betw="\n";
     for ($jj=0; $jj<sizeof($recs); $jj++) {
      $nw=preg_match($pregm, $recs[$jj], $matches);
      if ($nw) {
       for ($ii=0; $ii<sizeof($matches); $ii++) {
        if (strpos($matches[$ii], " bcn=") !== false) {
         if (strpos($betw, $matches[$ii]) === false) {
          $betw.='' . $matches[$ii] . "\n";
         }
        } 
       }
      }
     }
     file_put_contents(dirname(__FILE__) . "/" . "broadcast_api_test.php", $parts[0] . '/' . '/ ' . 'bcdata' . explode('/' . '/ end of ' . 'bcdata', $betw)[0] . '/' . '/ end of ' . 'bcdata' . $parte[1]);
     exit;
    }
  }

 if ($jmsg != "") {
   echo "<html><head><scr" . "ipt type='text/javascript'> function onl() { if (parent.document.getElementById(\"hdatetime\").value != \"" . $prehdate . ' ' . $midhdate . ' ' . $posthdate . "\") { parent.document.getElementById(\"hdatetime\").value=\"" . $hdate . "\"; parent.document.getElementById(\"tab\").value=\"" . str_replace("\n", '" + String.fromCharCode(10) + "', str_replace('"', '" + String.fromCharCode(34) + "', $jmsg)) . "\";  parent.document.getElementById('ta').value=\"" . str_replace("\n", '" + String.fromCharCode(10) + "', str_replace('"', '" + String.fromCharCode(34) + "', $jmsg)) . "\"; } } </scr" . "ipt></head><body onload=onl();></body></html>";
   exit;
 }
?>
<html>
<head>
<title>Broadcast API Usage - RJM Programming - July, 2022 ... thanks to https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API</title>
</head>
<body>
<style> 
  td { vertical-align: top; } 
  #ta { width: 98%; background-color: yellow; }
  #tab { width: 98%; background-color: lightblue; }
  .ac { font-size:34px; }
  .nce { font-size:24px;  width:26%; background-color:rgb(230,230,230); text-shadow: -1px 1px 2px #ff2d95; }
  input { background-color:rgb(250,250,250); margin-bottom:8px;  }
</style>
<h2>Broadcast API Usage</h2>
<h3>RJM Programming - July, 2022</h3>
<h4>Thanks to <a target=_blank title='https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API' href='//developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API'>https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API</a></h4>
<br><table border=20 style='width:100%;' cellspacing=5 cellpadding=5>
<tr><th title='Default broadcaster name you can change' contenteditable=true class=nce id=bname>Broadcaster</th><th id=binvite><select onchange=seli(this); id=bsel><option value='mailto:'>Email &#128231;</option><option value='sms:'>SMS &#128223;</option></select>&nbsp;<a class=ac style='text-decoration:none;cursor:pointer;' href='mailto:?subject=Invite%20a%20Broadcaster%20...&body=' id=abinvite title='Invite a Broadcaster they give a Channel name to'>&#9993;</a>&nbsp;<a class=ac style='text-decoration:none;cursor:pointer;' href='mailto:?subject=Invite%20a%20Broadcaster%20...&body=' id=abinviter title='Invite a Broadcaster with random Channel name'>&#127942;</a></th><th title='Default listener name you can change' contenteditable=true class=nce id=lname>Listener</th><th id=linvite><select onchange=seli(this); id=lsel><option value='mailto:'>Email &#128231;</option><option value='sms:'>SMS &#128223;</option></select>&nbsp;<a class=ac style='text-decoration:none;cursor:pointer;' href='mailto:?subject=Invite%20a%20Listener%20to%20Channel%20they%20Name%20...&body=' id=alinvite title='Invite a Listener they supply a Channel name for'>&#9993;</a>&nbsp;<a class=ac style='text-decoration:none;cursor:pointer;' href='mailto:?subject=Invite%20a%20Listener%20to%20Channel%20they%20Name%20...&body=' id=alinviter title='Invite a Listener of random Channel name'>&#127942;</a></th></tr>
<tr><td id=blist colspan=2>
<input style='width:100%;' id=itab type=text placeholder='RJM Programming Broadcast Channel Name of yours' value='' onblur='newone(this);'></input>
<textarea title='Where you can type your broadcaster message' id=tab placeholder='' value='' onblur='sthis(this);' rows=5 cols=80></textarea>
</td>
<td id=btextarea colspan=2>
<input id=ita style='width:100%;' type=text placeholder='RJM Programming Broadcast Channel Name of broadcaster to subscribe to' value='' onblur='onenew(this);'></input>
<textarea title='Where you can type your listener message' id=ta onblur='sthis(this);' placeholder='' value='' rows=5 cols=80></textarea>
</td></tr>
</table>

<script type='text/javascript'>
  var rname=location.search.split('randname=')[1] ? decodeURIComponent(location.search.split('randname=')[1].split('&')[0]).replace(/\+/g,' ') : "";
  var ebcn=location.search.split('bcnm=')[1] ? rname : "";
  var elcn=location.search.split('lcnm=')[1] ? rname : "";
  var lastmsg='';
  let ourbc=null;
  let yourbc=null;
  let pbc=null;
  let bc=null;
  var wois=null;
  var isdef=false;
  var smsn=null;
  var mf=Math.floor(Math.random() * 19878654);
  if (rname != '') { mf=eval(rname.replace('bcname',''));   }
  var abinvitedef='&#9993;';
  var altabinvitedef='&#127942;';
  document.getElementById('abinvite').href+=encodeURIComponent(document.URL.split('#')[0].split('?')[0].replace('http:','https:'));
  document.getElementById('abinviter').href=document.getElementById('abinviter').href.replace('Invite%20a%20Broadcaster%20...','Invite%20the%20Broadcaster%20for%20Channel%20bcname' + mf + '%20...') + encodeURIComponent(document.URL.split('#')[0].split('?')[0].replace('http:','https:') + '?bcnm=&randname=bcname' + mf);
  document.getElementById('alinvite').href+=encodeURIComponent(document.URL.split('#')[0].split('?')[0].replace('http:','https:')); 
  document.getElementById('alinviter').href=document.getElementById('alinviter').href.replace('Invite%20a%20Listener%20to%20Channel%20they%20Name%20...','Invite%20a%20Listener%20to%20Channel%20bcname' + mf + '%20...') + encodeURIComponent(document.URL.split('#')[0].split('?')[0].replace('http:','https:') + '?lcnm=&randname=bcname' + mf);
  document.getElementById('abinviter').title+=' bcname' + mf;
  document.getElementById('alinviter').title+=' bcname' + mf;
  document.getElementById('abinvite').setAttribute('data-href', '' + document.getElementById('abinvite').href);
  document.getElementById('abinviter').setAttribute('data-href', '' + document.getElementById('abinviter').href);
  document.getElementById('alinvite').setAttribute('data-href', '' + document.getElementById('alinvite').href);
  document.getElementById('alinviter').setAttribute('data-href', '' + document.getElementById('alinviter').href);
  
  var ebc=location.search.split('bc=')[1] ? decodeURIComponent(location.search.split('bc=')[1].split('&')[0]).replace(/\+/g,' ') : "";
  if (ebc.trim() != '') {
    //const bc=null;
    document.getElementById('itab').placeholder=ebc;
    document.getElementById('ita').placeholder=ebc;
    document.getElementById('itab').value='';
    document.getElementById('ita').value='';
    document.getElementById('itab').onblur=function(event) { event.target.value=ebc; }
    document.getElementById('ita').onblur=function(event) { event.target.value=ebc; }
    document.getElementById('itab').setAttribute('readonly',true);
    document.getElementById('tab').setAttribute('readonly',true);
    document.getElementById('tab').style.display='none';
    yourbc = new BroadcastChannel(ebc);
    yourbc.onmessage = event => { 
      if (minussc(event.data) != lastmsg) { 
        //alert('in with msg=' + event.data);
        //document.getElementById('ta').value+=String.fromCharCode(10) + event.data.replace(lastmsg, '');  
        document.getElementById('ta').value=String.fromCharCode(10) + event.data; //.replace(lastmsg, '');  
        document.getElementById('ta').title='' + new Date();
        document.getElementById('ta').rows='' + oureval(3 + eval('' + document.getElementById('ta').rows));  
        document.getElementById('tab').rows='' + oureval(3 + eval('' + document.getElementById('tab').rows)); 
        if (yourbc) { 
          //yourbc.postMessage(document.getElementById('lname').innerHTML + ': ' + minussc(event.data)); //.replace(lastmsg, ''));  
          yourbc.postMessage(event.data); //.replace(lastmsg, ''));  
          window.opener.andback(); // wois.focus();
        } 
        lastmsg=minussc(event.data); 
      } 
    };
    wois=window.opener;
  }
  if (ebcn != '') {
    document.getElementById('ita').value=ebcn;
    document.getElementById('ita').placeholder=ebcn;
    newone(document.getElementById('ita'));
  }
  if (elcn != '') {
    document.getElementById('ita').value=elcn;
    document.getElementById('ita').placeholder=elcn;
    document.getElementById('itab').value=elcn;
    document.getElementById('itab').placeholder=elcn;
    newone(document.getElementById('ita')); //onenew(document.getElementById('itab'));
  }
  
  function seli(inso) {
    if (('' + inso.id + ' ').substring(0,1) == 'b') {
      if (inso.value == 'mailto:') {
        document.getElementById('abinvite').href='' + document.getElementById('abinvite').getAttribute('data-href');
        document.getElementById('abinviter').href='' + document.getElementById('abinviter').getAttribute('data-href');
      } else if (inso.value == 'sms:') {
        if (smsn == null) { smsn=prompt('Please enter SMS number to send to.','');    }
        if (smsn != null) {
        if (('' + smsn).trim() != '') {
        document.getElementById('abinvite').href=='sms:' + smsn + '&body=' + document.getElementById('abinvite').getAttribute('data-href').split('&body=')[1];
        document.getElementById('abinviter').href='sms:' + smsn + '&body=' + document.getElementById('abinviter').getAttribute('data-href').split('&body=')[1];
        } else {
        inso.value='mailto:';
        }
        } else {
        inso.value='mailto:';
        }
      }
    } else if (('' + inso.id + ' ').substring(0,1) == 'l') {
      if (inso.value == 'mailto:') {
        document.getElementById('alinvite').href='' + document.getElementById('alinvite').getAttribute('data-href');
        document.getElementById('alinviter').href='' + document.getElementById('alinviter').getAttribute('data-href');
      } else if (inso.value == 'sms:') {
        if (smsn == null) { smsn=prompt('Please enter SMS number to send to.','');    }
        if (smsn != null) {
        if (('' + smsn).trim() != '') {
        document.getElementById('alinvite').href=='sms:' + smsn + '&body=' + document.getElementById('alinvite').getAttribute('data-href').split('&body=')[1];
        document.getElementById('alinviter').href='sms:' + smsn + '&body=' + document.getElementById('alinviter').getAttribute('data-href').split('&body=')[1];
        } else {
        inso.value='mailto:';
        }
        } else {
        inso.value='mailto:';
        }
      }
    }
  }
  
  function andback() {
    //alert('Am back');
    window.focus();
  }
  
  function oureval(inv) {
    var tabi=document.getElementById('tab').value.split(String.fromCharCode(10));
    var tai=document.getElementById('ta').value.split(String.fromCharCode(10));
    return Math.max(eval(2 + eval('' + tabi.length)), eval(2 + eval('' + tai.length)));
  }

  function newone(iv) {
   if (iv.value.trim() != '') {
    // Connection to a broadcast channel
    //if (ourbc) {
    //  ourbc.close();
    //}
    //const bc = new BroadcastChannel(iv.value);
    var ivalt=iv.value.replace(/\ /g,'').replace(/\//g,'');
    if (ivalt != iv.value) { iv.value=ivalt; }
    bc = new BroadcastChannel(iv.value);
    pbc=bc;
    ourbc=bc;
    document.getElementById('ita').setAttribute('readonly',true);
    document.getElementById('ta').setAttribute('readonly',true);
    document.getElementById('ta').style.display='none';
    ebc=iv.value;
    document.getElementById('itab').placeholder=ebc;
    document.getElementById('ita').placeholder=ebc;
    document.getElementById('itab').value='';
    document.getElementById('ita').value='';
    document.getElementById('itab').onblur=function(event) { event.target.value=ebc; }
    document.getElementById('ita').onblur=function(event) { event.target.value=ebc; }
    bc.onmessage = event => { 
      if (minussc(event.data) != lastmsg) { 
        //document.getElementById('tab').value+=String.fromCharCode(10) + event.data.replace(lastmsg, '');  
        document.getElementById('tab').value=String.fromCharCode(10) + event.data; //.replace(lastmsg, '');  
        document.getElementById('tab').title='' + new Date();
        document.getElementById('ta').rows='' + oureval(3 + eval('' + document.getElementById('ta').rows));  
        document.getElementById('tab').rows='' + oureval(3 + eval('' + document.getElementById('tab').rows)); 
        if (bc) { 
          //bc.postMessage(document.getElementById('bname').innerHTML + ': ' + minussc(event.data)); //.replace(lastmsg, ''));  
          bc.postMessage(event.data); //.replace(lastmsg, ''));  
          wois.focus();
        } 
        lastmsg=minussc(event.data); 
      } 
    };
    if (elcn != '') {
    wois=window.open(document.URL.split('#')[0].split('?')[0] + '?bc=' + encodeURIComponent(iv.placeholder), '_blank','top=50,left=500,width=800,height=800');
    } else if (document.URL.indexOf('nowin=') == -1) {
    wois=window.open(document.URL.split('#')[0].split('?')[0] + '?bc=' + encodeURIComponent(iv.placeholder), '_blank');
    }
   }
  }
  
  function onenew(iv) {
    yourbc = new BroadcastChannel(iv.value);
    yourbc.onmessage = event => { 
      if (minussc(event.data) != lastmsg) { 
        //document.getElementById('tab').value+=String.fromCharCode(10) + event.data.replace(lastmsg, '');  
        document.getElementById('tab').value=String.fromCharCode(10) + event.data; //.replace(lastmsg, '');  
        document.getElementById('tab').title='' + new Date();
        document.getElementById('ta').rows='' + oureval(3 + eval('' + document.getElementById('ta').rows));  
        document.getElementById('tab').rows='' + oureval(3 + eval('' + document.getElementById('tab').rows)); 
        if (ourbc) { 
          //ourbc.postMessage(document.getElementById('bname').innerHTML + ': ' + minussc(event.data)); //.replace(lastmsg, ''));  
          ourbc.postMessage(event.data); //.replace(lastmsg, ''));  
          wois.focus();
        } 
        lastmsg=minussc(event.data); 
      } 
    };
  }
  
  function minussc(insa) {
    if (insa.indexOf(': ') != -1) {
      return insa.replace(insa.split(': ')[0] + ': ','');
    }
    return insa;
  }
  
  function sthis(iv) {
    if (iv.value.trim() != '') {
    if (yourbc && !pbc) {
      yourbc.postMessage(ifit(document.getElementById('lname').innerHTML + ': ' + minussc(iv.value)));
      document.getElementById('ta').rows='' + oureval(3 + eval('' + document.getElementById('ta').rows));
      document.getElementById('tab').rows='' + oureval(3 + eval('' + document.getElementById('tab').rows));
      iv.placeholder+=String.fromCharCode(10) + iv.value;
      iv.value='';
    } else if (bc) {
      //alert('at broadcaster with msg=' + iv.value);
      //bc=newone(document.getElementById('itab'));
      bc.postMessage(ifit(document.getElementById('bname').innerHTML + ': ' + minussc(iv.value)));
      document.getElementById('ta').rows='' + oureval(3 + eval('' + document.getElementById('ta').rows));
      document.getElementById('tab').rows='' + oureval(3 + eval('' + document.getElementById('tab').rows));
      iv.placeholder+=String.fromCharCode(10) + iv.value;
      iv.value='';
    }
    }
  }
  
  function ifit(instg) {
    if (document.getElementById('itab').placeholder.indexOf('RJM Programming') == -1 && document.getElementById('itab').placeholder != '') {
    document.getElementById('phpbat').src='./broadcast_api_test.php?bcn=' + encodeURIComponent(document.getElementById('itab').placeholder) + '&msg=' + encodeURIComponent(instg) + '&rand=' + Math.floor(Math.random() * 198767543);
    } else if (document.getElementById('ita').placeholder.indexOf('RJM Programming') == -1 && document.getElementById('ita').placeholder != '') {
    document.getElementById('phpbat').src='./broadcast_api_test.php?bcn=' + encodeURIComponent(document.getElementById('ita').placeholder) + '&msg=' + encodeURIComponent(instg) + '&rand=' + Math.floor(Math.random() * 198767543);
    } else if (document.getElementById('itab').value.trim() != '') {
    document.getElementById('phpbat').src='./broadcast_api_test.php?bcn=' + encodeURIComponent(document.getElementById('itab').value) + '&msg=' + encodeURIComponent(instg) + '&rand=' + Math.floor(Math.random() * 198767543);
    } else if (document.getElementById('ita').value.trim() != '') {
    document.getElementById('phpbat').src='./broadcast_api_test.php?bcn=' + encodeURIComponent(document.getElementById('ita').value) + '&msg=' + encodeURIComponent(instg) + '&rand=' + Math.floor(Math.random() * 198767543);
    }
    return instg;
  }
  
  function ifwhatit() {
    if (document.getElementById('itab').placeholder.indexOf('RJM Programming') == -1 && document.getElementById('itab').placeholder != '') {
    document.getElementById('phpwhatbat').src='./broadcast_api_test.php?bcn=' + encodeURIComponent(document.getElementById('itab').placeholder) + '&what=' + Math.floor(Math.random() * 198767543);
    } else if (document.getElementById('ita').placeholder.indexOf('RJM Programming') == -1 && document.getElementById('ita').placeholder != '') {
    document.getElementById('phpwhatbat').src='./broadcast_api_test.php?bcn=' + encodeURIComponent(document.getElementById('ita').placeholder) + '&what=' + Math.floor(Math.random() * 198767543);
    } else if (document.getElementById('itab').value.trim() != '') {
    document.getElementById('phpwhatbat').src='./broadcast_api_test.php?bcn=' + encodeURIComponent(document.getElementById('itab').value) + '&what=' + Math.floor(Math.random() * 198767543);
    } else if (document.getElementById('ita').value.trim() != '') {
    document.getElementById('phpwhatbat').src='./broadcast_api_test.php?bcn=' + encodeURIComponent(document.getElementById('ita').value) + '&what=' + Math.floor(Math.random() * 198767543);
    }
    if (isdef) {
      document.getElementById('abinviter').innerHTML=altabinvitedef;
      document.getElementById('alinviter').innerHTML=altabinvitedef;
      isdef=false;
    } else {
      document.getElementById('abinviter').innerHTML=abinvitedef;
      document.getElementById('alinviter').innerHTML=abinvitedef;
      isdef=true;
    }
  }
  
  setInterval(ifwhatit, 8000);
  
<?php 
?>
  
</script>
<input id=hdatetime value='' type=hidden></input>
<iframe id=phpbat src='//www.rjmprogramming.com.au/About_Us.html' style='display:none;'></iframe>
<iframe id=phpwhatbat src='//www.rjmprogramming.com.au/About_Us.html' style='display:none;'></iframe>
</body>
</html>
<?php
// bcdata

// AM 07:56:58 Monday 11th of July 2022: bcn=bcname13621408 msg=TGlzdGVabmVyOiB0aGF0J3MgaW50ZWxsZXN0aW4gdGhlcmU=
// end of bcdata
?>
