| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Session; |
| 25 |
|
| 26 |
use InvalidArgumentException; |
| 27 |
use MediaWiki\Deferred\DeferredUpdates; |
| 28 |
use MediaWiki\HookContainer\HookContainer; |
| 29 |
use MediaWiki\HookContainer\HookRunner; |
| 30 |
use MediaWiki\MainConfigNames; |
| 31 |
use MediaWiki\MediaWikiServices; |
| 32 |
use MediaWiki\Request\WebRequest; |
| 33 |
use MediaWiki\User\User; |
| 34 |
use MWRestrictions; |
| 35 |
use Psr\Log\LoggerInterface; |
| 36 |
use Wikimedia\AtEase\AtEase; |
| 37 |
use Wikimedia\ObjectCache\CachedBagOStuff; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
final class SessionBackend { |
| 58 |
|
| 59 |
private $id; |
| 60 |
|
| 61 |
|
| 62 |
private $persist = false; |
| 63 |
|
| 64 |
|
| 65 |
private $remember = false; |
| 66 |
|
| 67 |
|
| 68 |
private $forceHTTPS = false; |
| 69 |
|
| 70 |
|
| 71 |
private $data = null; |
| 72 |
|
| 73 |
|
| 74 |
private $forcePersist = false; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private $persistenceChangeType; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
private $persistenceChangeData = []; |
| 91 |
|
| 92 |
|
| 93 |
private $metaDirty = false; |
| 94 |
|
| 95 |
|
| 96 |
private $dataDirty = false; |
| 97 |
|
| 98 |
|
| 99 |
private $dataHash = null; |
| 100 |
|
| 101 |
|
| 102 |
private $store; |
| 103 |
|
| 104 |
|
| 105 |
private $logger; |
| 106 |
|
| 107 |
|
| 108 |
private $hookRunner; |
| 109 |
|
| 110 |
|
| 111 |
private $lifetime; |
| 112 |
|
| 113 |
|
| 114 |
private $user; |
| 115 |
|
| 116 |
|
| 117 |
private $curIndex = 0; |
| 118 |
|
| 119 |
|
| 120 |
private $requests = []; |
| 121 |
|
| 122 |
|
| 123 |
private $provider; |
| 124 |
|
| 125 |
|
| 126 |
private $providerMetadata = null; |
| 127 |
|
| 128 |
|
| 129 |
private $expires = 0; |
| 130 |
|
| 131 |
|
| 132 |
private $loggedOut = 0; |
| 133 |
|
| 134 |
|
| 135 |
private $delaySave = 0; |
| 136 |
|
| 137 |
|
| 138 |
private $usePhpSessionHandling; |
| 139 |
|
| 140 |
private $checkPHPSessionRecursionGuard = false; |
| 141 |
|
| 142 |
|
| 143 |
private $shutdown = false; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function __construct( |
| 154 |
SessionId $id, SessionInfo $info, CachedBagOStuff $store, LoggerInterface $logger, |
| 155 |
HookContainer $hookContainer, $lifetime |
| 156 |
) { |
| 157 |
$phpSessionHandling = MediaWikiServices::getInstance()->getMainConfig() |
| 158 |
->get( MainConfigNames::PHPSessionHandling ); |
| 159 |
$this->usePhpSessionHandling = $phpSessionHandling !== 'disable'; |
| 160 |
|
| 161 |
if ( $info->getUserInfo() && !$info->getUserInfo()->isVerified() ) { |
| 162 |
throw new InvalidArgumentException( |
| 163 |
"Refusing to create session for unverified user {$info->getUserInfo()}" |
| 164 |
); |
| 165 |
} |
| 166 |
if ( $info->getProvider() === null ) { |
| 167 |
throw new InvalidArgumentException( 'Cannot create session without a provider' ); |
| 168 |
} |
| 169 |
if ( $info->getId() !== $id->getId() ) { |
| 170 |
throw new InvalidArgumentException( 'SessionId and SessionInfo don\'t match' ); |
| 171 |
} |
| 172 |
|
| 173 |
$this->id = $id; |
| 174 |
$this->user = $info->getUserInfo() |
| 175 |
? $info->getUserInfo()->getUser() |
| 176 |
: MediaWikiServices::getInstance()->getUserFactory()->newAnonymous(); |
| 177 |
$this->store = $store; |
| 178 |
$this->logger = $logger; |
| 179 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 180 |
$this->lifetime = $lifetime; |
| 181 |
$this->provider = $info->getProvider(); |
| 182 |
$this->persist = $info->wasPersisted(); |
| 183 |
$this->remember = $info->wasRemembered(); |
| 184 |
$this->forceHTTPS = $info->forceHTTPS(); |
| 185 |
$this->providerMetadata = $info->getProviderMetadata(); |
| 186 |
|
| 187 |
$blob = $store->get( $store->makeKey( 'MWSession', (string)$this->id ) ); |
| 188 |
if ( !is_array( $blob ) || |
| 189 |
!isset( $blob['metadata'] ) || !is_array( $blob['metadata'] ) || |
| 190 |
!isset( $blob['data'] ) || !is_array( $blob['data'] ) |
| 191 |
) { |
| 192 |
$this->data = []; |
| 193 |
$this->dataDirty = true; |
| 194 |
$this->metaDirty = true; |
| 195 |
$this->persistenceChangeType = 'no-store'; |
| 196 |
$this->logger->debug( |
| 197 |
'SessionBackend "{session}" is unsaved, marking dirty in constructor', |
| 198 |
[ |
| 199 |
'session' => $this->id->__toString(), |
| 200 |
] ); |
| 201 |
} else { |
| 202 |
$this->data = $blob['data']; |
| 203 |
if ( isset( $blob['metadata']['loggedOut'] ) ) { |
| 204 |
$this->loggedOut = (int)$blob['metadata']['loggedOut']; |
| 205 |
} |
| 206 |
if ( isset( $blob['metadata']['expires'] ) ) { |
| 207 |
$this->expires = (int)$blob['metadata']['expires']; |
| 208 |
} else { |
| 209 |
$this->metaDirty = true; |
| 210 |
$this->persistenceChangeType = 'no-expiry'; |
| 211 |
$this->logger->debug( |
| 212 |
'SessionBackend "{session}" metadata dirty due to missing expiration timestamp', |
| 213 |
[ |
| 214 |
'session' => $this->id->__toString(), |
| 215 |
] ); |
| 216 |
} |
| 217 |
} |
| 218 |
$this->dataHash = md5( serialize( $this->data ) ); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function getSession( WebRequest $request ) { |
| 227 |
$index = ++$this->curIndex; |
| 228 |
$this->requests[$index] = $request; |
| 229 |
$session = new Session( $this, $index, $this->logger ); |
| 230 |
return $session; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public function deregisterSession( $index ) { |
| 239 |
unset( $this->requests[$index] ); |
| 240 |
if ( !$this->shutdown && !count( $this->requests ) ) { |
| 241 |
$this->save( true ); |
| 242 |
$this->provider->getManager()->deregisterSessionBackend( $this ); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public function shutdown() { |
| 251 |
$this->save( true ); |
| 252 |
$this->shutdown = true; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public function getId() { |
| 260 |
return (string)$this->id; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public function getSessionId() { |
| 269 |
return $this->id; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public function resetId() { |
| 277 |
if ( $this->provider->persistsSessionId() ) { |
| 278 |
$oldId = (string)$this->id; |
| 279 |
$restart = $this->usePhpSessionHandling && $oldId === session_id() && |
| 280 |
PHPSessionHandler::isEnabled(); |
| 281 |
|
| 282 |
if ( $restart ) { |
| 283 |
|
| 284 |
|
| 285 |
session_write_close(); |
| 286 |
} |
| 287 |
|
| 288 |
$this->provider->getManager()->changeBackendId( $this ); |
| 289 |
$this->provider->sessionIdWasReset( $this, $oldId ); |
| 290 |
$this->metaDirty = true; |
| 291 |
$this->logger->debug( |
| 292 |
'SessionBackend "{session}" metadata dirty due to ID reset (formerly "{oldId}")', |
| 293 |
[ |
| 294 |
'session' => $this->id->__toString(), |
| 295 |
'oldId' => $oldId, |
| 296 |
] ); |
| 297 |
|
| 298 |
if ( $restart ) { |
| 299 |
session_id( (string)$this->id ); |
| 300 |
AtEase::quietCall( 'session_start' ); |
| 301 |
} |
| 302 |
|
| 303 |
$this->autosave(); |
| 304 |
|
| 305 |
|
| 306 |
$this->store->delete( $this->store->makeKey( 'MWSession', $oldId ) ); |
| 307 |
} |
| 308 |
|
| 309 |
return (string)$this->id; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public function getProvider() { |
| 317 |
return $this->provider; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
public function isPersistent() { |
| 328 |
return $this->persist; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public function persist() { |
| 338 |
if ( !$this->persist ) { |
| 339 |
$this->persist = true; |
| 340 |
$this->forcePersist = true; |
| 341 |
$this->metaDirty = true; |
| 342 |
$this->logger->debug( |
| 343 |
'SessionBackend "{session}" force-persist due to persist()', |
| 344 |
[ |
| 345 |
'session' => $this->id->__toString(), |
| 346 |
] ); |
| 347 |
$this->autosave(); |
| 348 |
} else { |
| 349 |
$this->renew(); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
public function unpersist() { |
| 357 |
if ( $this->persist ) { |
| 358 |
|
| 359 |
if ( $this->usePhpSessionHandling && PHPSessionHandler::isEnabled() && |
| 360 |
session_id() === (string)$this->id |
| 361 |
) { |
| 362 |
$this->logger->debug( |
| 363 |
'SessionBackend "{session}" Closing PHP session for unpersist', |
| 364 |
[ 'session' => $this->id->__toString() ] |
| 365 |
); |
| 366 |
session_write_close(); |
| 367 |
session_id( '' ); |
| 368 |
} |
| 369 |
|
| 370 |
$this->persist = false; |
| 371 |
$this->forcePersist = true; |
| 372 |
$this->metaDirty = true; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
$this->store->delete( $this->store->makeKey( 'MWSession', (string)$this->id ) ); |
| 377 |
|
| 378 |
$this->autosave(); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public function shouldRememberUser() { |
| 388 |
return $this->remember; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
public function setRememberUser( $remember ) { |
| 397 |
if ( $this->remember !== (bool)$remember ) { |
| 398 |
$this->remember = (bool)$remember; |
| 399 |
$this->metaDirty = true; |
| 400 |
$this->logger->debug( |
| 401 |
'SessionBackend "{session}" metadata dirty due to remember-user change', |
| 402 |
[ |
| 403 |
'session' => $this->id->__toString(), |
| 404 |
] ); |
| 405 |
$this->autosave(); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public function getRequest( $index ) { |
| 415 |
if ( !isset( $this->requests[$index] ) ) { |
| 416 |
throw new InvalidArgumentException( 'Invalid session index' ); |
| 417 |
} |
| 418 |
return $this->requests[$index]; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public function getUser(): User { |
| 426 |
return $this->user; |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
public function getAllowedUserRights() { |
| 434 |
return $this->provider->getAllowedUserRights( $this ); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public function getRestrictions(): ?MWRestrictions { |
| 443 |
return $this->provider->getRestrictions( $this->providerMetadata ); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
public function canSetUser() { |
| 451 |
return $this->provider->canChangeUser(); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public function setUser( $user ) { |
| 462 |
if ( !$this->canSetUser() ) { |
| 463 |
throw new \BadMethodCallException( |
| 464 |
'Cannot set user on this session; check $session->canSetUser() first' |
| 465 |
); |
| 466 |
} |
| 467 |
|
| 468 |
$this->user = $user; |
| 469 |
$this->metaDirty = true; |
| 470 |
$this->logger->debug( |
| 471 |
'SessionBackend "{session}" metadata dirty due to user change', |
| 472 |
[ |
| 473 |
'session' => $this->id->__toString(), |
| 474 |
] ); |
| 475 |
$this->autosave(); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
public function suggestLoginUsername( $index ) { |
| 484 |
if ( !isset( $this->requests[$index] ) ) { |
| 485 |
throw new InvalidArgumentException( 'Invalid session index' ); |
| 486 |
} |
| 487 |
return $this->provider->suggestLoginUsername( $this->requests[$index] ); |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
public function shouldForceHTTPS() { |
| 495 |
return $this->forceHTTPS; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
public function setForceHTTPS( $force ) { |
| 503 |
if ( $this->forceHTTPS !== (bool)$force ) { |
| 504 |
$this->forceHTTPS = (bool)$force; |
| 505 |
$this->metaDirty = true; |
| 506 |
$this->logger->debug( |
| 507 |
'SessionBackend "{session}" metadata dirty due to force-HTTPS change', |
| 508 |
[ |
| 509 |
'session' => $this->id->__toString(), |
| 510 |
] ); |
| 511 |
$this->autosave(); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
public function getLoggedOutTimestamp() { |
| 520 |
return $this->loggedOut; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
public function setLoggedOutTimestamp( $ts = null ) { |
| 527 |
$ts = (int)$ts; |
| 528 |
if ( $this->loggedOut !== $ts ) { |
| 529 |
$this->loggedOut = $ts; |
| 530 |
$this->metaDirty = true; |
| 531 |
$this->logger->debug( |
| 532 |
'SessionBackend "{session}" metadata dirty due to logged-out-timestamp change', |
| 533 |
[ |
| 534 |
'session' => $this->id->__toString(), |
| 535 |
] ); |
| 536 |
$this->autosave(); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
public function getProviderMetadata() { |
| 546 |
return $this->providerMetadata; |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
public function setProviderMetadata( $metadata ) { |
| 554 |
if ( $metadata !== null && !is_array( $metadata ) ) { |
| 555 |
throw new InvalidArgumentException( '$metadata must be an array or null' ); |
| 556 |
} |
| 557 |
if ( $this->providerMetadata !== $metadata ) { |
| 558 |
$this->providerMetadata = $metadata; |
| 559 |
$this->metaDirty = true; |
| 560 |
$this->logger->debug( |
| 561 |
'SessionBackend "{session}" metadata dirty due to provider metadata change', |
| 562 |
[ |
| 563 |
'session' => $this->id->__toString(), |
| 564 |
] ); |
| 565 |
$this->autosave(); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
public function &getData() { |
| 579 |
return $this->data; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
public function addData( array $newData ) { |
| 590 |
$data = &$this->getData(); |
| 591 |
foreach ( $newData as $key => $value ) { |
| 592 |
if ( !array_key_exists( $key, $data ) || $data[$key] !== $value ) { |
| 593 |
$data[$key] = $value; |
| 594 |
$this->dataDirty = true; |
| 595 |
$this->logger->debug( |
| 596 |
'SessionBackend "{session}" data dirty due to addData(): {callers}', |
| 597 |
[ |
| 598 |
'session' => $this->id->__toString(), |
| 599 |
'callers' => wfGetAllCallers( 5 ), |
| 600 |
] ); |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
public function dirty() { |
| 610 |
$this->dataDirty = true; |
| 611 |
$this->logger->debug( |
| 612 |
'SessionBackend "{session}" data dirty due to dirty(): {callers}', |
| 613 |
[ |
| 614 |
'session' => $this->id->__toString(), |
| 615 |
'callers' => wfGetAllCallers( 5 ), |
| 616 |
] ); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
public function renew() { |
| 626 |
if ( time() + $this->lifetime / 2 > $this->expires ) { |
| 627 |
$this->metaDirty = true; |
| 628 |
$this->logger->debug( |
| 629 |
'SessionBackend "{callers}" metadata dirty for renew(): {callers}', |
| 630 |
[ |
| 631 |
'session' => $this->id->__toString(), |
| 632 |
'callers' => wfGetAllCallers( 5 ), |
| 633 |
] ); |
| 634 |
if ( $this->persist ) { |
| 635 |
$this->persistenceChangeType = 'renew'; |
| 636 |
$this->forcePersist = true; |
| 637 |
$this->logger->debug( |
| 638 |
'SessionBackend "{session}" force-persist for renew(): {callers}', |
| 639 |
[ |
| 640 |
'session' => $this->id->__toString(), |
| 641 |
'callers' => wfGetAllCallers( 5 ), |
| 642 |
] ); |
| 643 |
} |
| 644 |
} |
| 645 |
$this->autosave(); |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
public function delaySave() { |
| 656 |
$this->delaySave++; |
| 657 |
return new \Wikimedia\ScopedCallback( function () { |
| 658 |
if ( --$this->delaySave <= 0 ) { |
| 659 |
$this->delaySave = 0; |
| 660 |
$this->save(); |
| 661 |
} |
| 662 |
} ); |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
private function autosave() { |
| 670 |
if ( $this->delaySave <= 0 ) { |
| 671 |
$this->save(); |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
public function save( $closing = false ) { |
| 685 |
$anon = $this->user->isAnon(); |
| 686 |
|
| 687 |
if ( !$anon && $this->provider->getManager()->isUserSessionPrevented( $this->user->getName() ) ) { |
| 688 |
$this->logger->debug( |
| 689 |
'SessionBackend "{session}" not saving, user {user} was ' . |
| 690 |
'passed to SessionManager::preventSessionsForUser', |
| 691 |
[ |
| 692 |
'session' => $this->id->__toString(), |
| 693 |
'user' => $this->user->__toString(), |
| 694 |
] ); |
| 695 |
return; |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
if ( !$anon && defined( 'MW_PHPUNIT_TEST' ) && MediaWikiServices::getInstance()->isStorageDisabled() ) { |
| 701 |
|
| 702 |
|
| 703 |
return; |
| 704 |
} |
| 705 |
if ( !$anon && !$this->user->getToken( false ) ) { |
| 706 |
$this->logger->debug( |
| 707 |
'SessionBackend "{session}" creating token for user {user} on save', |
| 708 |
[ |
| 709 |
'session' => $this->id->__toString(), |
| 710 |
'user' => $this->user->__toString(), |
| 711 |
] ); |
| 712 |
$this->user->setToken(); |
| 713 |
if ( !MediaWikiServices::getInstance()->getReadOnlyMode()->isReadOnly() ) { |
| 714 |
|
| 715 |
$user = $this->user; |
| 716 |
DeferredUpdates::addCallableUpdate( static function () use ( $user ) { |
| 717 |
$user->saveSettings(); |
| 718 |
} ); |
| 719 |
} |
| 720 |
$this->metaDirty = true; |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
if ( !$this->metaDirty && !$this->dataDirty && |
| 725 |
$this->dataHash !== md5( serialize( $this->data ) ) |
| 726 |
) { |
| 727 |
$this->logger->debug( |
| 728 |
'SessionBackend "{session}" data dirty due to hash mismatch, {expected} !== {got}', |
| 729 |
[ |
| 730 |
'session' => $this->id->__toString(), |
| 731 |
'expected' => $this->dataHash, |
| 732 |
'got' => md5( serialize( $this->data ) ), |
| 733 |
] ); |
| 734 |
$this->dataDirty = true; |
| 735 |
} |
| 736 |
|
| 737 |
if ( !$this->metaDirty && !$this->dataDirty && !$this->forcePersist ) { |
| 738 |
return; |
| 739 |
} |
| 740 |
|
| 741 |
$this->logger->debug( |
| 742 |
'SessionBackend "{session}" save: dataDirty={dataDirty} ' . |
| 743 |
'metaDirty={metaDirty} forcePersist={forcePersist}', |
| 744 |
[ |
| 745 |
'session' => $this->id->__toString(), |
| 746 |
'dataDirty' => (int)$this->dataDirty, |
| 747 |
'metaDirty' => (int)$this->metaDirty, |
| 748 |
'forcePersist' => (int)$this->forcePersist, |
| 749 |
] ); |
| 750 |
|
| 751 |
|
| 752 |
if ( $this->metaDirty || $this->forcePersist ) { |
| 753 |
if ( $this->persist ) { |
| 754 |
foreach ( $this->requests as $request ) { |
| 755 |
$request->setSessionId( $this->getSessionId() ); |
| 756 |
$this->logPersistenceChange( $request, true ); |
| 757 |
$this->provider->persistSession( $this, $request ); |
| 758 |
} |
| 759 |
if ( !$closing ) { |
| 760 |
$this->checkPHPSession(); |
| 761 |
} |
| 762 |
} else { |
| 763 |
foreach ( $this->requests as $request ) { |
| 764 |
if ( $request->getSessionId() === $this->id ) { |
| 765 |
$this->logPersistenceChange( $request, false ); |
| 766 |
$this->provider->unpersistSession( $request ); |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
$this->forcePersist = false; |
| 773 |
$this->persistenceChangeType = null; |
| 774 |
|
| 775 |
if ( !$this->metaDirty && !$this->dataDirty ) { |
| 776 |
return; |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
$metadata = $origMetadata = [ |
| 781 |
'provider' => (string)$this->provider, |
| 782 |
'providerMetadata' => $this->providerMetadata, |
| 783 |
'userId' => $anon ? 0 : $this->user->getId(), |
| 784 |
'userName' => MediaWikiServices::getInstance()->getUserNameUtils() |
| 785 |
->isValid( $this->user->getName() ) ? $this->user->getName() : null, |
| 786 |
'userToken' => $anon ? null : $this->user->getToken(), |
| 787 |
'remember' => !$anon && $this->remember, |
| 788 |
'forceHTTPS' => $this->forceHTTPS, |
| 789 |
'expires' => time() + $this->lifetime, |
| 790 |
'loggedOut' => $this->loggedOut, |
| 791 |
'persisted' => $this->persist, |
| 792 |
]; |
| 793 |
|
| 794 |
$this->hookRunner->onSessionMetadata( $this, $metadata, $this->requests ); |
| 795 |
|
| 796 |
foreach ( $origMetadata as $k => $v ) { |
| 797 |
if ( $metadata[$k] !== $v ) { |
| 798 |
throw new \UnexpectedValueException( "SessionMetadata hook changed metadata key \"$k\"" ); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
$flags = $this->persist ? 0 : CachedBagOStuff::WRITE_CACHE_ONLY; |
| 803 |
$this->store->set( |
| 804 |
$this->store->makeKey( 'MWSession', (string)$this->id ), |
| 805 |
[ |
| 806 |
'data' => $this->data, |
| 807 |
'metadata' => $metadata, |
| 808 |
], |
| 809 |
$metadata['expires'], |
| 810 |
$flags |
| 811 |
); |
| 812 |
|
| 813 |
$this->metaDirty = false; |
| 814 |
$this->dataDirty = false; |
| 815 |
$this->dataHash = md5( serialize( $this->data ) ); |
| 816 |
$this->expires = $metadata['expires']; |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
private function checkPHPSession() { |
| 824 |
if ( !$this->checkPHPSessionRecursionGuard ) { |
| 825 |
$this->checkPHPSessionRecursionGuard = true; |
| 826 |
$reset = new \Wikimedia\ScopedCallback( function () { |
| 827 |
$this->checkPHPSessionRecursionGuard = false; |
| 828 |
} ); |
| 829 |
|
| 830 |
if ( $this->usePhpSessionHandling && session_id() === '' && PHPSessionHandler::isEnabled() && |
| 831 |
SessionManager::getGlobalSession()->getId() === (string)$this->id |
| 832 |
) { |
| 833 |
$this->logger->debug( |
| 834 |
'SessionBackend "{session}" Taking over PHP session', |
| 835 |
[ |
| 836 |
'session' => $this->id->__toString(), |
| 837 |
] ); |
| 838 |
session_id( (string)$this->id ); |
| 839 |
AtEase::quietCall( 'session_start' ); |
| 840 |
} |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
private function logPersistenceChange( WebRequest $request, bool $persist ) { |
| 850 |
if ( !$this->isPersistent() && !$persist ) { |
| 851 |
|
| 852 |
|
| 853 |
return; |
| 854 |
} |
| 855 |
|
| 856 |
$verb = $persist ? 'Persisting' : 'Unpersisting'; |
| 857 |
if ( $this->persistenceChangeType === 'renew' ) { |
| 858 |
$message = "$verb session for renewal"; |
| 859 |
} elseif ( $this->persistenceChangeType === 'no-store' ) { |
| 860 |
$message = "$verb session due to no pre-existing stored session"; |
| 861 |
} elseif ( $this->persistenceChangeType === 'no-expiry' ) { |
| 862 |
$message = "$verb session due to lack of stored expiry"; |
| 863 |
} elseif ( $this->persistenceChangeType === null ) { |
| 864 |
$message = "$verb session for unknown reason"; |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
$id = $this->getId(); |
| 871 |
$user = $this->getUser()->isAnon() ? '<anon>' : $this->getUser()->getName(); |
| 872 |
if ( $this->persistenceChangeData |
| 873 |
&& $this->persistenceChangeData['id'] === $id |
| 874 |
&& $this->persistenceChangeData['user'] === $user |
| 875 |
|
| 876 |
&& $this->persistenceChangeData['message'] === $message |
| 877 |
) { |
| 878 |
return; |
| 879 |
} |
| 880 |
|
| 881 |
$this->persistenceChangeData = [ 'id' => $id, 'user' => $user, 'message' => $message ]; |
| 882 |
|
| 883 |
|
| 884 |
$this->logger->info( $message, [ |
| 885 |
'id' => $id, |
| 886 |
'provider' => get_class( $this->getProvider() ), |
| 887 |
'user' => $user, |
| 888 |
'clientip' => $request->getIP(), |
| 889 |
'userAgent' => $request->getHeader( 'user-agent' ), |
| 890 |
] ); |
| 891 |
} |
| 892 |
|
| 893 |
} |