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