<?php
// macos_ffmpeg_convert.php
// Supervise macOS (via PHP exec) ffmpeg (command line) Audio/Video/Image conversions and ImageMagick for images

$thecontent="";
$tastuff="";
$doutput="";
$outfileis="";
$sufext="_0";
$iext=0;
$cself="";

$verb="ffmpeg";
$iverb="convert";  // ImageMagick
$switchone="-i";
$switchtwo="";
$onl=" if (!navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) { document.getElementById('thewords').focus(); } ";

$iswitchone="";

if (file_exists("/usr/local/bin/ffmpeg")) {
  $verb="/usr/local/bin/ffmpeg";
}
if (file_exists("/usr/local/bin/convert")) {
  $iverb="/usr/local/bin/convert";
}

$wasswitchone="";
$wasverb="";

$divinfront="";

$indx=0;
$iof=0;

$cvse="Convert";
$title="<title>Audio/Video/Image Conversions via ffmpeg and/or Image/PDF Conversions via ImageMagick</title>";
$avtypes="";

$types = ["audio/x-m4r","audio/wav","audio/x-wav","audio/x-pn-realaudio","audio/x-mpegurl","audio/x-aiff","audio/mpeg","audio/mid",
      "audio/basic","audio/ogg","video/x-sgi-movie","video/x-msvideo","video/quicktime","audio/mp3","video/mp4","video/mpeg",
      "video/x-la-asf","video/ogg","video/webm","audio/mp4", "image/jpeg", "image/jpeg", "image/png", "image/gif", "image/bmp", "image/tif",
      "text/html", "text/html", "text/html", "text/javascript", "text/css", "text/plain", "text/xml", "text/csv",
      "application/vnd.ms-word", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/x-php", "application/pdf", "IMAGE/pdf",
      "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.ms-powerpoint",
      "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"];
$exts = [".m4r",".wav",".wav",".ram",".m3u",".aiff",".mp3",".rmi",
      ".snd",".ogg",".movie",".avi",".mov",".mp3",".m4v",".mpeg",
      ".lsx",".ogv",".webm",".m4a", ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tif",
      ".htm", ".html", ".htmls", ".js", ".css", ".txt", ".xml", ".csv",
      ".doc", ".docx", ".php", ".pdf", ".pdf",
      ".pptx", ".ppt",
      ".xls", ".xlsx"];

$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$ipad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$blackberry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");


 if (isset($argv)) { // command line
  $numargs = sizeof($argv);
  if ($numargs <= 4) {
   if (PHP_OS == 'WINNT') {
    echo '$ Input path ( eg. we will take blank to mean ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . ' ) then Audio/Video/Image/PDF Input File(spec) then Output path ( eg. we will take blank to mean ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . ' ) then Audio/Video/Image/PDF Output File Extension below ...' . "\n";
    $line = stream_get_line(STDIN, 1024, PHP_EOL);
    if (str_replace("\n", "", $line) == "") { $line=dirname(__FILE__) . DIRECTORY_SEPARATOR; }
    $lineinspec = stream_get_line(STDIN, 1024, PHP_EOL);
    $outline = stream_get_line(STDIN, 1024, PHP_EOL);
    if (str_replace("\n", "", $outline) == "") { $outline=dirname(__FILE__) . DIRECTORY_SEPARATOR; }
    $outlineinspec = stream_get_line(STDIN, 1024, PHP_EOL);
    if (strpos(($outlineinspec . "~"), "htm~") !== false) {  $outlineinspec.="l";   }
   } else {
    $line = readline('$ Input path ( eg. we will take blank to mean ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . ' ) then Audio/Video/Image/PDF Input File(spec) then Output path ( eg. we will take blank to mean ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . ' ) then Audio/Video/Image/PDF Output File Extension below ...' . "\n");
    if (str_replace("\n", "", $line) == "") { $line=dirname(__FILE__) . DIRECTORY_SEPARATOR; }
    $lineinspec = readline('');
    $outline = readline('');
    if (str_replace("\n", "", $outline) == "") { $outline=dirname(__FILE__) . DIRECTORY_SEPARATOR; }
    $outlineinspec = readline('');
    if (strpos(($outlineinspec . "~"), "htm~") !== false) {  $outlineinspec.="l";   }
   }
  $cmdsuff="";
   $openers="";
   $openersdelim="";
   foreach (glob($line . $lineinspec) as $filename) {    
    $outnameis=$outline . str_replace("." . explode(".", basename($filename))[-1 + sizeof(explode(".", basename($filename)))], ".", basename($filename)) . str_replace(".", "", $outlineinspec);
  if (file_exists($outnameis)) {
  $sufext="_0";
  $iext=0;
  $rone="";
  $rtwo="";
    while (file_exists($outnameis . $sufext)) {
      $iext++;
      $sufext="_" . $iext;
    }
    $rtwo=$outnameis;
    $rone=$outnameis . $sufext;
    echo "Renaming " . $outnameis . " to " . $outnameis . $sufext . "\n";
    try {
    rename($outnameis, $outnameis . $sufext);
    } catch (Exception $e) {
    echo $e->getMessage() . "\n";
    }
    if (!file_exists($outnameis . $sufext)) {
    echo "Renaming did not happen." . "\n";
    }
  }
    if (explode(".",$outlineinspec)[-1 + sizeof(explode(".",$outlineinspec))] == "pdJUNKf") {
    echo ("php " . $argv[0] . " " . $line . " " . basename($filename) . " " . $outline . " " . $outlineinspec . "\n");
    passthru("php " . $argv[0] . " " . $line . " " . basename($filename) . " " . $outline . " " . $outlineinspec);
    } else {
    $mimetypeis="";
    $videoin=false;
    for ($jia=0; $jia<sizeof($exts); $jia++) {
      if (strtolower("." . explode(".",$filename)[-1 + sizeof(explode(".",$filename))]) == strtolower($exts[$jia])) {
        if (strpos($types[$jia], "video/") !== false) { $videoin=true;
        if (PHP_OS == "Darwin") { $cmdsuff=" "; } }
      }
    }
    for ($jia=0; $jia<sizeof($exts); $jia++) {
      if (strtolower("." . explode(".",$outnameis)[-1 + sizeof(explode(".",$outnameis))]) == strtolower($exts[$jia])) {
        $mimetypeis=$types[$jia];
        if (strpos(strtolower($mimetypeis), "image/") !== false && $cmdsuff != "") {
          $cmdsuff.="; open -a Safari " . $outnameis . " ";
        }
        if ((strpos(strtolower($filename), ".pdf") !== false || strpos(strtolower($mimetypeis), "image/") !== false) && !$videoin) {
          if ($wasverb == "") {
            $wasverb=$verb;
            $wasswitchone=$switchone;
          }
          $verb=$iverb;
          if (strpos(strtolower($filename), ".pdf") !== false) {
          $switchone=" -delay 200 ";
          } else {
          $switchone=$iswitchone;
          }
        } else if ($wasverb != "") {
          $verb=$wasverb;
          $switchone=$wasswitchone;
        }
      }
    }
    //echo ($verb . " " . $switchone . " " . $filename . " " . $switchtwo . " " . $outnameis . $cmdsuff . " > macos_ffmpeg_convert.out 2> macos_ffmpeg_convert.err");
    echo ($verb . " " . $switchone . " " . $filename . " " . $switchtwo . " " . $outnameis . $cmdsuff . " > macos_ffmpeg_convert.out 2> macos_ffmpeg_convert.err \n");
    exec($verb . " " . $switchone . " " . $filename . " " . $switchtwo . " " . $outnameis . $cmdsuff . " > macos_ffmpeg_convert.out 2> macos_ffmpeg_convert.err; cat macos_ffmpeg_convert.err");
    //passthru($verb . " " . $switchone . " " . $filename . " " . $switchtwo . " " . $outnameis . $cmdsuff . " > macos_ffmpeg_convert.out 2> macos_ffmpeg_convert.err");
  if (file_exists("macos_ffmpeg_convert.out") && file_exists("macos_ffmpeg_convert.err")) {
  if (7 == 6 && filesize("macos_ffmpeg_convert.out") == 0 && filesize("macos_ffmpeg_convert.err") != 0) {
     if ($rone != "" && $rtwo != "") {
    echo "Renaming " . $rone . " back to " . $rtwo . "\n";
      rename($rone, $rtwo);
     }
     echo "\n\n" . file_get_contents("macos_ffmpeg_convert.err") . "\n\n";
  }
  }
    if (PHP_OS == "Darwin") { // && explode(".",$outlineinspec)[-1 + sizeof(explode(".",$outlineinspec))] != "pdf") {
       if (file_exists($outnameis)) {
       $openers.=$openersdelim . "open " . $outnameis;
       $openersdelim=" ; ";
       }
    }
    }
   }
   if (PHP_OS == "Darwin" && $openers != "") {
    echo "\n\nWill try to open successful (or old resurrected) files in 8 seconds ...\n"; 
    sleep(8);
    echo ($openers . " \n");
    exec($openers);
   }
   //echo "do the work";
  } else {
   $openers="";
   $openersdelim="";
   $line=trim($argv[1]);
   if (str_replace("\n", "", $line) == "") { $line=dirname(__FILE__) . DIRECTORY_SEPARATOR; }
   $lineinspec=$argv[2];
   $outline=trim($argv[3]);
   if (str_replace("\n", "", $outline) == "") { $outline=dirname(__FILE__) . DIRECTORY_SEPARATOR; }
   $outlineinspec=$argv[4];
  $cmdsuff="";
   foreach (glob($line . $lineinspec) as $filename) {   
     $outnameis=$outline . str_replace("." . explode(".", basename($filename))[-1 + sizeof(explode(".", basename($filename)))], ".", basename($filename)) . str_replace(".", "", $outlineinspec);
  if (file_exists($outnameis)) {
  $sufext="_0";
  $iext=0;
  $rone="";
  $rtwo="";
    while (file_exists($outnameis . $sufext)) {
      $iext++;
      $sufext="_" . $iext;
    }
    $rtwo=$outnameis;
    $rone=$outnameis . $sufext;
    echo "Renaming " . $outnameis . " to " . $outnameis . $sufext . "\n";
    rename($outnameis, $outnameis . $sufext);
  }

    $mimetypeis="";
    $videoin=false;
    for ($jia=0; $jia<sizeof($exts); $jia++) {
      if (strtolower("." . explode(".",$filename)[-1 + sizeof(explode(".",$filename))]) == strtolower($exts[$jia])) {
        if (strpos($types[$jia], "video/") !== false) { $videoin=true;
        if (PHP_OS == "Darwin") { $cmdsuff=" "; } }
      }
    }
    for ($jia=0; $jia<sizeof($exts); $jia++) {
      if (strtolower("." . explode(".",$outnameis)[-1 + sizeof(explode(".",$outnameis))]) == strtolower($exts[$jia])) {
        $mimetypeis=$types[$jia];
        if (strpos(strtolower($mimetypeis), "image/") !== false && $cmdsuff != "") {
          $cmdsuff.="; open -a Safari " . $outnameis . " ";
        }
        if ((strpos(strtolower($filename), ".pdf") !== false || strpos(strtolower($mimetypeis), "image/") !== false) && !$videoin) {
          if ($wasverb == "") {
            $wasverb=$verb;
            $wasswitchone=$switchone;
          }
          $verb=$iverb;
          if (strpos(strtolower($filename), ".pdf") !== false) {
          $switchone=" -delay 200 ";
          } else {
          $switchone=$iswitchone;
          }
        } else if ($wasverb != "") {
          $verb=$wasverb;
          $switchone=$wasswitchone;
        }
      }
    }

    //echo ($verb . " " . $switchone . " " . $filename . " " . $switchtwo . " " . $outnameis . $cmdsuff . " > macos_ffmpeg_convert.out 2> macos_ffmpeg_convert.err");
    echo ($verb . " " . $switchone . " " . $filename . " " . $switchtwo . " " . $outnameis . $cmdsuff . " > macos_ffmpeg_convert.out 2> macos_ffmpeg_convert.err \n");
    exec($verb . " " . $switchone . " " . $filename . " " . $switchtwo . " " . $outnameis . $cmdsuff . " > macos_ffmpeg_convert.out 2> macos_ffmpeg_convert.err; cat macos_ffmpeg_convert.err");
    //passthru($verb . " " . $switchone . " " . $filename . " " . $switchtwo . " " . $outnameis . $cmdsuff . " > macos_ffmpeg_convert.out 2> macos_ffmpeg_convert.err");
  if (file_exists("macos_ffmpeg_convert.out") && file_exists("macos_ffmpeg_convert.err")) {
  if (8 == 9 && filesize("macos_ffmpeg_convert.out") == 0 && filesize("macos_ffmpeg_convert.err") != 0) {
     if ($rone != "" && $rtwo != "") {
    echo "Renaming " . $rone . " back to " . $rtwo . "\n";
      rename($rone, $rtwo);
     }
     echo "\n\n" . file_get_contents("macos_ffmpeg_convert.err") . "\n\n";
  }
  }
    if (PHP_OS == "Darwin") { // && explode(".",$outlineinspec)[-1 + sizeof(explode(".",$outlineinspec))] != "pdf") {
       if (file_exists($outnameis)) {
       $openers.=$openersdelim . "open " . $outnameis;
       $openersdelim=" ; ";
       }
    }
   }
   if (PHP_OS == "Darwin" && $openers != "") {
    echo "\n\nWill try to open successful (or old resurrected) files in 8 seconds ...\n"; 
    sleep(8);
    echo ($openers . " \n");
    exec($openers);
   }
   //echo "Do the work ";
  }
  exit;
 }
 
 if (strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) { 
   if ($iphone || $ipad || $android || $ipod || $blackberry) { // thanks to http://www.phpmysqlscript.com/2012/03/detecting-users-screen-size-and.html
   $cself="&nbsp;&#129331;&nbsp;";
   $cvse="Email &#128231;";
   $divinfront="<button onmouseover='tsuper();' style='width:100%;height:100px;background-color:#ffcf9e;' onclick='return ddforb(event);' id=dbwithlf>Email &#128231; Audio/Video/Image/PDF(s)<div id=dbafterfirstline<br><br><div style='border:1px dashed darkorange;' id=bdiv onmouseover='domo(event);' onmouseout='domout(event);' onmousedown='ionclick(event);' onclick='return ionclick(event);' onfocus='onf(event);' contenteditable=true onblur='donblur(this);' title='Click in to change email address as required'></div></div></button>";
   } else {
   $cvse="Email";
   $divinfront="<button onmouseover='tsuper();' style='width:100%;height:100px;background-color:#ffcf9e;' onclick='return ddforb(event);' id=dbwithlf>Email Audio/Video/Image/PDF(s)<div id=dbafterfirstline<br><br><div style='border:1px dashed darkorange;' id=bdiv onmouseover='domo(event);' onmouseout='domout(event);' onmousedown='ionclick(event);' onclick='return ionclick(event);' onfocus='onf(event);' contenteditable=true onblur='donblur(this);' title='Click in to change email address as required'></div></div></button>";
   }
   $title="<title>Email off your Media Conversion Attachment Input Files for Your Inhouse Media Converter to Listen For and Convert</title>";
   $onl="";
 }
 
 if (isset($_POST['to']) && isset($_POST['subject']) && isset($_POST['body'])) {
   $tow=str_replace("+"," ",urldecode($_POST['to']));
   if (strpos($tow, "@") !== false) {
   $subjw=str_replace('*','m4v',str_replace("+"," ",urldecode($_POST['subject'])));
   $orign=basename(explode(" ", $subjw)[0]);
   $thenn=basename(explode(" ", $subjw)[-1 + sizeof(explode(" ", $subjw))]);
   $bodyn=str_replace(" ","+",urldecode($_POST['body']));
   if (strpos($bodyn, "data:") !== false) {
     file_put_contents($thenn, base64_decode(explode(";base64,", $bodyn)[1]));
   } else {
     file_put_contents($thenn, $bodyn);
   }
   if (1 == 1) { // thanks to https://community.hpe.com/t5/general/use-of-sendmail-and-uuencode/td-p/2431436#.XxqPtvgzZZo
   //$cmd="/usr/sbin/sendmail " . $tow . " << END\nSubject: " . $subjw . "\n`uuencode " . $thenn . " " . $thenn . "`\nEND";
   //$cmd="/usr/sbin/sendmail " . $tow . " << END\nSubject: " . $subjw . "\nFrom: rjmprogr@vs-rmetcalfe.au.syrahost.com\n`uuencode " . $thenn . " " . $thenn . "`\nEND";
   $cmd="/usr/sbin/sendmail " . $tow . " << END\nSubject: " . $subjw . "\n`uuencode " . $thenn . " " . $thenn . "`\nEND";
   //file_put_contents("00x.00x", $cmd);
   exec($cmd);
   exec("rm -f " . $thenn);
   } else {
   //file_put_contents("0x.0x", "uuencode " . $thenn . " " . $thenn . " > " . explode(".", $thenn)[0] . "; cat " . explode(".", $thenn)[0] . " | /usr/sbin/sendmail " . $tow . "; rm -f " . explode(".", $thenn)[0] . "; rm -f " . $thenn);
   //exec("uuencode " . $thenn . " " . $thenn . " > " . explode(".", $thenn)[0] . "; cat " . explode(".", $thenn)[0] . " | /usr/sbin/sendmail " . $tow . " > smail.out 2> smail.err; rm -f " . explode(".", $thenn)[0] . "; rm -f " . $thenn);
   exec("uuencode " . $thenn . " " . $thenn . " > " . explode(".", $thenn)[0] . "; cat " . explode(".", $thenn)[0] . " | /usr/sbin/sendmail " . $tow . " > smail.out 2> smail.err; rm -f " . explode(".", $thenn)[0] . "; rm -f " . $thenn);
   }
   }
   exit;
 }
 
 if (isset($_GET['globit'])) {
   $accf="";
   $accfd="";
   $accfout="";
   $accfoutext=".m4v";
   $pprefix=dirname(__FILE__) . DIRECTORY_SEPARATOR;
   if (isset($_GET['globpath'])) {
     if (str_replace("+", " ", urldecode($_GET['globpath'])) != "") {
     $pprefix=str_replace("+", " ", urldecode($_GET['globpath']));
     }
   }
   if (isset($_GET['globext'])) {
     $accfoutext="." . str_replace(".", "", str_replace("+", " ", urldecode($_GET['globext'])));
   }
   foreach (glob($pprefix . str_replace("+", " ", urldecode($_GET['globit']))) as $filename) {  
       $accf.=$accfd . basename($filename);
       $accfout.=$accfd . explode(".", basename($filename))[0] . $accfoutext;
       $accfd=",";
   }
   echo "<html><head>" . $title . "</head><body onload=\" parent.document.getElementById('thewords').value='" . $accf . "';  parent.document.getElementById('outputname').value='" . $accfout . "'; \"></body></html>";
   exit;
 }
 
for ($iav=0; $iav<sizeof($types); $iav++) {
  if (strpos($types[$iav], "audio/") !== false || strpos($types[$iav], "video/") !== false || strpos($types[$iav], "image/") !== false) {
    if (strpos(($types[$iav] . "~"), str_replace(".", "/", $exts[$iav]) . "~") !== false || 1 == 1) {
      $avtypes.="<option value=" . str_replace("gif","gif id=videoag",str_replace(".","",$exts[$iav])) . ">Convert to " . strtoupper(substr($types[$iav],0,1)) . str_replace("/"," ",substr($types[$iav],1)) . "</option>";
    }
  }
}

function nosourcei($inh) {
  global $verb;
  if (strpos(strtolower($inh), "<image ") != false) { 
    if (strpos(strtolower($inh), "/pdf") !== false) {
    //file_put_contents("x.x", str_replace("ffmpeg","imagemagick",str_replace("<image ","<object ",str_replace("</image>","</object>",str_replace("></source>","",str_replace(" controls><source type="," type=application/pdf data-type=",str_replace("</image>","</object>",  str_replace(" src="," data=", str_replace("image/pdf","application/pdf", str_replace("IMAGE","image", $inh))))))))));
    return str_replace("ffmpeg","imagemagick",str_replace("<image ","<object style=width:100%;height:100%; ",str_replace("</image>","</object>",str_replace("></source>","",str_replace(" controls><source type="," type=application/pdf data-type=",str_replace("</image>","</object>",  str_replace("image/pdf","application/pdf", str_replace(" src="," data=", str_replace("IMAGE","image", $inh)))))))));
    } else {
    $vfrom="ffmpeg";
    if (strpos($verb, "ffmpeg") !== false) {  $vfrom="youwillneverfindthis";  }
    //file_put_contents("xx.xx",str_replace("ffmpeg","imagemagick",str_replace("<image ","<img ",str_replace("</image>","</img>",str_replace("></source>","",str_replace(" controls><source type="," data-type=",str_replace("</image>","</img>",$inh)))))));
    return str_replace($vfrom,"imagemagick",str_replace("<image ","<img ",str_replace("></source>","",str_replace(" controls><source type="," data-type=",str_replace("</image>","</img>",$inh)))));
    }
  }
  return $inh;
}

function ourstr_replace($fris, $tois, $stris) {
  global $indx;
  if (strpos($stris, ",") !== false || ($indx == 0 && ($_GET['inpath'] || $_POST['inpath']))) {
    $interim=str_replace($fris, $tois, explode(",",$stris)[$indx]);
    if (isset($_GET['inpath'])) {
      return str_replace("+"," ",urldecode($_GET['inpath'])) . $interim;
    } else if (isset($_POST['inpath'])) {
      return str_replace("+"," ",urldecode($_POST['inpath'])) . $interim;
    } 
    return $interim;
  } 
  return str_replace($fris, $tois, $stris);
}

function yourstr_replace($fris, $tois, $stris) {
  global $indx;
  if (strpos($stris, ",") !== false || ($indx == 0 && ($_GET['outpath'] || $_POST['outpath']))) {
    $interim=str_replace($fris, $tois, explode(",",$stris)[$indx]);
    if (isset($_GET['outpath'])) {
      return str_replace("+"," ",urldecode($_GET['outpath'])) . $interim;
    } else if (isset($_POST['outpath'])) {
      return str_replace("+"," ",urldecode($_POST['outpath'])) . $interim;
    } 
    return $interim;
  } 
  return str_replace($fris, $tois, $stris);
}

if (isset($_GET['vsfs']) && isset($_GET['vsfn']) && isset($_GET['vsfi']) && isset($_GET['vsfu'])) {
  $docroot=dirname(__FILE__);
  if (file_exists("lookup_" . str_replace(")","",str_replace("(","",str_replace("+"," ",urldecode($_GET['vsfn'])))))) {
    unlink("lookup_" . str_replace(")","",str_replace("(","",str_replace("+"," ",urldecode($_GET['vsfn'])))));
  }
  exec("find " . $_SERVER['DOCUMENT_ROOT'] . " -name '" . str_replace(")","*",str_replace("(","*",str_replace("+"," ",urldecode($_GET['vsfn'])))) . "' -exec ls -l {} \\; 2> /dev/null > lookup_" . str_replace(")","",str_replace("(","",str_replace("+"," ",urldecode($_GET['vsfn'])))));
  if (file_exists("lookup_" . str_replace(")","",str_replace("(","",str_replace("+"," ",urldecode($_GET['vsfn'])))))) {
    $infois=file_get_contents(("lookup_" . str_replace(")","",str_replace("(","",str_replace("+"," ",urldecode($_GET['vsfn']))))));
    $lines=explode("\n", $infois);
    for ($il=0; $il<sizeof($lines); $il++) {
      $lines[$il]=str_replace("\r","",$lines[$il]);
      if (strpos($lines[$il], " " . $_GET['vsfs'] . " ") !== false) {
        //file_put_contents("look_" . str_replace(")","",str_replace("(","",str_replace("+"," ",urldecode($_GET['vsfn'])))), "thisplace=" . $docroot . "\n" . "rootplace=" . $_SERVER['DOCUMENT_ROOT'] . "\n" . "origplace=" . str_replace("+"," ",urldecode($_GET['vsfu'])) . "\n" . "foundplace=" . explode(" ", $lines[$il])[-1 + sizeof( explode(" ", $lines[$il]))] . "\n" . '<html><body onload=" parent.document.getElementById(' . "'" . str_replace("+"," ",urldecode($_GET['vsfi'])) . "'" . ").value='" . str_replace($_SERVER['DOCUMENT_ROOT'],explode("localhost",str_replace("+"," ",urldecode($_GET['vsfu'])))[0] . "localhost" . explode("/",explode("localhost",str_replace("+"," ",urldecode($_GET['vsfu'])))[1])[0] . "",explode(" ", $lines[$il])[-1 + sizeof( explode(" ", $lines[$il]))]) . "'; " . '"></body></html>');
        if (strpos(explode(" ", $lines[$il])[-1 + sizeof( explode(" ", $lines[$il]))], $_SERVER['DOCUMENT_ROOT']) !== false) {
          //file_put_contents("x7.x7",'<html><body onload=" if (parent.document.getElementById(' . "'" . 'eangle' . "'" . ')) { parent.document.getElementById(' . "'" . 'eangle' . "'" . ').value=' . "'" . $ort . "'" . ';  } top.document.getElementById(' . "'" . str_replace("+"," ",urldecode($_GET['vsfi'])) . "'" . ").value='" . $ort . "'; " . '"></body></html>');
          if (isset($_GET['turl'])) {      
          echo "<html><body onload=\" var tp='" . str_replace("junk" . $_SERVER['DOCUMENT_ROOT'],"juJUNKnk" . explode("localhost",str_replace("+"," ",urldecode($_GET['vsfu'])))[0] . "localhost" . explode("/",explode("localhost",str_replace("+"," ",urldecode($_GET['vsfu'])))[1])[0] . "",explode(" ", $lines[$il])[-1 + sizeof( explode(" ", $lines[$il]))]) . "'; " . "  var tt='" . str_replace("+"," ",urldecode($_GET['vsfn'])) . "'; "  . ' var ct=top.document.getElementById(' . "'" . 'cto' . "'" . ').value; ct=ct.replace(' . "'" . '*' . "'" . ',' . "'" . 'm4v' . "'" . '); top.document.getElementById(' . "'" . 'sin' . "'" . ').style.display=' . "'" . 'inline-block' . "'" . '; top.document.getElementById(' . "'" . 'path' . "'" . ").value=tp.replace(tt, ''); " . ' top.document.getElementById(' . "'" . 'opath' . "'" . ").value=tp.replace(tt, ''); " . '  top.selch(ct);   "></body></html>'; 
          } //else { 
          //echo '<html><body onload=" if (parent.document.getElementById(' . "'" . 'eangle' . "'" . ')) { parent.document.getElementById(' . "'" . 'eangle' . "'" . ').value=' . "'" . $ort . "'" . ';  } top.document.getElementById(' . "'" . str_replace("+"," ",urldecode($_GET['vsfi'])) . "'" . ").value='" . $ort . "'; " . '"></body></html>'; 
          //}
        } //else if (isset($_GET['turl'])) {      
          //echo '<html><body onload=" top.document.getElementById(' . "'" . 'turl' . "'" . ").placeholder='" . str_replace($_SERVER['DOCUMENT_ROOT'],explode("localhost",str_replace("+"," ",urldecode($_GET['vsfu'])))[0] . "localhost" . explode("/",explode("localhost",str_replace("+"," ",urldecode($_GET['vsfu'])))[1])[0] . "",explode(" ", $lines[$il])[-1 + sizeof( explode(" ", $lines[$il]))]) . "'; " . '  top.document.getElementById(' . "'" . 'turl' . "'" . ').title=top.document.getElementById(' . "'" . 'turl' . "'" . ').placeholder;   top.document.getElementById(' . "'" . 'thewords' . "'" . ').title=top.document.getElementById(' . "'" . 'thewords' . "'" . ').title.replace( top.document.getElementById(' . "'" . 'turl' . "'" . ').value, top.document.getElementById(' . "'" . 'turl' . "'" . ').placeholder);   top.document.getElementById(' . "'" . 'thewords' . "'" . ').value=top.document.getElementById(' . "'" . 'thewords' . "'" . ').value.replace( top.document.getElementById(' . "'" . 'turl' . "'" . ').value, top.document.getElementById(' . "'" . 'turl' . "'" . ').placeholder);   top.document.getElementById(' . "'" . 'turl' . "'" . ').value=top.document.getElementById(' . "'" . 'turl' . "'" . ').placeholder;   top.document.getElementById(' . "'" . 'thewords' . "'" . ').value=top.document.getElementById(' . "'" . 'thewords' . "'" . ').value.replace( top.document.getElementById(' . "'" . 'turl' . "'" . ').value, top.document.getElementById(' . "'" . 'turl' . "'" . ').placeholder);  "></body></html>'; 
        //} else { 
        //  echo '<html><body onload=" parent.document.getElementById(' . "'" . str_replace("+"," ",urldecode($_GET['vsfi'])) . "'" . ").value='" . str_replace($_SERVER['DOCUMENT_ROOT'],explode("localhost",str_replace("+"," ",urldecode($_GET['vsfu'])))[0] . "localhost" . explode("/",explode("localhost",str_replace("+"," ",urldecode($_GET['vsfu'])))[1])[0] . "",explode(" ", $lines[$il])[-1 + sizeof( explode(" ", $lines[$il]))]) . "'; " . '"></body></html>'; 
        //}
        unlink("lookup_" . str_replace(")","",str_replace("(","",str_replace("+"," ",urldecode($_GET['vsfn']))))); 
      } else {
        unlink("lookup_" . str_replace(")","",str_replace("(","",str_replace("+"," ",urldecode($_GET['vsfn']))))); 
      }
    }
  } 
  exit;
}



//if (PHP_OS == "Darwin" || $verb != "textutil" || isset($_GET['emailanyway'])) {
if (PHP_OS == "Darwin" || isset($_GET['emailanyway']) || $iphone || $ipad || $android || $ipod || $blackberry) {
if (isset($_GET['convertthis']) && isset($_GET['outputname'])) {
  if ($_GET['convertthis'] != "") {
  $thewords=str_replace("+"," ",urldecode($_GET['convertthis']));
  $thegoes=explode(",", $thewords);
  for ($indx=0; $indx<sizeof($thegoes); $indx++) {
  if (file_exists(yourstr_replace("+"," ",urldecode($_GET['outputname'])))) {
    while (file_exists(yourstr_replace("+"," ",urldecode($_GET['outputname'])) . $sufext)) {
      $iext++;
      $sufext="_" . $iext;
    }
    exec("rename " . yourstr_replace("+"," ",urldecode($_GET['outputname'])) . " " . yourstr_replace("+"," ",urldecode($_GET['outputname'])) . $sufext);
  }
  // pandoc -t pdf scjob.docx -o scjob.pdf  --pdf-engine=/Library/TeX/Root/bin/x86_64-darwin/pdflatex
  $cmdsuff="";
    $mimetypeis="";
    $videoin=false;
    for ($jia=0; $jia<sizeof($exts); $jia++) {
      if (strtolower("." . explode(".",ourstr_replace("+"," ",$thewords))[-1 + sizeof(explode(".",ourstr_replace("+"," ",$thewords)))]) == strtolower($exts[$jia])) {
        if (strpos($types[$jia], "video/") !== false) { $videoin=true;
        if (PHP_OS == "Darwin") { $cmdsuff=" "; } }
      }
    }
    for ($jia=0; $jia<sizeof($exts); $jia++) {
      if (strtolower("." . explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname'])))[-1 + sizeof(explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname']))))]) == strtolower($exts[$jia])) {
        $mimetypeis=$types[$jia];
        if (strpos(strtolower($mimetypeis), "image/") !== false && $cmdsuff != "") {
          $cmdsuff.="; open -a Safari " . yourstr_replace("+"," ",urldecode($_GET['outputname'])) . " ";
        }
        if ((strpos(strtolower(ourstr_replace("+"," ",$thewords)), ".pdf") !== false || strpos(strtolower($mimetypeis), "image/") !== false) && !$videoin) {
          if ($wasverb == "") {
            $wasverb=$verb;
            $wasswitchone=$switchone;
          }
          $verb=$iverb;
          if (strpos(strtolower(ourstr_replace("+"," ",$thewords)), ".pdf") !== false) {
          $switchone=" -delay 200 ";
          } else {
          $switchone=$iswitchone;
          }
        } else if ($wasverb != "") {
          $verb=$wasverb;
          $switchone=$wasswitchone;
        }
      }
    }
  //echo ($verb . " " . $switchone . " " . ourstr_replace("+"," ",$thewords) . " " . $filename . " " . $switchtwo . " " . $outnameis . $cmdsuff . " > macos_ffmpeg_convert.out 2> macos_ffmpeg_convert.err \n");
  exec($verb . " " . $switchone . " " . ourstr_replace("+"," ",$thewords) . " " . $switchtwo . " " . yourstr_replace("+"," ",urldecode($_GET['outputname'])) . $cmdsuff . " > macos_ffmpeg_convert.out 2> macos_ffmpeg_convert.err; cat macos_ffmpeg_convert.err");
  //passthru($verb . " " . $switchone . " " . ourstr_replace("+"," ",$thewords)  . " " . $switchtwo . " " . yourstr_replace("+"," ",urldecode($_GET['outputname'])) . $cmdsuff . " > macos_ffmpeg_convert.out 2> macos_ffmpeg_convert.err");


    $thecontent="";
    if ($mimetypeis != "") {
    $thecontent=nosourcei("<html><head><title>" . ourstr_replace("+"," ",$thewords) . " to " . yourstr_replace("+"," ",urldecode($_GET['outputname'])) . " via ffmpeg ...</title></head><body><h1>" . ourstr_replace("+"," ",$thewords) . " to " . yourstr_replace("+"," ",urldecode($_GET['outputname'])) . " via <a target=_blank title='ffmpeg Audio/Video/Image Conversion' href='https://ffmpeg.org/'>ffmpeg</a> ...</h1><" . explode("/",$mimetypeis)[0] . " controls><source type=" . $mimetypeis . " src='data:" . $mimetypeis . ";base64," . base64_encode(file_get_contents(yourstr_replace("+"," ",urldecode($_GET['outputname'])))) . "'></source></" . explode("/",$mimetypeis)[0] . "></body></html>");
    }

    if ($thecontent != "") {
      echo $thecontent;
      exit;
    }




  if (7 == 7 || explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname'])))[-1 + sizeof(explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname']))))] == "pdf") {
        $PDFfilename=yourstr_replace("+"," ",urldecode($_GET['outputname']));
        $pdf = file_get_contents($PDFfilename);

        header('Content-Type: application/pdf');
        header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
        header('Pragma: public');
        header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Content-Length: ' . strlen($pdf));
        header('Content-Disposition: inline; filename="' . basename($PDFfilename) .'";');
        ob_clean(); 
        flush(); 
        echo $pdf;  
        //exit; 
  } else if (explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname'])))[-1 + sizeof(explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname']))))] == "rtf") {
        $PDFfilename=yourstr_replace("+"," ",urldecode($_GET['outputname']));
        $pdf = file_get_contents($PDFfilename);

        header('Content-Type: application/rtf');
        header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
        header('Pragma: public');
        header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Content-Length: ' . strlen($pdf));
        header('Content-Disposition: inline; filename="' . basename($PDFfilename) .'";');
        ob_clean(); 
        flush(); 
        echo $pdf;  
        //exit; 
  } else if (explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname'])))[-1 + sizeof(explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname']))))] == "odt") {
        $PDFfilename=yourstr_replace("+"," ",urldecode($_GET['outputname']));
        $pdf = file_get_contents($PDFfilename);

        header('Content-Type: application/odt');
        header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
        header('Pragma: public');
        header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Content-Length: ' . strlen($pdf));
        header('Content-Disposition: inline; filename="' . basename($PDFfilename) .'";');
        ob_clean(); 
        flush(); 
        echo $pdf;  
        //exit; 
  }
  if (file_exists("macos_ffmpeg_convert.out") && file_exists("macos_ffmpeg_convert.err")) {
  if (filesize("macos_ffmpeg_convert.out") == 0 && filesize("macos_ffmpeg_convert.err") != 0) {
     if (6 == 7 && $rone != "" && $rtwo != "") {
      rename($rone, $rtwo);
     }
    //if (explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname'])))[-1 + sizeof(explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname']))))] == 'html') {
    //echo file_get_contents(ourstr_replace("+"," ",$thewords) . " " . $switchtwo . " " . yourstr_replace("+"," ",urldecode($_GET['outputname'])));
    //} else {
    if (file_exists(yourstr_replace("+"," ",urldecode($_GET['outputname']))) && explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname'])))[-1 + sizeof(explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname']))))] == 'html') {
    echo file_get_contents(yourstr_replace("+"," ",urldecode($_GET['outputname'])));
    } else {
    echo "<html><body onload=\"if (window.opener) { window.opener.document.getElementById('dothework').style.height='400px';    window.opener.document.getElementById('dothework').style.display='block';  } else if (window.parent) { parent.document.getElementById('dothework').style.height='400px';    parent.document.getElementById('dothework').style.display='block'; } \"><p>" . file_get_contents("macos_ffmpeg_convert.err") . "</p></body></html>";
    }
  } else {
    if (explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname'])))[-1 + sizeof(explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname']))))] == 'html') {
    echo file_get_contents(yourstr_replace("+"," ",urldecode($_GET['outputname'])));
    } else {
    echo explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname'])))[-1 + sizeof(explode(".",yourstr_replace("+"," ",urldecode($_GET['outputname']))))];
    }
  }
  }
  }
  exit;
  }
} else if (isset($_POST['convertthis']) && isset($_POST['outputname'])) {
  if ($_POST['convertthis'] != "") {
  $thewords=str_replace("+"," ",urldecode($_POST['convertthis']));
  $thegoes=explode(",", $thewords);
    //file_put_contents("xxxx.xxxx", $_POST['convertthis'] . " ... " . $_POST['outputname']);
  for ($indx=0; $indx<sizeof($thegoes); $indx++) {
    
  $rone="";
  $rtwo="";  
  
  if (file_exists(yourstr_replace("+"," ",urldecode($_POST['outputname'])))) {
    $sufext="_0";
    $iext=0;
    while (file_exists(yourstr_replace("+"," ",urldecode($_POST['outputname'])) . $sufext)) {
      $iext++;
      $sufext="_" . $iext;
    }
    $rtwo=yourstr_replace("+"," ",urldecode($_POST['outputname']));
    $rone=yourstr_replace("+"," ",urldecode($_POST['outputname'])) . $sufext;
    rename(yourstr_replace("+"," ",urldecode($_POST['outputname'])), yourstr_replace("+"," ",urldecode($_POST['outputname'])) . $sufext);
  }
  // pandoc -t pdf scjob.docx -o scjob.pdf
  $cmdsuff="";
  
    $mimetypeis="";
    $videoin=false;
    for ($jia=0; $jia<sizeof($exts); $jia++) {
      if (strtolower("." . explode(".",ourstr_replace("+"," ",$thewords))[-1 + sizeof(explode(".",ourstr_replace("+"," ",$thewords)))]) == strtolower($exts[$jia])) {
        if (strpos($types[$jia], "video/") !== false) { $videoin=true;
        if (PHP_OS == "Darwin") { $cmdsuff=" "; } }
      }
    }
    for ($jia=0; $jia<sizeof($exts); $jia++) {
      if (strtolower("." . explode(".",yourstr_replace("+"," ",urldecode($_POST['outputname'])))[-1 + sizeof(explode(".",yourstr_replace("+"," ",urldecode($_POST['outputname']))))]) == strtolower($exts[$jia])) {
        $mimetypeis=$types[$jia];
    //file_put_contents("xxxx.xxxx", file_get_contents("xxxx.xxxx") . "\n" . $mimetypeis . " wasverb=" . $wasverb);
        if (strpos(strtolower($mimetypeis), "image/") !== false && $cmdsuff != "") {
          $cmdsuff.="; open -a Safari " . yourstr_replace("+"," ",urldecode($_POST['outputname'])) . " ";
        }
        if ((strpos(strtolower(ourstr_replace("+"," ",$thewords)), ".pdf") !== false || strpos(strtolower($mimetypeis), "image/") !== false) && !$videoin) {
    //file_put_contents("xxxx.xxxx", file_get_contents("xxxx.xxxx") . "\n" . "1");
          if ($wasverb == "") {
            $wasverb=$verb;
            $wasswitchone=$switchone;
          }
          $verb=$iverb;
          if (strpos(strtolower(ourstr_replace("+"," ",$thewords)), ".pdf") !== false) {
          $switchone=" -delay 200 ";
          } else {
          $switchone=$iswitchone;
          }
        } else if ($wasverb != "") {
    //file_put_contents("xxxx.xxxx", file_get_contents("xxxx.xxxx") . "\n" . "2");
          $verb=$wasverb;
          $switchone=$wasswitchone;
        } else {
    //file_put_contents("xxxx.xxxx", file_get_contents("xxxx.xxxx") . "\n" . "3");
        }
      }
    }

  exec($verb . " " . $switchone . " " . ourstr_replace("+"," ",$thewords) . " " . $switchtwo . " " . yourstr_replace("+"," ",urldecode($_POST['outputname'])) . $cmdsuff . " > macos_ffmpeg_convert.out 2> macos_ffmpeg_convert.err; cat macos_ffmpeg_convert.err");
  //passthru($verb . " " . $switchone . " " . ourstr_replace("+"," ",$thewords) . " " . $switchtwo . " " . yourstr_replace("+"," ",urldecode($_POST['outputname'])) . $cmdsuff . " > macos_ffmpeg_convert.out 2> macos_ffmpeg_convert.err");



    $thecontent="";
    if ($mimetypeis != "") {
    $thecontent=nosourcei("<html><head><title>" . ourstr_replace("+"," ",$thewords) . " to " . yourstr_replace("+"," ",urldecode($_POST['outputname'])) . " via ffmpeg ...</title></head><body><h1>" . ourstr_replace("+"," ",$thewords) . " to " . yourstr_replace("+"," ",urldecode($_POST['outputname'])) . " via <a target=_blank title='ffmpeg Audio/Video Conversion' href='https://ffmpeg.org/'>ffmpeg</a> ...</h1><" . explode("/",$mimetypeis)[0] . " controls><source type=" . $mimetypeis . " src='data:" . $mimetypeis . ";base64," . base64_encode(file_get_contents(yourstr_replace("+"," ",urldecode($_POST['outputname'])))) . "'></source></" . explode("/",$mimetypeis)[0] . "></body></html>");
    }

    if ($thecontent != "") {
      echo $thecontent;
      exit;
    }




  if (9 == 9 || explode(".",yourstr_replace("+"," ",urldecode($_POST['outputname'])))[-1 + sizeof(explode(".",yourstr_replace("+"," ",urldecode($_POST['outputname']))))] == "pdf") {
        $PDFfilename=yourstr_replace("+"," ",urldecode($_POST['outputname']));
        $pdf = file_get_contents($PDFfilename);

        header('Content-Type: application/pdf');
        header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
        header('Pragma: public');
        header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Content-Length: ' . strlen($pdf));
        header('Content-Disposition: inline; filename="' . basename($PDFfilename) .'";');
        ob_clean(); 
        flush(); 
        echo $pdf;  
        //exit; 
  } else if (explode(".",yourstr_replace("+"," ",urldecode($_POST['outputname'])))[-1 + sizeof(explode(".",yourstr_replace("+"," ",urldecode($_POST['outputname']))))] == "rtf") {
        $PDFfilename=yourstr_replace("+"," ",urldecode($_POST['outputname']));
        $pdf = file_get_contents($PDFfilename);

        header('Content-Type: application/rtf');
        header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
        header('Pragma: public');
        header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Content-Length: ' . strlen($pdf));
        header('Content-Disposition: inline; filename="' . basename($PDFfilename) .'";');
        ob_clean(); 
        flush(); 
        echo $pdf;  
        //exit; 
  } else if (explode(".",yourstr_replace("+"," ",urldecode($_POST['outputname'])))[-1 + sizeof(explode(".",yourstr_replace("+"," ",urldecode($_POST['outputname']))))] == "odt") {
        $PDFfilename=yourstr_replace("+"," ",urldecode($_POST['outputname']));
        $pdf = file_get_contents($PDFfilename);

        header('Content-Type: application/odt');
        header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
        header('Pragma: public');
        header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Content-Length: ' . strlen($pdf));
        header('Content-Disposition: inline; filename="' . basename($PDFfilename) .'";');
        ob_clean(); 
        flush(); 
        echo $pdf;  
        //exit; 
  }
  if (file_exists("macos_ffmpeg_convert.out") && file_exists("macos_ffmpeg_convert.err")) {
  if (3 == 6 && filesize("macos_ffmpeg_convert.out") == 0 && filesize("macos_ffmpeg_convert.err") != 0) {
     if ($rone != "" && $rtwo != "") {
      rename($rone, $rtwo);
     }
    echo "<html><body onload=\" parent.document.getElementById('dothework').style.height='400px';  parent.document.getElementById('dothework').style.display='block'; \"><p>" . file_get_contents("macos_ffmpeg_convert.err") . "</p></body></html>";
  }
  }
  }
  exit;
  }
} else if ((!isset($_GET['convertthis']) && !isset($_POST['convertthis'])) || !isset($_GET['outputname']) || !isset($_POST['outputname'])) {
  $outfileis="";
  if (isset($_GET['outputname'])) {
    //file_put_contents("xxx.xxx", $_SERVER['QUERY_STRING']);
    $extis=explode(".",str_replace("+"," ",urldecode($_GET['outputname'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_GET['outputname']))))];
    $mimetypeis="";
    $videoin=false;
    for ($jia=0; $jia<sizeof($exts); $jia++) {
      if (strtolower("." . explode(".",str_replace("+"," ",urldecode($_GET['convertthis'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_GET['convertthis']))))]) == strtolower($exts[$jia])) {
        if (strpos($types[$jia], "video/") !== false) { $videoin=true;
        if (PHP_OS == "Darwin") { $cmdsuff=" "; } }
      }
    }
    for ($jia=0; $jia<sizeof($exts); $jia++) {
      if (strtolower("." . $extis) == strtolower($exts[$jia])) {
        $mimetypeis=$types[$jia];
        if (strpos(strtolower($mimetypeis), "image/") !== false && $cmdsuff != "") {
          $cmdsuff.="; open -a Safari " . yourstr_replace("+"," ",urldecode($_GET['outputname'])) . " ";
        }
        if ((strpos(strtolower(str_replace("+"," ",urldecode($_GET['convertthis']))), ".pdf") !== false || strpos(strtolower($mimetypeis), "image/") !== false) && !$videoin) {
          if ($wasverb == "") {
            $wasverb=$verb;
            $wasswitchone=$switchone;
          }
          $verb=$iverb;
          if (strpos(strtolower(str_replace("+"," ",urldecode($_GET['convertthis']))), ".pdf") !== false) {
          $switchone=" -delay 200 ";
          } else {
          $switchone=$iswitchone;
          }
        } else if ($wasverb != "") {
          $verb=$wasverb;
          $switchone=$wasswitchone;
        }
      }
    }
    if (file_exists(str_replace("+"," ",urldecode($_GET['outputname'])))) {
    if (strtolower("~" . $extis, "~htm") !== false || $mimetypeis != "") {
    $thecontent=nosourcei("<h1>" . yourstr_replace("+"," ",urldecode($_GET['outputname'])) . " via <a target=_blank title='ffmpeg Audio/Video Conversion' href='https://ffmpeg.org/'>ffmpeg</a> ...</h1><" . explode("/",$mimetypeis)[0] . " controls><source type=" . $mimetypeis . " src='data:" . $mimetypeis . ";base64," . base64_encode(file_get_contents(str_replace("+"," ",urldecode($_GET['outputname'])))) . "'></source></" . explode("/",$mimetypeis)[0] . ">");
    } else if (strtolower("~" . $extis, "~pdf") !== false) { // thanks to https://stackoverflow.com/questions/12730581/use-this-php-code-to-open-a-pdf-in-a-new-tab
        if (1 == 2) {
        $PDFfilename=str_replace("+"," ",urldecode($_GET['outputname']));
        $pdf = file_get_contents($PDFfilename);

        header('Content-Type: application/pdf');
        header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
        header('Pragma: public');
        header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Content-Length: ' . strlen($pdf));
        header('Content-Disposition: inline; filename="' . basename($PDFfilename) .'";');
        ob_clean(); 
        flush(); 
        echo $pdf; 
        }
        exit;  
    } else if (strtolower("~" . $extis, "~rtf") !== false) { // thanks to https://stackoverflow.com/questions/12730581/use-this-php-code-to-open-a-pdf-in-a-new-tab
        if (1 == 1) {
        $PDFfilename=str_replace("+"," ",urldecode($_GET['outputname']));
        $pdf = file_get_contents($PDFfilename);

        header('Content-Type: application/rtf');
        header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
        header('Pragma: public');
        header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Content-Length: ' . strlen($pdf));
        header('Content-Disposition: inline; filename="' . basename($PDFfilename) .'";');
        ob_clean(); 
        flush(); 
        echo $pdf; 
        } 
        exit; 
    } else {
    //echo explode(".",str_replace("+"," ",urldecode($_GET['outputname'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_GET['outputname']))))];
    //exit;
    header('Content-type: application/' . explode(".",str_replace("+"," ",urldecode($_GET['outputname'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_GET['outputname']))))]); 
    echo file_get_contents(str_replace("+"," ",urldecode($_GET['outputname'])));
    exit;
    }
    }
  } else if (isset($_POST['outputname'])) {  
    $extis=explode(".",str_replace("+"," ",urldecode($_POST['outputname'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['outputname']))))];

    $mimetypeis="";
    $videoin=false;
    for ($jia=0; $jia<sizeof($exts); $jia++) {
      if (strtolower("." . explode(".",str_replace("+"," ",urldecode($_POST['convertthis'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['convertthis']))))]) == strtolower($exts[$jia])) {
        if (strpos($types[$jia], "video/") !== false) { $videoin=true;
        if (PHP_OS == "Darwin") { $cmdsuff=" "; } }
      }
    }
    for ($jia=0; $jia<sizeof($exts); $jia++) {
      if (strtolower("." . $extis) == strtolower($exts[$jia])) {
        $mimetypeis=$types[$jia];
        if (strpos(strtolower($mimetypeis), "image/") !== false && $cmdsuff != "") {
          $cmdsuff.="; open -a Safari " . str_replace("+"," ",urldecode($_POST['convertthis'])) . " ";
        }
        if ((strpos(strtolower(str_replace("+"," ",urldecode($_POST['convertthis']))), ".pdf") !== false || strpos(strtolower($mimetypeis), "image/") !== false) && !$videoin) {
          if ($wasverb == "") {
            $wasverb=$verb;
            $wasswitchone=$switchone;
          }
          $wasverb=$iverb;
          if (strpos(strtolower(str_replace("+"," ",urldecode($_POST['convertthis']))), ".pdf") !== false) {
          $switchone=" -delay 200 ";
          } else {
          $switchone=$iswitchone;
          }
        } else if ($wasverb != "") {
          $verb=$wasverb;
          $switchone=$wasswitchone;
        }
      }
    }
    if (file_exists(str_replace("+"," ",urldecode($_POST['outputname'])))) {
    if (strtolower("~" . $extis, "~htm") !== false || $mimetypeis != "") {
    $thecontent=nosourcei("<h1>" . yourstr_replace("+"," ",urldecode($_GET['outputname'])) . " via <a target=_blank title='ffmpeg Audio/Video Conversion' href='https://ffmpeg.org/'>ffmpeg</a> ...</h1><" . explode("/",$mimetypeis)[0] . " controls><source type=" . $mimetypeis . " src='data:" . $mimetypeis . ";base64," . base64_encode(file_get_contents(str_replace("+"," ",urldecode($_POST['outputname'])))) . "'></source></" . explode("/",$mimetypeis)[0] . ">");
    } else if (strtolower("~" . $extis, "~pdf") !== false) { // thanks to https://stackoverflow.com/questions/12730581/use-this-php-code-to-open-a-pdf-in-a-new-tab
        if (1 == 2) {
        $PDFfilename=str_replace("+"," ",urldecode($_POST['outputname']));
        $pdf = file_get_contents($PDFfilename);

        header('Content-Type: application/pdf');
        header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
        header('Pragma: public');
        header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Content-Length: ' . strlen($pdf));
        header('Content-Disposition: inline; filename="' . basename($PDFfilename) .'";');
        ob_clean(); 
        flush(); 
        echo $pdf; 
        } 
        exit; 
    } else if (strtolower("~" . $extis, "~rtf") !== false) { // thanks to https://stackoverflow.com/questions/12730581/use-this-php-code-to-open-a-pdf-in-a-new-tab
        if (1 == 1) {
        $PDFfilename=str_replace("+"," ",urldecode($_POST['outputname']));
        $pdf = file_get_contents($PDFfilename);

        header('Content-Type: application/rtf');
        header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
        header('Pragma: public');
        header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Content-Length: ' . strlen($pdf));
        header('Content-Disposition: inline; filename="' . basename($PDFfilename) .'";');
        ob_clean(); 
        flush(); 
        echo $pdf; 
        } 
        exit; 
    } else {
    //echo explode(".",str_replace("+"," ",urldecode($_POST['outputname'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['outputname']))))];
    //exit;
    header('Content-type: application/' . explode(".",str_replace("+"," ",urldecode($_POST['outputname'])))[-1 + sizeof(explode(".",str_replace("+"," ",urldecode($_POST['outputname']))))]); 
    echo file_get_contents(str_replace("+"," ",urldecode($_POST['outputname'])));
    exit;
    }
    }
  }
  echo "<html>
<head>" . $title . "
<style>
  input[type=text] { background-color: #f0f0f0;  border-radius: 5px; }
  input[type=submit] { font-size: 18px; border-radius: 20px; }
  button { font-size: 18px; border-radius: 20px; }
  div[contenteditable=true] { font-size: 14px; border-radius: 5px; }
  table { border-radius: 25px; }
</style>
<script type='text/javascript'>
  var wasv='';
  var firstgo=true;
  var lastvalue='.m4v';
  var oktosub=true;
  var origemail='';
  
  
  function setoe() {
  localStorage.setItem('macos_ffmpeg_convert', encodeURIComponent(origemail));
  }

  function getoe() {
  var eeeis=decodeURIComponent(('' + localStorage.getItem('macos_ffmpeg_convert')).replace(/^null$/g,''));
  if (eeeis != '') { 
    origemail=eeeis; 
    if (document.getElementById('bdiv')) { document.getElementById('bdiv').innerHTML=origemail; } 
    if (document.getElementById('preaemail')) { document.getElementById('preaemail').innerHTML=origemail; } 
  }
  }
  
  function ots() {
    oktosub=true;
  }
  
  function dforb(evt) {
  evt.stopPropagation(); 
  if (oktosub) { document.getElementById('ifsubmit').click();  document.getElementById('myform').style.cursor='progress'; document.body.style.cursor='progress';  }
  return false;
  }
  
  function ddforb(evt) {
  evt.stopPropagation(); 
  if (oktosub) { document.getElementById('ifsubmit').click();  document.getElementById('myform').style.cursor='progress'; document.body.style.cursor='progress';  }
  return false;
  }
  
  function getask(defv, force) {
  if (defv.indexOf('@') != -1 && !force) {
  return defv;
  } else {
  var eee=prompt('Email to?',defv); 
  if (eee == null) { 
   eee=''; 
  } 
  if (eee.indexOf('@') != -1) { origemail=eee; setoe(); }
  return eee;
  }
  }
  
  function aonclick(evt) {
  evt.stopPropagation(); 
  athis=evt.target;
  oktosub=false; 
  var ee=getask(athis.innerHTML.replace('&nbsp;',''), true);
  if (ee != '') { 
   athis.innerHTML=ee.trim();  
  } 
  if (ee.indexOf('@') != -1) { 
   origemail=ee;
   oktosub=true; 
   document.getElementById('ifsubmit').click();   
  } else { 
   setTimeout(ots, 1000); 
  }
  return false;
  }
  
  function donblur(dthis) {
  oktosub=false; 
  var ee=dthis.innerHTML; 
  if (ee == null) { 
   ee=''; 
  } 
  if (ee != '') { 
   dthis.innerHTML=ee.trim();  
  } 
  if (ee.indexOf('@') != -1) { 
   if (ee != origemail) {
   oktosub=true; 
   origemail=ee;
   setoe();
   document.getElementById('ifsubmit').click();   
   }
  } else { 
   setTimeout(ots, 1000); 
  }
  }
  
  function domo(evt) {
  evt.stopPropagation(); 
  oktosub=false;
  return false;
  }
  
  function domout(evt) {
  evt.stopPropagation(); 
  oktosub=true;
  return false;
  }
  
  function ionclick(evt) {
  evt.stopPropagation(); 
  oktosub=false;
  return false;
  }
  
  function onf(evt) {
  evt.stopPropagation(); 
  oktosub=false;
  origemail=' ';
  }
  
  function tsuper() {
  if (document.getElementById('thsuperfluous')) { document.getElementById('thsuperfluous').style.visibility='hidden';    }
  if (document.getElementById('tdsuperfluous')) { document.getElementById('tdsuperfluous').style.visibility='hidden';    }
  }

  function woit() {
    var lf=100,tpq=100;
    for (var ij=0; ij<document.getElementById('thewords').value.split(',').length; ij++) {
    window.open(document.URL.split('?')[0].split('#')[0] + '?outputname=' + encodeURIComponent(document.getElementById('outputname').value.split(',')[ij]) + '&inpath=' + encodeURIComponent(document.getElementById('path').value) + '&outpath=' + encodeURIComponent(document.getElementById('opath').value) + '&convertthis=' + encodeURIComponent(document.getElementById('thewords').value.split(',')[ij]),'_blank','top=' + tpq + ',left=' + lf + ',width=500,height=500');
    lf+=20;
    tpq+=20;
    }
  }

  function thenreload() {
   if (1 == 4) {
    location.href=document.URL.split('?')[0].split('#')[0] + '?outputname=' + encodeURIComponent(document.getElementById('outputname').value) + '&inpath=' + encodeURIComponent(document.getElementById('path').value) + '&outpath=' + encodeURIComponent(document.getElementById('opath').value) + '&dummyconvertthis=' + encodeURIComponent(document.getElementById('thewords').value);
   }
  }
  
  function resultchange() {
    var newwo=null;
    if (document.getElementById('dbwithlf')) { document.getElementById('ifsubmit').style.display='none'; }
    if (document.getElementById('resultav').value == '') {
    setTimeout(resultchange, 2000);
    } else if (firstgo) {
    firstgo=false;
    setTimeout(resultchange, 6000);
    } else if (window.opener && document.URL.indexOf('outpath=') == -1) {
    } else if (window.opener && document.URL.indexOf('outpath=') != -1) {
    if (document.URL.split('outpath=')[1].indexOf('html') != -1) {
      newwo=window.open('','_blank','left=200,top=200,width=300,height=300');
      newwo.document.write(document.getElementById('resultav').value);
    }
    }
  }
  
  function selch(thisvalue) {
    if (lastvalue != '.*') {
     while (document.getElementById('outputname').value.indexOf(lastvalue) != -1) {
      document.getElementById('outputname').value=document.getElementById('outputname').value.replace(lastvalue, '.*');
     }
    }
    if (thisvalue.indexOf('/') != -1) {
    document.getElementById('outputname').value=document.getElementById('outputname').value.replace(/\.\*/g,'.' + thisvalue.split('/')[1]);
    lastvalue='.' + thisvalue.split('/')[1];
    } else {
    document.getElementById('outputname').value=document.getElementById('outputname').value.replace(/\.\*/g,'.' + thisvalue);
    lastvalue='.' + thisvalue;
    }
    document.getElementById('cbi').src=document.getElementById('cbi').src.split('#')[0].split('?')[0] + '?totype=' + encodeURIComponent(thisvalue.replace('*','m4v')) + '&d=' + Math.floor(Math.random() * 19876543);
  }
  
  function filespecit(iois) {
  var aconto=null;
  if (aconto != null) {
       aconto = (iois.contentWindow || iois.contentDocument);
       if (aconto != null) {
       if (aconto.document) { aconto = aconto.document; }
       if (aconto.body != null) {
         iois=iois;
       }
       }
  }
  }
 
 function assessit(tv) {
  if (tv.indexOf('*') != -1 || tv.indexOf('%') != -1 || tv.indexOf('$') != -1) {
    document.getElementById('globit').src=document.URL.split('#')[0].split('?')[0] + '?globit=' + encodeURIComponent(tv) + '&globpath=' + encodeURIComponent(document.getElementById('path').value) + '&globext=' + encodeURIComponent(document.getElementById('cto').value.replace('*','m4v'));
  } else if (tv.indexOf(',') == -1) {
    document.getElementById('outputname').value=tv.split('.')[0] + '.' + document.getElementById('cto').value.replace('*','m4v').replace('.','');
  }
 }
 
 function askee() {
   var msg='';
   if (document.getElementById('thewords').value.trim() != '' && document.getElementById('outputname').value.trim() != '') {
   var ee='';
   if (document.getElementById('bdiv')) {
   if (document.getElementById('bdiv').innerHTML.indexOf('@') != -1) {
   ee=document.getElementById('bdiv').innerHTML;
   } else {
   ee=prompt('Please enter your emailee address.','');
   }
   } else {
   ee=prompt('Please enter your emailee address.','');
   }
   if (ee == null) { ee=''; }
   if (ee.indexOf('@') != -1) {
     var inps=document.getElementsByTagName('input');
     for (var iii=0; iii<inps.length; iii++) {
     if (('~' + ('' + inps[iii].name) + '~').indexOf('to~') != -1 || ('~' + ('' + inps[iii].name) + '~').indexOf('~to') != -1) {
       inps[iii].value=ee;
     } else if (('~' + ('' + inps[iii].name) + '~').indexOf('subject~') != -1 || ('~' + ('' + inps[iii].name) + '~').indexOf('~subject') != -1) {
       //alert('lastvalue=' + lastvalue + ' and inps[iii].value=' + inps[iii].value + ' AND ' + document.getElementById('cto').value.toLowerCase()); 
       // lastvalue=.gif and inps[iii].value=0000__1-Arial_12pxnormal.pdf downloadable ready for your inhouse .pdf to .m4v conversions, sent to you (for you to download) as convert_audiovideo_please_m4v_0000__1-Arial_12pxnormal.pdf AND gif
       inps[iii].value=inps[iii].value.replace('_*_','_m4v_').replace('.* ','.m4v ');
       if (inps[iii].value.indexOf('_m4v_') != -1 && document.getElementById('cto').value.toLowerCase().replace('.','') != 'm4v') {
          inps[iii].value=inps[iii].value.replace('_m4v_', '_' + document.getElementById('cto').value.toLowerCase() + '_').replace('.m4v ', '.' + document.getElementById('cto').value.toLowerCase() + ' ');
       } else if (inps[iii].value.indexOf(lastvalue.replace('.','_') + '_') != -1 && lastvalue.replace('.','') != document.getElementById('cto').value.replace('.','')) {
          inps[iii].value=inps[iii].value.replace(lastvalue.replace('.','_') + '_', '_' + document.getElementById('cto').value.toLowerCase() + '_').replace(lastvalue + ' ', '.' + document.getElementById('cto').value.toLowerCase() + ' ');
       } 
     } else if (('' + inps[iii].type).toLowerCase().indexOf('submit') != -1 && ('' + inps[iii].id).toLowerCase().indexOf('ifsubmit') == -1) {
       inps[iii].click();
       if (msg == '') {
         msg='Email sent';
       } else {
         msg='Emails sent';
       }
     }
     }
   }
   }
   document.getElementById('myform').style.cursor='pointer'; 
   document.body.style.cursor='pointer';    
   if (msg != '') {
     alert(msg + ' to ' + ee);
   }
 }
</script>
</head>
<body onload=\"getoe(); " . $onl . " resultchange();\" style=\"background: linear-gradient(to left, #ffffff 20%, #ffefba 80%); \">
<br><h1>Supervise <span id=sverb><a target=_blank title='ffmpeg Audio/Video Conversion' href='https://ffmpeg.org/'>ffmpeg</a></span> Audio/Video or Image/PDF <a target=_blank title='ImageMagick Image Conversion' href='https://imagemagick.org/'>ImageMagick</a> Conversions</h1>
<h3>RJM Programming - July, 2020</h3>
" . $outfileis . "
<form id=myform onsubmit=\"if (document.getElementById('ifsubmit').value.indexOf('mail ') != -1) { setTimeout(askee,9000); return false; } else { if (document.getElementById('thewords').value.indexOf(',') != -1) {  woit(); return false; } else { document.getElementById('myform').style.cursor='progress'; document.body.style.cursor='progress'; setTimeout(thenreload,20000);  document.getElementById('dothework').style.height='400px'; document.getElementById('dothework').style.display='block';  return true; } }\" target=dothework action=./macos_ffmpeg_convert.php method=POST>
<table border=20 style='width:98%;' cellspacing=5 cellpadding=5>
<tr><th style='background-color:lightblue;'>Input Audio/Video/Image/PDF File(s) <span id=sin style=display:none;>in</span> <input title=Path name=inpath style=display:inline-block;width:50%; type=text id=path value='' placeholder='Optional input path'></input></th><th><select onchange=\" selch(this.value.toLowerCase());  \" style='display:inline-block;background-color:#dddddd;' name=convertto id=cto>
<option value='*'>Convert to</option>" . $avtypes . "<option value='pdf'>Convert Image to Document pdf</option><option value='GIF'>Convert pdf to Animated Gif</option> 
</select>&nbsp;<input title=Path name=outpath style=display:inline-block;width:30%; type=text id=opath value='' placeholder='Optional output path'></input> <input style='width:40%;' onblur=\"if (document.URL.toLowerCase().indexOf('rjmprogramming.com.au') != -1) { this.value=''; } if (this.value.toLowerCase().indexOf('.pdf') != -1) { document.getElementById('myform').target='_blank';   }  \" placeholder='Output Audio/Video/Image/PDF file(s)' type=text name=outputname id=outputname value=''></input></div>&nbsp;</th></tr>
<tr><td style='vertical-align:top;background-color:lightblue;'><input type=text onblur=\"if (document.URL.toLowerCase().indexOf('rjmprogramming.com.au') != -1) { this.value=''; } else { assessit(this.value); }\" style='width:90%;' placeholder='Input Audio/Video/Image/PDF file(s)' id=thewords name=convertthis  value=''></input><br><sup>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;or" . $cself . "&nbsp;...&nbsp;</sup><div style='display:inline-block;width:95px;height:30px;overflow:hidden;vertical-align:text-bottom;'><iframe frameborder=0 id=cbi data-style='border-top:1px solid black;border-bottom:1px solid black;border-left:2px solid yellow;border-right:1px solid yellow;' style='width:173px;height:228px;margin-top:-194px;' src='HTMLCSS/client_browsing.htm?totype=m4v&d=" . rand(0,18765432) . "'></iframe></div></td><td style=vertical-align:top;><div id=dforbutton>" . $divinfront . "</div><input id=ifsubmit style='width:100%;height:100px;background-color:orange;' type=submit value='" . $cvse . " Audio/Video/Image/PDF(s)'></input><br><div id=resultav>" . $thecontent . "</div></td></tr>
</table>
</form>
<div id=dag></div>
<iframe style='width:100%;height:0px;display:none;' frameborder=0 name=dothework id=dothework src=macos_ffmpeg_convert.php></iframe>
<iframe onload='filespecit(this);' style='display:none;' id=globit src=></iframe>
<input type=text style='position:absolute;top:-1200px;left:-1200px;'></input>
<input type=hidden id=turl value=''></input>
<div id=dforms>
</div>
</body>
</html>";
} 
} else {
 echo "<html><head><title>Email off your Audio/Video/Image/PDF Conversion Attachment Input Files for Your Inhouse Audio/Video/Image/PDF Converter to Listen For and Convert</title></head><body><p>This PHP web application only works with <a target=_blank title='ffmpeg Audio/Video/Image Conversion' href='https://ffmpeg.org/'>ffmpeg</a> (Audio/Video) and/or <a target=_blank title='ffmpeg Audio/Video/Image Conversion' href='https://imagemagick.org/'>ImageMagick</a> (Image/PDF).  Perhaps consider getting a local Apache/PHP/MySql web server such as <a target=_blank title='MAMP for Apache/PHP/MySql on Mac OS X local web server' href='http://www.mamp.info'><i>MAMP</i></a> or you have an email listener and will <a target=_self href='./macos_ffmpeg_convert.php?emailanyway=y'>Email and Download</a> to that emailee to do the conversions</p></body><html>";
 exit;
}
?>
