ConfigSetting.php 12.5 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
 +--------------------------------------------------------------------+
colemanw's avatar
colemanw committed
6
 | Copyright CiviCRM LLC (c) 2004-2015                                |
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
colemanw's avatar
colemanw committed
32
 * @copyright CiviCRM LLC (c) 2004-2015
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 91 92 93 94 95 96 97 98 99 100 101
    $domain = new CRM_Core_DAO_Domain();

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

    if (CRM_Core_Config::isUpgradeMode()) {
      $domain->selectAdd('config_backend');
    }
    else {
102
      $domain->selectAdd('config_backend, locales');
totten's avatar
totten committed
103 104 105 106 107 108 109 110
    }

    $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's avatar
kurund committed
111
        return FALSE;
totten's avatar
totten committed
112 113 114 115 116 117 118 119 120
      }

      $skipVars = self::skipVars();
      foreach ($skipVars as $skip) {
        if (array_key_exists($skip, $defaults)) {
          unset($defaults[$skip]);
        }
      }

121 122 123
      CRM_Core_BAO_ConfigSetting::applyLocale(Civi::settings($domain->id), $domain->locales);
    }
  }
totten's avatar
totten committed
124

125 126 127 128 129 130 131 132 133 134 135
  /**
   * 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
136

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

140
    $session = CRM_Core_Session::singleton();
totten's avatar
totten committed
141

142 143 144 145 146 147
    // 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
148

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

153 154 155 156 157 158 159
        //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
160

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

171 172 173 174 175 176
      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
177
        }
178
        $session->set('lcMessages', $chosenLocale);
totten's avatar
totten committed
179
      }
180 181 182 183 184 185

      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
186
        ) {
187
          $chosenLocale = $ufm->language;
totten's avatar
totten committed
188
        }
189
        $session->set('lcMessages', $chosenLocale);
totten's avatar
totten committed
190
      }
191 192 193 194 195 196 197 198 199 200
    }
    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
      $dbLocale = $multiLang ? ("_" . $settings->get('lcMessages')) : '';
      $chosenLocale = CRM_Utils_System::getUFLocale();
      if ($activatedLocales and !in_array($chosenLocale, explode(CRM_Core_DAO::VALUE_SEPARATOR, $activatedLocales))) {
        $chosenLocale = NULL;
201
      }
202
    }
totten's avatar
totten committed
203

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

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

212 213 214 215 216 217 218 219
    // 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
220 221 222
    }
  }

223 224 225 226 227 228
  /**
   * @param array $defaultValues
   *
   * @return string
   * @throws Exception
   */
229
  public static function doSiteMove($defaultValues = array()) {
totten's avatar
totten committed
230
    $moveStatus = ts('Beginning site move process...') . '<br />';
231 232 233 234 235 236 237 238 239 240 241
    $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
242
        }
243 244 245 246 247
        else {
          $moveStatus .= ts("WARNING: The setting (%1) is overridden and could not be reverted.", array(
            1 => $key,
          ));
          $moveStatus .= '<br />';
totten's avatar
totten committed
248 249 250 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
        }
      }
    }

    $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;
  }

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

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

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

310 311 312 313 314
    self::setEnabledComponents($enabledComponents);

    return TRUE;
  }

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

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

    self::setEnabledComponents($enabledComponents);

    return TRUE;
  }

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

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

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

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  /**
   * @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;
  }

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
  /**
   * @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
429
}