From 12a7b4b2b6447bede8881ed46729a774f634dc05 Mon Sep 17 00:00:00 2001
From: alvaro <alvaro@alia.(none)>
Date: Sat, 30 Jun 2012 07:23:57 -0700
Subject: [PATCH] New feature: add 404 (or other status) custom page.

Page will be processed by Haanga
---
 classes/HTTPStatus.php | 66 +++++++++++++++++-------------------------
 classes/Utils.php      | 18 ++++++------
 2 files changed, 35 insertions(+), 49 deletions(-)

diff --git a/classes/HTTPStatus.php b/classes/HTTPStatus.php
index 12e3de29..6ba8a213 100644
--- a/classes/HTTPStatus.php
+++ b/classes/HTTPStatus.php
@@ -3,71 +3,57 @@
 class HTTPStatus{
   
   public static function send303($uri, $ext){
-    global $conf;
-    $file = $conf['home'].$conf['httpStatus']['directory']."/303.template";
-    if(file_exists($file)){
-      $content = file_get_contents($file);
-    }else{
-      $content = $uri."\n\n";
-    }
   	header("HTTP/1.0 303 See Other");
   	header("Location: ".$uri);
   	header("Content-type: ".$ext);
-  	echo $content;
+  	echo HTTPStatus::_getContent("303", $uri);
   	exit(0);
   }
 
   public static function send401($uri){
-    global $conf;
-    $file = $conf['home'].$conf['httpStatus']['directory']."/401.template";
-    if(file_exists($file)){
-      $content = file_get_contents($file);
-    }else{
-      $content = $uri."\n\n";
-    }
   	header("HTTP/1.0 401 Forbidden");
-  	echo $content;
+  	echo HTTPStatus::_getContent("401", $uri);
   	exit(0);
   }
   
   public static function send404($uri){
-    global $conf;
-    $file = $conf['home'].$conf['httpStatus']['directory']."/404.template";
-    if(file_exists($file)){
-      $content = file_get_contents($file);
-    }else{
-      $content = "LODSPeaKr could not find ".$uri." or information about it.\nNo URIs in the triple store, or services configured with that URI\n";
-    }  	
     header("HTTP/1.0 404 Not Found");
-  	echo $content;
+    $alt = "LODSPeaKr couldn't find the resource ".$uri;
+  	echo HTTPStatus::_getContent("404", $alt);
   	exit(0);
   }
   
   public static function send406($uri){
-    global $conf;
-    $file = $conf['home'].$conf['httpStatus']['directory']."/406.template";
-    if(file_exists($file)){
-      $content = file_get_contents($file);
-    }else{
-      $content = "LODSPeaKr can't find a representation suitable for the content type you accept for $uri\n\n";
-    }  	
     header("HTTP/1.0 406 Not Acceptable");
-  	echo $content;
+    $alt = "LODSPeaKr can't return content acceptable according to the Accept headers sent in the request for ".$uri;
+  	echo HTTPStatus::_getContent("406", $alt);
   	exit(0);
   }
   
   public static function send500($uri){
-    global $conf;
-    $file = $conf['home'].$conf['httpStatus']['directory']."/406.template";
-    if(file_exists($file)){
-      $content = file_get_contents($file);
-    }else{
-      $content = "An internal error ocurred. Please try later\n\n";
-    }  
   	header("HTTP/1.0 500 Internal Server Error");
-  	echo $content;
+  	$alt = "There was an internal error when processing ".$uri;
+  	echo HTTPStatus::_getContent("500", $alt);
   	exit(0);
   }
+  
+  private static function _getContent($n, $alt){
+    global $conf;
+    global $lodspk;
+    $lodspk['root'] = $conf['root'];
+  	$lodspk['this']['value'] = $uri;
+  	$lodspk['this']['curie'] = Utils::uri2curie($uri);
+  	$lodspk['this']['local'] = $localUri;
+  	
+    $file = $conf['httpStatus']['directory']."/".$n.".template";
+   
+    if(file_exists($conf['home'].$file)){
+      require_once("Utils.php");
+      Utils::showView($lodspk, new stdClass(), $file);
+    }else{
+      return $alt."\n\n";
+    }
+  }
 }
 
 ?>
diff --git a/classes/Utils.php b/classes/Utils.php
index c1cdf787..b719da9c 100644
--- a/classes/Utils.php
+++ b/classes/Utils.php
@@ -544,15 +544,15 @@ class Utils{
 	if(is_string($data)){
 	  echo($data);
 	}elseif(is_file($conf['home'].$view)){
-         try{
-	  Haanga::Load($viewFile, $vars);
-          }catch(Exception $e){
-echo '<pre>';
-           echo $e->getMessage();
-var_dump($vars);
-echo($e->getMessage()."' in ".$e->getFile().":".$e->getLine()."\nStack trace:\n".$e->getTraceAsString());
-echo '</pre>';
-         }
+	  try{
+	    Haanga::Load($viewFile, $vars);
+	  }catch(Exception $e){
+	    echo '<pre>';
+	    echo $e->getMessage();
+	    var_dump($vars);
+	    echo($e->getMessage()."' in ".$e->getFile().":".$e->getLine()."\nStack trace:\n".$e->getTraceAsString());
+	    echo '</pre>';
+	  }
 	}elseif($view == null){
 	  $fnc = Haanga::compile('{{models|safe}}');
 	  $fnc($vars, TRUE);
-- 
GitLab