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