3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 reloads the config object
46 * @params array $params associated array of civicrm variables
51 static function create($params) {
53 $cache = CRM_Utils_Cache
::singleton();
54 $cache->delete('CRM_Core_Config');
55 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
59 * Function to add civicrm settings
61 * @params array $params associated array of civicrm variables
66 static function add(&$params) {
67 self
::fixParams($params);
69 // also set a template url so js files can use this
71 $params['civiRelativeURL'] = CRM_Utils_System
::url('CIVI_BASE_TEMPLATE');
72 $params['civiRelativeURL'] = str_replace('CIVI_BASE_TEMPLATE',
74 $params['civiRelativeURL']
77 $domain = new CRM_Core_DAO_Domain();
78 $domain->id
= CRM_Core_Config
::domainID();
80 if ($domain->config_backend
) {
81 $values = unserialize($domain->config_backend
);
82 self
::formatParams($params, $values);
86 if (isset($params['localeCustomStrings']) &&
87 is_array($params['localeCustomStrings'])
89 $domain->locale_custom_strings
= serialize($params['localeCustomStrings']);
92 // unset any of the variables we read from file that should not be stored in the database
93 // the username and certpath are stored flat with _test and _live
95 $skipVars = self
::skipVars();
96 foreach ($skipVars as $var) {
100 CRM_Core_BAO_Setting
::fixAndStoreDirAndURL($params);
102 // also skip all Dir Params, we dont need to store those in the DB!
103 foreach ($params as $name => $val) {
104 if (substr($name, -3) == 'Dir') {
105 unset($params[$name]);
109 //keep user preferred language upto date, CRM-7746
110 $session = CRM_Core_Session
::singleton();
111 $lcMessages = CRM_Utils_Array
::value('lcMessages', $params);
112 if ($lcMessages && $session->get('userID')) {
113 $languageLimit = CRM_Utils_Array
::value('languageLimit', $params);
114 if (is_array($languageLimit) &&
115 !in_array($lcMessages, array_keys($languageLimit))
117 $lcMessages = $session->get('lcMessages');
120 $ufm = new CRM_Core_DAO_UFMatch();
121 $ufm->contact_id
= $session->get('userID');
122 if ($lcMessages && $ufm->find(TRUE)) {
123 $ufm->language
= $lcMessages;
125 $session->set('lcMessages', $lcMessages);
126 $params['lcMessages'] = $lcMessages;
130 $domain->config_backend
= serialize($params);
135 * Function to fix civicrm setting variables
137 * @params array $params associated array of civicrm variables
142 static function fixParams(&$params) {
143 // in our old civicrm.settings.php we were using ISO code for country and
144 // province limit, now we have changed it to use ids
146 $countryIsoCodes = CRM_Core_PseudoConstant
::countryIsoCode();
148 $specialArray = array('countryLimit', 'provinceLimit');
150 foreach ($params as $key => $value) {
151 if (in_array($key, $specialArray) && is_array($value)) {
152 foreach ($value as $k => $val) {
153 if (!is_numeric($val)) {
154 $params[$key][$k] = array_search($val, $countryIsoCodes);
158 elseif ($key == 'defaultContactCountry') {
159 if (!is_numeric($value)) {
160 $params[$key] = array_search($value, $countryIsoCodes);
167 * Function to format the array containing before inserting in db
169 * @param array $params associated array of civicrm variables(submitted)
170 * @param array $values associated array of civicrm variables stored in db
175 static function formatParams(&$params, &$values) {
176 if (empty($params) ||
182 foreach ($params as $key => $val) {
183 if (array_key_exists($key, $values)) {
184 unset($values[$key]);
187 $params = array_merge($params, $values);
192 * Function to retrieve the settings values from db
194 * @return array $defaults
197 static function retrieve(&$defaults) {
198 $domain = new CRM_Core_DAO_Domain();
200 //we are initializing config, really can't use, CRM-7863
202 if (defined('CIVICRM_UF') && CIVICRM_UF
== 'Joomla') {
206 if (CRM_Core_Config
::isUpgradeMode()) {
207 $domain->selectAdd('config_backend');
209 elseif (CRM_Utils_Array
::value($urlVar, $_GET) == 'admin/modules/list/confirm') {
210 $domain->selectAdd('config_backend', 'locales');
213 $domain->selectAdd('config_backend, locales, locale_custom_strings');
216 $domain->id
= CRM_Core_Config
::domainID();
218 if ($domain->config_backend
) {
219 $defaults = unserialize($domain->config_backend
);
220 if ($defaults === FALSE ||
!is_array($defaults)) {
225 $skipVars = self
::skipVars();
226 foreach ($skipVars as $skip) {
227 if (array_key_exists($skip, $defaults)) {
228 unset($defaults[$skip]);
232 // since language field won't be present before upgrade.
233 if (CRM_Core_Config
::isUpgradeMode()) {
234 // dont add if its empty
235 if (!empty($defaults)) {
236 // retrieve directory and url preferences also
237 CRM_Core_BAO_Setting
::retrieveDirectoryAndURLPreferences($defaults);
242 // check if there are any locale strings
243 if ($domain->locale_custom_strings
) {
244 $defaults['localeCustomStrings'] = unserialize($domain->locale_custom_strings
);
247 $defaults['localeCustomStrings'] = NULL;
250 // are we in a multi-language setup?
251 $multiLang = $domain->locales ?
TRUE : FALSE;
253 // set the current language
256 $session = CRM_Core_Session
::singleton();
258 // on multi-lang sites based on request and civicrm_uf_match
260 $lcMessagesRequest = CRM_Utils_Request
::retrieve('lcMessages', 'String', $this);
261 $languageLimit = array();
262 if (array_key_exists('languageLimit', $defaults) && is_array($defaults['languageLimit'])) {
263 $languageLimit = $defaults['languageLimit'];
266 if (in_array($lcMessagesRequest, array_keys($languageLimit))) {
267 $lcMessages = $lcMessagesRequest;
269 //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache.
270 CRM_Core_BAO_Cache
::deleteGroup('navigation');
273 $lcMessagesRequest = NULL;
276 if (!$lcMessagesRequest) {
277 $lcMessagesSession = $session->get('lcMessages');
278 if (in_array($lcMessagesSession, array_keys($languageLimit))) {
279 $lcMessages = $lcMessagesSession;
282 $lcMessagesSession = NULL;
286 if ($lcMessagesRequest) {
287 $ufm = new CRM_Core_DAO_UFMatch();
288 $ufm->contact_id
= $session->get('userID');
289 if ($ufm->find(TRUE)) {
290 $ufm->language
= $lcMessages;
293 $session->set('lcMessages', $lcMessages);
296 if (!$lcMessages and $session->get('userID')) {
297 $ufm = new CRM_Core_DAO_UFMatch();
298 $ufm->contact_id
= $session->get('userID');
299 if ($ufm->find(TRUE) &&
300 in_array($ufm->language
, array_keys($languageLimit))
302 $lcMessages = $ufm->language
;
304 $session->set('lcMessages', $lcMessages);
309 // try to inherit the language from the hosting CMS
310 if (CRM_Utils_Array
::value('inheritLocale', $defaults)) {
311 // FIXME: On multilanguage installs, CRM_Utils_System::getUFLocale() in many cases returns nothing if $dbLocale is not set
312 $dbLocale = $multiLang ?
"_{$defaults['lcMessages']}" : '';
313 $lcMessages = CRM_Utils_System
::getUFLocale();
314 if ($domain->locales
and !in_array($lcMessages, explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
322 // update config lcMessages - CRM-5027 fixed.
323 $defaults['lcMessages'] = $lcMessages;
326 // if a single-lang site or the above didn't yield a result, use default
327 $lcMessages = CRM_Utils_Array
::value( 'lcMessages', $defaults );
330 // set suffix for table names - use views if more than one language
331 $dbLocale = $multiLang ?
"_{$lcMessages}" : '';
333 // FIXME: an ugly hack to fix CRM-4041
335 $tsLocale = $lcMessages;
337 // FIXME: as bad aplace as any to fix CRM-5428
338 // (to be moved to a sane location along with the above)
339 if (function_exists('mb_internal_encoding')) {
340 mb_internal_encoding('UTF-8');
344 // dont add if its empty
345 if (!empty($defaults)) {
346 // retrieve directory and url preferences also
347 CRM_Core_BAO_Setting
::retrieveDirectoryAndURLPreferences($defaults);
351 static function getConfigSettings() {
352 $config = CRM_Core_Config
::singleton();
354 $url = $dir = $siteName = $siteRoot = NULL;
355 if ($config->userFramework
== 'Joomla') {
356 $url = preg_replace('|administrator/components/com_civicrm/civicrm/|',
358 $config->userFrameworkResourceURL
361 // lets use imageUploadDir since we dont mess around with its values
362 // in the config object, lets kep it a bit generic since folks
363 // might have different values etc
364 $dir = preg_replace('|civicrm/templates_c/.*$|',
366 $config->templateCompileDir
368 $siteRoot = preg_replace('|/media/civicrm/.*$|',
370 $config->imageUploadDir
374 $url = preg_replace('|sites/[\w\.\-\_]+/modules/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
382 $dir = preg_replace('|/files/civicrm/.*$|',
384 $config->imageUploadDir
388 if (preg_match('|/sites/([\w\.\-\_]+)/|',
389 $config->imageUploadDir
,
392 $siteName = $matches[1];
394 $siteName = "/sites/$siteName/";
395 $siteNamePos = strpos($dir, $siteName);
396 if ($siteNamePos !== FALSE) {
397 $siteRoot = substr($dir, 0, $siteNamePos);
404 return array($url, $dir, $siteName, $siteRoot);
407 static function getBestGuessSettings() {
408 $config = CRM_Core_Config
::singleton();
410 $url = $config->userFrameworkBaseURL
;
411 $siteName = $siteRoot = NULL;
412 if ($config->userFramework
== 'Joomla') {
413 $url = preg_replace('|/administrator|',
415 $config->userFrameworkBaseURL
417 $siteRoot = preg_replace('|/media/civicrm/.*$|',
419 $config->imageUploadDir
422 $dir = preg_replace('|civicrm/templates_c/.*$|',
424 $config->templateCompileDir
427 if ($config->userFramework
!= 'Joomla') {
429 if (preg_match('|/sites/([\w\.\-\_]+)/|',
430 $config->templateCompileDir
,
433 $siteName = $matches[1];
435 $siteName = "/sites/$siteName/";
436 $siteNamePos = strpos($dir, $siteName);
437 if ($siteNamePos !== FALSE) {
438 $siteRoot = substr($dir, 0, $siteNamePos);
444 return array($url, $dir, $siteName, $siteRoot);
447 static function doSiteMove($defaultValues = array() ) {
448 $moveStatus = ts('Beginning site move process...') . '<br />';
449 // get the current and guessed values
450 list($oldURL, $oldDir, $oldSiteName, $oldSiteRoot) = self
::getConfigSettings();
451 list($newURL, $newDir, $newSiteName, $newSiteRoot) = self
::getBestGuessSettings();
454 // retrieve these values from the argument list
455 $variables = array('URL', 'Dir', 'SiteName', 'SiteRoot', 'Val_1', 'Val_2', 'Val_3');
456 $states = array('old', 'new');
457 foreach ($variables as $varSuffix) {
458 foreach ($states as $state) {
459 $var = "{$state}{$varSuffix}";
461 if (isset($defaultValues[$var])) {
462 $
$var = $defaultValues[$var];
468 $
$var = CRM_Utils_Request
::retrieve($var,
470 CRM_Core_DAO
::$_nullArray,
478 $from = $to = array();
479 foreach ($variables as $varSuffix) {
480 $oldVar = "old{$varSuffix}";
481 $newVar = "new{$varSuffix}";
482 //skip it if either is empty or both are exactly the same
493 SELECT config_backend
497 $params = array(1 => array(CRM_Core_Config
::domainID(), 'Integer'));
498 $configBackend = CRM_Core_DAO
::singleValueQuery($sql, $params);
499 if (!$configBackend) {
500 CRM_Core_Error
::fatal(ts('Returning early due to unexpected error - civicrm_domain.config_backend column value is NULL. Try visiting CiviCRM Home page.'));
502 $configBackend = unserialize($configBackend);
504 $configBackend = str_replace($from,
509 $configBackend = serialize($configBackend);
511 UPDATE civicrm_domain
512 SET config_backend = %2
515 $params[2] = array($configBackend, 'String');
516 CRM_Core_DAO
::executeQuery($sql, $params);
518 // Apply the changes to civicrm_option_values
519 $optionGroups = array('url_preferences', 'directory_preferences');
520 foreach ($optionGroups as $option) {
521 foreach ($variables as $varSuffix) {
522 $oldVar = "old{$varSuffix}";
523 $newVar = "new{$varSuffix}";
528 if ($from && $to && $from != $to) {
530 UPDATE civicrm_option_value
531 SET value = REPLACE(value, %1, %2)
532 WHERE option_group_id = (
534 FROM civicrm_option_group
537 $params = array(1 => array($from, 'String'),
538 2 => array($to, 'String'),
539 3 => array($option, 'String'),
541 CRM_Core_DAO
::executeQuery($sql, $params);
546 $moveStatus .= ts('Directory and Resource URLs have been updated in the moved database to reflect current site location.') . '<br />';
548 $config = CRM_Core_Config
::singleton();
550 // clear the template_c and upload directory also
551 $config->cleanup(3, TRUE);
552 $moveStatus .= ts('Template cache and upload directory have been cleared.') . '<br />';
555 CRM_Core_Config
::clearDBCache();
556 $moveStatus .= ts('Database cache tables cleared.') . '<br />';
558 $resetSessionTable = CRM_Utils_Request
::retrieve('resetSessionTable',
560 CRM_Core_DAO
::$_nullArray,
565 if ($config->userSystem
->is_drupal
&&
568 db_query("DELETE FROM {sessions} WHERE 1");
569 $moveStatus .= ts('Drupal session table cleared.') . '<br />';
572 $session = CRM_Core_Session
::singleton();
574 $moveStatus .= ts('Session has been reset.') . '<br />';
581 * takes a componentName and enables it in the config
582 * Primarily used during unit testing
584 * @param string $componentName name of the component to be enabled, needs to be valid
586 * @return boolean - true if valid component name and enabling succeeds, else false
589 static function enableComponent($componentName) {
590 $config = CRM_Core_Config
::singleton();
591 if (in_array($componentName, $config->enableComponents
)) {
592 // component is already enabled
595 $components = CRM_Core_Component
::getComponents();
597 // return if component does not exist
598 if (!array_key_exists($componentName, $components)) {
602 // get config_backend value
604 SELECT config_backend
608 $params = array(1 => array(CRM_Core_Config
::domainID(), 'Integer'));
609 $configBackend = CRM_Core_DAO
::singleValueQuery($sql, $params);
611 if (!$configBackend) {
612 static $alreadyVisited = FALSE;
613 if ($alreadyVisited) {
614 CRM_Core_Error
::fatal(ts('Returning early due to unexpected error - civicrm_domain.config_backend column value is NULL. Try visiting CiviCRM Home page.'));
617 $alreadyVisited = TRUE;
619 // try to recreate the config backend
620 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
621 return self
::enableComponent($componentName);
623 $configBackend = unserialize($configBackend);
625 $configBackend['enableComponents'][] = $componentName;
626 $configBackend['enableComponentIDs'][] = $components[$componentName]->componentID
;
628 // fix the config object
629 $config->enableComponents
= $configBackend['enableComponents'];
630 $config->enableComponentIDs
= $configBackend['enableComponentIDs'];
632 // also force reset of component array
633 CRM_Core_Component
::getEnabledComponents(TRUE);
635 // check if component is already there, is so return
636 $configBackend = serialize($configBackend);
638 UPDATE civicrm_domain
639 SET config_backend = %2
642 $params[2] = array($configBackend, 'String');
643 CRM_Core_DAO
::executeQuery($sql, $params);
648 static function skipVars() {
650 'dsn', 'templateCompileDir',
653 'userFrameworkBaseURL', 'userFrameworkClass', 'userHookClass',
654 'userPermissionClass', 'userFrameworkURLVar', 'userFrameworkVersion',
655 'newBaseURL', 'newBaseDir', 'newSiteName', 'configAndLogDir',
656 'qfKey', 'gettextResourceDir', 'cleanURL',
657 'locale_custom_strings', 'localeCustomStrings',
658 'autocompleteContactSearch',
659 'autocompleteContactReference',