Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-03-09-21-44-34
[civicrm-core.git] / install / civicrm.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 * @param $filesDirectory
34 */
35 function civicrm_setup($filesDirectory) {
36 global $crmPath, $sqlPath, $pkgPath, $tplPath;
37 global $compileDir;
38
39 $sqlPath = $crmPath . DIRECTORY_SEPARATOR . 'sql';
40 $tplPath = $crmPath . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'CRM' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR;
41
42 if (!is_dir($filesDirectory)) {
43 mkdir($filesDirectory, 0777);
44 chmod($filesDirectory, 0777);
45 }
46
47 $scratchDir = $filesDirectory . DIRECTORY_SEPARATOR . 'civicrm';
48 if (!is_dir($scratchDir)) {
49 mkdir($scratchDir, 0777);
50 }
51
52 $compileDir = $scratchDir . DIRECTORY_SEPARATOR . 'templates_c' . DIRECTORY_SEPARATOR;
53 if (!is_dir($compileDir)) {
54 mkdir($compileDir, 0777);
55 }
56 $compileDir = addslashes($compileDir);
57 }
58
59 /**
60 * @param string $name
61 * @param $buffer
62 */
63 function civicrm_write_file($name, &$buffer) {
64 $fd = fopen($name, "w");
65 if (!$fd) {
66 die("Cannot open $name");
67 }
68 fwrite($fd, $buffer);
69 fclose($fd);
70 }
71
72 /**
73 * @param $config
74 */
75 function civicrm_main(&$config) {
76 global $sqlPath, $crmPath, $cmsPath, $installType;
77
78 if ($installType == 'drupal') {
79 $siteDir = isset($config['site_dir']) ? $config['site_dir'] : getSiteDir($cmsPath, $_SERVER['SCRIPT_FILENAME']);
80 civicrm_setup($cmsPath . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR .
81 $siteDir . DIRECTORY_SEPARATOR . 'files'
82 );
83 }
84 elseif ($installType == 'wordpress') {
85 civicrm_setup(WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'files');
86 }
87
88 $dsn = "mysql://{$config['mysql']['username']}:{$config['mysql']['password']}@{$config['mysql']['server']}/{$config['mysql']['database']}?new_link=true";
89
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 == 'wordpress') {
115 $configFile = $cmsPath . DIRECTORY_SEPARATOR . 'civicrm.settings.php';
116 }
117
118 $string = civicrm_config($config);
119 civicrm_write_file($configFile,
120 $string
121 );
122
123 }
124
125 /**
126 * @param $dsn
127 * @param string $fileName
128 * @param bool $lineMode
129 */
130 function civicrm_source($dsn, $fileName, $lineMode = FALSE) {
131 global $crmPath;
132
133 require_once "$crmPath/packages/DB.php";
134
135 $db = DB::connect($dsn);
136 if (PEAR::isError($db)) {
137 die("Cannot open $dsn: " . $db->getMessage());
138 }
139 $db->query("SET NAMES utf8");
140
141 $db->query("SET NAMES utf8");
142
143 if (!$lineMode) {
144 $string = file_get_contents($fileName);
145
146 // change \r\n to fix windows issues
147 $string = str_replace("\r\n", "\n", $string);
148
149 //get rid of comments starting with # and --
150
151 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
152 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
153
154 $queries = preg_split('/;\s*$/m', $string);
155 foreach ($queries as $query) {
156 $query = trim($query);
157 if (!empty($query)) {
158 $res = &$db->query($query);
159 if (PEAR::isError($res)) {
160 print_r($res);
161 die("Cannot execute $query: " . $res->getMessage());
162 }
163 }
164 }
165 }
166 else {
167 $fd = fopen($fileName, "r");
168 while ($string = fgets($fd)) {
169 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
170 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
171
172 $string = trim($string);
173 if (!empty($string)) {
174 $res = &$db->query($string);
175 if (PEAR::isError($res)) {
176 die("Cannot execute $string: " . $res->getMessage());
177 }
178 }
179 }
180 }
181 }
182
183 /**
184 * @param $config
185 *
186 * @return string
187 */
188 function civicrm_config(&$config) {
189 global $crmPath, $comPath;
190 global $compileDir;
191 global $tplPath, $installType;
192
193 $params = array(
194 'crmRoot' => $crmPath,
195 'templateCompileDir' => $compileDir,
196 'frontEnd' => 0,
197 'dbUser' => addslashes($config['mysql']['username']),
198 'dbPass' => addslashes($config['mysql']['password']),
199 'dbHost' => $config['mysql']['server'],
200 'dbName' => addslashes($config['mysql']['database']),
201 );
202
203 $params['baseURL'] = isset($config['base_url']) ? $config['base_url'] : civicrm_cms_base();
204 if ($installType == 'drupal') {
205 if (version_compare(VERSION, '7.0-rc1') >= 0) {
206 $params['cms'] = 'Drupal';
207 $params['CMSdbUser'] = addslashes($config['drupal']['username']);
208 $params['CMSdbPass'] = addslashes($config['drupal']['password']);
209 $params['CMSdbHost'] = $config['drupal']['server'];
210 $params['CMSdbName'] = addslashes($config['drupal']['database']);
211 }
212 elseif (version_compare(VERSION, '6.0') >= 0) {
213 $params['cms'] = 'Drupal6';
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 }
220 else {
221 $params['cms'] = 'WordPress';
222 $params['CMSdbUser'] = addslashes(DB_USER);
223 $params['CMSdbPass'] = addslashes(DB_PASSWORD);
224 $params['CMSdbHost'] = DB_HOST;
225 $params['CMSdbName'] = addslashes(DB_NAME);
226
227 // CRM-12386
228 $params['crmRoot'] = addslashes($params['crmRoot']);
229 }
230
231 $params['siteKey'] = md5(uniqid('', TRUE) . $params['baseURL']);
232
233 $str = file_get_contents($tplPath . 'civicrm.settings.php.template');
234 foreach ($params as $key => $value) {
235 $str = str_replace('%%' . $key . '%%', $value, $str);
236 }
237 return trim($str);
238 }
239
240 /**
241 * @return string
242 */
243 function civicrm_cms_base() {
244 global $installType;
245
246 // for drupal
247 $numPrevious = 6;
248
249 if (isset($_SERVER['HTTPS']) &&
250 !empty($_SERVER['HTTPS']) &&
251 strtolower($_SERVER['HTTPS']) != 'off'
252 ) {
253 $url = 'https://' . $_SERVER['HTTP_HOST'];
254 }
255 else {
256 $url = 'http://' . $_SERVER['HTTP_HOST'];
257 }
258
259 $baseURL = $_SERVER['SCRIPT_NAME'];
260
261 if ($installType == 'drupal') {
262 //don't assume 6 dir levels, as civicrm
263 //may or may not be in sites/all/modules/
264 //lets allow to install in custom dir. CRM-6840
265 global $cmsPath;
266 $crmDirLevels = str_replace($cmsPath, '', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
267 $baseURL = str_replace($crmDirLevels, '', str_replace('\\', '/', $baseURL));
268 }
269 elseif ($installType == 'wordpress') {
270 $baseURL = str_replace($url, '', site_url());
271 }
272 else {
273 for ($i = 1; $i <= $numPrevious; $i++) {
274 $baseURL = dirname($baseURL);
275 }
276 }
277
278 // remove the last directory separator string from the directory
279 if (substr($baseURL, -1, 1) == DIRECTORY_SEPARATOR) {
280 $baseURL = substr($baseURL, 0, -1);
281 }
282
283 // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze
284 $baseURL = str_replace(DIRECTORY_SEPARATOR, '/', $baseURL);
285
286 if ($baseURL != '/') {
287 $baseURL .= '/';
288 }
289
290 return $url . $baseURL;
291 }
292
293 /**
294 * @return string
295 */
296 function civicrm_home_url() {
297 $drupalURL = civicrm_cms_base();
298 return $drupalURL . 'index.php?q=civicrm';
299 }