Merge pull request #16733 from eileenmcnaughton/smarty
[civicrm-core.git] / install / civicrm.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC (c) 2004-2020
16 * $Id$
17 * @param $filesDirectory
18 */
19 function civicrm_setup($filesDirectory) {
20 global $crmPath, $sqlPath, $pkgPath, $tplPath;
21 global $compileDir;
22
23 // Setup classloader
24 // This is needed to allow CiviCRM to be installed by drush.
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();
29
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
50 /**
51 * @param string $name
52 * @param $buffer
53 */
54 function civicrm_write_file($name, &$buffer) {
55 $fd = fopen($name, "w");
56 if (!$fd) {
57 die("Cannot open $name");
58 }
59 fwrite($fd, $buffer);
60 fclose($fd);
61 }
62
63 /**
64 * @param $config
65 */
66 function civicrm_main(&$config) {
67 global $sqlPath, $crmPath, $cmsPath, $installType;
68
69 if ($installType == 'drupal') {
70 $siteDir = $config['site_dir'] ?? getSiteDir($cmsPath, $_SERVER['SCRIPT_FILENAME']);
71 civicrm_setup($cmsPath . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . $siteDir . DIRECTORY_SEPARATOR . 'files'
72 );
73 }
74 elseif ($installType == 'backdrop') {
75 civicrm_setup($cmsPath . DIRECTORY_SEPARATOR . 'files');
76 }
77 elseif ($installType == 'wordpress') {
78 $upload_dir = wp_upload_dir();
79 $files_dirname = $upload_dir['basedir'];
80 civicrm_setup($files_dirname);
81 }
82
83 $parts = explode(':', $config['mysql']['server']);
84 if (empty($parts[1])) {
85 $parts[1] = 3306;
86 }
87 $config['mysql']['server'] = implode(':', $parts);
88
89 $dsn = "mysql://{$config['mysql']['username']}:{$config['mysql']['password']}@{$config['mysql']['server']}/{$config['mysql']['database']}?new_link=true";
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 }
114 elseif ($installType == 'backdrop') {
115 $configFile = $cmsPath . DIRECTORY_SEPARATOR . 'civicrm.settings.php';
116 }
117 elseif ($installType == 'wordpress') {
118 $configFile = $files_dirname . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR . 'civicrm.settings.php';
119 }
120
121 $string = civicrm_config($config);
122 civicrm_write_file($configFile,
123 $string
124 );
125 }
126
127 /**
128 * @param $dsn
129 * @param string $fileName
130 * @param bool $lineMode
131 */
132 function civicrm_source($dsn, $fileName, $lineMode = FALSE) {
133 global $crmPath;
134
135 require_once "$crmPath/packages/DB.php";
136
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
144 $db = DB::connect($dsn);
145 if (PEAR::isError($db)) {
146 die("Cannot open $dsn: " . $db->getMessage());
147 }
148 $db->query("SET NAMES utf8");
149
150 $db->query("SET NAMES utf8");
151
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
192 /**
193 * @param $config
194 *
195 * @return string
196 */
197 function civicrm_config(&$config) {
198 global $crmPath, $comPath;
199 global $compileDir;
200 global $tplPath, $installType;
201
202 // Ex: $extraSettings[] = '$civicrm_settings["domain"]["foo"] = "bar";';
203 $extraSettings = array();
204
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
215 $params['baseURL'] = $config['base_url'] ?? civicrm_cms_base();
216 if ($installType == 'drupal' && defined('VERSION')) {
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) {
225 $params['cms'] = 'Drupal';
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) {
232 $params['cms'] = 'Drupal6';
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 }
238 }
239 elseif ($installType == 'drupal') {
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 }
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 }
253 else {
254 $params['cms'] = 'WordPress';
255 $params['CMSdbUser'] = addslashes(DB_USER);
256 $params['CMSdbPass'] = addslashes(DB_PASSWORD);
257 $params['CMSdbHost'] = DB_HOST;
258 $params['CMSdbName'] = addslashes(DB_NAME);
259
260 // CRM-12386
261 $params['crmRoot'] = addslashes($params['crmRoot']);
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'] = "";
274 }
275
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.
278
279 $str = file_get_contents($tplPath . 'civicrm.settings.php.template');
280 foreach ($params as $key => $value) {
281 $str = str_replace('%%' . $key . '%%', $value, $str);
282 }
283 return trim($str);
284 }
285
286 /**
287 * @return string
288 */
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
307 if ($installType == 'drupal' || $installType == 'backdrop') {
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
339 /**
340 * @return string
341 */
342 function civicrm_home_url() {
343 $drupalURL = civicrm_cms_base();
344 return $drupalURL . 'index.php?q=civicrm';
345 }