session.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. $config = yii\helpers\ArrayHelper::merge(
  3. require __DIR__ . '/../config/main.php',
  4. require __DIR__ . '/../config/main-local.php'
  5. );
  6. function byteLength($string)
  7. {
  8. return mb_strlen($string, '8bit');
  9. }
  10. function byteSubstr($string, $start, $length = null){
  11. if ($length === null) {
  12. $length = byteLength($string);
  13. }
  14. return mb_substr($string, $start, $length, '8bit');
  15. }
  16. function compareString($expected, $actual)
  17. {
  18. if (!is_string($expected)) {
  19. throw new Exception('Expected expected value to be a string, ' . gettype($expected) . ' given.');
  20. }
  21. if (!is_string($actual)) {
  22. throw new Exception('Expected actual value to be a string, ' . gettype($actual) . ' given.');
  23. }
  24. if (function_exists('hash_equals')) {
  25. return hash_equals($expected, $actual);
  26. }
  27. $expected .= "\0";
  28. $actual .= "\0";
  29. $expectedLength = byteLength($expected);
  30. $actualLength = byteLength($actual);
  31. $diff = $expectedLength - $actualLength;
  32. for ($i = 0; $i < $actualLength; $i++) {
  33. $diff |= (ord($actual[$i]) ^ ord($expected[$i % $expectedLength]));
  34. }
  35. return $diff === 0;
  36. }
  37. function validateData($data, $key, $rawHash = false)
  38. {
  39. $macHash = 'sha256';
  40. $test = @hash_hmac($macHash, '', '', $rawHash);
  41. if (!$test) {
  42. throw new Exception('Failed to generate HMAC with hash algorithm: ' . $macHash);
  43. }
  44. $hashLength = byteLength($test);
  45. if (byteLength($data) >= $hashLength) {
  46. $hash = byteSubstr($data, 0, $hashLength);
  47. $pureData = byteSubstr($data, $hashLength, null);
  48. $calculatedHash = hash_hmac($macHash, $pureData, $key, $rawHash);
  49. if (compareString($hash, $calculatedHash)) {
  50. return $pureData;
  51. }
  52. }
  53. return false;
  54. }
  55. $rawHash = false;
  56. $key = $config['components']['request']['cookieValidationKey'];
  57. unset($config['components']['user']['loginUrl']);
  58. foreach ($_COOKIE as $name => $value) {
  59. $data = validateData($value, $key);
  60. if ($data === false) {
  61. continue;
  62. }
  63. if (defined('PHP_VERSION_ID') && PHP_VERSION_ID >= 70000) {
  64. $data = @unserialize($data, ['allowed_classes' => false]);
  65. } else {
  66. $data = @unserialize($data);
  67. }
  68. if (is_array($data) && isset($data[0], $data[1]) && $data[0] === $name) {
  69. $cookies_value_arr = json_decode($data[1],true);
  70. // if($name==$config['components']['user']['identityCookie']['name']){
  71. if(in_array($name,[$config['components']['user']['identityCookie']['name'],$config['components']['store_user']['identityCookie']['name'],$config['components']['mch_user']['identityCookie']['name']])){
  72. $config['components']['session']['cookieParams']['lifetime'] = isset( $cookies_value_arr[2])? $cookies_value_arr[2]:3600;
  73. $config['components']['session']['timeout'] = isset( $cookies_value_arr[2])? $cookies_value_arr[2]:3600;
  74. }
  75. }
  76. }
  77. return $config;