<?php
// domain.php - RJM Programming - October, 2014
// Ask about country of origin of a URL via middleman Tcl

$defc = "us";
if (isset($_GET['read'])) {
  echo "<html><head><title>domain.php - RJM Programming - October, 2014</title></head><body style='background-color:yellow;'><p align='center'>";
  //echo file_get_contents(dirname(__FILE__) . "/domain.html") . " is country of origin of " . urldecode($_GET['inurl']);
  echo urldecode($_GET['read']) . " is country of origin of " . urldecode($_GET['inurl']);
  echo "</p><br><br><h1 align='center'>Country of Origin of URL</h1><br><br><div  align='center'><form align='center' method='GET' action='./domain.php'>URL: <input type='text' name='inurl' id='inurl'></input><br><br><input type='submit' name='submit' value='Find Country of Origin' id='submit'></input></form></div></body></html>";
} else if (isset($_GET['inurl'])) {
  $urlparts = explode("://", urldecode($_GET['inurl']));
  $andthenparts = explode("/", $urlparts[sizeof($urlparts) - 1]);
  $dotparts = explode(".", $andthenparts[0]);
  $defc = $dotparts[sizeof($dotparts) - 1];
  exec("tclsh " . dirname(__FILE__) . "/domain.tcl " . $defc . " > " . dirname(__FILE__) . "/domain.html");
  header("Location: ./domain.php?read=" . urlencode(file_get_contents(dirname(__FILE__) . "/domain.html"))  . "&inurl=" . $_GET['inurl']);
  exit;
} else {
  echo "<html><head><title>domain.php - RJM Programming - October, 2014</title></head><body style='background-color:yellow;'><p align='center'>";
  echo "</p><br><br><h1 align='center'>Country of Origin of URL</h1><br><br><div  align='center'><form align='center' method='GET' action='./domain.php'>URL: <input type='text' name='inurl' id='inurl'></input><br><br><input type='submit' name='submit' value='Find Country of Origin' id='submit'></input></form></div></body></html>";
}
?>