Commit d9d7e7dd authored by totten's avatar totten

CRM-16373 - Save URLs and paths through settings instead of domain

parent 86bfd7a8
...@@ -36,11 +36,21 @@ ...@@ -36,11 +36,21 @@
*/ */
class CRM_Admin_Form_Setting_Path extends CRM_Admin_Form_Setting { 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. * Build the form object.
*/ */
public function buildQuickForm() { public function buildQuickForm() {
CRM_Utils_System::setTitle(ts('Settings - Upload Directories')); CRM_Utils_System::setTitle(ts('Settings - Upload Directories'));
parent::buildQuickForm();
$directories = array( $directories = array(
'uploadDir' => ts('Temporary Files'), 'uploadDir' => ts('Temporary Files'),
...@@ -51,16 +61,15 @@ class CRM_Admin_Form_Setting_Path extends CRM_Admin_Form_Setting { ...@@ -51,16 +61,15 @@ class CRM_Admin_Form_Setting_Path extends CRM_Admin_Form_Setting {
'extensionsDir' => ts('CiviCRM Extensions Directory'), 'extensionsDir' => ts('CiviCRM Extensions Directory'),
); );
foreach ($directories as $name => $title) { foreach ($directories as $name => $title) {
$this->add('text', $name, $title); //$this->add('text', $name, $title);
$this->addRule($name, $this->addRule($name,
ts("'%1' directory does not exist", ts("'%1' directory does not exist",
array(1 => $title) array(1 => $title)
), ),
'fileExists' 'settingPath'
); );
} }
parent::buildQuickForm();
} }
public function postProcess() { public function postProcess() {
......
...@@ -37,6 +37,10 @@ ...@@ -37,6 +37,10 @@
class CRM_Admin_Form_Setting_Url extends CRM_Admin_Form_Setting { class CRM_Admin_Form_Setting_Url extends CRM_Admin_Form_Setting {
protected $_settings = array( protected $_settings = array(
'disable_core_css' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, '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 { ...@@ -48,10 +52,6 @@ class CRM_Admin_Form_Setting_Url extends CRM_Admin_Form_Setting {
'version' => 3, '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('enableSSL', ts('Force Secure URLs (SSL)'));
$this->addYesNo('verifySSL', ts('Verify SSL Certs')); $this->addYesNo('verifySSL', ts('Verify SSL Certs'));
// FIXME: verifySSL should use $_settings instead of manually adding fields // FIXME: verifySSL should use $_settings instead of manually adding fields
......
...@@ -84,8 +84,6 @@ class CRM_Core_BAO_ConfigSetting { ...@@ -84,8 +84,6 @@ class CRM_Core_BAO_ConfigSetting {
unset($params[$var]); unset($params[$var]);
} }
CRM_Core_BAO_Setting::fixAndStoreDirAndURL($params);
// also skip all Dir Params, we dont need to store those in the DB! // also skip all Dir Params, we dont need to store those in the DB!
foreach ($params as $name => $val) { foreach ($params as $name => $val) {
if (substr($name, -3) == 'Dir') { if (substr($name, -3) == 'Dir') {
...@@ -315,9 +313,6 @@ class CRM_Core_BAO_ConfigSetting { ...@@ -315,9 +313,6 @@ class CRM_Core_BAO_ConfigSetting {
// dont add if its empty // dont add if its empty
if (!empty($defaults)) { if (!empty($defaults)) {
// retrieve directory and url preferences also
CRM_Core_BAO_Setting::retrieveDirectoryAndURLPreferences($defaults);
// Pickup enabled-components from settings table if found. // Pickup enabled-components from settings table if found.
$enableComponents = Civi::settings()->get('enable_components'); $enableComponents = Civi::settings()->get('enable_components');
if (!empty($enableComponents)) { if (!empty($enableComponents)) {
......
...@@ -904,166 +904,6 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { ...@@ -904,166 +904,6 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting {
self::setItem($optionValue, $group, $name); 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 * Determine what, if any, overrides have been provided
* for a setting. * for a setting.
......
...@@ -269,6 +269,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page { ...@@ -269,6 +269,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
'positiveInteger', 'positiveInteger',
'xssString', 'xssString',
'fileExists', 'fileExists',
'settingPath',
'autocomplete', 'autocomplete',
'validContact', 'validContact',
); );
......
...@@ -160,6 +160,22 @@ class CRM_Utils_Rule { ...@@ -160,6 +160,22 @@ class CRM_Utils_Rule {
return (bool) filter_var($url, FILTER_VALIDATE_URL); 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 * @param $string
* *
...@@ -707,6 +723,19 @@ class CRM_Utils_Rule { ...@@ -707,6 +723,19 @@ class CRM_Utils_Rule {
return file_exists($path); 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 $value
* @param $options * @param $options
......
...@@ -50,7 +50,7 @@ return array( ...@@ -50,7 +50,7 @@ return array(
'is_contact' => 0, 'is_contact' => 0,
'description' => 'Absolute URL of the location where the civicrm module or component has been installed.', 'description' => 'Absolute URL of the location where the civicrm module or component has been installed.',
'help_text' => NULL, 'help_text' => NULL,
'validate_callback' => 'CRM_Utils_Rule::url', 'validate_callback' => 'CRM_Utils_Rule::urlish',
), ),
'imageUploadURL' => array( 'imageUploadURL' => array(
'group' => 'url', 'group' => 'url',
...@@ -66,7 +66,7 @@ return array( ...@@ -66,7 +66,7 @@ return array(
'is_contact' => 0, 'is_contact' => 0,
'description' => 'URL of the location for uploaded image files.', 'description' => 'URL of the location for uploaded image files.',
'help_text' => NULL, 'help_text' => NULL,
'validate_callback' => 'CRM_Utils_Rule::url', 'validate_callback' => 'CRM_Utils_Rule::urlish',
), ),
'customCSSURL' => array( 'customCSSURL' => array(
'group' => 'url', 'group' => 'url',
...@@ -82,7 +82,7 @@ return array( ...@@ -82,7 +82,7 @@ return array(
'is_contact' => 0, '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.', '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, 'help_text' => NULL,
'validate_callback' => 'CRM_Utils_Rule::url', 'validate_callback' => 'CRM_Utils_Rule::urlish',
), ),
'extensionsURL' => array( 'extensionsURL' => array(
'group' => 'url', 'group' => 'url',
...@@ -98,6 +98,6 @@ return array( ...@@ -98,6 +98,6 @@ return array(
'is_contact' => 0, 'is_contact' => 0,
'description' => 'Base URL for extension resources (images, stylesheets, etc). This should match extensionsDir.', 'description' => 'Base URL for extension resources (images, stylesheets, etc). This should match extensionsDir.',
'help_text' => NULL, 'help_text' => NULL,
'validate_callback' => 'CRM_Utils_Rule::url', 'validate_callback' => 'CRM_Utils_Rule::urlish',
), ),
); );
...@@ -110,19 +110,6 @@ class CRM_Core_BAO_SettingTest extends CiviUnitTestCase { ...@@ -110,19 +110,6 @@ class CRM_Core_BAO_SettingTest extends CiviUnitTestCase {
$this->assertEquals('/test/override', $values['imageUploadDir']); $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(); * This test checks that CRM_Core_BAO_Setting::updateSettingsFromMetaData();
* 1) Removes 'maxAttachments' from config (because 'prefetch' is not set in the metadata it should * 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 { ...@@ -176,7 +163,7 @@ class CRM_Core_BAO_SettingTest extends CiviUnitTestCase {
/** /**
* Ensure that overrides in $civicrm_setting apply when * Ensure that overrides in $civicrm_setting apply when
* when using retrieveDirectoryAndURLPreferences(). * when using getItem().
*/ */
public function testConvertConfigToSettingNoPrefetch() { public function testConvertConfigToSettingNoPrefetch() {
$settings = array('maxAttachments' => 6); $settings = array('maxAttachments' => 6);
......
...@@ -244,7 +244,7 @@ class api_v3_SettingTest extends CiviUnitTestCase { ...@@ -244,7 +244,7 @@ class api_v3_SettingTest extends CiviUnitTestCase {
public function testCreateInvalidURLSettings() { public function testCreateInvalidURLSettings() {
$params = array( $params = array(
'domain_id' => $this->_domainID2, 'domain_id' => $this->_domainID2,
'userFrameworkResourceURL' => 'dfhkdhfd', 'userFrameworkResourceURL' => 'dfhkd*hfd',
); );
$result = $this->callAPIFailure('setting', 'create', $params); $result = $this->callAPIFailure('setting', 'create', $params);
$params = array( $params = array(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment