Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
17 | */ |
18 | ||
19 | /** | |
192d36c5 | 20 | * File contains functions used in civicrm configuration. |
6a488035 TO |
21 | */ |
22 | class CRM_Core_BAO_ConfigSetting { | |
23 | ||
24 | /** | |
100fef9d | 25 | * Create civicrm settings. This is the same as add but it clears the cache and |
b597d0b1 | 26 | * reloads the config object |
6a488035 | 27 | * |
6a0b768e TO |
28 | * @param array $params |
29 | * Associated array of civicrm variables. | |
6a488035 | 30 | */ |
00be9182 | 31 | public static function create($params) { |
6a488035 TO |
32 | self::add($params); |
33 | $cache = CRM_Utils_Cache::singleton(); | |
34 | $cache->delete('CRM_Core_Config'); | |
0e04f44e | 35 | $cache->delete('CRM_Core_Config' . CRM_Core_Config::domainID()); |
6a488035 TO |
36 | $config = CRM_Core_Config::singleton(TRUE, TRUE); |
37 | } | |
38 | ||
39 | /** | |
fe482240 | 40 | * Add civicrm settings. |
6a488035 | 41 | * |
6a0b768e TO |
42 | * @param array $params |
43 | * Associated array of civicrm variables. | |
6a488035 | 44 | */ |
00be9182 | 45 | public static function add(&$params) { |
6a488035 TO |
46 | $domain = new CRM_Core_DAO_Domain(); |
47 | $domain->id = CRM_Core_Config::domainID(); | |
48 | $domain->find(TRUE); | |
49 | if ($domain->config_backend) { | |
6e5b5e59 | 50 | $params = array_merge(unserialize($domain->config_backend), $params); |
6a488035 TO |
51 | } |
52 | ||
7e0c769c | 53 | $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params); |
6a488035 | 54 | |
6a488035 TO |
55 | // also skip all Dir Params, we dont need to store those in the DB! |
56 | foreach ($params as $name => $val) { | |
57 | if (substr($name, -3) == 'Dir') { | |
58 | unset($params[$name]); | |
59 | } | |
60 | } | |
61 | ||
6a488035 TO |
62 | $domain->config_backend = serialize($params); |
63 | $domain->save(); | |
64 | } | |
65 | ||
6a488035 | 66 | /** |
fe482240 | 67 | * Retrieve the settings values from db. |
6a488035 | 68 | * |
da6b46f4 EM |
69 | * @param $defaults |
70 | * | |
a6c01b45 | 71 | * @return array |
6a488035 | 72 | */ |
00be9182 | 73 | public static function retrieve(&$defaults) { |
6a488035 | 74 | $domain = new CRM_Core_DAO_Domain(); |
747859fe | 75 | $isUpgrade = CRM_Core_Config::isUpgradeMode(); |
6a488035 TO |
76 | |
77 | //we are initializing config, really can't use, CRM-7863 | |
78 | $urlVar = 'q'; | |
79 | if (defined('CIVICRM_UF') && CIVICRM_UF == 'Joomla') { | |
80 | $urlVar = 'task'; | |
81 | } | |
82 | ||
eed7e803 | 83 | if ($isUpgrade && CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_domain', 'config_backend')) { |
6a488035 TO |
84 | $domain->selectAdd('config_backend'); |
85 | } | |
8ef8eacf | 86 | else { |
87 | $domain->selectAdd('locales'); | |
88 | } | |
6a488035 TO |
89 | |
90 | $domain->id = CRM_Core_Config::domainID(); | |
91 | $domain->find(TRUE); | |
92 | if ($domain->config_backend) { | |
93 | $defaults = unserialize($domain->config_backend); | |
94 | if ($defaults === FALSE || !is_array($defaults)) { | |
be2fb01f | 95 | $defaults = []; |
608e6658 | 96 | return FALSE; |
6a488035 TO |
97 | } |
98 | ||
99 | $skipVars = self::skipVars(); | |
100 | foreach ($skipVars as $skip) { | |
101 | if (array_key_exists($skip, $defaults)) { | |
102 | unset($defaults[$skip]); | |
103 | } | |
104 | } | |
8ef8eacf | 105 | } |
106 | if (!$isUpgrade) { | |
107 | CRM_Core_BAO_ConfigSetting::applyLocale(Civi::settings($domain->id), $domain->locales); | |
b72b5fc0 TO |
108 | } |
109 | } | |
6a488035 | 110 | |
b72b5fc0 TO |
111 | /** |
112 | * Evaluate locale preferences and activate a chosen locale by | |
113 | * updating session+global variables. | |
114 | * | |
115 | * @param \Civi\Core\SettingsBag $settings | |
116 | * @param string $activatedLocales | |
117 | * Imploded list of locales which are supported in the DB. | |
b72b5fc0 TO |
118 | */ |
119 | public static function applyLocale($settings, $activatedLocales) { | |
120 | // are we in a multi-language setup? | |
121 | $multiLang = $activatedLocales ? TRUE : FALSE; | |
6a488035 | 122 | |
b72b5fc0 TO |
123 | // set the current language |
124 | $chosenLocale = NULL; | |
6a488035 | 125 | |
b72b5fc0 | 126 | $session = CRM_Core_Session::singleton(); |
6a488035 | 127 | |
921ed8ae AS |
128 | $permittedLanguages = CRM_Core_I18n::uiLanguages(TRUE); |
129 | ||
130 | // The locale to be used can come from various places: | |
131 | // - the request (url) | |
132 | // - the session | |
133 | // - civicrm_uf_match | |
134 | // - inherited from the CMS | |
135 | // Only look at this if there is actually a choice of permitted languages | |
136 | if (count($permittedLanguages) >= 2) { | |
b72b5fc0 | 137 | $requestLocale = CRM_Utils_Request::retrieve('lcMessages', 'String'); |
921ed8ae | 138 | if (in_array($requestLocale, $permittedLanguages)) { |
b72b5fc0 | 139 | $chosenLocale = $requestLocale; |
6a488035 | 140 | |
b72b5fc0 TO |
141 | //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache. |
142 | // Ed: This doesn't sound good. | |
96689db3 | 143 | // Civi::cache('navigation')->flush(); |
b72b5fc0 TO |
144 | } |
145 | else { | |
146 | $requestLocale = NULL; | |
147 | } | |
6a488035 | 148 | |
b72b5fc0 TO |
149 | if (!$requestLocale) { |
150 | $sessionLocale = $session->get('lcMessages'); | |
921ed8ae | 151 | if (in_array($sessionLocale, $permittedLanguages)) { |
b72b5fc0 TO |
152 | $chosenLocale = $sessionLocale; |
153 | } | |
154 | else { | |
155 | $sessionLocale = NULL; | |
6a488035 | 156 | } |
b72b5fc0 | 157 | } |
6a488035 | 158 | |
b72b5fc0 TO |
159 | if ($requestLocale) { |
160 | $ufm = new CRM_Core_DAO_UFMatch(); | |
161 | $ufm->contact_id = $session->get('userID'); | |
162 | if ($ufm->find(TRUE)) { | |
163 | $ufm->language = $chosenLocale; | |
164 | $ufm->save(); | |
6a488035 | 165 | } |
b72b5fc0 | 166 | $session->set('lcMessages', $chosenLocale); |
6a488035 | 167 | } |
b72b5fc0 TO |
168 | |
169 | if (!$chosenLocale and $session->get('userID')) { | |
170 | $ufm = new CRM_Core_DAO_UFMatch(); | |
171 | $ufm->contact_id = $session->get('userID'); | |
172 | if ($ufm->find(TRUE) && | |
921ed8ae | 173 | in_array($ufm->language, $permittedLanguages) |
4c235182 | 174 | ) { |
b72b5fc0 | 175 | $chosenLocale = $ufm->language; |
6a488035 | 176 | } |
b72b5fc0 | 177 | $session->set('lcMessages', $chosenLocale); |
6a488035 | 178 | } |
b72b5fc0 TO |
179 | } |
180 | global $dbLocale; | |
181 | ||
182 | // try to inherit the language from the hosting CMS | |
183 | if ($settings->get('inheritLocale')) { | |
184 | // FIXME: On multilanguage installs, CRM_Utils_System::getUFLocale() in many cases returns nothing if $dbLocale is not set | |
5da9acde CG |
185 | $lcMessages = $settings->get('lcMessages'); |
186 | $dbLocale = $multiLang && $lcMessages ? "_{$lcMessages}" : ''; | |
b72b5fc0 TO |
187 | $chosenLocale = CRM_Utils_System::getUFLocale(); |
188 | if ($activatedLocales and !in_array($chosenLocale, explode(CRM_Core_DAO::VALUE_SEPARATOR, $activatedLocales))) { | |
189 | $chosenLocale = NULL; | |
5b6ed484 | 190 | } |
b72b5fc0 | 191 | } |
6a488035 | 192 | |
b72b5fc0 TO |
193 | if (empty($chosenLocale)) { |
194 | //CRM-11993 - if a single-lang site, use default | |
195 | $chosenLocale = $settings->get('lcMessages'); | |
196 | } | |
6a488035 | 197 | |
b72b5fc0 | 198 | // set suffix for table names - use views if more than one language |
5da9acde | 199 | $dbLocale = $multiLang && $chosenLocale ? "_{$chosenLocale}" : ''; |
6a488035 | 200 | |
b72b5fc0 TO |
201 | // FIXME: an ugly hack to fix CRM-4041 |
202 | global $tsLocale; | |
203 | $tsLocale = $chosenLocale; | |
204 | ||
205 | // FIXME: as bad aplace as any to fix CRM-5428 | |
206 | // (to be moved to a sane location along with the above) | |
207 | if (function_exists('mb_internal_encoding')) { | |
208 | mb_internal_encoding('UTF-8'); | |
6a488035 | 209 | } |
6a488035 TO |
210 | } |
211 | ||
b5c2afd0 EM |
212 | /** |
213 | * @param array $defaultValues | |
214 | * | |
215 | * @return string | |
216 | * @throws Exception | |
217 | */ | |
be2fb01f | 218 | public static function doSiteMove($defaultValues = []) { |
6a488035 | 219 | $moveStatus = ts('Beginning site move process...') . '<br />'; |
4f240ac1 TO |
220 | $settings = Civi::settings(); |
221 | ||
222 | foreach (array_merge(self::getPathSettings(), self::getUrlSettings()) as $key) { | |
223 | $value = $settings->get($key); | |
224 | if ($value && $value != $settings->getDefault($key)) { | |
225 | if ($settings->getMandatory($key) === NULL) { | |
226 | $settings->revert($key); | |
be2fb01f | 227 | $moveStatus .= ts("WARNING: The setting (%1) has been reverted.", [ |
4f240ac1 | 228 | 1 => $key, |
be2fb01f | 229 | ]); |
4f240ac1 | 230 | $moveStatus .= '<br />'; |
6a488035 | 231 | } |
4f240ac1 | 232 | else { |
be2fb01f | 233 | $moveStatus .= ts("WARNING: The setting (%1) is overridden and could not be reverted.", [ |
4f240ac1 | 234 | 1 => $key, |
be2fb01f | 235 | ]); |
4f240ac1 | 236 | $moveStatus .= '<br />'; |
6a488035 TO |
237 | } |
238 | } | |
239 | } | |
240 | ||
6a488035 TO |
241 | $config = CRM_Core_Config::singleton(); |
242 | ||
243 | // clear the template_c and upload directory also | |
244 | $config->cleanup(3, TRUE); | |
245 | $moveStatus .= ts('Template cache and upload directory have been cleared.') . '<br />'; | |
246 | ||
247 | // clear all caches | |
248 | CRM_Core_Config::clearDBCache(); | |
0a12cd4a | 249 | Civi::cache('session')->clear(); |
6a488035 TO |
250 | $moveStatus .= ts('Database cache tables cleared.') . '<br />'; |
251 | ||
252 | $resetSessionTable = CRM_Utils_Request::retrieve('resetSessionTable', | |
253 | 'Boolean', | |
254 | CRM_Core_DAO::$_nullArray, | |
255 | FALSE, | |
256 | FALSE, | |
257 | 'REQUEST' | |
258 | ); | |
259 | if ($config->userSystem->is_drupal && | |
260 | $resetSessionTable | |
261 | ) { | |
262 | db_query("DELETE FROM {sessions} WHERE 1"); | |
263 | $moveStatus .= ts('Drupal session table cleared.') . '<br />'; | |
264 | } | |
265 | else { | |
266 | $session = CRM_Core_Session::singleton(); | |
267 | $session->reset(2); | |
268 | $moveStatus .= ts('Session has been reset.') . '<br />'; | |
269 | } | |
270 | ||
271 | return $moveStatus; | |
272 | } | |
273 | ||
274 | /** | |
fe482240 | 275 | * Takes a componentName and enables it in the config. |
6a488035 TO |
276 | * Primarily used during unit testing |
277 | * | |
6a0b768e TO |
278 | * @param string $componentName |
279 | * Name of the component to be enabled, needs to be valid. | |
6a488035 | 280 | * |
608e6658 | 281 | * @return bool |
a6c01b45 | 282 | * true if valid component name and enabling succeeds, else false |
6a488035 | 283 | */ |
00be9182 | 284 | public static function enableComponent($componentName) { |
6a488035 TO |
285 | $config = CRM_Core_Config::singleton(); |
286 | if (in_array($componentName, $config->enableComponents)) { | |
287 | // component is already enabled | |
288 | return TRUE; | |
289 | } | |
6a488035 TO |
290 | |
291 | // return if component does not exist | |
b086633a | 292 | if (!array_key_exists($componentName, CRM_Core_Component::getComponents())) { |
6a488035 TO |
293 | return FALSE; |
294 | } | |
295 | ||
3124edb3 | 296 | // get enabled-components from DB and add to the list |
84fb7424 | 297 | $enabledComponents = Civi::settings()->get('enable_components'); |
3124edb3 | 298 | $enabledComponents[] = $componentName; |
6a488035 | 299 | |
b086633a TO |
300 | self::setEnabledComponents($enabledComponents); |
301 | ||
302 | return TRUE; | |
303 | } | |
304 | ||
b896fa44 EM |
305 | /** |
306 | * Disable specified component. | |
307 | * | |
308 | * @param string $componentName | |
309 | * | |
310 | * @return bool | |
311 | */ | |
00be9182 | 312 | public static function disableComponent($componentName) { |
b086633a | 313 | $config = CRM_Core_Config::singleton(); |
4c235182 EM |
314 | if (!in_array($componentName, $config->enableComponents) || |
315 | !array_key_exists($componentName, CRM_Core_Component::getComponents()) | |
316 | ) { | |
b896fa44 | 317 | // Post-condition is satisfied. |
b086633a TO |
318 | return TRUE; |
319 | } | |
320 | ||
321 | // get enabled-components from DB and add to the list | |
84fb7424 | 322 | $enabledComponents = Civi::settings()->get('enable_components'); |
be2fb01f | 323 | $enabledComponents = array_diff($enabledComponents, [$componentName]); |
b086633a TO |
324 | |
325 | self::setEnabledComponents($enabledComponents); | |
326 | ||
327 | return TRUE; | |
328 | } | |
329 | ||
b896fa44 EM |
330 | /** |
331 | * Set enabled components. | |
332 | * | |
333 | * @param array $enabledComponents | |
334 | */ | |
b086633a | 335 | public static function setEnabledComponents($enabledComponents) { |
edbcbd96 TO |
336 | // fix the config object. update db. |
337 | Civi::settings()->set('enable_components', $enabledComponents); | |
6a488035 TO |
338 | |
339 | // also force reset of component array | |
340 | CRM_Core_Component::getEnabledComponents(TRUE); | |
6a488035 TO |
341 | } |
342 | ||
b5c2afd0 EM |
343 | /** |
344 | * @return array | |
345 | */ | |
00be9182 | 346 | public static function skipVars() { |
be2fb01f | 347 | return [ |
4c235182 EM |
348 | 'dsn', |
349 | 'templateCompileDir', | |
6a488035 TO |
350 | 'userFrameworkDSN', |
351 | 'userFramework', | |
4c235182 EM |
352 | 'userFrameworkBaseURL', |
353 | 'userFrameworkClass', | |
354 | 'userHookClass', | |
355 | 'userPermissionClass', | |
59735506 | 356 | 'userPermissionTemp', |
4c235182 EM |
357 | 'userFrameworkURLVar', |
358 | 'userFrameworkVersion', | |
359 | 'newBaseURL', | |
360 | 'newBaseDir', | |
361 | 'newSiteName', | |
362 | 'configAndLogDir', | |
363 | 'qfKey', | |
364 | 'gettextResourceDir', | |
365 | 'cleanURL', | |
7e0c769c | 366 | 'entryURL', |
4c235182 EM |
367 | 'locale_custom_strings', |
368 | 'localeCustomStrings', | |
6a488035 TO |
369 | 'autocompleteContactSearch', |
370 | 'autocompleteContactReference', | |
371 | 'checksumTimeout', | |
92a8de72 | 372 | 'checksum_timeout', |
be2fb01f | 373 | ]; |
6a488035 | 374 | } |
96025800 | 375 | |
7e0c769c TO |
376 | /** |
377 | * @param array $params | |
378 | * @return array | |
379 | */ | |
380 | public static function filterSkipVars($params) { | |
381 | $skipVars = self::skipVars(); | |
382 | foreach ($skipVars as $var) { | |
383 | unset($params[$var]); | |
384 | } | |
385 | foreach (array_keys($params) as $key) { | |
386 | if (preg_match('/^_qf_/', $key)) { | |
387 | unset($params[$key]); | |
388 | } | |
389 | } | |
390 | return $params; | |
391 | } | |
392 | ||
4f240ac1 TO |
393 | /** |
394 | * @return array | |
395 | */ | |
396 | private static function getUrlSettings() { | |
be2fb01f | 397 | return [ |
4f240ac1 TO |
398 | 'userFrameworkResourceURL', |
399 | 'imageUploadURL', | |
400 | 'customCSSURL', | |
401 | 'extensionsURL', | |
be2fb01f | 402 | ]; |
4f240ac1 TO |
403 | } |
404 | ||
405 | /** | |
406 | * @return array | |
407 | */ | |
408 | private static function getPathSettings() { | |
be2fb01f | 409 | return [ |
4f240ac1 TO |
410 | 'uploadDir', |
411 | 'imageUploadDir', | |
412 | 'customFileUploadDir', | |
413 | 'customTemplateDir', | |
414 | 'customPHPPathDir', | |
415 | 'extensionsDir', | |
be2fb01f | 416 | ]; |
4f240ac1 TO |
417 | } |
418 | ||
6a488035 | 419 | } |