<?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);

function server_remote_addr() {
    $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;
    return str_replace(":", "-", $rma);
}

if ($argv) {  // command line ...
    while (1) {
    foreach (glob("chat_*.rls") as $ourfilename) {      
      //rename($ourfilename, explode("__", $ourfilename)[0] . ".line");
      rename($ourfilename, explode(".rls", $ourfilename)[0] . ".line");
    }
    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 (1) {
    foreach (glob("c_" . server_remote_addr() . "__*.rec") 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=\" parent.document.getElementById('tdchatin').innerHTML+='" . str_replace("\n", "<br>",file_get_contents($ourfilename)) . "<br>';   parent.document.getElementById('ichat').value='';   \"></body></html>";
      unlink($ourfilename);
      //exit;
    }
      //exit;
    }
    if (1 == 4) { sleep(5); }
    }
   exit;
} else if (isset($_POST['ipaddress']) && isset($_POST['ichat'])) { 
   //file_put_contents("c_" . str_replace("+"," ",urldecode($_POST['ipaddress'])) . "__" . server_remote_addr() . ".rxec", str_replace("+"," ",urldecode($_POST['ichat'])));
   file_put_contents("c_" . str_replace("+"," ",urldecode($_POST['ipaddress'])) . "__" . server_remote_addr() . ".rec", str_replace("+"," ",urldecode($_POST['ichat'])));
    if (1) {
    foreach (glob("c_" . server_remote_addr() . "__*.rec") 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=\" parent.document.getElementById('tdchatin').innerHTML+='" . str_replace("\n", "<br>",file_get_contents($ourfilename)) . "<br>';  \"></body></html>";
      unlink($ourfilename);
      //exit;
    }
      //exit;
    }
    if (1 == 4) { sleep(5); }
    }
   exit;
} else if (isset($_GET['nothing'])) { 
  exit;
} else if (isset($_GET['address']) && isset($_GET['ipaddress'])) { 
   file_put_contents("chat_" . str_replace("@","",str_replace("+"," ",urldecode($_GET['address']))) . "__" . str_replace("+"," ",urldecode($_GET['ipaddress'])) . ".rls", str_replace("+"," ",urldecode($_GET['ichat'])));
   exit;
} else if (isset($_GET['address'])) { 
  if (urldecode($_GET['address']) == "") {
    if (1 == 1) {
    foreach (glob("c_" . server_remote_addr() . "__*.rec") 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=\" parent.document.getElementById('tdchatin').innerHTML+='" . str_replace("\n", "<br>",file_get_contents($ourfilename)) . "<br>';   parent.document.getElementById('ichat').value='';   \"></body></html>";
      unlink($ourfilename);
      //exit;
    }
      //exit;
    }
    if (1 == 4) { sleep(5); }
    }
    if (1 == 13) {
    foreach (glob("c_*__" . server_remote_addr() . ".rec") 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=\" parent.document.getElementById('tdchatin').innerHTML+='" . str_replace("\n", "<br>",file_get_contents($ourfilename)) . "<br>';   parent.document.getElementById('ichat').value='';   \"></body></html>";
      unlink($ourfilename);
      //exit;
    }
      //exit;
    }
    if (1 == 4) { sleep(5); }
    }
    exit;
  } else {  // here from email or SMS link
    $sra="";
    $cont="";
    foreach (glob("chat_" . str_replace("@","",str_replace("+"," ",urldecode($_GET['address']))) . "__*.line") as $ourfilename) { 
      $sra=explode(".",explode("chat_" . str_replace("@","",str_replace("+"," ",urldecode($_GET['address']))) . "__", $ourfilename)[1])[0];
      $cont=file_get_contents($ourfilename) . "<br>";
      unlink($ourfilename);
    }
   echo "<!doctype html>
<html>
<head>
<script type='text/javascript'>
  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 butlogic() {
    if (document.getElementById('isms').value.trim() != '') { 
     document.getElementById('jchild').src=document.URL.split('#')[0].split('?')[0] + '?address=' + encodeURIComponent(document.getElementById('isms').value) + '&ipaddress=' + encodeURIComponent('" . $sra . "') + '&ichat=' + encodeURIComponent(document.getElementById('ichat').value);
     var wasasms=document.getElementById('asms').href;
     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;
    } else if (document.getElementById('iemail').value.indexOf('@') != -1) {   
     document.getElementById('jchild').src=document.URL.split('#')[0].split('?')[0] + '?address=' + encodeURIComponent(document.getElementById('iemail').value) + '&ipaddress=' + encodeURIComponent('" . $sra . "') + '&ichat=' + encodeURIComponent(document.getElementById('ichat').value);
     var wasaemail=document.getElementById('aemail').href;
     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);
  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 onlis() {
    document.getElementById('ichild').src=document.URL.split('#')[0].split('?')[0] + '?x=' + Math.floor(Math.random() * 198765434) + '&address=';
    setTimeout(onlis, 6000);
  }
</script>
</head>
<body onload='onlis();'>
<h1>PHP Listener</h1>
<h3>RJM Programming - August, 2020</h3>
<table style='width:100%;background-color:#f0f0f0;' border=20 cellpadding=5 cellspacing=5>
<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').setAttribute('disabled',false);  }\" style=width:90%; type=text placeholder='Email address' value='' id=iemail></input></td><td></td><td><input onblur=\"if (this.value.trim() != '') { document.getElementById('binvite').setAttribute('disabled',false);  }\" 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>Invite</button></td></tr>
<tr><td colspan=3><hr></td></tr>
<tr><th colspan=3>Chat</th></tr>
<tr><th>In</th><th></th><th>Out</th></tr>
<tr><td id=tdchatin><!--textarea style=width:100%;height:100px; id=chatin-->" . $cont . "<!--/textarea--></td><td></td><td><form target=jchild id=myform action=./php_listener.php method=POST><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();>Send</button></td></tr>
</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?address='></iframe>
<iframe style=display:none; onload='checkitagain(this);' name=jchild id=jchild src='./php_listener.php?nothing='></iframe>
</body>
</html>";
  } 
} else {
   echo "<!doctype html>
<html>
<head>
<script type='text/javascript'>
  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 butlogic() {
    if (document.getElementById('isms').value.trim() != '') { 
     document.getElementById('jchild').src=document.URL.split('#')[0].split('?')[0] + '?address=' + encodeURIComponent(document.getElementById('isms').value) + '&ipaddress=' + encodeURIComponent('" . server_remote_addr() . "') + '&ichat=' + encodeURIComponent(document.getElementById('ichat').value);
     var wasasms=document.getElementById('asms').href;
     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;
    } else if (document.getElementById('iemail').value.indexOf('@') != -1) {   
     document.getElementById('jchild').src=document.URL.split('#')[0].split('?')[0] + '?address=' + encodeURIComponent(document.getElementById('iemail').value) + '&ipaddress=' + encodeURIComponent('" . server_remote_addr() . "') + '&ichat=' + encodeURIComponent(document.getElementById('ichat').value);
     //alert(document.getElementById('jchild').src);
     var wasaemail=document.getElementById('aemail').href;
     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', '');
  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 onlis() {
    document.getElementById('ichild').src=document.URL.split('#')[0].split('?')[0] + '?x=' + Math.floor(Math.random() * 198765434) + '&address=';
    setTimeout(onlis, 6000);
  }
</script>
</head>
<body onload='onlis();'>
<h1>PHP Listener</h1>
<h3>RJM Programming - August, 2020</h3>
<table style='width:100%;background-color:#f0f0f0;' border=20 cellpadding=5 cellspacing=5>
<tr><th colspan=3>Invitation to chat ...</th></tr>
<tr><th>Email</th><th> ... or ...</th><th>SMS</th></tr>
<tr><td><input style=width:90%; type=text placeholder='Email address' value='' id=iemail></input></td><td></td><td><input style=width:90%; type=text placeholder='SMS number' value='' id=isms></input></td></tr>
<tr><td colspan=3 style=text-align:center;><button onclick=butlogic();>Invite</button></td></tr>
<tr><td colspan=3><hr></td></tr>
<tr><th colspan=3>Chat</th></tr>
<tr><th>In</th><th></th><th>Out</th></tr>
<tr><td id=tdchatin><textarea style=width:100%;height:100px; id=chatin></textarea></td><td></td><td><form target=jchild action=./php_listener.php method=POST><input type=hidden value='" . server_remote_addr() . "' 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();>Send</button></td></tr>
</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?address='></iframe>
<iframe style=display:none; onload='checkitagain(this);' name=jchild id=jchild src='./php_listener.php?nothing='></iframe>
</body>
</html>";
}

exit;
?>