
<?php
// apple_script_url.php
// RJM Programming
// February 2022

$prets='MyAppleScript';
$ts=date('_dmY_his', time());
$tsplus=$prets . $ts;


$midbit=rand(0,19876754);
$gretv='';

$localsug='HTTP://localhost:8888/';

if (PHP_OS == 'WINNT' || PHP_OS == 'WIN32' || PHP_OS == 'Windows') {
  $homename='USERPROFILE';
  $localsug='HTTP://localhost/';
  $openverb='start';
  $openext='.bat';
  $tellprefix='';
} else {
  $homename='HOME';
  $openverb='open';
  $openext='.scpt';
  $tellprefix="tell application \"Terminal\"\n activate\n do script \"cd '" . getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . "'\" in window 1\n do script \"";
}

function wincomplexity($inurl) {
  if (PHP_OS == 'WINNT' || PHP_OS == 'WIN32' || PHP_OS == 'Windows') {
    $outurl=str_replace('&', '^&', $inurl);
    $huhs=explode("'", $outurl);
    if (sizeof($huhs) >= 3) { return ' ' . $huhs[1];  }
    return explode('"', $outurl)[0];
  }
  return $inurl;
}

function oururldecode($inue) {
  global $gretv;
  $retv=urldecode($inue);
  $retsare=explode("localhost", $inue);
  if (sizeof($retsare) > 1) {
    $retv=str_replace($retsare[0] . 'localhost' . explode('#', explode('?', explode('/', $retsare[1])[0])[0])[0] . '/', $_SERVER['DOCUMENT_ROOT'], $retv);
  }
  $gretv=$retv;
  return $retv;
}

if (isset($argc)) {
  if (PHP_OS == "Darwin" || PHP_OS == 'WINNT' || PHP_OS == 'WIN32' || PHP_OS == 'Windows') {
    if ($argc >= 2) {
     if (file_exists(getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . DIRECTORY_SEPARATOR . 'MyAppleScript' . $ts . $openext)) {
       unlink(getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . DIRECTORY_SEPARATOR . 'MyAppleScript' . $ts . $openext);
     }
     file_put_contents(getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . DIRECTORY_SEPARATOR . 'MyAppleScript' . $ts . $openext, $tellprefix . $openverb . wincomplexity(" '" . $argv[1] . "'\" in window 1\nend tell"));
if (PHP_OS == 'WINNT' || PHP_OS == 'WIN32' || PHP_OS == 'Windows') {
     exec(file_get_contents(getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . DIRECTORY_SEPARATOR . 'MyAppleScript' . $ts . $openext));
} else {
     exec("cd " . getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop ; ' . $openverb . ' MyAppleScript' . $ts . $openext);
}
    }
  }
} else if (isset($_GET['url']) || (isset($_GET['imagetest']) && strpos($_SERVER['SERVER_NAME'], 'localhost') !== false)) {

  if (isset($_GET['imagetest']) && strpos($_SERVER['SERVER_NAME'], 'localhost') !== false) {
  //file_put_contents('xx.xx', (urldecode($_GET['imagetest'])));
  if (function_exists(imagecreatetruecolor)) {
  $im = imagecreatetruecolor(120, 10);
  $text_color = imagecolorallocate($im, 233, 14, 91);
  imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
  // Output the image
  if (strpos(oururldecode($_GET['imagetest']), '.') === false) {
  if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . "imagetest.jpg")) { unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . "imagetest.jpg");  }
  $gretv=dirname(__FILE__) . DIRECTORY_SEPARATOR . "imagetest.jpg";
  imagejpeg($im, dirname(__FILE__) . DIRECTORY_SEPARATOR . "imagetest.jpg");
  } else if (strpos(strtolower(oururldecode($_GET['imagetest'])), '.gif') !== false) {
  //file_put_contents('xxx.xxx', $_SERVER['SERVER_NAME']);
  if (file_exists(oururldecode($_GET['imagetest']))) { unlink(oururldecode($_GET['imagetest']));  }
  imagegif($im, oururldecode($_GET['imagetest']));
  //file_put_contents('xxxx.xxxx', $_SERVER['SERVER_NAME']);
  } else if (strpos(strtolower(oururldecode($_GET['imagetest'])), '.bmp') !== false) {
  if (file_exists(oururldecode($_GET['imagetest']))) { unlink(oururldecode($_GET['imagetest']));  }
  imagebmp($im, oururldecode($_GET['imagetest']), true);
  } else if (strpos(strtolower(oururldecode($_GET['imagetest'])), '.png') !== false) {
  if (file_exists(oururldecode($_GET['imagetest']))) { unlink(oururldecode($_GET['imagetest']));  }
  imagepng($im, oururldecode($_GET['imagetest']));
  } else if (strpos(strtolower(oururldecode($_GET['imagetest'])), '.jp') !== false) {
  if (file_exists(oururldecode($_GET['imagetest']))) { unlink(oururldecode($_GET['imagetest']));  }
  imagejpeg($im, oururldecode($_GET['imagetest']));
  }
  // Free up memory
  imagedestroy($im);
  if (file_exists($gretv)) {
  unlink($gretv);
  echo "<html><body onload=\"window.close();\"></body></html>";
  }
  //sleep(25);
  //unlink(oururldecode($_GET['imgtest']));
 } else { echo "<html><body onload=\"window.close();\"></body></html>";  }
  exit;
  }

  if (isset($_GET['tsplus'])) {
      // .replace(/\%\)/g,'__').replace(/\(/g,'_').replace(/\)/g,'_').replace(/\//g,'_').replace(/\{/g,'_').replace(/\}/g,'_').replace(/\:/g,'_').replace(/\*/g,'_').replace(/\$/g,'_').replace(/\,/g,'_').replace(/\./g,'_').replace(/\;/g,'_')
  if (isset($_GET['nothing'])) {
      $tsplus= str_replace('.','_',  str_replace(',','_',  str_replace('*','_',   str_replace('$','_',  str_replace(':','_', str_replace(';','_',   str_replace('}','_', str_replace('{','_',  str_replace('/','_', str_replace(')','_', str_replace('(','_', str_replace('%)','__', str_replace(str_replace('+',' ',urldecode($_GET['nothing'])), '',   str_replace(' ','',str_replace('+','',urldecode($_GET['tsplus'])))  )  ))))))))))));
      //file_put_contents('xxxx0xx.vvvvvv', str_replace(' ','',str_replace('+','',urldecode($_GET['tsplus']))) . ' ... ' . $tsplus);
  } else {
      $tsplus= str_replace('.','_',  str_replace(',','_',  str_replace('*','_',   str_replace('$','_',  str_replace(':','_', str_replace(';','_',   str_replace('}','_', str_replace('{','_',  str_replace('/','_', str_replace(')','_', str_replace('(','_', str_replace('%)','__',   str_replace(' ','',str_replace('+','',urldecode($_GET['tsplus'])))  ))))))))))));
      //file_put_contents('xxxx1xx.vvvvvv', $tsplus);
  }
      //file_put_contents('xxxxxx.vvvvvv', $tsplus);
  }

  if (PHP_OS == "Darwin" || PHP_OS == 'WINNT' || PHP_OS == 'WIN32' || PHP_OS == 'Windows') {
    if (isset($_GET['justplay'])) {
    $moreopens='';
    if (isset($_GET['showothers'])) {
    foreach (glob(getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . DIRECTORY_SEPARATOR . 'http*' . $openext) as $infl) {
      if (strpos($infl, $tsplus) === false) {
if (PHP_OS == 'WINNT' || PHP_OS == 'WIN32' || PHP_OS == 'Windows') {
        $moreopens.=' ' . file_get_contents($infl) . '; ';
} else {
        $moreopens.=' ' . $openverb . ' ' . basename($infl) . '; ';
}
      }
    }
    }
    if (!file_exists(getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . DIRECTORY_SEPARATOR . $tsplus . $openext)) {
    file_put_contents(getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . DIRECTORY_SEPARATOR . $tsplus . $openext, $tellprefix . $openverb . wincomplexity(" '" . str_replace('+','%20',$_GET['url']) . "'\" in window 1\nend tell"));
    }
if (PHP_OS == 'WINNT' || PHP_OS == 'WIN32' || PHP_OS == 'Windows') {
    passthru(file_get_contents(getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . DIRECTORY_SEPARATOR . $tsplus . $openext));
} else {
    passthru('cd ' . getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop ; ' . $moreopens . ' ' . $openverb . ' ' . $tsplus . $openext);
}
    } else {
    $moreopens='';
    if (isset($_GET['showothers'])) {
    foreach (glob(getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . DIRECTORY_SEPARATOR . 'http*' . $openext) as $infl) {
      if (strpos($infl, $tsplus) === false) {
if (PHP_OS == 'WINNT' || PHP_OS == 'WIN32' || PHP_OS == 'Windows') {
        $moreopens.=' ' . file_get_contents($infl) . '; ';
} else {
        $moreopens.=' ' . $openverb . ' ' . basename($infl) . '; ';
}
      }
    }
    }
    if (file_exists(getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . DIRECTORY_SEPARATOR . $tsplus . $openext)) {
       unlink(getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . DIRECTORY_SEPARATOR . $tsplus . $openext);
    }
    file_put_contents(getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . DIRECTORY_SEPARATOR . $tsplus . $openext, $tellprefix . $openverb . wincomplexity(" '" . str_replace('+','%20',$_GET['url']) . "'\" in window 1\nend tell"));
    if (!isset($_GET['noplay'])) {
if (PHP_OS == 'WINNT' || PHP_OS == 'WIN32' || PHP_OS == 'Windows') {
    passthru(file_get_contents(getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop' . DIRECTORY_SEPARATOR . $tsplus . $openext));
} else {
    passthru('cd ' . getenv($homename) . DIRECTORY_SEPARATOR . 'Desktop ; ' . $moreopens . ' ' . $openverb . ' ' . $tsplus . $openext);
}
    }
    }
  } else if (strpos($_SERVER['SERVER_NAME'], 'localhost') === false) {
    if (isset($_GET['localsug'])) {
    if (trim($_GET['localsug']) != '') {
      $localsug=str_replace('+',' ',urldecode($_GET['localsug']));
      if (strpos(($localsug . '`'), '/`') === false) {
        $localsug.='/';
      }
      if (strpos(strtolower($localsug), '//') === false) {
        $localsug='//' . $localsug;
      }
      if (strpos(strtolower($localsug), 'http') === false) {
        $localsug='http:' . $localsug;
      }
    }
    }
    echo "<html><body onload=\" window.open('" . $localsug . "apple_script_url.php?url=" . str_replace('+','%20',$_GET['url']) . "','_self');  \"></body></html>";
  }
}
?>

