| +-----------------------------------------------------------------------+ */ /** * Class for HTML code creation */ class html { protected $tagname; protected $content; protected $attrib = []; protected $allowed = []; public static $doctype = 'xhtml'; public static $lc_tags = true; public static $common_attrib = ['id', 'class', 'style', 'title', 'align', 'unselectable', 'tabindex', 'role']; public static $containers = ['iframe', 'div', 'span', 'p', 'h1', 'h2', 'h3', 'ul', 'form', 'textarea', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'style', 'script', 'a']; public static $bool_attrib = ['checked', 'multiple', 'disabled', 'selected', 'autofocus', 'readonly', 'required']; /** * Constructor * * @param array $attrib Hash array with tag attributes */ public function __construct($attrib = []) { // @phpstan-ignore-next-line if (is_array($attrib)) { $this->attrib = $attrib; } } /** * Return the tag code * * @return string The finally composed HTML tag */ public function show() { return self::tag($this->tagname, $this->attrib, $this->content, array_merge(self::$common_attrib, $this->allowed)); } // STATIC METHODS /** * Generic method to create a HTML tag * * @param string $tagname Tag name * @param array|string $attrib Tag attributes as key/value pairs, or 'class' attribute value * @param array|string $content Optional Tag content (creates a container tag) * @param array $allowed List with allowed attributes, omit to allow all * * @return string The XHTML tag */ public static function tag($tagname, $attrib = [], $content = null, $allowed = null) { if (is_string($attrib)) { $attrib = ['class' => $attrib]; } $inline_tags = ['a', 'span', 'img']; $suffix = (isset($attrib['nl']) && $content && $attrib['nl'] && !in_array($tagname, $inline_tags)) ? "\n" : ''; $tagname = self::$lc_tags ? strtolower($tagname) : $tagname; if (isset($content) || in_array($tagname, self::$containers)) { $suffix = !empty($attrib['noclose']) ? $suffix : '' . $tagname . '>' . $suffix; unset($attrib['noclose'], $attrib['nl']); if (is_array($content)) { $content = implode('', $content); } return '<' . $tagname . self::attrib_string($attrib, $allowed) . '>' . $content . $suffix; } return '<' . $tagname . self::attrib_string($attrib, $allowed) . '>' . $suffix; } /** * Return DOCTYPE tag of specified type * * @param string $type Document type (html5, xhtml, 'xhtml-trans, xhtml-strict) */ public static function doctype($type) { $doctypes = [ 'html5' => '', 'xhtml' => '', 'xhtml-trans' => '', 'xhtml-strict' => '', ]; if (!empty($doctypes[$type])) { self::$doctype = preg_replace('/-\w+$/', '', $type); return $doctypes[$type]; } return ''; } /** * Derived method for
blocks
*
* @param mixed $attr Hash array with tag attributes or string with class name
* @param string $cont Paragraph content
*
* @return string HTML code
*
* @see html::tag()
*/
public static function p($attr = null, $cont = null)
{
if (is_string($attr)) {
$attr = ['class' => $attr];
}
return self::tag('p', $attr, $cont, self::$common_attrib);
}
/**
* Derived method to create
*
* @param string|array $attr Hash array with tag attributes or string with image source (src)
*
* @return string HTML code
*
* @see html::tag()
*/
public static function img($attr = null)
{
if (is_string($attr)) {
$attr = ['src' => $attr];
}
$allowed = ['src', 'alt', 'width', 'height', 'border', 'usemap', 'onclick', 'onerror', 'onload'];
return self::tag('img', $attr + ['alt' => ''], null, array_merge(self::$common_attrib, $allowed));
}
/**
* Derived method for link tags
*
* @param string|array $attr Hash array with tag attributes or string with link location (href)
* @param string $cont Link content
*
* @return string HTML code
*
* @see html::tag()
*/
public static function a($attr, $cont)
{
if (is_string($attr)) {
$attr = ['href' => $attr];
}
$allowed = ['href', 'target', 'name', 'rel', 'onclick', 'onmouseover', 'onmouseout', 'onmousedown', 'onmouseup'];
return self::tag('a', $attr, $cont, array_merge(self::$common_attrib, $allowed));
}
/**
* Derived method for inline span tags
*
* @param string|array $attr Hash array with tag attributes or string with class name
* @param string $cont Tag content
*
* @return string HTML code
*
* @see html::tag()
*/
public static function span($attr, $cont)
{
if (is_string($attr)) {
$attr = ['class' => $attr];
}
return self::tag('span', $attr, $cont, self::$common_attrib);
}
/**
* Derived method for form element labels
*
* @param string|array $attr Hash array with tag attributes or string with 'for' attrib
* @param string $cont Tag content
*
* @return string HTML code
*
* @see html::tag()
*/
public static function label($attr, $cont)
{
if (is_string($attr)) {
$attr = ['for' => $attr];
}
return self::tag('label', $attr, $cont, array_merge(self::$common_attrib, ['for', 'onkeypress']));
}
/**
* Derived method to create
*
* @param string|array $attr Hash array with tag attributes or string with frame source (src)
* @param string $cont Tag content
*
* @return string HTML code
*
* @see html::tag()
*/
public static function iframe($attr = null, $cont = null)
{
if (is_string($attr)) {
$attr = ['src' => $attr];
}
$allowed = ['src', 'name', 'width', 'height', 'border', 'frameborder', 'onload', 'allowfullscreen', 'sandbox'];
return self::tag('iframe', $attr, $cont, array_merge(self::$common_attrib, $allowed));
}
/**
* Derived method to create