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