From d9d7e7dd0ff305a09ffde228ff3fe8ec833bb161 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sun, 23 Aug 2015 10:03:44 -0700 Subject: [PATCH] CRM-16373 - Save URLs and paths through settings instead of domain --- CRM/Admin/Form/Setting/Path.php | 15 +- CRM/Admin/Form/Setting/Url.php | 8 +- CRM/Core/BAO/ConfigSetting.php | 5 - CRM/Core/BAO/Setting.php | 160 --------------------- CRM/Core/Form.php | 1 + CRM/Utils/Rule.php | 29 ++++ settings/Url.setting.php | 8 +- tests/phpunit/CRM/Core/BAO/SettingTest.php | 15 +- tests/phpunit/api/v3/SettingTest.php | 2 +- 9 files changed, 52 insertions(+), 191 deletions(-) diff --git a/CRM/Admin/Form/Setting/Path.php b/CRM/Admin/Form/Setting/Path.php index f33b15a57e..ba11d3c46c 100644 --- a/CRM/Admin/Form/Setting/Path.php +++ b/CRM/Admin/Form/Setting/Path.php @@ -36,11 +36,21 @@ */ class CRM_Admin_Form_Setting_Path extends CRM_Admin_Form_Setting { + protected $_settings = array( + 'uploadDir' => CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME, + 'imageUploadDir' => CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME, + 'customFileUploadDir' => CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME, + 'customTemplateDir' => CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME, + 'customPHPPathDir' => CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME, + 'extensionsDir' => CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME, + ); + /** * Build the form object. */ public function buildQuickForm() { CRM_Utils_System::setTitle(ts('Settings - Upload Directories')); + parent::buildQuickForm(); $directories = array( 'uploadDir' => ts('Temporary Files'), @@ -51,16 +61,15 @@ class CRM_Admin_Form_Setting_Path extends CRM_Admin_Form_Setting { 'extensionsDir' => ts('CiviCRM Extensions Directory'), ); foreach ($directories as $name => $title) { - $this->add('text', $name, $title); + //$this->add('text', $name, $title); $this->addRule($name, ts("'%1' directory does not exist", array(1 => $title) ), - 'fileExists' + 'settingPath' ); } - parent::buildQuickForm(); } public function postProcess() { diff --git a/CRM/Admin/Form/Setting/Url.php b/CRM/Admin/Form/Setting/Url.php index 4dbcf9eb49..7a3f8d0072 100644 --- a/CRM/Admin/Form/Setting/Url.php +++ b/CRM/Admin/Form/Setting/Url.php @@ -37,6 +37,10 @@ class CRM_Admin_Form_Setting_Url extends CRM_Admin_Form_Setting { protected $_settings = array( 'disable_core_css' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, + 'userFrameworkResourceURL' => CRM_Core_BAO_Setting::URL_PREFERENCES_NAME, + 'imageUploadURL' => CRM_Core_BAO_Setting::URL_PREFERENCES_NAME, + 'customCSSURL' => CRM_Core_BAO_Setting::URL_PREFERENCES_NAME, + 'extensionsURL' => CRM_Core_BAO_Setting::URL_PREFERENCES_NAME, ); /** @@ -48,10 +52,6 @@ class CRM_Admin_Form_Setting_Url extends CRM_Admin_Form_Setting { 'version' => 3, )); - $this->addElement('text', 'userFrameworkResourceURL', ts('CiviCRM Resource URL')); - $this->addElement('text', 'imageUploadURL', ts('Image Upload URL')); - $this->addElement('text', 'customCSSURL', ts('Custom css URL')); - $this->addElement('text', 'extensionsURL', ts('Extension Resource URL')); $this->addYesNo('enableSSL', ts('Force Secure URLs (SSL)')); $this->addYesNo('verifySSL', ts('Verify SSL Certs')); // FIXME: verifySSL should use $_settings instead of manually adding fields diff --git a/CRM/Core/BAO/ConfigSetting.php b/CRM/Core/BAO/ConfigSetting.php index 646ac5ffd5..01a5010848 100644 --- a/CRM/Core/BAO/ConfigSetting.php +++ b/CRM/Core/BAO/ConfigSetting.php @@ -84,8 +84,6 @@ class CRM_Core_BAO_ConfigSetting { unset($params[$var]); } - CRM_Core_BAO_Setting::fixAndStoreDirAndURL($params); - // also skip all Dir Params, we dont need to store those in the DB! foreach ($params as $name => $val) { if (substr($name, -3) == 'Dir') { @@ -315,9 +313,6 @@ class CRM_Core_BAO_ConfigSetting { // dont add if its empty if (!empty($defaults)) { - // retrieve directory and url preferences also - CRM_Core_BAO_Setting::retrieveDirectoryAndURLPreferences($defaults); - // Pickup enabled-components from settings table if found. $enableComponents = Civi::settings()->get('enable_components'); if (!empty($enableComponents)) { diff --git a/CRM/Core/BAO/Setting.php b/CRM/Core/BAO/Setting.php index cf2d834efb..72a4aaaba8 100644 --- a/CRM/Core/BAO/Setting.php +++ b/CRM/Core/BAO/Setting.php @@ -904,166 +904,6 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { self::setItem($optionValue, $group, $name); } - /** - * @param array $params - * @param int $domainID - */ - public static function fixAndStoreDirAndURL(&$params, $domainID = NULL) { - if (self::isUpgradeFromPreFourOneAlpha1()) { - return; - } - - if (empty($domainID)) { - $domainID = CRM_Core_Config::domainID(); - } - $sql = " - SELECT name, group_name - FROM civicrm_setting - WHERE domain_id = %1 - AND ( group_name = %2 - OR group_name = %3 ) -"; - $sqlParams = array( - 1 => array($domainID, 'Integer'), - 2 => array(self::DIRECTORY_PREFERENCES_NAME, 'String'), - 3 => array(self::URL_PREFERENCES_NAME, 'String'), - ); - - $dirParams = array(); - $urlParams = array(); - - $dao = CRM_Core_DAO::executeQuery($sql, - $sqlParams, - TRUE, - NULL, - FALSE, - TRUE, - // trap exceptions as error - TRUE - ); - - if (is_a($dao, 'DB_Error')) { - if (CRM_Core_Config::isUpgradeMode()) { - // seems like this is a 4.0 -> 4.1 upgrade, so we suppress this error and continue - return; - } - else { - echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n"; - exit(); - } - } - - while ($dao->fetch()) { - if (!isset($params[$dao->name])) { - continue; - } - if ($dao->group_name == self::DIRECTORY_PREFERENCES_NAME) { - $dirParams[$dao->name] = CRM_Utils_Array::value($dao->name, $params, ''); - } - else { - $urlParams[$dao->name] = CRM_Utils_Array::value($dao->name, $params, ''); - } - unset($params[$dao->name]); - } - - if (!empty($dirParams)) { - self::storeDirectoryOrURLPreferences($dirParams, - self::DIRECTORY_PREFERENCES_NAME - ); - } - - if (!empty($urlParams)) { - self::storeDirectoryOrURLPreferences($urlParams, - self::URL_PREFERENCES_NAME - ); - } - } - - /** - * @param array $params - * @param $group - */ - public static function storeDirectoryOrURLPreferences(&$params, $group) { - foreach ($params as $name => $value) { - // always try to store relative directory or url from CMS root - $value = ($group == self::DIRECTORY_PREFERENCES_NAME) ? CRM_Utils_File::relativeDirectory($value) : CRM_Utils_System::relativeURL($value); - - self::setItem($value, $group, $name); - } - } - - /** - * @param array $params - * @param bool $setInConfig - */ - public static function retrieveDirectoryAndURLPreferences(&$params, $setInConfig = FALSE) { - if (CRM_Core_Config::isUpgradeMode()) { - $isJoomla = (defined('CIVICRM_UF') && CIVICRM_UF == 'Joomla') ? TRUE : FALSE; - // hack to set the resource base url so that js/ css etc is loaded correctly - if ($isJoomla) { - $params['userFrameworkResourceURL'] = CRM_Utils_File::addTrailingSlash(CIVICRM_UF_BASEURL, '/') . str_replace('administrator', '', CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'userFrameworkResourceURL', 'value', 'name')); - } - if (self::isUpgradeFromPreFourOneAlpha1()) { - return; - } - } - - if ($setInConfig) { - $config = CRM_Core_Config::singleton(); - } - - $sql = " -SELECT name, group_name, value -FROM civicrm_setting -WHERE ( group_name = %1 -OR group_name = %2 ) -AND domain_id = %3 -"; - $sqlParams = array( - 1 => array(self::DIRECTORY_PREFERENCES_NAME, 'String'), - 2 => array(self::URL_PREFERENCES_NAME, 'String'), - 3 => array(CRM_Core_Config::domainID(), 'Integer'), - ); - - $dao = CRM_Core_DAO::executeQuery($sql, - $sqlParams, - TRUE, - NULL, - FALSE, - TRUE, - // trap exceptions as error - TRUE - ); - - if (is_a($dao, 'DB_Error')) { - echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n"; - exit(); - } - - while ($dao->fetch()) { - $value = self::getOverride($dao->group_name, $dao->name, NULL); - if ($value === NULL && $dao->value) { - $value = unserialize($dao->value); - if ($dao->group_name == self::DIRECTORY_PREFERENCES_NAME) { - $value = CRM_Utils_File::absoluteDirectory($value); - } - else { - // CRM-7622: we need to remove the language part - $value = CRM_Utils_System::absoluteURL($value, TRUE); - } - } - // CRM-10931, If DB doesn't have any value, carry on with any default value thats already available - if (!isset($value) && !empty($params[$dao->name])) { - $value = $params[$dao->name]; - } - $params[$dao->name] = $value; - - if ($setInConfig) { - $config->{$dao->name} = $value; - } - } - } - /** * Determine what, if any, overrides have been provided * for a setting. diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 69e20b624e..89b2524dd3 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -269,6 +269,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page { 'positiveInteger', 'xssString', 'fileExists', + 'settingPath', 'autocomplete', 'validContact', ); diff --git a/CRM/Utils/Rule.php b/CRM/Utils/Rule.php index 201a580825..50a687e0ca 100644 --- a/CRM/Utils/Rule.php +++ b/CRM/Utils/Rule.php @@ -160,6 +160,22 @@ class CRM_Utils_Rule { return (bool) filter_var($url, FILTER_VALIDATE_URL); } + /** + * @param $url + * + * @return bool + */ + public static function urlish($url) { + if (empty($url)) { + return TRUE; + } + if (!preg_match('/^([a-z]+):/', $url)) { + // allow relative URL's (CRM-15598) + $url = 'http://' . $_SERVER['HTTP_HOST'] . '/' . ltrim($url, '/'); + } + return (bool) filter_var($url, FILTER_VALIDATE_URL); + } + /** * @param $string * @@ -707,6 +723,19 @@ class CRM_Utils_Rule { return file_exists($path); } + /** + * Determine whether the value contains a valid reference to a directory. + * + * Paths stored in the setting system may be absolute -- or may be + * relative to the default data directory. + * + * @param string $path + * @return bool + */ + public static function settingPath($path) { + return is_dir(\CRM_Utils_File::absoluteDirectory($path)); + } + /** * @param $value * @param $options diff --git a/settings/Url.setting.php b/settings/Url.setting.php index aacaa11440..c81fb99be2 100644 --- a/settings/Url.setting.php +++ b/settings/Url.setting.php @@ -50,7 +50,7 @@ return array( 'is_contact' => 0, 'description' => 'Absolute URL of the location where the civicrm module or component has been installed.', 'help_text' => NULL, - 'validate_callback' => 'CRM_Utils_Rule::url', + 'validate_callback' => 'CRM_Utils_Rule::urlish', ), 'imageUploadURL' => array( 'group' => 'url', @@ -66,7 +66,7 @@ return array( 'is_contact' => 0, 'description' => 'URL of the location for uploaded image files.', 'help_text' => NULL, - 'validate_callback' => 'CRM_Utils_Rule::url', + 'validate_callback' => 'CRM_Utils_Rule::urlish', ), 'customCSSURL' => array( 'group' => 'url', @@ -82,7 +82,7 @@ return array( 'is_contact' => 0, 'description' => 'You can modify the look and feel of CiviCRM by adding your own stylesheet. For small to medium sized modifications, use your css file to override some of the styles in civicrm.css. Or if you need to make drastic changes, you can choose to disable civicrm.css completely.', 'help_text' => NULL, - 'validate_callback' => 'CRM_Utils_Rule::url', + 'validate_callback' => 'CRM_Utils_Rule::urlish', ), 'extensionsURL' => array( 'group' => 'url', @@ -98,6 +98,6 @@ return array( 'is_contact' => 0, 'description' => 'Base URL for extension resources (images, stylesheets, etc). This should match extensionsDir.', 'help_text' => NULL, - 'validate_callback' => 'CRM_Utils_Rule::url', + 'validate_callback' => 'CRM_Utils_Rule::urlish', ), ); diff --git a/tests/phpunit/CRM/Core/BAO/SettingTest.php b/tests/phpunit/CRM/Core/BAO/SettingTest.php index 475f2995ed..87a1cee59e 100644 --- a/tests/phpunit/CRM/Core/BAO/SettingTest.php +++ b/tests/phpunit/CRM/Core/BAO/SettingTest.php @@ -110,19 +110,6 @@ class CRM_Core_BAO_SettingTest extends CiviUnitTestCase { $this->assertEquals('/test/override', $values['imageUploadDir']); } - /** - * Ensure that overrides in $civicrm_setting apply when - * when using retrieveDirectoryAndURLPreferences(). - */ - public function testRetrieveDirectoryAndURLPreferences_Override() { - global $civicrm_setting; - $civicrm_setting[CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME]['imageUploadDir'] = '/test/override'; - - $params = array(); - CRM_Core_BAO_Setting::retrieveDirectoryAndURLPreferences($params); - $this->assertEquals('/test/override', $params['imageUploadDir']); - } - /** * This test checks that CRM_Core_BAO_Setting::updateSettingsFromMetaData(); * 1) Removes 'maxAttachments' from config (because 'prefetch' is not set in the metadata it should @@ -176,7 +163,7 @@ class CRM_Core_BAO_SettingTest extends CiviUnitTestCase { /** * Ensure that overrides in $civicrm_setting apply when - * when using retrieveDirectoryAndURLPreferences(). + * when using getItem(). */ public function testConvertConfigToSettingNoPrefetch() { $settings = array('maxAttachments' => 6); diff --git a/tests/phpunit/api/v3/SettingTest.php b/tests/phpunit/api/v3/SettingTest.php index 27bc07a814..7b5dae352f 100644 --- a/tests/phpunit/api/v3/SettingTest.php +++ b/tests/phpunit/api/v3/SettingTest.php @@ -244,7 +244,7 @@ class api_v3_SettingTest extends CiviUnitTestCase { public function testCreateInvalidURLSettings() { $params = array( 'domain_id' => $this->_domainID2, - 'userFrameworkResourceURL' => 'dfhkdhfd', + 'userFrameworkResourceURL' => 'dfhkd*hfd', ); $result = $this->callAPIFailure('setting', 'create', $params); $params = array( -- 2.25.1