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 ini_set(
'session.use_cookies', 0 );
127 ini_set(
'session.use_trans_sid', 0 );
133 session_cache_limiter(
'' );
136 \Wikimedia\PhpSessionSerializer::setSerializeHandler();
140 session_set_save_handler( self::$instance,
true );
153 if ( $this->manager !==
$manager ) {
155 if ( $this->manager ) {
156 session_write_close();
161 \Wikimedia\PhpSessionSerializer::setLogger( $this->logger );
179 return defined(
'HHVM_VERSION' ) || version_compare( PHP_VERSION,
'7.0.0',
'>=' ) ?
true : 0;
189 return defined(
'HHVM_VERSION' ) || version_compare( PHP_VERSION,
'7.0.0',
'>=' ) ?
false : -1;
199 public function open( $save_path, $session_name ) {
200 if ( self::$instance !== $this ) {
201 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
203 if ( !$this->enable ) {
204 throw new \BadMethodCallException(
'Attempt to use PHP session management' );
215 if ( self::$instance !== $this ) {
216 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
218 $this->sessionFieldCache = [];
229 if ( self::$instance !== $this ) {
230 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
232 if ( !$this->enable ) {
233 throw new \BadMethodCallException(
'Attempt to use PHP session management' );
236 $session = $this->manager->getSessionById( $id,
false );
242 $data = iterator_to_array( $session );
243 $this->sessionFieldCache[$id] = $data;
244 return (
string)\Wikimedia\PhpSessionSerializer::encode( $data );
256 public function write( $id, $dataStr ) {
257 if ( self::$instance !== $this ) {
258 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
260 if ( !$this->enable ) {
261 throw new \BadMethodCallException(
'Attempt to use PHP session management' );
264 $session = $this->manager->getSessionById( $id,
true );
268 $this->logger->warning(
269 __METHOD__ .
': Session "{session}" cannot be loaded, skipping write.',
277 $data = \Wikimedia\PhpSessionSerializer::decode( $dataStr );
278 if ( $data ===
null ) {
286 $cache = isset( $this->sessionFieldCache[$id] ) ? $this->sessionFieldCache[$id] : [];
287 foreach ( $data
as $key =>
$value ) {
288 if ( !array_key_exists( $key,
$cache ) ) {
289 if ( $session->exists( $key ) ) {
291 $this->logger->warning(
292 __METHOD__ .
": Key \"$key\" added in both Session and \$_SESSION!"
296 $session->set( $key,
$value );
301 } elseif ( !$session->exists( $key ) ) {
303 $this->logger->warning(
304 __METHOD__ .
": Key \"$key\" deleted in Session and changed in \$_SESSION!"
306 $session->set( $key,
$value );
308 } elseif (
$cache[$key] === $session->get( $key ) ) {
310 $session->set( $key,
$value );
314 $this->logger->warning(
315 __METHOD__ .
": Key \"$key\" changed in both Session and \$_SESSION!"
321 \Wikimedia\PhpSessionSerializer::setLogger(
new \Psr\Log\NullLogger() );
323 if ( !array_key_exists( $key, $data ) && $session->exists( $key ) &&
324 \Wikimedia\PhpSessionSerializer::encode( [ $key =>
true ] )
326 if (
$cache[$key] === $session->get( $key ) ) {
328 $session->remove( $key );
332 $this->logger->warning(
333 __METHOD__ .
": Key \"$key\" changed in Session and deleted in \$_SESSION!"
338 \Wikimedia\PhpSessionSerializer::setLogger( $this->logger );
344 $this->logger->warning(
'Something wrote to $_SESSION!' );
348 $this->sessionFieldCache[$id] = iterator_to_array( $session );
363 if ( self::$instance !== $this ) {
364 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
366 if ( !$this->enable ) {
367 throw new \BadMethodCallException(
'Attempt to use PHP session management' );
369 $session = $this->manager->getSessionById( $id,
false );
383 public function gc( $maxlifetime ) {
384 if ( self::$instance !== $this ) {
385 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
387 $before = date(
'YmdHis', time() );
388 $this->
store->deleteObjectsExpiringBefore( $before );