Merge pull request #6288 from eileenmcnaughton/CRM-16523
[civicrm-core.git] / install / civicrm.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 * $Id$
33 * @param $filesDirectory
34 */
35 function civicrm_setup($filesDirectory) {
36 global $crmPath, $sqlPath, $pkgPath, $tplPath;
37 global $compileDir;
38
39 // Setup classloader
40 // This is needed to allow CiviCRM to be installed by drush.
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();
45
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
66 /**
67 * @param string $name
68 * @param $buffer
69 */
70 function civicrm_write_file($name, &$buffer) {
71 $fd = fopen($name, "w");
72 if (!$fd) {
73 die("Cannot open $name");
74 }
75 fwrite($fd, $buffer);
76 fclose($fd);
77 }
78
79 /**
80 * @param $config
81 */
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 }
91 elseif ($installType == 'wordpress') {
92 civicrm_setup(WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'files');
93 }
94
95 $dsn = "mysql://{$config['mysql']['username']}:{$config['mysql']['password']}@{$config['mysql']['server']}/{$config['mysql']['database']}?new_link=true";
96
97 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm.mysql');
98
99 if (!empty($config['loadGenerated'])) {
100 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_generated.mysql', TRUE);
101 }
102 else {
103 if (isset($config['seedLanguage'])
104 and preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $config['seedLanguage'])
105 and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql")
106 and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql")
107 ) {
108 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql");
109 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql");
110 }
111 else {
112 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_data.mysql');
113 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_acl.mysql');
114 }
115 }
116
117 // generate backend settings file
118 if ($installType == 'drupal') {
119 $configFile = $cmsPath . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . $siteDir . DIRECTORY_SEPARATOR . 'civicrm.settings.php';
120 }
121 elseif ($installType == 'wordpress') {
122 $configFile = $cmsPath . DIRECTORY_SEPARATOR . 'civicrm.settings.php';
123 }
124
125 $string = civicrm_config($config);
126 civicrm_write_file($configFile,
127 $string
128 );
129
130 }
131
132 /**
133 * @param $dsn
134 * @param string $fileName
135 * @param bool $lineMode
136 */
137 function civicrm_source($dsn, $fileName, $lineMode = FALSE) {
138 global $crmPath;
139
140 require_once "$crmPath/packages/DB.php";
141
142 $db = DB::connect($dsn);
143 if (PEAR::isError($db)) {
144 die("Cannot open $dsn: " . $db->getMessage());
145 }
146 $db->query("SET NAMES utf8");
147
148 $db->query("SET NAMES utf8");
149
150 if (!$lineMode) {
151 $string = file_get_contents($fileName);
152
153 // change \r\n to fix windows issues
154 $string = str_replace("\r\n", "\n", $string);
155
156 //get rid of comments starting with # and --
157
158 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
159 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
160
161 $queries = preg_split('/;\s*$/m', $string);
162 foreach ($queries as $query) {
163 $query = trim($query);
164 if (!empty($query)) {
165 $res = &$db->query($query);
166 if (PEAR::isError($res)) {
167 print_r($res);
168 die("Cannot execute $query: " . $res->getMessage());
169 }
170 }
171 }
172 }
173 else {
174 $fd = fopen($fileName, "r");
175 while ($string = fgets($fd)) {
176 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
177 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
178
179 $string = trim($string);
180 if (!empty($string)) {
181 $res = &$db->query($string);
182 if (PEAR::isError($res)) {
183 die("Cannot execute $string: " . $res->getMessage());
184 }
185 }
186 }
187 }
188 }
189
190 /**
191 * @param $config
192 *
193 * @return string
194 */
195 function civicrm_config(&$config) {
196 global $crmPath, $comPath;
197 global $compileDir;
198 global $tplPath, $installType;
199
200 $params = array(
201 'crmRoot' => $crmPath,
202 'templateCompileDir' => $compileDir,
203 'frontEnd' => 0,
204 'dbUser' => addslashes($config['mysql']['username']),
205 'dbPass' => addslashes($config['mysql']['password']),
206 'dbHost' => $config['mysql']['server'],
207 'dbName' => addslashes($config['mysql']['database']),
208 );
209
210 $params['baseURL'] = isset($config['base_url']) ? $config['base_url'] : civicrm_cms_base();
211 if ($installType == 'drupal' && defined('VERSION')) {
212 if (version_compare(VERSION, '7.0-rc1') >= 0) {
213 $params['cms'] = 'Drupal';
214 $params['CMSdbUser'] = addslashes($config['drupal']['username']);
215 $params['CMSdbPass'] = addslashes($config['drupal']['password']);
216 $params['CMSdbHost'] = $config['drupal']['server'];
217 $params['CMSdbName'] = addslashes($config['drupal']['database']);
218 }
219 elseif (version_compare(VERSION, '6.0') >= 0) {
220 $params['cms'] = 'Drupal6';
221 $params['CMSdbUser'] = addslashes($config['drupal']['username']);
222 $params['CMSdbPass'] = addslashes($config['drupal']['password']);
223 $params['CMSdbHost'] = $config['drupal']['server'];
224 $params['CMSdbName'] = addslashes($config['drupal']['database']);
225 }
226 }
227 elseif ($installType == 'drupal') {
228 $params['cms'] = $config['cms'];
229 $params['CMSdbUser'] = addslashes($config['cmsdb']['username']);
230 $params['CMSdbPass'] = addslashes($config['cmsdb']['password']);
231 $params['CMSdbHost'] = $config['cmsdb']['server'];
232 $params['CMSdbName'] = addslashes($config['cmsdb']['database']);
233 }
234 else {
235 $params['cms'] = 'WordPress';
236 $params['CMSdbUser'] = addslashes(DB_USER);
237 $params['CMSdbPass'] = addslashes(DB_PASSWORD);
238 $params['CMSdbHost'] = DB_HOST;
239 $params['CMSdbName'] = addslashes(DB_NAME);
240
241 // CRM-12386
242 $params['crmRoot'] = addslashes($params['crmRoot']);
243 }
244
245 $params['siteKey'] = md5(rand() . mt_rand() . rand() . uniqid('', TRUE) . $params['baseURL']);
246 // Would prefer openssl_random_pseudo_bytes(), but I don't think it's universally available.
247
248 $str = file_get_contents($tplPath . 'civicrm.settings.php.template');
249 foreach ($params as $key => $value) {
250 $str = str_replace('%%' . $key . '%%', $value, $str);
251 }
252 return trim($str);
253 }
254
255 /**
256 * @return string
257 */
258 function civicrm_cms_base() {
259 global $installType;
260
261 // for drupal
262 $numPrevious = 6;
263
264 if (isset($_SERVER['HTTPS']) &&
265 !empty($_SERVER['HTTPS']) &&
266 strtolower($_SERVER['HTTPS']) != 'off'
267 ) {
268 $url = 'https://' . $_SERVER['HTTP_HOST'];
269 }
270 else {
271 $url = 'http://' . $_SERVER['HTTP_HOST'];
272 }
273
274 $baseURL = $_SERVER['SCRIPT_NAME'];
275
276 if ($installType == 'drupal') {
277 //don't assume 6 dir levels, as civicrm
278 //may or may not be in sites/all/modules/
279 //lets allow to install in custom dir. CRM-6840
280 global $cmsPath;
281 $crmDirLevels = str_replace($cmsPath, '', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
282 $baseURL = str_replace($crmDirLevels, '', str_replace('\\', '/', $baseURL));
283 }
284 elseif ($installType == 'wordpress') {
285 $baseURL = str_replace($url, '', site_url());
286 }
287 else {
288 for ($i = 1; $i <= $numPrevious; $i++) {
289 $baseURL = dirname($baseURL);
290 }
291 }
292
293 // remove the last directory separator string from the directory
294 if (substr($baseURL, -1, 1) == DIRECTORY_SEPARATOR) {
295 $baseURL = substr($baseURL, 0, -1);
296 }
297
298 // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze
299 $baseURL = str_replace(DIRECTORY_SEPARATOR, '/', $baseURL);
300
301 if ($baseURL != '/') {
302 $baseURL .= '/';
303 }
304
305 return $url . $baseURL;
306 }
307
308 /**
309 * @return string
310 */
311 function civicrm_home_url() {
312 $drupalURL = civicrm_cms_base();
313 return $drupalURL . 'index.php?q=civicrm';
314 }