Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
066989f0
Commit
066989f0
authored
Aug 01, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #4425: generate cookie validation keys in local config files.
parent
2a8ca600
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
186 additions
and
133 deletions
+186
-133
main.php
apps/advanced/backend/config/main.php
+0
-4
composer.json
apps/advanced/composer.json
+1
-6
main-local.php
apps/advanced/environments/dev/backend/config/main-local.php
+8
-1
main-local.php
.../advanced/environments/dev/frontend/config/main-local.php
+8
-1
index.php
apps/advanced/environments/index.php
+19
-5
main-local.php
.../advanced/environments/prod/backend/config/main-local.php
+6
-0
main-local.php
...advanced/environments/prod/frontend/config/main-local.php
+6
-0
main.php
apps/advanced/frontend/config/main.php
+0
-4
init
apps/advanced/init
+138
-112
No files found.
apps/advanced/backend/config/main.php
View file @
066989f0
...
...
@@ -13,10 +13,6 @@ return [
'bootstrap'
=>
[
'log'
],
'modules'
=>
[],
'components'
=>
[
'request'
=>
[
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey'
=>
''
,
],
'user'
=>
[
'identityClass'
=>
'common\models\User'
,
'enableAutoLogin'
=>
true
,
...
...
apps/advanced/composer.json
View file @
066989f0
...
...
@@ -32,8 +32,7 @@
},
"scripts"
:
{
"post-create-project-cmd"
:
[
"yii
\\
composer
\\
Installer::setPermission"
,
"yii
\\
composer
\\
Installer::generateCookieValidationKey"
"yii
\\
composer
\\
Installer::setPermission"
]
},
"config"
:
{
...
...
@@ -46,10 +45,6 @@
"frontend/runtime"
,
"frontend/web/assets"
],
"config"
:
[
"frontend/config/main.php"
,
"backend/config/main.php"
]
}
}
apps/advanced/environments/dev/backend/config/main-local.php
View file @
066989f0
<?php
$config
=
[];
$config
=
[
'components'
=>
[
'request'
=>
[
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey'
=>
''
,
],
],
];
if
(
!
YII_ENV_TEST
)
{
// configuration adjustments for 'dev' environment
...
...
apps/advanced/environments/dev/frontend/config/main-local.php
View file @
066989f0
<?php
$config
=
[];
$config
=
[
'components'
=>
[
'request'
=>
[
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey'
=>
''
,
],
],
];
if
(
!
YII_ENV_TEST
)
{
// configuration adjustments for 'dev' environment
...
...
apps/advanced/environments/index.php
View file @
066989f0
...
...
@@ -9,9 +9,15 @@
* return [
* 'environment name' => [
* 'path' => 'directory storing the local files',
* '
w
ritable' => [
* '
setW
ritable' => [
* // list of directories that should be set writable
* ],
* 'setExecutable' => [
* // list of directories that should be set executable
* ],
* 'setCookieValidationKey' => [
* // list of config files that need to be inserted with automatically generated cookie validation keys
* ],
* ],
* ];
* ```
...
...
@@ -19,26 +25,34 @@
return
[
'Development'
=>
[
'path'
=>
'dev'
,
'
w
ritable'
=>
[
'
setW
ritable'
=>
[
'backend/runtime'
,
'backend/web/assets'
,
'frontend/runtime'
,
'frontend/web/assets'
,
],
'
e
xecutable'
=>
[
'
setE
xecutable'
=>
[
'yii'
,
],
'setCookieValidationKey'
=>
[
'backend/config/main-local.php'
,
'frontend/config/main-local.php'
,
],
],
'Production'
=>
[
'path'
=>
'prod'
,
'
w
ritable'
=>
[
'
setW
ritable'
=>
[
'backend/runtime'
,
'backend/web/assets'
,
'frontend/runtime'
,
'frontend/web/assets'
,
],
'
e
xecutable'
=>
[
'
setE
xecutable'
=>
[
'yii'
,
],
'setCookieValidationKey'
=>
[
'backend/config/main-local.php'
,
'frontend/config/main-local.php'
,
],
],
];
apps/advanced/environments/prod/backend/config/main-local.php
View file @
066989f0
<?php
return
[
'components'
=>
[
'request'
=>
[
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey'
=>
''
,
],
],
];
apps/advanced/environments/prod/frontend/config/main-local.php
View file @
066989f0
<?php
return
[
'components'
=>
[
'request'
=>
[
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey'
=>
''
,
],
],
];
apps/advanced/frontend/config/main.php
View file @
066989f0
...
...
@@ -12,10 +12,6 @@ return [
'bootstrap'
=>
[
'log'
],
'controllerNamespace'
=>
'frontend\controllers'
,
'components'
=>
[
'request'
=>
[
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey'
=>
''
,
],
'user'
=>
[
'identityClass'
=>
'common\models\User'
,
'enableAutoLogin'
=>
true
,
...
...
apps/advanced/init
View file @
066989f0
...
...
@@ -14,6 +14,10 @@
* @license http://www.yiiframework.com/license/
*/
if
(
!
extension_loaded
(
'mcrypt'
))
{
die
(
'The mcrypt PHP extension is required by Yii2.'
);
}
$params
=
getParams
();
$root
=
str_replace
(
'\\'
,
'/'
,
__DIR__
);
$envs
=
require
(
"
$root
/environments/index.php"
);
...
...
@@ -23,147 +27,169 @@ echo "Yii Application Initialization Tool v1.0\n\n";
$envName
=
null
;
if
(
empty
(
$params
[
'env'
])
||
$params
[
'env'
]
===
'1'
)
{
echo
"Which environment do you want the application to be initialized in?
\n\n
"
;
foreach
(
$envNames
as
$i
=>
$name
)
{
echo
" [
$i
]
$name
\n
"
;
}
echo
"
\n
Your choice [0-"
.
(
count
(
$envs
)
-
1
)
.
', or "q" to quit] '
;
$answer
=
trim
(
fgets
(
STDIN
));
if
(
!
ctype_digit
(
$answer
)
||
!
in_array
(
$answer
,
range
(
0
,
count
(
$envs
)
-
1
)))
{
echo
"
\n
Quit initialization.
\n
"
;
exit
(
0
);
}
if
(
isset
(
$envNames
[
$answer
]))
{
$envName
=
$envNames
[
$answer
];
}
echo
"Which environment do you want the application to be initialized in?
\n\n
"
;
foreach
(
$envNames
as
$i
=>
$name
)
{
echo
" [
$i
]
$name
\n
"
;
}
echo
"
\n
Your choice [0-"
.
(
count
(
$envs
)
-
1
)
.
', or "q" to quit] '
;
$answer
=
trim
(
fgets
(
STDIN
));
if
(
!
ctype_digit
(
$answer
)
||
!
in_array
(
$answer
,
range
(
0
,
count
(
$envs
)
-
1
)))
{
echo
"
\n
Quit initialization.
\n
"
;
exit
(
0
);
}
if
(
isset
(
$envNames
[
$answer
]))
{
$envName
=
$envNames
[
$answer
];
}
}
else
{
$envName
=
$params
[
'env'
];
$envName
=
$params
[
'env'
];
}
if
(
!
in_array
(
$envName
,
$envNames
))
{
$envsList
=
implode
(
', '
,
$envNames
);
echo
"
\n
$envName
is not a valid environment. Try one of the following:
$envsList
.
\n
"
;
exit
(
2
);
$envsList
=
implode
(
', '
,
$envNames
);
echo
"
\n
$envName
is not a valid environment. Try one of the following:
$envsList
.
\n
"
;
exit
(
2
);
}
$env
=
$envs
[
$envName
];
if
(
empty
(
$params
[
'env'
]))
{
echo
"
\n
Initialize the application under '
{
$envNames
[
$answer
]
}
' environment? [yes|no] "
;
$answer
=
trim
(
fgets
(
STDIN
));
if
(
strncasecmp
(
$answer
,
'y'
,
1
))
{
echo
"
\n
Quit initialization.
\n
"
;
exit
(
0
);
}
echo
"
\n
Initialize the application under '
{
$envNames
[
$answer
]
}
' environment? [yes|no] "
;
$answer
=
trim
(
fgets
(
STDIN
));
if
(
strncasecmp
(
$answer
,
'y'
,
1
))
{
echo
"
\n
Quit initialization.
\n
"
;
exit
(
0
);
}
}
echo
"
\n
Start initialization ...
\n\n
"
;
$files
=
getFileList
(
"
$root
/environments/
{
$env
[
'path'
]
}
"
);
$all
=
false
;
foreach
(
$files
as
$file
)
{
if
(
!
copyFile
(
$root
,
"environments/
{
$env
[
'path'
]
}
/
$file
"
,
$file
,
$all
,
$params
))
{
break
;
}
}
if
(
isset
(
$env
[
'writable'
]))
{
foreach
(
$env
[
'writable'
]
as
$writable
)
{
echo
" chmod 0777
$writable
\n
"
;
@
chmod
(
"
$root
/
$writable
"
,
0777
);
}
if
(
!
copyFile
(
$root
,
"environments/
{
$env
[
'path'
]
}
/
$file
"
,
$file
,
$all
,
$params
))
{
break
;
}
}
if
(
isset
(
$env
[
'executable'
]))
{
foreach
(
$env
[
'executable'
]
as
$executable
)
{
echo
" chmod 0755
$executable
\n
"
;
@
chmod
(
"
$root
/
$executable
"
,
0755
);
}
$callbacks
=
[
'setCookieValidationKey'
,
'setWritable'
,
'setExecutable'
];
foreach
(
$callbacks
as
$callback
)
{
if
(
!
empty
(
$env
[
$callback
]))
{
$callback
(
$root
,
$env
[
$callback
]
);
}
}
echo
"
\n
... initialization completed.
\n\n
"
;
function
getFileList
(
$root
,
$basePath
=
''
)
{
$files
=
[];
$handle
=
opendir
(
$root
);
while
((
$path
=
readdir
(
$handle
))
!==
false
)
{
if
(
$path
===
'.svn'
||
$path
===
'.'
||
$path
===
'..'
)
{
continue
;
}
$fullPath
=
"
$root
/
$path
"
;
$relativePath
=
$basePath
===
''
?
$path
:
"
$basePath
/
$path
"
;
if
(
is_dir
(
$fullPath
))
{
$files
=
array_merge
(
$files
,
getFileList
(
$fullPath
,
$relativePath
));
}
else
{
$files
[]
=
$relativePath
;
}
}
closedir
(
$handle
);
return
$files
;
$files
=
[];
$handle
=
opendir
(
$root
);
while
((
$path
=
readdir
(
$handle
))
!==
false
)
{
if
(
$path
===
'.svn'
||
$path
===
'.'
||
$path
===
'..'
)
{
continue
;
}
$fullPath
=
"
$root
/
$path
"
;
$relativePath
=
$basePath
===
''
?
$path
:
"
$basePath
/
$path
"
;
if
(
is_dir
(
$fullPath
))
{
$files
=
array_merge
(
$files
,
getFileList
(
$fullPath
,
$relativePath
));
}
else
{
$files
[]
=
$relativePath
;
}
}
closedir
(
$handle
);
return
$files
;
}
function
copyFile
(
$root
,
$source
,
$target
,
&
$all
,
$params
)
{
if
(
!
is_file
(
$root
.
'/'
.
$source
))
{
echo
" skip
$target
(
$source
not exist)
\n
"
;
return
true
;
}
if
(
is_file
(
$root
.
'/'
.
$target
))
{
if
(
file_get_contents
(
$root
.
'/'
.
$source
)
===
file_get_contents
(
$root
.
'/'
.
$target
))
{
echo
" unchanged
$target
\n
"
;
return
true
;
}
if
(
$all
)
{
echo
" overwrite
$target
\n
"
;
}
else
{
echo
" exist
$target
\n
"
;
echo
" ...overwrite? [Yes|No|All|Quit] "
;
$answer
=
!
empty
(
$params
[
'overwrite'
])
?
$params
[
'overwrite'
]
:
trim
(
fgets
(
STDIN
));
if
(
!
strncasecmp
(
$answer
,
'q'
,
1
))
{
return
false
;
}
else
{
if
(
!
strncasecmp
(
$answer
,
'y'
,
1
))
{
echo
" overwrite
$target
\n
"
;
}
else
{
if
(
!
strncasecmp
(
$answer
,
'a'
,
1
))
{
echo
" overwrite
$target
\n
"
;
$all
=
true
;
}
else
{
echo
" skip
$target
\n
"
;
return
true
;
}
}
}
}
file_put_contents
(
$root
.
'/'
.
$target
,
file_get_contents
(
$root
.
'/'
.
$source
));
return
true
;
}
echo
" generate
$target
\n
"
;
@
mkdir
(
dirname
(
$root
.
'/'
.
$target
),
0777
,
true
);
file_put_contents
(
$root
.
'/'
.
$target
,
file_get_contents
(
$root
.
'/'
.
$source
));
return
true
;
if
(
!
is_file
(
$root
.
'/'
.
$source
))
{
echo
" skip
$target
(
$source
not exist)
\n
"
;
return
true
;
}
if
(
is_file
(
$root
.
'/'
.
$target
))
{
if
(
file_get_contents
(
$root
.
'/'
.
$source
)
===
file_get_contents
(
$root
.
'/'
.
$target
))
{
echo
" unchanged
$target
\n
"
;
return
true
;
}
if
(
$all
)
{
echo
" overwrite
$target
\n
"
;
}
else
{
echo
" exist
$target
\n
"
;
echo
" ...overwrite? [Yes|No|All|Quit] "
;
$answer
=
!
empty
(
$params
[
'overwrite'
])
?
$params
[
'overwrite'
]
:
trim
(
fgets
(
STDIN
));
if
(
!
strncasecmp
(
$answer
,
'q'
,
1
))
{
return
false
;
}
else
{
if
(
!
strncasecmp
(
$answer
,
'y'
,
1
))
{
echo
" overwrite
$target
\n
"
;
}
else
{
if
(
!
strncasecmp
(
$answer
,
'a'
,
1
))
{
echo
" overwrite
$target
\n
"
;
$all
=
true
;
}
else
{
echo
" skip
$target
\n
"
;
return
true
;
}
}
}
}
file_put_contents
(
$root
.
'/'
.
$target
,
file_get_contents
(
$root
.
'/'
.
$source
));
return
true
;
}
echo
" generate
$target
\n
"
;
@
mkdir
(
dirname
(
$root
.
'/'
.
$target
),
0777
,
true
);
file_put_contents
(
$root
.
'/'
.
$target
,
file_get_contents
(
$root
.
'/'
.
$source
));
return
true
;
}
function
getParams
()
{
$rawParams
=
[];
if
(
isset
(
$_SERVER
[
'argv'
]))
{
$rawParams
=
$_SERVER
[
'argv'
];
array_shift
(
$rawParams
);
}
$params
=
[];
foreach
(
$rawParams
as
$param
)
{
if
(
preg_match
(
'/^--(\w+)(=(.*))?$/'
,
$param
,
$matches
))
{
$name
=
$matches
[
1
];
$params
[
$name
]
=
isset
(
$matches
[
3
])
?
$matches
[
3
]
:
true
;
}
else
{
$params
[]
=
$param
;
}
}
return
$params
;
$rawParams
=
[];
if
(
isset
(
$_SERVER
[
'argv'
]))
{
$rawParams
=
$_SERVER
[
'argv'
];
array_shift
(
$rawParams
);
}
$params
=
[];
foreach
(
$rawParams
as
$param
)
{
if
(
preg_match
(
'/^--(\w+)(=(.*))?$/'
,
$param
,
$matches
))
{
$name
=
$matches
[
1
];
$params
[
$name
]
=
isset
(
$matches
[
3
])
?
$matches
[
3
]
:
true
;
}
else
{
$params
[]
=
$param
;
}
}
return
$params
;
}
function
setWritable
(
$root
,
$paths
)
{
foreach
(
$paths
as
$writable
)
{
echo
" chmod 0777
$writable
\n
"
;
@
chmod
(
"
$root
/
$writable
"
,
0777
);
}
}
function
setExecutable
(
$root
,
$paths
)
{
foreach
(
$paths
as
$executable
)
{
echo
" chmod 0755
$executable
\n
"
;
@
chmod
(
"
$root
/
$executable
"
,
0755
);
}
}
function
setCookieValidationKey
(
$root
,
$paths
)
{
foreach
(
$paths
as
$file
)
{
echo
" generating cookie validation key
$file
\n
"
;
$file
=
$root
.
'/'
.
$file
;
$length
=
32
;
$bytes
=
mcrypt_create_iv
(
$length
,
MCRYPT_DEV_URANDOM
);
$key
=
strtr
(
substr
(
base64_encode
(
$bytes
),
0
,
$length
),
'+/='
,
'_-.'
);
$content
=
preg_replace
(
'/(("|\')cookieValidationKey("|\')\s*=>\s*)(""|\'\')/'
,
"
\\
1'
$key
'"
,
file_get_contents
(
$file
));
file_put_contents
(
$file
,
$content
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment