From: Tim Otten Date: Sun, 23 Aug 2015 08:38:35 +0000 (-0700) Subject: CRM-16373 - CRM_Core_Config - Simplify initialization, esp `templateCompileDir` X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=635f0b8655fb0b77ba882cde2081e5055326d175;p=civicrm-core.git CRM-16373 - CRM_Core_Config - Simplify initialization, esp `templateCompileDir` Generally, this commit removes some silly 1-line functions, misplaed functions, duplicated steps, obvious comments, and expired comments. There is a change in `templateCompileDir`. The `templateCompileDir` had two different intialization blocks (in different places), which likely arose from variations in the code-paths used for memcache systems. Both routines append a locale and create the dir. This is redundant, and it unnecessarily forces us to resolve the locale early during initialization. But honestly we don't need to append the locale until much later (when/if Smarty is first used). There are a few oddballs who use `$config->templateCompileDir` and they may see a slight change (working with the real templateCompileDir rather than the locale subdir): * The cleanup. If this code causes a change, it will be an improvement: instead of cleaning the Smarty cache for one locale, it'll clean the entire Smarty cache for all locales. * Random temp files. The temp files seem to be transactional in nature (tied to the specific invocatoin) and not to the locale. --- diff --git a/CRM/Core/Config.php b/CRM/Core/Config.php index c9d54c33fd..2f3a49b0b1 100644 --- a/CRM/Core/Config.php +++ b/CRM/Core/Config.php @@ -94,7 +94,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { * * @var string */ - public $templateCompileDir = './templates_c/en_US/'; + public $templateCompileDir; /** * @var string @@ -119,13 +119,6 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { */ public $customPHPPathDir; - /** - * The factory class used to instantiate our DB objects - * - * @var string - */ - private $DAOFactoryClass = 'CRM_Contact_DAO_Factory'; - /** * The handle to the log that we are using * @var object @@ -183,128 +176,32 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { */ public static function &singleton($loadFromDB = TRUE, $force = FALSE) { if (self::$_singleton === NULL || $force) { - // goto a simple error handler $GLOBALS['civicrm_default_error_scope'] = CRM_Core_TemporaryErrorScope::create(array('CRM_Core_Error', 'handle')); $errorScope = CRM_Core_TemporaryErrorScope::create(array('CRM_Core_Error', 'simpleHandler')); - // lets ensure we set E_DEPRECATED to minimize errors - // CRM-6327 if (defined('E_DEPRECATED')) { error_reporting(error_reporting() & ~E_DEPRECATED); } - // first, attempt to get configuration object from cache $cache = CRM_Utils_Cache::singleton(); self::$_singleton = $cache->get('CRM_Core_Config' . CRM_Core_Config::domainID()); - // if not in cache, fire off config construction if (!self::$_singleton) { self::$_singleton = new CRM_Core_Config(); self::$_singleton->_initialize($loadFromDB); - - //initialize variables. for gencode we cannot load from the - //db since the db might not be initialized - if ($loadFromDB) { - // initialize stuff from the settings file - self::$_singleton->setCoreVariables(); - - self::$_singleton->_initVariables(); - - // I don't think we need to do this twice - // however just keeping this commented for now in 4.4 - // in case we hit any issues - CRM-13064 - // We can safely delete this once we release 4.4.4 - // self::$_singleton->setCoreVariables(); - } $cache->set('CRM_Core_Config' . CRM_Core_Config::domainID(), self::$_singleton); } else { - // we retrieve the object from memcache, so we now initialize the objects - self::$_singleton->_initialize($loadFromDB); - - // CRM-9803, NYSS-4822 - // this causes various settings to be reset and hence we should - // only use the config object that we retrieved from memcache - } - - self::$_singleton->initialized = 1; - - if (isset(self::$_singleton->customPHPPathDir) && - self::$_singleton->customPHPPathDir - ) { - $include_path = self::$_singleton->customPHPPathDir . PATH_SEPARATOR . get_include_path(); - set_include_path($include_path); + self::$_singleton->_initialize(FALSE); } - // set the callback at the very very end, to avoid an infinite loop - // set the error callback unset($errorScope); - // call the hook so other modules can add to the config - // again doing this at the very very end CRM_Utils_Hook::config(self::$_singleton); - - // make sure session is always initialised - $session = CRM_Core_Session::singleton(); - - // for logging purposes, pass the userID to the db - $userID = $session->get('userID'); - if ($userID) { - CRM_Core_DAO::executeQuery('SET @civicrm_user_id = %1', - array(1 => array($userID, 'Integer')) - ); - } - - // initialize authentication source - self::$_singleton->initAuthSrc(); + self::$_singleton->authenticate(); } return self::$_singleton; } - /** - * @param string $userFramework - * One of 'Drupal', 'Joomla', etc. - */ - private function _setUserFrameworkConfig($userFramework) { - - $this->userFrameworkClass = 'CRM_Utils_System_' . $userFramework; - $this->userHookClass = 'CRM_Utils_Hook_' . $userFramework; - $userPermissionClass = 'CRM_Core_Permission_' . $userFramework; - $this->userPermissionClass = new $userPermissionClass(); - - $class = $this->userFrameworkClass; - // redundant with _initVariables - $this->userSystem = new $class(); - - if ($userFramework == 'Joomla') { - $this->userFrameworkURLVar = 'task'; - } - - if (defined('CIVICRM_UF_BASEURL')) { - $this->userFrameworkBaseURL = CRM_Utils_File::addTrailingSlash(CIVICRM_UF_BASEURL, '/'); - - //format url for language negotiation, CRM-7803 - $this->userFrameworkBaseURL = CRM_Utils_System::languageNegotiationURL($this->userFrameworkBaseURL); - - if (CRM_Utils_System::isSSL()) { - $this->userFrameworkBaseURL = str_replace('http://', 'https://', - $this->userFrameworkBaseURL - ); - } - } - - if (defined('CIVICRM_UF_DSN')) { - $this->userFrameworkDSN = CIVICRM_UF_DSN; - } - - // this is dynamically figured out in the civicrm.settings.php file - if (defined('CIVICRM_CLEANURL')) { - $this->cleanURL = CIVICRM_CLEANURL; - } - else { - $this->cleanURL = 0; - } - } - /** * Initializes the entire application. * Reads constants defined in civicrm.settings.php and @@ -313,74 +210,41 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { * @param bool $loadFromDB */ private function _initialize($loadFromDB = TRUE) { - - // following variables should be set in CiviCRM settings and - // as crucial ones, are defined upon initialisation - // instead of in CRM_Core_Config_Defaults - if (defined('CIVICRM_DSN')) { - $this->dsn = CIVICRM_DSN; + if (!defined('CIVICRM_DSN') && $loadFromDB) { + $this->fatal('You need to define CIVICRM_DSN in civicrm.settings.php'); } - elseif ($loadFromDB) { - // bypass when calling from gencode - echo 'You need to define CIVICRM_DSN in civicrm.settings.php'; - exit(); + $this->dsn = defined('CIVICRM_DSN') ? CIVICRM_DSN : NULL; + + if (!defined('CIVICRM_TEMPLATE_COMPILEDIR') && $loadFromDB) { + $this->fatal('You need to define CIVICRM_TEMPLATE_COMPILEDIR in civicrm.settings.php'); } if (defined('CIVICRM_TEMPLATE_COMPILEDIR')) { - $this->templateCompileDir = CRM_Utils_File::addTrailingSlash(CIVICRM_TEMPLATE_COMPILEDIR); - - // also make sure we create the config directory within this directory - // the below statement will create both the templates directory and the config and log directory - $this->configAndLogDir - = CRM_Utils_File::baseFilePath($this->templateCompileDir) . - 'ConfigAndLog' . DIRECTORY_SEPARATOR; + $this->configAndLogDir = CRM_Utils_File::baseFilePath() . 'ConfigAndLog' . DIRECTORY_SEPARATOR; CRM_Utils_File::createDir($this->configAndLogDir); CRM_Utils_File::restrictAccess($this->configAndLogDir); - // we're automatically prefixing compiled templates directories with country/language code - global $tsLocale; - if (!empty($tsLocale)) { - $this->templateCompileDir .= CRM_Utils_File::addTrailingSlash($tsLocale); - } - elseif (!empty($this->lcMessages)) { - $this->templateCompileDir .= CRM_Utils_File::addTrailingSlash($this->lcMessages); - } - + $this->templateCompileDir = defined('CIVICRM_TEMPLATE_COMPILEDIR') ? CRM_Utils_File::addTrailingSlash(CIVICRM_TEMPLATE_COMPILEDIR) : NULL; CRM_Utils_File::createDir($this->templateCompileDir); CRM_Utils_File::restrictAccess($this->templateCompileDir); } - elseif ($loadFromDB) { - echo 'You need to define CIVICRM_TEMPLATE_COMPILEDIR in civicrm.settings.php'; - exit(); - } - $this->_initDAO(); + CRM_Core_DAO::init($this->dsn); - if (defined('CIVICRM_UF')) { - $this->userFramework = CIVICRM_UF; - $this->_setUserFrameworkConfig($this->userFramework); - } - else { - echo 'You need to define CIVICRM_UF in civicrm.settings.php'; - exit(); + if (!defined('CIVICRM_UF')) { + $this->fatal('You need to define CIVICRM_UF in civicrm.settings.php'); } + $this->setUserFramework(CIVICRM_UF); - // also initialize the logger - self::$_log = Log::singleton('display'); - } - - /** - * Initialize the DataObject framework. - */ - private function _initDAO() { - CRM_Core_DAO::init($this->dsn); + if ($loadFromDB) { + $this->_initVariables(); + } - $factoryClass = $this->DAOFactoryClass; - require_once str_replace('_', DIRECTORY_SEPARATOR, $factoryClass) . '.php'; - CRM_Core_DAO::setFactory(new $factoryClass()); - if (CRM_Utils_Constant::value('CIVICRM_MYSQL_STRICT', CRM_Utils_System::isDevelopment())) { - CRM_Core_DAO::executeQuery('SET SESSION sql_mode = STRICT_TRANS_TABLES'); + if (isset($this->customPHPPathDir) && $this->customPHPPathDir) { + set_include_path($this->customPHPPathDir . PATH_SEPARATOR . get_include_path()); } + + $this->initialized = 1; } /** @@ -400,6 +264,8 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { * Initialize the config variables. */ private function _initVariables() { + $this->templateDir = array(dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR); + // retrieve serialised settings $variables = array(); CRM_Core_BAO_ConfigSetting::retrieve($variables); @@ -446,30 +312,25 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { )), ts('Check Settings'), 'alert'); } } - elseif ($key == 'lcMessages') { - // reset the templateCompileDir to locale-specific and make sure it exists - if (substr($this->templateCompileDir, -1 * strlen($value) - 1, -1) != $value) { - $this->templateCompileDir .= CRM_Utils_File::addTrailingSlash($value); - CRM_Utils_File::createDir($this->templateCompileDir); - CRM_Utils_File::restrictAccess($this->templateCompileDir); - } - } $this->$key = $value; } if ($this->userFrameworkResourceURL) { - // we need to do this here so all blocks also load from an ssl server if (CRM_Utils_System::isSSL()) { - CRM_Utils_System::mapConfigToSSL(); + $this->userFrameworkResourceURL = str_replace('http://', 'https://', $this->userFrameworkResourceURL); + $this->resourceBase = $this->userFrameworkResourceURL; + + if (!empty($this->extensionsURL)) { + $this->extensionsURL = str_replace('http://', 'https://', $this->extensionsURL); + } + + $this->userSystem->mapConfigToSSL(); } + $rrb = parse_url($this->userFrameworkResourceURL); - // don't use absolute path if resources are stored on a different server - // CRM-4642 $this->resourceBase = $this->userFrameworkResourceURL; - if (isset($_SERVER['HTTP_HOST']) && - isset($rrb['host']) - ) { + if (isset($_SERVER['HTTP_HOST']) && isset($rrb['host'])) { $this->resourceBase = ($rrb['host'] == $_SERVER['HTTP_HOST']) ? $rrb['path'] : $this->userFrameworkResourceURL; } } @@ -478,17 +339,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { $this->customFileUploadDir = $this->uploadDir; } - if ($this->geoProvider) { - $this->geocodeMethod = 'CRM_Utils_Geocode_' . $this->geoProvider; - } - elseif ($this->mapProvider) { - $this->geocodeMethod = 'CRM_Utils_Geocode_' . $this->mapProvider; - } - - require_once str_replace('_', DIRECTORY_SEPARATOR, $this->userFrameworkClass) . '.php'; - $class = $this->userFrameworkClass; - // redundant with _setUserFrameworkConfig - $this->userSystem = new $class(); + $this->geocodeMethod = CRM_Utils_Geocode::getProviderClass(); } /** @@ -565,8 +416,18 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { /** * This method should initialize auth sources. */ - public function initAuthSrc() { + public function authenticate() { + // make sure session is always initialised $session = CRM_Core_Session::singleton(); + + // for logging purposes, pass the userID to the db + $userID = $session->get('userID'); + if ($userID) { + CRM_Core_DAO::executeQuery('SET @civicrm_user_id = %1', + array(1 => array($userID, 'Integer')) + ); + } + if ($session->get('userID') && !$session->get('authSrc')) { $session->set('authSrc', CRM_Core_Permission::AUTH_SRC_LOGIN); } @@ -743,10 +604,48 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { /** * Wrapper function to allow unit tests to switch user framework on the fly. + * + * @param string $userFramework + * One of 'Drupal', 'Joomla', etc. */ - public function setUserFramework($userFramework = NULL) { + public function setUserFramework($userFramework) { $this->userFramework = $userFramework; - $this->_setUserFrameworkConfig($userFramework); + $this->userFrameworkClass = 'CRM_Utils_System_' . $userFramework; + $this->userHookClass = 'CRM_Utils_Hook_' . $userFramework; + $userPermissionClass = 'CRM_Core_Permission_' . $userFramework; + $this->userPermissionClass = new $userPermissionClass(); + + $class = $this->userFrameworkClass; + $this->userSystem = new $class(); + + if ($userFramework == 'Joomla') { + $this->userFrameworkURLVar = 'task'; + } + + if (defined('CIVICRM_UF_BASEURL')) { + $this->userFrameworkBaseURL = CRM_Utils_File::addTrailingSlash(CIVICRM_UF_BASEURL, '/'); + + //format url for language negotiation, CRM-7803 + $this->userFrameworkBaseURL = CRM_Utils_System::languageNegotiationURL($this->userFrameworkBaseURL); + + if (CRM_Utils_System::isSSL()) { + $this->userFrameworkBaseURL = str_replace('http://', 'https://', + $this->userFrameworkBaseURL + ); + } + } + + if (defined('CIVICRM_UF_DSN')) { + $this->userFrameworkDSN = CIVICRM_UF_DSN; + } + + // this is dynamically figured out in the civicrm.settings.php file + if (defined('CIVICRM_CLEANURL')) { + $this->cleanURL = CIVICRM_CLEANURL; + } + else { + $this->cleanURL = 0; + } } /** @@ -773,4 +672,9 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { self::$_singleton = NULL; } + private function fatal($message) { + echo $message; + exit(); + } + } diff --git a/CRM/Core/Config/Defaults.php b/CRM/Core/Config/Defaults.php index f0f954430c..f4dd44f70e 100644 --- a/CRM/Core/Config/Defaults.php +++ b/CRM/Core/Config/Defaults.php @@ -42,15 +42,6 @@ */ class CRM_Core_Config_Defaults { - public function setCoreVariables() { - global $civicrm_root; - - $this->templateDir = array( - $civicrm_root . DIRECTORY_SEPARATOR . - 'templates' . DIRECTORY_SEPARATOR, - ); - } - /** * Set the default values. * in an empty db, also called when setting component using GUI @@ -79,9 +70,7 @@ class CRM_Core_Config_Defaults { } $baseCMSURL = CRM_Utils_System::baseCMSURL(); - if ($config->templateCompileDir) { - $path = CRM_Utils_File::baseFilePath($config->templateCompileDir); - } + $path = CRM_Utils_File::baseFilePath(); if (!isset($defaults['enableSSL'])) { $defaults['enableSSL'] = 0; } @@ -106,9 +95,7 @@ class CRM_Core_Config_Defaults { ); if (strpos($civicrm_root, - DIRECTORY_SEPARATOR . 'sites' . - DIRECTORY_SEPARATOR . 'all' . - DIRECTORY_SEPARATOR . 'modules' + DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'all' . DIRECTORY_SEPARATOR . 'modules' ) === FALSE ) { $startPos = strpos($civicrm_root, @@ -150,14 +137,14 @@ class CRM_Core_Config_Defaults { } } - if (!isset($defaults['imageUploadDir']) && is_dir($config->templateCompileDir)) { + if (!isset($defaults['imageUploadDir']) && is_dir($path)) { $imgDir = $path . "persist/contribute/"; CRM_Utils_File::createDir($imgDir); $defaults['imageUploadDir'] = $imgDir; } - if (!isset($defaults['uploadDir']) && is_dir($config->templateCompileDir)) { + if (!isset($defaults['uploadDir']) && is_dir($path)) { $uploadDir = $path . "upload/"; CRM_Utils_File::createDir($uploadDir); @@ -165,7 +152,7 @@ class CRM_Core_Config_Defaults { $defaults['uploadDir'] = $uploadDir; } - if (!isset($defaults['customFileUploadDir']) && is_dir($config->templateCompileDir)) { + if (!isset($defaults['customFileUploadDir']) && is_dir($path)) { $customDir = $path . "custom/"; CRM_Utils_File::createDir($customDir); diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 180cfba184..e139a3d9fa 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -104,6 +104,10 @@ class CRM_Core_DAO extends DB_DataObject { if (defined('CIVICRM_DAO_DEBUG')) { self::DebugLevel(CIVICRM_DAO_DEBUG); } + CRM_Core_DAO::setFactory(new CRM_Contact_DAO_Factory()); + if (CRM_Utils_Constant::value('CIVICRM_MYSQL_STRICT', CRM_Utils_System::isDevelopment())) { + CRM_Core_DAO::executeQuery('SET SESSION sql_mode = STRICT_TRANS_TABLES'); + } } /** diff --git a/CRM/Core/Smarty.php b/CRM/Core/Smarty.php index fb84a6c1a9..edc321d6d2 100644 --- a/CRM/Core/Smarty.php +++ b/CRM/Core/Smarty.php @@ -97,7 +97,9 @@ class CRM_Core_Smarty extends Smarty { else { $this->template_dir = $config->templateDir; } - $this->compile_dir = $config->templateCompileDir; + $this->compile_dir = CRM_Utils_File::addTrailingSlash(CRM_Utils_File::addTrailingSlash($config->templateCompileDir) . $this->getLocale()); + CRM_Utils_File::createDir($this->compile_dir); + CRM_Utils_File::restrictAccess($this->compile_dir); // check and ensure it is writable // else we sometime suppress errors quietly and this results @@ -315,4 +317,18 @@ class CRM_Core_Smarty extends Smarty { return $this; } + private function getLocale() { + global $tsLocale; + if (!empty($tsLocale)) { + return $tsLocale; + } + + $config = CRM_Core_Config::singleton(); + if (!empty($config->lcMessages)) { + return $config->lcMessages; + } + + return 'en_US'; + } + } diff --git a/CRM/Upgrade/Form.php b/CRM/Upgrade/Form.php index c46f121e83..099431f6da 100644 --- a/CRM/Upgrade/Form.php +++ b/CRM/Upgrade/Form.php @@ -123,7 +123,7 @@ class CRM_Upgrade_Form extends CRM_Core_Form { $this->locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales); $smarty = CRM_Core_Smarty::singleton(); - $smarty->compile_dir = $this->_config->templateCompileDir; + //$smarty->compile_dir = $this->_config->templateCompileDir; $smarty->assign('multilingual', $this->multilingual); $smarty->assign('locales', $this->locales); diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index eecc40712f..7da2868bbc 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -496,13 +496,14 @@ HTACCESS; * Create the base file path from which all our internal directories are * offset. This is derived from the template compile directory set */ - public static function baseFilePath($templateCompileDir = NULL) { + public static function baseFilePath() { static $_path = NULL; if (!$_path) { - if ($templateCompileDir == NULL) { - $config = CRM_Core_Config::singleton(); - $templateCompileDir = $config->templateCompileDir; + // Note: Don't rely on $config; that creates a dependency loop. + if (!defined('CIVICRM_TEMPLATE_COMPILEDIR')) { + throw new RuntimeException("Undefined constant: CIVICRM_TEMPLATE_COMPILEDIR"); } + $templateCompileDir = CIVICRM_TEMPLATE_COMPILEDIR; $path = dirname($templateCompileDir); diff --git a/CRM/Utils/Geocode.php b/CRM/Utils/Geocode.php new file mode 100644 index 0000000000..789a98dd76 --- /dev/null +++ b/CRM/Utils/Geocode.php @@ -0,0 +1,50 @@ +geoProvider) { + return 'CRM_Utils_Geocode_' . $config->geoProvider; + } + elseif ($config->mapProvider) { + return 'CRM_Utils_Geocode_' . $config->mapProvider; + } + else { + return ''; + } + } + +} diff --git a/CRM/Utils/System.php b/CRM/Utils/System.php index 8505ba808f..c9bcbab49a 100644 --- a/CRM/Utils/System.php +++ b/CRM/Utils/System.php @@ -499,21 +499,6 @@ class CRM_Utils_System { return $config->userSystem->postURL($action); } - /** - * Rewrite various system URLs to https. - */ - public static function mapConfigToSSL() { - $config = CRM_Core_Config::singleton(); - $config->userFrameworkResourceURL = str_replace('http://', 'https://', $config->userFrameworkResourceURL); - $config->resourceBase = $config->userFrameworkResourceURL; - - if (!empty($config->extensionsURL)) { - $config->extensionsURL = str_replace('http://', 'https://', $config->extensionsURL); - } - - return $config->userSystem->mapConfigToSSL(); - } - /** * Get the base URL of the system. *