<?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.'/index.html');


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

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

/*
// Create a paragraph so the padding looks good
$p0 = $ndom->createElement('div');
$a = $ndom->createAttribute( 'class' );
$a->value = 'row';
$p0->appendChild($a);
$ndom->appendChild($p0);

$p1 = $ndom->createElement('div');
$a = $ndom->createAttribute( 'class' );
$a->value = 'col-md-12';
$p1->appendChild($a);
$p0->appendChild($p1);

$p = $ndom->createElement('p');
$p1->appendChild($p);
*/

$p = $ndom;

// Copy the body
$xpath = new DOMXPATH($dom);

foreach($xpath->query('/html/body/*') as $rec) {
	$node = $ndom->importNode($rec,true);
	$p->appendChild($node);
}

// Remove the unncessary bits
$p->removeChild($p->firstChild);
$p->removeChild($p->firstChild);
$p->removeChild($p->firstChild);
$p->removeChild($p->firstChild);

/*
// Add the anchor class to each a element
foreach($ndom->getElementsByTagName('a') as $rec) {
	if (!$rec->hasAttribute( 'class' )) {
		$a = $ndom->createAttribute( 'class' );
		$a->value = 'anchor';
		$rec->appendChild($a);
	}
}
*/


// Fix the hrefs
foreach($ndom->getElementsByTagName('a') as $link ) {
	if ($link->hasAttribute('href')) {
		$href = $link->getAttribute('href');
		if (( substr($href,0,5) != 'http:' )  and (substr($href,0,2) != '..' )) {
			if (substr($href,-4) === '.fli' ) {
				$link->setAttribute( 'href', $xrlink.'/'.$href );
			}
			else if (substr($href,-4) === '.mp4' ) {
				$link->setAttribute( 'href', $xrlink.'/'.$href );
			}
			else if (substr($href,-4) === '.ogg' ) {
				$link->setAttribute( 'href', $xrlink.'/'.$href );
			}
			else {
				$link->setAttribute( 'href', '4day.php?link='.$xrlink.'/'.$href );
			}
		}
	}
	$link->nodeValue = $link->nodeValue."\n";
}

/*
// Clean up whitespace
foreach($ndom->getElementsByTagName('a') as $link ) {
	$a->nodeValue = trim($a->nodeValue);
}
*/

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

// print the beast.
$ndom->normalizeDocument();
$ndom->formatOutput = true;
echo $ndom->saveHTML();

?>


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