dd189db3b1df2dcdc9cae4da640bcb600bf3e369
[civicrm-core.git] / install / civicrm.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 $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 $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 fputs($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 $fileName
128 * @param bool $lineMode
129 */
130 function civicrm_source($dsn, $fileName, $lineMode = FALSE) {
131 global $crmPath;
132 require_once "$crmPath/packages/DB.php";
133
134 $db = DB::connect($dsn);
135 if (PEAR::isError($db)) {
136 die("Cannot open $dsn: " . $db->getMessage());
137 }
138 $db->query("SET NAMES utf8");
139
140 $db->query("SET NAMES utf8");
141
142 if (!$lineMode) {
143 $string = file_get_contents($fileName);
144
145 // change \r\n to fix windows issues
146 $string = str_replace("\r\n", "\n", $string);
147
148 //get rid of comments starting with # and --
149
150 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
151 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
152
153 $queries = preg_split('/;\s*$/m', $string);
154 foreach ($queries as $query) {
155 $query = trim($query);
156 if (!empty($query)) {
157 $res = &$db->query($query);
158 if (PEAR::isError($res)) {
159 print_r($res);
160 die("Cannot execute $query: " . $res->getMessage());
161 }
162 }
163 }
164 }
165 else {
166 $fd = fopen($fileName, "r");
167 while ($string = fgets($fd)) {
168 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
169 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
170
171 $string = trim($string);
172 if (!empty($string)) {
173 $res = &$db->query($string);
174 if (PEAR::isError($res)) {
175 die("Cannot execute $string: " . $res->getMessage());
176 }
177 }
178 }
179 }
180 }
181
182 /**
183 * @param $config
184 *
185 * @return string
186 */
187 function civicrm_config(&$config) {
188 global $crmPath, $comPath;
189 global $compileDir;
190 global $tplPath, $installType;
191
192 $params = array(
193 'crmRoot' => $crmPath,
194 'templateCompileDir' => $compileDir,
195 'frontEnd' => 0,
196 'dbUser' => addslashes($config['mysql']['username']),
197 'dbPass' => addslashes($config['mysql']['password']),
198 'dbHost' => $config['mysql']['server'],
199 'dbName' => addslashes($config['mysql']['database']),
200 );
201
202 $params['baseURL'] = isset($config['base_url']) ? $config['base_url'] : civicrm_cms_base();
203 if ($installType == 'drupal') {
204 if (version_compare(VERSION, '7.0-rc1') >= 0) {
205 $params['cms'] = 'Drupal';
206 $params['CMSdbUser'] = addslashes($config['drupal']['username']);
207 $params['CMSdbPass'] = addslashes($config['drupal']['password']);
208 $params['CMSdbHost'] = $config['drupal']['server'];
209 $params['CMSdbName'] = addslashes($config['drupal']['database']);
210 }
211 elseif (version_compare(VERSION, '6.0') >= 0) {
212 $params['cms'] = 'Drupal6';
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 }
219 else {
220 $params['cms'] = 'WordPress';
221 $params['CMSdbUser'] = addslashes(DB_USER);
222 $params['CMSdbPass'] = addslashes(DB_PASSWORD);
223 $params['CMSdbHost'] = DB_HOST;
224 $params['CMSdbName'] = addslashes(DB_NAME);
225
226 // CRM-12386
227 $params['crmRoot'] = addslashes($params['crmRoot']);
228 }
229
230 $params['siteKey'] = md5(uniqid('', TRUE) . $params['baseURL']);
231
232 $str = file_get_contents($tplPath . 'civicrm.settings.php.template');
233 foreach ($params as $key => $value) {
234 $str = str_replace('%%' . $key . '%%', $value, $str);
235 }
236 return trim($str);
237 }
238
239 /**
240 * @return string
241 */
242 function civicrm_cms_base() {
243 global $installType;
244
245 // for drupal
246 $numPrevious = 6;
247
248 if (isset($_SERVER['HTTPS']) &&
249 !empty($_SERVER['HTTPS']) &&
250 strtolower($_SERVER['HTTPS']) != 'off'
251 ) {
252 $url = 'https://' . $_SERVER['HTTP_HOST'];
253 }
254 else {
255 $url = 'http://' . $_SERVER['HTTP_HOST'];
256 }
257
258 $baseURL = $_SERVER['SCRIPT_NAME'];
259
260 if ($installType == 'drupal') {
261 //don't assume 6 dir levels, as civicrm
262 //may or may not be in sites/all/modules/
263 //lets allow to install in custom dir. CRM-6840
264 global $cmsPath;
265 $crmDirLevels = str_replace($cmsPath, '', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
266 $baseURL = str_replace($crmDirLevels, '', str_replace('\\', '/', $baseURL));
267 }
268 elseif ($installType == 'wordpress') {
269 $baseURL = str_replace($url, '', site_url());
270 }
271 else {
272 for ($i = 1; $i <= $numPrevious; $i++) {
273 $baseURL = dirname($baseURL);
274 }
275 }
276
277 // remove the last directory separator string from the directory
278 if (substr($baseURL, -1, 1) == DIRECTORY_SEPARATOR) {
279 $baseURL = substr($baseURL, 0, -1);
280 }
281
282 // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze
283 $baseURL = str_replace(DIRECTORY_SEPARATOR, '/', $baseURL);
284
285 if ($baseURL != '/') {
286 $baseURL .= '/';
287 }
288
289 return $url . $baseURL;
290 }
291
292 /**
293 * @return string
294 */
295 function civicrm_home_url() {
296 $drupalURL = civicrm_cms_base();
297 return $drupalURL . 'index.php?q=civicrm';
298 }
299