Merge pull request #16338 from mattwire/note_timestamp
[civicrm-core.git] / setup / plugins / installFiles / InstallSettingsFile.civi-setup.php
CommitLineData
4bcd4c62
TO
1<?php
2/**
3 * @file
4 *
5 * Generate the civicrm.settings.php file.
6 */
7
8if (!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\Setup\FileUtil::isCreateable($m->settingsPath)) {
32 $e->addError('system', 'settingsWritable', sprintf('The settings file "%s" cannot be created. Ensure the parent folder is writable.', $m->settingsPath));
33 }
34 else {
35 $e->addInfo('system', 'settingsWritable', sprintf('The settings file "%s" can be created.', $m->settingsPath));
36 }
37 });
38
39/**
40 * Read the $model and create the "civicrm.settings.php".
41 */
42\Civi\Setup::dispatcher()
43 ->addListener('civi.setup.installFiles', function (\Civi\Setup\Event\InstallFilesEvent $e) {
44 \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'installFiles'));
45
46 /**
47 * @var \Civi\Setup\Model $m
48 */
49 $m = $e->getModel();
50
51 // Map from the logical $model to civicrm.settings.php variables.
52 $params = array();
53 $params['crmRoot'] = addslashes(rtrim($m->srcPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR);
54 $params['templateCompileDir'] = addslashes($m->templateCompilePath);
55 // ??why is frontEnd=0??
56 $params['frontEnd'] = 0;
57 $params['baseURL'] = addslashes(rtrim($m->cmsBaseUrl, '/'));
58 $params['dbUser'] = addslashes($m->db['username']);
59 $params['dbPass'] = addslashes($m->db['password']);
60 $params['dbHost'] = addslashes($m->db['server']);
61 $params['dbName'] = addslashes($m->db['database']);
62 $params['cms'] = addslashes($m->cms);
63 $params['CMSdbUser'] = addslashes($m->cmsDb['username']);
64 $params['CMSdbPass'] = addslashes($m->cmsDb['password']);
65 $params['CMSdbHost'] = addslashes($m->cmsDb['server']);
66 $params['CMSdbName'] = addslashes($m->cmsDb['database']);
67 $params['siteKey'] = addslashes($m->siteKey);
68
69 $extraSettings = array();
70
71 foreach ($m->paths as $key => $aspects) {
72 foreach ($aspects as $aspect => $value) {
73 $extraSettings[] = sprintf('$civicrm_paths[%s][%s] = %s;', var_export($key, 1), var_export($aspect, 1), var_export($value, 1));
74 }
75 }
76
77 foreach ($m->mandatorySettings as $key => $value) {
78 $extraSettings[] = sprintf('$civicrm_setting[%s][%s] = %s;', '\'domain\'', var_export($key, 1), var_export($value, 1));
79 }
80
81 // FIXME $m->defaultSettings, $m->components, $m->extensions, $m->callbacks
82
83 if ($extraSettings) {
84 $params['extraSettings'] = "Additional settings generated by installer:\n" . implode("\n", $extraSettings);
85 }
86 else {
87 $params['extraSettings'] = "";
88 }
89
90 $parent = dirname($m->settingsPath);
91 if (!file_exists($parent)) {
92 Civi\Setup::log()->info('[InstallSettingsFile.civi-setup.php] mkdir "{path}"', ['path' => $parent]);
93 mkdir($parent, 0777, TRUE);
94 \Civi\Setup\FileUtil::makeWebWriteable($parent);
95 }
96
97 // And persist it...
98 $tplPath = implode(DIRECTORY_SEPARATOR,
99 [$m->srcPath, 'templates', 'CRM', 'common', 'civicrm.settings.php.template']
100 );
101 $str = file_get_contents($tplPath);
102 foreach ($params as $key => $value) {
103 $str = str_replace('%%' . $key . '%%', $value, $str);
104 }
105 $str = trim($str) . "\n";
106 file_put_contents($m->settingsPath, $str);
107
108 }, \Civi\Setup::PRIORITY_LATE);