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

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

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

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

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") { 
    if ($argc >= 2) {
     if (file_exists(getenv('HOME') . '/Desktop/MyAppleScript' . $ts . '.scpt')) {
       unlink(getenv('HOME') . '/Desktop/MyAppleScript' . $ts . '.scpt');
     }
     file_put_contents(getenv('HOME') . '/Desktop/MyAppleScript' . $ts . '.scpt', "tell application \"Terminal\"\n	activate\n	do script \"cd '" . getenv('HOME') . '/Desktop' . "'\" in window 1\n	do script \"open '" . $argv[1] . "'\" in window 1\nend tell");
     exec("cd " . getenv('HOME') . '/Desktop ; open MyAppleScript' . $ts . '.scpt');
    }
  }
} 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'])));
  $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__) . "/" . "imagetest.jpg")) { unlink(dirname(__FILE__) . "/" . "imagetest.jpg");  }
  $gretv=dirname(__FILE__) . "/" . "imagetest.jpg";
  imagejpeg($im, dirname(__FILE__) . "/" . "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']));
  exit;
  }

  if (isset($_GET['tsplus'])) { 
      $tsplus=str_replace('+',' ',urldecode($_GET['tsplus'])); 
  } 

  if (PHP_OS == "Darwin") { 
    if (isset($_GET['justplay'])) {
    $moreopens='';
    if (isset($_GET['showothers'])) {
    foreach (glob(getenv('HOME') . '/Desktop/http*.scpt') as $infl) {
      if (strpos($infl, $tsplus) === false) {
        $moreopens.=' open ' . basename($infl) . '; ';
      }
    }
    } 
    if (!file_exists(getenv('HOME') . '/Desktop/' . $tsplus . '.scpt')) {
    file_put_contents(getenv('HOME') . '/Desktop/' . $tsplus . '.scpt', "tell application \"Terminal\"\n	activate\n	do script \"cd '" . getenv('HOME') . '/Desktop' . "'\" in window 1\n	do script \"open '" . str_replace('+','%20',$_GET['url']) . "'\" in window 1\nend tell");
    } 
    passthru("cd " . getenv('HOME') . '/Desktop ; ' . $moreopens . ' open ' . $tsplus . '.scpt');
    } else {
    $moreopens='';
    if (isset($_GET['showothers'])) {
    foreach (glob(getenv('HOME') . '/Desktop/http*.scpt') as $infl) {
      if (strpos($infl, $tsplus) === false) {
        $moreopens.=' open ' . basename($infl) . '; ';
      }
    }
    } 
    if (file_exists(getenv('HOME') . '/Desktop/' . $tsplus . '.scpt')) {
       unlink(getenv('HOME') . '/Desktop/' . $tsplus . '.scpt');
    }
    file_put_contents(getenv('HOME') . '/Desktop/' . $tsplus . '.scpt', "tell application \"Terminal\"\n	activate\n	do script \"cd '" . getenv('HOME') . '/Desktop' . "'\" in window 1\n	do script \"open '" . str_replace('+','%20',$_GET['url']) . "'\" in window 1\nend tell");
    //exec('open \"' . getenv('HOME') . '/Desktop/' . $tsplus . '.scpt\"');
    if (!isset($_GET['noplay'])) {
    passthru("cd " . getenv('HOME') . '/Desktop ; ' . $moreopens . ' open ' . $tsplus . '.scpt');
    }
    }
  } 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>";
  }
}
?>