Commit | Line | Data |
---|---|---|
4bcd4c62 TO |
1 | <?php |
2 | /** | |
3 | * @file | |
4 | * | |
5 | * Generate the civicrm.settings.php file. | |
6 | */ | |
7 | ||
8 | if (!defined('CIVI_SETUP')) { | |
9 | exit("Installation plugins must only be loaded by the installer.\n"); | |
10 | } | |
11 | ||
12 | /** | |
13 | * Validate the $model. | |
14 | */ | |
15 | \Civi\Setup::dispatcher() | |
16 | ->addListener('civi.setup.checkRequirements', function(\Civi\Setup\Event\CheckRequirementsEvent $e) { | |
17 | \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'checkRequirements')); | |
18 | ||
19 | /** | |
20 | * @var \Civi\Setup\Model $m | |
21 | */ | |
22 | $m = $e->getModel(); | |
23 | ||
24 | if (empty($m->settingsPath)) { | |
25 | $e->addError('system', 'settingsPath', sprintf('The settingsPath is undefined.')); | |
26 | } | |
27 | else { | |
28 | $e->addInfo('system', 'settingsPath', sprintf('The settingsPath is defined.')); | |
29 | } | |
30 | ||
8a0992a5 | 31 | // If Civi is already installed, Drupal 8's status report page also calls us |
32 | // and so we need to modify the check slightly since we want the reverse | |
33 | // conditions. | |
34 | $installed = \Civi\Setup::instance()->checkInstalled(); | |
35 | $alreadyInstalled = $installed->isSettingInstalled() || $installed->isDatabaseInstalled(); | |
36 | ||
4bcd4c62 | 37 | if (!\Civi\Setup\FileUtil::isCreateable($m->settingsPath)) { |
8a0992a5 | 38 | if ($alreadyInstalled) { |
39 | $e->addInfo('system', 'settingsWritable', sprintf('The settings file "%s" is protected from writing.', $m->settingsPath)); | |
40 | } | |
41 | else { | |
42 | $e->addError('system', 'settingsWritable', sprintf('The settings file "%s" cannot be created. Ensure the parent folder is writable.', $m->settingsPath)); | |
43 | } | |
4bcd4c62 TO |
44 | } |
45 | else { | |
8a0992a5 | 46 | if ($alreadyInstalled) { |
47 | // Note if we were to output an error, we wouldn't be able to use | |
48 | // `cv core:install` to do an in-place reinstall since it would fail | |
49 | // requirements checks. | |
50 | $e->addWarning('system', 'settingsWritable', sprintf('The settings file "%s" should not be writable.', $m->settingsPath)); | |
51 | } | |
52 | else { | |
53 | $e->addInfo('system', 'settingsWritable', sprintf('The settings file "%s" can be created.', $m->settingsPath)); | |
54 | } | |
4bcd4c62 TO |
55 | } |
56 | }); | |
57 | ||
58 | /** | |
59 | * Read the $model and create the "civicrm.settings.php". | |
60 | */ | |
61 | \Civi\Setup::dispatcher() | |
62 | ->addListener('civi.setup.installFiles', function (\Civi\Setup\Event\InstallFilesEvent $e) { | |
63 | \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'installFiles')); | |
64 | ||
65 | /** | |
66 | * @var \Civi\Setup\Model $m | |
67 | */ | |
68 | $m = $e->getModel(); | |
69 | ||
70 | // Map from the logical $model to civicrm.settings.php variables. | |
71 | $params = array(); | |
72 | $params['crmRoot'] = addslashes(rtrim($m->srcPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR); | |
73 | $params['templateCompileDir'] = addslashes($m->templateCompilePath); | |
74 | // ??why is frontEnd=0?? | |
75 | $params['frontEnd'] = 0; | |
76 | $params['baseURL'] = addslashes(rtrim($m->cmsBaseUrl, '/')); | |
77 | $params['dbUser'] = addslashes($m->db['username']); | |
78 | $params['dbPass'] = addslashes($m->db['password']); | |
79 | $params['dbHost'] = addslashes($m->db['server']); | |
80 | $params['dbName'] = addslashes($m->db['database']); | |
04b1abad | 81 | // The '&' prefix is awkward, but we don't know what's already in the file. |
82 | // At the time of writing, it has ?new_link=true. If that is removed, | |
83 | // then need to update this. | |
84 | // The PHP_QUERY_RFC3986 is important because PEAR::DB will interpret plus | |
85 | // signs as a reference to its old DSN format and mangle the DSN, so we | |
86 | // need to use %20 for spaces. | |
87 | $params['dbSSL'] = empty($m->db['ssl_params']) ? '' : addslashes('&' . http_build_query($m->db['ssl_params'], '', '&', PHP_QUERY_RFC3986)); | |
4bcd4c62 TO |
88 | $params['cms'] = addslashes($m->cms); |
89 | $params['CMSdbUser'] = addslashes($m->cmsDb['username']); | |
90 | $params['CMSdbPass'] = addslashes($m->cmsDb['password']); | |
91 | $params['CMSdbHost'] = addslashes($m->cmsDb['server']); | |
92 | $params['CMSdbName'] = addslashes($m->cmsDb['database']); | |
fc2b5ff8 | 93 | // The '&' prefix is awkward, but we don't know what's already in the file. |
94 | // At the time of writing, it has ?new_link=true. If that is removed, | |
95 | // then need to update this. | |
96 | // The PHP_QUERY_RFC3986 is important because PEAR::DB will interpret plus | |
97 | // signs as a reference to its old DSN format and mangle the DSN, so we | |
98 | // need to use %20 for spaces. | |
99 | $params['CMSdbSSL'] = empty($m->cmsDb['ssl_params']) ? '' : addslashes('&' . http_build_query($m->cmsDb['ssl_params'], '', '&', PHP_QUERY_RFC3986)); | |
4bcd4c62 | 100 | $params['siteKey'] = addslashes($m->siteKey); |
2ce5684a | 101 | $params['credKeys'] = addslashes(implode(' ', $m->credKeys)); |
8846c128 | 102 | $params['signKeys'] = addslashes(implode(' ', $m->signKeys)); |
4bcd4c62 TO |
103 | |
104 | $extraSettings = array(); | |
105 | ||
106 | foreach ($m->paths as $key => $aspects) { | |
107 | foreach ($aspects as $aspect => $value) { | |
108 | $extraSettings[] = sprintf('$civicrm_paths[%s][%s] = %s;', var_export($key, 1), var_export($aspect, 1), var_export($value, 1)); | |
109 | } | |
110 | } | |
111 | ||
112 | foreach ($m->mandatorySettings as $key => $value) { | |
113 | $extraSettings[] = sprintf('$civicrm_setting[%s][%s] = %s;', '\'domain\'', var_export($key, 1), var_export($value, 1)); | |
114 | } | |
115 | ||
116 | // FIXME $m->defaultSettings, $m->components, $m->extensions, $m->callbacks | |
117 | ||
118 | if ($extraSettings) { | |
119 | $params['extraSettings'] = "Additional settings generated by installer:\n" . implode("\n", $extraSettings); | |
120 | } | |
121 | else { | |
122 | $params['extraSettings'] = ""; | |
123 | } | |
124 | ||
125 | $parent = dirname($m->settingsPath); | |
126 | if (!file_exists($parent)) { | |
127 | Civi\Setup::log()->info('[InstallSettingsFile.civi-setup.php] mkdir "{path}"', ['path' => $parent]); | |
128 | mkdir($parent, 0777, TRUE); | |
129 | \Civi\Setup\FileUtil::makeWebWriteable($parent); | |
130 | } | |
131 | ||
132 | // And persist it... | |
133 | $tplPath = implode(DIRECTORY_SEPARATOR, | |
134 | [$m->srcPath, 'templates', 'CRM', 'common', 'civicrm.settings.php.template'] | |
135 | ); | |
136 | $str = file_get_contents($tplPath); | |
137 | foreach ($params as $key => $value) { | |
138 | $str = str_replace('%%' . $key . '%%', $value, $str); | |
139 | } | |
140 | $str = trim($str) . "\n"; | |
141 | file_put_contents($m->settingsPath, $str); | |
142 | ||
143 | }, \Civi\Setup::PRIORITY_LATE); |