<?php
 // daylight_saving_time.php
 // RJM Programming
 // September, 2015
 // Thanks to //gomashup.com and https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

 if (isset($_GET['justip'])) {
  echo "dst_" . str_replace(":", "-", $_SERVER['REMOTE_ADDR']) . "_0.html";
  for ($i=1; $i<=12; $i++) {
    if (!file_exists("dst_" . str_replace(":", "-", $_SERVER['REMOTE_ADDR']) . "_" . $i . ".html")) {
     file_put_contents("dst_" . str_replace(":", "-", $_SERVER['REMOTE_ADDR']) . "_" . $i . ".html", "");
    }
  }
  exit;
 }

 $mapsuffix=",%22A%20tooltip%20and%20clicking%20link%20for%20Google%20Map%20of%20%3Ca%20target=_blank%20href=%27https://www.google.com.au/maps/place/place1%27%3Eplace1%3C/a%3E%22%20,%27";
 $mapurl="//www.rjmprogramming.com.au/PHP/Map/map.php";
 //$mapurl="//localhost:8888/PHP/Map/map.php";
 $mapurl="/PHP/Map/map.php";
 $ourtitle=urlencode("Timezone Places");
 $ourlabel="[%27Lat%27,";
 //$ourvalue="%27Lon%27,%20%27Name%27]";
 $ourvalue="%27Lon%27,%27Name%27]";
 $ourlabel="['Lat',";
 //$ourvalue="'Lon', 'Name']";
 $ourvalue="'Lon','Name']";
 $ourdata="";
 $prefix=$mapurl . "?title=" . $ourtitle . "&label=" . $ourlabel . "&value=" . $ourvalue . "&data=";
 $clickall="";
 $tzi="";
 $place="";
 $region="";
 $toplace="";
 $tz="";
 if (isset($_GET['timezone'])) $tz=$_GET['timezone'];
 if (isset($_POST['timezone'])) $tz=$_POST['timezone'];
 if (isset($_GET['to'])) $toplace=$_GET['to'];
 if (isset($_POST['to'])) $toplace=$_POST['to'];
 if (isset($_GET['place'])) $place=$_GET['place'];
 if (isset($_POST['place'])) $place=$_POST['place'];
 if (isset($_GET['region'])) $region=$_GET['region'];
 if (isset($_POST['region'])) $region=$_POST['region'];
 
  
 if (isset($_GET['fnkey'])) {
   write_to_server_remote_addr($_GET['fnkey'], $tz . "," . $place . "," . $toplace . "," . $region);
   echo $tz . "," . $place . "," . $toplace . "," . $region;
   exit;
 }
 
 if (isset($_POST['fnkey'])) {
   write_to_server_remote_addr($_POST['fnkey'], $tz . "," . $place . "," . $toplace . "," . $region);
 }
 

 function write_to_server_remote_addr($inst, $csvline) {
   file_put_contents("dst_" . str_replace(":", "-", $_SERVER['REMOTE_ADDR']) . "_" . $inst . ".html", $csvline);
 }
 
 function dowbits() {
   $wcont="";
   if (file_exists("wbits.html")) {
     $wcont=@file_get_contents("wbits.html");
     $wcont=str_replace("−", "-", $wcont);
   } else {
     $overallextra="";
     if (file_exists("wgmt.geo")) {
     $wbits=@file_get_contents("wgmt.geo");
     $wbits=str_replace("−", "-", $wbits);
     } else {
     $wbits=@file_get_contents("https://en.wikipedia.org/wiki/List_of_tz_database_time_zones");
     $wbits=str_replace("−", "-", $wbits);
     }
     file_put_contents("wgmt.geo", $wbits);
     //$w30bits=explode(':30">', $wbits);
     $w30bits=explode('0</a></td>', $wbits);
     for ($iw=0; $iw<(-1 + sizeof($w30bits)); $iw+=1) { // used to be +=2
       if (strpos($w30bits[$iw], " title=") !== false) {
       $onebits=explode('</a>', $w30bits[$iw]);
       $suffixg="";
       $suffixd="";
       if (sizeof($onebits) > 1) {
        //$preonebits=explode('"UTC', $w30bits[1 + $iw]);
        $preonebits=explode('>', $w30bits[1 + $iw]);
        if (strpos($onebits[-1 + sizeof($onebits)], ":3") !== false) $suffixg=".5";
        if (strpos($preonebits[-1 + sizeof($preonebits)], ":3") !== false) $suffixd=".5";
        $xdst=str_replace("−0", "-", str_replace("+", "", str_replace("+0", "", str_replace("-0", "-", str_replace(":0", "", str_replace(":3", "", $preonebits[-1 + sizeof($preonebits)])))))) . $suffixd;
        $preonebits=explode('>', $onebits[-1 + sizeof($onebits)]);
        $xgmt=str_replace("−0", "-", str_replace("+", "", str_replace("+0", "", str_replace("-0", "-", str_replace(":0", "", str_replace(":3", "", $preonebits[-1 + sizeof($preonebits)])))))) . $suffixg;
        if (strpos($w30bits[1 + $iw], " title=") !== false) $xdst=$xgmt;
        $preonebits=explode('>', $onebits[-2 + sizeof($onebits)]);
        $vs=$preonebits[-1 + sizeof($preonebits)];
       }
       $onebits=explode('</td>', $w30bits[$iw]);
       if (sizeof($onebits) > 3) {
        $preonebits=explode('>', $onebits[-4 + sizeof($onebits)]);
        $latlong=$preonebits[-1 + sizeof($preonebits)];
        if (strpos($vs, "°") === false) $overallextra .= "\n" . ' "TimeZoneId ' . $latlong . display_latlong($latlong, $vs) . '" : "' . str_replace("@%@/@%@", "-", $vs) . '", "DST" : "' . $xdst . '", "GMT" : "' . $xgmt . '" ';
       }
       }
     }
     file_put_contents("wbits.html", $overallextra);
     $wcont=@file_get_contents("wbits.html");
   }
   return $wcont;
 }
 
 function display_latlong($inll, $vis) {
  global $mapsuffix, $clickall, $toplace, $place;
  $mapsplace=$vis;
  $visbits=explode("/", $vis);
  if (sizeof($visbits) == 3) {
    $mapsplace=$visbits[2] . "/" . $visbits[1] . "/" . $visbits[0];
  } else if (sizeof($visbits) > 1) {
    $mapsplace=substr($vis, (strlen($visbits[0]) + 1)) . "/" . $visbits[0];
  }
  $outll=str_replace("~", "", str_replace("~ ", "", str_replace("+", " ", str_replace("-", " -", "~" . $inll))));
  $obits=explode(" ", $outll);
  if (sizeof($obits) != 2) return "";
  $factor=1.0;
  if (strpos($obits[0], "-") !== false) $factor=-1.0;
  $latl=strlen(str_replace("-", "", $obits[0]));
  $latis=0.0;
  if ($latl >= 5) {
   $latis+=substr($obits[0], strlen($obits[0]) - 2, 2) / 3600.0;
   $latis+=substr($obits[0], strlen($obits[0]) - 4, 2) / 60.0;
   $latis+=str_replace("-", "", substr($obits[0], 0, strlen($obits[0]) - 4)); 
   $latis*=$factor;
  } else if ($latl >= 3) {
   $latis+=substr($obits[0], strlen($obits[0]) - 2, 2) / 60.0;
   $latis+=str_replace("-", "", substr($obits[0], 0, strlen($obits[0]) - 2)); 
   $latis*=$factor;
  } else {
   $latis+=str_replace("-", "", substr($obits[0], 0, strlen($obits[0]) - 0)); 
   $latis*=$factor;
  }
  $factor=1.0;
  if (strpos($obits[1], "-") !== false) $factor=-1.0;
  $longl=strlen(str_replace("-", "", $obits[1]));
  $longis=0.0;
  if ($longl >= 6) {
   $longis+=substr($obits[1], strlen($obits[1]) - 2, 2) / 3600.0;
   $longis+=substr($obits[1], strlen($obits[1]) - 4, 2) / 60.0;
   $longis+=str_replace("-", "", substr($obits[1], 0, strlen($obits[1]) - 4)); 
   $longis*=$factor;
  } else if ($longl >= 4) {
   $longis+=substr($obits[1], strlen($obits) - 2, 2) / 60.0;
   $longis+=str_replace("-", "", substr($obits[1], 0, strlen($obits[1]) - 2)); 
   $longis*=$factor;
  } else {
   $longis+=str_replace("-", "", substr($obits[1], 0, strlen($obits[1]) - 0)); 
   $longis*=$factor;
  }
  if ($place != "" && $toplace != "") {
    return " ,[" . str_replace("3333333", "33", str_replace("6666667", "66", $latis)) . "," . str_replace("3333333", "33", str_replace("6666667", "66", $longis)) . str_replace("place1", str_replace(" ", "%20", $mapsplace), $mapsuffix) . str_replace(" ", "%20", $vis) . "~]";
  } else {
    return " ,[" . str_replace("3333333", "33", str_replace("6666667", "66", $latis)) . "," . str_replace("3333333", "33", str_replace("6666667", "66", $longis)) . ",~" . $vis . "~]";
  }
 } 
  
 if (isset($_GET['url'])) {
   $prebits=explode("&", urldecode($_GET['url']));
   $bits=explode("/", $prebits[0]);
   $zonematch=str_replace("−", "-", $bits[-1 + sizeof($bits)]);
   
   if (file_exists("wbits.html")) {
   $acontobodyinnerHTML=@file_get_contents("wbits.html");
   } else if (11 == 11) {
   $acontobodyinnerHTML=dowbits();
   } else {
   
   if (file_exists("gmt" . $bits[-1 + sizeof($bits)] . ".xxx")) {
   $acontobodyinnerHTML=@file_get_contents("gmt" . $bits[-1 + sizeof($bits)] . ".xxx");
   } else if (file_exists("gmt1.xxx") && $bits[-1 + sizeof($bits)] == "") { //  && ($region != "" || $place != "")) {
   $acontobodyinnerHTML="";   
   foreach (glob("gmt*.xxx") as $filename) {
   $ac=@file_get_contents($filename);
   if (strpos($ac, " : ") !== false) $acontobodyinnerHTML.=$ac;
   }
   if (($region == "" && $place == "")) $place="-";
   } else {
   $acontobodyinnerHTML=@file_get_contents(str_replace('@$@','',str_replace('@$@//','//','@$@' . urldecode($_GET['url']))));
   }
   
   
   }
   
   $datacnt=1;
   $pretablebit="<table><tr><td valign='top'>";
   //$formbit="</td>" . "<td rowspan=" . $datacnt . " id=gmh style=display:block;><form target=mapif action=" . $mapurl . " method=POST id=myform><input type=hidden id=title name=title value='" . urldecode($ourtitle) . "'></input><input type=hidden id=label name=label " . " value=\"['Lat','\" " . "></input><input type=hidden id=value name=value " . " value=\"'Lon', 'Name']\" " . "></input><textarea style=display:none; name=data id=data row=56 col=50></textarea><input " . ' onclick="document.getElementById(' . "'mapif'" . ').style.display=' . "'inline'" . ';" style="display:none;" type="submit" id="mapsubmit" value="Submit"></input></form><span id="thgm"><b>Optional Google Map May Go Below</b></span><br><iframe style="border:6px solid teal;display:none;width:430px;height:400px;" src="' . $mapurl . "?title=Timezone+Places&label=['Lat'%2C&value='Lon'%2C+'Name']&data=%2C[12.65%2C-8%2C~Africa%2FBamako~]%2C[-6.8%2C39.28333%2C~Africa%2FDar_es_Salaam~]" . '"' . ' id="mapif" name="mapif"></iframe><br></td>';
   //if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mobile') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'android')) {
   //$formbit="</td>" . "<td rowspan=" . $datacnt . " id=gmh style=display:block;><form target=mapif action=" . $mapurl . " method=POST id=myform><input type=hidden id=title name=title value='" . urldecode($ourtitle) . "'></input><input type=hidden id=label name=label " . " value=\"['Lat','\" " . "></input><input type=hidden id=value name=value " . " value=\"'Lon','Name']\" " . "></input><textarea style=display:none; name=data id=data row=56 col=50></textarea><input " . ' onclick="document.getElementById(' . "'mapif'" . ').style.display=' . "'inline'" . ';" style="display:none;" type="submit" id="mapsubmit" value="Submit"></input></form><span id="thgm"><b>Optional Google Map May Go Below</b></span><br><iframe style="border:6px solid teal;display:none;width:430px;height:400px;" sandbox="allow-top-navigation allow-same-origin allow-scripts allow-popups allow-forms" data-srcdoc="" src="' . $mapurl . "?title=Timezone+Places&label=['Lat'%2C&value='Lon'%2C'Name']&data=%2C[12.65%2C-8%2C~Africa%2FBamako~]%2C[-6.8%2C39.28333%2C~Africa%2FDar_es_Salaam~]" . '"' . ' id="mapif" name="mapif"></iframe><br></td>';
   //} else {
   $formbit="</td>" . "<td rowspan=" . $datacnt . " id=gmh style=display:block;><form target=mapif action=" . $mapurl . " method=POST id=myform><input type=hidden id=title name=title value='" . urldecode($ourtitle) . "'></input><input type=hidden id=label name=label " . " value=\"['Lat','\" " . "></input><input type=hidden id=value name=value " . " value=\"'Lon','Name']\" " . "></input><textarea style=display:none; name=data id=data row=56 col=50></textarea><input " . ' onclick="document.getElementById(' . "'mapif'" . ').style.display=' . "'inline'" . ';" style="display:none;" type="submit" id="mapsubmit" value="Submit"></input></form><span id="thgm"><b>Optional Google Map May Go Below</b></span><br><iframe style="border:6px solid teal;display:none;width:430px;height:400px;" sandbox="allow-top-navigation-by-user-activation allow-same-origin allow-scripts allow-popups allow-forms" data-srcdoc="" src="' . $mapurl . "?title=Timezone+Places&label=['Lat'%2C&value='Lon'%2C'Name']&data=%2C[12.65%2C-8%2C~Africa%2FBamako~]%2C[-6.8%2C39.28333%2C~Africa%2FDar_es_Salaam~]" . '"' . ' id="mapif" name="mapif"></iframe><br></td>';
   //}
   $posttablebit="</td></tr></table>";
   $mapbit=""; //<th id='thgm'>Optional Google Map May Go Below</th>";
   $tablestuff="<table cellpadding=5 cellspacing=5><thead><tr><th>Time Zone ID</th><th>Daylight Saving Time (<i>Now</i>)</th><th>Greenwich Mean Time(zone)</th>" . $mapbit . "</tr></thead><tbody></tbody></table>";

   $disp=true;
   $stuff=explode(" : ", $acontobodyinnerHTML);
   if (sizeof($stuff) > 1) {
         for ($i=1; $i<sizeof($stuff); $i+=3) {
           $thistitle="";
           $dqstuff=explode('"', $stuff[$i]);
           $tzi=$dqstuff[1];
           if (strpos($stuff[-1 + $i], " ,[") !== false) {
             $dqstuff=explode(" ,[", $stuff[-1 + $i]);
             if (($place != "" && $toplace != "") || strpos($dqstuff[-1 + sizeof($dqstuff)], ",%22A") === false) {
              if ($place != "" && $toplace != "" && strpos($dqstuff[-1 + sizeof($dqstuff)], ",%22A") === false) {
  $mapsplace=$tzi;
  $visbits=explode("/", $tzi);
  if (sizeof($visbits) > 1) {
    $mapsplace=substr($tzi, (strlen($visbits[0]) + 1)) . "/" . $visbits[0];
  }
               $thistitle=",[" . str_replace('"', '', str_replace(",~", str_replace("place1", str_replace(" ", "%20", $mapsplace), $mapsuffix), $dqstuff[-1 + sizeof($dqstuff)]));
              } else {
               $thistitle=",[" . str_replace('"', '', $dqstuff[-1 + sizeof($dqstuff)]);
              }
             } else {
              $thistitle=",[" . str_replace('"', '', substr($dqstuff[-1 + sizeof($dqstuff)], 0, strpos($dqstuff[-1 + sizeof($dqstuff)], ",%22A")) . ",~" . str_replace(" ", "%20", $tzi) . "~]");
             }
           }      
           $dqstuff=explode('"', $stuff[(1 + $i)]);
           $dst=str_replace("−", "-", $dqstuff[1]);
           $dqstuff=explode('"', $stuff[(2 + $i)]);
           $gmt=str_replace("−", "-", $dqstuff[1]);
           $disp=true;
           $okay=true;
           $timestamp = time();
           if ($zonematch != "") {
             if (strpos(("~" . $gmt . "."), "~" . $zonematch . ".") === false) $disp=false;
           }
           if ($region != "") {
             if (strpos(("~" . $tzi), "~" . $region . "-") === false && strpos(("~" . $tzi), "~" . $region . "/") === false) $disp=false;
           }
           if ($place != "" && $toplace != "") {
             $clickall=' class="onload" ';
             if (strpos(strtolower(str_replace("_", " ", $tzi)), strtolower(str_replace("_", " ", $place))) === false && strpos(strtolower(str_replace("_", " ", $tzi)), strtolower(str_replace("_", " ", $toplace))) === false) $disp=false;
           } else if ($place != "") {
             if (strpos(strtolower(str_replace("_", " ", $tzi)), strtolower(str_replace("_", " ", $place))) === false) $disp=false;
           } else if ($toplace != "") {
             if (strpos(strtolower(str_replace("_", " ", $tzi)), strtolower(str_replace("_", " ", $toplace))) === false) $disp=false;
           }
           if ($disp) {
             // Thanks to //stackoverflow.com/questions/851574/how-do-i-get-greenwich-mean-time-in-php
             $okay = date_default_timezone_set(str_replace("-", "/", $tzi));
             $dr = date('r', $timestamp);
             $drs = explode(" +", str_replace("+0", "+", str_replace("30", ".5", str_replace("00", "", str_replace("000", "0", $dr)))));
             if (sizeof($drs) == 1) {
              $drs = explode(" -", str_replace("-0", "-", str_replace("30", ".5", str_replace("00", "", str_replace("000", "0", $dr)))));
              if (sizeof($drs) == 2) $drs[1]="-" . $drs[1];
             }
             if (sizeof($drs) == 2) {
              if ($drs[1] == "") $drs[1] = "0";
             }
            
             if (!$okay && $firstgood != "") {
              $dst = $gmt;
              $dr = $firstgood;
             }
             
             if ($okay && sizeof($drs) == 2) $dst = $drs[1]; 

             if ($dst != $gmt) {
              //if ($okay && sizeof($drs) == 2) $dst = $drs[1]; 
   $datacnt++;
              //$tablestuff=str_replace("</tbody>", "<tr><td><a title='" . $thistitle . "' href='#" . $tzi . "' id='" . $tzi . "' onclick=' lasttitle=this.title; lastid=this.id; isok=prerepos(); if (isok == 0) { wois=window.open(\"//www.timezoneconverter.com/cgi-bin/zoneinfo?tz=" . str_replace("-", "/", $tzi) . "\",\"_blank\",\"top=50,left=50,width=700,height=800\"); } setTimeout(repos,2000);  '>" . $tzi . "</a></td><td><b><font color=blue>" . $dst . "</font></b>    (<i>" . $dr . "</i>)</td><td>" . $gmt . "</td>" . $formbit . "</tr></tbody>", $tablestuff);
   if ($datacnt >= 3 && $clickall != "" && strpos(strtolower(str_replace("_", " ", $tzi)), strtolower(str_replace("_", " ", $place))) !== false) {
              $tablestuff=str_replace("<tbody>", "<tbody><tr><td><a" . $clickall . " title='" . $thistitle . "' href='#" . $tzi . "' id='" . $tzi . "' onclick=' lasttitle=this.title; lastid=this.id; isok=prerepos(); if (isok == 0 || gotforced) { wois=window.open(\"HTTP://www.timezoneconverter.com/cgi-bin/zoneinfo?tz=" . str_replace("-", "/", $tzi) . "\",\"_blank\",\"top=50,left=50,width=700,height=800\"); } setTimeout(repos,2000);  '>" . $tzi . "</a></td><td><b><font color=blue>" . $dst . "</font></b>    (<i>" . $dr . "</i>)</td><td>" . $gmt . "</td></tr>", $tablestuff);
   } else {
              $tablestuff=str_replace("</tbody></table>", "<tr><td><a" . $clickall . " title='" . $thistitle . "' href='#" . $tzi . "' id='" . $tzi . "' onclick=' lasttitle=this.title; lastid=this.id; isok=prerepos(); if (isok == 0 || gotforced) { wois=window.open(\"HTTP://www.timezoneconverter.com/cgi-bin/zoneinfo?tz=" . str_replace("-", "/", $tzi) . "\",\"_blank\",\"top=50,left=50,width=700,height=800\"); } setTimeout(repos,2000);  '>" . $tzi . "</a></td><td><b><font color=blue>" . $dst . "</font></b>    (<i>" . $dr . "</i>)</td><td>" . $gmt . "</td></tr></tbody></table>" . $formbit . $posttablebit, $tablestuff);
   }
              $tablestuff=str_replace("<table ", $pretablebit . "<table ", $tablestuff);
              $formbit="";
              $pretablebit="";
              $posttablebit="";
             } else {
              //if ($okay && sizeof($drs) == 2) $dst = $drs[1];
              if ($okay && $dst == $gmt && $firstgood == "") $firstgood = $dr;
   $datacnt++;
   if ($datacnt >= 3 && $clickall != "" && strpos(strtolower(str_replace("_", " ", $tzi)), strtolower(str_replace("_", " ", $place))) !== false) {
              $tablestuff=str_replace("<tbody>", "<tbody><tr><td><a" . $clickall . " title='" . $thistitle . "' href='#" . $tzi . "' id='" . $tzi . "' onclick=' lasttitle=this.title; lastid=this.id; isok=prerepos(); if (isok == 0 || gotforced) { wois=window.open(\"HTTP://www.timezoneconverter.com/cgi-bin/zoneinfo?tz=" . str_replace("-", "/", $tzi) . "\",\"_blank\",\"top=50,left=50,width=700,height=800\");  } setTimeout(repos,2000); '>" . $tzi . "</a></td><td>" . $dst . "    (<i>" . $dr . "</i>)</td><td>" . $gmt . "</td></tr>", $tablestuff);
   } else {
              $tablestuff=str_replace("</tbody></table>", "<tr><td><a" . $clickall . " title='" . $thistitle . "' href='#" . $tzi . "' id='" . $tzi . "' onclick=' lasttitle=this.title; lastid=this.id; isok=prerepos(); if (isok == 0 || gotforced) { wois=window.open(\"HTTP://www.timezoneconverter.com/cgi-bin/zoneinfo?tz=" . str_replace("-", "/", $tzi) . "\",\"_blank\",\"top=50,left=50,width=700,height=800\");  } setTimeout(repos,2000); '>" . $tzi . "</a></td><td>" . $dst . "    (<i>" . $dr . "</i>)</td><td>" . $gmt . "</td></tr></tbody></table>" . $formbit . $posttablebit, $tablestuff);
   }
              $tablestuff=str_replace("<table ", $pretablebit . "<table ", $tablestuff);
              $formbit="";
              $pretablebit="";
              $posttablebit="";
              }
           }
         }
         $retstuff='<html><head><style> td { text-align: center; } </style><scri' . 'pt> var rt="' . str_replace("+", " ", urlencode(str_replace(" rowspan=1 ", " rowspan=" . $datacnt . " ", $tablestuff))) . '"; </scr' . 'ipt></head><body onload=" parent.document.getElementById(' . "'result'" . ').innerHTML=decodeURIComponent(rt); "></body></html>';
   }
 }
 echo $retstuff;
?>
