| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- $config = yii\helpers\ArrayHelper::merge(
- require __DIR__ . '/../config/main.php',
- require __DIR__ . '/../config/main-local.php'
- );
- function byteLength($string)
- {
- return mb_strlen($string, '8bit');
- }
- function byteSubstr($string, $start, $length = null){
- if ($length === null) {
- $length = byteLength($string);
- }
- return mb_substr($string, $start, $length, '8bit');
- }
- function compareString($expected, $actual)
- {
- if (!is_string($expected)) {
- throw new Exception('Expected expected value to be a string, ' . gettype($expected) . ' given.');
- }
- if (!is_string($actual)) {
- throw new Exception('Expected actual value to be a string, ' . gettype($actual) . ' given.');
- }
- if (function_exists('hash_equals')) {
- return hash_equals($expected, $actual);
- }
- $expected .= "\0";
- $actual .= "\0";
- $expectedLength = byteLength($expected);
- $actualLength = byteLength($actual);
- $diff = $expectedLength - $actualLength;
- for ($i = 0; $i < $actualLength; $i++) {
- $diff |= (ord($actual[$i]) ^ ord($expected[$i % $expectedLength]));
- }
- return $diff === 0;
- }
- function validateData($data, $key, $rawHash = false)
- {
- $macHash = 'sha256';
- $test = @hash_hmac($macHash, '', '', $rawHash);
- if (!$test) {
- throw new Exception('Failed to generate HMAC with hash algorithm: ' . $macHash);
- }
- $hashLength = byteLength($test);
- if (byteLength($data) >= $hashLength) {
- $hash = byteSubstr($data, 0, $hashLength);
- $pureData = byteSubstr($data, $hashLength, null);
- $calculatedHash = hash_hmac($macHash, $pureData, $key, $rawHash);
- if (compareString($hash, $calculatedHash)) {
- return $pureData;
- }
- }
- return false;
- }
- $rawHash = false;
- $key = $config['components']['request']['cookieValidationKey'];
- unset($config['components']['user']['loginUrl']);
- foreach ($_COOKIE as $name => $value) {
- $data = validateData($value, $key);
- if ($data === false) {
- continue;
- }
- if (defined('PHP_VERSION_ID') && PHP_VERSION_ID >= 70000) {
- $data = @unserialize($data, ['allowed_classes' => false]);
- } else {
- $data = @unserialize($data);
- }
- if (is_array($data) && isset($data[0], $data[1]) && $data[0] === $name) {
- $cookies_value_arr = json_decode($data[1],true);
- // if($name==$config['components']['user']['identityCookie']['name']){
- if(in_array($name,[$config['components']['user']['identityCookie']['name'],$config['components']['store_user']['identityCookie']['name'],$config['components']['mch_user']['identityCookie']['name']])){
- $config['components']['session']['cookieParams']['lifetime'] = isset( $cookies_value_arr[2])? $cookies_value_arr[2]:3600;
- $config['components']['session']['timeout'] = isset( $cookies_value_arr[2])? $cookies_value_arr[2]:3600;
- }
- }
- }
- return $config;
|