Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
6b7eb9df | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
6b7eb9df TO |
6 | | This code is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
f452d72c | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
d7c8cf03 | 16 | * @param $filesDirectory |
6a488035 TO |
17 | */ |
18 | function civicrm_setup($filesDirectory) { | |
19 | global $crmPath, $sqlPath, $pkgPath, $tplPath; | |
20 | global $compileDir; | |
21 | ||
a585d559 | 22 | // Setup classloader |
653c1eee | 23 | // This is needed to allow CiviCRM to be installed by drush. |
a585d559 TM |
24 | // TODO: move to civicrm.drush.inc drush_civicrm_install() |
25 | global $crmPath; | |
26 | require_once $crmPath . '/CRM/Core/ClassLoader.php'; | |
27 | CRM_Core_ClassLoader::singleton()->register(); | |
653c1eee | 28 | |
6a488035 TO |
29 | $sqlPath = $crmPath . DIRECTORY_SEPARATOR . 'sql'; |
30 | $tplPath = $crmPath . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'CRM' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR; | |
31 | ||
32 | if (!is_dir($filesDirectory)) { | |
33 | mkdir($filesDirectory, 0777); | |
34 | chmod($filesDirectory, 0777); | |
35 | } | |
36 | ||
37 | $scratchDir = $filesDirectory . DIRECTORY_SEPARATOR . 'civicrm'; | |
38 | if (!is_dir($scratchDir)) { | |
39 | mkdir($scratchDir, 0777); | |
40 | } | |
41 | ||
42 | $compileDir = $scratchDir . DIRECTORY_SEPARATOR . 'templates_c' . DIRECTORY_SEPARATOR; | |
43 | if (!is_dir($compileDir)) { | |
44 | mkdir($compileDir, 0777); | |
45 | } | |
46 | $compileDir = addslashes($compileDir); | |
47 | } | |
48 | ||
627456b5 | 49 | /** |
100fef9d | 50 | * @param string $name |
627456b5 EM |
51 | * @param $buffer |
52 | */ | |
6a488035 TO |
53 | function civicrm_write_file($name, &$buffer) { |
54 | $fd = fopen($name, "w"); | |
55 | if (!$fd) { | |
56 | die("Cannot open $name"); | |
57 | } | |
408b79bf | 58 | fwrite($fd, $buffer); |
6a488035 TO |
59 | fclose($fd); |
60 | } | |
61 | ||
627456b5 EM |
62 | /** |
63 | * @param $config | |
64 | */ | |
6a488035 TO |
65 | function civicrm_main(&$config) { |
66 | global $sqlPath, $crmPath, $cmsPath, $installType; | |
67 | ||
68 | if ($installType == 'drupal') { | |
2e1f50d6 | 69 | $siteDir = $config['site_dir'] ?? getSiteDir($cmsPath, $_SERVER['SCRIPT_FILENAME']); |
f553d1ea | 70 | civicrm_setup($cmsPath . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . $siteDir . DIRECTORY_SEPARATOR . 'files' |
6a488035 TO |
71 | ); |
72 | } | |
5757adf3 HD |
73 | elseif ($installType == 'backdrop') { |
74 | civicrm_setup($cmsPath . DIRECTORY_SEPARATOR . 'files'); | |
75 | } | |
6a488035 | 76 | elseif ($installType == 'wordpress') { |
7ba2c8ad KC |
77 | $upload_dir = wp_upload_dir(); |
78 | $files_dirname = $upload_dir['basedir']; | |
79 | civicrm_setup($files_dirname); | |
6a488035 TO |
80 | } |
81 | ||
3916ca43 SL |
82 | $parts = explode(':', $config['mysql']['server']); |
83 | if (empty($parts[1])) { | |
84 | $parts[1] = 3306; | |
85 | } | |
86 | $config['mysql']['server'] = implode(':', $parts); | |
6a488035 | 87 | |
3916ca43 | 88 | $dsn = "mysql://{$config['mysql']['username']}:{$config['mysql']['password']}@{$config['mysql']['server']}/{$config['mysql']['database']}?new_link=true"; |
6a488035 TO |
89 | civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm.mysql'); |
90 | ||
91 | if (!empty($config['loadGenerated'])) { | |
1d256104 | 92 | civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_generated.mysql'); |
6a488035 TO |
93 | } |
94 | else { | |
95 | if (isset($config['seedLanguage']) | |
96 | and preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $config['seedLanguage']) | |
97 | and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql") | |
98 | and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql") | |
99 | ) { | |
100 | civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql"); | |
101 | civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql"); | |
102 | } | |
103 | else { | |
104 | civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_data.mysql'); | |
105 | civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_acl.mysql'); | |
106 | } | |
107 | } | |
108 | ||
109 | // generate backend settings file | |
110 | if ($installType == 'drupal') { | |
111 | $configFile = $cmsPath . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . $siteDir . DIRECTORY_SEPARATOR . 'civicrm.settings.php'; | |
112 | } | |
5757adf3 HD |
113 | elseif ($installType == 'backdrop') { |
114 | $configFile = $cmsPath . DIRECTORY_SEPARATOR . 'civicrm.settings.php'; | |
115 | } | |
6a488035 | 116 | elseif ($installType == 'wordpress') { |
7ba2c8ad | 117 | $configFile = $files_dirname . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR . 'civicrm.settings.php'; |
6a488035 TO |
118 | } |
119 | ||
120 | $string = civicrm_config($config); | |
121 | civicrm_write_file($configFile, | |
122 | $string | |
123 | ); | |
6a488035 TO |
124 | } |
125 | ||
627456b5 EM |
126 | /** |
127 | * @param $dsn | |
100fef9d | 128 | * @param string $fileName |
627456b5 EM |
129 | * @param bool $lineMode |
130 | */ | |
6a488035 TO |
131 | function civicrm_source($dsn, $fileName, $lineMode = FALSE) { |
132 | global $crmPath; | |
4046d167 | 133 | |
3045e3f4 ML |
134 | // CRM-19699 See also CRM_Core_DAO for PHP7 mysqli compatiblity. |
135 | // Duplicated here because this is not using CRM_Core_DAO directly | |
136 | // and this function may be called directly from Drush. | |
137 | if (!defined('DB_DSN_MODE')) { | |
138 | define('DB_DSN_MODE', 'auto'); | |
139 | } | |
140 | ||
6a488035 TO |
141 | $db = DB::connect($dsn); |
142 | if (PEAR::isError($db)) { | |
143 | die("Cannot open $dsn: " . $db->getMessage()); | |
144 | } | |
d10ba6cb | 145 | $db->query('SET NAMES utf8mb4'); |
79c099b8 | 146 | |
6a488035 TO |
147 | if (!$lineMode) { |
148 | $string = file_get_contents($fileName); | |
149 | ||
150 | // change \r\n to fix windows issues | |
151 | $string = str_replace("\r\n", "\n", $string); | |
152 | ||
153 | //get rid of comments starting with # and -- | |
154 | ||
155 | $string = preg_replace("/^#[^\n]*$/m", "\n", $string); | |
156 | $string = preg_replace("/^(--[^-]).*/m", "\n", $string); | |
157 | ||
158 | $queries = preg_split('/;\s*$/m', $string); | |
159 | foreach ($queries as $query) { | |
160 | $query = trim($query); | |
161 | if (!empty($query)) { | |
162 | $res = &$db->query($query); | |
163 | if (PEAR::isError($res)) { | |
164 | print_r($res); | |
165 | die("Cannot execute $query: " . $res->getMessage()); | |
166 | } | |
167 | } | |
168 | } | |
169 | } | |
170 | else { | |
171 | $fd = fopen($fileName, "r"); | |
172 | while ($string = fgets($fd)) { | |
173 | $string = preg_replace("/^#[^\n]*$/m", "\n", $string); | |
174 | $string = preg_replace("/^(--[^-]).*/m", "\n", $string); | |
175 | ||
176 | $string = trim($string); | |
177 | if (!empty($string)) { | |
178 | $res = &$db->query($string); | |
179 | if (PEAR::isError($res)) { | |
180 | die("Cannot execute $string: " . $res->getMessage()); | |
181 | } | |
182 | } | |
183 | } | |
184 | } | |
185 | } | |
186 | ||
627456b5 EM |
187 | /** |
188 | * @param $config | |
189 | * | |
190 | * @return string | |
191 | */ | |
6a488035 TO |
192 | function civicrm_config(&$config) { |
193 | global $crmPath, $comPath; | |
194 | global $compileDir; | |
195 | global $tplPath, $installType; | |
196 | ||
f553d1ea | 197 | // Ex: $extraSettings[] = '$civicrm_settings["domain"]["foo"] = "bar";'; |
affcc9d2 | 198 | $extraSettings = []; |
f553d1ea | 199 | |
6a488035 TO |
200 | $params = array( |
201 | 'crmRoot' => $crmPath, | |
202 | 'templateCompileDir' => $compileDir, | |
203 | 'frontEnd' => 0, | |
204 | 'dbUser' => addslashes($config['mysql']['username']), | |
205 | 'dbPass' => addslashes($config['mysql']['password']), | |
206 | 'dbHost' => $config['mysql']['server'], | |
207 | 'dbName' => addslashes($config['mysql']['database']), | |
2057c8cc | 208 | // These need to be filled manually when using the old installer if an |
209 | // SSL connection to MySQL is needed. | |
210 | 'dbSSL' => '', | |
211 | 'CMSdbSSL' => '', | |
6a488035 TO |
212 | ); |
213 | ||
2e1f50d6 | 214 | $params['baseURL'] = $config['base_url'] ?? civicrm_cms_base(); |
09d7c1e5 | 215 | if ($installType == 'drupal' && defined('VERSION')) { |
3916ca43 SL |
216 | if (version_compare(VERSION, '8.0') >= 0) { |
217 | $params['cms'] = 'Drupal'; | |
218 | $params['CMSdbUser'] = addslashes($config['drupal']['username']); | |
219 | $params['CMSdbPass'] = addslashes($config['drupal']['password']); | |
220 | $params['CMSdbHost'] = $config['drupal']['host'] . ":" . !empty($config['drupal']['port']) ? $config['drupal']['port'] : "3306"; | |
221 | $params['CMSdbName'] = addslashes($config['drupal']['database']); | |
222 | } | |
223 | elseif (version_compare(VERSION, '7.0-rc1') >= 0) { | |
56fdfc52 | 224 | $params['cms'] = 'Drupal'; |
5126fe42 DL |
225 | $params['CMSdbUser'] = addslashes($config['drupal']['username']); |
226 | $params['CMSdbPass'] = addslashes($config['drupal']['password']); | |
227 | $params['CMSdbHost'] = $config['drupal']['server']; | |
228 | $params['CMSdbName'] = addslashes($config['drupal']['database']); | |
229 | } | |
230 | elseif (version_compare(VERSION, '6.0') >= 0) { | |
56fdfc52 | 231 | $params['cms'] = 'Drupal6'; |
5126fe42 DL |
232 | $params['CMSdbUser'] = addslashes($config['drupal']['username']); |
233 | $params['CMSdbPass'] = addslashes($config['drupal']['password']); | |
234 | $params['CMSdbHost'] = $config['drupal']['server']; | |
235 | $params['CMSdbName'] = addslashes($config['drupal']['database']); | |
236 | } | |
6a488035 | 237 | } |
68d1a6f6 | 238 | elseif ($installType == 'drupal') { |
09d7c1e5 EM |
239 | $params['cms'] = $config['cms']; |
240 | $params['CMSdbUser'] = addslashes($config['cmsdb']['username']); | |
241 | $params['CMSdbPass'] = addslashes($config['cmsdb']['password']); | |
242 | $params['CMSdbHost'] = $config['cmsdb']['server']; | |
243 | $params['CMSdbName'] = addslashes($config['cmsdb']['database']); | |
244 | } | |
5757adf3 HD |
245 | elseif ($installType == 'backdrop') { |
246 | $params['cms'] = 'Backdrop'; | |
247 | $params['CMSdbUser'] = addslashes($config['backdrop']['username']); | |
248 | $params['CMSdbPass'] = addslashes($config['backdrop']['password']); | |
249 | $params['CMSdbHost'] = $config['backdrop']['server']; | |
250 | $params['CMSdbName'] = addslashes($config['backdrop']['database']); | |
251 | } | |
6a488035 | 252 | else { |
56fdfc52 | 253 | $params['cms'] = 'WordPress'; |
6a488035 TO |
254 | $params['CMSdbUser'] = addslashes(DB_USER); |
255 | $params['CMSdbPass'] = addslashes(DB_PASSWORD); | |
256 | $params['CMSdbHost'] = DB_HOST; | |
257 | $params['CMSdbName'] = addslashes(DB_NAME); | |
5126fe42 DL |
258 | |
259 | // CRM-12386 | |
260 | $params['crmRoot'] = addslashes($params['crmRoot']); | |
f553d1ea KC |
261 | //CRM-16421 |
262 | ||
263 | $extraSettings[] = sprintf('$civicrm_paths[\'wp.frontend.base\'][\'url\'] = %s;', var_export(home_url() . '/', 1)); | |
264 | $extraSettings[] = sprintf('$civicrm_paths[\'wp.backend.base\'][\'url\'] = %s;', var_export(admin_url(), 1)); | |
265 | $extraSettings[] = sprintf('$civicrm_setting[\'URL Preferences\'][\'userFrameworkResourceURL\'] = %s;', var_export(plugin_dir_url(CIVICRM_PLUGIN_FILE) . 'civicrm', 1)); | |
266 | } | |
267 | ||
268 | if ($extraSettings) { | |
269 | $params['extraSettings'] = "Additional settings generated by installer:\n" . implode("\n", $extraSettings); | |
270 | } | |
271 | else { | |
272 | $params['extraSettings'] = ""; | |
6a488035 TO |
273 | } |
274 | ||
f782f7e3 TO |
275 | $params['siteKey'] = md5(rand() . mt_rand() . rand() . uniqid('', TRUE) . $params['baseURL']); |
276 | // Would prefer openssl_random_pseudo_bytes(), but I don't think it's universally available. | |
6a488035 | 277 | |
befcb21d | 278 | $str = file_get_contents($tplPath . 'civicrm.settings.php.template'); |
6a488035 TO |
279 | foreach ($params as $key => $value) { |
280 | $str = str_replace('%%' . $key . '%%', $value, $str); | |
281 | } | |
282 | return trim($str); | |
283 | } | |
284 | ||
627456b5 EM |
285 | /** |
286 | * @return string | |
287 | */ | |
6a488035 TO |
288 | function civicrm_cms_base() { |
289 | global $installType; | |
290 | ||
291 | // for drupal | |
292 | $numPrevious = 6; | |
293 | ||
294 | if (isset($_SERVER['HTTPS']) && | |
295 | !empty($_SERVER['HTTPS']) && | |
296 | strtolower($_SERVER['HTTPS']) != 'off' | |
297 | ) { | |
298 | $url = 'https://' . $_SERVER['HTTP_HOST']; | |
299 | } | |
300 | else { | |
301 | $url = 'http://' . $_SERVER['HTTP_HOST']; | |
302 | } | |
303 | ||
304 | $baseURL = $_SERVER['SCRIPT_NAME']; | |
305 | ||
5757adf3 | 306 | if ($installType == 'drupal' || $installType == 'backdrop') { |
6a488035 TO |
307 | //don't assume 6 dir levels, as civicrm |
308 | //may or may not be in sites/all/modules/ | |
309 | //lets allow to install in custom dir. CRM-6840 | |
310 | global $cmsPath; | |
311 | $crmDirLevels = str_replace($cmsPath, '', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME'])); | |
312 | $baseURL = str_replace($crmDirLevels, '', str_replace('\\', '/', $baseURL)); | |
313 | } | |
314 | elseif ($installType == 'wordpress') { | |
315 | $baseURL = str_replace($url, '', site_url()); | |
316 | } | |
317 | else { | |
318 | for ($i = 1; $i <= $numPrevious; $i++) { | |
319 | $baseURL = dirname($baseURL); | |
320 | } | |
321 | } | |
322 | ||
323 | // remove the last directory separator string from the directory | |
324 | if (substr($baseURL, -1, 1) == DIRECTORY_SEPARATOR) { | |
325 | $baseURL = substr($baseURL, 0, -1); | |
326 | } | |
327 | ||
328 | // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze | |
329 | $baseURL = str_replace(DIRECTORY_SEPARATOR, '/', $baseURL); | |
330 | ||
331 | if ($baseURL != '/') { | |
332 | $baseURL .= '/'; | |
333 | } | |
334 | ||
335 | return $url . $baseURL; | |
336 | } | |
337 | ||
627456b5 EM |
338 | /** |
339 | * @return string | |
340 | */ | |
6a488035 TO |
341 | function civicrm_home_url() { |
342 | $drupalURL = civicrm_cms_base(); | |
343 | return $drupalURL . 'index.php?q=civicrm'; | |
344 | } |