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