From a3b2e0f6a826e05d1fb47305034730b5d52df6e8 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Thu, 28 Jan 2010 09:29:55 +0000
Subject: [PATCH] Template: Add compatibility with old dictionary format.

Fixes issue 263.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2148 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/XHTML/Template.php | 86 +++++++++++++++++++++----------
 1 file changed, 60 insertions(+), 26 deletions(-)

diff --git a/lib/SimpleSAML/XHTML/Template.php b/lib/SimpleSAML/XHTML/Template.php
index f0952f2dc..d7716b5af 100644
--- a/lib/SimpleSAML/XHTML/Template.php
+++ b/lib/SimpleSAML/XHTML/Template.php
@@ -434,49 +434,83 @@ class SimpleSAML_XHTML_Template {
 
 
 	/**
-	 * Read a dictionary file.
+	 * Read a dictionary file in json format.
 	 *
-	 * @param $filename  The absolute path to the dictionary file.
-	 * @return The translation array which was found in the dictionary file.
+	 * @param string $filename  The absolute path to the dictionary file, minus the .definition.json ending.
+	 * @return array  The translation array from the file.
 	 */
-	private function readDictionaryFile($filename) {
-		assert('is_string($filename)');
-		
+	private function readDictionaryJSON($filename) {
 		$definitionFile = $filename . '.definition.json';
-		$translationFile = $filename . '.translation.json';
+		assert('file_exists($definitionFile)');
 
-		SimpleSAML_Logger::debug('Template: Reading [' . $filename . ']');
-
-		if (!file_exists($definitionFile)) {
-			SimpleSAML_Logger::error($_SERVER['PHP_SELF'].' - Template: Could not find template file [' . $this->template . '] at [' . $definitionFile . ']');
-					
-			return array();
-		}
-		
 		$fileContent = file_get_contents($definitionFile);
-		$fileArray = json_decode($fileContent, TRUE);
-		
-		if (empty($fileArray)) {
+		$lang = json_decode($fileContent, TRUE);
+
+		if (empty($lang)) {
 			SimpleSAML_Logger::error('Invalid dictionary definition file [' . $definitionFile . ']');
 			return array();
 		}
-		
-		$lang = json_decode($fileContent, TRUE);
 
-		$moreTrans = NULL;
+		$translationFile = $filename . '.translation.json';
 		if (file_exists($translationFile)) {
 			$fileContent = file_get_contents($translationFile);
 			$moreTrans = json_decode($fileContent, TRUE);
+			if (!empty($moreTrans)) {
+				$lang = self::lang_merge($lang, $moreTrans);
+			}
+		}
+
+		return $lang;
+	}
+
+
+	/**
+	 * Read a dictionary file in PHP format.
+	 *
+	 * @param string $filename  The absolute path to the dictionary file.
+	 * @return array  The translation array from the file.
+	 */
+	private function readDictionaryPHP($filename) {
+		$phpFile = $filename . '.php';
+		assert('file_exists($phpFile)');
 
+		$lang = NULL;
+		include($phpFile);
+		if (isset($lang)) {
+			return $lang;
 		}
-		if (!empty($moreTrans))
-			$lang = self::lang_merge($lang, $moreTrans);
 
-		// echo '<pre>'; print_r($lang); exit;
+		return array();
+	}
 
-		return $lang;
+
+	/**
+	 * Read a dictionary file.
+	 *
+	 * @param $filename  The absolute path to the dictionary file.
+	 * @return The translation array which was found in the dictionary file.
+	 */
+	private function readDictionaryFile($filename) {
+		assert('is_string($filename)');
+
+		SimpleSAML_Logger::debug('Template: Reading [' . $filename . ']');
+
+		$jsonFile = $filename . '.definition.json';
+		if (file_exists($jsonFile)) {
+			return $this->readDictionaryJSON($filename);
+		}
+
+
+		$phpFile = $filename . '.php';
+		if (file_exists($phpFile)) {
+			return $this->readDictionaryPHP($filename);
+		}
+
+		SimpleSAML_Logger::error($_SERVER['PHP_SELF'].' - Template: Could not find template file [' . $this->template . '] at [' . $filename . ']');
+		return array();
 	}
-	
+
+
 	// Merge two translation arrays.
 	public static function lang_merge($def, $lang) {
 		foreach($def AS $key => $value) {
-- 
GitLab