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
,
328 // update config lcMessages - CRM-5027 fixed.
329 $defaults['lcMessages'] = $lcMessages;
332 // if a single-lang site or the above didn't yield a result, use default
333 $lcMessages = CRM_Utils_Array
::value('lcMessages', $defaults);
336 // set suffix for table names - use views if more than one language
337 $dbLocale = $multiLang ?
"_{$lcMessages}" : '';
339 // FIXME: an ugly hack to fix CRM-4041
341 $tsLocale = $lcMessages;
343 // FIXME: as bad aplace as any to fix CRM-5428
344 // (to be moved to a sane location along with the above)
345 if (function_exists('mb_internal_encoding')) {
346 mb_internal_encoding('UTF-8');
350 // dont add if its empty
351 if (!empty($defaults)) {
352 // retrieve directory and url preferences also
353 CRM_Core_BAO_Setting
::retrieveDirectoryAndURLPreferences($defaults);
355 // Pickup enabled-components from settings table if found.
356 $enableComponents = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components', NULL, array());
357 if (!empty($enableComponents)) {
358 $defaults['enableComponents'] = $enableComponents;
360 $components = CRM_Core_Component
::getComponents();
361 $enabledComponentIDs = array();
362 foreach ($defaults['enableComponents'] as $name) {
363 $enabledComponentIDs[] = $components[$name]->componentID
;
365 $defaults['enableComponentIDs'] = $enabledComponentIDs;
373 static function getConfigSettings() {
374 $config = CRM_Core_Config
::singleton();
376 $url = $dir = $siteName = $siteRoot = NULL;
377 if ($config->userFramework
== 'Joomla') {
379 '|administrator/components/com_civicrm/civicrm/|',
381 $config->userFrameworkResourceURL
384 // lets use imageUploadDir since we dont mess around with its values
385 // in the config object, lets kep it a bit generic since folks
386 // might have different values etc
388 //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
389 if (DIRECTORY_SEPARATOR
== '\\') {
391 '|civicrm[/\\\\]templates_c[/\\\\].*$|',
393 $config->templateCompileDir
398 '|civicrm/templates_c/.*$|',
400 $config->templateCompileDir
404 $siteRoot = preg_replace(
405 '|/media/civicrm/.*$|',
407 $config->imageUploadDir
410 else if ($config->userFramework
== 'WordPress') {
412 '|wp-content/plugins/civicrm/civicrm/|',
414 $config->userFrameworkResourceURL
417 // lets use imageUploadDir since we dont mess around with its values
418 // in the config object, lets kep it a bit generic since folks
419 // might have different values etc
421 //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
422 if (DIRECTORY_SEPARATOR
== '\\') {
424 '|civicrm[/\\\\]templates_c[/\\\\].*$|',
426 $config->templateCompileDir
431 '|civicrm/templates_c/.*$|',
433 $config->templateCompileDir
437 $siteRoot = preg_replace(
438 '|/wp-content/plugins/files/civicrm/.*$|',
440 $config->imageUploadDir
445 '|sites/[\w\.\-\_]+/modules/civicrm/|',
447 $config->userFrameworkResourceURL
450 // lets use imageUploadDir since we dont mess around with its values
451 // in the config object, lets kep it a bit generic since folks
452 // might have different values etc
454 //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
455 if (DIRECTORY_SEPARATOR
== '\\') {
457 '|[/\\\\]files[/\\\\]civicrm[/\\\\].*$|',
459 $config->imageUploadDir
464 '|/files/civicrm/.*$|',
466 $config->imageUploadDir
472 '|/sites/([\w\.\-\_]+)/|',
473 $config->imageUploadDir
,
476 $siteName = $matches[1];
478 $siteName = "/sites/$siteName/";
479 $siteNamePos = strpos($dir, $siteName);
480 if ($siteNamePos !== FALSE) {
481 $siteRoot = substr($dir, 0, $siteNamePos);
487 return array($url, $dir, $siteName, $siteRoot);
491 * Return likely default settings
492 * @return array site settings
494 * - $dir Base Directory
498 static function getBestGuessSettings() {
499 $config = CRM_Core_Config
::singleton();
501 //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
502 if (DIRECTORY_SEPARATOR
== '\\') {
503 $needle = 'civicrm[/\\\\]templates_c[/\\\\].*$';
506 $needle = 'civicrm/templates_c/.*$';
512 $config->templateCompileDir
515 list($url, $siteName, $siteRoot) = $config->userSystem
->getDefaultSiteSettings($dir);
516 return array($url, $dir, $siteName, $siteRoot);
520 * @param array $defaultValues
525 static function doSiteMove($defaultValues = array()) {
526 $moveStatus = ts('Beginning site move process...') . '<br />';
527 // get the current and guessed values
528 list($oldURL, $oldDir, $oldSiteName, $oldSiteRoot) = self
::getConfigSettings();
529 list($newURL, $newDir, $newSiteName, $newSiteRoot) = self
::getBestGuessSettings();
532 // retrieve these values from the argument list
533 $variables = array('URL', 'Dir', 'SiteName', 'SiteRoot', 'Val_1', 'Val_2', 'Val_3');
534 $states = array('old', 'new');
535 foreach ($variables as $varSuffix) {
536 foreach ($states as $state) {
537 $var = "{$state}{$varSuffix}";
539 if (isset($defaultValues[$var])) {
540 $
$var = $defaultValues[$var];
546 $
$var = CRM_Utils_Request
::retrieve($var,
548 CRM_Core_DAO
::$_nullArray,
556 $from = $to = array();
557 foreach ($variables as $varSuffix) {
558 $oldVar = "old{$varSuffix}";
559 $newVar = "new{$varSuffix}";
560 //skip it if either is empty or both are exactly the same
571 SELECT config_backend
575 $params = array(1 => array(CRM_Core_Config
::domainID(), 'Integer'));
576 $configBackend = CRM_Core_DAO
::singleValueQuery($sql, $params);
577 if (!$configBackend) {
578 CRM_Core_Error
::fatal(ts('Returning early due to unexpected error - civicrm_domain.config_backend column value is NULL. Try visiting CiviCRM Home page.'));
580 $configBackend = unserialize($configBackend);
582 $configBackend = str_replace($from,
587 $configBackend = serialize($configBackend);
589 UPDATE civicrm_domain
590 SET config_backend = %2
593 $params[2] = array($configBackend, 'String');
594 CRM_Core_DAO
::executeQuery($sql, $params);
596 // Apply the changes to civicrm_option_values
597 $optionGroups = array('url_preferences', 'directory_preferences');
598 foreach ($optionGroups as $option) {
599 foreach ($variables as $varSuffix) {
600 $oldVar = "old{$varSuffix}";
601 $newVar = "new{$varSuffix}";
606 if ($from && $to && $from != $to) {
608 UPDATE civicrm_option_value
609 SET value = REPLACE(value, %1, %2)
610 WHERE option_group_id = (
612 FROM civicrm_option_group
616 1 => array($from, 'String'),
617 2 => array($to, 'String'),
618 3 => array($option, 'String'),
620 CRM_Core_DAO
::executeQuery($sql, $params);
626 ts('Directory and Resource URLs have been updated in the moved database to reflect current site location.') .
629 $config = CRM_Core_Config
::singleton();
631 // clear the template_c and upload directory also
632 $config->cleanup(3, TRUE);
633 $moveStatus .= ts('Template cache and upload directory have been cleared.') . '<br />';
636 CRM_Core_Config
::clearDBCache();
637 $moveStatus .= ts('Database cache tables cleared.') . '<br />';
639 $resetSessionTable = CRM_Utils_Request
::retrieve('resetSessionTable',
641 CRM_Core_DAO
::$_nullArray,
646 if ($config->userSystem
->is_drupal
&&
649 db_query("DELETE FROM {sessions} WHERE 1");
650 $moveStatus .= ts('Drupal session table cleared.') . '<br />';
653 $session = CRM_Core_Session
::singleton();
655 $moveStatus .= ts('Session has been reset.') . '<br />';
662 * takes a componentName and enables it in the config
663 * Primarily used during unit testing
665 * @param string $componentName name of the component to be enabled, needs to be valid
667 * @return boolean - true if valid component name and enabling succeeds, else false
670 static function enableComponent($componentName) {
671 $config = CRM_Core_Config
::singleton();
672 if (in_array($componentName, $config->enableComponents
)) {
673 // component is already enabled
677 // return if component does not exist
678 if (!array_key_exists($componentName, CRM_Core_Component
::getComponents())) {
682 // get enabled-components from DB and add to the list
684 CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components', NULL, array());
685 $enabledComponents[] = $componentName;
687 self
::setEnabledComponents($enabledComponents);
692 static function disableComponent($componentName) {
693 $config = CRM_Core_Config
::singleton();
694 if (!in_array($componentName, $config->enableComponents
) ||
695 !array_key_exists($componentName, CRM_Core_Component
::getComponents())
697 // post-condition satisified
701 // get enabled-components from DB and add to the list
703 CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components', NULL, array());
704 $enabledComponents = array_diff($enabledComponents, array($componentName));
706 self
::setEnabledComponents($enabledComponents);
711 public static function setEnabledComponents($enabledComponents) {
712 $config = CRM_Core_Config
::singleton();
713 $components = CRM_Core_Component
::getComponents();
715 $enabledComponentIDs = array();
716 foreach ($enabledComponents as $name) {
717 $enabledComponentIDs[] = $components[$name]->componentID
;
720 // fix the config object
721 $config->enableComponents
= $enabledComponents;
722 $config->enableComponentIDs
= $enabledComponentIDs;
724 // also force reset of component array
725 CRM_Core_Component
::getEnabledComponents(TRUE);
728 CRM_Core_BAO_Setting
::setItem($enabledComponents,
729 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components');
735 static function skipVars() {
738 'templateCompileDir',
741 'userFrameworkBaseURL',
742 'userFrameworkClass',
744 'userPermissionClass',
745 'userFrameworkURLVar',
746 'userFrameworkVersion',
752 'gettextResourceDir',
754 'locale_custom_strings',
755 'localeCustomStrings',
756 'autocompleteContactSearch',
757 'autocompleteContactReference',