From a6b8c184b34623c340914717a24e85e1547a0a75 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Mon, 4 Aug 2008 05:44:14 +0000
Subject: [PATCH] Utilities::fatalError: Add possibility for replacements in
 error text.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@792 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Utilities.php | 43 ++++++++++++++++++++++++++++++------
 templates/default/error.php  |  6 +++--
 www/errorreport.php          |  1 +
 3 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index 9cf6953a5..b794f89ec 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -388,22 +388,50 @@ class SimpleSAML_Utilities {
 
 
 	/** 
+	 * Show and log fatal error message.
+	 *
 	 * This function logs a error message to the error log and shows the
 	 * message to the user. Script execution terminates afterwards.
 	 *
-	 *  @param $title       Short title for the error message.
-	 *  @param $message     The error message.
+	 * The error code comes from the errors-dictionary. It can optionally include parameters, which
+	 * will be substituted into the output string.
+	 *
+	 * @param string $trackid  The trackid of the user, from $session->getTrackID().
+	 * @param mixed $errorcode  Either a string with the error code, or an array with the error code and
+	 *                          additional parameters.
+	 * @param Exception $e  The exception which caused the error.
 	 */
-	public static function fatalError($trackid = 'na', $errorcode = null, Exception $e = null, $level = LOG_ERR) {
+	public static function fatalError($trackid = 'na', $errorcode = null, Exception $e = null) {
 	
 		$config = SimpleSAML_Configuration::getInstance();
-		
+
+		if(is_array($errorcode)) {
+			$parameters = $errorcode;
+			unset($parameters[0]);
+			$errorcode = $errorcode[0];
+		} else {
+			$parameters = array();
+		}
+
 		// Get the exception message if there is any exception provided.
 		$emsg   = (empty($e) ? 'No exception available' : $e->getMessage());
 		$etrace = (empty($e) ? 'No exception available' : $e->getTraceAsString()); 
-		
+
+		if(!empty($errorcode) && count($parameters) > 0) {
+			$reptext = array();
+			foreach($parameters as $k => $v) {
+				$reptext[] = '"' . $k . '"' . ' => "' . $v . '"';
+			}
+			$reptext = '(' . implode(', ', $reptext) . ')';
+			$error = $errorcode . $reptext;
+		} elseif(!empty($errorcode)) {
+			$error = $errorcode;
+		} else {
+			$error = 'na';
+		}
+
 		// Log a error message
-		SimpleSAML_Logger::error($_SERVER['PHP_SELF'].' - UserError: ErrCode:'.(!empty($errorcode) ? $errorcode : 'na').': '.urlencode($emsg) );
+		SimpleSAML_Logger::error($_SERVER['PHP_SELF'].' - UserError: ErrCode:' . $error . ': ' . urlencode($emsg) );
 		
 		$languagefile = null;
 		if (isset($errorcode)) $languagefile = 'errors';
@@ -413,7 +441,8 @@ class SimpleSAML_Utilities {
 		
 		
 		$t->data['errorcode'] = $errorcode;
-		
+		$t->data['parameters'] = $parameters;
+
 		$t->data['showerrors'] = $config->getValue('showerrors', true);
 
 		/* Check if there is a valid technical contact email address. */
diff --git a/templates/default/error.php b/templates/default/error.php
index fcc1b1a54..8ca64bfbf 100644
--- a/templates/default/error.php
+++ b/templates/default/error.php
@@ -11,8 +11,9 @@
 	?></h2>
 
 <?php
-if($this->t('descr_' . $this->data['errorcode'])) {
-	echo $this->t('descr_' . $this->data['errorcode']);
+$descr = $this->t('descr_' . $this->data['errorcode'], $this->data['parameters']);
+if($descr) {
+	echo htmlspecialchars($descr);
 }
 ?>
 
@@ -67,6 +68,7 @@ if (!empty($this->data['errorreportaddress'])) {
 		<input type="hidden" name="exceptionmsg" value="<?php echo htmlspecialchars($this->data['exceptionmsg']); ?>" />
 		<input type="hidden" name="exceptiontrace" value="<?php echo htmlspecialchars($this->data['exceptiontrace']); ?>" />
 		<input type="hidden" name="errorcode" value="<?php echo htmlspecialchars($this->data['errorcode']); ?>" />
+		<input type="hidden" name="parameters" value="<?php echo htmlspecialchars(var_export($this->data['parameters'], TRUE)); ?>" />
 		<input type="hidden" name="url" value="<?php echo htmlspecialchars($this->data['url']); ?>" />
 
 		<input type="submit" name="send" value="<?php echo $this->t('report_submit'); ?>" />
diff --git a/www/errorreport.php b/www/errorreport.php
index bcc13f1a5..0874925fe 100644
--- a/www/errorreport.php
+++ b/www/errorreport.php
@@ -22,6 +22,7 @@ $mailFormat = array(
 	'email' => 'Email address of submitter',
 	'url' => 'URL of page where the error occured',
 	'errorcode' => 'Error code',
+	'parameters' => 'Parameters for the error',
 	'text' => 'Message from user',
 	'trackid' => 'Track id for the user\' session',
 	'exceptionmsg' => 'Exception message',
-- 
GitLab