<?php

/*
To use:
Create a web-accessible directory, let's say foobar, with all your .rdf, .ttl, .ntriples and .html files in it.
Copy lodpub.php and path.php into it.
Access path.php from your web server.
Follow the instruction to paste that text into .htaccess
*/

/*
Some explanation:
We use a different method, and I have tried to extract the essence, and keep the code very simple.
We trap all 404 (File not Found) in the directory, and then any requests coming in for non-existent files will generate a 303 with an extension added, depending on the Accept header.
Note that you probably need the leading "/" followed by the full path from the domain root, otherwise it will just print out the text "lodpub.php";
(That is not what the apache specs seem to say, but it is what seems to happen).
If you get "Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request." then it means that web server is not finding your ErrorDocument .
Put the file path.php in the same directory and point your browser at it - this will tell you what the path should be.

Note that the httpd.conf (in /etc/httpd/conf) may not let your override, if your admins have tied things down really tight.
Mine says:
    AllowOverride All

Finally, at the moment, note that I think that apache default config does not put the correct MIME type on rdf files, but that is a separate issue, and it makes no difference that the 303 happened.
*/

## This is a simplified version of the rkbexplorer error handler that does a lot of stuff to
## deal with the multiple domains, and also does dynamic queries into the triplestore when a
## 404 happens.
## It is probably not right yet for the simplified version, but someone else may like to take it and polish it?
##
## 2009-07-05, Hugh Glaser, hg@ecs.soton.ac.uk
##
##  This is the apache 404 error handler for this directory
##
##  If 404 on requested resource-xyz then inspect accept headers
##  and issue a 303 to either resource-xyz.rdf or resource-xyz.html etc.
##
##
	$extensions = array('.rdf', '.ttl', '.ntriples', '.html');
	$ext = substr($_SERVER['REQUEST_URI'], (strrpos($_SERVER['REQUEST_URI'],'.')));
	// Check to see that we don't come back in and loop.
	if (in_array($ext, $extensions)) do_404();

	//  Inspect accept headers
	if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/rdf+xml') !== false) {
		header('HTTP/1.1 303 See Other');
		header('Location: http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] . '.rdf', 303);
		die;

	} else if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'text/turtle') !== false) {
		header('HTTP/1.1 303 See Other');
		header('Location: http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] . '.ttl', 303);
		die;

	} else if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'text/n3') !== false) {
		header('HTTP/1.1 303 See Other');
		header('Location: http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] . '.ntriples', 303);
		die;

	} else {
		//  otherwise redirect to human-readable representation
		// Check we are not looking for a non-existent html file.
		header('HTTP/1.1 303 See Other');
		header('Location: http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] . '.html', 303);
		die;
	}

function do_404() {
	header('HTTP/1.1 404 Not Found');
	echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>404 Not Found</title> </head><body> <h1>Not Found</h1> <p>The requested URL ';
	echo $_SERVER['REQUEST_URI'];
	echo " was not found on this server.</p></body></html>\n";
	die;
}
	
?>
