<?php
  // open_extravaganza.php
  // RJM Programming
  // November, 2025
  $afteropen='';
  $preret='';
  $ret='';
  $selstuff='';
  $afteropens=[];
  $delim='';
  $thisone=1;
  $wds="\\";
  
  if (PHP_OS == 'Darwin') {
    $selstuff="<br><br><form onsubmit=\"formclickmode=0; document.body.style.cursor='pointer'; if (lastselo) { lastselo.style.cursor='pointer'; lastselo=null; } if (document.getElementById('opsel')) { if (document.getElementById('opsel').value.length > 0) { this.action=document.getElementById('opsel').value;  }   } return true;\" id=myform method=GET action='./open_extravaganza.php'><select style=background-color:yellow; size=15 id=mysel multiple name=afteropen onchange=\"if (this.value.length != 0) { if (1 == 1) { formclickmode=prg(1,event.target); } else { document.getElementById('sbutmac').click(); } } \"><option value=''>Select macOS open command type(s) below ...</option><option value=' -a Finder'>Finder</option><option value=' -a Preview'>Preview</option><option value=' -a TextEdit'>TextEdit</option><option value=' -b com.apple.VoiceMemos'>Voice Memo</option><option value=' -b com.apple.QuickTimePlayerX https://www.rjmprogramming.com.au/Windows/windows_step_recorder.mov'>QuickTime Player</option><option value=' ~/Desktop/Screen\\ Shot\\ *.png'>Preview all screenshot files</option><option value=' -a Firefox https://www.rjmprogramming.com.au'>Firefox</option><option value=' -a Safari http://localhost:8888/macos_say_record.php'>Safari interface attempt to say</option><option value=' ~/Documents ~/Desktop ~/Downloads'>Open the D folders</option><option value=' https://google.com'>Google</option><option value=' https://wikipedia.org'>Wikipedia</option><option value=' https://www.rjmprogramming.com.au'>RJM Programming</option><option value='ls -l . | open -f'>Pipe folder listing into TextEdit</option><option value='x-man-page://open'>Show man page in Terminal</option></select><br><br><input id=sbutmac type=submit value='Execute'></input></form>";
  } else if (substr(strtolower('' . PHP_OS),0,3) == 'win') {
    $selstuff="<br><br><form onsubmit=\"formclickmode=0; document.body.style.cursor='pointer'; if (lastselo) { lastselo.style.cursor='pointer'; lastselo=null; } if (document.getElementById('opsel')) {  if (document.getElementById('opsel').value.length > 0) {  this.action=document.getElementById('opsel').value;   }  } return true;\" id=myform method=GET action='./open_extravaganza.php'><select style=background-color:yellow; size=5 id=mysel multiple name=afteropen onchange=\"if (this.value.length != 0) { if (1 == 1) { formclickmode=prg(-1,event.target); } else { document.getElementById('sbutwin').click(); } } \"><option value=''>Select Windows explorer.exe command type(s) below ...</option><option value=' '>File Explorer</option><option value=' https://google.com'>Google</option><option value=' https://wikipedia.org'>Wikipedia</option><option value=' https://www.rjmprogramming.com.au'>RJM Programming</option></select><br><br><input id=sbutwin type=submit value='Execute'></input></form>";
  } else {
    $selstuff="<br><br><form target=_blank onsubmit=\"formclickmode=0; document.body.style.cursor='pointer'; if (lastselo) { lastselo.style.cursor='pointer'; lastselo=null; } if (document.getElementById('opsel')) { if (document.getElementById('opsel').value.length > 0) { this.action=document.getElementById('opsel').value;  }   } return true;\" id=myformmac method=GET style=display:none; action='http://localhost:8888/open_extravaganza.php'><select multiple style=background-color:yellow; size=15 id=myselmac name=afteropen onchange=\"if (this.value.length != 0) { if (1 == 1) { formclickmode=prg(1,event.target); } else { document.getElementById('sbutmac').click(); }  } \"><option value=''>Select macOS open command type(s) below ...</option><option value=' -a Finder'>Finder</option><option value=' -a Preview'>Preview</option><option value=' -a TextEdit'>TextEdit</option><option value=' -b com.apple.VoiceMemos'>Voice Memo</option><option value=' -b com.apple.QuickTimePlayerX https://www.rjmprogramming.com.au/Windows/windows_step_recorder.mov'>QuickTime Player</option><option value=' ~/Desktop/Screen\\ Shot\\ *.png'>Preview all screenshot files</option><option value=' -a Firefox https://www.rjmprogramming.com.au'>Firefox</option><option value=' -a Safari http://localhost:8888/macos_say_record.php'>Safari interface attempt to say</option><option value=' ~/Documents ~/Desktop ~/Downloads'>Open the D folders</option><option value=' https://google.com'>Google</option><option value=' https://wikipedia.org'>Wikipedia</option><option value=' https://www.rjmprogramming.com.au'>RJM Programming</option><option value='ls -l . | open -f'>Pipe folder listing into TextEdit</option><option value='x-man-page://open'>Show man page in Terminal</option></select><br><br><input id=sbutmac type=submit value='Execute'></input></form>";
    $selstuff.="<br><br><form target=_blank onsubmit=\"formclickmode=0; document.body.style.cursor='pointer'; if (lastselo) { lastselo.style.cursor='pointer'; lastselo=null; } if (document.getElementById('opsel')) {  if (document.getElementById('opsel').value.length > 0) {  this.action=document.getElementById('opsel').value;   }  } return true;\" id=myformwin method=GET style=display:none; action='http://localhost/open_extravaganza.php'><select multiple style=background-color:yellow; size=4 id=myselwin name=afteropen onchange=\"if (this.value.length != 0) { if (1 == 1) { formclickmode=prg(-1,event.target); } else { document.getElementById('sbutwin').click(); }  } \"><option value=''>Select Windows explorer.exe command type(s) below ...</option><option value=' '>File Explorer</option><option value=' https://google.com'>Google</option><option value=' https://wikipedia.org'>Wikipedia</option><option value=' https://www.rjmprogramming.com.au'>RJM Programming</option></select><br><br><input id=sbutwin type=submit value='Execute'></input></form>";
  }
  
  if (isset($_GET['afteropen'])) {
    if (sizeof(explode('afteropen=', '?' . $_SERVER['QUERY_STRING'])) > 2) {
    for ($thisone=1; $thisone<sizeof(explode('afteropen=', '?' . $_SERVER['QUERY_STRING'])); $thisone++) {
    array_push($afteropens, str_replace('+',' ',urldecode(explode('#',explode('&',explode('afteropen=', $_SERVER['QUERY_STRING'])[$thisone])[0])[0])));
    }
    } else {
    $afteropen=(trim(str_replace('+',' ',urldecode($_GET['afteropen']))) == '' ? '' : ' ' . trim(str_replace('+',' ',urldecode($_GET['afteropen']))));
    $afteropens=[$afteropen];
    }
  } else if (isset($_POST['afteropen'])) {
    if (sizeof(explode('afteropen=', '?' . $_SERVER['QUERY_STRING'])) > 2) {
    for ($thisone=1; $thisone<sizeof(explode('afteropen=', '?' . $_SERVER['QUERY_STRING'])); $thisone++) {
    array_push($afteropens, str_replace('+',' ',urldecode(explode('#',explode('&',explode('afteropen=', $_SERVER['QUERY_STRING'])[$thisone])[0])[0])));
    }
    } else {
    $afteropen=(trim(str_replace('+',' ',urldecode($_POST['afteropen']))) == '' ? '' : ' ' . trim(str_replace('+',' ',urldecode($_POST['afteropen']))));
    $afteropens=[$afteropen];
    }
  } 
  
  for ($i=0; $i<sizeof($afteropens); $i++) {
  $afteropen=$afteropens[$i];
  if ($afteropen != '' || isset($_GET['afteropen']) || isset($_POST['afteropen'])) {
   if (PHP_OS == 'Darwin') {
    if (strpos($afteropen, ' open ') === false) {
    $preret.=$delim . 'open' . $afteropen;
    } else {
    $preret.=$delim . '' . $afteropen;
    }
    if (($i + 1) == sizeof($afteropens)) {
    $ret=shell_exec($preret);
    $preret.="\n";
    }
   } else if (substr(strtolower('' . PHP_OS),0,3) == 'win') {
    if (strpos($afteropen, ' explorer. ') === false) {
    $preret.=$delim . 'explorer.exe' . $afteropen . '';
    } else {
    $preret.=$delim . '' . $afteropen;
    }
    if (($i + 1) == sizeof($afteropens)) {
    if (1 == 1) {
    $fp = fopen(str_replace("/",$wds,$_SERVER['DOCUMENT_ROOT']) . $wds . 'open_extravaganza.bxt', 'x');
    fwrite($fp, str_replace(" ; ", "\n", $preret) . "\nerase " . str_replace("/",$wds,$_SERVER['DOCUMENT_ROOT']) . $wds . 'open_extravaganza.bat');
    fclose($fp);
    } else if (4 == 7) {
    $fp = fopen(str_replace("/",$wds,$_SERVER['DOCUMENT_ROOT']) . $wds . 'open_extravaganza.bat', 'x');
    fwrite($fp, str_replace(" ; ", "\n", $preret) . "\nerase " . str_replace("/",$wds,$_SERVER['DOCUMENT_ROOT']) . $wds . 'open_extravaganza.bat');
    fclose($fp);
    } else {
    file_put_contents(str_replace("/",$wds,$_SERVER['DOCUMENT_ROOT']) . $wds . 'open_extravaganza.bat', str_replace(" ; ", "\n", $preret) . "\nerase " . str_replace("/",$wds,$_SERVER['DOCUMENT_ROOT']) . $wds . 'open_extravaganza.bat');
    }
    try {
    if (file_exists(str_replace("/",$wds,$_SERVER['DOCUMENT_ROOT']) . $wds . 'open_extravaganza.bat')) {
    $ret=shell_exec(str_replace("/",$wds,$_SERVER['DOCUMENT_ROOT']) . $wds . 'open_extravaganza.bat');
    }
    } catch (Exception $e) {
    }  
  //passthru($preret);
//$descriptorspec = [STDIN, STDOUT, STDOUT];
//$cmd = $preret;
//$proc = proc_open($cmd, $descriptorspec, $pipes);
//$ret = fread($pipes[1], 1024);
//$ret .= fread($pipes[2], 1024);

//fclose($pipes[0]);
//fclose($pipes[1]);
//fclose($pipes[2]);

//proc_close($proc);
    //$ret=shell_exec($preret);
    //system($preret, $ret);
 //$file = popen($preret,"r");
 //some code to be executed
 //pclose($file);
   $preret.="\n";
    }
   }
  }
  $delim=' ; ';
  }
  
  $afteropens=[];
  $delim='';
  $thisone=1;

  if ($preret == '' && strpos($_SERVER['SERVER_NAME'], 'rjmprogramming.com.au') !== false) {
    $selstuff=str_replace('POST', 'GET', $selstuff);
    $preret=" <select title=\"Right click to go to Apache/PHP/MySql MAMP local web server webpage\" oncontextmenu=\"window.open('//www.mamp.info','_blank');\" id=opsel onchange=\"if (this.value.length > 0) { wo=window.open(this.value,'_blank','top=0,left=0,width=' + screen.width + ',height=' + screen.height);  }\"><option id=omamp value=''>MAMP</option><option id=omacos value='http://localhost:8888/open_extravaganza.php'>macOS</option><option id=owindows value='http://localhost/open_extravaganza.php'>Windows</option></select>";
  }
  
echo "<!doctype HTML>
<html>
<head>
<title>Local Operating System Menu</title>
<meta charset=\"UTF-8\">
<scri" . "pt type=text/javascript>
    var wo=null;
    var formclickmode=0;
    var lastselo=null;
    
    function fcm() {
      if (formclickmode == 0) {
        formclickmode=0;
      } else if (formclickmode < 0) {
        formclickmode--;
        if (formclickmode <= -10) {
          document.body.style.cursor='pointer';
          if (lastselo) { lastselo.style.cursor='pointer'; lastselo=null; }
          formclickmode=0;
          document.getElementById('sbutwin').click();
        }
      } else {
        formclickmode++;
        if (formclickmode >= 10) {
          document.body.style.cursor='pointer';
          if (lastselo) { lastselo.style.cursor='pointer'; lastselo=null; }
          formclickmode=0;
          document.getElementById('sbutmac').click();
        }
      }
    }
    
    function mostlikely() {
     if (document.getElementById('opsel')) {
      if (('' + navigator.platform).toLowerCase().indexOf('mac') != 0) {
        if (('' + navigator.platform).toLowerCase().indexOf('win') != -1) {
        if (document.getElementById('myformwin')) {
          document.getElementById('myformwin').style.display='block';
        }
        }
        document.getElementById('opsel').innerHTML=document.getElementById('omamp').outerHTML + document.getElementById('owindows').outerHTML + document.getElementById('omacos').outerHTML;
      } else {
        if (document.getElementById('myformmac')) {
          document.getElementById('myformmac').style.display='block';
        }
      }
     }
    }
    
    function prg(wht, selo) {
      if (selo) { lastselo=selo; lastselo.style.cursor='progress'; }
      document.body.style.cursor='progress';
      return wht;
    }
    
    setInterval(fcm, 1000);
</scri" . "pt>
<style>
 p {
   background-color: orange;
 }
</style>
</head>
<body onload=mostlikely();><h1>Local Operating System " . explode(' ',$preret)[0] . " Menu </h1><h2>RJM Programming</h2><h3>November, 2025</h3><h4>Thanks to <a target=bank tite='https://scriptingosx.com/2017/02/the-macos-open-command/' href='//scriptingosx.com/2017/02/the-macos-open-command/'>https://scriptingosx.com/2017/02/the-macos-open-command/</a></h4><br><p>" . str_replace("\n","<br>",$preret) . str_replace("\n","<br>",$ret) . "</p>" . $selstuff . "</body>
</html>";
?>