@@ -224,6 +224,36 @@ public function addText($name, $label, $required = true, $attributes = [], $crea
224224 return $ element ;
225225 }
226226
227+ /**
228+ * Adds a text field to the form to be used as internal url (URL without the domain part).
229+ * A trim-filter is attached to the field.
230+ *
231+ * @param string|array $label The label for the form-element
232+ * @param string $name The element name
233+ * @param bool $required (optional) Is the form-element required (default=true)
234+ * @param array $attributes (optional) List of attributes for the form-element
235+ *
236+ * @return HTML_QuickForm_text
237+ */
238+ public function addInternalUrl ($ name , $ label , $ required = true , $ attributes = [], $ createElement = false )
239+ {
240+ if ($ createElement ) {
241+ $ element = $ this ->createElement ('text ' , $ name , $ label , $ attributes );
242+ } else {
243+ $ element = $ this ->addElement ('text ' , $ name , $ label , $ attributes );
244+ }
245+
246+ $ this ->applyFilter ($ name , 'trim ' );
247+ $ this ->applyFilter ($ name , 'plain_url_filter ' );
248+ $ this ->addRule ($ name , get_lang ('InsertAValidUrl ' ), 'internal_url ' );
249+
250+ if ($ required ) {
251+ $ this ->addRule ($ name , get_lang ('ThisFieldIsRequired ' ), 'required ' );
252+ }
253+
254+ return $ element ;
255+ }
256+
227257 /**
228258 * Add hidden course params.
229259 */
@@ -1268,6 +1298,7 @@ public function addUrl($name, $label, $required = true, $attributes = [])
12681298 {
12691299 $ this ->addElement ('url ' , $ name , $ label , $ attributes );
12701300 $ this ->applyFilter ($ name , 'trim ' );
1301+
12711302 $ this ->addRule ($ name , get_lang ('InsertAValidUrl ' ), 'url ' );
12721303
12731304 if ($ required ) {
@@ -2048,3 +2079,20 @@ function mobile_phone_number_filter($mobilePhoneNumber)
20482079
20492080 return ltrim ($ mobilePhoneNumber , '0 ' );
20502081}
2082+
2083+ /**
2084+ * Cleans JS from a URL.
2085+ *
2086+ * @param string $html URL to clean
2087+ * @param int $mode (optional)
2088+ *
2089+ * @return string The cleaned URL
2090+ */
2091+ function plain_url_filter ($ html , $ mode = NO_HTML )
2092+ {
2093+ $ allowed_tags = HTML_QuickForm_Rule_HTML::get_allowed_tags ($ mode );
2094+ $ html = kses_no_null ($ html );
2095+ $ html = kses_js_entities ($ html );
2096+ $ allowed_html_fixed = kses_array_lc ($ allowed_tags );
2097+ return kses_split ($ html , $ allowed_html_fixed , array ('http ' , 'https ' ));
2098+ }
0 commit comments