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