<?php
// bytheby.php
// Populate select tag of parent with latest tutorials at this blog
// RJM Programming
// January, 2015

function noonclick($ins) {
 $outs=$ins;
 $bits=explode("'", $ins);
 if (sizeof($bits) > 1) {
  $outs = "";
  for ($ii=0; $ii<sizeof($bits); $ii++) {
   if (strpos($bits[$ii],"windowopen") === false) $outs .= "' + \n '" .  str_replace("(toggle sort order)", "(show several postings as required)",str_replace("(show blog, toggle sort order)", "(show several postings as required)",str_replace("\n","",str_replace("onclick=","",$bits[$ii]))));
  }
 }
 return $outs;
}

function DOMinnerHTML(DOMNode $element)  { // thanks to http://stackoverflow.com/questions/2087103/innerhtml-in-phps-domdocument
    $innerHTML = ""; 
    $children  = $element->childNodes;

    foreach ($children as $child) 
    { 
        $innerHTML .= $element->ownerDocument->saveHTML($child);
    }

    return $innerHTML; 
} 

if (isset($_GET['populate'])) {
  try {
  $data = @file_get_contents("http://www.rjmprogramming.com.au/index.html");
  $doc = new DOMDocument();
  $doc->preserveWhiteSpace = FALSE;
  $before = libxml_use_internal_errors(true);
  $doc->loadHTML($data);
  $predata = noonclick(DOMinnerHTML($doc->getElementById('tuts')));
  echo "<html><head><script type='text/javascript'> function doit() {   parent.document.getElementById('" . $_GET['populate'] . "').innerHTML='" . $predata . "'; } </script><body onload='doit();'>";
  } catch (Exception $e) {
  throw new Exception( 'Something really wrong', 0, $e);
  }
} else {
  echo "<html><body>";
}
?>
</body>
</html>
