From 88e1dd72585eacb2c8596aebc9938af1a26c350b Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Thu, 10 Mar 2011 10:01:42 +0000
Subject: [PATCH] Allow for templates describing the errors to be included into
 the main error template.

This patch also changes the MetadataNotFound and NoState errors to use this structure.

Thanks to Andjelko Horvat for implementing this.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2773 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Error/Error.php             |  9 +++++
 lib/SimpleSAML/Error/MetadataNotFound.php  | 31 ++----------------
 lib/SimpleSAML/Error/NoState.php           | 38 +---------------------
 modules/core/templates/no_metadata.tpl.php |  7 ----
 modules/core/templates/no_state.tpl.php    | 28 ----------------
 templates/error.php                        |  8 +++--
 6 files changed, 18 insertions(+), 103 deletions(-)

diff --git a/lib/SimpleSAML/Error/Error.php b/lib/SimpleSAML/Error/Error.php
index af86ae4c0..f31636e77 100644
--- a/lib/SimpleSAML/Error/Error.php
+++ b/lib/SimpleSAML/Error/Error.php
@@ -50,6 +50,14 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
 	private $parameters;
 
 
+	/**
+	 * Name of custom include template for the error.
+	 *
+	 * @var string|NULL
+	 */
+	protected $includeTemplate = NULL;
+
+
 	/**
 	 * Constructor for this error.
 	 *
@@ -217,6 +225,7 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
 		$data['module'] = $this->module;
 		$data['dictTitle'] = $this->dictTitle;
 		$data['dictDescr'] = $this->dictDescr;
+		$data['includeTemplate'] = $this->includeTemplate;
 
 		/* Check if there is a valid technical contact email address. */
 		if($config->getString('technicalcontact_email', 'na@example.org') !== 'na@example.org') {
diff --git a/lib/SimpleSAML/Error/MetadataNotFound.php b/lib/SimpleSAML/Error/MetadataNotFound.php
index fb2d48323..940b914e3 100644
--- a/lib/SimpleSAML/Error/MetadataNotFound.php
+++ b/lib/SimpleSAML/Error/MetadataNotFound.php
@@ -8,13 +8,6 @@
  */
 class SimpleSAML_Error_MetadataNotFound extends SimpleSAML_Error_Error {
 
-	/**
-	 * The entityID we were unable to locate.
-	 *
-	 * @var string
-	 */
-	private $entityId;
-
 
 	/**
 	 * Create the error
@@ -24,31 +17,11 @@ class SimpleSAML_Error_MetadataNotFound extends SimpleSAML_Error_Error {
 	public function __construct($entityId) {
 		assert('is_string($entityId)');
 
+		$this->includeTemplate = 'core:no_metadata.tpl.php';
 		parent::__construct(array(
 				'METADATANOTFOUND',
-				'ENTITYID' => htmlspecialchars(var_export($entityId, TRUE))
+				'%ENTITYID%' => htmlspecialchars(var_export($entityId, TRUE))
 		));
-
-		$this->entityId = $entityId;
-	}
-
-
-	/**
-	 * Show the error to the user.
-	 *
-	 * This function does not return.
-	 */
-	public function show() {
-
-		header('HTTP/1.0 500 Internal Server Error');
-
-		$this->logError();
-
-		$globalConfig = SimpleSAML_Configuration::getInstance();
-		$t = new SimpleSAML_XHTML_Template($globalConfig, 'core:no_metadata.tpl.php');
-		$t->data['entityId'] = $this->entityId;
-		$t->show();
-		exit();
 	}
 
 }
diff --git a/lib/SimpleSAML/Error/NoState.php b/lib/SimpleSAML/Error/NoState.php
index 723380726..903840dee 100644
--- a/lib/SimpleSAML/Error/NoState.php
+++ b/lib/SimpleSAML/Error/NoState.php
@@ -14,44 +14,8 @@ class SimpleSAML_Error_NoState extends SimpleSAML_Error_Error {
 	 * Create the error
 	 */
 	public function __construct() {
+		$this->includeTemplate = 'core:no_state.tpl.php';
 		parent::__construct('NOSTATE');
 	}
 
-
-	/**
-	 * Show the error to the user.
-	 *
-	 * This function does not return.
-	 */
-	public function show() {
-
-		header('HTTP/1.0 500 Internal Server Error');
-
-		$errorData = $this->saveError();
-
-		$session = SimpleSAML_Session::getInstance();
-		$attributes = $session->getAttributes();
-		if (isset($attributes['mail'][0])) {
-			$email = $attributes['mail'][0];
-		} else {
-			$email = '';
-		}
-
-
-		$globalConfig = SimpleSAML_Configuration::getInstance();
-		$t = new SimpleSAML_XHTML_Template($globalConfig, 'core:no_state.tpl.php');
-
-		/* Enable error reporting if we have a valid technical contact email. */
-		if($globalConfig->getString('technicalcontact_email', 'na@example.org') !== 'na@example.org') {
-			/* Enable error reporting. */
-			$baseurl = SimpleSAML_Utilities::getBaseURL();
-			$t->data['errorReportAddress'] = $baseurl . 'errorreport.php';
-			$t->data['reportId'] = $errorData['reportId'];
-			$t->data['email'] = $email;
-		}
-
-		$t->show();
-		exit();
-	}
-
 }
diff --git a/modules/core/templates/no_metadata.tpl.php b/modules/core/templates/no_metadata.tpl.php
index 09cdbbc33..d3b638b22 100644
--- a/modules/core/templates/no_metadata.tpl.php
+++ b/modules/core/templates/no_metadata.tpl.php
@@ -1,11 +1,5 @@
 <?php
 
-$this->data['header'] = $this->t('{core:no_metadata:header}');
-$this->includeAtTemplateBase('includes/header.php');
-
-echo('<h2>' . $this->t('{core:no_metadata:header}') . '</h2>');
-echo('<p>' . $this->t('{core:no_metadata:not_found_for}') . '</p>');
-echo('<code style="margin-left: 3em;">' . htmlspecialchars($this->data['entityId']) . '</code>');
 echo('<p>' . $this->t('{core:no_metadata:config_problem}') . '</p>');
 
 echo('<ul>');
@@ -13,4 +7,3 @@ echo('<li>' . $this->t('{core:no_metadata:suggestion_user_link}') . '</li>');
 echo('<li>' . $this->t('{core:no_metadata:suggestion_developer}') . '</li>');
 echo('</ul>');
 
-$this->includeAtTemplateBase('includes/footer.php');
diff --git a/modules/core/templates/no_state.tpl.php b/modules/core/templates/no_state.tpl.php
index 8cf07f793..f86e346e0 100644
--- a/modules/core/templates/no_state.tpl.php
+++ b/modules/core/templates/no_state.tpl.php
@@ -1,11 +1,5 @@
 <?php
 
-$this->data['header'] = $this->t('{core:no_state:header}');
-$this->includeAtTemplateBase('includes/header.php');
-
-echo('<h2>' . $this->t('{core:no_state:header}') . '</h2>');
-echo('<p>' . $this->t('{core:no_state:description}') . '</p>');
-
 echo('<h3>' . $this->t('{core:no_state:suggestions}') . '</h3>');
 echo('<ul>');
 echo('<li>' . $this->t('{core:no_state:suggestion_goback}') . '</li>');
@@ -19,25 +13,3 @@ echo('<li>' . $this->t('{core:no_state:cause_openbrowser}') . '</li>');
 echo('<li>' . $this->t('{core:no_state:cause_nocookie}') . '</li>');
 echo('</ul>');
 
-
-/* Add error report submit section if we have a valid technical contact. */
-if (isset($this->data['errorReportAddress'])) {
-
-	echo('<h2>' . $this->t('{core:no_state:report_header}') . '</h2>');
-
-	echo('<form action="' . htmlspecialchars($this->data['errorReportAddress']) . '" method="post">');
-
-	echo('<p>' . $this->t('{core:no_state:report_text}') . '</p>');
-	echo('<p>' . $this->t('{errors:report_email}') . '<input type="text" size="25" name="email" value="' . htmlspecialchars($this->data['email']) . '"/></p>');
-
-	echo('<p>');
-	echo('<textarea style="width: 300px; height: 100px" name="text">' . $this->t('{errors:report_explain}') . '</textarea>');
-	echo('</p>');
-	echo('<p>');
-	echo('<input type="hidden" name="reportId" value="' . $this->data['reportId'] . '" />');
-	echo('<input type="submit" name="send" value="' . $this->t('{errors:report_submit}') . '" />');
-	echo('</p>');
-	echo('</form>');
-}
-
-$this->includeAtTemplateBase('includes/footer.php');
diff --git a/templates/error.php b/templates/error.php
index b9603a435..b4358af42 100644
--- a/templates/error.php
+++ b/templates/error.php
@@ -1,5 +1,5 @@
 <?php 
-	$this->data['header'] = $this->t('error_header');
+	$this->data['header'] = $this->t($this->data['dictTitle']);
 	
 	$this->data['head'] = '
 <meta name="robots" content="noindex, nofollow" />
@@ -8,11 +8,15 @@
 	$this->includeAtTemplateBase('includes/header.php'); 
 ?>
 
-
 	<h2><?php echo $this->t($this->data['dictTitle']); ?></h2>
 
 <?php
 echo htmlspecialchars($this->t($this->data['dictDescr'], $this->data['parameters']));
+
+/* Include optional information for error. */
+if (isset($this->data['includeTemplate'])) {
+	$this->includeAtTemplateBase($this->data['includeTemplate']);
+}
 ?>
 
 	<div class="trackidtext">
-- 
GitLab