All pastes #455723 Raw Edit

webvictim

public text v1 · immutable
#455723 ·published 2007-04-24 12:22 UTC
rendered paste body
<?php
/*
City guide XML Processor
P&P Associates Limited
written by Gus Luxton on 23/04/2007
gus@ppassociates.co.uk
*/

// $xml_filename is just the name of the city in sentence case
$xml_filename = ucwords(rawurldecode($_GET['dest']));
// $section_requested is Overview, Excursions, Nightlife etc etc
$section_requested = rawurldecode($_GET['section']);

// if no destination name is passed to the script, show the index
if (strlen($xml_filename)==0)
{
	echo "<html>\n";
	echo "<head>\n";
	echo "<title>Asda Travel > Destination Guides</title>\n";
	echo "<link rel='stylesheet' type='text/css' href='style.css'>";
	echo "</head>\n\n";
	echo "<body>\n";
	echo "<a name='top'>\n";
	
	echo "<table width='100%' cellspacing='1' cellpadding='1'>\n";
	echo "<tr><td colspan='2'>";
	echo "<a href='http://www.asda-travel.co.uk/'>Asda Travel</a> > \n";
	echo "Destination Guides\n\n";
	echo "</td></tr>\n";
	echo "<tr><td colspan='2'><hr size='1'></td></tr>\n";
	echo "<tr><td cellspacing='2' cellpadding='2' width='20%' valign='top'>\n";
	echo "<ul>\n";

	// create directory object and set up a counter for the array that will hold the filenames
	$mydir = dir('/home/sites/webvictim/destguides/xml');
	$j=0;
	$file_array=array();

	// while there are files still in the directory
	while(($file = $mydir->read()) !== false)
	{
		// if the extension is .xml
		if(substr($file,(strpos($file,".")))==".xml")
		{
			// add the file into the array (minus its .xml extension)
			$file = substr($file, 0, strlen($file)-4);
	        $file_array[$j] = "<a href='index.php?dest=$file'>$file</a><br>\n";
		}
		$j++;
	}
	// close the directory
	$mydir->close();

	// sort and print the list of destinations
	asort($file_array);
	foreach ($file_array as $filename)
	{
		echo $filename;
	}

	echo "</ul>\n";
	echo "</td>\n";
	
	echo "</body>\n";
	echo "</html>";
}

// otherwise show the XML for the destination
else
{
	// if no section has been requested, show the overview
	if (!(strlen($section_requested)>0))
	{
		$section_requested="Overview";
	}	
	
	// if the file doesn't exist (i.e. they've entered a bogus destination name in their URL bar)
	// then show an error message and exit nicely.
	if (!(file_exists("xml/".$xml_filename.".xml")))
	{
		die("There is no travel guide for that destination (<b>$xml_filename</b>).");
	}
	// if it does exist, then load the XML file, add <![CDATA[ and ]]> to <DataInfo> sections so they are
	// treated as text and not parsed into XML objects by SimpleXML, then create the SimpleXML object $xml
	else
	{
		$xml_file =	str_replace(array("<DataInfo>","</DataInfo>"),array("<DataInfo><![CDATA[","]]></DataInfo>"),file_get_contents("xml/".$xml_filename.".xml"));
		$xml = new SimpleXMLElement($xml_file);
		if (!($xml))
		{
			die("SimpleXML object creation error!");
		}
	}

	echo "<html>\n";
	echo "<head>\n";
	echo "<title>Asda Travel > Destination Guides > $xml_filename</title>\n";
	echo "<link rel='stylesheet' type='text/css' href='style.css'>";
	echo "</head>\n\n";
	echo "<body>\n";
	echo "<a name='top'>\n";
	echo "<table width='100%' cellspacing='1' cellpadding='1'>\n";
	echo "<tr><td colspan='2'>";
	
	// for each section in the XML file (they vary a lot) add a menu entry into the array
	$i=0;
	foreach ($xml->Region->Section as $section)
	{
		$menu[$i] = $section->SectionTitle;
		$i++;
	}

	/* 	sort the menu array so it's in alphabetical order
	echo "<pre>";
	print_r($menu);
	echo "</pre>";
	exit();	
	asort($menu); */

	echo "<a href='http://www.asda-travel.co.uk/'>Asda Travel</a> > \n";
	echo "<a href='".$_SERVER['PHP_SELF']."'>Destination Guides</a> > \n";
	echo "<b>$xml_filename</b>\n\n";
	echo "</td></tr>\n";
	echo "<tr><td colspan='2'><hr size='1'></td></tr>\n";
	echo "<tr><td cellspacing='2' cellpadding='2' width='20%' valign='top'>\n";
	echo "<ul>\n";
	// print the menu at the left hand side with appropriate links for navigation
	foreach ($menu as $menuItem)
	{
		$self = $_SERVER['PHP_SELF'];
		$menuItemEncoded = rawurlencode($menuItem);
		echo "<li><a href='$self?dest=$xml_filename&section=$menuItemEncoded'>$menuItem</a></li>\n";
	}
	echo "</ul>\n";
	echo "</td>\n";
	
	// if a particular section has been requested
	if (isset($section_requested))
	{
		// go through the file until we get to the appropriate part
		foreach ($xml->Region->Section as $section)
		{
			// and if it exists, create $body with all the information in
			if (strcasecmp($section->SectionTitle, $section_requested)==0)
			{
				$body.="<table width='100%' align='center'>\n";
				$body.="<tr><th bgcolor='#CCCCCC'>".$section->SectionTitle."</th></tr>\n";
				$body.="<tr><td>".(string)$section->OutputData->DataInfo."</td></tr>\n";
				$body.="</table>\n";
				$body.="<p>\n\n";
			}
		}
		// if there is no section with that name
		if (!(strlen($body)>0))
		{
			// print an error
			echo "<td width='80%' valign='top'>\n";
			echo "Sorry, that section does not exist.<p>\n";
			echo "</td></tr></table>\n";
		}
		// otherwise, print the section information
		else
		{
			echo "<td class='body' width='80%' valign='top'>\n";
			echo $body."<p>\n";
			echo "</td></tr></table>\n";
			// if the body is over 4000 characters (that's approximately one screenful at 1024x768)
			if (strlen($body)>4000)
			{
				// give them a "back to the top" link
				echo "<p align='center'><a href='#top'>Back to the top</a></p>\n";
			}
			// also print a "back to the index" link for ease
			echo "<p align='center'><a href='".$_SERVER['PHP_SELF']."'>Back to the index</a>";
		}
	}
	echo "</body>\n";
	echo "</html>";
}
?>