Merge pull request #4887 from pratikshad/broken-webtest
[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 *
34 */
35 function civicrm_setup($filesDirectory) {
36 global $crmPath, $sqlPath, $pkgPath, $tplPath;
37 global $compileDir;
38
39 $pkgPath = $crmPath . DIRECTORY_SEPARATOR . 'packages';
40 set_include_path($crmPath . PATH_SEPARATOR .
41 $pkgPath . PATH_SEPARATOR .
42 get_include_path()
43 );
44
45 $sqlPath = $crmPath . DIRECTORY_SEPARATOR . 'sql';
46 $tplPath = $crmPath . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'CRM' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR;
47
48 if (!is_dir($filesDirectory)) {
49 mkdir($filesDirectory, 0777);
50 chmod($filesDirectory, 0777);
51 }
52
53 $scratchDir = $filesDirectory . DIRECTORY_SEPARATOR . 'civicrm';
54 if (!is_dir($scratchDir)) {
55 mkdir($scratchDir, 0777);
56 }
57
58 $compileDir = $scratchDir . DIRECTORY_SEPARATOR . 'templates_c' . DIRECTORY_SEPARATOR;
59 if (!is_dir($compileDir)) {
60 mkdir($compileDir, 0777);
61 }
62 $compileDir = addslashes($compileDir);
63 }
64
65 /**
66 * @param string $name
67 * @param $buffer
68 */
69 function civicrm_write_file($name, &$buffer) {
70 $fd = fopen($name, "w");
71 if (!$fd) {
72 die("Cannot open $name");
73 }
74 fputs($fd, $buffer);
75 fclose($fd);
76 }
77
78 /**
79 * @param $config
80 */
81 function civicrm_main(&$config) {
82 global $sqlPath, $crmPath, $cmsPath, $installType;
83
84 if ($installType == 'drupal') {
85 $siteDir = isset($config['site_dir']) ? $config['site_dir'] : getSiteDir($cmsPath, $_SERVER['SCRIPT_FILENAME']);
86 civicrm_setup($cmsPath . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR .
87 $siteDir . DIRECTORY_SEPARATOR . 'files'
88 );
89 }
90 elseif ($installType == 'wordpress') {
91 civicrm_setup(WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'files');
92 }
93
94 $dsn = "mysql://{$config['mysql']['username']}:{$config['mysql']['password']}@{$config['mysql']['server']}/{$config['mysql']['database']}?new_link=true";
95
96 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm.mysql');
97
98 if (!empty($config['loadGenerated'])) {
99 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_generated.mysql', TRUE);
100 }
101 else {
102 if (isset($config['seedLanguage'])
103 and preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $config['seedLanguage'])
104 and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql")
105 and file_exists($sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql")
106 ) {
107 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$config['seedLanguage']}.mysql");
108 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . "civicrm_acl.{$config['seedLanguage']}.mysql");
109 }
110 else {
111 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_data.mysql');
112 civicrm_source($dsn, $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_acl.mysql');
113 }
114 }
115
116 // generate backend settings file
117 if ($installType == 'drupal') {
118 $configFile = $cmsPath . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . $siteDir . DIRECTORY_SEPARATOR . 'civicrm.settings.php';
119 }
120 elseif ($installType == 'wordpress') {
121 $configFile = $cmsPath . DIRECTORY_SEPARATOR . 'civicrm.settings.php';
122 }
123
124 $string = civicrm_config($config);
125 civicrm_write_file($configFile,
126 $string
127 );
128
129 }
130
131 /**
132 * @param $dsn
133 * @param string $fileName
134 * @param bool $lineMode
135 */
136 function civicrm_source($dsn, $fileName, $lineMode = FALSE) {
137 global $crmPath;
138
139 require_once "$crmPath/packages/DB.php";
140
141 $db = DB::connect($dsn);
142 if (PEAR::isError($db)) {
143 die("Cannot open $dsn: " . $db->getMessage());
144 }
145 $db->query("SET NAMES utf8");
146
147 $db->query("SET NAMES utf8");
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 $params = array(
200 'crmRoot' => $crmPath,
201 'templateCompileDir' => $compileDir,
202 'frontEnd' => 0,
203 'dbUser' => addslashes($config['mysql']['username']),
204 'dbPass' => addslashes($config['mysql']['password']),
205 'dbHost' => $config['mysql']['server'],
206 'dbName' => addslashes($config['mysql']['database']),
207 );
208
209 $params['baseURL'] = isset($config['base_url']) ? $config['base_url'] : civicrm_cms_base();
210 if ($installType == 'drupal') {
211 if (version_compare(VERSION, '7.0-rc1') >= 0) {
212 $params['cms'] = 'Drupal';
213 $params['CMSdbUser'] = addslashes($config['drupal']['username']);
214 $params['CMSdbPass'] = addslashes($config['drupal']['password']);
215 $params['CMSdbHost'] = $config['drupal']['server'];
216 $params['CMSdbName'] = addslashes($config['drupal']['database']);
217 }
218 elseif (version_compare(VERSION, '6.0') >= 0) {
219 $params['cms'] = 'Drupal6';
220 $params['CMSdbUser'] = addslashes($config['drupal']['username']);
221 $params['CMSdbPass'] = addslashes($config['drupal']['password']);
222 $params['CMSdbHost'] = $config['drupal']['server'];
223 $params['CMSdbName'] = addslashes($config['drupal']['database']);
224 }
225 }
226 else {
227 $params['cms'] = 'WordPress';
228 $params['CMSdbUser'] = addslashes(DB_USER);
229 $params['CMSdbPass'] = addslashes(DB_PASSWORD);
230 $params['CMSdbHost'] = DB_HOST;
231 $params['CMSdbName'] = addslashes(DB_NAME);
232
233 // CRM-12386
234 $params['crmRoot'] = addslashes($params['crmRoot']);
235 }
236
237 $params['siteKey'] = md5(uniqid('', TRUE) . $params['baseURL']);
238
239 $str = file_get_contents($tplPath . 'civicrm.settings.php.template');
240 foreach ($params as $key => $value) {
241 $str = str_replace('%%' . $key . '%%', $value, $str);
242 }
243 return trim($str);
244 }
245
246 /**
247 * @return string
248 */
249 function civicrm_cms_base() {
250 global $installType;
251
252 // for drupal
253 $numPrevious = 6;
254
255 if (isset($_SERVER['HTTPS']) &&
256 !empty($_SERVER['HTTPS']) &&
257 strtolower($_SERVER['HTTPS']) != 'off'
258 ) {
259 $url = 'https://' . $_SERVER['HTTP_HOST'];
260 }
261 else {
262 $url = 'http://' . $_SERVER['HTTP_HOST'];
263 }
264
265 $baseURL = $_SERVER['SCRIPT_NAME'];
266
267 if ($installType == 'drupal') {
268 //don't assume 6 dir levels, as civicrm
269 //may or may not be in sites/all/modules/
270 //lets allow to install in custom dir. CRM-6840
271 global $cmsPath;
272 $crmDirLevels = str_replace($cmsPath, '', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
273 $baseURL = str_replace($crmDirLevels, '', str_replace('\\', '/', $baseURL));
274 }
275 elseif ($installType == 'wordpress') {
276 $baseURL = str_replace($url, '', site_url());
277 }
278 else {
279 for ($i = 1; $i <= $numPrevious; $i++) {
280 $baseURL = dirname($baseURL);
281 }
282 }
283
284 // remove the last directory separator string from the directory
285 if (substr($baseURL, -1, 1) == DIRECTORY_SEPARATOR) {
286 $baseURL = substr($baseURL, 0, -1);
287 }
288
289 // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze
290 $baseURL = str_replace(DIRECTORY_SEPARATOR, '/', $baseURL);
291
292 if ($baseURL != '/') {
293 $baseURL .= '/';
294 }
295
296 return $url . $baseURL;
297 }
298
299 /**
300 * @return string
301 */
302 function civicrm_home_url() {
303 $drupalURL = civicrm_cms_base();
304 return $drupalURL . 'index.php?q=civicrm';
305 }