3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * Function to create civicrm settings. This is the same as add but it clears the cache and
45 * reloads the config object
47 * @params array $params associated array of civicrm variables
54 static function create($params) {
56 $cache = CRM_Utils_Cache
::singleton();
57 $cache->delete('CRM_Core_Config');
58 $cache->delete('CRM_Core_Config' . CRM_Core_Config
::domainID());
59 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
63 * Function to add civicrm settings
65 * @params array $params associated array of civicrm variables
72 static function add(&$params) {
73 self
::fixParams($params);
75 // also set a template url so js files can use this
77 $params['civiRelativeURL'] = CRM_Utils_System
::url('CIVI_BASE_TEMPLATE');
78 $params['civiRelativeURL'] =
82 $params['civiRelativeURL']
85 // also add the version number for use by template / js etc
86 $params['civiVersion'] = CRM_Utils_System
::version();
88 $domain = new CRM_Core_DAO_Domain();
89 $domain->id
= CRM_Core_Config
::domainID();
91 if ($domain->config_backend
) {
92 $values = unserialize($domain->config_backend
);
93 self
::formatParams($params, $values);
97 if (isset($params['localeCustomStrings']) &&
98 is_array($params['localeCustomStrings'])
100 $domain->locale_custom_strings
= serialize($params['localeCustomStrings']);
103 // unset any of the variables we read from file that should not be stored in the database
104 // the username and certpath are stored flat with _test and _live
106 $skipVars = self
::skipVars();
107 foreach ($skipVars as $var) {
108 unset($params[$var]);
111 CRM_Core_BAO_Setting
::fixAndStoreDirAndURL($params);
113 // also skip all Dir Params, we dont need to store those in the DB!
114 foreach ($params as $name => $val) {
115 if (substr($name, -3) == 'Dir') {
116 unset($params[$name]);
120 //keep user preferred language upto date, CRM-7746
121 $session = CRM_Core_Session
::singleton();
122 $lcMessages = CRM_Utils_Array
::value('lcMessages', $params);
123 if ($lcMessages && $session->get('userID')) {
124 $languageLimit = CRM_Utils_Array
::value('languageLimit', $params);
125 if (is_array($languageLimit) &&
126 !in_array($lcMessages, array_keys($languageLimit))
128 $lcMessages = $session->get('lcMessages');
131 $ufm = new CRM_Core_DAO_UFMatch();
132 $ufm->contact_id
= $session->get('userID');
133 if ($lcMessages && $ufm->find(TRUE)) {
134 $ufm->language
= $lcMessages;
136 $session->set('lcMessages', $lcMessages);
137 $params['lcMessages'] = $lcMessages;
141 $domain->config_backend
= serialize($params);
146 * Function to fix civicrm setting variables
148 * @params array $params associated array of civicrm variables
155 static function fixParams(&$params) {
156 // in our old civicrm.settings.php we were using ISO code for country and
157 // province limit, now we have changed it to use ids
159 $countryIsoCodes = CRM_Core_PseudoConstant
::countryIsoCode();
161 $specialArray = array('countryLimit', 'provinceLimit');
163 foreach ($params as $key => $value) {
164 if (in_array($key, $specialArray) && is_array($value)) {
165 foreach ($value as $k => $val) {
166 if (!is_numeric($val)) {
167 $params[$key][$k] = array_search($val, $countryIsoCodes);
171 elseif ($key == 'defaultContactCountry') {
172 if (!is_numeric($value)) {
173 $params[$key] = array_search($value, $countryIsoCodes);
180 * Function to format the array containing before inserting in db
182 * @param array $params associated array of civicrm variables(submitted)
183 * @param array $values associated array of civicrm variables stored in db
188 static function formatParams(&$params, &$values) {
189 if (empty($params) ||
195 foreach ($params as $key => $val) {
196 if (array_key_exists($key, $values)) {
197 unset($values[$key]);
200 $params = array_merge($params, $values);
205 * Function to retrieve the settings values from db
209 * @return array $defaults
212 static function retrieve(&$defaults) {
213 $domain = new CRM_Core_DAO_Domain();
215 //we are initializing config, really can't use, CRM-7863
217 if (defined('CIVICRM_UF') && CIVICRM_UF
== 'Joomla') {
221 if (CRM_Core_Config
::isUpgradeMode()) {
222 $domain->selectAdd('config_backend');
224 elseif (CRM_Utils_Array
::value($urlVar, $_GET) == 'admin/modules/list/confirm') {
225 $domain->selectAdd('config_backend', 'locales');
228 $domain->selectAdd('config_backend, locales, locale_custom_strings');
231 $domain->id
= CRM_Core_Config
::domainID();
233 if ($domain->config_backend
) {
234 $defaults = unserialize($domain->config_backend
);
235 if ($defaults === FALSE ||
!is_array($defaults)) {
240 $skipVars = self
::skipVars();
241 foreach ($skipVars as $skip) {
242 if (array_key_exists($skip, $defaults)) {
243 unset($defaults[$skip]);
247 // check if there are any locale strings
248 if ($domain->locale_custom_strings
) {
249 $defaults['localeCustomStrings'] = unserialize($domain->locale_custom_strings
);
252 $defaults['localeCustomStrings'] = NULL;
255 // are we in a multi-language setup?
256 $multiLang = $domain->locales ?
TRUE : FALSE;
258 // set the current language
261 $session = CRM_Core_Session
::singleton();
263 // on multi-lang sites based on request and civicrm_uf_match
265 $lcMessagesRequest = CRM_Utils_Request
::retrieve('lcMessages', 'String', $this);
266 $languageLimit = array();
267 if (array_key_exists('languageLimit', $defaults) && is_array($defaults['languageLimit'])) {
268 $languageLimit = $defaults['languageLimit'];
271 if (in_array($lcMessagesRequest, array_keys($languageLimit))) {
272 $lcMessages = $lcMessagesRequest;
274 //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache.
275 CRM_Core_BAO_Cache
::deleteGroup('navigation');
278 $lcMessagesRequest = NULL;
281 if (!$lcMessagesRequest) {
282 $lcMessagesSession = $session->get('lcMessages');
283 if (in_array($lcMessagesSession, array_keys($languageLimit))) {
284 $lcMessages = $lcMessagesSession;
287 $lcMessagesSession = NULL;
291 if ($lcMessagesRequest) {
292 $ufm = new CRM_Core_DAO_UFMatch();
293 $ufm->contact_id
= $session->get('userID');
294 if ($ufm->find(TRUE)) {
295 $ufm->language
= $lcMessages;
298 $session->set('lcMessages', $lcMessages);
301 if (!$lcMessages and $session->get('userID')) {
302 $ufm = new CRM_Core_DAO_UFMatch();
303 $ufm->contact_id
= $session->get('userID');
304 if ($ufm->find(TRUE) &&
305 in_array($ufm->language
, array_keys($languageLimit))
307 $lcMessages = $ufm->language
;
309 $session->set('lcMessages', $lcMessages);
314 // try to inherit the language from the hosting CMS
315 if (!empty($defaults['inheritLocale'])) {
316 // FIXME: On multilanguage installs, CRM_Utils_System::getUFLocale() in many cases returns nothing if $dbLocale is not set
317 $dbLocale = $multiLang ?
"_{$defaults['lcMessages']}" : '';
318 $lcMessages = CRM_Utils_System
::getUFLocale();
319 if ($domain->locales
and !in_array($lcMessages, explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
327 // update config lcMessages - CRM-5027 fixed.
328 $defaults['lcMessages'] = $lcMessages;
331 // if a single-lang site or the above didn't yield a result, use default
332 $lcMessages = CRM_Utils_Array
::value( 'lcMessages', $defaults );
335 // set suffix for table names - use views if more than one language
336 $dbLocale = $multiLang ?
"_{$lcMessages}" : '';
338 // FIXME: an ugly hack to fix CRM-4041
340 $tsLocale = $lcMessages;
342 // FIXME: as bad aplace as any to fix CRM-5428
343 // (to be moved to a sane location along with the above)
344 if (function_exists('mb_internal_encoding')) {
345 mb_internal_encoding('UTF-8');
349 // dont add if its empty
350 if (!empty($defaults)) {
351 // retrieve directory and url preferences also
352 CRM_Core_BAO_Setting
::retrieveDirectoryAndURLPreferences($defaults);
354 // Pickup enabled-components from settings table if found.
355 $enableComponents = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components', NULL, array());
356 if (!empty($enableComponents)) {
357 $defaults['enableComponents'] = $enableComponents;
359 $components = CRM_Core_Component
::getComponents();
360 $enabledComponentIDs = array();
361 foreach ($defaults['enableComponents'] as $name) {
362 $enabledComponentIDs[] = $components[$name]->componentID
;
364 $defaults['enableComponentIDs'] = $enabledComponentIDs;
372 static function getConfigSettings() {
373 $config = CRM_Core_Config
::singleton();
375 $url = $dir = $siteName = $siteRoot = NULL;
376 if ($config->userFramework
== 'Joomla') {
378 '|administrator/components/com_civicrm/civicrm/|',
380 $config->userFrameworkResourceURL
383 // lets use imageUploadDir since we dont mess around with its values
384 // in the config object, lets kep it a bit generic since folks
385 // might have different values etc
387 '|civicrm/templates_c/.*$|',
389 $config->templateCompileDir
391 $siteRoot = preg_replace(
392 '|/media/civicrm/.*$|',
394 $config->imageUploadDir
397 else if ($config->userFramework
== 'WordPress') {
399 '|wp-content/plugins/civicrm/civicrm/|',
401 $config->userFrameworkResourceURL
404 // lets use imageUploadDir since we dont mess around with its values
405 // in the config object, lets kep it a bit generic since folks
406 // might have different values etc
408 '|civicrm/templates_c/.*$|',
410 $config->templateCompileDir
412 $siteRoot = preg_replace(
413 '|/wp-content/plugins/files/civicrm/.*$|',
415 $config->imageUploadDir
420 '|sites/[\w\.\-\_]+/modules/civicrm/|',
422 $config->userFrameworkResourceURL
425 // lets use imageUploadDir since we dont mess around with its values
426 // in the config object, lets kep it a bit generic since folks
427 // might have different values etc
429 '|/files/civicrm/.*$|',
431 $config->imageUploadDir
436 '|/sites/([\w\.\-\_]+)/|',
437 $config->imageUploadDir
,
440 $siteName = $matches[1];
442 $siteName = "/sites/$siteName/";
443 $siteNamePos = strpos($dir, $siteName);
444 if ($siteNamePos !== FALSE) {
445 $siteRoot = substr($dir, 0, $siteNamePos);
452 return array($url, $dir, $siteName, $siteRoot);
456 * Return likely default settings
457 * @return array site settings
459 * - $dir Base Directory
463 static function getBestGuessSettings() {
464 $config = CRM_Core_Config
::singleton();
466 '|civicrm/templates_c/.*$|',
468 $config->templateCompileDir
471 list($url, $siteName, $siteRoot) = $config->userSystem
->getDefaultSiteSettings($dir);
472 return array($url, $dir, $siteName, $siteRoot);
476 * @param array $defaultValues
481 static function doSiteMove($defaultValues = array() ) {
482 $moveStatus = ts('Beginning site move process...') . '<br />';
483 // get the current and guessed values
484 list($oldURL, $oldDir, $oldSiteName, $oldSiteRoot) = self
::getConfigSettings();
485 list($newURL, $newDir, $newSiteName, $newSiteRoot) = self
::getBestGuessSettings();
488 // retrieve these values from the argument list
489 $variables = array('URL', 'Dir', 'SiteName', 'SiteRoot', 'Val_1', 'Val_2', 'Val_3');
490 $states = array('old', 'new');
491 foreach ($variables as $varSuffix) {
492 foreach ($states as $state) {
493 $var = "{$state}{$varSuffix}";
495 if (isset($defaultValues[$var])) {
496 $
$var = $defaultValues[$var];
502 $
$var = CRM_Utils_Request
::retrieve($var,
504 CRM_Core_DAO
::$_nullArray,
512 $from = $to = array();
513 foreach ($variables as $varSuffix) {
514 $oldVar = "old{$varSuffix}";
515 $newVar = "new{$varSuffix}";
516 //skip it if either is empty or both are exactly the same
527 SELECT config_backend
531 $params = array(1 => array(CRM_Core_Config
::domainID(), 'Integer'));
532 $configBackend = CRM_Core_DAO
::singleValueQuery($sql, $params);
533 if (!$configBackend) {
534 CRM_Core_Error
::fatal(ts('Returning early due to unexpected error - civicrm_domain.config_backend column value is NULL. Try visiting CiviCRM Home page.'));
536 $configBackend = unserialize($configBackend);
538 $configBackend = str_replace($from,
543 $configBackend = serialize($configBackend);
545 UPDATE civicrm_domain
546 SET config_backend = %2
549 $params[2] = array($configBackend, 'String');
550 CRM_Core_DAO
::executeQuery($sql, $params);
552 // Apply the changes to civicrm_option_values
553 $optionGroups = array('url_preferences', 'directory_preferences');
554 foreach ($optionGroups as $option) {
555 foreach ($variables as $varSuffix) {
556 $oldVar = "old{$varSuffix}";
557 $newVar = "new{$varSuffix}";
562 if ($from && $to && $from != $to) {
564 UPDATE civicrm_option_value
565 SET value = REPLACE(value, %1, %2)
566 WHERE option_group_id = (
568 FROM civicrm_option_group
571 $params = array(1 => array($from, 'String'),
572 2 => array($to, 'String'),
573 3 => array($option, 'String'),
575 CRM_Core_DAO
::executeQuery($sql, $params);
580 $moveStatus .= ts('Directory and Resource URLs have been updated in the moved database to reflect current site location.') . '<br />';
582 $config = CRM_Core_Config
::singleton();
584 // clear the template_c and upload directory also
585 $config->cleanup(3, TRUE);
586 $moveStatus .= ts('Template cache and upload directory have been cleared.') . '<br />';
589 CRM_Core_Config
::clearDBCache();
590 $moveStatus .= ts('Database cache tables cleared.') . '<br />';
592 $resetSessionTable = CRM_Utils_Request
::retrieve('resetSessionTable',
594 CRM_Core_DAO
::$_nullArray,
599 if ($config->userSystem
->is_drupal
&&
602 db_query("DELETE FROM {sessions} WHERE 1");
603 $moveStatus .= ts('Drupal session table cleared.') . '<br />';
606 $session = CRM_Core_Session
::singleton();
608 $moveStatus .= ts('Session has been reset.') . '<br />';
615 * takes a componentName and enables it in the config
616 * Primarily used during unit testing
618 * @param string $componentName name of the component to be enabled, needs to be valid
620 * @return boolean - true if valid component name and enabling succeeds, else false
623 static function enableComponent($componentName) {
624 $config = CRM_Core_Config
::singleton();
625 if (in_array($componentName, $config->enableComponents
)) {
626 // component is already enabled
629 $components = CRM_Core_Component
::getComponents();
631 // return if component does not exist
632 if (!array_key_exists($componentName, $components)) {
636 // get enabled-components from DB and add to the list
638 CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components', NULL, array());
639 $enabledComponents[] = $componentName;
641 $enabledComponentIDs = array();
642 foreach ($enabledComponents as $name) {
643 $enabledComponentIDs[] = $components[$name]->componentID
;
646 // fix the config object
647 $config->enableComponents
= $enabledComponents;
648 $config->enableComponentIDs
= $enabledComponentIDs;
650 // also force reset of component array
651 CRM_Core_Component
::getEnabledComponents(TRUE);
654 CRM_Core_BAO_Setting
::setItem($enabledComponents,
655 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,'enable_components');
663 static function skipVars() {
665 'dsn', 'templateCompileDir',
668 'userFrameworkBaseURL', 'userFrameworkClass', 'userHookClass',
669 'userPermissionClass', 'userFrameworkURLVar', 'userFrameworkVersion',
670 'newBaseURL', 'newBaseDir', 'newSiteName', 'configAndLogDir',
671 'qfKey', 'gettextResourceDir', 'cleanURL',
672 'locale_custom_strings', 'localeCustomStrings',
673 'autocompleteContactSearch',
674 'autocompleteContactReference',