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