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

if (strpos($_SERVER['SERVER_NAME'], 'rjmprogramming.com.au') !== false && 6 == 6) {
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') { // thanks to https://www.google.com/search?q=php+check+for+https+protocol&rlz=1C5OZZY_en&oq=php+check+for+https+protocol&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIHCAEQIRigATIHCAIQIRigATIHCAMQIRigATIHCAQQIRifBdIBCDg4MTRqMGo3qAIAsAIA&sourceid=chrome&ie=UTF-8
    // The request was made over HTTPS
    $dru='http://' . $_SERVER['SERVER_NAME'] . "" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "" . $_SERVER['REQUEST_URI']; // . $_SERVER['QUERY_STRING'];
    header('Location: ' . $dru);  //    echo "HTTPS is active.";
} //else {
    // The request was made over HTTP
    //echo "HTTP is active.";
//}
}
 
$addchange="";
$fgtz="";
$space="";
$proposed="tz_places.php";
$proposed=dirname(__FILE__) . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "PHP" . DIRECTORY_SEPARATOR . "tz_places.php";
$qm="?";

if (!function_exists("readline")) {  // thanks to https://stackoverflow.com/questions/23238378/call-to-undefined-function-readline
    function readline($prompt = null){
        if ($prompt){
            echo $prompt;
        }
        $fp = fopen("php://stdin","r");
        $line = rtrim(fgets($fp, 1024));
        return $line;
    }
}

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'])) {
  $fg=str_replace("+"," ",urldecode($_GET['tz']));
if (strpos(trim($fg), "/") === false && $fg != "") {
      $oneword=explode(" ", trim($fg));
      if (sizeof($oneword) == 1) {
      if ($oneword[0] != strtoupper($oneword[0])) {
       if (!file_exists($proposed)) { $proposed="../PHP/tz_places.php"; }
       if (file_exists($proposed)) {
        $tzcont=@file_get_contents($proposed);
        if (strpos(strtoupper($tzcont), "/" . strtoupper($oneword[0])) !== false) {
          $fbit=explode("/" . strtoupper($oneword[0]), strtoupper($tzcont));
          $lbit=explode('"', substr($tzcont, 0, strlen($fbit[0])))[-1 + sizeof(explode('"', substr($tzcont, 0, strlen($fbit[0]))))];
          if (trim($fg) != $fg) {
          $fg=$lbit . explode('"', substr($tzcont, strlen($fbit[0])))[0] . " ";
          } else {
          $fg=$lbit . explode('"', substr($tzcont, strlen($fbit[0])))[0] . " ";
          }
        }
       }
      }
      } else if ($oneword[0] != strtoupper($oneword[0])) {
       if (!file_exists($proposed)) { $proposed="../PHP/tz_places.php"; }
       if (file_exists($proposed)) {
        $tzcont=@file_get_contents($proposed);
        if (strpos(strtoupper($tzcont), "/" . strtoupper(  str_replace(" ", "_", trim($fg))  )) !== false) {
          $fbit=explode( "/" . strtoupper(  str_replace(" ", "_", trim($fg))  ), strtoupper($tzcont));
          $lbit=explode('"', substr($tzcont, 0, strlen($fbit[0])))[-1 + sizeof(explode('"', substr($tzcont, 0, strlen($fbit[0]))))];
          if (trim($fg) != $fg) {
          $fg=$lbit . explode('"', substr($tzcont, strlen($fbit[0])))[0] . " ";
          } else {
          $fg=$lbit . explode('"', substr($tzcont, strlen($fbit[0])))[0] . " ";
          }
        }
       }
      }  
}      
  $fgtz=firstof($fg);
}
  $scr="<style>  
body {
background: -webkit-linear-gradient(center top , rgb(255, 255, 0), rgb(0, 255, 255)) repeat scroll 0% 0% transparent;
background: -o-linear-gradient(center top , rgb(255, 255, 0), rgb(0, 255, 255)) repeat scroll 0% 0% transparent;
background: -moz-linear-gradient(center top , rgb(255, 255, 0), rgb(0, 255, 255)) repeat scroll 0% 0% transparent;
background: linear-gradient(180deg, rgb(255, 255, 0), rgb(0, 255, 255)); /* linear-gradient(center top , rgb(255, 255, 0), rgb(0, 255, 255)) repeat scroll 0% 0% transparent */
}
#big_txt {  /*" . " thanks to https://codepen.io/YuvarajTana/pen/auiqx   */ 
    color:lightgrey;
    font-size:120px;
    transform:rotate(300deg);
    -webkit-transform:rotate(300deg);
    height:50%;
    width:50%;
    margin-left:30px;
    z-index:-1;
    opacity:0.8;
}
#myif {
    border: 1px dotted orange;
    background-color: yellow;
}
#tz {
    border: 9px solid lightgreen;
    background-color: #f0f0f0;
}
</style>
  <title>TimeZone Places - RJM Programming - September, 2019</title>
  <script type='text/javascript'>
  var dtz='',  ddtz='', dprefix='', dsuffix='';
  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() {
    if (document.getElementById('tz')) {
    document.getElementById('tz').focus();
    }
    var alreadydone=true;
    var isIE = (navigator.appName == 'Microsoft Internet Explorer' || (navigator.appName == 'Netscape' && navigator.appVersion.indexOf('Edge') > -1));
    if (!isIE) { dprefix='<details open style=margin-top:-25px;><summary></summary>'; dsuffix='</details>'; }
    " . $addchange . " 
    if (window.parent && document.URL.indexOf('&') != -1) {
      if (parent.document.getElementById('ds')) {
       var huh=parent.document.getElementById('ds').innerHTML;
       if (parent.document.getElementById('dtz')) {
       if (parent.document.getElementById('dtz').innerHTML != '') {
       if (top.dtz == '') { alreadydone=false; top.dtz='</td><td style=vertical-align:top;width:80%;>' + parent.document.getElementById('dtz').innerHTML.replace('50%','100%') + '</td></tr></table>'; }
       if (top.ddtz == '') { top.ddtz='<table style=width:100%; cellpadding=10><tr><td style=vertical-align:top;>'; }
       }
       }
       if (parent.document.getElementById('aclock')) {
       if (parent.document.getElementById('aclock').innerHTML == '' && '' != '" . $space . "') {
       if (top.dtz != '') { parent.document.getElementById('dtz').innerHTML='';   }
       if (parent.document.getElementById('big_txt')) {  parent.document.getElementById('big_txt').innerHTML='';  }
       parent.document.getElementById('aclock').innerHTML=top.dprefix + '<iframe src=\"//www.rjmprogramming.com.au/HTMLCSS/colour_wheel.html" . $qm . "mode=" . urlencode($fgtz) . "&rand=' + Math.floor(Math.random() * 19876564) + '\" style=\"width:100%;height:800px;\"></iframe>' + top.dsuffix; 
       }
       }
       if (alreadydone) {
       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] : '');
       } else if (!alreadydone) { 
       parent.document.getElementById('ds').innerHTML=top.ddtz + '<iframe id=myif style=height:40px;display:inline-block; 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>' + top.dtz + '<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);
    }
  }
  
  function obit() {
   var thisvalue=document.getElementById('tz').value;  
   if (thisvalue.trim() == '') { var x=thisvalue; thisvalue='\$TZ' + x; } if (thisvalue.indexOf('/') != -1 || thisvalue.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(thisvalue);  } 
  }
  
  setTimeout(onl,1500);
  </script>";
if (isset($_GET['tz'])) {
//while (1) {
  //sleep(1);
  if (strpos($_SERVER['SERVER_NAME'], 'rjmprogramming.com.au') !== false) {
  if ($qm == "#") {
  echo "<!doctype html><html><body><p id=xds>" . exec("date") . "</p>" . $scr . "</body></html>";
  } else {
  echo "<!doctype html><html><body><p id=xds>" . exec("TZ=" . $fgtz . " date") . "</p>" . $scr . "</body></html>";
  }
  } else {
  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 {
$typefgtz="";
if (isset($_GET['typetz'])) {
  $typefgtz=str_replace("+"," ",urldecode($_GET['typetz']));
}
if ($typefgtz != "") {
echo "<!doctype html><html><head>" . $scr . "</head><body onload='setTimeout(obit,1800);'><div id=ds></div><br><div id=aclock></div><div id=dtz><input style='width:50%;display:inline-block;' onblur=\" obit(); \" type=text title='... or enter just a TimeZone place name' 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='" . $typefgtz . "'></input></div><input type=text style='position:absolute;left:-200px;top:-200px;' value=''></input><iframe title='TimeZone place current date and time' id=myif style=display:none; src=></iframe><div id=big_txt>TimeZone<br>Places<br><font size=4>RJM Programming</font><br>&nbsp;&nbsp;<font size=3>September, 2019</font></div>"; 
} else {
echo "<!doctype html><html><head>" . $scr . "</head><body><div id=ds></div><br><div id=aclock></div><div id=dtz><input style='width:50%;display:inline-block;' onblur=\" obit(); \" type=text title='... or enter just a TimeZone place name' 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></div><input type=text style='position:absolute;left:-200px;top:-200px;' value=''></input><iframe title='TimeZone place current date and time' id=myif style=display:none; src=></iframe><div id=big_txt>TimeZone<br>Places<br><font size=4>RJM Programming</font><br>&nbsp;&nbsp;<font size=3>September, 2019</font></div>"; 
}
}
} 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 or CEST Berlin (append space to concurrently open webpage) (else return): ");
if (strpos(trim($line), "/") === false && $line != "") {
      $oneword=explode(" ", trim($line));
      if (sizeof($oneword) == 1) {
      if ($oneword[0] != strtoupper($oneword[0])) {
       if (!file_exists($proposed)) { $proposed="../PHP/tz_places.php"; }
       if (file_exists($proposed)) {
        $tzcont=@file_get_contents($proposed);
        if (strpos(strtoupper($tzcont), "/" . strtoupper($oneword[0])) !== false) {
          $fbit=explode("/" . strtoupper($oneword[0]), strtoupper($tzcont));
          $lbit=explode('"', substr($tzcont, 0, strlen($fbit[0])))[-1 + sizeof(explode('"', substr($tzcont, 0, strlen($fbit[0]))))];
          if (trim($line) != $line) {
          $line=$lbit . explode('"', substr($tzcont, strlen($fbit[0])))[0] . " ";
          } else {
          $line=$lbit . explode('"', substr($tzcont, strlen($fbit[0])))[0] . " ";
          }
        }
       }
      }
      } else if ($oneword[0] != strtoupper($oneword[0])) {
       if (!file_exists($proposed)) { $proposed="../PHP/tz_places.php"; }
       if (file_exists($proposed)) {
        $tzcont=@file_get_contents($proposed);
        if (strpos(strtoupper($tzcont), "/" . strtoupper(  str_replace(" ", "_", trim($line))  )) !== false) {
          $fbit=explode( "/" . strtoupper(  str_replace(" ", "_", trim($line))  ), strtoupper($tzcont));
          $lbit=explode('"', substr($tzcont, 0, strlen($fbit[0])))[-1 + sizeof(explode('"', substr($tzcont, 0, strlen($fbit[0]))))];
          if (trim($line) != $line) {
          $line=$lbit . explode('"', substr($tzcont, strlen($fbit[0])))[0] . " ";
          } else {
          $line=$lbit . explode('"', substr($tzcont, strlen($fbit[0])))[0] . " ";
          }
        }
       }
      }  
} 
$pup=php_uname();   
 
if (strpos(trim($line), "/") !== false) {
  file_put_contents("stop_x1.x1", "");
  sleep(2);
  if (trim($line) != $line || strpos($pup, "Linux") !== false) {
  if (strpos($line, "/") !== false) {
  exec("TZ=" . $line . " ./x1 " . $line . " .");
  } else {
  exec("./x1 " . $line . " .");
  }
  }
  if (strpos($pup, "Linux") !== false) {  exec("lynx HTTP://www.rjmprogramming.com.au/Mac/get_tz.php?tz=" . urlencode($line)); exit(0);  }
} else {
  file_put_contents("stopx1.x1", "TZ=" . $line . " ./x1 " . $line);
  exec("./x1 " . $line . " .");
  if (strpos($pup, "Linux") !== false) {  exec("lynx HTTP://www.rjmprogramming.com.au/Mac/get_tz.php?tz=" . urlencode($line) . "%20");  }
  exit(1);
} 
}
?>
