<?php
$title = "Gliders";
ini_set('include_path', './' . PATH_SEPARATOR . '../' . PATH_SEPARATOR . '../../' . PATH_SEPARATOR . ini_get('include_path'));
include_once("include/header.php");
include_once("include/normpath.php");
?>

    <!--
   **** start with layout component from http://getbootstrap.com/css/#grid
   Any relative links not in the same directory as the templated file should be prepended with
   <?php /* echo $BASEDIR */ ?>
-->
<?php
$debug = False;
$rlink = $_REQUEST["link"];
if (substr($rlink,0,4) === 'http' ) die('link error');

$xrlink = normalise_path( $rlink );
if (substr($xrlink,0,2) === '..' ) die('link error');

$dom = new DOMDocument;
$dom->loadHTMLFile($xrlink);

if ($debug) {
	$stuff = htmlspecialchars($dom->saveXML());
	echo "<p>INPUT<code>$stuff</code></p>";
}

// Create the output document
$ndom = new DOMDocument;
//$ndom->formatOutput = true;

// Extract the interesting bits
$xpath = new DOMXPath($dom);
$prev = $xpath->query('/html/body/p/a[3]')->item(0);
$next = $xpath->query('/html/body/p/a[4]')->item(0);
$list = $xpath->query('/html/body/p/a[5]')->item(0);
$img = $xpath->query('/html/body/p/img[1]')->item(0);

// Fix the links
$href = $prev->getAttribute('href');
$prev->setAttribute('href', '4day.php?link='.dirname($xrlink).'/'.$href);

$href = $next->getAttribute('href');
$next->setAttribute('href', '4day.php?link='.dirname($xrlink).'/'.$href);

$list->setAttribute('href', '4day_index.php?link='.dirname($xrlink));

// Fix the image
if ($debug) {
	echo "<code>rlink=$rlink</code>";
	$dr = dirname($rlink);
	echo "<code>dr=$dr</code>";
}

$src = $img->getAttribute('src');
$img->setAttribute('src', dirname($xrlink).'/'.$src);


// Build up the output document
$node = $ndom->importNode($prev,true);
$ndom->appendChild($node);
$node = $ndom->importNode($next,true);
$ndom->appendChild($node);
$node = $ndom->importNode($list,true);
$ndom->appendChild($node);
$node = $ndom->createElement("BR");
$ndom->appendChild($node);

$node = $ndom->importNode($img,true);
$ndom->appendChild($node);

if ($debug) {
	$stuff = htmlspecialchars($ndom->saveXML());
	echo "<p>MID<code>$stuff</code></p>";
}


// print the beast.
echo $ndom->saveHTML();

?>


<?php
include("include/sfooter.php");
?>
