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