ConfigSetting.php 22.7 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
4
 | CiviCRM version 4.6                                                |
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 36 37 38 39 40 41 42 43
 * $Id$
 *
 */

/**
 * file contains functions used in civicrm configuration
 *
 */
class CRM_Core_BAO_ConfigSetting {

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

  /**
61
   * Add civicrm settings.
totten's avatar
totten committed
62
   *
63 64
   * @param array $params
   *   Associated array of civicrm variables.
Eileen McNaughton's avatar
Eileen McNaughton committed
65
   *
kurund's avatar
kurund committed
66
   * @return void
totten's avatar
totten committed
67
   */
68
  public static function add(&$params) {
totten's avatar
totten committed
69 70 71 72 73
    self::fixParams($params);

    // also set a template url so js files can use this
    // CRM-6194
    $params['civiRelativeURL'] = CRM_Utils_System::url('CIVI_BASE_TEMPLATE');
kurund's avatar
kurund committed
74 75
    $params['civiRelativeURL']
      = str_replace(
lobo's avatar
lobo committed
76 77
        'CIVI_BASE_TEMPLATE',
        '',
Eileen McNaughton's avatar
Eileen McNaughton committed
78 79
        $params['civiRelativeURL']
      );
totten's avatar
totten committed
80

lobo's avatar
lobo committed
81 82 83
    // also add the version number for use by template / js etc
    $params['civiVersion'] = CRM_Utils_System::version();

totten's avatar
totten committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    $domain = new CRM_Core_DAO_Domain();
    $domain->id = CRM_Core_Config::domainID();
    $domain->find(TRUE);
    if ($domain->config_backend) {
      $values = unserialize($domain->config_backend);
      self::formatParams($params, $values);
    }

    // CRM-6151
    if (isset($params['localeCustomStrings']) &&
      is_array($params['localeCustomStrings'])
    ) {
      $domain->locale_custom_strings = serialize($params['localeCustomStrings']);
    }

    // unset any of the variables we read from file that should not be stored in the database
    // the username and certpath are stored flat with _test and _live
    // check CRM-1470
    $skipVars = self::skipVars();
    foreach ($skipVars as $var) {
      unset($params[$var]);
    }

    CRM_Core_BAO_Setting::fixAndStoreDirAndURL($params);

    // 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]);
      }
    }

Dmitry Smirnov's avatar
Dmitry Smirnov committed
116
    //keep user preferred language up to date, CRM-7746
totten's avatar
totten committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    $session = CRM_Core_Session::singleton();
    $lcMessages = CRM_Utils_Array::value('lcMessages', $params);
    if ($lcMessages && $session->get('userID')) {
      $languageLimit = CRM_Utils_Array::value('languageLimit', $params);
      if (is_array($languageLimit) &&
        !in_array($lcMessages, array_keys($languageLimit))
      ) {
        $lcMessages = $session->get('lcMessages');
      }

      $ufm = new CRM_Core_DAO_UFMatch();
      $ufm->contact_id = $session->get('userID');
      if ($lcMessages && $ufm->find(TRUE)) {
        $ufm->language = $lcMessages;
        $ufm->save();
        $session->set('lcMessages', $lcMessages);
        $params['lcMessages'] = $lcMessages;
      }
    }

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

  /**
142
   * Fix civicrm setting variables.
totten's avatar
totten committed
143
   *
144 145
   * @param array $params
   *   Associated array of civicrm variables.
Eileen McNaughton's avatar
Eileen McNaughton committed
146
   *
kurund's avatar
kurund committed
147
   * @return void
totten's avatar
totten committed
148
   */
149
  public static function fixParams(&$params) {
totten's avatar
totten committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    // in our old civicrm.settings.php we were using ISO code for country and
    // province limit, now we have changed it to use ids

    $countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode();

    $specialArray = array('countryLimit', 'provinceLimit');

    foreach ($params as $key => $value) {
      if (in_array($key, $specialArray) && is_array($value)) {
        foreach ($value as $k => $val) {
          if (!is_numeric($val)) {
            $params[$key][$k] = array_search($val, $countryIsoCodes);
          }
        }
      }
      elseif ($key == 'defaultContactCountry') {
        if (!is_numeric($value)) {
          $params[$key] = array_search($value, $countryIsoCodes);
        }
      }
    }
  }

  /**
174
   * Format the array containing before inserting in db.
totten's avatar
totten committed
175
   *
176 177 178 179
   * @param array $params
   *   Associated array of civicrm variables(submitted).
   * @param array $values
   *   Associated array of civicrm variables stored in db.
totten's avatar
totten committed
180
   *
kurund's avatar
kurund committed
181
   * @return void
totten's avatar
totten committed
182
   */
183
  public static function formatParams(&$params, &$values) {
totten's avatar
totten committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    if (empty($params) ||
      !is_array($params)
    ) {
      $params = $values;
    }
    else {
      foreach ($params as $key => $val) {
        if (array_key_exists($key, $values)) {
          unset($values[$key]);
        }
      }
      $params = array_merge($params, $values);
    }
  }

  /**
200
   * Retrieve the settings values from db.
totten's avatar
totten committed
201
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
202 203
   * @param $defaults
   *
204
   * @return array
totten's avatar
totten committed
205
   */
206
  public static function retrieve(&$defaults) {
totten's avatar
totten committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    $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');
    }
    elseif (CRM_Utils_Array::value($urlVar, $_GET) == 'admin/modules/list/confirm') {
      $domain->selectAdd('config_backend', 'locales');
    }
    else {
      $domain->selectAdd('config_backend, locales, locale_custom_strings');
    }

    $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
231
        return FALSE;
totten's avatar
totten committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
      }

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

      // check if there are any locale strings
      if ($domain->locale_custom_strings) {
        $defaults['localeCustomStrings'] = unserialize($domain->locale_custom_strings);
      }
      else {
        $defaults['localeCustomStrings'] = NULL;
      }

      // are we in a multi-language setup?
      $multiLang = $domain->locales ? TRUE : FALSE;

      // set the current language
      $lcMessages = NULL;

      $session = CRM_Core_Session::singleton();

      // on multi-lang sites based on request and civicrm_uf_match
      if ($multiLang) {
        $lcMessagesRequest = CRM_Utils_Request::retrieve('lcMessages', 'String', $this);
        $languageLimit = array();
        if (array_key_exists('languageLimit', $defaults) && is_array($defaults['languageLimit'])) {
          $languageLimit = $defaults['languageLimit'];
        }

        if (in_array($lcMessagesRequest, array_keys($languageLimit))) {
          $lcMessages = $lcMessagesRequest;

          //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache.
          CRM_Core_BAO_Cache::deleteGroup('navigation');
        }
        else {
          $lcMessagesRequest = NULL;
        }

        if (!$lcMessagesRequest) {
          $lcMessagesSession = $session->get('lcMessages');
          if (in_array($lcMessagesSession, array_keys($languageLimit))) {
            $lcMessages = $lcMessagesSession;
          }
          else {
            $lcMessagesSession = NULL;
          }
        }

        if ($lcMessagesRequest) {
          $ufm = new CRM_Core_DAO_UFMatch();
          $ufm->contact_id = $session->get('userID');
          if ($ufm->find(TRUE)) {
            $ufm->language = $lcMessages;
            $ufm->save();
          }
          $session->set('lcMessages', $lcMessages);
        }

        if (!$lcMessages 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))
          ) {
            $lcMessages = $ufm->language;
          }
          $session->set('lcMessages', $lcMessages);
        }
      }
      global $dbLocale;

      // try to inherit the language from the hosting CMS
309
      if (!empty($defaults['inheritLocale'])) {
totten's avatar
totten committed
310 311 312 313
        // FIXME: On multilanguage installs, CRM_Utils_System::getUFLocale() in many cases returns nothing if $dbLocale is not set
        $dbLocale = $multiLang ? "_{$defaults['lcMessages']}" : '';
        $lcMessages = CRM_Utils_System::getUFLocale();
        if ($domain->locales and !in_array($lcMessages, explode(CRM_Core_DAO::VALUE_SEPARATOR,
Eileen McNaughton's avatar
Eileen McNaughton committed
314 315 316
            $domain->locales
          ))
        ) {
totten's avatar
totten committed
317 318 319 320
          $lcMessages = NULL;
        }
      }

321
      if (empty($lcMessages)) {
atif-shaikh's avatar
atif-shaikh committed
322
        //CRM-11993 - if a single-lang site, use default
323 324
        $lcMessages = CRM_Utils_Array::value('lcMessages', $defaults);
      }
totten's avatar
totten committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

      // set suffix for table names - use views if more than one language
      $dbLocale = $multiLang ? "_{$lcMessages}" : '';

      // FIXME: an ugly hack to fix CRM-4041
      global $tsLocale;
      $tsLocale = $lcMessages;

      // 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');
      }
    }

    // dont add if its empty
    if (!empty($defaults)) {
      // retrieve directory and url preferences also
      CRM_Core_BAO_Setting::retrieveDirectoryAndURLPreferences($defaults);
Deepak Srivastava's avatar
Deepak Srivastava committed
344

345
      // Pickup enabled-components from settings table if found.
346 347 348
      $enableComponents = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components', NULL, array());
      if (!empty($enableComponents)) {
        $defaults['enableComponents'] = $enableComponents;
349

350 351 352
        // Lookup component IDs. Note: Do *not* instantiate components.
        // Classloading may not be fully setup yet.
        $components = CRM_Core_Component::getComponentIDs();
Deepak Srivastava's avatar
Deepak Srivastava committed
353 354
        $enabledComponentIDs = array();
        foreach ($defaults['enableComponents'] as $name) {
355
          $enabledComponentIDs[] = $components[$name];
Deepak Srivastava's avatar
Deepak Srivastava committed
356 357 358
        }
        $defaults['enableComponentIDs'] = $enabledComponentIDs;
      }
totten's avatar
totten committed
359 360 361
    }
  }

362 363 364
  /**
   * @return array
   */
365
  public static function getConfigSettings() {
totten's avatar
totten committed
366 367 368 369
    $config = CRM_Core_Config::singleton();

    $url = $dir = $siteName = $siteRoot = NULL;
    if ($config->userFramework == 'Joomla') {
lobo's avatar
lobo committed
370 371
      $url = preg_replace(
        '|administrator/components/com_civicrm/civicrm/|',
totten's avatar
totten committed
372 373 374 375 376 377 378
        '',
        $config->userFrameworkResourceURL
      );

      // lets use imageUploadDir since we dont mess around with its values
      // in the config object, lets kep it a bit generic since folks
      // might have different values etc
Eileen McNaughton's avatar
Eileen McNaughton committed
379 380 381 382 383 384 385

      //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
      if (DIRECTORY_SEPARATOR == '\\') {
        $dir = preg_replace(
          '|civicrm[/\\\\]templates_c[/\\\\].*$|',
          '',
          $config->templateCompileDir
386
        );
Eileen McNaughton's avatar
Eileen McNaughton committed
387 388 389 390 391 392
      }
      else {
        $dir = preg_replace(
          '|civicrm/templates_c/.*$|',
          '',
          $config->templateCompileDir
393
        );
Eileen McNaughton's avatar
Eileen McNaughton committed
394 395
      }

lobo's avatar
lobo committed
396 397 398 399 400 401
      $siteRoot = preg_replace(
        '|/media/civicrm/.*$|',
        '',
        $config->imageUploadDir
      );
    }
402
    elseif ($config->userFramework == 'WordPress') {
lobo's avatar
lobo committed
403 404 405 406 407 408 409 410 411
      $url = preg_replace(
        '|wp-content/plugins/civicrm/civicrm/|',
        '',
        $config->userFrameworkResourceURL
      );

      // lets use imageUploadDir since we dont mess around with its values
      // in the config object, lets kep it a bit generic since folks
      // might have different values etc
Eileen McNaughton's avatar
Eileen McNaughton committed
412 413 414 415 416 417 418

      //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
      if (DIRECTORY_SEPARATOR == '\\') {
        $dir = preg_replace(
          '|civicrm[/\\\\]templates_c[/\\\\].*$|',
          '',
          $config->templateCompileDir
419
        );
Eileen McNaughton's avatar
Eileen McNaughton committed
420 421 422 423 424 425
      }
      else {
        $dir = preg_replace(
          '|civicrm/templates_c/.*$|',
          '',
          $config->templateCompileDir
426
        );
Eileen McNaughton's avatar
Eileen McNaughton committed
427 428
      }

lobo's avatar
lobo committed
429 430
      $siteRoot = preg_replace(
        '|/wp-content/plugins/files/civicrm/.*$|',
totten's avatar
totten committed
431 432 433 434 435
        '',
        $config->imageUploadDir
      );
    }
    else {
lobo's avatar
lobo committed
436 437
      $url = preg_replace(
        '|sites/[\w\.\-\_]+/modules/civicrm/|',
totten's avatar
totten committed
438 439 440 441 442 443 444
        '',
        $config->userFrameworkResourceURL
      );

      // lets use imageUploadDir since we dont mess around with its values
      // in the config object, lets kep it a bit generic since folks
      // might have different values etc
Eileen McNaughton's avatar
Eileen McNaughton committed
445 446 447 448 449 450 451

      //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
      if (DIRECTORY_SEPARATOR == '\\') {
        $dir = preg_replace(
          '|[/\\\\]files[/\\\\]civicrm[/\\\\].*$|',
          '\\\\files\\\\',
          $config->imageUploadDir
452
        );
Eileen McNaughton's avatar
Eileen McNaughton committed
453 454 455 456 457 458
      }
      else {
        $dir = preg_replace(
          '|/files/civicrm/.*$|',
          '/files/',
          $config->imageUploadDir
459
        );
Eileen McNaughton's avatar
Eileen McNaughton committed
460 461
      }

totten's avatar
totten committed
462
      $matches = array();
lobo's avatar
lobo committed
463
      if (preg_match(
Eileen McNaughton's avatar
Eileen McNaughton committed
464 465 466 467
        '|/sites/([\w\.\-\_]+)/|',
        $config->imageUploadDir,
        $matches
      )) {
totten's avatar
totten committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481
        $siteName = $matches[1];
        if ($siteName) {
          $siteName = "/sites/$siteName/";
          $siteNamePos = strpos($dir, $siteName);
          if ($siteNamePos !== FALSE) {
            $siteRoot = substr($dir, 0, $siteNamePos);
          }
        }
      }
    }

    return array($url, $dir, $siteName, $siteRoot);
  }

Eileen McNaughton's avatar
Eileen McNaughton committed
482
  /**
483
   * Return likely default settings.
484 485
   * @return array
   *   site settings
486 487 488 489
   *   - $url
   *   - $dir Base Directory
   *   - $siteName
   *   - $siteRoot
Eileen McNaughton's avatar
Eileen McNaughton committed
490
   */
491
  public static function getBestGuessSettings() {
totten's avatar
totten committed
492
    $config = CRM_Core_Config::singleton();
Eileen McNaughton's avatar
Eileen McNaughton committed
493 494 495 496 497 498 499 500 501 502

    //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
    if (DIRECTORY_SEPARATOR == '\\') {
      $needle = 'civicrm[/\\\\]templates_c[/\\\\].*$';
    }
    else {
      $needle = 'civicrm/templates_c/.*$';
    }

    $dir = preg_replace(
503
      "|$needle|",
totten's avatar
totten committed
504 505 506 507
      '',
      $config->templateCompileDir
    );

508
    list($url, $siteName, $siteRoot) = $config->userSystem->getDefaultSiteSettings($dir);
totten's avatar
totten committed
509 510 511
    return array($url, $dir, $siteName, $siteRoot);
  }

512 513 514 515 516 517
  /**
   * @param array $defaultValues
   *
   * @return string
   * @throws Exception
   */
518
  public static function doSiteMove($defaultValues = array()) {
totten's avatar
totten committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
    $moveStatus = ts('Beginning site move process...') . '<br />';
    // get the current and guessed values
    list($oldURL, $oldDir, $oldSiteName, $oldSiteRoot) = self::getConfigSettings();
    list($newURL, $newDir, $newSiteName, $newSiteRoot) = self::getBestGuessSettings();

    // retrieve these values from the argument list
    $variables = array('URL', 'Dir', 'SiteName', 'SiteRoot', 'Val_1', 'Val_2', 'Val_3');
    $states = array('old', 'new');
    foreach ($variables as $varSuffix) {
      foreach ($states as $state) {
        $var = "{$state}{$varSuffix}";
        if (!isset($$var)) {
          if (isset($defaultValues[$var])) {
            $$var = $defaultValues[$var];
          }
          else {
            $$var = NULL;
          }
        }
        $$var = CRM_Utils_Request::retrieve($var,
          'String',
          CRM_Core_DAO::$_nullArray,
          FALSE,
          $$var,
          'REQUEST'
        );
      }
    }

    $from = $to = array();
    foreach ($variables as $varSuffix) {
      $oldVar = "old{$varSuffix}";
      $newVar = "new{$varSuffix}";
      //skip it if either is empty or both are exactly the same
      if ($$oldVar &&
        $$newVar &&
        $$oldVar != $$newVar
      ) {
        $from[] = $$oldVar;
        $to[] = $$newVar;
      }
    }

    $sql = "
SELECT config_backend
FROM   civicrm_domain
WHERE  id = %1
";
    $params = array(1 => array(CRM_Core_Config::domainID(), 'Integer'));
    $configBackend = CRM_Core_DAO::singleValueQuery($sql, $params);
    if (!$configBackend) {
      CRM_Core_Error::fatal(ts('Returning early due to unexpected error - civicrm_domain.config_backend column value is NULL. Try visiting CiviCRM Home page.'));
    }
    $configBackend = unserialize($configBackend);

    $configBackend = str_replace($from,
      $to,
      $configBackend
    );

    $configBackend = serialize($configBackend);
    $sql = "
UPDATE civicrm_domain
SET    config_backend = %2
WHERE  id = %1
";
    $params[2] = array($configBackend, 'String');
    CRM_Core_DAO::executeQuery($sql, $params);

    // Apply the changes to civicrm_option_values
    $optionGroups = array('url_preferences', 'directory_preferences');
    foreach ($optionGroups as $option) {
      foreach ($variables as $varSuffix) {
        $oldVar = "old{$varSuffix}";
        $newVar = "new{$varSuffix}";

        $from = $$oldVar;
        $to = $$newVar;

        if ($from && $to && $from != $to) {
          $sql = '
UPDATE civicrm_option_value
SET    value = REPLACE(value, %1, %2)
WHERE  option_group_id = (
  SELECT id
  FROM   civicrm_option_group
  WHERE  name = %3 )
';
Eileen McNaughton's avatar
Eileen McNaughton committed
607 608
          $params = array(
            1 => array($from, 'String'),
totten's avatar
totten committed
609 610 611 612 613 614 615 616
            2 => array($to, 'String'),
            3 => array($option, 'String'),
          );
          CRM_Core_DAO::executeQuery($sql, $params);
        }
      }
    }

Eileen McNaughton's avatar
Eileen McNaughton committed
617 618 619
    $moveStatus .=
      ts('Directory and Resource URLs have been updated in the moved database to reflect current site location.') .
      '<br />';
totten's avatar
totten committed
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

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

  /**
654
   * Takes a componentName and enables it in the config.
totten's avatar
totten committed
655 656
   * Primarily used during unit testing
   *
657 658
   * @param string $componentName
   *   Name of the component to be enabled, needs to be valid.
totten's avatar
totten committed
659
   *
kurund's avatar
kurund committed
660
   * @return bool
661
   *   true if valid component name and enabling succeeds, else false
totten's avatar
totten committed
662
   */
663
  public static function enableComponent($componentName) {
totten's avatar
totten committed
664 665 666 667 668 669 670
    $config = CRM_Core_Config::singleton();
    if (in_array($componentName, $config->enableComponents)) {
      // component is already enabled
      return TRUE;
    }

    // return if component does not exist
671
    if (!array_key_exists($componentName, CRM_Core_Component::getComponents())) {
totten's avatar
totten committed
672 673 674
      return FALSE;
    }

Deepak Srivastava's avatar
Deepak Srivastava committed
675
    // get enabled-components from DB and add to the list
kurund's avatar
kurund committed
676 677
    $enabledComponents
      = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components', NULL, array());
Deepak Srivastava's avatar
Deepak Srivastava committed
678
    $enabledComponents[] = $componentName;
totten's avatar
totten committed
679

680 681 682 683 684
    self::setEnabledComponents($enabledComponents);

    return TRUE;
  }

Eileen McNaughton's avatar
Eileen McNaughton committed
685 686 687 688 689 690 691
  /**
   * Disable specified component.
   *
   * @param string $componentName
   *
   * @return bool
   */
692
  public static function disableComponent($componentName) {
693
    $config = CRM_Core_Config::singleton();
Eileen McNaughton's avatar
Eileen McNaughton committed
694 695 696
    if (!in_array($componentName, $config->enableComponents) ||
      !array_key_exists($componentName, CRM_Core_Component::getComponents())
    ) {
Eileen McNaughton's avatar
Eileen McNaughton committed
697
      // Post-condition is satisfied.
698 699 700 701
      return TRUE;
    }

    // get enabled-components from DB and add to the list
kurund's avatar
kurund committed
702 703
    $enabledComponents
      = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components', NULL, array());
704 705 706 707 708 709 710
    $enabledComponents = array_diff($enabledComponents, array($componentName));

    self::setEnabledComponents($enabledComponents);

    return TRUE;
  }

Eileen McNaughton's avatar
Eileen McNaughton committed
711 712 713 714 715
  /**
   * Set enabled components.
   *
   * @param array $enabledComponents
   */
716 717 718 719
  public static function setEnabledComponents($enabledComponents) {
    $config = CRM_Core_Config::singleton();
    $components = CRM_Core_Component::getComponents();

Deepak Srivastava's avatar
Deepak Srivastava committed
720 721 722
    $enabledComponentIDs = array();
    foreach ($enabledComponents as $name) {
      $enabledComponentIDs[] = $components[$name]->componentID;
totten's avatar
totten committed
723 724 725
    }

    // fix the config object
Deepak Srivastava's avatar
Deepak Srivastava committed
726 727
    $config->enableComponents = $enabledComponents;
    $config->enableComponentIDs = $enabledComponentIDs;
totten's avatar
totten committed
728 729 730 731

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

Deepak Srivastava's avatar
Deepak Srivastava committed
732 733
    // update DB
    CRM_Core_BAO_Setting::setItem($enabledComponents,
734
      CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components');
totten's avatar
totten committed
735 736
  }

737 738 739
  /**
   * @return array
   */
740
  public static function skipVars() {
totten's avatar
totten committed
741
    return array(
Eileen McNaughton's avatar
Eileen McNaughton committed
742 743
      'dsn',
      'templateCompileDir',
totten's avatar
totten committed
744 745
      'userFrameworkDSN',
      'userFramework',
Eileen McNaughton's avatar
Eileen McNaughton committed
746 747 748 749
      'userFrameworkBaseURL',
      'userFrameworkClass',
      'userHookClass',
      'userPermissionClass',
750
      'userPermissionTemp',
Eileen McNaughton's avatar
Eileen McNaughton committed
751 752 753 754 755 756 757 758 759 760 761
      'userFrameworkURLVar',
      'userFrameworkVersion',
      'newBaseURL',
      'newBaseDir',
      'newSiteName',
      'configAndLogDir',
      'qfKey',
      'gettextResourceDir',
      'cleanURL',
      'locale_custom_strings',
      'localeCustomStrings',
totten's avatar
totten committed
762 763 764 765 766
      'autocompleteContactSearch',
      'autocompleteContactReference',
      'checksumTimeout',
    );
  }
767

totten's avatar
totten committed
768
}