Commit | Line | Data |
---|---|---|
4bcd4c62 TO |
1 | <?php |
2 | /** | |
3 | * @file | |
4 | * | |
5 | * Generate the site key. | |
6 | */ | |
7 | ||
8 | if (!defined('CIVI_SETUP')) { | |
9 | exit("Installation plugins must only be loaded by the installer.\n"); | |
10 | } | |
11 | ||
12 | \Civi\Setup::dispatcher() | |
13 | ->addListener('civi.setup.installFiles', function (\Civi\Setup\Event\InstallFilesEvent $e) { | |
14 | \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'installFiles')); | |
15 | ||
16 | $toAlphanum = function($bits) { | |
17 | return preg_replace(';[^a-zA-Z0-9];', '', base64_encode($bits)); | |
18 | }; | |
19 | ||
cfb48750 TO |
20 | if (!empty($e->getModel()->siteKey)) { |
21 | // skip | |
22 | } | |
23 | elseif (function_exists('random_bytes')) { | |
24 | $e->getModel()->siteKey = $toAlphanum(random_bytes(32)); | |
25 | } | |
26 | elseif (function_exists('openssl_random_pseudo_bytes')) { | |
27 | $e->getModel()->siteKey = $toAlphanum(openssl_random_pseudo_bytes(32)); | |
28 | } | |
29 | else { | |
30 | throw new \RuntimeException("Failed to generate a random site key"); | |
31 | } | |
4bcd4c62 TO |
32 | |
33 | \Civi\Setup::log()->info(sprintf('[%s] Done %s', basename(__FILE__), 'installFiles')); | |
34 | ||
35 | }, \Civi\Setup::PRIORITY_PREPARE); |