dev/core#2370 - Installer - Bump up entropy for autogenerated cred keys
[civicrm-core.git] / setup / plugins / installFiles / InstallSettingsFile.civi-setup.php
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
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
37 if (!\Civi\Setup\FileUtil::isCreateable($m->settingsPath)) {
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 }
44 }
45 else {
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 }
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']);
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));
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']);
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));
100 $params['siteKey'] = addslashes($m->siteKey);
101 $params['credKeys'] = addslashes(implode(' ', $m->credKeys));
102
103 $extraSettings = array();
104
105 foreach ($m->paths as $key => $aspects) {
106 foreach ($aspects as $aspect => $value) {
107 $extraSettings[] = sprintf('$civicrm_paths[%s][%s] = %s;', var_export($key, 1), var_export($aspect, 1), var_export($value, 1));
108 }
109 }
110
111 foreach ($m->mandatorySettings as $key => $value) {
112 $extraSettings[] = sprintf('$civicrm_setting[%s][%s] = %s;', '\'domain\'', var_export($key, 1), var_export($value, 1));
113 }
114
115 // FIXME $m->defaultSettings, $m->components, $m->extensions, $m->callbacks
116
117 if ($extraSettings) {
118 $params['extraSettings'] = "Additional settings generated by installer:\n" . implode("\n", $extraSettings);
119 }
120 else {
121 $params['extraSettings'] = "";
122 }
123
124 $parent = dirname($m->settingsPath);
125 if (!file_exists($parent)) {
126 Civi\Setup::log()->info('[InstallSettingsFile.civi-setup.php] mkdir "{path}"', ['path' => $parent]);
127 mkdir($parent, 0777, TRUE);
128 \Civi\Setup\FileUtil::makeWebWriteable($parent);
129 }
130
131 // And persist it...
132 $tplPath = implode(DIRECTORY_SEPARATOR,
133 [$m->srcPath, 'templates', 'CRM', 'common', 'civicrm.settings.php.template']
134 );
135 $str = file_get_contents($tplPath);
136 foreach ($params as $key => $value) {
137 $str = str_replace('%%' . $key . '%%', $value, $str);
138 }
139 $str = trim($str) . "\n";
140 file_put_contents($m->settingsPath, $str);
141
142 }, \Civi\Setup::PRIORITY_LATE);