3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
38 * file contains functions used in civicrm configuration
41 class CRM_Core_BAO_ConfigSetting
{
44 * Create civicrm settings. This is the same as add but it clears the cache and
45 * reloads the config object
47 * @param array $params
48 * Associated array of civicrm variables.
53 public static function create($params) {
55 $cache = CRM_Utils_Cache
::singleton();
56 $cache->delete('CRM_Core_Config');
57 $cache->delete('CRM_Core_Config' . CRM_Core_Config
::domainID());
58 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
62 * Add civicrm settings
64 * @param array $params
65 * Associated array of civicrm variables.
70 public static function add(&$params) {
71 self
::fixParams($params);
73 // also set a template url so js files can use this
75 $params['civiRelativeURL'] = CRM_Utils_System
::url('CIVI_BASE_TEMPLATE');
76 $params['civiRelativeURL'] =
80 $params['civiRelativeURL']
83 // also add the version number for use by template / js etc
84 $params['civiVersion'] = CRM_Utils_System
::version();
86 $domain = new CRM_Core_DAO_Domain();
87 $domain->id
= CRM_Core_Config
::domainID();
89 if ($domain->config_backend
) {
90 $values = unserialize($domain->config_backend
);
91 self
::formatParams($params, $values);
95 if (isset($params['localeCustomStrings']) &&
96 is_array($params['localeCustomStrings'])
98 $domain->locale_custom_strings
= serialize($params['localeCustomStrings']);
101 // unset any of the variables we read from file that should not be stored in the database
102 // the username and certpath are stored flat with _test and _live
104 $skipVars = self
::skipVars();
105 foreach ($skipVars as $var) {
106 unset($params[$var]);
109 CRM_Core_BAO_Setting
::fixAndStoreDirAndURL($params);
111 // also skip all Dir Params, we dont need to store those in the DB!
112 foreach ($params as $name => $val) {
113 if (substr($name, -3) == 'Dir') {
114 unset($params[$name]);
118 //keep user preferred language upto date, CRM-7746
119 $session = CRM_Core_Session
::singleton();
120 $lcMessages = CRM_Utils_Array
::value('lcMessages', $params);
121 if ($lcMessages && $session->get('userID')) {
122 $languageLimit = CRM_Utils_Array
::value('languageLimit', $params);
123 if (is_array($languageLimit) &&
124 !in_array($lcMessages, array_keys($languageLimit))
126 $lcMessages = $session->get('lcMessages');
129 $ufm = new CRM_Core_DAO_UFMatch();
130 $ufm->contact_id
= $session->get('userID');
131 if ($lcMessages && $ufm->find(TRUE)) {
132 $ufm->language
= $lcMessages;
134 $session->set('lcMessages', $lcMessages);
135 $params['lcMessages'] = $lcMessages;
139 $domain->config_backend
= serialize($params);
144 * Fix civicrm setting variables
146 * @param array $params
147 * Associated array of civicrm variables.
152 public static function fixParams(&$params) {
153 // in our old civicrm.settings.php we were using ISO code for country and
154 // province limit, now we have changed it to use ids
156 $countryIsoCodes = CRM_Core_PseudoConstant
::countryIsoCode();
158 $specialArray = array('countryLimit', 'provinceLimit');
160 foreach ($params as $key => $value) {
161 if (in_array($key, $specialArray) && is_array($value)) {
162 foreach ($value as $k => $val) {
163 if (!is_numeric($val)) {
164 $params[$key][$k] = array_search($val, $countryIsoCodes);
168 elseif ($key == 'defaultContactCountry') {
169 if (!is_numeric($value)) {
170 $params[$key] = array_search($value, $countryIsoCodes);
177 * Format the array containing before inserting in db
179 * @param array $params
180 * Associated array of civicrm variables(submitted).
181 * @param array $values
182 * Associated array of civicrm variables stored in db.
187 public static function formatParams(&$params, &$values) {
188 if (empty($params) ||
194 foreach ($params as $key => $val) {
195 if (array_key_exists($key, $values)) {
196 unset($values[$key]);
199 $params = array_merge($params, $values);
204 * Retrieve the settings values from db
208 * @return array $defaults
211 public static function retrieve(&$defaults) {
212 $domain = new CRM_Core_DAO_Domain();
214 //we are initializing config, really can't use, CRM-7863
216 if (defined('CIVICRM_UF') && CIVICRM_UF
== 'Joomla') {
220 if (CRM_Core_Config
::isUpgradeMode()) {
221 $domain->selectAdd('config_backend');
223 elseif (CRM_Utils_Array
::value($urlVar, $_GET) == 'admin/modules/list/confirm') {
224 $domain->selectAdd('config_backend', 'locales');
227 $domain->selectAdd('config_backend, locales, locale_custom_strings');
230 $domain->id
= CRM_Core_Config
::domainID();
232 if ($domain->config_backend
) {
233 $defaults = unserialize($domain->config_backend
);
234 if ($defaults === FALSE ||
!is_array($defaults)) {
239 $skipVars = self
::skipVars();
240 foreach ($skipVars as $skip) {
241 if (array_key_exists($skip, $defaults)) {
242 unset($defaults[$skip]);
246 // check if there are any locale strings
247 if ($domain->locale_custom_strings
) {
248 $defaults['localeCustomStrings'] = unserialize($domain->locale_custom_strings
);
251 $defaults['localeCustomStrings'] = NULL;
254 // are we in a multi-language setup?
255 $multiLang = $domain->locales ?
TRUE : FALSE;
257 // set the current language
260 $session = CRM_Core_Session
::singleton();
262 // on multi-lang sites based on request and civicrm_uf_match
264 $lcMessagesRequest = CRM_Utils_Request
::retrieve('lcMessages', 'String', $this);
265 $languageLimit = array();
266 if (array_key_exists('languageLimit', $defaults) && is_array($defaults['languageLimit'])) {
267 $languageLimit = $defaults['languageLimit'];
270 if (in_array($lcMessagesRequest, array_keys($languageLimit))) {
271 $lcMessages = $lcMessagesRequest;
273 //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache.
274 CRM_Core_BAO_Cache
::deleteGroup('navigation');
277 $lcMessagesRequest = NULL;
280 if (!$lcMessagesRequest) {
281 $lcMessagesSession = $session->get('lcMessages');
282 if (in_array($lcMessagesSession, array_keys($languageLimit))) {
283 $lcMessages = $lcMessagesSession;
286 $lcMessagesSession = NULL;
290 if ($lcMessagesRequest) {
291 $ufm = new CRM_Core_DAO_UFMatch();
292 $ufm->contact_id
= $session->get('userID');
293 if ($ufm->find(TRUE)) {
294 $ufm->language
= $lcMessages;
297 $session->set('lcMessages', $lcMessages);
300 if (!$lcMessages and $session->get('userID')) {
301 $ufm = new CRM_Core_DAO_UFMatch();
302 $ufm->contact_id
= $session->get('userID');
303 if ($ufm->find(TRUE) &&
304 in_array($ufm->language
, array_keys($languageLimit))
306 $lcMessages = $ufm->language
;
308 $session->set('lcMessages', $lcMessages);
313 // try to inherit the language from the hosting CMS
314 if (!empty($defaults['inheritLocale'])) {
315 // FIXME: On multilanguage installs, CRM_Utils_System::getUFLocale() in many cases returns nothing if $dbLocale is not set
316 $dbLocale = $multiLang ?
"_{$defaults['lcMessages']}" : '';
317 $lcMessages = CRM_Utils_System
::getUFLocale();
318 if ($domain->locales
and !in_array($lcMessages, explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
326 if (empty($lcMessages)) {
327 //CRM-11993 - if a single-lang site, use default
328 $lcMessages = CRM_Utils_Array
::value('lcMessages', $defaults);
331 // set suffix for table names - use views if more than one language
332 $dbLocale = $multiLang ?
"_{$lcMessages}" : '';
334 // FIXME: an ugly hack to fix CRM-4041
336 $tsLocale = $lcMessages;
338 // FIXME: as bad aplace as any to fix CRM-5428
339 // (to be moved to a sane location along with the above)
340 if (function_exists('mb_internal_encoding')) {
341 mb_internal_encoding('UTF-8');
345 // dont add if its empty
346 if (!empty($defaults)) {
347 // retrieve directory and url preferences also
348 CRM_Core_BAO_Setting
::retrieveDirectoryAndURLPreferences($defaults);
350 // Pickup enabled-components from settings table if found.
351 $enableComponents = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components', NULL, array());
352 if (!empty($enableComponents)) {
353 $defaults['enableComponents'] = $enableComponents;
355 $components = CRM_Core_Component
::getComponents();
356 $enabledComponentIDs = array();
357 foreach ($defaults['enableComponents'] as $name) {
358 $enabledComponentIDs[] = $components[$name]->componentID
;
360 $defaults['enableComponentIDs'] = $enabledComponentIDs;
368 public static function getConfigSettings() {
369 $config = CRM_Core_Config
::singleton();
371 $url = $dir = $siteName = $siteRoot = NULL;
372 if ($config->userFramework
== 'Joomla') {
374 '|administrator/components/com_civicrm/civicrm/|',
376 $config->userFrameworkResourceURL
379 // lets use imageUploadDir since we dont mess around with its values
380 // in the config object, lets kep it a bit generic since folks
381 // might have different values etc
383 //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
384 if (DIRECTORY_SEPARATOR
== '\\') {
386 '|civicrm[/\\\\]templates_c[/\\\\].*$|',
388 $config->templateCompileDir
393 '|civicrm/templates_c/.*$|',
395 $config->templateCompileDir
399 $siteRoot = preg_replace(
400 '|/media/civicrm/.*$|',
402 $config->imageUploadDir
405 else if ($config->userFramework
== 'WordPress') {
407 '|wp-content/plugins/civicrm/civicrm/|',
409 $config->userFrameworkResourceURL
412 // lets use imageUploadDir since we dont mess around with its values
413 // in the config object, lets kep it a bit generic since folks
414 // might have different values etc
416 //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
417 if (DIRECTORY_SEPARATOR
== '\\') {
419 '|civicrm[/\\\\]templates_c[/\\\\].*$|',
421 $config->templateCompileDir
426 '|civicrm/templates_c/.*$|',
428 $config->templateCompileDir
432 $siteRoot = preg_replace(
433 '|/wp-content/plugins/files/civicrm/.*$|',
435 $config->imageUploadDir
440 '|sites/[\w\.\-\_]+/modules/civicrm/|',
442 $config->userFrameworkResourceURL
445 // lets use imageUploadDir since we dont mess around with its values
446 // in the config object, lets kep it a bit generic since folks
447 // might have different values etc
449 //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
450 if (DIRECTORY_SEPARATOR
== '\\') {
452 '|[/\\\\]files[/\\\\]civicrm[/\\\\].*$|',
454 $config->imageUploadDir
459 '|/files/civicrm/.*$|',
461 $config->imageUploadDir
467 '|/sites/([\w\.\-\_]+)/|',
468 $config->imageUploadDir
,
471 $siteName = $matches[1];
473 $siteName = "/sites/$siteName/";
474 $siteNamePos = strpos($dir, $siteName);
475 if ($siteNamePos !== FALSE) {
476 $siteRoot = substr($dir, 0, $siteNamePos);
482 return array($url, $dir, $siteName, $siteRoot);
486 * Return likely default settings
487 * @return array site settings
489 * - $dir Base Directory
493 public static function getBestGuessSettings() {
494 $config = CRM_Core_Config
::singleton();
496 //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
497 if (DIRECTORY_SEPARATOR
== '\\') {
498 $needle = 'civicrm[/\\\\]templates_c[/\\\\].*$';
501 $needle = 'civicrm/templates_c/.*$';
507 $config->templateCompileDir
510 list($url, $siteName, $siteRoot) = $config->userSystem
->getDefaultSiteSettings($dir);
511 return array($url, $dir, $siteName, $siteRoot);
515 * @param array $defaultValues
520 public static function doSiteMove($defaultValues = array()) {
521 $moveStatus = ts('Beginning site move process...') . '<br />';
522 // get the current and guessed values
523 list($oldURL, $oldDir, $oldSiteName, $oldSiteRoot) = self
::getConfigSettings();
524 list($newURL, $newDir, $newSiteName, $newSiteRoot) = self
::getBestGuessSettings();
527 // retrieve these values from the argument list
528 $variables = array('URL', 'Dir', 'SiteName', 'SiteRoot', 'Val_1', 'Val_2', 'Val_3');
529 $states = array('old', 'new');
530 foreach ($variables as $varSuffix) {
531 foreach ($states as $state) {
532 $var = "{$state}{$varSuffix}";
534 if (isset($defaultValues[$var])) {
535 $
$var = $defaultValues[$var];
541 $
$var = CRM_Utils_Request
::retrieve($var,
543 CRM_Core_DAO
::$_nullArray,
551 $from = $to = array();
552 foreach ($variables as $varSuffix) {
553 $oldVar = "old{$varSuffix}";
554 $newVar = "new{$varSuffix}";
555 //skip it if either is empty or both are exactly the same
566 SELECT config_backend
570 $params = array(1 => array(CRM_Core_Config
::domainID(), 'Integer'));
571 $configBackend = CRM_Core_DAO
::singleValueQuery($sql, $params);
572 if (!$configBackend) {
573 CRM_Core_Error
::fatal(ts('Returning early due to unexpected error - civicrm_domain.config_backend column value is NULL. Try visiting CiviCRM Home page.'));
575 $configBackend = unserialize($configBackend);
577 $configBackend = str_replace($from,
582 $configBackend = serialize($configBackend);
584 UPDATE civicrm_domain
585 SET config_backend = %2
588 $params[2] = array($configBackend, 'String');
589 CRM_Core_DAO
::executeQuery($sql, $params);
591 // Apply the changes to civicrm_option_values
592 $optionGroups = array('url_preferences', 'directory_preferences');
593 foreach ($optionGroups as $option) {
594 foreach ($variables as $varSuffix) {
595 $oldVar = "old{$varSuffix}";
596 $newVar = "new{$varSuffix}";
601 if ($from && $to && $from != $to) {
603 UPDATE civicrm_option_value
604 SET value = REPLACE(value, %1, %2)
605 WHERE option_group_id = (
607 FROM civicrm_option_group
611 1 => array($from, 'String'),
612 2 => array($to, 'String'),
613 3 => array($option, 'String'),
615 CRM_Core_DAO
::executeQuery($sql, $params);
621 ts('Directory and Resource URLs have been updated in the moved database to reflect current site location.') .
624 $config = CRM_Core_Config
::singleton();
626 // clear the template_c and upload directory also
627 $config->cleanup(3, TRUE);
628 $moveStatus .= ts('Template cache and upload directory have been cleared.') . '<br />';
631 CRM_Core_Config
::clearDBCache();
632 $moveStatus .= ts('Database cache tables cleared.') . '<br />';
634 $resetSessionTable = CRM_Utils_Request
::retrieve('resetSessionTable',
636 CRM_Core_DAO
::$_nullArray,
641 if ($config->userSystem
->is_drupal
&&
644 db_query("DELETE FROM {sessions} WHERE 1");
645 $moveStatus .= ts('Drupal session table cleared.') . '<br />';
648 $session = CRM_Core_Session
::singleton();
650 $moveStatus .= ts('Session has been reset.') . '<br />';
657 * Takes a componentName and enables it in the config
658 * Primarily used during unit testing
660 * @param string $componentName
661 * Name of the component to be enabled, needs to be valid.
663 * @return boolean - true if valid component name and enabling succeeds, else false
666 public static function enableComponent($componentName) {
667 $config = CRM_Core_Config
::singleton();
668 if (in_array($componentName, $config->enableComponents
)) {
669 // component is already enabled
673 // return if component does not exist
674 if (!array_key_exists($componentName, CRM_Core_Component
::getComponents())) {
678 // get enabled-components from DB and add to the list
680 CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components', NULL, array());
681 $enabledComponents[] = $componentName;
683 self
::setEnabledComponents($enabledComponents);
688 public static function disableComponent($componentName) {
689 $config = CRM_Core_Config
::singleton();
690 if (!in_array($componentName, $config->enableComponents
) ||
691 !array_key_exists($componentName, CRM_Core_Component
::getComponents())
693 // post-condition satisified
697 // get enabled-components from DB and add to the list
699 CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components', NULL, array());
700 $enabledComponents = array_diff($enabledComponents, array($componentName));
702 self
::setEnabledComponents($enabledComponents);
707 public static function setEnabledComponents($enabledComponents) {
708 $config = CRM_Core_Config
::singleton();
709 $components = CRM_Core_Component
::getComponents();
711 $enabledComponentIDs = array();
712 foreach ($enabledComponents as $name) {
713 $enabledComponentIDs[] = $components[$name]->componentID
;
716 // fix the config object
717 $config->enableComponents
= $enabledComponents;
718 $config->enableComponentIDs
= $enabledComponentIDs;
720 // also force reset of component array
721 CRM_Core_Component
::getEnabledComponents(TRUE);
724 CRM_Core_BAO_Setting
::setItem($enabledComponents,
725 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components');
731 public static function skipVars() {
734 'templateCompileDir',
737 'userFrameworkBaseURL',
738 'userFrameworkClass',
740 'userPermissionClass',
741 'userFrameworkURLVar',
742 'userFrameworkVersion',
748 'gettextResourceDir',
750 'locale_custom_strings',
751 'localeCustomStrings',
752 'autocompleteContactSearch',
753 'autocompleteContactReference',