5 * Generate the civicrm.settings.php file.
8 if (!defined('CIVI_SETUP')) {
9 exit("Installation plugins must only be loaded by the installer.\n");
13 * Validate the $model.
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'));
20 * @var \Civi\Setup\Model $m
24 if (empty($m->settingsPath
)) {
25 $e->addError('system', 'settingsPath', sprintf('The settingsPath is undefined.'));
28 $e->addInfo('system', 'settingsPath', sprintf('The settingsPath is defined.'));
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
34 $installed = \Civi\Setup
::instance()->checkInstalled();
35 $alreadyInstalled = $installed->isSettingInstalled() ||
$installed->isDatabaseInstalled();
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
));
42 $e->addError('system', 'settingsWritable', sprintf('The settings file "%s" cannot be created. Ensure the parent folder is writable.', $m->settingsPath
));
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
));
53 $e->addInfo('system', 'settingsWritable', sprintf('The settings file "%s" can be created.', $m->settingsPath
));
59 * Read the $model and create the "civicrm.settings.php".
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'));
66 * @var \Civi\Setup\Model $m
70 // Map from the logical $model to civicrm.settings.php variables.
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
));
103 $extraSettings = array();
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));
111 foreach ($m->mandatorySettings
as $key => $value) {
112 $extraSettings[] = sprintf('$civicrm_setting[%s][%s] = %s;', '\'domain\'', var_export($key, 1), var_export($value, 1));
115 // FIXME $m->defaultSettings, $m->components, $m->extensions, $m->callbacks
117 if ($extraSettings) {
118 $params['extraSettings'] = "Additional settings generated by installer:\n" . implode("\n", $extraSettings);
121 $params['extraSettings'] = "";
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);
132 $tplPath = implode(DIRECTORY_SEPARATOR
,
133 [$m->srcPath
, 'templates', 'CRM', 'common', 'civicrm.settings.php.template']
135 $str = file_get_contents($tplPath);
136 foreach ($params as $key => $value) {
137 $str = str_replace('%%' . $key . '%%', $value, $str);
139 $str = trim($str) . "\n";
140 file_put_contents($m->settingsPath
, $str);
142 }, \Civi\Setup
::PRIORITY_LATE
);