ConfigSetting.php 12.7 KB
Newer Older
totten's avatar
totten committed
1
2
3
<?php
/*
 +--------------------------------------------------------------------+
Kurund Jalmi's avatar
Kurund Jalmi committed
4
 | CiviCRM version 4.7                                                |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
6
 | Copyright CiviCRM LLC (c) 2004-2017                                |
totten's avatar
totten committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27
28
29
30
31

/**
 *
 *
 * @package CRM
yashodha's avatar
yashodha committed
32
 * @copyright CiviCRM LLC (c) 2004-2017
totten's avatar
totten committed
33
34
35
 */

/**
eileenmcnaugton's avatar
eileenmcnaugton committed
36
 * File contains functions used in civicrm configuration.
totten's avatar
totten committed
37
38
39
40
 */
class CRM_Core_BAO_ConfigSetting {

  /**
colemanw's avatar
colemanw committed
41
   * Create civicrm settings. This is the same as add but it clears the cache and
lobo's avatar
lobo committed
42
   * reloads the config object
totten's avatar
totten committed
43
   *
44
45
   * @param array $params
   *   Associated array of civicrm variables.
totten's avatar
totten committed
46
   */
47
  public static function create($params) {
totten's avatar
totten committed
48
49
50
    self::add($params);
    $cache = CRM_Utils_Cache::singleton();
    $cache->delete('CRM_Core_Config');
eileen's avatar
eileen committed
51
    $cache->delete('CRM_Core_Config' . CRM_Core_Config::domainID());
totten's avatar
totten committed
52
53
54
55
    $config = CRM_Core_Config::singleton(TRUE, TRUE);
  }

  /**
56
   * Add civicrm settings.
totten's avatar
totten committed
57
   *
58
59
   * @param array $params
   *   Associated array of civicrm variables.
totten's avatar
totten committed
60
   */
61
  public static function add(&$params) {
totten's avatar
totten committed
62
63
64
65
    $domain = new CRM_Core_DAO_Domain();
    $domain->id = CRM_Core_Config::domainID();
    $domain->find(TRUE);
    if ($domain->config_backend) {
66
      $params = array_merge(unserialize($domain->config_backend), $params);
totten's avatar
totten committed
67
68
    }

69
    $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
totten's avatar
totten committed
70
71
72
73
74
75
76
77
78
79
80
81
82

    // also skip all Dir Params, we dont need to store those in the DB!
    foreach ($params as $name => $val) {
      if (substr($name, -3) == 'Dir') {
        unset($params[$name]);
      }
    }

    $domain->config_backend = serialize($params);
    $domain->save();
  }

  /**
83
   * Retrieve the settings values from db.
totten's avatar
totten committed
84
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
85
86
   * @param $defaults
   *
87
   * @return array
totten's avatar
totten committed
88
   */
89
  public static function retrieve(&$defaults) {
totten's avatar
totten committed
90
    $domain = new CRM_Core_DAO_Domain();
91
    $isUpgrade = CRM_Core_Config::isUpgradeMode();
totten's avatar
totten committed
92
93
94
95
96
97
98

    //we are initializing config, really can't use, CRM-7863
    $urlVar = 'q';
    if (defined('CIVICRM_UF') && CIVICRM_UF == 'Joomla') {
      $urlVar = 'task';
    }

99
    if ($isUpgrade && CRM_Core_DAO::checkFieldExists('civicrm_domain', 'config_backend')) {
totten's avatar
totten committed
100
101
      $domain->selectAdd('config_backend');
    }
jitendrapurohit's avatar
jitendrapurohit committed
102
103
104
    else {
      $domain->selectAdd('locales');
    }
totten's avatar
totten committed
105
106
107
108
109
110
111

    $domain->id = CRM_Core_Config::domainID();
    $domain->find(TRUE);
    if ($domain->config_backend) {
      $defaults = unserialize($domain->config_backend);
      if ($defaults === FALSE || !is_array($defaults)) {
        $defaults = array();
Kurund Jalmi's avatar
Kurund Jalmi committed
112
        return FALSE;
totten's avatar
totten committed
113
114
115
116
117
118
119
120
      }

      $skipVars = self::skipVars();
      foreach ($skipVars as $skip) {
        if (array_key_exists($skip, $defaults)) {
          unset($defaults[$skip]);
        }
      }
jitendrapurohit's avatar
jitendrapurohit committed
121
122
123
    }
    if (!$isUpgrade) {
      CRM_Core_BAO_ConfigSetting::applyLocale(Civi::settings($domain->id), $domain->locales);
124
125
    }
  }
totten's avatar
totten committed
126

127
128
129
130
131
132
133
134
135
136
137
  /**
   * Evaluate locale preferences and activate a chosen locale by
   * updating session+global variables.
   *
   * @param \Civi\Core\SettingsBag $settings
   * @param string $activatedLocales
   *   Imploded list of locales which are supported in the DB.
   */
  public static function applyLocale($settings, $activatedLocales) {
    // are we in a multi-language setup?
    $multiLang = $activatedLocales ? TRUE : FALSE;
totten's avatar
totten committed
138

139
140
    // set the current language
    $chosenLocale = NULL;
totten's avatar
totten committed
141

142
    $session = CRM_Core_Session::singleton();
totten's avatar
totten committed
143

144
145
146
147
148
149
    // on multi-lang sites based on request and civicrm_uf_match
    if ($multiLang) {
      $languageLimit = array();
      if (is_array($settings->get('languageLimit'))) {
        $languageLimit = $settings->get('languageLimit');
      }
totten's avatar
totten committed
150

151
152
153
      $requestLocale = CRM_Utils_Request::retrieve('lcMessages', 'String');
      if (in_array($requestLocale, array_keys($languageLimit))) {
        $chosenLocale = $requestLocale;
totten's avatar
totten committed
154

155
156
157
158
159
160
161
        //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache.
        // Ed: This doesn't sound good.
        CRM_Core_BAO_Cache::deleteGroup('navigation');
      }
      else {
        $requestLocale = NULL;
      }
totten's avatar
totten committed
162

163
164
165
166
167
168
169
      if (!$requestLocale) {
        $sessionLocale = $session->get('lcMessages');
        if (in_array($sessionLocale, array_keys($languageLimit))) {
          $chosenLocale = $sessionLocale;
        }
        else {
          $sessionLocale = NULL;
totten's avatar
totten committed
170
        }
171
      }
totten's avatar
totten committed
172

173
174
175
176
177
178
      if ($requestLocale) {
        $ufm = new CRM_Core_DAO_UFMatch();
        $ufm->contact_id = $session->get('userID');
        if ($ufm->find(TRUE)) {
          $ufm->language = $chosenLocale;
          $ufm->save();
totten's avatar
totten committed
179
        }
180
        $session->set('lcMessages', $chosenLocale);
totten's avatar
totten committed
181
      }
182
183
184
185
186
187

      if (!$chosenLocale and $session->get('userID')) {
        $ufm = new CRM_Core_DAO_UFMatch();
        $ufm->contact_id = $session->get('userID');
        if ($ufm->find(TRUE) &&
          in_array($ufm->language, array_keys($languageLimit))
Eileen McNaughton's avatar
Eileen McNaughton committed
188
        ) {
189
          $chosenLocale = $ufm->language;
totten's avatar
totten committed
190
        }
191
        $session->set('lcMessages', $chosenLocale);
totten's avatar
totten committed
192
      }
193
194
195
196
197
198
    }
    global $dbLocale;

    // try to inherit the language from the hosting CMS
    if ($settings->get('inheritLocale')) {
      // FIXME: On multilanguage installs, CRM_Utils_System::getUFLocale() in many cases returns nothing if $dbLocale is not set
199
200
      $lcMessages = $settings->get('lcMessages');
      $dbLocale = $multiLang && $lcMessages ? "_{$lcMessages}" : '';
201
202
203
      $chosenLocale = CRM_Utils_System::getUFLocale();
      if ($activatedLocales and !in_array($chosenLocale, explode(CRM_Core_DAO::VALUE_SEPARATOR, $activatedLocales))) {
        $chosenLocale = NULL;
204
      }
205
    }
totten's avatar
totten committed
206

207
208
209
210
    if (empty($chosenLocale)) {
      //CRM-11993 - if a single-lang site, use default
      $chosenLocale = $settings->get('lcMessages');
    }
totten's avatar
totten committed
211

212
    // set suffix for table names - use views if more than one language
213
    $dbLocale = $multiLang && $chosenLocale ? "_{$chosenLocale}" : '';
totten's avatar
totten committed
214

215
216
217
218
219
220
221
222
    // FIXME: an ugly hack to fix CRM-4041
    global $tsLocale;
    $tsLocale = $chosenLocale;

    // FIXME: as bad aplace as any to fix CRM-5428
    // (to be moved to a sane location along with the above)
    if (function_exists('mb_internal_encoding')) {
      mb_internal_encoding('UTF-8');
totten's avatar
totten committed
223
224
225
    }
  }

226
227
228
229
230
231
  /**
   * @param array $defaultValues
   *
   * @return string
   * @throws Exception
   */
232
  public static function doSiteMove($defaultValues = array()) {
totten's avatar
totten committed
233
    $moveStatus = ts('Beginning site move process...') . '<br />';
234
235
236
237
238
239
240
241
242
243
244
    $settings = Civi::settings();

    foreach (array_merge(self::getPathSettings(), self::getUrlSettings()) as $key) {
      $value = $settings->get($key);
      if ($value && $value != $settings->getDefault($key)) {
        if ($settings->getMandatory($key) === NULL) {
          $settings->revert($key);
          $moveStatus .= ts("WARNING: The setting (%1) has been reverted.", array(
            1 => $key,
          ));
          $moveStatus .= '<br />';
totten's avatar
totten committed
245
        }
246
247
248
249
250
        else {
          $moveStatus .= ts("WARNING: The setting (%1) is overridden and could not be reverted.", array(
            1 => $key,
          ));
          $moveStatus .= '<br />';
totten's avatar
totten committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
        }
      }
    }

    $config = CRM_Core_Config::singleton();

    // clear the template_c and upload directory also
    $config->cleanup(3, TRUE);
    $moveStatus .= ts('Template cache and upload directory have been cleared.') . '<br />';

    // clear all caches
    CRM_Core_Config::clearDBCache();
    $moveStatus .= ts('Database cache tables cleared.') . '<br />';

    $resetSessionTable = CRM_Utils_Request::retrieve('resetSessionTable',
      'Boolean',
      CRM_Core_DAO::$_nullArray,
      FALSE,
      FALSE,
      'REQUEST'
    );
    if ($config->userSystem->is_drupal &&
      $resetSessionTable
    ) {
      db_query("DELETE FROM {sessions} WHERE 1");
      $moveStatus .= ts('Drupal session table cleared.') . '<br />';
    }
    else {
      $session = CRM_Core_Session::singleton();
      $session->reset(2);
      $moveStatus .= ts('Session has been reset.') . '<br />';
    }

    return $moveStatus;
  }

  /**
288
   * Takes a componentName and enables it in the config.
totten's avatar
totten committed
289
290
   * Primarily used during unit testing
   *
291
292
   * @param string $componentName
   *   Name of the component to be enabled, needs to be valid.
totten's avatar
totten committed
293
   *
Kurund Jalmi's avatar
Kurund Jalmi committed
294
   * @return bool
295
   *   true if valid component name and enabling succeeds, else false
totten's avatar
totten committed
296
   */
297
  public static function enableComponent($componentName) {
totten's avatar
totten committed
298
299
300
301
302
303
304
    $config = CRM_Core_Config::singleton();
    if (in_array($componentName, $config->enableComponents)) {
      // component is already enabled
      return TRUE;
    }

    // return if component does not exist
305
    if (!array_key_exists($componentName, CRM_Core_Component::getComponents())) {
totten's avatar
totten committed
306
307
308
      return FALSE;
    }

Deepak Srivastava's avatar
Deepak Srivastava committed
309
    // get enabled-components from DB and add to the list
310
    $enabledComponents = Civi::settings()->get('enable_components');
Deepak Srivastava's avatar
Deepak Srivastava committed
311
    $enabledComponents[] = $componentName;
totten's avatar
totten committed
312

313
314
315
316
317
    self::setEnabledComponents($enabledComponents);

    return TRUE;
  }

Eileen McNaughton's avatar
Eileen McNaughton committed
318
319
320
321
322
323
324
  /**
   * Disable specified component.
   *
   * @param string $componentName
   *
   * @return bool
   */
325
  public static function disableComponent($componentName) {
326
    $config = CRM_Core_Config::singleton();
Eileen McNaughton's avatar
Eileen McNaughton committed
327
328
329
    if (!in_array($componentName, $config->enableComponents) ||
      !array_key_exists($componentName, CRM_Core_Component::getComponents())
    ) {
Eileen McNaughton's avatar
Eileen McNaughton committed
330
      // Post-condition is satisfied.
331
332
333
334
      return TRUE;
    }

    // get enabled-components from DB and add to the list
335
    $enabledComponents = Civi::settings()->get('enable_components');
336
337
338
339
340
341
342
    $enabledComponents = array_diff($enabledComponents, array($componentName));

    self::setEnabledComponents($enabledComponents);

    return TRUE;
  }

Eileen McNaughton's avatar
Eileen McNaughton committed
343
344
345
346
347
  /**
   * Set enabled components.
   *
   * @param array $enabledComponents
   */
348
  public static function setEnabledComponents($enabledComponents) {
349
350
    // fix the config object. update db.
    Civi::settings()->set('enable_components', $enabledComponents);
totten's avatar
totten committed
351
352
353
354
355

    // also force reset of component array
    CRM_Core_Component::getEnabledComponents(TRUE);
  }

356
357
358
  /**
   * @return array
   */
359
  public static function skipVars() {
totten's avatar
totten committed
360
    return array(
Eileen McNaughton's avatar
Eileen McNaughton committed
361
362
      'dsn',
      'templateCompileDir',
totten's avatar
totten committed
363
364
      'userFrameworkDSN',
      'userFramework',
Eileen McNaughton's avatar
Eileen McNaughton committed
365
366
367
368
      'userFrameworkBaseURL',
      'userFrameworkClass',
      'userHookClass',
      'userPermissionClass',
369
      'userPermissionTemp',
Eileen McNaughton's avatar
Eileen McNaughton committed
370
371
372
373
374
375
376
377
378
      'userFrameworkURLVar',
      'userFrameworkVersion',
      'newBaseURL',
      'newBaseDir',
      'newSiteName',
      'configAndLogDir',
      'qfKey',
      'gettextResourceDir',
      'cleanURL',
379
      'entryURL',
Eileen McNaughton's avatar
Eileen McNaughton committed
380
381
      'locale_custom_strings',
      'localeCustomStrings',
totten's avatar
totten committed
382
383
384
      'autocompleteContactSearch',
      'autocompleteContactReference',
      'checksumTimeout',
385
      'checksum_timeout',
totten's avatar
totten committed
386
387
    );
  }
388

389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
  /**
   * @param array $params
   * @return array
   */
  public static function filterSkipVars($params) {
    $skipVars = self::skipVars();
    foreach ($skipVars as $var) {
      unset($params[$var]);
    }
    foreach (array_keys($params) as $key) {
      if (preg_match('/^_qf_/', $key)) {
        unset($params[$key]);
      }
    }
    return $params;
  }

406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
  /**
   * @return array
   */
  private static function getUrlSettings() {
    return array(
      'userFrameworkResourceURL',
      'imageUploadURL',
      'customCSSURL',
      'extensionsURL',
    );
  }

  /**
   * @return array
   */
  private static function getPathSettings() {
    return array(
      'uploadDir',
      'imageUploadDir',
      'customFileUploadDir',
      'customTemplateDir',
      'customPHPPathDir',
      'extensionsDir',
    );
  }

totten's avatar
totten committed
432
}