<?php
// get_tz.php
// RJM Programming
// Optionally helps out x1.cc 

$addchange="";
$fgtz="";
$space="";
$proposed="tz_places.php";
$qm="?";

function firstof($intz) {
  global $addchange, $space, $proposed, $qm;
  $unresolved=true;
  $good="";
  $twovs="";
  if (trim($intz) != $intz) { $space=" "; }
  $outtz=trim($intz);
  if (strtoupper($outtz) == "\$TZ") { 
    $qm="#"; 
    $addchange=" parent.document.getElementById('tz').value='';  ";
    return strtoupper($outtz); 
  }
  $words=explode(" ", str_replace(",", " ", $outtz));
  if (trim($outtz) != "") {
    if (strpos($outtz, "/") === false) {
      if (!file_exists($proposed)) { $proposed="../PHP/tz_places.php"; }
      if (file_exists($proposed)) {
       $tzcont=@file_get_contents($proposed);
       while ($unresolved) {
        if (strpos(str_replace("[",",",strtoupper($tzcont)), ',"' . strtoupper(explode(" ",$outtz)[0]) . '",') !== false && strpos($tzcont, ',""') !== false) {
          $atleast=explode('"' . strtoupper($words[0]) . '",', $tzcont);
          $atleasttwo=explode('"' . strtoupper($words[0]) . '",', strtoupper($tzcont));
          $aftereq=explode("=", $atleasttwo[0])[-1 + sizeof(explode("=", $atleasttwo[0]))];
          $numcommas=explode(",", $aftereq);
          $tzlist=explode(",", str_replace("[","",explode("=", explode(',""', $tzcont)[0])[-1 + sizeof(explode("=", explode(',""', $tzcont)[0]))]));
          if (sizeof($tzlist) > sizeof($numcommas)) {
            $addchange=" if (parent.document.getElementById('tz').value == '" . $intz . "') { parent.document.getElementById('tz').value='" . str_replace('"','',str_replace("'","",$tzlist[-1 + sizeof($numcommas)])) . "'; } ";
            $good=str_replace('"','',str_replace("'","",$tzlist[-1 + sizeof($numcommas)]));
            $goods=explode("/", $good);
            if (strpos($tzcont, $atleast[0] . '"' . $words[0] . '",') !== false) {
            $tzcont=str_replace($atleast[0] . '"' . $words[0] . '",', $atleast[0] . '"#@$",', $tzcont);
            $onevs=strtoupper("/" . str_replace("_", " ", str_replace("-", " ", $goods[-1 + sizeof($goods)])));
            if (sizeof($words) > 1) {
            $twovs=strtoupper("/" . str_replace("_", " ", str_replace("-", " ", substr($outtz, (1 + strlen($words[0]))))));
            }
            if (sizeof($words) <= 1) {
              $unresolved=false;
            } else if (sizeof($words) == 2 && strlen($words[1]) == 2) {
              $tz = new DateTimeZone($good);
              $cloc = $tz->getLocation(); 
              $ctrycode = $cloc['country_code']; 
              if (strtoupper($ctrycode) == strtoupper($words[1])) {   $unresolved=false;  }
            } else if (strpos($onevs, $twovs) !== false) {
              $unresolved=false;
            }
            } else {
              $unresolved=false;
            }
          }
        } else {
          $unresolved=false;
        }
       }
      }
    }
  }
  if ($good != "") {  return $good;    }
  return $outtz;
}

if (!isset($argc)) { 
if (isset($_GET['tz'])) {
  $fgtz=firstof(str_replace("+"," ",urldecode($_GET['tz'])));
}
  $scr="<script type='text/javascript'>
  function myifer() {
    parent.document.getElementById('myif').src=document.URL.split('#')[0].split('?')[0] + '?rand=' + Math.floor(Math.random() * 1987564) + '&tz=' + (location.search.split('tz=')[1] ? location.search.split('tz=')[1].split('&')[0] : '');
  }
  function onl() {
    " . $addchange . " 
    if (window.parent && document.URL.indexOf('&') != -1) {
      if (parent.document.getElementById('ds')) {
       var huh=parent.document.getElementById('ds').innerHTML;
       if (parent.document.getElementById('aclock')) {
       if (parent.document.getElementById('aclock').innerHTML == '' && '' != '" . $space . "') {
       parent.document.getElementById('aclock').innerHTML='<iframe src=\"//www.rjmprogramming.com.au/HTMLCSS/colour_wheel.html" . $qm . "mode=" . urlencode($fgtz) . "\" style=\"width:100%;height:800px;\"></iframe>'; 
       }
       }
       parent.document.getElementById('ds').innerHTML='<iframe id=myif style=height:30px; src=\"' + document.URL.split('#')[0].split('?')[0] + '?rand=' + Math.floor(Math.random() * 1987564) + '&tz=' + (location.search.split('tz=')[1] ? location.search.split('tz=')[1].split('&')[0] : '') + '\"></iframe><br>'; // + huh;
       setTimeout(myifer, 2000);
      }
    } else if (!document.getElementById('myif')) {
      document.body.innerHTML+='<iframe id=myif style=display:none; src=></iframe>';
    } else if (1 == 2) {
      document.getElementById('myif').src=document.URL.split('#')[0].split('?')[0] + '?rand=' + Math.floor(Math.random() * 1987564) + '&tz=' + (location.search.split('tz=')[1] ? location.search.split('tz=')[1].split('&')[0] : '');
      setTimeout(onl, 2000);
    }
  }
  
  setTimeout(onl,1500);
  </script>";
if (isset($_GET['tz'])) {
//while (1) {
  //sleep(1);
  if ($qm == "#") {
  echo "<!doctype html><html><body><p id=xds>" . passthru("date") . "</p>" . $scr . "</body></html>";
  } else {
  echo "<!doctype html><html><body><p id=xds>" . passthru("TZ=" . $fgtz . " date") . "</p>" . $scr . "</body></html>";
  }
//}
//exit;
} else {
echo "<!doctype html><html><head>" . $scr . "</head><body><div id=ds></div><br><div id=aclock></div><input style='width:50%;' onblur=\"  if (this.value.trim() == '') { var x=this.value; this.value='\$TZ' + x; } if (this.value.indexOf('/') != -1 || this.value.trim() != '') {  if (document.getElementById('aclock')) { document.getElementById('aclock').innerHTML=''; }  document.getElementById('myif').src=document.URL.split('#')[0].split('?')[0] + '?rand=' + Math.floor(Math.random() * 19765453) + '&tz=' + encodeURIComponent(this.value);  } \" type=text placeholder='Optionally specify timezone that is not your local one eg. Europe/Berlin or CEST Berlin (append space for graphical Analogue Clock)' name=tz id=tz value=''></input><input type=text style='position:absolute;left:-200px;top:-200px;' value=''></input><iframe id=myif style=display:none; src=></iframe>"; 
}
} else {
if (file_exists("stop_x1.x1")) { unlink("stop_x1.x1"); }
$line = readline("Optionally specify timezone that is not your local one eg. Europe/Berlin (else return): ");
if (strpos(trim($line), "/") !== false) {
  file_put_contents("stop_x1.x1", "");
  sleep(2);
  exec("TZ=" . $line . " ./x1 " . $line);
  exit(1);
} else {
  exit(0);
} 
}
?>
