Merge pull request #265 from colemanw/del-fix
[civicrm-core.git] / install / civicrm.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 version_compare(VERSION, '7.0-rc1') >= 0
192 ) {
193 $params['cms'] = 'Drupal';
194 $params['CMSdbUser'] = addslashes($config['drupal']['username']);
195 $params['CMSdbPass'] = addslashes($config['drupal']['password']);
196 $params['CMSdbHost'] = $config['drupal']['server'];
197 $params['CMSdbName'] = addslashes($config['drupal']['database']);
198 }
199 elseif ($installType == 'drupal' &&
200 version_compare(VERSION, '6.0') >= 0
201 ) {
202 $params['cms'] = 'Drupal6';
203 $params['CMSdbUser'] = addslashes($config['drupal']['username']);
204 $params['CMSdbPass'] = addslashes($config['drupal']['password']);
205 $params['CMSdbHost'] = $config['drupal']['server'];
206 $params['CMSdbName'] = addslashes($config['drupal']['database']);
207 }
208 else {
209 $params['cms'] = 'WordPress';
210 $params['CMSdbUser'] = addslashes(DB_USER);
211 $params['CMSdbPass'] = addslashes(DB_PASSWORD);
212 $params['CMSdbHost'] = DB_HOST;
213 $params['CMSdbName'] = addslashes(DB_NAME);
214 }
215
216 $params['siteKey'] = md5(uniqid('', TRUE) . $params['baseURL']);
217
218 $str = file_get_contents($tplPath . 'civicrm.settings.php.tpl');
219 foreach ($params as $key => $value) {
220 $str = str_replace('%%' . $key . '%%', $value, $str);
221 }
222 return trim($str);
223 }
224
225 function civicrm_cms_base() {
226 global $installType;
227
228 // for drupal
229 $numPrevious = 6;
230
231 if (isset($_SERVER['HTTPS']) &&
232 !empty($_SERVER['HTTPS']) &&
233 strtolower($_SERVER['HTTPS']) != 'off'
234 ) {
235 $url = 'https://' . $_SERVER['HTTP_HOST'];
236 }
237 else {
238 $url = 'http://' . $_SERVER['HTTP_HOST'];
239 }
240
241 $baseURL = $_SERVER['SCRIPT_NAME'];
242
243 if ($installType == 'drupal') {
244 //don't assume 6 dir levels, as civicrm
245 //may or may not be in sites/all/modules/
246 //lets allow to install in custom dir. CRM-6840
247 global $cmsPath;
248 $crmDirLevels = str_replace($cmsPath, '', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
249 $baseURL = str_replace($crmDirLevels, '', str_replace('\\', '/', $baseURL));
250 }
251 elseif ($installType == 'wordpress') {
252 $baseURL = str_replace($url, '', site_url());
253 }
254 else {
255 for ($i = 1; $i <= $numPrevious; $i++) {
256 $baseURL = dirname($baseURL);
257 }
258 }
259
260 // remove the last directory separator string from the directory
261 if (substr($baseURL, -1, 1) == DIRECTORY_SEPARATOR) {
262 $baseURL = substr($baseURL, 0, -1);
263 }
264
265 // also convert all DIRECTORY_SEPARATOR to the forward slash for windoze
266 $baseURL = str_replace(DIRECTORY_SEPARATOR, '/', $baseURL);
267
268 if ($baseURL != '/') {
269 $baseURL .= '/';
270 }
271
272 return $url . $baseURL;
273 }
274
275 function civicrm_home_url() {
276 $drupalURL = civicrm_cms_base();
277 return $drupalURL . 'index.php?q=civicrm';
278 }
279