1 | <?php |
||
7 | class Installer { |
||
8 | public static $KEYS = array( |
||
9 | 'AUTH_KEY', |
||
10 | 'SECURE_AUTH_KEY', |
||
11 | 'LOGGED_IN_KEY', |
||
12 | 'NONCE_KEY', |
||
13 | 'AUTH_SALT', |
||
14 | 'SECURE_AUTH_SALT', |
||
15 | 'LOGGED_IN_SALT', |
||
16 | 'NONCE_SALT' |
||
17 | ); |
||
18 | |||
19 | public static function addSalts(Event $event) { |
||
20 | $root = dirname(dirname(dirname(__DIR__))); |
||
21 | $composer = $event->getComposer(); |
||
22 | $io = $event->getIO(); |
||
23 | |||
24 | if (!$io->isInteractive()) { |
||
25 | $generate_salts = $composer->getConfig()->get('generate-salts'); |
||
26 | } else { |
||
27 | $generate_salts = $io->askConfirmation('<info>Generate salts and append to .env file?</info> [<comment>Y,n</comment>]? ', true); |
||
28 | } |
||
29 | |||
30 | if (!$generate_salts) { |
||
31 | return 1; |
||
32 | } |
||
33 | |||
34 | $salts = array_map(function ($key) { |
||
35 | return sprintf("%s='%s'", $key, Installer::generateSalt()); |
||
36 | }, self::$KEYS); |
||
37 | |||
38 | $env_file = "{$root}/.env"; |
||
39 | |||
40 | if (copy("{$root}/.env.example", $env_file)) { |
||
41 | file_put_contents($env_file, implode($salts, "\n"), FILE_APPEND | LOCK_EX); |
||
42 | } else { |
||
43 | $io->write("<error>An error occured while copying your .env file</error>"); |
||
44 | return 1; |
||
45 | } |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Slightly modified/simpler version of wp_generate_password |
||
50 | * https://github.com/WordPress/WordPress/blob/cd8cedc40d768e9e1d5a5f5a08f1bd677c804cb9/wp-includes/pluggable.php#L1575 |
||
51 | */ |
||
52 | public static function generateSalt($length = 64) { |
||
53 | $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; |
||
54 | $chars .= '!@#$%^&*()'; |
||
55 | $chars .= '-_ []{}<>~`+=,.;:/?|'; |
||
56 | |||
57 | $salt = ''; |
||
58 | for ($i = 0; $i < $length; $i++) { |
||
59 | $salt .= substr($chars, rand(0, strlen($chars) - 1), 1); |
||
60 | } |
||
61 | |||
62 | return $salt; |
||
63 | } |
||
64 | } |
||
65 |