<?php

include "common_functions.php";

$xmlfile = 'index';
	
//safety
if (strpos('/',$xmlfile)!==false) {
	die('foei');	
}

if (!file_exists('pages/'.$xmlfile.'.xml')) {
	die('index not found');
}

//lees de xml file
$xmldata = file_get_contents("pages/".$xmlfile.".xml");

//parse de xml file
$xmlparser = xml_parser_create();

xml_parser_set_option($xmlparser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xmlparser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($xmlparser, XML_OPTION_TARGET_ENCODING, "ISO-8859-1");


$values = array();
$index  = array();

$ret = xml_parse_into_struct($xmlparser,$xmldata,$values,$index);

if ($ret==0) {
	die("xml error: ".xml_error_string(xml_get_error_code($xmlparser))." at line ".
           xml_get_current_line_number($xmlparser)." column ".
	    xml_get_current_column_number($xmlparser));
}

xml_parser_free($xmlparser);

//voer de boel uit
$title = $values[$index['title'][0]]['value']; 

echo "<?xml version=\"1.0\"?>\n";
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" ";
echo "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
echo "<html xmlns='http://www.w3.org/1999/xhtml'>\n";
echo "<head>\n";
echo "<link rel='stylesheet' href='style.css'/>\n";
echo "<title>";
echo idd_htmlentities($title);
echo "</title>\n";
echo "</head>\n";
echo "<body>\n\n";

echo "<div class='outdiv'>\n";

//we hebben een mapje als dat in de XML staat
if (isset($index['map'])) {
	echo "<div class='map'>\n";
	echo "<a href='maps/".idd_urlencode($values[$index['map'][0]]['attributes']['src']).".jpg'>";
	echo "<img src='maps/".idd_urlencode($values[$index['map'][0]]['attributes']['src'])."-small.jpg' alt='map'/>\n";
	echo "</a></div>\n";
}

echo "<h2>".idd_htmlentities($title)."</h2>\n";

$start = $index['text'][0];
$end   = $index['text'][1];

for ($i=$start;$i<$end;$i++) {
	$tag  = $values[$i]['tag'];
	$type = $values[$i]['type'];
	switch ($tag) {
	case 'p': 
		if ($type=='open') echo '<p>';
		if ($type=='close') echo '</p>';
		if ($type=='cdata') echo wordwrap(idd_htmlentities($values[$i]['value']),120);
		if ($type=='complete') echo '<p>'.wordwrap(idd_htmlentities($values[$i]['value']),120)."</p>\n";
		break;
	case 'a':
	case 'img':
	case 'div':
	case 'hr';
		idd_print_r($values[$i],$i);
		if ($type=='complete' || $type=='open') {
			echo "<$tag";
			if (isset($values[$i]['attributes'])) {
				foreach ($values[$i]['attributes'] as $att=>$val) {
					echo ' '.idd_htmlentities($att).'="'.idd_htmlentities($val).'"';
				}
			}
			if ($type=='complete' && !isset($values[$i]['value'])) echo '/';
			echo ">";
		}
		if ($type=='complete' || $type=='cdata') {
			if (isset($values[$i]['value'])) {
				echo idd_htmlentities($values[$i]['value']);
			}
		}
		if (($type=='complete' && isset($values[$i]['value'])) || $type=='close') echo "</$tag>";
	}
}

$start = $index['pages'][0];
$end   = $index['pages'][1];

$beschr = '';
$file   = '';

echo "<ul>\n";
for ($i=$start;$i<$end;$i++) {
	
	switch($values[$i]['tag']) {
	case 'page':
		if ($values[$i]['type']=='close') {
			echo "<li><a href='dag/$file'>".idd_htmlentities($beschr)."</a></li>\n";
		}
		break;

	case 'besch':
		$beschr = $values[$i]['value'];
		break;
		
	case 'file':
		$file  = $values[$i]['value'];
		break;
	}		
}
echo "</ul>\n";

echo "</div>\n";
echo "<div class='botdiv'><p>Tekst en foto's: &copy; Reggy en Maarten 2006</p></div>\n\n";
echo "<div class='navigation'>\n";
echo "<ul>";
echo "<li><a href='index'>index</a></li>";

if (isset($index['prev'])) {
	echo "<li><a href='dag/{$values[$index['prev'][0]]['value']}'>prev</a></li>";
}
if (isset($index['next'])) { 
	echo "<li><a href='dag/{$values[$index['next'][0]]['value']}'>next</a></li>";
}
echo "</ul>\n";
echo "</div>\n";
echo "</body>\n";
echo "</html>\n";

?>