diff --git a/classes/admin_setting_configurl.php b/classes/admin_setting_configurl.php new file mode 100644 index 0000000..219b1aa --- /dev/null +++ b/classes/admin_setting_configurl.php @@ -0,0 +1,67 @@ +. + +/** + * Elasticsearch engine. + * + * Provides an interface between Moodles Global search functionality + * and the Elasticsearch (https://www.elastic.co/products/elasticsearch) + * search engine. + * + * Elasticsearch presents a REST Webservice API that we communicate with + * via Curl. + * + * @package search_elastic + * @copyright Matt Porritt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace search_elastic; + +/** + * Elasticsearch settings. + * + * @package search_elastic + * @copyright 2023 Lars Thoms + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class admin_setting_configurl extends \admin_setting_configtext { + + /** + * Unlike `PARAM_URL`, HTTP BasicAuth components are allowed. + * It must also be an HTTP scheme, and port or path specifications + * are not allowed. + * + * @param $data + * @return \lang_string|mixed|string|true + * @throws \coding_exception + */ + public function validate($data) { + if (self::validateURL($data)) { + return true; + } else { + return get_string('validateerror', 'admin'); + } + } + + static public function validateURL($data) { + global $CFG; + + $data = (string)fix_utf8($data); + include_once($CFG->dirroot . '/lib/validateurlsyntax.php'); + return (!empty($data) && validateUrlSyntax($data, 's+H?S?F-E-u?P?a+I?p-f-q-r-')); + } +} diff --git a/classes/enrich/text/tika.php b/classes/enrich/text/tika.php index 4bce03d..ce163af 100644 --- a/classes/enrich/text/tika.php +++ b/classes/enrich/text/tika.php @@ -195,7 +195,7 @@ public function analyze_file($file) { */ public static function form_definition_extra($form, $mform, $customdata, $config) { $mform->addElement('text', 'tikahostname', get_string ('tikahostname', 'search_elastic')); - $mform->setType('tikahostname', PARAM_URL); + $mform->setType('tikahostname', PARAM_TEXT); $mform->addHelpButton('tikahostname', 'tikahostname', 'search_elastic'); self::set_default('tikahostname', 'http://127.0.0.1', $mform, $customdata, $config); diff --git a/classes/enrich_form.php b/classes/enrich_form.php index f5b3c17..f5f9bfb 100644 --- a/classes/enrich_form.php +++ b/classes/enrich_form.php @@ -199,4 +199,22 @@ public function definition() { $this->add_action_buttons(); } + /** + * Unlike `PARAM_URL`, HTTP BasicAuth components are allowed. + * It must also be an HTTP scheme, and port or path specifications + * are not allowed. + * + * @param $data + * @param $files + * @return array + */ + function validation($data, $files) + { + $errors = parent::validation($data, $files); + if (!admin_setting_configurl::validateURL($data['tikahostname'])) { + $errors['tikahostname'] = 'Error - incorrect URL'; + } + return $errors; + } + } diff --git a/settings.php b/settings.php index 0e866c2..a661de2 100644 --- a/settings.php +++ b/settings.php @@ -23,6 +23,7 @@ */ use search_elastic\admin_setting_check; +use search_elastic\admin_setting_configurl; use search_elastic\check\server_ready_check; defined('MOODLE_INTERNAL') || die(); @@ -40,8 +41,8 @@ new server_ready_check())); } - $settings->add(new admin_setting_configtext('search_elastic/hostname', get_string ('hostname', 'search_elastic'), - get_string ('hostname_help', 'search_elastic'), '', PARAM_URL)); + $settings->add(new admin_setting_configurl('search_elastic/hostname', get_string ('hostname', 'search_elastic'), + get_string ('hostname_help', 'search_elastic'), '')); $settings->add(new admin_setting_configtext('search_elastic/port', get_string ('port', 'search_elastic'), get_string ('port_help', 'search_elastic'), 9200, PARAM_INT));