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