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 | |
f299f7db | 15 | * @copyright CiviCRM LLC (c) 2004-2020 |
6a488035 | 16 | * $Id$ |
d7c8cf03 | 17 | * @param $filesDirectory |
6a488035 TO |
18 | */ |
19 | function 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 |
54 | function 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 |
66 | function 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 |
132 | function 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 | } | |
8e58c9aa | 148 | $db->query("SET NAMES utf8"); |
6a488035 | 149 | |
79c099b8 ML |
150 | $db->query("SET NAMES utf8"); |
151 | ||
6a488035 TO |
152 | if (!$lineMode) { |
153 | $string = file_get_contents($fileName); | |
154 | ||
155 | // change \r\n to fix windows issues | |
156 | $string = str_replace("\r\n", "\n", $string); | |
157 | ||
158 | //get rid of comments starting with # and -- | |
159 | ||
160 | $string = preg_replace("/^#[^\n]*$/m", "\n", $string); | |
161 | $string = preg_replace("/^(--[^-]).*/m", "\n", $string); | |
162 | ||
163 | $queries = preg_split('/;\s*$/m', $string); | |
164 | foreach ($queries as $query) { | |
165 | $query = trim($query); | |
166 | if (!empty($query)) { | |
167 | $res = &$db->query($query); | |
168 | if (PEAR::isError($res)) { | |
169 | print_r($res); | |
170 | die("Cannot execute $query: " . $res->getMessage()); | |
171 | } | |
172 | } | |
173 | } | |
174 | } | |
175 | else { | |
176 | $fd = fopen($fileName, "r"); | |
177 | while ($string = fgets($fd)) { | |
178 | $string = preg_replace("/^#[^\n]*$/m", "\n", $string); | |
179 | $string = preg_replace("/^(--[^-]).*/m", "\n", $string); | |
180 | ||
181 | $string = trim($string); | |
182 | if (!empty($string)) { | |
183 | $res = &$db->query($string); | |
184 | if (PEAR::isError($res)) { | |
185 | die("Cannot execute $string: " . $res->getMessage()); | |
186 | } | |
187 | } | |
188 | } | |
189 | } | |
190 | } | |
191 | ||
627456b5 EM |
192 | /** |
193 | * @param $config | |
194 | * | |
195 | * @return string | |
196 | */ | |
6a488035 TO |
197 | function civicrm_config(&$config) { |
198 | global $crmPath, $comPath; | |
199 | global $compileDir; | |
200 | global $tplPath, $installType; | |
201 | ||
f553d1ea | 202 | // Ex: $extraSettings[] = '$civicrm_settings["domain"]["foo"] = "bar";'; |
affcc9d2 | 203 | $extraSettings = []; |
f553d1ea | 204 | |
6a488035 TO |
205 | $params = array( |
206 | 'crmRoot' => $crmPath, | |
207 | 'templateCompileDir' => $compileDir, | |
208 | 'frontEnd' => 0, | |
209 | 'dbUser' => addslashes($config['mysql']['username']), | |
210 | 'dbPass' => addslashes($config['mysql']['password']), | |
211 | 'dbHost' => $config['mysql']['server'], | |
212 | 'dbName' => addslashes($config['mysql']['database']), | |
213 | ); | |
214 | ||
2e1f50d6 | 215 | $params['baseURL'] = $config['base_url'] ?? civicrm_cms_base(); |
09d7c1e5 | 216 | if ($installType == 'drupal' && defined('VERSION')) { |
3916ca43 SL |
217 | if (version_compare(VERSION, '8.0') >= 0) { |
218 | $params['cms'] = 'Drupal'; | |
219 | $params['CMSdbUser'] = addslashes($config['drupal']['username']); | |
220 | $params['CMSdbPass'] = addslashes($config['drupal']['password']); | |
221 | $params['CMSdbHost'] = $config['drupal']['host'] . ":" . !empty($config['drupal']['port']) ? $config['drupal']['port'] : "3306"; | |
222 | $params['CMSdbName'] = addslashes($config['drupal']['database']); | |
223 | } | |
224 | elseif (version_compare(VERSION, '7.0-rc1') >= 0) { | |
56fdfc52 | 225 | $params['cms'] = 'Drupal'; |
5126fe42 DL |
226 | $params['CMSdbUser'] = addslashes($config['drupal']['username']); |
227 | $params['CMSdbPass'] = addslashes($config['drupal']['password']); | |
228 | $params['CMSdbHost'] = $config['drupal']['server']; | |
229 | $params['CMSdbName'] = addslashes($config['drupal']['database']); | |
230 | } | |
231 | elseif (version_compare(VERSION, '6.0') >= 0) { | |
56fdfc52 | 232 | $params['cms'] = 'Drupal6'; |
5126fe42 DL |
233 | $params['CMSdbUser'] = addslashes($config['drupal']['username']); |
234 | $params['CMSdbPass'] = addslashes($config['drupal']['password']); | |
235 | $params['CMSdbHost'] = $config['drupal']['server']; | |
236 | $params['CMSdbName'] = addslashes($config['drupal']['database']); | |
237 | } | |
6a488035 | 238 | } |
68d1a6f6 | 239 | elseif ($installType == 'drupal') { |
09d7c1e5 EM |
240 | $params['cms'] = $config['cms']; |
241 | $params['CMSdbUser'] = addslashes($config['cmsdb']['username']); | |
242 | $params['CMSdbPass'] = addslashes($config['cmsdb']['password']); | |
243 | $params['CMSdbHost'] = $config['cmsdb']['server']; | |
244 | $params['CMSdbName'] = addslashes($config['cmsdb']['database']); | |
245 | } | |
5757adf3 HD |
246 | elseif ($installType == 'backdrop') { |
247 | $params['cms'] = 'Backdrop'; | |
248 | $params['CMSdbUser'] = addslashes($config['backdrop']['username']); | |
249 | $params['CMSdbPass'] = addslashes($config['backdrop']['password']); | |
250 | $params['CMSdbHost'] = $config['backdrop']['server']; | |
251 | $params['CMSdbName'] = addslashes($config['backdrop']['database']); | |
252 | } | |
6a488035 | 253 | else { |
56fdfc52 | 254 | $params['cms'] = 'WordPress'; |
6a488035 TO |
255 | $params['CMSdbUser'] = addslashes(DB_USER); |
256 | $params['CMSdbPass'] = addslashes(DB_PASSWORD); | |
257 | $params['CMSdbHost'] = DB_HOST; | |
258 | $params['CMSdbName'] = addslashes(DB_NAME); | |
5126fe42 DL |
259 | |
260 | // CRM-12386 | |
261 | $params['crmRoot'] = addslashes($params['crmRoot']); | |
f553d1ea KC |
262 | //CRM-16421 |
263 | ||
264 | $extraSettings[] = sprintf('$civicrm_paths[\'wp.frontend.base\'][\'url\'] = %s;', var_export(home_url() . '/', 1)); | |
265 | $extraSettings[] = sprintf('$civicrm_paths[\'wp.backend.base\'][\'url\'] = %s;', var_export(admin_url(), 1)); | |
266 | $extraSettings[] = sprintf('$civicrm_setting[\'URL Preferences\'][\'userFrameworkResourceURL\'] = %s;', var_export(plugin_dir_url(CIVICRM_PLUGIN_FILE) . 'civicrm', 1)); | |
267 | } | |
268 | ||
269 | if ($extraSettings) { | |
270 | $params['extraSettings'] = "Additional settings generated by installer:\n" . implode("\n", $extraSettings); | |
271 | } | |
272 | else { | |
273 | $params['extraSettings'] = ""; | |
6a488035 TO |
274 | } |
275 | ||
f782f7e3 TO |
276 | $params['siteKey'] = md5(rand() . mt_rand() . rand() . uniqid('', TRUE) . $params['baseURL']); |
277 | // Would prefer openssl_random_pseudo_bytes(), but I don't think it's universally available. | |
6a488035 | 278 | |
befcb21d | 279 | $str = file_get_contents($tplPath . 'civicrm.settings.php.template'); |
6a488035 TO |
280 | foreach ($params as $key => $value) { |
281 | $str = str_replace('%%' . $key . '%%', $value, $str); | |
282 | } | |
283 | return trim($str); | |
284 | } | |
285 | ||
627456b5 EM |
286 | /** |
287 | * @return string | |
288 | */ | |
6a488035 TO |
289 | function civicrm_cms_base() { |
290 | global $installType; | |
291 | ||
292 | // for drupal | |
293 | $numPrevious = 6; | |
294 | ||
295 | if (isset($_SERVER['HTTPS']) && | |
296 | !empty($_SERVER['HTTPS']) && | |
297 | strtolower($_SERVER['HTTPS']) != 'off' | |
298 | ) { | |
299 | $url = 'https://' . $_SERVER['HTTP_HOST']; | |
300 | } | |
301 | else { | |
302 | $url = 'http://' . $_SERVER['HTTP_HOST']; | |
303 | } | |
304 | ||
305 | $baseURL = $_SERVER['SCRIPT_NAME']; | |
306 | ||
5757adf3 | 307 | if ($installType == 'drupal' || $installType == 'backdrop') { |
6a488035 TO |
308 | //don't assume 6 dir levels, as civicrm |
309 | //may or may not be in sites/all/modules/ | |
310 | //lets allow to install in custom dir. CRM-6840 | |
311 | global $cmsPath; | |
312 | $crmDirLevels = str_replace($cmsPath, '', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME'])); | |
313 | $baseURL = str_replace($crmDirLevels, '', str_replace('\\', '/', $baseURL)); | |
314 | } | |
315 | elseif ($installType == 'wordpress') { | |
316 | $baseURL = str_replace($url, '', site_url()); | |
317 | } | |
318 | else { | |
319 | for ($i = 1; $i <= $numPrevious; $i++) { | |
320 | $baseURL = dirname($baseURL); | |
321 | } | |
322 | } | |
323 | ||
324 | // remove the last directory separator string from the directory | |
325 | if (substr($baseURL, -1, 1) == DIRECTORY_SEPARATOR) { | |
326 | $baseURL = substr($baseURL, 0, -1); | |
327 | } | |
328 | ||
329 | // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze | |
330 | $baseURL = str_replace(DIRECTORY_SEPARATOR, '/', $baseURL); | |
331 | ||
332 | if ($baseURL != '/') { | |
333 | $baseURL .= '/'; | |
334 | } | |
335 | ||
336 | return $url . $baseURL; | |
337 | } | |
338 | ||
627456b5 EM |
339 | /** |
340 | * @return string | |
341 | */ | |
6a488035 TO |
342 | function civicrm_home_url() { |
343 | $drupalURL = civicrm_cms_base(); | |
344 | return $drupalURL . 'index.php?q=civicrm'; | |
345 | } |