element */ function script(string $source, string $trailing = "\n"): string { return "$source$trailing"; } /** Return \n"; } /** Get a nonce="" attribute with CSP nonce */ function nonce(): string { return ' nonce="' . get_nonce() . '"'; } /** Get * @param string|int $value * @return string HTML */ function input_hidden(string $name, $value = ""): string { return "\n"; } /** Get CSRF * @return string HTML */ function input_token(): string { return input_hidden("token", get_token()); } /** Get a target="_blank" attribute */ function target_blank(): string { return ' target="_blank" rel="noreferrer noopener"'; } /** Escape for HTML */ function h(?string $string): string { return str_replace("\0", "�", htmlspecialchars($string, ENT_QUOTES, 'utf-8')); } /** Convert \n to
*/ function nl_br(string $string): string { return str_replace("\n", "
", $string); // nl2br() uses XHTML before PHP 5.3 } /** Generate HTML checkbox * @param string|int $value */ function checkbox(string $name, $value, ?bool $checked, string $label = "", string $onclick = "", string $class = "", string $labelled_by = ""): string { $return = "" . ($onclick ? script("qsl('input').onclick = function () { $onclick };", "") : "") ; return ($label != "" || $class ? "$return" . h($label) . "" : $return); } /** Generate list of HTML options * @param string[]|string[][] $options array of strings or arrays (creates optgroup) * @param mixed $selected * @param bool $use_keys always use array keys for value="", otherwise only string keys are used */ function optionlist($options, $selected = null, bool $use_keys = false): string { $return = ""; foreach ($options as $k => $v) { $opts = array($k => $v); if (is_array($v)) { $return .= ''; $opts = $v; } foreach ($opts as $key => $val) { $return .= '' . h($val) ; } if (is_array($v)) { $return .= ''; } } return $return; } /** Generate HTML " . $label_option . optionlist($options, $value) . "" . ($onchange ? script("qsl('select').onchange = function () { $onchange };", "") : "") ; } /** Generate HTML radio list * @param string[] $options */ function html_radios(string $name, array $options, ?string $value = "", string $separator = ""): string { $return = ""; foreach ($options as $key => $val) { $return .= "$separator"; } return $return; } /** Get onclick confirmation */ function confirm(string $message = "", string $selector = "qsl('input')"): string { return script("$selector.onclick = () => confirm('" . ($message ? js_escape($message) : lang('Are you sure?')) . "');", ""); } /** Print header for hidden fieldset (close by ) * @param bool $visible */ function print_fieldset(string $id, string $legend, $visible = false): void { echo "
"; echo "$legend"; echo script("qsl('a').onclick = partial(toggle, 'fieldset-$id');", ""); echo ""; echo "