Add domain id to cachestring
[civicrm-core.git] / CRM / Core / Config.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Config handles all the run time configuration changes that the system needs to deal with.
30 * Typically we'll have different values for a user's sandbox, a qa sandbox and a production area.
31 * The default values in general, should reflect production values (minimizes chances of screwing up)
32 *
33 * @package CRM
34 * @copyright CiviCRM LLC (c) 2004-2013
35 * $Id$
36 *
37 */
38
39 require_once 'Log.php';
40 require_once 'Mail.php';
41
42 require_once 'api/api.php';
43 class CRM_Core_Config extends CRM_Core_Config_Variables {
44 ///
45 /// BASE SYSTEM PROPERTIES (CIVICRM.SETTINGS.PHP)
46 ///
47
48 /**
49 * the dsn of the database connection
50 * @var string
51 */
52 public $dsn;
53
54 /**
55 * the name of user framework
56 * @var string
57 */
58 public $userFramework = 'Drupal';
59
60 /**
61 * the name of user framework url variable name
62 * @var string
63 */
64 public $userFrameworkURLVar = 'q';
65
66 /**
67 * the dsn of the database connection for user framework
68 * @var string
69 */
70 public $userFrameworkDSN = NULL;
71
72 /**
73 * The connector module for the CMS/UF
74 *
75 * @var CRM_Util_System_{$uf}
76 */
77 public $userSystem = NULL;
78
79 /**
80 * The root directory where Smarty should store
81 * compiled files
82 * @var string
83 */
84 public $templateCompileDir = './templates_c/en_US/';
85
86 public $configAndLogDir = NULL;
87
88 // END: BASE SYSTEM PROPERTIES (CIVICRM.SETTINGS.PHP)
89
90 ///
91 /// BEGIN HELPER CLASS PROPERTIES
92 ///
93
94 /**
95 * are we initialized and in a proper state
96 * @var string
97 */
98 public $initialized = 0;
99
100 /**
101 * the factory class used to instantiate our DB objects
102 * @var string
103 */
104 private $DAOFactoryClass = 'CRM_Contact_DAO_Factory';
105
106 /**
107 * The handle to the log that we are using
108 * @var object
109 */
110 private static $_log = NULL;
111
112 /**
113 * the handle on the mail handler that we are using
114 * @var object
115 */
116 private static $_mail = NULL;
117
118 /**
119 * We only need one instance of this object. So we use the singleton
120 * pattern and cache the instance in this variable
121 * @var object
122 * @static
123 */
124 private static $_singleton = NULL;
125
126 /**
127 * component registry object (of CRM_Core_Component type)
128 */
129 public $componentRegistry = NULL;
130
131 ///
132 /// END HELPER CLASS PROPERTIES
133 ///
134
135 ///
136 /// RUNTIME SET CLASS PROPERTIES
137 ///
138
139 /**
140 * to determine wether the call is from cms or civicrm
141 */
142 public $inCiviCRM = FALSE;
143
144 ///
145 /// END: RUNTIME SET CLASS PROPERTIES
146 ///
147
148 /**
149 * Define recaptcha key
150 */
151
152 public $recaptchaPublicKey;
153
154 /**
155 * The constructor. Sets domain id if defined, otherwise assumes
156 * single instance installation.
157 *
158 * @return void
159 * @access private
160 */
161 private function __construct() {
162 }
163
164 /**
165 * Singleton function used to manage this object.
166 *
167 * @param $loadFromDB boolean whether to load from the database
168 * @param $force boolean whether to force a reconstruction
169 *
170 * @return object
171 * @static
172 */
173 static function &singleton($loadFromDB = TRUE, $force = FALSE) {
174 if (self::$_singleton === NULL || $force) {
175 // goto a simple error handler
176 $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_CALLBACK;
177 $GLOBALS['_PEAR_default_error_options'] = array('CRM_Core_Error', 'simpleHandler');
178
179 // lets ensure we set E_DEPRECATED to minimize errors
180 // CRM-6327
181 if (defined('E_DEPRECATED')) {
182 error_reporting(error_reporting() & ~E_DEPRECATED);
183 }
184
185 // first, attempt to get configuration object from cache
186 $cache = CRM_Utils_Cache::singleton();
187 self::$_singleton = $cache->get('CRM_Core_Config' . CRM_Core_Config::domainID());
188 // if not in cache, fire off config construction
189 if (!self::$_singleton) {
190 self::$_singleton = new CRM_Core_Config;
191 self::$_singleton->_initialize($loadFromDB);
192
193 //initialize variables. for gencode we cannot load from the
194 //db since the db might not be initialized
195 if ($loadFromDB) {
196 self::$_singleton->_initVariables();
197
198 // retrieve and overwrite stuff from the settings file
199 self::$_singleton->setCoreVariables();
200 }
201 $cache->set('CRM_Core_Config' . CRM_Core_Config::domainID(), self::$_singleton);
202 }
203 else {
204 // we retrieve the object from memcache, so we now initialize the objects
205 self::$_singleton->_initialize($loadFromDB);
206
207 // CRM-9803, NYSS-4822
208 // this causes various settings to be reset and hence we should
209 // only use the config object that we retrived from memcache
210 }
211
212 self::$_singleton->initialized = 1;
213
214 if (isset(self::$_singleton->customPHPPathDir) &&
215 self::$_singleton->customPHPPathDir
216 ) {
217 $include_path = self::$_singleton->customPHPPathDir . PATH_SEPARATOR . get_include_path();
218 set_include_path($include_path);
219 }
220
221 // set the callback at the very very end, to avoid an infinite loop
222 // set the error callback
223 CRM_Core_Error::setCallback();
224
225 // call the hook so other modules can add to the config
226 // again doing this at the very very end
227 CRM_Utils_Hook::config(self::$_singleton);
228
229 // make sure session is always initialised
230 $session = CRM_Core_Session::singleton();
231
232 // for logging purposes, pass the userID to the db
233 $userID = $session->get('userID');
234 if ($userID) {
235 CRM_Core_DAO::executeQuery('SET @civicrm_user_id = %1',
236 array(1 => array($userID, 'Integer'))
237 );
238 }
239 }
240 return self::$_singleton;
241 }
242
243
244 private function _setUserFrameworkConfig($userFramework) {
245
246 $this->userFrameworkClass = 'CRM_Utils_System_' . $userFramework;
247 $this->userHookClass = 'CRM_Utils_Hook_' . $userFramework;
248 $userPermissionClass = 'CRM_Core_Permission_' . $userFramework;
249 $this->userPermissionClass = new $userPermissionClass();
250
251 $class = $this->userFrameworkClass;
252 // redundant with _initVariables
253 $userSystem = $this->userSystem = new $class();
254
255 if ($userFramework == 'Joomla') {
256 $this->userFrameworkURLVar = 'task';
257 }
258
259 if (defined('CIVICRM_UF_BASEURL')) {
260 $this->userFrameworkBaseURL = CRM_Utils_File::addTrailingSlash(CIVICRM_UF_BASEURL, '/');
261
262 //format url for language negotiation, CRM-7803
263 $this->userFrameworkBaseURL = CRM_Utils_System::languageNegotiationURL($this->userFrameworkBaseURL);
264
265 if (CRM_Utils_System::isSSL()) {
266 $this->userFrameworkBaseURL = str_replace('http://', 'https://',
267 $this->userFrameworkBaseURL
268 );
269 }
270 }
271
272 if (defined('CIVICRM_UF_DSN')) {
273 $this->userFrameworkDSN = CIVICRM_UF_DSN;
274 }
275
276 // this is dynamically figured out in the civicrm.settings.php file
277 if (defined('CIVICRM_CLEANURL')) {
278 $this->cleanURL = CIVICRM_CLEANURL;
279 }
280 else {
281 $this->cleanURL = 0;
282 }
283
284 $this->userFrameworkVersion = $userSystem->getVersion();
285
286 if ($userFramework == 'Joomla') {
287 global $mainframe;
288 $dbprefix = $mainframe ? $mainframe->getCfg('dbprefix') : 'jos_';
289 $this->userFrameworkUsersTableName = $dbprefix . 'users';
290 }
291 elseif ($userFramework == 'WordPress') {
292 global $wpdb;
293 $dbprefix = $wpdb ? $wpdb->prefix : '';
294 $this->userFrameworkUsersTableName = $dbprefix . 'users';
295 }
296 }
297
298 /**
299 * Initializes the entire application.
300 * Reads constants defined in civicrm.settings.php and
301 * stores them in config properties.
302 *
303 * @return void
304 * @access public
305 */
306 private function _initialize($loadFromDB = TRUE) {
307
308 // following variables should be set in CiviCRM settings and
309 // as crucial ones, are defined upon initialisation
310 // instead of in CRM_Core_Config_Defaults
311 if (defined('CIVICRM_DSN')) {
312 $this->dsn = CIVICRM_DSN;
313 }
314 elseif ($loadFromDB) {
315 // bypass when calling from gencode
316 echo 'You need to define CIVICRM_DSN in civicrm.settings.php';
317 exit();
318 }
319
320 if (defined('CIVICRM_TEMPLATE_COMPILEDIR')) {
321 $this->templateCompileDir = CRM_Utils_File::addTrailingSlash(CIVICRM_TEMPLATE_COMPILEDIR);
322
323 // also make sure we create the config directory within this directory
324 // the below statement will create both the templates directory and the config and log directory
325 $this->configAndLogDir =
326 CRM_Utils_File::baseFilePath($this->templateCompileDir) .
327 'ConfigAndLog' . DIRECTORY_SEPARATOR;
328 CRM_Utils_File::createDir($this->configAndLogDir);
329
330 // we're automatically prefixing compiled templates directories with country/language code
331 global $tsLocale;
332 if (!empty($tsLocale)) {
333 $this->templateCompileDir .= CRM_Utils_File::addTrailingSlash($tsLocale);
334 }
335 elseif (!empty($this->lcMessages)) {
336 $this->templateCompileDir .= CRM_Utils_File::addTrailingSlash($this->lcMessages);
337 }
338
339 CRM_Utils_File::createDir($this->templateCompileDir);
340 }
341 elseif ($loadFromDB) {
342 echo 'You need to define CIVICRM_TEMPLATE_COMPILEDIR in civicrm.settings.php';
343 exit();
344 }
345
346 $this->_initDAO();
347
348 if (defined('CIVICRM_UF')) {
349 $this->userFramework = CIVICRM_UF;
350 $this->_setUserFrameworkConfig($this->userFramework);
351 }
352 else {
353 echo 'You need to define CIVICRM_UF in civicrm.settings.php';
354 exit();
355 }
356
357 // also initialize the logger
358 self::$_log = Log::singleton('display');
359
360 // initialize component registry early to avoid "race"
361 // between CRM_Core_Config and CRM_Core_Component (they
362 // are co-dependant)
363 $this->componentRegistry = new CRM_Core_Component();
364 }
365
366 /**
367 * initialize the DataObject framework
368 *
369 * @return void
370 * @access private
371 */
372 private function _initDAO() {
373 CRM_Core_DAO::init($this->dsn);
374
375 $factoryClass = $this->DAOFactoryClass;
376 require_once str_replace('_', DIRECTORY_SEPARATOR, $factoryClass) . '.php';
377 CRM_Core_DAO::setFactory(new $factoryClass());
378 if (CRM_Utils_Constant::value('CIVICRM_MYSQL_STRICT', CRM_Utils_System::isDevelopment())) {
379 CRM_Core_DAO::executeQuery('SET SESSION sql_mode = STRICT_TRANS_TABLES');
380 }
381 }
382
383 /**
384 * returns the singleton logger for the application
385 *
386 * @param
387 * @access private
388 *
389 * @return object
390 */
391 static public function &getLog() {
392 if (!isset(self::$_log)) {
393 self::$_log = Log::singleton('display');
394 }
395
396 return self::$_log;
397 }
398
399 /**
400 * initialize the config variables
401 *
402 * @return void
403 * @access private
404 */
405 private function _initVariables() {
406 // retrieve serialised settings
407 $variables = array();
408 CRM_Core_BAO_ConfigSetting::retrieve($variables);
409
410 // if settings are not available, go down the full path
411 if (empty($variables)) {
412 // Step 1. get system variables with their hardcoded defaults
413 $variables = get_object_vars($this);
414
415 // Step 2. get default values (with settings file overrides if
416 // available - handled in CRM_Core_Config_Defaults)
417 CRM_Core_Config_Defaults::setValues($variables);
418
419 // retrieve directory and url preferences also
420 CRM_Core_BAO_Setting::retrieveDirectoryAndURLPreferences($variables);
421
422 // add component specific settings
423 $this->componentRegistry->addConfig($this);
424
425 // serialise settings
426 $settings = $variables;
427 CRM_Core_BAO_ConfigSetting::add($settings);
428 }
429
430 $urlArray = array('userFrameworkResourceURL', 'imageUploadURL');
431 $dirArray = array('uploadDir', 'customFileUploadDir');
432
433 foreach ($variables as $key => $value) {
434 if (in_array($key, $urlArray)) {
435 $value = CRM_Utils_File::addTrailingSlash($value, '/');
436 }
437 elseif (in_array($key, $dirArray)) {
438 if ($value) {
439 $value = CRM_Utils_File::addTrailingSlash($value);
440 }
441 if (empty($value) || (CRM_Utils_File::createDir($value, FALSE) === FALSE)) {
442 // seems like we could not create the directories
443 // settings might have changed, lets suppress a message for now
444 // so we can make some more progress and let the user fix their settings
445 // for now we assign it to a know value
446 // CRM-4949
447 $value = $this->templateCompileDir;
448 $url = CRM_Utils_System::url('civicrm/admin/setting/path', 'reset=1');
449 CRM_Core_Session::setStatus(ts('%1 has an incorrect directory path. Please go to the <a href="%2">path setting page</a> and correct it.', array(
450 1 => $key,
451 2 => $url
452 )), ts('Check Settings'), 'alert');
453 }
454 }
455 elseif ($key == 'lcMessages') {
456 // reset the templateCompileDir to locale-specific and make sure it exists
457 if (substr($this->templateCompileDir, -1 * strlen($value) - 1, -1) != $value) {
458 $this->templateCompileDir .= CRM_Utils_File::addTrailingSlash($value);
459 CRM_Utils_File::createDir($this->templateCompileDir);
460 }
461 }
462
463 $this->$key = $value;
464 }
465
466 if ($this->userFrameworkResourceURL) {
467 // we need to do this here so all blocks also load from an ssl server
468 if (CRM_Utils_System::isSSL()) {
469 CRM_Utils_System::mapConfigToSSL();
470 }
471 $rrb = parse_url($this->userFrameworkResourceURL);
472 // dont use absolute path if resources are stored on a different server
473 // CRM-4642
474 $this->resourceBase = $this->userFrameworkResourceURL;
475 if (isset($_SERVER['HTTP_HOST']) &&
476 isset($rrb['host'])
477 ) {
478 $this->resourceBase = ($rrb['host'] == $_SERVER['HTTP_HOST']) ? $rrb['path'] : $this->userFrameworkResourceURL;
479 }
480 }
481
482 if (!$this->customFileUploadDir) {
483 $this->customFileUploadDir = $this->uploadDir;
484 }
485
486 if ($this->geoProvider) {
487 $this->geocodeMethod = 'CRM_Utils_Geocode_' . $this->geoProvider;
488 }
489 elseif ($this->mapProvider) {
490 $this->geocodeMethod = 'CRM_Utils_Geocode_' . $this->mapProvider;
491 }
492
493 require_once (str_replace('_', DIRECTORY_SEPARATOR, $this->userFrameworkClass) . '.php');
494 $class = $this->userFrameworkClass;
495 // redundant with _setUserFrameworkConfig
496 $this->userSystem = new $class();
497 }
498
499 /**
500 * retrieve a mailer to send any mail from the applciation
501 *
502 * @param boolean $persist open a persistent smtp connection, should speed up mailings
503 *
504 * @access private
505 *
506 * @return object
507 */
508 static function &getMailer($persist = FALSE) {
509 if (!isset(self::$_mail)) {
510 $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
511 'mailing_backend'
512 );
513
514 if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB ||
515 (defined('CIVICRM_MAILER_SPOOL') && CIVICRM_MAILER_SPOOL)
516 ) {
517 self::$_mail = new CRM_Mailing_BAO_Spool();
518 }
519 elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) {
520 if ($mailingInfo['smtpServer'] == '' || !$mailingInfo['smtpServer']) {
521 CRM_Core_Error::fatal(ts('There is no valid smtp server setting. Click <a href=\'%1\'>Administer CiviCRM >> Global Settings</a> to set the SMTP Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting', 'reset=1'))));
522 }
523
524 $params['host'] = $mailingInfo['smtpServer'] ? $mailingInfo['smtpServer'] : 'localhost';
525 $params['port'] = $mailingInfo['smtpPort'] ? $mailingInfo['smtpPort'] : 25;
526
527 if ($mailingInfo['smtpAuth']) {
528 $params['username'] = $mailingInfo['smtpUsername'];
529 $params['password'] = CRM_Utils_Crypt::decrypt($mailingInfo['smtpPassword']);
530 $params['auth'] = TRUE;
531 }
532 else {
533 $params['auth'] = FALSE;
534 }
535
536 // set the localhost value, CRM-3153
537 $params['localhost'] = CRM_Utils_Array::value('SERVER_NAME', $_SERVER, 'localhost');
538
539 // also set the timeout value, lets set it to 30 seconds
540 // CRM-7510
541 $params['timeout'] = 30;
542
543 // CRM-9349
544 $params['persist'] = $persist;
545
546 self::$_mail = Mail::factory('smtp', $params);
547 }
548 elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) {
549 if ($mailingInfo['sendmail_path'] == '' ||
550 !$mailingInfo['sendmail_path']
551 ) {
552 CRM_Core_Error::fatal(ts('There is no valid sendmail path setting. Click <a href=\'%1\'>Administer CiviCRM >> Global Settings</a> to set the Sendmail Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting', 'reset=1'))));
553 }
554 $params['sendmail_path'] = $mailingInfo['sendmail_path'];
555 $params['sendmail_args'] = $mailingInfo['sendmail_args'];
556
557 self::$_mail = Mail::factory('sendmail', $params);
558 }
559 elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) {
560 $params = array();
561 self::$_mail = Mail::factory('mail', $params);
562 }
563 elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK) {
564 self::$_mail = Mail::factory('mock', $params);
565 }
566 else {
567 CRM_Core_Session::setStatus(ts('There is no valid SMTP server Setting Or SendMail path setting. Click <a href=\'%1\'>Administer CiviCRM >> Global Settings</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting', 'reset=1'))), ts('Check Settings'), 'alert');
568 }
569 }
570 return self::$_mail;
571 }
572
573 /**
574 * delete the web server writable directories
575 *
576 * @param int $value 1 - clean templates_c, 2 - clean upload, 3 - clean both
577 *
578 * @access public
579 *
580 * @return void
581 */
582 public function cleanup($value, $rmdir = TRUE) {
583 $value = (int ) $value;
584
585 if ($value & 1) {
586 // clean templates_c
587 CRM_Utils_File::cleanDir($this->templateCompileDir, $rmdir);
588 CRM_Utils_File::createDir($this->templateCompileDir);
589 }
590 if ($value & 2) {
591 // clean upload dir
592 CRM_Utils_File::cleanDir($this->uploadDir);
593 CRM_Utils_File::createDir($this->uploadDir);
594 CRM_Utils_File::restrictAccess($this->uploadDir);
595 }
596 }
597
598 /**
599 * verify that the needed parameters are not null in the config
600 *
601 * @param CRM_Core_Config (reference ) the system config object
602 * @param array (reference ) the parameters that need a value
603 *
604 * @return boolean
605 * @static
606 * @access public
607 */
608 static function check(&$config, &$required) {
609 foreach ($required as $name) {
610 if (CRM_Utils_System::isNull($config->$name)) {
611 return FALSE;
612 }
613 }
614 return TRUE;
615 }
616
617 /**
618 * reset the serialized array and recompute
619 * use with care
620 */
621 function reset() {
622 $query = "UPDATE civicrm_domain SET config_backend = null";
623 CRM_Core_DAO::executeQuery($query);
624 }
625
626 /**
627 * one function to get domain ID
628 */
629 static function domainID($domainID = NULL, $reset = FALSE) {
630 static $domain;
631 if ($domainID) {
632 $domain = $domainID;
633 }
634 if ($reset || empty($domain)) {
635 $domain = defined('CIVICRM_DOMAIN_ID') ? CIVICRM_DOMAIN_ID : 1;
636 }
637
638 return $domain;
639 }
640
641 /**
642 * do general cleanup of caches, temp directories and temp tables
643 * CRM-8739
644 */
645 function cleanupCaches($sessionReset = TRUE) {
646 // cleanup templates_c directory
647 $this->cleanup(1, FALSE);
648
649 // clear db caching
650 self::clearDBCache();
651
652 if ($sessionReset) {
653 $session = CRM_Core_Session::singleton();
654 $session->reset(2);
655 }
656 }
657
658 /**
659 * Do general cleanup of module permissions.
660 */
661 function cleanupPermissions() {
662 $module_files = CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles();
663 if ($this->userPermissionClass->isModulePermissionSupported()) {
664 // Can store permissions -- so do it!
665 $this->userPermissionClass->upgradePermissions(
666 CRM_Core_Permission::basicPermissions()
667 );
668 } else {
669 // Cannot store permissions -- warn if any modules require them
670 $modules_with_perms = array();
671 foreach ($module_files as $module_file) {
672 $perms = $this->userPermissionClass->getModulePermissions($module_file['prefix']);
673 if (!empty($perms)) {
674 $modules_with_perms[] = $module_file['prefix'];
675 }
676 }
677 if (!empty($modules_with_perms)) {
678 CRM_Core_Session::setStatus(
679 ts('Some modules define permissions, but the CMS cannot store them: %1', array(1 => implode(', ', $modules_with_perms))),
680 ts('Permission Error'),
681 'error'
682 );
683 }
684 }
685 }
686
687 /**
688 * Flush information about loaded modules
689 */
690 function clearModuleList() {
691 CRM_Extension_System::singleton()->getCache()->flush();
692 CRM_Utils_Hook::singleton(TRUE);
693 CRM_Core_PseudoConstant::getModuleExtensions(TRUE);
694 CRM_Core_Module::getAll(TRUE);
695 }
696
697 /**
698 * clear db cache
699 */
700 public static function clearDBCache() {
701 $queries = array(
702 'TRUNCATE TABLE civicrm_acl_cache',
703 'TRUNCATE TABLE civicrm_acl_contact_cache',
704 'TRUNCATE TABLE civicrm_cache',
705 'TRUNCATE TABLE civicrm_prevnext_cache',
706 'UPDATE civicrm_group SET cache_date = NULL',
707 'TRUNCATE TABLE civicrm_group_contact_cache',
708 'TRUNCATE TABLE civicrm_menu',
709 'UPDATE civicrm_setting SET value = NULL WHERE name="navigation" AND contact_id IS NOT NULL',
710 'DELETE FROM civicrm_setting WHERE name="modulePaths"', // CRM-10543
711 );
712
713 foreach ($queries as $query) {
714 CRM_Core_DAO::executeQuery($query);
715 }
716
717 // also delete all the import and export temp tables
718 self::clearTempTables();
719 }
720
721 /**
722 * clear leftover temporary tables
723 */
724 public static function clearTempTables() {
725 // CRM-5645
726 $dao = CRM_Core_DAO::executeQuery("SELECT DATABASE();");
727 $query = "
728 SELECT TABLE_NAME as tableName
729 FROM INFORMATION_SCHEMA.TABLES
730 WHERE TABLE_SCHEMA = %1
731 AND ( TABLE_NAME LIKE 'civicrm_import_job_%'
732 OR TABLE_NAME LIKE 'civicrm_export_temp%'
733 OR TABLE_NAME LIKE 'civicrm_task_action_temp%' )
734 ";
735
736 $params = array(1 => array($dao->database(), 'String'));
737 $tableDAO = CRM_Core_DAO::executeQuery($query, $params);
738 $tables = array();
739 while ($tableDAO->fetch()) {
740 $tables[] = $tableDAO->tableName;
741 }
742 if (!empty($tables)) {
743 $table = implode(',', $tables);
744 // drop leftover temporary tables
745 CRM_Core_DAO::executeQuery("DROP TABLE $table");
746 }
747 }
748
749 /**
750 * function to check if running in upgrade mode
751 */
752 static function isUpgradeMode($path = NULL) {
753 if (defined('CIVICRM_UPGRADE_ACTIVE')) {
754 return TRUE;
755 }
756
757 if (!$path) {
758 // note: do not re-initialize config here, since this function is part of
759 // config initialization itself
760 $urlVar = 'q';
761 if (defined('CIVICRM_UF') && CIVICRM_UF == 'Joomla') {
762 $urlVar = 'task';
763 }
764
765 $path = CRM_Utils_Array::value($urlVar, $_GET);
766 }
767
768 if ($path && preg_match('/^civicrm\/upgrade(\/.*)?$/', $path)) {
769 return TRUE;
770 }
771
772 return FALSE;
773 }
774
775 /**
776 * Wrapper function to allow unit tests to switch user framework on the fly
777 */
778 public function setUserFramework($userFramework = NULL) {
779 $this->userFramework = $userFramework;
780 $this->_setUserFrameworkConfig($userFramework);
781 }
782 }
783 // end CRM_Core_Config
784