Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
626f739f | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
626f739f | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
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']); | |
87 | civicrm_setup($cmsPath . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . | |
88 | $siteDir . DIRECTORY_SEPARATOR . 'files' | |
89 | ); | |
90 | } | |
5757adf3 HD |
91 | elseif ($installType == 'backdrop') { |
92 | civicrm_setup($cmsPath . DIRECTORY_SEPARATOR . 'files'); | |
93 | } | |
6a488035 | 94 | elseif ($installType == 'wordpress') { |
7ba2c8ad KC |
95 | $upload_dir = wp_upload_dir(); |
96 | $files_dirname = $upload_dir['basedir']; | |
97 | civicrm_setup($files_dirname); | |
6a488035 TO |
98 | } |
99 | ||
3916ca43 SL |
100 | $parts = explode(':', $config['mysql']['server']); |
101 | if (empty($parts[1])) { | |
102 | $parts[1] = 3306; | |
103 | } | |
104 | $config['mysql']['server'] = implode(':', $parts); | |
6a488035 | 105 | |
3916ca43 | 106 | $dsn = "mysql://{$config['mysql']['username']}:{$config['mysql']['password']}@{$config['mysql']['server']}/{$config['mysql']['database']}?new_link=true"; |
6a488035 TO |
107 | civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm.mysql'); |
108 | ||
109 | if (!empty($config['loadGenerated'])) { | |
110 | civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_generated.mysql', TRUE); | |
111 | } | |
112 | else { | |
113 | if (isset($config['seedLanguage']) | |
114 | and preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $config['seedLanguage']) | |
115 | and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql") | |
116 | and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql") | |
117 | ) { | |
118 | civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql"); | |
119 | civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql"); | |
120 | } | |
121 | else { | |
122 | civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_data.mysql'); | |
123 | civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_acl.mysql'); | |
124 | } | |
125 | } | |
126 | ||
127 | // generate backend settings file | |
128 | if ($installType == 'drupal') { | |
129 | $configFile = $cmsPath . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . $siteDir . DIRECTORY_SEPARATOR . 'civicrm.settings.php'; | |
130 | } | |
5757adf3 HD |
131 | elseif ($installType == 'backdrop') { |
132 | $configFile = $cmsPath . DIRECTORY_SEPARATOR . 'civicrm.settings.php'; | |
133 | } | |
6a488035 | 134 | elseif ($installType == 'wordpress') { |
7ba2c8ad | 135 | $configFile = $files_dirname . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR . 'civicrm.settings.php'; |
6a488035 TO |
136 | } |
137 | ||
138 | $string = civicrm_config($config); | |
139 | civicrm_write_file($configFile, | |
140 | $string | |
141 | ); | |
142 | ||
143 | } | |
144 | ||
627456b5 EM |
145 | /** |
146 | * @param $dsn | |
100fef9d | 147 | * @param string $fileName |
627456b5 EM |
148 | * @param bool $lineMode |
149 | */ | |
6a488035 TO |
150 | function civicrm_source($dsn, $fileName, $lineMode = FALSE) { |
151 | global $crmPath; | |
4046d167 | 152 | |
6a488035 TO |
153 | require_once "$crmPath/packages/DB.php"; |
154 | ||
3045e3f4 ML |
155 | // CRM-19699 See also CRM_Core_DAO for PHP7 mysqli compatiblity. |
156 | // Duplicated here because this is not using CRM_Core_DAO directly | |
157 | // and this function may be called directly from Drush. | |
158 | if (!defined('DB_DSN_MODE')) { | |
159 | define('DB_DSN_MODE', 'auto'); | |
160 | } | |
161 | ||
6a488035 TO |
162 | $db = DB::connect($dsn); |
163 | if (PEAR::isError($db)) { | |
164 | die("Cannot open $dsn: " . $db->getMessage()); | |
165 | } | |
8e58c9aa | 166 | $db->query("SET NAMES utf8"); |
6a488035 | 167 | |
79c099b8 ML |
168 | $db->query("SET NAMES utf8"); |
169 | ||
6a488035 TO |
170 | if (!$lineMode) { |
171 | $string = file_get_contents($fileName); | |
172 | ||
173 | // change \r\n to fix windows issues | |
174 | $string = str_replace("\r\n", "\n", $string); | |
175 | ||
176 | //get rid of comments starting with # and -- | |
177 | ||
178 | $string = preg_replace("/^#[^\n]*$/m", "\n", $string); | |
179 | $string = preg_replace("/^(--[^-]).*/m", "\n", $string); | |
180 | ||
181 | $queries = preg_split('/;\s*$/m', $string); | |
182 | foreach ($queries as $query) { | |
183 | $query = trim($query); | |
184 | if (!empty($query)) { | |
185 | $res = &$db->query($query); | |
186 | if (PEAR::isError($res)) { | |
187 | print_r($res); | |
188 | die("Cannot execute $query: " . $res->getMessage()); | |
189 | } | |
190 | } | |
191 | } | |
192 | } | |
193 | else { | |
194 | $fd = fopen($fileName, "r"); | |
195 | while ($string = fgets($fd)) { | |
196 | $string = preg_replace("/^#[^\n]*$/m", "\n", $string); | |
197 | $string = preg_replace("/^(--[^-]).*/m", "\n", $string); | |
198 | ||
199 | $string = trim($string); | |
200 | if (!empty($string)) { | |
201 | $res = &$db->query($string); | |
202 | if (PEAR::isError($res)) { | |
203 | die("Cannot execute $string: " . $res->getMessage()); | |
204 | } | |
205 | } | |
206 | } | |
207 | } | |
208 | } | |
209 | ||
627456b5 EM |
210 | /** |
211 | * @param $config | |
212 | * | |
213 | * @return string | |
214 | */ | |
6a488035 TO |
215 | function civicrm_config(&$config) { |
216 | global $crmPath, $comPath; | |
217 | global $compileDir; | |
218 | global $tplPath, $installType; | |
219 | ||
220 | $params = array( | |
221 | 'crmRoot' => $crmPath, | |
222 | 'templateCompileDir' => $compileDir, | |
223 | 'frontEnd' => 0, | |
224 | 'dbUser' => addslashes($config['mysql']['username']), | |
225 | 'dbPass' => addslashes($config['mysql']['password']), | |
226 | 'dbHost' => $config['mysql']['server'], | |
227 | 'dbName' => addslashes($config['mysql']['database']), | |
228 | ); | |
229 | ||
230 | $params['baseURL'] = isset($config['base_url']) ? $config['base_url'] : civicrm_cms_base(); | |
09d7c1e5 | 231 | if ($installType == 'drupal' && defined('VERSION')) { |
3916ca43 SL |
232 | if (version_compare(VERSION, '8.0') >= 0) { |
233 | $params['cms'] = 'Drupal'; | |
234 | $params['CMSdbUser'] = addslashes($config['drupal']['username']); | |
235 | $params['CMSdbPass'] = addslashes($config['drupal']['password']); | |
236 | $params['CMSdbHost'] = $config['drupal']['host'] . ":" . !empty($config['drupal']['port']) ? $config['drupal']['port'] : "3306"; | |
237 | $params['CMSdbName'] = addslashes($config['drupal']['database']); | |
238 | } | |
239 | elseif (version_compare(VERSION, '7.0-rc1') >= 0) { | |
56fdfc52 | 240 | $params['cms'] = 'Drupal'; |
5126fe42 DL |
241 | $params['CMSdbUser'] = addslashes($config['drupal']['username']); |
242 | $params['CMSdbPass'] = addslashes($config['drupal']['password']); | |
243 | $params['CMSdbHost'] = $config['drupal']['server']; | |
244 | $params['CMSdbName'] = addslashes($config['drupal']['database']); | |
245 | } | |
246 | elseif (version_compare(VERSION, '6.0') >= 0) { | |
56fdfc52 | 247 | $params['cms'] = 'Drupal6'; |
5126fe42 DL |
248 | $params['CMSdbUser'] = addslashes($config['drupal']['username']); |
249 | $params['CMSdbPass'] = addslashes($config['drupal']['password']); | |
250 | $params['CMSdbHost'] = $config['drupal']['server']; | |
251 | $params['CMSdbName'] = addslashes($config['drupal']['database']); | |
252 | } | |
6a488035 | 253 | } |
68d1a6f6 | 254 | elseif ($installType == 'drupal') { |
09d7c1e5 EM |
255 | $params['cms'] = $config['cms']; |
256 | $params['CMSdbUser'] = addslashes($config['cmsdb']['username']); | |
257 | $params['CMSdbPass'] = addslashes($config['cmsdb']['password']); | |
258 | $params['CMSdbHost'] = $config['cmsdb']['server']; | |
259 | $params['CMSdbName'] = addslashes($config['cmsdb']['database']); | |
260 | } | |
5757adf3 HD |
261 | elseif ($installType == 'backdrop') { |
262 | $params['cms'] = 'Backdrop'; | |
263 | $params['CMSdbUser'] = addslashes($config['backdrop']['username']); | |
264 | $params['CMSdbPass'] = addslashes($config['backdrop']['password']); | |
265 | $params['CMSdbHost'] = $config['backdrop']['server']; | |
266 | $params['CMSdbName'] = addslashes($config['backdrop']['database']); | |
267 | } | |
6a488035 | 268 | else { |
56fdfc52 | 269 | $params['cms'] = 'WordPress'; |
6a488035 TO |
270 | $params['CMSdbUser'] = addslashes(DB_USER); |
271 | $params['CMSdbPass'] = addslashes(DB_PASSWORD); | |
272 | $params['CMSdbHost'] = DB_HOST; | |
273 | $params['CMSdbName'] = addslashes(DB_NAME); | |
5126fe42 DL |
274 | |
275 | // CRM-12386 | |
276 | $params['crmRoot'] = addslashes($params['crmRoot']); | |
6a488035 TO |
277 | } |
278 | ||
f782f7e3 TO |
279 | $params['siteKey'] = md5(rand() . mt_rand() . rand() . uniqid('', TRUE) . $params['baseURL']); |
280 | // Would prefer openssl_random_pseudo_bytes(), but I don't think it's universally available. | |
6a488035 | 281 | |
befcb21d | 282 | $str = file_get_contents($tplPath . 'civicrm.settings.php.template'); |
6a488035 TO |
283 | foreach ($params as $key => $value) { |
284 | $str = str_replace('%%' . $key . '%%', $value, $str); | |
285 | } | |
286 | return trim($str); | |
287 | } | |
288 | ||
627456b5 EM |
289 | /** |
290 | * @return string | |
291 | */ | |
6a488035 TO |
292 | function civicrm_cms_base() { |
293 | global $installType; | |
294 | ||
295 | // for drupal | |
296 | $numPrevious = 6; | |
297 | ||
298 | if (isset($_SERVER['HTTPS']) && | |
299 | !empty($_SERVER['HTTPS']) && | |
300 | strtolower($_SERVER['HTTPS']) != 'off' | |
301 | ) { | |
302 | $url = 'https://' . $_SERVER['HTTP_HOST']; | |
303 | } | |
304 | else { | |
305 | $url = 'http://' . $_SERVER['HTTP_HOST']; | |
306 | } | |
307 | ||
308 | $baseURL = $_SERVER['SCRIPT_NAME']; | |
309 | ||
5757adf3 | 310 | if ($installType == 'drupal' || $installType == 'backdrop') { |
6a488035 TO |
311 | //don't assume 6 dir levels, as civicrm |
312 | //may or may not be in sites/all/modules/ | |
313 | //lets allow to install in custom dir. CRM-6840 | |
314 | global $cmsPath; | |
315 | $crmDirLevels = str_replace($cmsPath, '', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME'])); | |
316 | $baseURL = str_replace($crmDirLevels, '', str_replace('\\', '/', $baseURL)); | |
317 | } | |
318 | elseif ($installType == 'wordpress') { | |
319 | $baseURL = str_replace($url, '', site_url()); | |
320 | } | |
321 | else { | |
322 | for ($i = 1; $i <= $numPrevious; $i++) { | |
323 | $baseURL = dirname($baseURL); | |
324 | } | |
325 | } | |
326 | ||
327 | // remove the last directory separator string from the directory | |
328 | if (substr($baseURL, -1, 1) == DIRECTORY_SEPARATOR) { | |
329 | $baseURL = substr($baseURL, 0, -1); | |
330 | } | |
331 | ||
332 | // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze | |
333 | $baseURL = str_replace(DIRECTORY_SEPARATOR, '/', $baseURL); | |
334 | ||
335 | if ($baseURL != '/') { | |
336 | $baseURL .= '/'; | |
337 | } | |
338 | ||
339 | return $url . $baseURL; | |
340 | } | |
341 | ||
627456b5 EM |
342 | /** |
343 | * @return string | |
344 | */ | |
6a488035 TO |
345 | function civicrm_home_url() { |
346 | $drupalURL = civicrm_cms_base(); | |
347 | return $drupalURL . 'index.php?q=civicrm'; | |
348 | } |