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