Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * | |
31 | * @package CRM | |
e7112fa7 | 32 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | /** | |
38 | * file contains functions used in civicrm configuration | |
39 | * | |
40 | */ | |
41 | class CRM_Core_BAO_ConfigSetting { | |
42 | ||
43 | /** | |
100fef9d | 44 | * Create civicrm settings. This is the same as add but it clears the cache and |
b597d0b1 | 45 | * reloads the config object |
6a488035 | 46 | * |
6a0b768e TO |
47 | * @param array $params |
48 | * Associated array of civicrm variables. | |
77b97be7 | 49 | * |
608e6658 | 50 | * @return void |
6a488035 | 51 | */ |
00be9182 | 52 | public static function create($params) { |
6a488035 TO |
53 | self::add($params); |
54 | $cache = CRM_Utils_Cache::singleton(); | |
55 | $cache->delete('CRM_Core_Config'); | |
0e04f44e | 56 | $cache->delete('CRM_Core_Config' . CRM_Core_Config::domainID()); |
6a488035 TO |
57 | $config = CRM_Core_Config::singleton(TRUE, TRUE); |
58 | } | |
59 | ||
60 | /** | |
fe482240 | 61 | * Add civicrm settings. |
6a488035 | 62 | * |
6a0b768e TO |
63 | * @param array $params |
64 | * Associated array of civicrm variables. | |
dd244018 | 65 | * |
608e6658 | 66 | * @return void |
6a488035 | 67 | */ |
00be9182 | 68 | public static function add(&$params) { |
6a488035 TO |
69 | self::fixParams($params); |
70 | ||
6a488035 TO |
71 | $domain = new CRM_Core_DAO_Domain(); |
72 | $domain->id = CRM_Core_Config::domainID(); | |
73 | $domain->find(TRUE); | |
74 | if ($domain->config_backend) { | |
75 | $values = unserialize($domain->config_backend); | |
76 | self::formatParams($params, $values); | |
77 | } | |
78 | ||
79 | // CRM-6151 | |
80 | if (isset($params['localeCustomStrings']) && | |
81 | is_array($params['localeCustomStrings']) | |
82 | ) { | |
83 | $domain->locale_custom_strings = serialize($params['localeCustomStrings']); | |
84 | } | |
85 | ||
86 | // unset any of the variables we read from file that should not be stored in the database | |
87 | // the username and certpath are stored flat with _test and _live | |
88 | // check CRM-1470 | |
89 | $skipVars = self::skipVars(); | |
90 | foreach ($skipVars as $var) { | |
91 | unset($params[$var]); | |
92 | } | |
93 | ||
94 | CRM_Core_BAO_Setting::fixAndStoreDirAndURL($params); | |
95 | ||
96 | // also skip all Dir Params, we dont need to store those in the DB! | |
97 | foreach ($params as $name => $val) { | |
98 | if (substr($name, -3) == 'Dir') { | |
99 | unset($params[$name]); | |
100 | } | |
101 | } | |
102 | ||
b44e3f84 | 103 | //keep user preferred language up to date, CRM-7746 |
6a488035 TO |
104 | $session = CRM_Core_Session::singleton(); |
105 | $lcMessages = CRM_Utils_Array::value('lcMessages', $params); | |
106 | if ($lcMessages && $session->get('userID')) { | |
107 | $languageLimit = CRM_Utils_Array::value('languageLimit', $params); | |
108 | if (is_array($languageLimit) && | |
109 | !in_array($lcMessages, array_keys($languageLimit)) | |
110 | ) { | |
111 | $lcMessages = $session->get('lcMessages'); | |
112 | } | |
113 | ||
114 | $ufm = new CRM_Core_DAO_UFMatch(); | |
115 | $ufm->contact_id = $session->get('userID'); | |
116 | if ($lcMessages && $ufm->find(TRUE)) { | |
117 | $ufm->language = $lcMessages; | |
118 | $ufm->save(); | |
119 | $session->set('lcMessages', $lcMessages); | |
120 | $params['lcMessages'] = $lcMessages; | |
121 | } | |
122 | } | |
123 | ||
124 | $domain->config_backend = serialize($params); | |
125 | $domain->save(); | |
126 | } | |
127 | ||
128 | /** | |
fe482240 | 129 | * Fix civicrm setting variables. |
6a488035 | 130 | * |
6a0b768e TO |
131 | * @param array $params |
132 | * Associated array of civicrm variables. | |
dd244018 | 133 | * |
608e6658 | 134 | * @return void |
6a488035 | 135 | */ |
00be9182 | 136 | public static function fixParams(&$params) { |
6a488035 TO |
137 | // in our old civicrm.settings.php we were using ISO code for country and |
138 | // province limit, now we have changed it to use ids | |
139 | ||
140 | $countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode(); | |
141 | ||
142 | $specialArray = array('countryLimit', 'provinceLimit'); | |
143 | ||
144 | foreach ($params as $key => $value) { | |
145 | if (in_array($key, $specialArray) && is_array($value)) { | |
146 | foreach ($value as $k => $val) { | |
147 | if (!is_numeric($val)) { | |
148 | $params[$key][$k] = array_search($val, $countryIsoCodes); | |
149 | } | |
150 | } | |
151 | } | |
152 | elseif ($key == 'defaultContactCountry') { | |
153 | if (!is_numeric($value)) { | |
154 | $params[$key] = array_search($value, $countryIsoCodes); | |
155 | } | |
156 | } | |
157 | } | |
158 | } | |
159 | ||
160 | /** | |
fe482240 | 161 | * Format the array containing before inserting in db. |
6a488035 | 162 | * |
6a0b768e TO |
163 | * @param array $params |
164 | * Associated array of civicrm variables(submitted). | |
165 | * @param array $values | |
166 | * Associated array of civicrm variables stored in db. | |
6a488035 | 167 | * |
608e6658 | 168 | * @return void |
6a488035 | 169 | */ |
00be9182 | 170 | public static function formatParams(&$params, &$values) { |
6a488035 TO |
171 | if (empty($params) || |
172 | !is_array($params) | |
173 | ) { | |
174 | $params = $values; | |
175 | } | |
176 | else { | |
177 | foreach ($params as $key => $val) { | |
178 | if (array_key_exists($key, $values)) { | |
179 | unset($values[$key]); | |
180 | } | |
181 | } | |
182 | $params = array_merge($params, $values); | |
183 | } | |
184 | } | |
185 | ||
186 | /** | |
fe482240 | 187 | * Retrieve the settings values from db. |
6a488035 | 188 | * |
da6b46f4 EM |
189 | * @param $defaults |
190 | * | |
a6c01b45 | 191 | * @return array |
6a488035 | 192 | */ |
00be9182 | 193 | public static function retrieve(&$defaults) { |
6a488035 TO |
194 | $domain = new CRM_Core_DAO_Domain(); |
195 | ||
196 | //we are initializing config, really can't use, CRM-7863 | |
197 | $urlVar = 'q'; | |
198 | if (defined('CIVICRM_UF') && CIVICRM_UF == 'Joomla') { | |
199 | $urlVar = 'task'; | |
200 | } | |
201 | ||
202 | if (CRM_Core_Config::isUpgradeMode()) { | |
203 | $domain->selectAdd('config_backend'); | |
204 | } | |
205 | elseif (CRM_Utils_Array::value($urlVar, $_GET) == 'admin/modules/list/confirm') { | |
206 | $domain->selectAdd('config_backend', 'locales'); | |
207 | } | |
208 | else { | |
209 | $domain->selectAdd('config_backend, locales, locale_custom_strings'); | |
210 | } | |
211 | ||
212 | $domain->id = CRM_Core_Config::domainID(); | |
213 | $domain->find(TRUE); | |
214 | if ($domain->config_backend) { | |
215 | $defaults = unserialize($domain->config_backend); | |
216 | if ($defaults === FALSE || !is_array($defaults)) { | |
217 | $defaults = array(); | |
608e6658 | 218 | return FALSE; |
6a488035 TO |
219 | } |
220 | ||
221 | $skipVars = self::skipVars(); | |
222 | foreach ($skipVars as $skip) { | |
223 | if (array_key_exists($skip, $defaults)) { | |
224 | unset($defaults[$skip]); | |
225 | } | |
226 | } | |
227 | ||
6a488035 TO |
228 | // check if there are any locale strings |
229 | if ($domain->locale_custom_strings) { | |
230 | $defaults['localeCustomStrings'] = unserialize($domain->locale_custom_strings); | |
231 | } | |
232 | else { | |
233 | $defaults['localeCustomStrings'] = NULL; | |
234 | } | |
235 | ||
236 | // are we in a multi-language setup? | |
237 | $multiLang = $domain->locales ? TRUE : FALSE; | |
238 | ||
239 | // set the current language | |
240 | $lcMessages = NULL; | |
241 | ||
242 | $session = CRM_Core_Session::singleton(); | |
243 | ||
244 | // on multi-lang sites based on request and civicrm_uf_match | |
245 | if ($multiLang) { | |
246 | $lcMessagesRequest = CRM_Utils_Request::retrieve('lcMessages', 'String', $this); | |
247 | $languageLimit = array(); | |
248 | if (array_key_exists('languageLimit', $defaults) && is_array($defaults['languageLimit'])) { | |
249 | $languageLimit = $defaults['languageLimit']; | |
250 | } | |
251 | ||
252 | if (in_array($lcMessagesRequest, array_keys($languageLimit))) { | |
253 | $lcMessages = $lcMessagesRequest; | |
254 | ||
255 | //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache. | |
256 | CRM_Core_BAO_Cache::deleteGroup('navigation'); | |
257 | } | |
258 | else { | |
259 | $lcMessagesRequest = NULL; | |
260 | } | |
261 | ||
262 | if (!$lcMessagesRequest) { | |
263 | $lcMessagesSession = $session->get('lcMessages'); | |
264 | if (in_array($lcMessagesSession, array_keys($languageLimit))) { | |
265 | $lcMessages = $lcMessagesSession; | |
266 | } | |
267 | else { | |
268 | $lcMessagesSession = NULL; | |
269 | } | |
270 | } | |
271 | ||
272 | if ($lcMessagesRequest) { | |
273 | $ufm = new CRM_Core_DAO_UFMatch(); | |
274 | $ufm->contact_id = $session->get('userID'); | |
275 | if ($ufm->find(TRUE)) { | |
276 | $ufm->language = $lcMessages; | |
277 | $ufm->save(); | |
278 | } | |
279 | $session->set('lcMessages', $lcMessages); | |
280 | } | |
281 | ||
282 | if (!$lcMessages and $session->get('userID')) { | |
283 | $ufm = new CRM_Core_DAO_UFMatch(); | |
284 | $ufm->contact_id = $session->get('userID'); | |
285 | if ($ufm->find(TRUE) && | |
286 | in_array($ufm->language, array_keys($languageLimit)) | |
287 | ) { | |
288 | $lcMessages = $ufm->language; | |
289 | } | |
290 | $session->set('lcMessages', $lcMessages); | |
291 | } | |
292 | } | |
293 | global $dbLocale; | |
294 | ||
295 | // try to inherit the language from the hosting CMS | |
a7488080 | 296 | if (!empty($defaults['inheritLocale'])) { |
6a488035 TO |
297 | // FIXME: On multilanguage installs, CRM_Utils_System::getUFLocale() in many cases returns nothing if $dbLocale is not set |
298 | $dbLocale = $multiLang ? "_{$defaults['lcMessages']}" : ''; | |
299 | $lcMessages = CRM_Utils_System::getUFLocale(); | |
300 | if ($domain->locales and !in_array($lcMessages, explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
4c235182 EM |
301 | $domain->locales |
302 | )) | |
303 | ) { | |
6a488035 TO |
304 | $lcMessages = NULL; |
305 | } | |
306 | } | |
307 | ||
5b6ed484 | 308 | if (empty($lcMessages)) { |
6b3f6bd5 | 309 | //CRM-11993 - if a single-lang site, use default |
5b6ed484 | 310 | $lcMessages = CRM_Utils_Array::value('lcMessages', $defaults); |
311 | } | |
6a488035 TO |
312 | |
313 | // set suffix for table names - use views if more than one language | |
314 | $dbLocale = $multiLang ? "_{$lcMessages}" : ''; | |
315 | ||
316 | // FIXME: an ugly hack to fix CRM-4041 | |
317 | global $tsLocale; | |
318 | $tsLocale = $lcMessages; | |
319 | ||
320 | // FIXME: as bad aplace as any to fix CRM-5428 | |
321 | // (to be moved to a sane location along with the above) | |
322 | if (function_exists('mb_internal_encoding')) { | |
323 | mb_internal_encoding('UTF-8'); | |
324 | } | |
325 | } | |
326 | ||
327 | // dont add if its empty | |
328 | if (!empty($defaults)) { | |
329 | // retrieve directory and url preferences also | |
330 | CRM_Core_BAO_Setting::retrieveDirectoryAndURLPreferences($defaults); | |
3124edb3 | 331 | |
2efcf0c2 | 332 | // Pickup enabled-components from settings table if found. |
4acb1c97 DS |
333 | $enableComponents = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components', NULL, array()); |
334 | if (!empty($enableComponents)) { | |
335 | $defaults['enableComponents'] = $enableComponents; | |
2efcf0c2 | 336 | |
2ce81580 TO |
337 | // Lookup component IDs. Note: Do *not* instantiate components. |
338 | // Classloading may not be fully setup yet. | |
339 | $components = CRM_Core_Component::getComponentIDs(); | |
3124edb3 DS |
340 | $enabledComponentIDs = array(); |
341 | foreach ($defaults['enableComponents'] as $name) { | |
2ce81580 | 342 | $enabledComponentIDs[] = $components[$name]; |
3124edb3 DS |
343 | } |
344 | $defaults['enableComponentIDs'] = $enabledComponentIDs; | |
345 | } | |
6a488035 TO |
346 | } |
347 | } | |
348 | ||
b5c2afd0 EM |
349 | /** |
350 | * @return array | |
351 | */ | |
00be9182 | 352 | public static function getConfigSettings() { |
6a488035 TO |
353 | $config = CRM_Core_Config::singleton(); |
354 | ||
355 | $url = $dir = $siteName = $siteRoot = NULL; | |
356 | if ($config->userFramework == 'Joomla') { | |
e51744f5 DL |
357 | $url = preg_replace( |
358 | '|administrator/components/com_civicrm/civicrm/|', | |
6a488035 TO |
359 | '', |
360 | $config->userFrameworkResourceURL | |
361 | ); | |
362 | ||
363 | // lets use imageUploadDir since we dont mess around with its values | |
364 | // in the config object, lets kep it a bit generic since folks | |
365 | // might have different values etc | |
4c235182 EM |
366 | |
367 | //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths | |
368 | if (DIRECTORY_SEPARATOR == '\\') { | |
369 | $dir = preg_replace( | |
370 | '|civicrm[/\\\\]templates_c[/\\\\].*$|', | |
371 | '', | |
372 | $config->templateCompileDir | |
37b395e8 | 373 | ); |
4c235182 EM |
374 | } |
375 | else { | |
376 | $dir = preg_replace( | |
377 | '|civicrm/templates_c/.*$|', | |
378 | '', | |
379 | $config->templateCompileDir | |
37b395e8 | 380 | ); |
4c235182 EM |
381 | } |
382 | ||
e51744f5 DL |
383 | $siteRoot = preg_replace( |
384 | '|/media/civicrm/.*$|', | |
385 | '', | |
386 | $config->imageUploadDir | |
387 | ); | |
388 | } | |
4c9b6178 | 389 | elseif ($config->userFramework == 'WordPress') { |
e51744f5 DL |
390 | $url = preg_replace( |
391 | '|wp-content/plugins/civicrm/civicrm/|', | |
392 | '', | |
393 | $config->userFrameworkResourceURL | |
394 | ); | |
395 | ||
396 | // lets use imageUploadDir since we dont mess around with its values | |
397 | // in the config object, lets kep it a bit generic since folks | |
398 | // might have different values etc | |
4c235182 EM |
399 | |
400 | //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths | |
401 | if (DIRECTORY_SEPARATOR == '\\') { | |
402 | $dir = preg_replace( | |
403 | '|civicrm[/\\\\]templates_c[/\\\\].*$|', | |
404 | '', | |
405 | $config->templateCompileDir | |
37b395e8 | 406 | ); |
4c235182 EM |
407 | } |
408 | else { | |
409 | $dir = preg_replace( | |
410 | '|civicrm/templates_c/.*$|', | |
411 | '', | |
412 | $config->templateCompileDir | |
37b395e8 | 413 | ); |
4c235182 EM |
414 | } |
415 | ||
e51744f5 DL |
416 | $siteRoot = preg_replace( |
417 | '|/wp-content/plugins/files/civicrm/.*$|', | |
6a488035 TO |
418 | '', |
419 | $config->imageUploadDir | |
420 | ); | |
421 | } | |
422 | else { | |
e51744f5 DL |
423 | $url = preg_replace( |
424 | '|sites/[\w\.\-\_]+/modules/civicrm/|', | |
6a488035 TO |
425 | '', |
426 | $config->userFrameworkResourceURL | |
427 | ); | |
428 | ||
429 | // lets use imageUploadDir since we dont mess around with its values | |
430 | // in the config object, lets kep it a bit generic since folks | |
431 | // might have different values etc | |
4c235182 EM |
432 | |
433 | //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths | |
434 | if (DIRECTORY_SEPARATOR == '\\') { | |
435 | $dir = preg_replace( | |
436 | '|[/\\\\]files[/\\\\]civicrm[/\\\\].*$|', | |
437 | '\\\\files\\\\', | |
438 | $config->imageUploadDir | |
37b395e8 | 439 | ); |
4c235182 EM |
440 | } |
441 | else { | |
442 | $dir = preg_replace( | |
443 | '|/files/civicrm/.*$|', | |
444 | '/files/', | |
445 | $config->imageUploadDir | |
37b395e8 | 446 | ); |
4c235182 EM |
447 | } |
448 | ||
6a488035 | 449 | $matches = array(); |
e51744f5 | 450 | if (preg_match( |
4c235182 EM |
451 | '|/sites/([\w\.\-\_]+)/|', |
452 | $config->imageUploadDir, | |
453 | $matches | |
454 | )) { | |
6a488035 TO |
455 | $siteName = $matches[1]; |
456 | if ($siteName) { | |
457 | $siteName = "/sites/$siteName/"; | |
458 | $siteNamePos = strpos($dir, $siteName); | |
459 | if ($siteNamePos !== FALSE) { | |
460 | $siteRoot = substr($dir, 0, $siteNamePos); | |
461 | } | |
462 | } | |
463 | } | |
464 | } | |
465 | ||
6a488035 TO |
466 | return array($url, $dir, $siteName, $siteRoot); |
467 | } | |
468 | ||
4c235182 | 469 | /** |
fe482240 | 470 | * Return likely default settings. |
a6c01b45 CW |
471 | * @return array |
472 | * site settings | |
16b10e64 CW |
473 | * - $url |
474 | * - $dir Base Directory | |
475 | * - $siteName | |
476 | * - $siteRoot | |
4c235182 | 477 | */ |
00be9182 | 478 | public static function getBestGuessSettings() { |
6a488035 | 479 | $config = CRM_Core_Config::singleton(); |
4c235182 EM |
480 | |
481 | //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths | |
482 | if (DIRECTORY_SEPARATOR == '\\') { | |
483 | $needle = 'civicrm[/\\\\]templates_c[/\\\\].*$'; | |
484 | } | |
485 | else { | |
486 | $needle = 'civicrm/templates_c/.*$'; | |
487 | } | |
488 | ||
489 | $dir = preg_replace( | |
37b395e8 | 490 | "|$needle|", |
6a488035 TO |
491 | '', |
492 | $config->templateCompileDir | |
493 | ); | |
494 | ||
9977c6f5 | 495 | list($url, $siteName, $siteRoot) = $config->userSystem->getDefaultSiteSettings($dir); |
6a488035 TO |
496 | return array($url, $dir, $siteName, $siteRoot); |
497 | } | |
498 | ||
b5c2afd0 EM |
499 | /** |
500 | * @param array $defaultValues | |
501 | * | |
502 | * @return string | |
503 | * @throws Exception | |
504 | */ | |
00be9182 | 505 | public static function doSiteMove($defaultValues = array()) { |
6a488035 TO |
506 | $moveStatus = ts('Beginning site move process...') . '<br />'; |
507 | // get the current and guessed values | |
508 | list($oldURL, $oldDir, $oldSiteName, $oldSiteRoot) = self::getConfigSettings(); | |
509 | list($newURL, $newDir, $newSiteName, $newSiteRoot) = self::getBestGuessSettings(); | |
510 | ||
6a488035 TO |
511 | // retrieve these values from the argument list |
512 | $variables = array('URL', 'Dir', 'SiteName', 'SiteRoot', 'Val_1', 'Val_2', 'Val_3'); | |
513 | $states = array('old', 'new'); | |
514 | foreach ($variables as $varSuffix) { | |
515 | foreach ($states as $state) { | |
516 | $var = "{$state}{$varSuffix}"; | |
517 | if (!isset($$var)) { | |
518 | if (isset($defaultValues[$var])) { | |
519 | $$var = $defaultValues[$var]; | |
520 | } | |
521 | else { | |
522 | $$var = NULL; | |
523 | } | |
524 | } | |
525 | $$var = CRM_Utils_Request::retrieve($var, | |
526 | 'String', | |
527 | CRM_Core_DAO::$_nullArray, | |
528 | FALSE, | |
529 | $$var, | |
530 | 'REQUEST' | |
531 | ); | |
532 | } | |
533 | } | |
534 | ||
535 | $from = $to = array(); | |
536 | foreach ($variables as $varSuffix) { | |
537 | $oldVar = "old{$varSuffix}"; | |
538 | $newVar = "new{$varSuffix}"; | |
539 | //skip it if either is empty or both are exactly the same | |
540 | if ($$oldVar && | |
541 | $$newVar && | |
542 | $$oldVar != $$newVar | |
543 | ) { | |
544 | $from[] = $$oldVar; | |
545 | $to[] = $$newVar; | |
546 | } | |
547 | } | |
548 | ||
549 | $sql = " | |
550 | SELECT config_backend | |
551 | FROM civicrm_domain | |
552 | WHERE id = %1 | |
553 | "; | |
554 | $params = array(1 => array(CRM_Core_Config::domainID(), 'Integer')); | |
555 | $configBackend = CRM_Core_DAO::singleValueQuery($sql, $params); | |
556 | if (!$configBackend) { | |
557 | CRM_Core_Error::fatal(ts('Returning early due to unexpected error - civicrm_domain.config_backend column value is NULL. Try visiting CiviCRM Home page.')); | |
558 | } | |
559 | $configBackend = unserialize($configBackend); | |
560 | ||
561 | $configBackend = str_replace($from, | |
562 | $to, | |
563 | $configBackend | |
564 | ); | |
565 | ||
566 | $configBackend = serialize($configBackend); | |
567 | $sql = " | |
568 | UPDATE civicrm_domain | |
569 | SET config_backend = %2 | |
570 | WHERE id = %1 | |
571 | "; | |
572 | $params[2] = array($configBackend, 'String'); | |
573 | CRM_Core_DAO::executeQuery($sql, $params); | |
574 | ||
575 | // Apply the changes to civicrm_option_values | |
576 | $optionGroups = array('url_preferences', 'directory_preferences'); | |
577 | foreach ($optionGroups as $option) { | |
578 | foreach ($variables as $varSuffix) { | |
579 | $oldVar = "old{$varSuffix}"; | |
580 | $newVar = "new{$varSuffix}"; | |
581 | ||
582 | $from = $$oldVar; | |
583 | $to = $$newVar; | |
584 | ||
585 | if ($from && $to && $from != $to) { | |
586 | $sql = ' | |
587 | UPDATE civicrm_option_value | |
588 | SET value = REPLACE(value, %1, %2) | |
589 | WHERE option_group_id = ( | |
590 | SELECT id | |
591 | FROM civicrm_option_group | |
592 | WHERE name = %3 ) | |
593 | '; | |
4c235182 EM |
594 | $params = array( |
595 | 1 => array($from, 'String'), | |
6a488035 TO |
596 | 2 => array($to, 'String'), |
597 | 3 => array($option, 'String'), | |
598 | ); | |
599 | CRM_Core_DAO::executeQuery($sql, $params); | |
600 | } | |
601 | } | |
602 | } | |
603 | ||
4c235182 EM |
604 | $moveStatus .= |
605 | ts('Directory and Resource URLs have been updated in the moved database to reflect current site location.') . | |
606 | '<br />'; | |
6a488035 TO |
607 | |
608 | $config = CRM_Core_Config::singleton(); | |
609 | ||
610 | // clear the template_c and upload directory also | |
611 | $config->cleanup(3, TRUE); | |
612 | $moveStatus .= ts('Template cache and upload directory have been cleared.') . '<br />'; | |
613 | ||
614 | // clear all caches | |
615 | CRM_Core_Config::clearDBCache(); | |
616 | $moveStatus .= ts('Database cache tables cleared.') . '<br />'; | |
617 | ||
618 | $resetSessionTable = CRM_Utils_Request::retrieve('resetSessionTable', | |
619 | 'Boolean', | |
620 | CRM_Core_DAO::$_nullArray, | |
621 | FALSE, | |
622 | FALSE, | |
623 | 'REQUEST' | |
624 | ); | |
625 | if ($config->userSystem->is_drupal && | |
626 | $resetSessionTable | |
627 | ) { | |
628 | db_query("DELETE FROM {sessions} WHERE 1"); | |
629 | $moveStatus .= ts('Drupal session table cleared.') . '<br />'; | |
630 | } | |
631 | else { | |
632 | $session = CRM_Core_Session::singleton(); | |
633 | $session->reset(2); | |
634 | $moveStatus .= ts('Session has been reset.') . '<br />'; | |
635 | } | |
636 | ||
637 | return $moveStatus; | |
638 | } | |
639 | ||
640 | /** | |
fe482240 | 641 | * Takes a componentName and enables it in the config. |
6a488035 TO |
642 | * Primarily used during unit testing |
643 | * | |
6a0b768e TO |
644 | * @param string $componentName |
645 | * Name of the component to be enabled, needs to be valid. | |
6a488035 | 646 | * |
608e6658 | 647 | * @return bool |
a6c01b45 | 648 | * true if valid component name and enabling succeeds, else false |
6a488035 | 649 | */ |
00be9182 | 650 | public static function enableComponent($componentName) { |
6a488035 TO |
651 | $config = CRM_Core_Config::singleton(); |
652 | if (in_array($componentName, $config->enableComponents)) { | |
653 | // component is already enabled | |
654 | return TRUE; | |
655 | } | |
6a488035 TO |
656 | |
657 | // return if component does not exist | |
b086633a | 658 | if (!array_key_exists($componentName, CRM_Core_Component::getComponents())) { |
6a488035 TO |
659 | return FALSE; |
660 | } | |
661 | ||
3124edb3 | 662 | // get enabled-components from DB and add to the list |
608e6658 | 663 | $enabledComponents |
664 | = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components', NULL, array()); | |
3124edb3 | 665 | $enabledComponents[] = $componentName; |
6a488035 | 666 | |
b086633a TO |
667 | self::setEnabledComponents($enabledComponents); |
668 | ||
669 | return TRUE; | |
670 | } | |
671 | ||
b896fa44 EM |
672 | /** |
673 | * Disable specified component. | |
674 | * | |
675 | * @param string $componentName | |
676 | * | |
677 | * @return bool | |
678 | */ | |
00be9182 | 679 | public static function disableComponent($componentName) { |
b086633a | 680 | $config = CRM_Core_Config::singleton(); |
4c235182 EM |
681 | if (!in_array($componentName, $config->enableComponents) || |
682 | !array_key_exists($componentName, CRM_Core_Component::getComponents()) | |
683 | ) { | |
b896fa44 | 684 | // Post-condition is satisfied. |
b086633a TO |
685 | return TRUE; |
686 | } | |
687 | ||
688 | // get enabled-components from DB and add to the list | |
608e6658 | 689 | $enabledComponents |
690 | = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components', NULL, array()); | |
b086633a TO |
691 | $enabledComponents = array_diff($enabledComponents, array($componentName)); |
692 | ||
693 | self::setEnabledComponents($enabledComponents); | |
694 | ||
695 | return TRUE; | |
696 | } | |
697 | ||
b896fa44 EM |
698 | /** |
699 | * Set enabled components. | |
700 | * | |
701 | * @param array $enabledComponents | |
702 | */ | |
b086633a TO |
703 | public static function setEnabledComponents($enabledComponents) { |
704 | $config = CRM_Core_Config::singleton(); | |
705 | $components = CRM_Core_Component::getComponents(); | |
706 | ||
3124edb3 DS |
707 | $enabledComponentIDs = array(); |
708 | foreach ($enabledComponents as $name) { | |
709 | $enabledComponentIDs[] = $components[$name]->componentID; | |
6a488035 | 710 | } |
6a488035 TO |
711 | |
712 | // fix the config object | |
3124edb3 DS |
713 | $config->enableComponents = $enabledComponents; |
714 | $config->enableComponentIDs = $enabledComponentIDs; | |
6a488035 TO |
715 | |
716 | // also force reset of component array | |
717 | CRM_Core_Component::getEnabledComponents(TRUE); | |
718 | ||
3124edb3 DS |
719 | // update DB |
720 | CRM_Core_BAO_Setting::setItem($enabledComponents, | |
b086633a | 721 | CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components'); |
6a488035 TO |
722 | } |
723 | ||
b5c2afd0 EM |
724 | /** |
725 | * @return array | |
726 | */ | |
00be9182 | 727 | public static function skipVars() { |
6a488035 | 728 | return array( |
4c235182 EM |
729 | 'dsn', |
730 | 'templateCompileDir', | |
6a488035 TO |
731 | 'userFrameworkDSN', |
732 | 'userFramework', | |
4c235182 EM |
733 | 'userFrameworkBaseURL', |
734 | 'userFrameworkClass', | |
735 | 'userHookClass', | |
736 | 'userPermissionClass', | |
59735506 | 737 | 'userPermissionTemp', |
4c235182 EM |
738 | 'userFrameworkURLVar', |
739 | 'userFrameworkVersion', | |
740 | 'newBaseURL', | |
741 | 'newBaseDir', | |
742 | 'newSiteName', | |
743 | 'configAndLogDir', | |
744 | 'qfKey', | |
745 | 'gettextResourceDir', | |
746 | 'cleanURL', | |
747 | 'locale_custom_strings', | |
748 | 'localeCustomStrings', | |
6a488035 TO |
749 | 'autocompleteContactSearch', |
750 | 'autocompleteContactReference', | |
751 | 'checksumTimeout', | |
752 | ); | |
753 | } | |
96025800 | 754 | |
6a488035 | 755 | } |