From b48e80cc2651fcad3a4d8075120bb541c758bc8c Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Mon, 12 Jul 2010 07:59:40 +0000
Subject: [PATCH] Template: Add [gs]etLanguageCookie functions.

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

diff --git a/lib/SimpleSAML/XHTML/Template.php b/lib/SimpleSAML/XHTML/Template.php
index 0031701c7..149788bb4 100644
--- a/lib/SimpleSAML/XHTML/Template.php
+++ b/lib/SimpleSAML/XHTML/Template.php
@@ -75,11 +75,7 @@ class SimpleSAML_XHTML_Template {
 	 */
 	public function setLanguage($language) {
 		$this->language = $language;
-		// setcookie ( string $name [, string $value [, int $expire [, string $path [, string $domain [, bool $secure [, bool $httponly ]]]]]] )
-		// time()+60*60*24*900 expires 900 days from now.
-		if (!headers_sent()) {
-			setcookie('language', $language, time()+60*60*24*900, '/');
-		}
+		SimpleSAML_XHTML_Template::setLanguageCookie($language);
 	}
 
 	/**
@@ -97,9 +93,10 @@ class SimpleSAML_XHTML_Template {
 		}
 
 		// Language is provided in a stored COOKIE
-		if (isset($_COOKIE['language'])) {
-			$this->language = $_COOKIE['language'];
-			return $this->language;
+		$languageCookie = SimpleSAML_XHTML_Template::getLanguageCookie();
+		if ($languageCookie !== NULL) {
+			$this->language = $languageCookie;
+			return $languageCookie;
 		}
 		
 		if ($checkHTTP) {
@@ -608,6 +605,35 @@ class SimpleSAML_XHTML_Template {
 		throw new Exception($error);
 	}
 
+
+	/**
+	 * Retrieve the user-selected language from a cookie.
+	 *
+	 * @return string|NULL  The language, or NULL if unset.
+	 */
+	public static function getLanguageCookie() {
+
+		if (!isset($_COOKIE['language'])) {
+			return NULL;
+		}
+		return (string)$_COOKIE['language'];
+	}
+
+
+	/**
+	 * Set the user-selected language in a cookie.
+	 *
+	 * @param string $language  The language.
+	 */
+	public static function setLanguageCookie($language) {
+		assert('is_string($language)');
+
+		if (headers_sent()) {
+			return;
+		}
+		setcookie('language', $language, time()+60*60*24*900, '/');
+	}
+
 }
 
 ?>
-- 
GitLab