<?php
// macos_say_record.php
// Supervise and synchronize 'say [words]' (say) some text

$thecontent="";
$tastuff="";
$daudio="";
$audiois="";

if (PHP_OS == "Darwin") {
if (isset($_GET['saythis']) && isset($_GET['audioname'])) {
  if ($_GET['saythis'] != "") {
  file_put_contents("xqx.xqx","say -o " . dirname(__FILE__) . "/" . str_replace("+"," ",urldecode($_GET['audioname'])) . " " . str_replace("\n",",,,,,,",$thewords) . " > " . dirname(__FILE__) . "/sayout.out" . " 2> " . dirname(__FILE__) . "/sayerr.err");
  $thewords=str_replace("+"," ",urldecode($_GET['saythis']));
  file_put_contents("xq.xq","say -o " . dirname(__FILE__) . "/" . str_replace("+"," ",urldecode($_GET['audioname'])) . " " . str_replace("\n",",,,,,,",$thewords) . " > " . dirname(__FILE__) . "/sayout.out" . " 2> " . dirname(__FILE__) . "/sayerr.err");
  exec("say -o " . dirname(__FILE__) . "/" . str_replace("+"," ",urldecode($_GET['audioname'])) . " " . str_replace("\n",",,,,,,",$thewords) . " > " . dirname(__FILE__) . "/sayout.out" . " 2> " . dirname(__FILE__) . "/sayerr.err");
  }
} else if (isset($_POST['saythis']) && isset($_POST['audioname'])) {
  if ($_POST['saythis'] != "") {
  file_put_contents("xqqx.xqqx","say -o " . dirname(__FILE__) . "/" . str_replace("+"," ",urldecode($_POST['audioname'])) . " " . str_replace("\n",",,,,,,",$thewords) . " > " . dirname(__FILE__) . "/sayout.out" . " 2> " . dirname(__FILE__) . "/sayerr.err");
  $thewords=str_replace("+"," ",urldecode($_POST['saythis']));
  //echo "say -o " . dirname(__FILE__) . "/" . str_replace("+"," ",urldecode($_POST['audioname'])) . " " . str_replace("\n",",,,,,,",$thewords);
  //exit;
  file_put_contents("xqq.xqq","say -o " . dirname(__FILE__) . "/" . str_replace("+"," ",urldecode($_POST['audioname'])) . " " . str_replace("\n",",,,,,,",$thewords) . " > " . dirname(__FILE__) . "/sayout.out" . " 2> " . dirname(__FILE__) . "/sayerr.err");
  exec("say -o " . dirname(__FILE__) . "/" . str_replace("+"," ",urldecode($_POST['audioname'])) . " " . str_replace("\n",",,,,,,",$thewords) . " > " . dirname(__FILE__) . "/sayout.out" . " 2> " . dirname(__FILE__) . "/sayerr.err");
  }
} else if ((!isset($_GET['saythis']) && !isset($_POST['saythis'])) || !isset($_GET['audioname']) || !isset($_POST['audioname'])) {
  $audiois="";
  if (isset($_GET['audioname'])) {
    $audiois="<audio controls autoplay loop><source type=audio/x-aiff src=" . str_replace("+"," ",urldecode($_GET['audioname'])) . "></source></audio>";
    $thecontent="data:audio/x-aiff;base64," . base64_encode(file_get_contents(dirname(__FILE__) . "/" . str_replace("+"," ",urldecode($_GET['audioname']))));
    $daudio="<audio id=thisaudio controls loop><source type=audio/x-aiff src=" . $thecontent . "></source></audio>";
    //unlink(dirname(__FILE__) . "/" . str_replace("+"," ",urldecode($_GET['audioname'])));
  } else if (isset($_POST['audioname'])) {  
    $audiois="<audio controls autoplay loop><source type=audio/x-aiff src=" . str_replace("+"," ",urldecode($_POST['audioname'])) . "></source></audio>";
    $thecontent="data:audio/x-aiff;base64," . base64_encode(file_get_contents(dirname(__FILE__) . "/" . str_replace("+"," ",urldecode($_POST['audioname']))));
    $daudio="<audio id=thisaudio controls loop><source type=audio/x-aiff src=" . $thecontent . "></source></audio>";
    //unlink(dirname(__FILE__) . "/" . str_replace("+"," ",urldecode($_POST['audioname'])));
  }
  if (isset($_GET['saythis'])) {
    $tastuff=str_replace("+"," ",urldecode($_GET['saythis']));
  } else if (isset($_GET['saythis'])) {
    $tastuff=str_replace("+"," ",urldecode($_POST['saythis']));
  }
  echo "<html>
<head>
<script type='text/javascript'>
  var wasv='';
  var firstgo=true;

  function thenreload() {
    location.href=document.URL.split('?')[0].split('#')[0] + '?audioname=' + encodeURIComponent(document.getElementById('audioname').value) + '&dummysaythis=' + encodeURIComponent(document.getElementById('thewords').value);
  }
  
  function resultchange() {
    if (document.getElementById('result').value == '') {
    setTimeout(resultchange, 2000);
    } else if (firstgo) {
    firstgo=false;
    setTimeout(resultchange, 6000);
    } else if (window.opener) {
    window.opener.document.getElementById('aurl').value=document.getElementById('result').value;
    window.opener.document.getElementById('daudio').innerHTML='" . $daudio . "'; 
    window.opener.atab(window.opener.document.getElementById('aurl'));
    }
  }
</script>
</head>
<body onload='resultchange();'>
<h1>Supervise and synchronize 'say [words]'</h1>
<h3>RJM Programming - July, 2020</h3>
" . $audiois . "
<form id=myform onsubmit=\"document.getElementById('myform').style.cursor='progress'; document.body.style.cursor='progress'; setTimeout(thenreload,20000); return true;\" target=dothework action=./macos_say_record.php method=POST>
<table border=20>
<tr><th>Words</th><th>Record to <input type=text name=audioname id=audioname value='audiocapture.aiff'></input></th></tr>
<tr><td style=vertical-align:top;><textarea id=thewords name=saythis cols=80 rows=10 value=''>" . $tastuff . "</textarea></td><td style=vertical-align:top;><input style='width:100%;height:100px;background-color:yellow;' type=submit value='Record These Words'></input><br><textarea style=width:200px id=result cols=80 rows=10>" . $thecontent . "</textarea></td></tr>
</table>
</form>
<iframe name=dothework id=dothework style=display:none; src=macos_say_record.php></iframe>
</body>
</html>";
} 
} else {
 echo "<html><body><p>This PHP web application only works with macOS and its 'say [words]' command line Text to Speech and/or Audio functionality.</p></body><html>";
 exit;
}
?>
