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 * Create civicrm settings. This is the same as add but it clears the cache and
45 * reloads the config object
47 * @param array $params associated array of civicrm variables
52 static function create($params) {
54 $cache = CRM_Utils_Cache
::singleton();
55 $cache->delete('CRM_Core_Config');
56 $cache->delete('CRM_Core_Config' . CRM_Core_Config
::domainID());
57 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
61 * Add civicrm settings
63 * @param array $params associated array of civicrm variables
68 static function add(&$params) {
69 self
::fixParams($params);
71 // also set a template url so js files can use this
73 $params['civiRelativeURL'] = CRM_Utils_System
::url('CIVI_BASE_TEMPLATE');
74 $params['civiRelativeURL'] =
78 $params['civiRelativeURL']
81 // also add the version number for use by template / js etc
82 $params['civiVersion'] = CRM_Utils_System
::version();
84 $domain = new CRM_Core_DAO_Domain();
85 $domain->id
= CRM_Core_Config
::domainID();
87 if ($domain->config_backend
) {
88 $values = unserialize($domain->config_backend
);
89 self
::formatParams($params, $values);
93 if (isset($params['localeCustomStrings']) &&
94 is_array($params['localeCustomStrings'])
96 $domain->locale_custom_strings
= serialize($params['localeCustomStrings']);
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
102 $skipVars = self
::skipVars();
103 foreach ($skipVars as $var) {
104 unset($params[$var]);
107 CRM_Core_BAO_Setting
::fixAndStoreDirAndURL($params);
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]);
116 //keep user preferred language upto date, CRM-7746
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))
124 $lcMessages = $session->get('lcMessages');
127 $ufm = new CRM_Core_DAO_UFMatch();
128 $ufm->contact_id
= $session->get('userID');
129 if ($lcMessages && $ufm->find(TRUE)) {
130 $ufm->language
= $lcMessages;
132 $session->set('lcMessages', $lcMessages);
133 $params['lcMessages'] = $lcMessages;
137 $domain->config_backend
= serialize($params);
142 * Fix civicrm setting variables
144 * @param array $params associated array of civicrm variables
149 static function fixParams(&$params) {
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
153 $countryIsoCodes = CRM_Core_PseudoConstant
::countryIsoCode();
155 $specialArray = array('countryLimit', 'provinceLimit');
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);
165 elseif ($key == 'defaultContactCountry') {
166 if (!is_numeric($value)) {
167 $params[$key] = array_search($value, $countryIsoCodes);
174 * Format the array containing before inserting in db
176 * @param array $params associated array of civicrm variables(submitted)
177 * @param array $values associated array of civicrm variables stored in db
182 static function formatParams(&$params, &$values) {
183 if (empty($params) ||
189 foreach ($params as $key => $val) {
190 if (array_key_exists($key, $values)) {
191 unset($values[$key]);
194 $params = array_merge($params, $values);
199 * Retrieve the settings values from db
203 * @return array $defaults
206 static function retrieve(&$defaults) {
207 $domain = new CRM_Core_DAO_Domain();
209 //we are initializing config, really can't use, CRM-7863
211 if (defined('CIVICRM_UF') && CIVICRM_UF
== 'Joomla') {
215 if (CRM_Core_Config
::isUpgradeMode()) {
216 $domain->selectAdd('config_backend');
218 elseif (CRM_Utils_Array
::value($urlVar, $_GET) == 'admin/modules/list/confirm') {
219 $domain->selectAdd('config_backend', 'locales');
222 $domain->selectAdd('config_backend, locales, locale_custom_strings');
225 $domain->id
= CRM_Core_Config
::domainID();
227 if ($domain->config_backend
) {
228 $defaults = unserialize($domain->config_backend
);
229 if ($defaults === FALSE ||
!is_array($defaults)) {
234 $skipVars = self
::skipVars();
235 foreach ($skipVars as $skip) {
236 if (array_key_exists($skip, $defaults)) {
237 unset($defaults[$skip]);
241 // check if there are any locale strings
242 if ($domain->locale_custom_strings
) {
243 $defaults['localeCustomStrings'] = unserialize($domain->locale_custom_strings
);
246 $defaults['localeCustomStrings'] = NULL;
249 // are we in a multi-language setup?
250 $multiLang = $domain->locales ?
TRUE : FALSE;
252 // set the current language
255 $session = CRM_Core_Session
::singleton();
257 // on multi-lang sites based on request and civicrm_uf_match
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'];
265 if (in_array($lcMessagesRequest, array_keys($languageLimit))) {
266 $lcMessages = $lcMessagesRequest;
268 //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache.
269 CRM_Core_BAO_Cache
::deleteGroup('navigation');
272 $lcMessagesRequest = NULL;
275 if (!$lcMessagesRequest) {
276 $lcMessagesSession = $session->get('lcMessages');
277 if (in_array($lcMessagesSession, array_keys($languageLimit))) {
278 $lcMessages = $lcMessagesSession;
281 $lcMessagesSession = NULL;
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;
292 $session->set('lcMessages', $lcMessages);
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))
301 $lcMessages = $ufm->language
;
303 $session->set('lcMessages', $lcMessages);
308 // try to inherit the language from the hosting CMS
309 if (!empty($defaults['inheritLocale'])) {
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
,
321 if (empty($lcMessages)) {
322 // if a single-lang site, use default
324 $lcMessages = CRM_Utils_Array
::value('lcMessages', $defaults);
327 // set suffix for table names - use views if more than one language
328 $dbLocale = $multiLang ?
"_{$lcMessages}" : '';
330 // FIXME: an ugly hack to fix CRM-4041
332 $tsLocale = $lcMessages;
334 // FIXME: as bad aplace as any to fix CRM-5428
335 // (to be moved to a sane location along with the above)
336 if (function_exists('mb_internal_encoding')) {
337 mb_internal_encoding('UTF-8');
341 // dont add if its empty
342 if (!empty($defaults)) {
343 // retrieve directory and url preferences also
344 CRM_Core_BAO_Setting
::retrieveDirectoryAndURLPreferences($defaults);
346 // Pickup enabled-components from settings table if found.
347 $enableComponents = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components', NULL, array());
348 if (!empty($enableComponents)) {
349 $defaults['enableComponents'] = $enableComponents;
351 $components = CRM_Core_Component
::getComponents();
352 $enabledComponentIDs = array();
353 foreach ($defaults['enableComponents'] as $name) {
354 $enabledComponentIDs[] = $components[$name]->componentID
;
356 $defaults['enableComponentIDs'] = $enabledComponentIDs;
364 static function getConfigSettings() {
365 $config = CRM_Core_Config
::singleton();
367 $url = $dir = $siteName = $siteRoot = NULL;
368 if ($config->userFramework
== 'Joomla') {
370 '|administrator/components/com_civicrm/civicrm/|',
372 $config->userFrameworkResourceURL
375 // lets use imageUploadDir since we dont mess around with its values
376 // in the config object, lets kep it a bit generic since folks
377 // might have different values etc
379 //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
380 if (DIRECTORY_SEPARATOR
== '\\') {
382 '|civicrm[/\\\\]templates_c[/\\\\].*$|',
384 $config->templateCompileDir
389 '|civicrm/templates_c/.*$|',
391 $config->templateCompileDir
395 $siteRoot = preg_replace(
396 '|/media/civicrm/.*$|',
398 $config->imageUploadDir
401 else if ($config->userFramework
== 'WordPress') {
403 '|wp-content/plugins/civicrm/civicrm/|',
405 $config->userFrameworkResourceURL
408 // lets use imageUploadDir since we dont mess around with its values
409 // in the config object, lets kep it a bit generic since folks
410 // might have different values etc
412 //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
413 if (DIRECTORY_SEPARATOR
== '\\') {
415 '|civicrm[/\\\\]templates_c[/\\\\].*$|',
417 $config->templateCompileDir
422 '|civicrm/templates_c/.*$|',
424 $config->templateCompileDir
428 $siteRoot = preg_replace(
429 '|/wp-content/plugins/files/civicrm/.*$|',
431 $config->imageUploadDir
436 '|sites/[\w\.\-\_]+/modules/civicrm/|',
438 $config->userFrameworkResourceURL
441 // lets use imageUploadDir since we dont mess around with its values
442 // in the config object, lets kep it a bit generic since folks
443 // might have different values etc
445 //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
446 if (DIRECTORY_SEPARATOR
== '\\') {
448 '|[/\\\\]files[/\\\\]civicrm[/\\\\].*$|',
450 $config->imageUploadDir
455 '|/files/civicrm/.*$|',
457 $config->imageUploadDir
463 '|/sites/([\w\.\-\_]+)/|',
464 $config->imageUploadDir
,
467 $siteName = $matches[1];
469 $siteName = "/sites/$siteName/";
470 $siteNamePos = strpos($dir, $siteName);
471 if ($siteNamePos !== FALSE) {
472 $siteRoot = substr($dir, 0, $siteNamePos);
478 return array($url, $dir, $siteName, $siteRoot);
482 * Return likely default settings
483 * @return array site settings
485 * - $dir Base Directory
489 static function getBestGuessSettings() {
490 $config = CRM_Core_Config
::singleton();
492 //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
493 if (DIRECTORY_SEPARATOR
== '\\') {
494 $needle = 'civicrm[/\\\\]templates_c[/\\\\].*$';
497 $needle = 'civicrm/templates_c/.*$';
503 $config->templateCompileDir
506 list($url, $siteName, $siteRoot) = $config->userSystem
->getDefaultSiteSettings($dir);
507 return array($url, $dir, $siteName, $siteRoot);
511 * @param array $defaultValues
516 static function doSiteMove($defaultValues = array()) {
517 $moveStatus = ts('Beginning site move process...') . '<br />';
518 // get the current and guessed values
519 list($oldURL, $oldDir, $oldSiteName, $oldSiteRoot) = self
::getConfigSettings();
520 list($newURL, $newDir, $newSiteName, $newSiteRoot) = self
::getBestGuessSettings();
523 // retrieve these values from the argument list
524 $variables = array('URL', 'Dir', 'SiteName', 'SiteRoot', 'Val_1', 'Val_2', 'Val_3');
525 $states = array('old', 'new');
526 foreach ($variables as $varSuffix) {
527 foreach ($states as $state) {
528 $var = "{$state}{$varSuffix}";
530 if (isset($defaultValues[$var])) {
531 $
$var = $defaultValues[$var];
537 $
$var = CRM_Utils_Request
::retrieve($var,
539 CRM_Core_DAO
::$_nullArray,
547 $from = $to = array();
548 foreach ($variables as $varSuffix) {
549 $oldVar = "old{$varSuffix}";
550 $newVar = "new{$varSuffix}";
551 //skip it if either is empty or both are exactly the same
562 SELECT config_backend
566 $params = array(1 => array(CRM_Core_Config
::domainID(), 'Integer'));
567 $configBackend = CRM_Core_DAO
::singleValueQuery($sql, $params);
568 if (!$configBackend) {
569 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 $configBackend = unserialize($configBackend);
573 $configBackend = str_replace($from,
578 $configBackend = serialize($configBackend);
580 UPDATE civicrm_domain
581 SET config_backend = %2
584 $params[2] = array($configBackend, 'String');
585 CRM_Core_DAO
::executeQuery($sql, $params);
587 // Apply the changes to civicrm_option_values
588 $optionGroups = array('url_preferences', 'directory_preferences');
589 foreach ($optionGroups as $option) {
590 foreach ($variables as $varSuffix) {
591 $oldVar = "old{$varSuffix}";
592 $newVar = "new{$varSuffix}";
597 if ($from && $to && $from != $to) {
599 UPDATE civicrm_option_value
600 SET value = REPLACE(value, %1, %2)
601 WHERE option_group_id = (
603 FROM civicrm_option_group
607 1 => array($from, 'String'),
608 2 => array($to, 'String'),
609 3 => array($option, 'String'),
611 CRM_Core_DAO
::executeQuery($sql, $params);
617 ts('Directory and Resource URLs have been updated in the moved database to reflect current site location.') .
620 $config = CRM_Core_Config
::singleton();
622 // clear the template_c and upload directory also
623 $config->cleanup(3, TRUE);
624 $moveStatus .= ts('Template cache and upload directory have been cleared.') . '<br />';
627 CRM_Core_Config
::clearDBCache();
628 $moveStatus .= ts('Database cache tables cleared.') . '<br />';
630 $resetSessionTable = CRM_Utils_Request
::retrieve('resetSessionTable',
632 CRM_Core_DAO
::$_nullArray,
637 if ($config->userSystem
->is_drupal
&&
640 db_query("DELETE FROM {sessions} WHERE 1");
641 $moveStatus .= ts('Drupal session table cleared.') . '<br />';
644 $session = CRM_Core_Session
::singleton();
646 $moveStatus .= ts('Session has been reset.') . '<br />';
653 * Takes a componentName and enables it in the config
654 * Primarily used during unit testing
656 * @param string $componentName name of the component to be enabled, needs to be valid
658 * @return boolean - true if valid component name and enabling succeeds, else false
661 static function enableComponent($componentName) {
662 $config = CRM_Core_Config
::singleton();
663 if (in_array($componentName, $config->enableComponents
)) {
664 // component is already enabled
668 // return if component does not exist
669 if (!array_key_exists($componentName, CRM_Core_Component
::getComponents())) {
673 // get enabled-components from DB and add to the list
675 CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components', NULL, array());
676 $enabledComponents[] = $componentName;
678 self
::setEnabledComponents($enabledComponents);
683 static function disableComponent($componentName) {
684 $config = CRM_Core_Config
::singleton();
685 if (!in_array($componentName, $config->enableComponents
) ||
686 !array_key_exists($componentName, CRM_Core_Component
::getComponents())
688 // post-condition satisified
692 // get enabled-components from DB and add to the list
694 CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components', NULL, array());
695 $enabledComponents = array_diff($enabledComponents, array($componentName));
697 self
::setEnabledComponents($enabledComponents);
702 public static function setEnabledComponents($enabledComponents) {
703 $config = CRM_Core_Config
::singleton();
704 $components = CRM_Core_Component
::getComponents();
706 $enabledComponentIDs = array();
707 foreach ($enabledComponents as $name) {
708 $enabledComponentIDs[] = $components[$name]->componentID
;
711 // fix the config object
712 $config->enableComponents
= $enabledComponents;
713 $config->enableComponentIDs
= $enabledComponentIDs;
715 // also force reset of component array
716 CRM_Core_Component
::getEnabledComponents(TRUE);
719 CRM_Core_BAO_Setting
::setItem($enabledComponents,
720 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_components');
726 static function skipVars() {
729 'templateCompileDir',
732 'userFrameworkBaseURL',
733 'userFrameworkClass',
735 'userPermissionClass',
736 'userFrameworkURLVar',
737 'userFrameworkVersion',
743 'gettextResourceDir',
745 'locale_custom_strings',
746 'localeCustomStrings',
747 'autocompleteContactSearch',
748 'autocompleteContactReference',