<?php
// svg_clock.php
// SVG Network Clock
// Supervised by svg_clock.html
// Thanks to The PHP Anthology Volume II: Applications
header("Content-type: image/svg+xml");

$lri=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
$dri=["U+1F1E6","U+1F1E7","U+1F1E8","U+1F1E9","U+1F1EA","U+1F1EB","U+1F1EC","U+1F1ED","U+1F1EE","U+1F1EF","U+1F1F0","U+1F1F1","U+1F1F2","U+1F1F3","U+1F1F4","U+1F1F5","U+1F1F6","U+1F1F7","U+1F1F8","U+1F1F9","U+1F1FA","U+1F1FB","U+1F1FC","U+1F1FD","U+1F1FE","U+1F1FF"];
$flagentity="";	
$prebit='';
$postbit='';

echo '<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>';

if (isset($_GET['timezone'])) {
if (strpos(urldecode($_GET['timezone']), "localtime") !== false) {
$turl = explode("localtime", str_replace("+", " ", urldecode($_GET['timezone'])))[1];
if (isset($_GET['both']) || isset($_GET['analogue'])) {
  $turl.='</text><circle id="cclock" cx="200" cy="200" r="150" fill="navy"/><text>';
}
//echo $turl;
//exit;
} else {
$turl = str_replace('timezone=&', 'timezone=GMT&','cldate.php?timezone=' . urlencode($_GET['timezone']) . "&" . str_replace('timezone=','prtimezoneev=',str_replace('?','&',explode('?', explode('#', ('?' . ($_SERVER['QUERY_STRING'] . '?#')))[0])[1])));
}
} else {
$turl = str_replace('timezone=&', 'timezone=GMT&','cldate.php?timezone=' . urlencode($_GET['timezone']) . "&" . str_replace('timezone=','prtimezoneev=',str_replace('?','&',explode('?', explode('#', ('?' . ($_SERVER['QUERY_STRING'] . '?#')))[0])[1])));
}
?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" onload="init(evt);">
  <defs>
      <radialGradient id="navyGradient"
            cx="0.5" cy="0.5" r="0.5" fx="0.25" fy="0.25">
        <stop offset="0%" stop-color="navy"/>
        <stop offset="100%" stop-color="purple"/>
      </radialGradient>
      <radialGradient id="yellowGradient"
            cx="0.5" cy="0.5" r="0.5" fx="0.25" fy="0.25">
        <stop offset="0%" stop-color="yellow"/>
        <stop offset="100%" stop-color="orange"/>
      </radialGradient>
  </defs>

<script type="text/javascript">
<![CDATA[
var sclock, ssclock;
var cclock, cclocktitle;
var chour;
var cminute;
var csecond;
var theturl='<?php echo $turl; ?>';

// Thanks to https://www.jibbering.com/2002/5/dynamic-update-svg.html below
function HTTP() {
 var xmlhttp
 /*@cc_on @*/
 /*@if (@_jscript_version >= 5)
   try {
   xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
  } catch (e) {
   try {
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
   } catch (E) {
    xmlhttp=false
   }
  }
 @else
  xmlhttp=false
 @end @*/
 if (!xmlhttp) {
  try {
   xmlhttp = new XMLHttpRequest();
  } catch (e) {
   xmlhttp=false
  }
 }
 return xmlhttp
}

if (typeof getURL=='undefined') {
 getURL=function(url,fn) { 
  var xmlhttp=new HTTP();
  if (xmlhttp) {
   xmlhttp.open("GET",url,true);
   xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
     fn({status:xmlhttp.status,content:xmlhttp.responseText,
      contentType:xmlhttp.getResponseHeader("Content-Type")})
    }
   }
   xmlhttp.send()
  } else {
   //Some Appropriate Fallback...
  }
 }
}
if (typeof postURL=='undefined') {
 postURL=function(url,txt,fn,type,enc) {
  var xmlhttp=new HTTP();
  if (xmlhttp) {
   xmlhttp.open("POST",url,true);
   if (enc) xmlhttp.setRequestHeader("Content-Encoding",enc)
   if (type) xmlhttp.setRequestHeader("Content-Type",type)
   xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
     fn({status:xmlhttp.status,content:xmlhttp.responseText,
      contentType:xmlhttp.getResponseHeader("Content-Type")})
    }
   }
   xmlhttp.send(txt)
  } else {
   //Some Appropriate Fallback...
  }
 }
}

function init(evt) {
  if (window.svgDocument == null) {
    svgDocument = evt.target.ownerDocument;
  }
  ssclock = svgDocument.getElementById("sclock");
  sclock = svgDocument.getElementById("sclock").firstChild;
  cclock = svgDocument.getElementById("cclock");
  cclocktitle = svgDocument.getElementById("cclock").firstElementChild;
  chour = svgDocument.getElementById("chour");
  cminute = svgDocument.getElementById("cminute");
  csecond = svgDocument.getElementById("csecond");
  gettime();
}

function gettime() {
<?php
  if (strpos($turl, "cldate.php?timezone=") !== false) {
  echo "\n getURL('" . $turl . "', showtime); \n";
  } else {
  echo "\n showtime('" . str_replace(" GMT  "," GMT +",$turl) . "'); \n";
  } 
?>
}


function eachsecond(ints) {
  var his=eval('' + ints.split(':')[0]);
  var mis=eval('' + ints.split(':')[1]);
  var sis=eval('' + ints.split(':')[2]);
  var hang=eval(180 - eval(0 + Math.round(eval(eval(his % 12) + eval(mis / 60) + eval(sis / 3600)) * 30)) % 360);
  var mang=eval(180 - eval(0 + Math.round(eval(mis + eval(sis / 60)) * 6)) % 360);
  var sang=eval(180 - eval(0 + Math.round(sis * 6)) % 360);
  //top.document.title=ints + ' ' + his + ':' + mis + ':' + sis + ' ' + hang + ';' + mang + ';' + sang;
  var retps='', retds='';
  retps+=retds + Math.round(eval(200.0 + eval('' + Math.sin(hang * Math.PI / 180.0) * 100.0)));
  retds=',';
  retps+=retds + Math.round(eval(200.0 + eval('' + Math.cos(hang * Math.PI / 180.0) * 100.0)));
  retds=',';
  retps+=retds + Math.round(eval(200.0 + eval('' + Math.sin(mang * Math.PI / 180.0) * 140.0)));
  retds=',';
  retps+=retds + Math.round(eval(200.0 + eval('' + Math.cos(mang * Math.PI / 180.0) * 140.0)));
  retds=',';
  retps+=retds + Math.round(eval(200.0 + eval('' + Math.sin(sang * Math.PI / 180.0)) * 120.0));
  retds=',';
  retps+=retds + Math.round(eval(200.0 + eval('' + Math.cos(sang * Math.PI / 180.0)) * 120.0));
  return retps;
} 

function showtime(rsp) {
  var pref='', suff='', rspcontent;
  if (theturl.indexOf('cldate.php?timezone=') == -1) {
    rspcontent=rsp.replace(/\ GMT\ 0/g, ' GMT+0').replace(/\ GMT\ 1/g, ' GMT+1').replace(/\ GMT\ 2/g, ' GMT+2').replace(/\ GMT\ 3/g, ' GMT+3').replace(/\ GMT\ 4/g, ' GMT+4').replace(/\ GMT\ 5/g, ' GMT+5').replace(/\ GMT\ 6/g, ' GMT+6').replace(/\ GMT\ 7/g, ' GMT+7').replace(/\ GMT\ 8/g, ' GMT+8').replace(/\ GMT\ 9/g, ' GMT+9');
  } else {
    rspcontent=rsp.content;
  } 
  var tstmp=rspcontent.split('<')[0].split('http')[0];
  if (rspcontent.indexOf('<a ') != -1) {
    pref='<a ' + rspcontent.split('<a ')[1].split('>')[0] + '>';
    suff='</a>';
  } else if (rspcontent.indexOf('http') != -1) {
    //$midbit="<a xlink:href=\"http://www.timezoneconverter.com/cgi-bin/zoneinfo?tz=" . urlencode(str_replace("+","_",urldecode($_GET['timezone']))) . "\" target=\"_blank\">" . "http://www.timezoneconverter.com/cgi-bin/zoneinfo?tz=" . urlencode(str_replace("+","_",urldecode($_GET['timezone']))) . "</a>";
    pref='<a xlink:href="http' + rspcontent.split('http')[1].split('"')[0] + '" target="_blank">';
    suff='</a>';
  }
  //pref='';
  //suff='';
  var hmsbit='';
  if (rspcontent.indexOf('<circle') != -1) {
    cclocktitle.innerHTML=tstmp;
    hmsbit=eachsecond(tstmp.split(' ')[1]);
    if (eval(tstmp.split(' ')[1].split(':')[0]) >= 12) {
    cclock.style.fill = 'url(#navyGradient)'; // 'navy';
    chour.style.stroke = 'white';
    cminute.style.stroke = 'white';
    csecond.style.stroke = 'white';
    } else {
    cclock.style.fill = 'url(#yellowGradient)'; // 'yellow';
    chour.style.stroke = 'navy';
    cminute.style.stroke = 'navy';
    csecond.style.stroke = 'navy';
    }
    chour.setAttribute('x2', hmsbit.split(',')[0]);
    chour.setAttribute('y2', hmsbit.split(',')[1]);
    cminute.setAttribute('x2', hmsbit.split(',')[2]);
    cminute.setAttribute('y2', hmsbit.split(',')[3]);
    csecond.setAttribute('x2', hmsbit.split(',')[4]);
    csecond.setAttribute('y2', hmsbit.split(',')[5]);
  }
  if (document.URL.indexOf('analogue=') != -1) {
    sclock.data = ''; //rsp.content.split('<')[0];
  } else {
    if (pref != '') {
       sclock.data = tstmp + ' (DST link -+^)'; //rsp.content.split('<')[0];
       ssclock.setAttribute('class', 'Rrrrr'); 
       //ssclock.innerHTML = pref + sclock.innerHTML + suff; //rsp.content.split('<')[0];
    } else {
       sclock.data = tstmp; //rsp.content.split('<')[0];
       ssclock.setAttribute('class', 'Ggggg');
    }
  }
  setTimeout('gettime()', 1000);
}
]]>
</script>
<style>
    .small { font: italic 13px sans-serif; }
    .heavy { font: bold 30px sans-serif; }

    /* Note that the color of the text is set with the    *
     * fill property, the color property is for HTML only */
    .Rrrrr { font: italic 40px serif; fill: red; }
    .Ggggg { font: italic 40px serif; fill: green; }
</style>
<a title="Click for Colour Wheel time" onclick="window.open('//www.rjmprogramming.com.au/HTMLCSS/colour_wheel.html<?php $modebit=''; if (isset($_GET['timezone'])) { $modebit='?mode=' . str_replace(' ','_',str_replace('+','_',urldecode($_GET['timezone']))); } echo $modebit; ?>','_blank','left=600,top=30,width=700,height=800');"><text id="sclock" x="10" y="30" fill="green" class="Ggggg" text-anchor="left">
 Time goes here 
</text></a>
<g>
<a title="Click for Colour Wheel time" onclick="window.open('//www.rjmprogramming.com.au/HTMLCSS/colour_wheel.html<?php $modebit=''; if (isset($_GET['timezone'])) { $modebit='?mode=' . str_replace(' ','_',str_replace('+','_',urldecode($_GET['timezone']))); } echo $modebit; ?>','_blank','left=600,top=30,width=700,height=800');">
<circle id="cclock" cx="200" cy="200" r="150" fill="transparent">
<title></title>
</circle></a>
<line id="chour" x1="200" y1="200" x2="200" y2="100" style="stroke:transparent;stroke-width:3" />
<line id="cminute" x1="200" y1="200" x2="340" y2="200" style="stroke:transparent;stroke-width:2" />
<line id="csecond" x1="200" y1="200" x2="200" y2="320" style="stroke:transparent;stroke-width:1" />
</g>
</svg>



