<?php
// PHP Linux diff file differences ... RJM Programming November 2013
$called="";
$fillinone="http://";
$fillintwo="http://";
$onec="";
$twoc="";
$suffix="";
$iframebits="";
$onloadbit="";
$suffid="_" . rand();

function ourfile_get_contents($inuis) {
  if (strpos(("~" . $inuis), "~//") !== false) {
   return file_get_contents("http:" . $inuis);
  } 
  return file_get_contents($inuis);
}

if (isset($_GET['one']) && !isset($_GET['two'])) {
  if (str_replace("--GETME", "", $_GET['one']) != $_GET['one']) {
  $called="y";
  $iframebits="<h1 id=latest_file" . $suffid . ">Latest file ... <a href=#differences" . $suffid . "  title=Differences>Differences</a> below this ... " . $_GET['one'] . "</h1><br><details open><summary></summary><iframe style='background-color:yellow;width:100%;height:300px;' height=300 src='one.one' title='" . $_GET['one'] . "'></iframe></details><br>";
  $iframebits.="<h1 id=differences" . $suffid . ">Differences <a href=#latest_file" . $suffid . "  title=Latest>^</a> <a href=#older_file" . $suffid . "  title=Older>v</a></h1><br><details open><summary></summary><iframe style='background-color:pink;width:100%;height:300px;' height=300 src='huh.huh' title='Differences between " . $_GET['one'] . " and " . str_replace("--GETME", "-GETME", $_GET['one']) . "'></iframe></details><br>";
  $iframebits.="<h1 id=older_file" . $suffid . ">Older file ... <a href=#differences" . $suffid . "  title=Differences>Differences</a> just above ... " . str_replace("--GETME", "-GETME", $_GET['one']) . "</h1><br><details open><summary></summary><iframe style='background-color:lightgreen;width:100%;height:300px;' height=300 src='two.two' title='" . str_replace("--GETME", "-GETME", $_GET['one']) . "'></iframe></details><br>";
  $suffix = " file(s) not found";
  if (str_replace("GETME", "", $_GET['one']) != $_GET['one']) {
    $suffix="";
    $onec = ourfile_get_contents($_GET['one']);
    if (str_replace(" ","",$onec) == "" || strpos($onec, "No newline" . " at end of file") !== false) $suffix = " " . $_GET['one'] . " not found";
    $twoc = ourfile_get_contents(str_replace("--GETME", "-GETME", $_GET['one']));
    if (str_replace(" ","",$twoc) == "" || strpos($twoc, "No newline" . " at end of file") !== false) $suffix .= " " . str_replace("--GETME", "-GETME", $_GET['one']) . " not found";
  } else {
    $suffix = " sorry, no can do";
  }
  } else if (str_replace("-GETME", "", $_GET['one']) != $_GET['one']) {
  $called="y";
  $iframebits="<h1 id=latest_file" . $suffid . ">Latest file ... <a href=#differences" . $suffid . "  title=Differences>Differences</a> below this ... " . $_GET['one'] . "</h1><br><details open><summary></summary><iframe style='background-color:yellow;width:100%;height:300px;' height=300 src='one.one' title='" . $_GET['one'] . "'></iframe></details><br>";
  $iframebits.="<h1 id=differences" . $suffid . ">Differences <a href=#latest_file" . $suffid . "  title=Latest>^</a> <a href=#older_file" . $suffid . "  title=Older>v</a></h1><br><details open><summary></summary><iframe style='background-color:pink;width:100%;height:300px;' height=300 src='huh.huh' title='Differences between " . $_GET['one'] . " and " . str_replace("-GETME", "_GETME", $_GET['one']) . "'></iframe></details><br>";
  $iframebits.="<h1 id=older_file" . $suffid . ">Older file ... <a href=#differences" . $suffid . "  title=Differences>Differences</a> just above ... " . str_replace("-GETME", "_GETME", $_GET['one']) . "</h1><br><details open><summary></summary><iframe style='background-color:lightgreen;width:100%;height:300px;' height=300 src='two.two' title='" . str_replace("-GETME", "_GETME", $_GET['one']) . "'></iframe></details><br>";
  $suffix = " file(s) not found";
  if (str_replace("GETME", "", $_GET['one']) != $_GET['one']) {
    $suffix="";
    $onec = ourfile_get_contents($_GET['one']);
    if (str_replace(" ","",$onec) == "" || strpos($onec, "No newline" . " at end of file") !== false) $suffix = " " . $_GET['one'] . " not found";
    $twoc = ourfile_get_contents(str_replace("-GETME", "_GETME", $_GET['one']));
    if (str_replace(" ","",$twoc) == "" || strpos($twoc, "No newline" . " at end of file") !== false) $suffix .= " " . str_replace("-GETME", "_GETME", $_GET['one']) . " not found";
  } else {
    $suffix = " sorry, no can do";
  }
  } else if (str_replace("GETME", "", $_GET['one']) != $_GET['one']) {
    $fillinone=$_GET['one'];
  }
} else if (isset($_GET['one']) && isset($_GET['two'])) {
  if ($_GET['one'] != "" && $_GET['two'] != "" && $_GET['one'] != $_GET['two'] && str_replace("GETME","",str_replace("_","",str_replace("-","",$_GET['one']))) == str_replace("GETME","",str_replace("_","",str_replace("-","",$_GET['two'])))) {
  if (str_replace("GETME", "", $_GET['one']) != $_GET['one'] && str_replace("GETME", "", $_GET['two']) != $_GET['two']) {
  $xprefix="";
  $xsuffix="";
  } else if (str_replace("GETME", "", $_GET['one']) != $_GET['one'] || str_replace("GETME", "", $_GET['two']) != $_GET['two']) {
  $xprefix="<div id='diffdiv' style='display:none;'>";
  $xsuffix="</div>";
  $onloadbit=" onload=\" var huh=prompt('Password? (Hint: Why is a duck?)','');  if (huh != null && huh == 'BecausE IT FlieS SO HigH') { document.getElementById('diffdiv').style.display='block';  } else { document.getElementById('diffdiv').innerHTML='';  } \"";
  $prevg="";
  if (str_replace("GETME", "", $_GET['one']) == $_GET['one']) {
    if ("" != @ourfile_get_contents(str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $_GET['two']))))) {
      $prevg=str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $_GET['two'])));
      while ($prevg != "") {
       $_GET['one']=$prevg;
       if ("" != ourfile_get_contents(str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $prevg))))) {
        $prevg=str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $prevg)));
       } else {
        $prevg="";
       }
      }
      $xprefix="";
      $xsuffix="";
      $onloadbit="";
    }
  } else {
    if ("" != @ourfile_get_contents(str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $_GET['one']))))) {
      $prevg=str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $_GET['one'])));
      while ($prevg != "") {
       $_GET['two']=$prevg;
       if ("" != ourfile_get_contents(str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $prevg))))) {
        $prevg=str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $prevg)));
       } else {
        $prevg="";
       }
      }
      $xprefix="";
      $xsuffix="";
      $onloadbit="";
    }
  }
  }
  $called="y";
  $iframebits=$xprefix . "<h1 id=latest_file" . $suffid . ">Latest file ... <a href=#differences" . $suffid . "  title=Differences>Differences</a> below this " . $_GET['one'] . "</h1><br><details open><summary></summary><iframe style='background-color:yellow;width:100%;height:300px;' height=300 src='one.one' title='" . $_GET['one'] . "'></iframe></details><br>";
  $iframebits.="<h1 id=differences" . $suffid . ">Differences <a href=#latest_file" . $suffid . "  title=Latest>^</a> <a href=#older_file" . $suffid . "  title=Older>v</a></h1><br><details open><summary></summary><iframe style='background-color:pink;width:100%;height:300px;' height=300 src='huh.huh' title='Differences between " . $_GET['one'] . " and " . $_GET['two'] . "'></iframe></details><br>";
  $iframebits.="<h1 id=older_file" . $suffid . ">Older file ... <a href=#differences" . $suffid . "  title=Differences>Differences</a> just above " . $_GET['two'] . "</h1><br><details open><summary></summary><iframe style='background-color:lightgreen;width:100%;height:300px;' height=300 src='two.two' title='" . $_GET['two'] . "'></iframe></details><br>" . $xsuffix;
  }
  $suffix = " file(s) not found";
  if (str_replace("GETME", "", $_GET['one']) != $_GET['one'] || str_replace("GETME", "", $_GET['two']) != $_GET['two']) {
    $suffix="";
    $onec = ourfile_get_contents($_GET['one']);
    if (str_replace(" ","",$onec) == "" || strpos($onec, "No newline" . " at end of file") !== false) $suffix = " " . $_GET['one'] . " not found";
    $twoc = ourfile_get_contents($_GET['two']);
    if (str_replace(" ","",$twoc) == "" || strpos($twoc, "No newline" . " at end of file") !== false) $suffix .= " " . $_GET['two'] . " not found";
  } else {
    $suffix = " sorry, no can do";
  }
} else if (isset($_POST['one']) && !isset($_POST['two'])) {
  if (str_replace("--GETME", "", $_POST['one']) != $_POST['one']) {
  $called="y";
  $iframebits="<h1 id=latest_file" . $suffid . ">Latest file ... <a href=#differences" . $suffid . "  title=Differences>Differences</a> below this" . $_POST['one'] . "</h1><br><details open><summary></summary><iframe style='background-color:yellow;width:100%;height:300px;' height=300 src='one.one' title='" . $_POST['one'] . "'></iframe></details><br>";
  $iframebits.="<h1 id=differences" . $suffid . ">Differences <a href=#latest_file" . $suffid . "  title=Latest>^</a> <a href=#older_file" . $suffid . "  title=Older>v</a></h1><br><details open><summary></summary><iframe style='background-color:pink;width:100%;height:300px;' height=300 src='huh.huh' title='Differences between " . $_POST['one'] . " and " . str_replace("--GETME", "-GETME", $_POST['one']) . "'></iframe></details><br>";
  $iframebits.="<h1 id=older_file" . $suffid . ">Older file ... <a href=#differences" . $suffid . "  title=Differences>Differences</a> just above" . str_replace("--GETME", "-GETME", $_POST['one']) . "</h1><br><details open><summary></summary><iframe style='background-color:lightgreen;width:100%;height:300px;' height=300 src='two.two' title='" . str_replace("--GETME", "-GETME", $_POST['one']) . "'></iframe></details><br>";
  $suffix = " file(s) not found";
  if (str_replace("GETME", "", $_POST['one']) != $_POST['one']) {
    $suffix="";
    $onec = ourfile_get_contents($_POST['one']);
    if (str_replace(" ","",$onec) == "" || strpos($onec, "No newline" . " at end of file") !== false) $suffix = " " . $_POST['one'] . " not found";
    $twoc = ourfile_get_contents(str_replace("--GETME", "-GETME", $_POST['one']));
    if (str_replace(" ","",$twoc) == "" || strpos($twoc, "No newline" . " at end of file") !== false) $suffix .= " " . str_replace("--GETME", "-GETME", $_POST['one']) . " not found";
  } else {
    $suffix = " sorry, no can do";
  }
  } else if (str_replace("-GETME", "", $_POST['one']) != $_POST['one']) {
  $called="y";
  $iframebits="<h1 id=latest_file" . $suffid . ">Latest file ... <a href=#differences" . $suffid . "  title=Differences>Differences</a> below this" . $_POST['one'] . "</h1><br><details open><summary></summary><iframe style='background-color:yellow;width:100%;height:300px;' height=300 src='one.one' title='" . $_POST['one'] . "'></iframe></details><br>";
  $iframebits.="<h1 id=differences" . $suffid . ">Differences <a href=#latest_file" . $suffid . "  title=Latest>^</a> <a href=#older_file" . $suffid . "  title=Older>v</a></h1><br><details open><summary></summary><iframe style='background-color:pink;width:100%;height:300px;' height=300 src='huh.huh' title='Differences between " . $_POST['one'] . " and " . str_replace("-GETME", "_GETME", $_POST['one']) . "'></iframe></details><br>";
  $iframebits.="<h1 id=older_file" . $suffid . ">Older file ... <a href=#differences" . $suffid . "  title=Differences>Differences</a> just above" . str_replace("-GETME", "_GETME", $_POST['one']) . "</h1><br><details open><summary></summary><iframe style='background-color:lightgreen;width:100%;height:300px;' height=300 src='two.two' title='" . str_replace("-GETME", "_GETME", $_POST['one']) . "'></iframe></details><br>";
  $suffix = " file(s) not found";
  if (str_replace("GETME", "", $_POST['one']) != $_POST['one']) {
    $suffix="";
    $onec = ourfile_get_contents($_POST['one']);
    if (str_replace(" ","",$onec) == "" || strpos($onec, "No newline" . " at end of file") !== false) $suffix = " " . $_POST['one'] . " not found";
    $twoc = ourfile_get_contents(str_replace("-GETME", "_GETME", $_POST['one']));
    if (str_replace(" ","",$twoc) == "" || strpos($twoc, "No newline" . " at end of file") !== false) $suffix .= " " . str_replace("-GETME", "_GETME", $_POST['one']) . " not found";
  } else {
    $suffix = " sorry, no can do";
  }
  } else if (str_replace("GETME", "", $_POST['one']) != $_POST['one']) {
    $fillinone=$_POST['one'];
  }
} else if (isset($_POST['one']) && isset($_POST['two'])) {
  $suffix = " file(s) not found";
  if ($_POST['one'] != "" && $_POST['two'] != "" && $_POST['one'] != $_POST['two'] && str_replace("GETME","",str_replace("_","",str_replace("-","",$_POST['one']))) == str_replace("GETME","",str_replace("_","",str_replace("-","",$_POST['two'])))) {
  if (str_replace("GETME", "", $_POST['one']) != $_POST['one'] && str_replace("GETME", "", $_POST['two']) != $_POST['two']) {
  $xprefix="";
  $xsuffix="";
  } else if (str_replace("GETME", "", $_POST['one']) != $_POST['one'] || str_replace("GETME", "", $_POST['two']) != $_POST['two']) {
  $xprefix="<div id='diffdiv' style='display:none;'>";
  $xsuffix="</div>";
  $onloadbit=" onload=\" var huh=prompt('Password? (Hint: Why is a duck?)','');  if (huh != null && huh == 'BecausE IT FlieS SO HigH') { document.getElementById('diffdiv').style.display='block';  } else { document.getElementById('diffdiv').innerHTML='';  }  \"";
  $prevg="";
  if (str_replace("GETME", "", $_POST['one']) == $_POST['one']) {
    if ("" != @ourfile_get_contents(str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $_POST['two']))))) {
      $prevg=str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $_POST['two'])));
      while ($prevg != "") {
       $_POST['one']=$prevg;
       if ("" != @ourfile_get_contents(str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $prevg))))) {
        $prevg=str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $prevg)));
       } else {
        $prevg="";
       }
      }
      $xprefix="";
      $xsuffix="";
      $onloadbit="";
    }
  } else {
    if ("" != ourfile_get_contents(str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $_POST['one']))))) {
      $prevg=str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $_POST['one'])));
      while ($prevg != "") {
       $_POST['two']=$prevg;
       if ("" != ourfile_get_contents(str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $prevg))))) {
        $prevg=str_replace("GET~ME","GETME",str_replace("_GETME", "-GET~ME", str_replace("-GETME", "--GET~ME", $prevg)));
       } else {
        $prevg="";
       }
      }
      $xprefix="";
      $xsuffix="";
      $onloadbit="";
    }
  }
  }
  $called="y";
  $iframebits=$xprefix . "<h1 id=latest_file" . $suffid . ">Latest file ... <a href=#differences" . $suffid . "  title=Differences>Differences</a> below this " . $_POST['one'] . "</h1><br><details open><summary></summary><iframe style='background-color:yellow;width:100%;height:300px;' height=300 src='one.one' title='" . $_POST['one'] . "'></iframe></details><br>";
  $iframebits.="<h1 id=differences" . $suffid . ">Differences <a href=#latest_file" . $suffid . "  title=Latest>^</a> <a href=#older_file" . $suffid . "  title=Older>v</a></h1><br><details open><summary></summary><iframe style='background-color:pink;width:100%;height:300px;' height=300 src='huh.huh' title='Differences between " . $_POST['one'] . " and " . $_POST['two'] . "'></iframe></details><br>";
  $iframebits.="<h1 id=older_file" . $suffid . ">Older file ... <a href=#differences" . $suffid . "  title=Differences>Differences</a> just above " . $_POST['two'] . "</h1><br><details open><summary></summary><iframe style='background-color:lightgreen;width:100%;height:300px;' height=300 src='two.two' title='" . $_POST['two'] . "'></iframe></details><br>" . $xsuffix;
  }
  if (str_replace("GETME", "", $_POST['one']) != $_POST['one'] || str_replace("GETME", "", $_POST['two']) != $_POST['two']) {
    $suffix="";
    $onec = ourfile_get_contents($_POST['one']);
    if (str_replace(" ","",$onec) == "" || strpos($onec, "No newline" . " at end of file") !== false) $suffix = " " . $_POST['one'] . " not found";
    $twoc = ourfile_get_contents($_POST['two']);
    if (str_replace(" ","",$twoc) == "" || strpos($twoc, "No newline" . " at end of file") !== false) $suffix .= " " . $_POST['two'] . " not found";
  } else {
    $suffix = " sorry, no can do";
  }
}

if ($suffix == "" && (str_replace(" ","",$onec) != "" || str_replace(" ","",$twoc) != "")) {
  $fh = fopen(dirname(__FILE__) . "/one.one", "w");
  fwrite($fh, $onec);
  fclose($fh);
  $fh = fopen(dirname(__FILE__) . "/two.two", "w");
  fwrite($fh, $twoc);
  fclose($fh);
  exec("diff " . $_GET['minus'] . " " . dirname(__FILE__) . "/one.one " . dirname(__FILE__) . "/two.two > " . dirname(__FILE__) . "/huh.huh");
  if ($called == "") unlink(dirname(__FILE__) . "/one.one");
  if ($called == "") unlink(dirname(__FILE__) . "/two.two");
  if ($called != "") {
    echo "<html><head><title>PHP Linux diff file differences ... First file, Differences, Second file ... RJM Programming November 2013</title></head><body" . $onloadbit . ">\n";
    echo $iframebits;
    echo "</body></html>";
  } else {
    header("Location: " . "/huh.huh");
  }
} else {
  echo "<html><head><title>PHP Linux diff file differences ... RJM Programming November 2013</title></head><body>\n";
  echo "<form style='background-color:yellow;' action='" . $_SERVER['PHP_SELF'] . "' method='POST'><h1>File comparison ... " . $suffix . "</h1>" . "\n";
  echo "Enter URL (ending in GETME) for file comparison 1 of 2: <input style='background-color:pink;' size=120 name='one' id='one' type='text' value='" . $fillinone . "'></input><br>\n"; 
  echo "Enter URL (ending in GETME) for file comparison 2 of 2: <input style='background-color:pink;' size=120 name='two' id='two' type='text' value='" . $fillintwo . "'></input><br><br>\n";
  echo "<input type='submit' name='submit' id='submit' value='Compare'></input></form>";
  echo "</body></html>";
}
?>
