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 |   30 +++++++++++++++++++++++++++---
 1 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/uphpCAS.php b/uphpCAS.php
index 77ba8d3..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 {

--
Gitblit v1.9.1