XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
XCube_Session.class.php
1<?php
11
17 public $mSessionName = '';
18
24
30
36
37 public function __construct( $sessionName = '', $sessionExpire = 0 ) {
38 $this->setParam( $sessionName, $sessionExpire );
39
40 $this->mSetupSessionHandler = new XCube_Delegate();
41 $this->mSetupSessionHandler->register( 'XCube_Session.SetupSessionHandler' );
42
43 $this->mGetSessionCookiePath = new XCube_Delegate();
44 $this->mGetSessionCookiePath->register( 'XCube_Session.GetSessionCookiePath' );
45 }
46
53 public function setParam( $sessionName = '', $sessionExpire = 0 ) {
54 $allIniArray = ini_get_all();
55
56 if ( '' !== $sessionName ) {
57 $this->mSessionName = $sessionName;
58 } else {
59 $this->mSessionName = $allIniArray['session.name']['global_value'];
60 }
61
62 if ( ! empty( $sessionExpire ) ) {
63 $this->mSessionLifetime = 60 * $sessionExpire;
64 } else {
65 $this->mSessionLifetime = $allIniArray['session.cookie_lifetime']['global_value'];
66 }
67 }
68
72 public function start() {
73 $this->mSetupSessionHandler->call();
74
75 session_name( $this->mSessionName );
76 session_set_cookie_params( $this->mSessionLifetime, $this->_cookiePath() );
77
78 session_start();
79
80 if ( ! empty( $this->mSessionLifetime ) && isset( $_COOKIE[ $this->mSessionName ] ) ) {
81 // Refresh lifetime of Session Cookie
82 $session_params = session_get_cookie_params();
83 ! $session_params['domain'] and $session_params['domain'] = null;
84 $session_cookie_params = [
86 session_id(),
88 $this->_cookiePath(),
89 $session_params['domain'],
90 $session_params['secure']
91 ];
92 if ( isset( $session_params['httponly'] ) ) {
93 $session_cookie_params[] = $session_params['httponly'];
94 }
96 // setcookie(...$session_cookie_params);
97 call_user_func_array( 'setcookie', $session_cookie_params );
98
99 }
100 }
101
107 public function destroy( $forceCookieClear = false ) {
108 // If the current session name is not the same as config value.
109 // Session cookie should be clear
110 // (This case will occur when session config params are changed in preference screen.)
111 $currentSessionName = session_name();
112 if ( isset( $_COOKIE[ $currentSessionName ] ) ) {
113 if ( $forceCookieClear || ( $currentSessionName !== $this->mSessionName ) ) {
114 // Clearing Session Cookie
115 setcookie( $currentSessionName, '', ['expires' => time() - 86400, 'path' => $this->_cookiePath()] );
116 }
117 }
118 session_destroy();
119 }
120
124 public function regenerate() {
125 $oldSessionID = session_id();
126 session_regenerate_id();
127 $newSessionID = session_id();
128 session_id( $oldSessionID );
129 $this->destroy();
130 $oldSession = $_SESSION;
131 session_id( $newSessionID );
132 $this->start();
133 $_SESSION = [];
134 foreach ( array_keys( $oldSession ) as $key ) {
135 $_SESSION[ $key ] = $oldSession[ $key ];
136 }
137 }
138
142 public function rename() {
143 if ( session_name() !== $this->mSessionName ) {
144 $oldSessionID = session_id();
145 $oldSession = $_SESSION;
146 $this->destroy();
147 session_id( $oldSessionID );
148 $this->start();
149 $_SESSION = [];
150 foreach ( array_keys( $oldSession ) as $key ) {
151 $_SESSION[ $key ] = $oldSession[ $key ];
152 }
153 }
154 }
155
159 public function _cookiePath() {
160 static $sessionCookiePath = null;
161 if ( empty( $sessionCookiePath ) ) {
162 $this->mGetSessionCookiePath->call( new XCube_Ref( $sessionCookiePath ) );
163 if ( empty( $sessionCookiePath ) ) {
164 $sessionCookiePath = '/';
165 }
166 }
167
168 return $sessionCookiePath;
169 }
170}
[Final] Used for the simple mechanism for common delegation in XCube.
$mGetSessionCookiePath
[READ ONLY] XCube_Delegate
destroy( $forceCookieClear=false)
$mSetupSessionHandler
[READ ONLY] XCube_Delegate
$mSessionName
[READ ONLY] string
$mSessionLifetime
[READ ONLY] int
setParam( $sessionName='', $sessionExpire=0)