CodeIgniter4/system/Helpers/html_helper.php

544 lines
16 KiB
PHP
Raw Normal View History

2016-11-07 17:41:46 +08:00
<?php
2020-07-29 01:47:24 +08:00
2016-11-07 17:41:46 +08:00
/**
2021-07-19 21:32:33 +08:00
* This file is part of CodeIgniter 4 framework.
2016-11-07 17:41:46 +08:00
*
2020-10-24 16:38:41 +08:00
* (c) CodeIgniter Foundation <admin@codeigniter.com>
2016-11-07 17:41:46 +08:00
*
2021-07-19 21:32:33 +08:00
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
2016-11-07 17:41:46 +08:00
*/
2018-11-10 02:57:39 -02:00
2020-10-07 17:48:51 +00:00
use CodeIgniter\Files\Exceptions\FileNotFoundException;
2020-10-04 00:27:56 +07:00
use Config\DocTypes;
2020-10-07 17:48:51 +00:00
use Config\Mimes;
// CodeIgniter HTML Helpers
2021-06-07 19:06:26 +08:00
if (! function_exists('ul')) {
2021-06-04 22:51:52 +08:00
/**
* Unordered List
*
* Generates an HTML unordered list from an single or
* multi-dimensional array.
*
* @param mixed $attributes HTML attributes string, array, object
*/
function ul(array $list, $attributes = ''): string
{
return _list('ul', $list, $attributes);
}
2016-11-07 17:41:46 +08:00
}
2021-06-07 19:06:26 +08:00
if (! function_exists('ol')) {
2021-06-04 22:51:52 +08:00
/**
* Ordered List
*
* Generates an HTML ordered list from an single or multi-dimensional array.
*
* @param mixed $attributes HTML attributes string, array, object
*/
function ol(array $list, $attributes = ''): string
{
return _list('ol', $list, $attributes);
}
2016-11-07 17:41:46 +08:00
}
2021-06-07 19:06:26 +08:00
if (! function_exists('_list')) {
2021-06-04 22:51:52 +08:00
/**
* Generates the list
*
* Generates an HTML ordered list from an single or multi-dimensional array.
*
2021-07-24 19:30:51 +08:00
* @param mixed $list
* @param mixed $attributes string, array, object
2021-06-04 22:51:52 +08:00
*/
function _list(string $type = 'ul', $list = [], $attributes = '', int $depth = 0): string
{
// Set the indentation based on the depth
$out = str_repeat(' ', $depth)
// Write the opening list tag
. '<' . $type . stringify_attributes($attributes) . ">\n";
// Cycle through the list elements. If an array is
// encountered we will recursively call _list()
2021-06-07 19:06:26 +08:00
foreach ($list as $key => $val) {
2021-06-04 22:51:52 +08:00
$out .= str_repeat(' ', $depth + 2) . '<li>';
2021-06-07 19:06:26 +08:00
if (! is_array($val)) {
2021-06-04 22:51:52 +08:00
$out .= $val;
2021-06-07 19:06:26 +08:00
} else {
2021-06-07 03:26:46 +02:00
$out .= $key
2021-06-04 22:51:52 +08:00
. "\n"
. _list($type, $val, '', $depth + 4)
. str_repeat(' ', $depth + 2);
}
$out .= "</li>\n";
}
// Set the indentation for the closing tag and apply it
return $out . str_repeat(' ', $depth) . '</' . $type . ">\n";
}
2016-11-07 17:41:46 +08:00
}
2021-06-07 19:06:26 +08:00
if (! function_exists('img')) {
2021-06-04 22:51:52 +08:00
/**
* Image
*
* Generates an image element
*
2021-06-11 23:46:56 +08:00
* @param array|string $src Image source URI, or array of attributes and values
2021-06-08 12:11:23 +08:00
* @param bool $indexPage Whether to treat $src as a routed URI string
2021-06-11 23:46:56 +08:00
* @param array|object|string $attributes Additional HTML attributes
2021-06-04 22:51:52 +08:00
*/
function img($src = '', bool $indexPage = false, $attributes = ''): string
{
2021-06-07 19:06:26 +08:00
if (! is_array($src)) {
2021-06-04 22:51:52 +08:00
$src = ['src' => $src];
}
2021-06-07 19:06:26 +08:00
if (! isset($src['src'])) {
2021-06-04 22:51:52 +08:00
$src['src'] = $attributes['src'] ?? '';
}
2021-06-07 19:06:26 +08:00
if (! isset($src['alt'])) {
2021-06-04 22:51:52 +08:00
$src['alt'] = $attributes['alt'] ?? '';
}
$img = '<img';
// Check for a relative URI
2021-06-07 19:06:26 +08:00
if (! preg_match('#^([a-z]+:)?//#i', $src['src']) && strpos($src['src'], 'data:') !== 0) {
if ($indexPage === true) {
2021-06-04 22:51:52 +08:00
$img .= ' src="' . site_url($src['src']) . '"';
2021-06-07 19:06:26 +08:00
} else {
2021-06-04 22:51:52 +08:00
$img .= ' src="' . slash_item('baseURL') . $src['src'] . '"';
}
unset($src['src']);
}
// Append any other values
2021-06-07 19:06:26 +08:00
foreach ($src as $key => $value) {
2021-06-04 22:51:52 +08:00
$img .= ' ' . $key . '="' . $value . '"';
}
// Prevent passing completed values to stringify_attributes
2021-06-07 19:06:26 +08:00
if (is_array($attributes)) {
2021-06-04 22:51:52 +08:00
unset($attributes['alt'], $attributes['src']);
}
return $img . stringify_attributes($attributes) . ' />';
}
2016-11-07 17:41:46 +08:00
}
2021-06-07 19:06:26 +08:00
if (! function_exists('img_data')) {
2021-06-04 22:51:52 +08:00
/**
* Image (data)
*
* Generates a src-ready string from an image using the "data:" protocol
*
* @param string $path Image source path
* @param string|null $mime MIME type to use, or null to guess
*/
2021-07-09 23:13:08 +08:00
function img_data(string $path, ?string $mime = null): string
2021-06-04 22:51:52 +08:00
{
2021-06-07 19:06:26 +08:00
if (! is_file($path) || ! is_readable($path)) {
2021-06-04 22:51:52 +08:00
throw FileNotFoundException::forFileNotFound($path);
}
// Read in file binary data
$handle = fopen($path, 'rb');
$data = fread($handle, filesize($path));
fclose($handle);
// Encode as base64
$data = base64_encode($data);
// Figure out the type (Hail Mary to JPEG)
$mime ??= Mimes::guessTypeFromExtension(pathinfo($path, PATHINFO_EXTENSION)) ?? 'image/jpg';
2021-06-04 22:51:52 +08:00
return 'data:' . $mime . ';base64,' . $data;
}
2020-10-07 17:48:51 +00:00
}
2021-06-07 19:06:26 +08:00
if (! function_exists('doctype')) {
2021-06-04 22:51:52 +08:00
/**
* Doctype
*
* Generates a page document type declaration
*
* Examples of valid options: html5, xhtml-11, xhtml-strict, xhtml-trans,
* xhtml-frame, html4-strict, html4-trans, and html4-frame.
* All values are saved in the doctypes config file.
*
* @param string $type The doctype to be generated
*/
function doctype(string $type = 'html5'): string
{
$config = new DocTypes();
$doctypes = $config->list;
2021-06-04 22:51:52 +08:00
return $doctypes[$type] ?? false;
}
2016-11-07 17:41:46 +08:00
}
2021-06-07 19:06:26 +08:00
if (! function_exists('script_tag')) {
2021-06-04 22:51:52 +08:00
/**
* Script
*
* Generates link to a JS file
*
2021-06-08 12:11:23 +08:00
* @param mixed $src Script source or an array
* @param bool $indexPage Should indexPage be added to the JS path
2021-06-04 22:51:52 +08:00
*/
function script_tag($src = '', bool $indexPage = false): string
{
$script = '<script ';
2021-06-07 19:06:26 +08:00
if (! is_array($src)) {
2021-06-04 22:51:52 +08:00
$src = ['src' => $src];
}
2021-06-07 19:06:26 +08:00
foreach ($src as $k => $v) {
if ($k === 'src' && ! preg_match('#^([a-z]+:)?//#i', $v)) {
if ($indexPage === true) {
2021-06-04 22:51:52 +08:00
$script .= 'src="' . site_url($v) . '" ';
2021-06-07 19:06:26 +08:00
} else {
2021-06-04 22:51:52 +08:00
$script .= 'src="' . slash_item('baseURL') . $v . '" ';
}
2021-06-07 19:06:26 +08:00
} else {
// for attributes without values, like async or defer, use NULL.
$script .= $k . (is_null($v) ? ' ' : '="' . $v . '" ');
2021-06-04 22:51:52 +08:00
}
}
return $script . 'type="text/javascript"></script>';
2021-06-04 22:51:52 +08:00
}
2016-11-07 17:41:46 +08:00
}
2021-06-07 19:06:26 +08:00
if (! function_exists('link_tag')) {
2021-06-04 22:51:52 +08:00
/**
* Link
*
* Generates link to a CSS file
*
2021-07-24 19:30:51 +08:00
* @param mixed $href Stylesheet href or an array
* @param bool $indexPage should indexPage be added to the CSS path.
2021-06-04 22:51:52 +08:00
*/
function link_tag($href = '', string $rel = 'stylesheet', string $type = 'text/css', string $title = '', string $media = '', bool $indexPage = false, string $hreflang = ''): string
{
$link = '<link ';
// extract fields if needed
2021-06-07 19:06:26 +08:00
if (is_array($href)) {
2021-06-04 22:51:52 +08:00
$rel = $href['rel'] ?? $rel;
$type = $href['type'] ?? $type;
$title = $href['title'] ?? $title;
$media = $href['media'] ?? $media;
$hreflang = $href['hreflang'] ?? '';
$indexPage = $href['indexPage'] ?? $indexPage;
$href = $href['href'] ?? '';
}
2021-06-07 19:06:26 +08:00
if (! preg_match('#^([a-z]+:)?//#i', $href)) {
if ($indexPage === true) {
2021-06-04 22:51:52 +08:00
$link .= 'href="' . site_url($href) . '" ';
2021-06-07 19:06:26 +08:00
} else {
2021-06-04 22:51:52 +08:00
$link .= 'href="' . slash_item('baseURL') . $href . '" ';
}
2021-06-07 19:06:26 +08:00
} else {
2021-06-04 22:51:52 +08:00
$link .= 'href="' . $href . '" ';
}
2021-06-07 19:06:26 +08:00
if ($hreflang !== '') {
2021-06-04 22:51:52 +08:00
$link .= 'hreflang="' . $hreflang . '" ';
}
$link .= 'rel="' . $rel . '" ';
2021-06-07 19:06:26 +08:00
if (! in_array($rel, ['alternate', 'canonical'], true)) {
2021-06-04 22:51:52 +08:00
$link .= 'type="' . $type . '" ';
}
2021-06-07 19:06:26 +08:00
if ($media !== '') {
2021-06-04 22:51:52 +08:00
$link .= 'media="' . $media . '" ';
}
2021-06-07 19:06:26 +08:00
if ($title !== '') {
2021-06-04 22:51:52 +08:00
$link .= 'title="' . $title . '" ';
}
return $link . '/>';
}
}
2021-06-07 19:06:26 +08:00
if (! function_exists('video')) {
2021-06-04 22:51:52 +08:00
/**
* Video
*
* Generates a video element to embed videos. The video element can
* contain one or more video sources
*
2021-06-08 12:11:23 +08:00
* @param mixed $src Either a source string or an array of sources
* @param string $unsupportedMessage The message to display if the media tag is not supported by the browser
* @param string $attributes HTML attributes
2021-06-04 22:51:52 +08:00
*/
function video($src, string $unsupportedMessage = '', string $attributes = '', array $tracks = [], bool $indexPage = false): string
{
2021-06-07 19:06:26 +08:00
if (is_array($src)) {
2021-06-04 22:51:52 +08:00
return _media('video', $src, $unsupportedMessage, $attributes, $tracks);
}
$video = '<video';
2021-06-07 19:06:26 +08:00
if (_has_protocol($src)) {
2021-06-04 22:51:52 +08:00
$video .= ' src="' . $src . '"';
2021-06-07 19:06:26 +08:00
} elseif ($indexPage === true) {
2021-06-04 22:51:52 +08:00
$video .= ' src="' . site_url($src) . '"';
2021-06-07 19:06:26 +08:00
} else {
2021-06-04 22:51:52 +08:00
$video .= ' src="' . slash_item('baseURL') . $src . '"';
}
2021-06-07 19:06:26 +08:00
if ($attributes !== '') {
2021-06-04 22:51:52 +08:00
$video .= ' ' . $attributes;
}
$video .= ">\n";
foreach ($tracks as $track) {
$video .= _space_indent() . $track . "\n";
2021-06-04 22:51:52 +08:00
}
2021-06-07 19:06:26 +08:00
if (! empty($unsupportedMessage)) {
2021-06-04 22:51:52 +08:00
$video .= _space_indent()
. $unsupportedMessage
. "\n";
}
return $video . "</video>\n";
}
}
2021-06-07 19:06:26 +08:00
if (! function_exists('audio')) {
2021-06-04 22:51:52 +08:00
/**
* Audio
*
* Generates an audio element to embed sounds
*
2021-06-08 12:11:23 +08:00
* @param mixed $src Either a source string or an array of sources
* @param string $unsupportedMessage The message to display if the media tag is not supported by the browser.
* @param string $attributes HTML attributes
2021-06-04 22:51:52 +08:00
*/
function audio($src, string $unsupportedMessage = '', string $attributes = '', array $tracks = [], bool $indexPage = false): string
{
2021-06-07 19:06:26 +08:00
if (is_array($src)) {
2021-06-04 22:51:52 +08:00
return _media('audio', $src, $unsupportedMessage, $attributes, $tracks);
}
$audio = '<audio';
2021-06-07 19:06:26 +08:00
if (_has_protocol($src)) {
2021-06-04 22:51:52 +08:00
$audio .= ' src="' . $src . '"';
2021-06-07 19:06:26 +08:00
} elseif ($indexPage === true) {
2021-06-04 22:51:52 +08:00
$audio .= ' src="' . site_url($src) . '"';
2021-06-07 19:06:26 +08:00
} else {
2021-06-04 22:51:52 +08:00
$audio .= ' src="' . slash_item('baseURL') . $src . '"';
}
2021-06-07 19:06:26 +08:00
if ($attributes !== '') {
2021-06-04 22:51:52 +08:00
$audio .= ' ' . $attributes;
}
$audio .= '>';
foreach ($tracks as $track) {
$audio .= "\n" . _space_indent() . $track;
2021-06-04 22:51:52 +08:00
}
2021-06-07 19:06:26 +08:00
if (! empty($unsupportedMessage)) {
2021-06-04 22:51:52 +08:00
$audio .= "\n" . _space_indent() . $unsupportedMessage . "\n";
}
return $audio . "</audio>\n";
}
}
2021-06-07 19:06:26 +08:00
if (! function_exists('_media')) {
2021-06-04 22:51:52 +08:00
/**
* Generate media based tag
*
* @param string $unsupportedMessage The message to display if the media tag is not supported by the browser.
*/
function _media(string $name, array $types = [], string $unsupportedMessage = '', string $attributes = '', array $tracks = []): string
{
$media = '<' . $name;
2021-06-07 19:06:26 +08:00
if (empty($attributes)) {
2021-06-04 22:51:52 +08:00
$media .= '>';
2021-06-07 19:06:26 +08:00
} else {
2021-06-04 22:51:52 +08:00
$media .= ' ' . $attributes . '>';
}
$media .= "\n";
2021-06-07 19:06:26 +08:00
foreach ($types as $option) {
2021-06-04 22:51:52 +08:00
$media .= _space_indent() . $option . "\n";
}
foreach ($tracks as $track) {
$media .= _space_indent() . $track . "\n";
2021-06-04 22:51:52 +08:00
}
2021-06-07 19:06:26 +08:00
if (! empty($unsupportedMessage)) {
2021-06-04 22:51:52 +08:00
$media .= _space_indent() . $unsupportedMessage . "\n";
}
return $media . ('</' . $name . ">\n");
}
}
2021-06-07 19:06:26 +08:00
if (! function_exists('source')) {
2021-06-04 22:51:52 +08:00
/**
* Source
*
* Generates a source element that specifies multiple media resources
* for either audio or video element
*
2021-06-08 12:11:23 +08:00
* @param string $src The path of the media resource
* @param string $type The MIME-type of the resource with optional codecs parameters
* @param string $attributes HTML attributes
2021-06-04 22:51:52 +08:00
*/
function source(string $src, string $type = 'unknown', string $attributes = '', bool $indexPage = false): string
{
2021-06-07 19:06:26 +08:00
if (! _has_protocol($src)) {
2021-06-04 22:51:52 +08:00
$src = $indexPage === true ? site_url($src) : slash_item('baseURL') . $src;
}
$source = '<source src="' . $src
. '" type="' . $type . '"';
2021-06-07 19:06:26 +08:00
if (! empty($attributes)) {
2021-06-04 22:51:52 +08:00
$source .= ' ' . $attributes;
}
return $source . ' />';
}
}
2021-06-07 19:06:26 +08:00
if (! function_exists('track')) {
2021-06-04 22:51:52 +08:00
/**
* Track
*
* Generates a track element to specify timed tracks. The tracks are
* formatted in WebVTT format.
*
2021-07-24 19:30:51 +08:00
* @param string $src The path of the .VTT file
2021-06-04 22:51:52 +08:00
*/
function track(string $src, string $kind, string $srcLanguage, string $label): string
{
return '<track src="' . $src
. '" kind="' . $kind
. '" srclang="' . $srcLanguage
. '" label="' . $label
. '" />';
}
}
2021-06-07 19:06:26 +08:00
if (! function_exists('object')) {
2021-06-04 22:51:52 +08:00
/**
* Object
*
* Generates an object element that represents the media
* as either image or a resource plugin such as audio, video,
* Java applets, ActiveX, PDF and Flash
*
2021-06-08 12:11:23 +08:00
* @param string $data A resource URL
* @param string $type Content-type of the resource
* @param string $attributes HTML attributes
2021-06-04 22:51:52 +08:00
*/
function object(string $data, string $type = 'unknown', string $attributes = '', array $params = [], bool $indexPage = false): string
{
2021-06-07 19:06:26 +08:00
if (! _has_protocol($data)) {
2021-06-04 22:51:52 +08:00
$data = $indexPage === true ? site_url($data) : slash_item('baseURL') . $data;
}
$object = '<object data="' . $data . '" '
. $attributes . '>';
2021-06-07 19:06:26 +08:00
if (! empty($params)) {
2021-06-04 22:51:52 +08:00
$object .= "\n";
}
2021-06-07 19:06:26 +08:00
foreach ($params as $param) {
2021-06-04 22:51:52 +08:00
$object .= _space_indent() . $param . "\n";
}
return $object . "</object>\n";
}
}
2021-06-07 19:06:26 +08:00
if (! function_exists('param')) {
2021-06-04 22:51:52 +08:00
/**
* Param
*
* Generates a param element that defines parameters
* for the object element.
*
* @param string $name The name of the parameter
* @param string $value The value of the parameter
* @param string $type The MIME-type
* @param string $attributes HTML attributes
*/
function param(string $name, string $value, string $type = 'ref', string $attributes = ''): string
{
return '<param name="' . $name
. '" type="' . $type
. '" value="' . $value
. '" ' . $attributes . ' />';
}
}
2021-06-07 19:06:26 +08:00
if (! function_exists('embed')) {
2021-06-04 22:51:52 +08:00
/**
* Embed
*
* Generates an embed element
*
2021-06-08 12:11:23 +08:00
* @param string $src The path of the resource to embed
* @param string $type MIME-type
* @param string $attributes HTML attributes
2021-06-04 22:51:52 +08:00
*/
function embed(string $src, string $type = 'unknown', string $attributes = '', bool $indexPage = false): string
{
2021-06-07 19:06:26 +08:00
if (! _has_protocol($src)) {
2021-06-04 22:51:52 +08:00
$src = $indexPage === true ? site_url($src) : slash_item('baseURL') . $src;
}
return '<embed src="' . $src
. '" type="' . $type . '" '
. $attributes . " />\n";
}
}
2021-06-07 19:06:26 +08:00
if (! function_exists('_has_protocol')) {
2021-06-04 22:51:52 +08:00
/**
* Test the protocol of a URI.
*
* @return false|int
2021-06-04 22:51:52 +08:00
*/
function _has_protocol(string $url)
{
return preg_match('#^([a-z]+:)?//#i', $url);
}
}
2021-06-07 19:06:26 +08:00
if (! function_exists('_space_indent')) {
2021-06-04 22:51:52 +08:00
/**
* Provide space indenting.
*/
function _space_indent(int $depth = 2): string
{
return str_repeat(' ', $depth);
}
2016-11-07 17:41:46 +08:00
}