From e3559a5632d0158d5c2bb5ce2c78f0f78096fd4c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaime=20P=C3=A9rez=20Crespo?= <jaime.perez@uninett.no>
Date: Wed, 5 Feb 2014 13:42:26 +0000
Subject: [PATCH] Solve the problem in issue #594 in a backwards compatible way
 that doesn't break anything.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@3358 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Session.php | 64 ++++++++++++++++----------------------
 1 file changed, 26 insertions(+), 38 deletions(-)

diff --git a/lib/SimpleSAML/Session.php b/lib/SimpleSAML/Session.php
index 404b54819..f3ecffc92 100644
--- a/lib/SimpleSAML/Session.php
+++ b/lib/SimpleSAML/Session.php
@@ -177,7 +177,6 @@ class SimpleSAML_Session {
 		$this->trackid = substr(md5(uniqid(rand(), true)), 0, 10);
 
 		$this->dirty = TRUE;
-		$this->addShutdownFunction();
 
 		/* Initialize data for session check function if defined */
 		$globalConfig = SimpleSAML_Configuration::getInstance();
@@ -189,6 +188,32 @@ class SimpleSAML_Session {
 	}
 
 
+    /**
+     * Destructor for this class. It will save the session to the session handler
+     * in case the session has been marked as dirty. Do nothing otherwise.
+     */
+    public function __destruct() {
+        if(!$this->dirty) {
+            /* Session hasn't changed - don't bother saving it. */
+            return;
+        }
+
+        $this->dirty = FALSE;
+
+        $sh = SimpleSAML_SessionHandler::getSessionHandler();
+
+        try {
+            $sh->saveSession($this);
+        } catch (Exception $e) {
+            if (!($e instanceof SimpleSAML_Error_Exception)) {
+                $e = new SimpleSAML_Error_UnserializableException($e);
+            }
+            SimpleSAML_Logger::error('Unable to save session.');
+            $e->logError();
+        }
+    }
+
+
 	/**
 	 * Upgrade this session object to use the $authData property.
 	 *
@@ -247,7 +272,6 @@ class SimpleSAML_Session {
 	 * This function is called after this class has been deserialized.
 	 */
 	public function __wakeup() {
-		$this->addShutdownFunction();
 
 		/* TODO: Remove for version 1.8. */
 		if ($this->authData === NULL) {
@@ -1166,42 +1190,6 @@ class SimpleSAML_Session {
 	}
 
 
-	/**
-	 * Save the session to the session handler.
-	 *
-	 * This function will check the dirty-flag to check if the session has changed.
-	 */
-	public function saveSession() {
-
-		if(!$this->dirty) {
-			/* Session hasn't changed - don't bother saving it. */
-			return;
-		}
-
-		$this->dirty = FALSE;
-
-		$sh = SimpleSAML_SessionHandler::getSessionHandler();
-
-		try {
-			$sh->saveSession($this);
-		} catch (Exception $e) {
-			if (!($e instanceof SimpleSAML_Error_Exception)) {
-				$e = new SimpleSAML_Error_UnserializableException($e);
-			}
-			SimpleSAML_Logger::error('Unable to save session.');
-			$e->logError();
-		}
-	}
-
-
-	/**
-	 * Add a shutdown function for saving this session object on exit.
-	 */
-	private function addShutdownFunction() {
-		register_shutdown_function(array($this, 'saveSession'));
-	}
-
-
 	/**
 	 * Set the logout state for this session.
 	 *
-- 
GitLab