26 use Psr\Log\LoggerInterface;
70 switch ( $PHPSessionHandling ) {
82 $this->enable =
false;
101 return self::$instance && self::$instance->enable;
109 if ( self::$instance ) {
115 if ( defined(
'MW_NO_SESSION_HANDLER' ) ) {
116 throw new \BadMethodCallException(
'MW_NO_SESSION_HANDLER is defined' );
120 self::$instance =
new self(
$manager );
123 session_write_close();
126 \Wikimedia\suppressWarnings();
129 ini_set(
'session.use_cookies', 0 );
130 ini_set(
'session.use_trans_sid', 0 );
136 session_cache_limiter(
'' );
139 \Wikimedia\PhpSessionSerializer::setSerializeHandler();
143 session_set_save_handler( self::$instance,
true );
145 \Wikimedia\restoreWarnings();
159 if ( $this->manager !==
$manager ) {
161 if ( $this->manager ) {
162 session_write_close();
167 \Wikimedia\PhpSessionSerializer::setLogger( $this->logger );
178 public function open( $save_path, $session_name ) {
179 if ( self::$instance !== $this ) {
180 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
182 if ( !$this->enable ) {
183 throw new \BadMethodCallException(
'Attempt to use PHP session management' );
194 if ( self::$instance !== $this ) {
195 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
197 $this->sessionFieldCache = [];
208 if ( self::$instance !== $this ) {
209 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
211 if ( !$this->enable ) {
212 throw new \BadMethodCallException(
'Attempt to use PHP session management' );
215 $session = $this->manager->getSessionById( $id,
false );
221 $data = iterator_to_array( $session );
222 $this->sessionFieldCache[$id] = $data;
223 return (
string)\Wikimedia\PhpSessionSerializer::encode( $data );
235 public function write( $id, $dataStr ) {
236 if ( self::$instance !== $this ) {
237 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
239 if ( !$this->enable ) {
240 throw new \BadMethodCallException(
'Attempt to use PHP session management' );
243 $session = $this->manager->getSessionById( $id,
true );
247 $this->logger->warning(
248 __METHOD__ .
': Session "{session}" cannot be loaded, skipping write.',
256 $data = \Wikimedia\PhpSessionSerializer::decode( $dataStr );
257 if ( $data ===
null ) {
265 $cache = $this->sessionFieldCache[$id] ?? [];
266 foreach ( $data
as $key =>
$value ) {
267 if ( !array_key_exists( $key,
$cache ) ) {
268 if ( $session->exists( $key ) ) {
270 $this->logger->warning(
271 __METHOD__ .
": Key \"$key\" added in both Session and \$_SESSION!"
275 $session->set( $key,
$value );
280 } elseif ( !$session->exists( $key ) ) {
282 $this->logger->warning(
283 __METHOD__ .
": Key \"$key\" deleted in Session and changed in \$_SESSION!"
285 $session->set( $key,
$value );
287 } elseif (
$cache[$key] === $session->get( $key ) ) {
289 $session->set( $key,
$value );
293 $this->logger->warning(
294 __METHOD__ .
": Key \"$key\" changed in both Session and \$_SESSION!"
300 \Wikimedia\PhpSessionSerializer::setLogger(
new \Psr\Log\NullLogger() );
302 if ( !array_key_exists( $key, $data ) && $session->exists( $key ) &&
303 \Wikimedia\PhpSessionSerializer::encode( [ $key =>
true ] )
305 if (
$cache[$key] === $session->get( $key ) ) {
307 $session->remove( $key );
311 $this->logger->warning(
312 __METHOD__ .
": Key \"$key\" changed in Session and deleted in \$_SESSION!"
317 \Wikimedia\PhpSessionSerializer::setLogger( $this->logger );
323 $this->logger->warning(
'Something wrote to $_SESSION!' );
327 $this->sessionFieldCache[$id] = iterator_to_array( $session );
342 if ( self::$instance !== $this ) {
343 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
345 if ( !$this->enable ) {
346 throw new \BadMethodCallException(
'Attempt to use PHP session management' );
348 $session = $this->manager->getSessionById( $id,
false );
362 public function gc( $maxlifetime ) {
363 if ( self::$instance !== $this ) {
364 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
366 $before = date(
'YmdHis', time() );
367 $this->
store->deleteObjectsExpiringBefore( $before );