Commit f09c78aa by Qiang Xue

save security keys as a serialized string instead of exported variable.

parent 4b49a31f
<?php <?php
$params = require(__DIR__ . '/params.php'); $params = require(__DIR__ . '/params.php');
return [ return [
'id' => 'bootstrap-console', 'id' => 'basic-console',
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'preload' => ['log'], 'preload' => ['log'],
'controllerPath' => dirname(__DIR__) . '/commands', 'controllerPath' => dirname(__DIR__) . '/commands',
......
<?php <?php
$params = require(__DIR__ . '/params.php'); $params = require(__DIR__ . '/params.php');
$config = [ $config = [
'id' => 'bootstrap', 'id' => 'basic',
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'), 'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'),
'components' => [ 'components' => [
......
...@@ -175,7 +175,7 @@ class BaseSecurity ...@@ -175,7 +175,7 @@ class BaseSecurity
/** /**
* Returns a secret key associated with the specified name. * Returns a secret key associated with the specified name.
* If the secret key does not exist, a random key will be generated * If the secret key does not exist, a random key will be generated
* and saved in the file "keys.php" under the application's runtime directory * and saved in the file "keys.data" under the application's runtime directory
* so that the same secret key can be returned in future requests. * so that the same secret key can be returned in future requests.
* @param string $name the name that is associated with the secret key * @param string $name the name that is associated with the secret key
* @param integer $length the length of the key that should be generated if not exists * @param integer $length the length of the key that should be generated if not exists
...@@ -184,16 +184,16 @@ class BaseSecurity ...@@ -184,16 +184,16 @@ class BaseSecurity
public static function getSecretKey($name, $length = 32) public static function getSecretKey($name, $length = 32)
{ {
static $keys; static $keys;
$keyFile = Yii::$app->getRuntimePath() . '/keys.php'; $keyFile = Yii::$app->getRuntimePath() . '/keys.data';
if ($keys === null) { if ($keys === null) {
$keys = []; $keys = [];
if (is_file($keyFile)) { if (is_file($keyFile)) {
$keys = require($keyFile); $keys = unserialize(file_get_contents($keyFile));
} }
} }
if (!isset($keys[$name])) { if (!isset($keys[$name])) {
$keys[$name] = static::generateRandomKey($length); $keys[$name] = static::generateRandomKey($length);
file_put_contents($keyFile, "<?php\nreturn " . var_export($keys, true) . ";\n"); file_put_contents($keyFile, serialize($keys));
} }
return $keys[$name]; return $keys[$name];
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment