From 634a496d01950b9ff791d3bc99accece43a3dd4f Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Mon, 05 Oct 2020 15:46:45 +0000
Subject: [PATCH] Fix session handling die to changes in PHP 7.1

---
 uphpCAS.php |   38 ++++++++++++++++++++++++++++++++------
 1 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/uphpCAS.php b/uphpCAS.php
index d073771..bfb2b69 100644
--- a/uphpCAS.php
+++ b/uphpCAS.php
@@ -14,6 +14,7 @@
 	protected $serverUrl = '';
 	protected $serviceUrl;
 	protected $sessionName = 'uphpCAS-user';
+	protected $sessionStarted = FALSE;
 	protected $method = 'POST';
 	protected $caFile = NULL;
 	
@@ -112,6 +113,23 @@
 		$this->caFile = $caFile;
 	}
 	
+	public function session_start() {
+		if($this->sessionStarted) {
+			return TRUE;
+		}
+		if(version_compare(PHP_VERSION, '7.1.0', '<')) {
+			@session_start();
+		} else {
+			if(!isset($_SESSION)) {
+				if(!session_start()) {
+					throw new RuntimeException('Cannot start session');
+				}
+			}
+		}
+		$this->sessionStarted = TRUE;
+		return TRUE;
+	}
+	
 	public function loginUrl() {
 		return $this->serverUrl.'/login?method='.$this->method
 			.'&service='.urlencode($this->serviceUrl);
@@ -122,10 +140,14 @@
 			.($returnUrl ? '?service='.urlencode($returnUrl) : '');
 	}
 	
+	public function logoutLocal() {
+		$this->session_start();
+		unset($_SESSION[$this->sessionName]);
+	}
+	
 	public function logout($returnUrl = NULL) {
-		session_start();
+		$this->logoutLocal();
 		if($this->isAuthenticated()) {
-			unset($_SESSION[$this->sessionName]);
 			header('Location: '.$this->logoutUrl($returnUrl));
 			die();
 		} elseif($returnUrl) {
@@ -135,15 +157,17 @@
 	}
 	
 	public function isAuthenticated() {
+		$this->session_start();
 		return isset($_SESSION[$this->sessionName]);
 	}
 	
 	public function authenticate() {
-		session_start();
+		$this->session_start();
 		if($this->isAuthenticated()) {
 			return $_SESSION[$this->sessionName];
 		} elseif(isset($_REQUEST['ticket'])) {
 			$user = $this->verifyTicket($_REQUEST['ticket']);
+			session_regenerate_id();
 			$_SESSION[$this->sessionName] = $user;
 			return $user;
 		} else {
@@ -228,13 +252,15 @@
 				}
 			}
 		} catch(Exception $e) {
-			throw new JasigException('Authentication error: CAS server'
-					.' response invalid - parse error', 0, $e);
-		} finally {
 			libxml_clear_errors();
 			libxml_disable_entity_loader($xmlEntityLoader);
 			libxml_use_internal_errors($xmlInternalErrors);
+			throw new JasigException('Authentication error: CAS server'
+					.' response invalid - parse error', 0, $e);
 		}
+		libxml_clear_errors();
+		libxml_disable_entity_loader($xmlEntityLoader);
+		libxml_use_internal_errors($xmlInternalErrors);
 		
 		$failure = $xml->getElementsByTagName('authenticationFailure');
 		$success = $xml->getElementsByTagName('authenticationSuccess');

--
Gitblit v1.9.1