* @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\HTML; use Joomla\CMS\Environment\Browser; use Joomla\CMS\Factory; use Joomla\CMS\Filesystem\File; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Language\Text; use Joomla\CMS\Layout\LayoutHelper; use Joomla\CMS\Uri\Uri; use Joomla\Utilities\ArrayHelper; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Utility class for all HTML drawing classes * * @since 1.5 */ abstract class HTMLHelper { /** * Option values related to the generation of HTML output. Recognized * options are: * fmtDepth, integer. The current indent depth. * fmtEol, string. The end of line string, default is linefeed. * fmtIndent, string. The string to use for indentation, default is * tab. * * @var array * @since 1.5 */ public static $formatOptions = array('format.depth' => 0, 'format.eol' => "\n", 'format.indent' => "\t"); /** * An array to hold included paths * * @var string[] * @since 1.5 * @deprecated 5.0 */ protected static $includePaths = array(); /** * An array to hold method references * * @var callable[] * @since 1.6 * @deprecated 5.0 */ protected static $registry = array(); /** * The service registry for custom and overridden JHtml helpers * * @var Registry * @since 4.0.0 */ protected static $serviceRegistry; /** * Method to extract a key * * @param string $key The name of helper method to load, (prefix).(class).function * prefix and class are optional and can be used to load custom html helpers. * * @return array Contains lowercase key, prefix, file, function. * * @since 1.6 * @deprecated 5.0 Use the service registry instead */ protected static function extract($key) { $key = preg_replace('#[^A-Z0-9_\.]#i', '', $key); // Check to see whether we need to load a helper file $parts = explode('.', $key); if (\count($parts) === 3) { @trigger_error( 'Support for a three segment service key is deprecated and will be removed in Joomla 5.0, use the service registry instead', E_USER_DEPRECATED ); } $prefix = \count($parts) === 3 ? array_shift($parts) : 'JHtml'; $file = \count($parts) === 2 ? array_shift($parts) : ''; $func = array_shift($parts); return array(strtolower($prefix . '.' . $file . '.' . $func), $prefix, $file, $func); } /** * Class loader method * * Additional arguments may be supplied and are passed to the sub-class. * Additional include paths are also able to be specified for third-party use * * @param string $key The name of helper method to load, (prefix).(class).function * prefix and class are optional and can be used to load custom * html helpers. * @param array $methodArgs The arguments to pass forward to the method being called * * @return mixed Result of HTMLHelper::call($function, $args) * * @since 1.5 * @throws \InvalidArgumentException */ final public static function _(string $key, ...$methodArgs) { list($key, $prefix, $file, $func) = static::extract($key); if (\array_key_exists($key, static::$registry)) { $function = static::$registry[$key]; return static::call($function, $methodArgs); } /* * Support fetching services from the registry if a custom class prefix was not given (a three segment key), * the service comes from a class other than this one, and a service has been registered for the file. */ if ($prefix === 'JHtml' && $file !== '' && static::getServiceRegistry()->hasService($file)) { $service = static::getServiceRegistry()->getService($file); $toCall = array($service, $func); if (!\is_callable($toCall)) { throw new \InvalidArgumentException(sprintf('%s::%s not found.', $file, $func), 500); } static::register($key, $toCall); return static::call($toCall, $methodArgs); } $className = $prefix . ucfirst($file); if (!class_exists($className)) { $path = Path::find(static::$includePaths, strtolower($file) . '.php'); if (!$path) { throw new \InvalidArgumentException(sprintf('%s %s not found.', $prefix, $file), 500); } \JLoader::register($className, $path); if (!class_exists($className)) { throw new \InvalidArgumentException(sprintf('%s not found.', $className), 500); } } // If calling a method from this class, do not allow access to internal methods if ($className === __CLASS__) { if (!((new \ReflectionMethod($className, $func))->isPublic())) { throw new \InvalidArgumentException('Access to internal class methods is not allowed.'); } } $toCall = array($className, $func); if (!\is_callable($toCall)) { throw new \InvalidArgumentException(sprintf('%s::%s not found.', $className, $func), 500); } static::register($key, $toCall); return static::call($toCall, $methodArgs); } /** * Registers a function to be called with a specific key * * @param string $key The name of the key * @param callable $function Function or method * * @return boolean True if the function is callable * * @since 1.6 * @deprecated 5.0 Use the service registry instead */ public static function register($key, callable $function) { @trigger_error( 'Support for registering functions is deprecated and will be removed in Joomla 5.0, use the service registry instead', E_USER_DEPRECATED ); list($key) = static::extract($key); static::$registry[$key] = $function; return true; } /** * Removes a key for a method from registry. * * @param string $key The name of the key * * @return boolean True if a set key is unset * * @since 1.6 * @deprecated 5.0 Use the service registry instead */ public static function unregister($key) { @trigger_error( 'Support for registering functions is deprecated and will be removed in Joomla 5.0, use the service registry instead', E_USER_DEPRECATED ); list($key) = static::extract($key); if (isset(static::$registry[$key])) { unset(static::$registry[$key]); return true; } return false; } /** * Test if the key is registered. * * @param string $key The name of the key * * @return boolean True if the key is registered. * * @since 1.6 */ public static function isRegistered($key) { list($key) = static::extract($key); return isset(static::$registry[$key]); } /** * Retrieves the service registry. * * @return Registry * * @since 4.0.0 */ public static function getServiceRegistry(): Registry { if (!static::$serviceRegistry) { static::$serviceRegistry = Factory::getContainer()->get(Registry::class); } return static::$serviceRegistry; } /** * Function caller method * * @param callable $function Function or method to call * @param array $args Arguments to be passed to function * * @return mixed Function result or false on error. * * @link https://www.php.net/manual/en/function.call-user-func-array.php * @since 1.6 * @throws \InvalidArgumentException */ protected static function call(callable $function, $args) { // Workaround to allow calling helper methods have arguments passed by reference $temp = []; foreach ($args as &$arg) { $temp[] = &$arg; } return \call_user_func_array($function, $temp); } /** * Write a `` element * * @param string $url The relative URL to use for the href attribute * @param string $text The target attribute to use * @param array|string $attribs Attributes to be added to the `` element * * @return string * * @since 1.5 */ public static function link($url, $text, $attribs = null) { if (\is_array($attribs)) { $attribs = ArrayHelper::toString($attribs); } return '' . $text . ''; } /** * Write a `'; } /** * Compute the files to be included * * @param string $folder Folder name to search in (i.e. images, css, js). * @param string $file Path to file. * @param boolean $relative Flag if the path to the file is relative to the /media folder (and searches in template). * @param boolean $detectBrowser Flag if the browser should be detected to include specific browser files. * @param boolean $detectDebug Flag if debug mode is enabled to include uncompressed files if debug is on. * * @return array files to be included. * * @see Browser * @since 1.6 */ protected static function includeRelativeFiles($folder, $file, $relative, $detectBrowser, $detectDebug) { // Set debug flag $debugMode = false; // Detect debug mode if ($detectDebug && JDEBUG) { $debugMode = true; } // If http is present in filename if (strpos($file, 'http') === 0 || strpos($file, '//') === 0) { $includes = [$file]; } else { // Extract extension and strip the file $strip = File::stripExt($file); $ext = File::getExt($file); // Prepare array of files $includes = []; // Detect browser and compute potential files if ($detectBrowser) { $navigator = Browser::getInstance(); $browser = $navigator->getBrowser(); $major = $navigator->getMajor(); $minor = $navigator->getMinor(); $minExt = ''; if (\strlen($strip) > 4 && preg_match('#\.min$#', $strip)) { $minExt = '.min'; $strip = preg_replace('#\.min$#', '', $strip); } // Try to include files named filename.ext, filename_browser.ext, filename_browser_major.ext, filename_browser_major_minor.ext // where major and minor are the browser version names $potential = [ $strip . $minExt, $strip . '_' . $browser . $minExt, $strip . '_' . $browser . '_' . $major . $minExt, $strip . '_' . $browser . '_' . $major . '_' . $minor . $minExt, ]; } else { $potential = [$strip]; } // If relative search in template directory or media directory if ($relative) { $app = Factory::getApplication(); $template = $app->getTemplate(true); $templaPath = JPATH_THEMES; if ($template->inheritable || !empty($template->parent)) { $client = $app->isClient('administrator') === true ? 'administrator' : 'site'; $templaPath = JPATH_ROOT . "/media/templates/$client"; } // For each potential files foreach ($potential as $strip) { $files = []; $files[] = $strip . '.' . $ext; /** * Loop on 1 or 2 files and break on first found. * Add the content of the MD5SUM file located in the same folder to url to ensure cache browser refresh * This MD5SUM file must represent the signature of the folder content */ foreach ($files as $file) { if (!empty($template->parent)) { $found = static::addFileToBuffer("$templaPath/$template->template/$folder/$file", $ext, $debugMode); if (empty($found)) { $found = static::addFileToBuffer("$templaPath/$template->parent/$folder/$file", $ext, $debugMode); } } else { $found = static::addFileToBuffer("$templaPath/$template->template/$folder/$file", $ext, $debugMode); } if (!empty($found)) { $includes[] = $found; break; } else { // If the file contains any /: it can be in a media extension subfolder if (strpos($file, '/')) { // Divide the file extracting the extension as the first part before / list($extension, $file) = explode('/', $file, 2); // If the file yet contains any /: it can be a plugin if (strpos($file, '/')) { // Divide the file extracting the element as the first part before / list($element, $file) = explode('/', $file, 2); // Try to deal with plugins group in the media folder $found = static::addFileToBuffer(JPATH_ROOT . "/media/$extension/$element/$folder/$file", $ext, $debugMode); if (!empty($found)) { $includes[] = $found; break; } // Try to deal with classical file in a media subfolder called element $found = static::addFileToBuffer(JPATH_ROOT . "/media/$extension/$folder/$element/$file", $ext, $debugMode); if (!empty($found)) { $includes[] = $found; break; } // Try to deal with system files in the template folder if (!empty($template->parent)) { $found = static::addFileToBuffer("$templaPath/$template->template/$folder/system/$element/$file", $ext, $debugMode); if (!empty($found)) { $includes[] = $found; break; } $found = static::addFileToBuffer("$templaPath/$template->parent/$folder/system/$element/$file", $ext, $debugMode); if (!empty($found)) { $includes[] = $found; break; } } else { // Try to deal with system files in the media folder $found = static::addFileToBuffer(JPATH_ROOT . "/media/system/$folder/$element/$file", $ext, $debugMode); if (!empty($found)) { $includes[] = $found; break; } } } else { // Try to deal with files in the extension's media folder $found = static::addFileToBuffer(JPATH_ROOT . "/media/$extension/$folder/$file", $ext, $debugMode); if (!empty($found)) { $includes[] = $found; break; } // Try to deal with system files in the template folder if (!empty($template->parent)) { $found = static::addFileToBuffer("$templaPath/$template->template/$folder/system/$file", $ext, $debugMode); if (!empty($found)) { $includes[] = $found; break; } $found = static::addFileToBuffer("$templaPath/$template->parent/$folder/system/$file", $ext, $debugMode); if (!empty($found)) { $includes[] = $found; break; } } else { // Try to deal with system files in the template folder $found = static::addFileToBuffer("$templaPath/$template->template/$folder/system/$file", $ext, $debugMode); if (!empty($found)) { $includes[] = $found; break; } } // Try to deal with system files in the media folder $found = static::addFileToBuffer(JPATH_ROOT . "/media/system/$folder/$file", $ext, $debugMode); if (!empty($found)) { $includes[] = $found; break; } } } else { // Try to deal with system files in the media folder $found = static::addFileToBuffer(JPATH_ROOT . "/media/system/$folder/$file", $ext, $debugMode); if (!empty($found)) { $includes[] = $found; break; } } } } } } else { // If not relative and http is not present in filename foreach ($potential as $strip) { $files = []; $files[] = $strip . '.' . $ext; /** * Loop on 1 or 2 files and break on first found. * Add the content of the MD5SUM file located in the same folder to url to ensure cache browser refresh * This MD5SUM file must represent the signature of the folder content */ foreach ($files as $file) { $path = JPATH_ROOT . "/$file"; $found = static::addFileToBuffer($path, $ext, $debugMode); if (!empty($found)) { $includes[] = $found; break; } } } } } return $includes; } /** * Gets a URL, cleans the Joomla specific params and returns an object * * @param string $url The relative or absolute URL to use for the src attribute. * * @return object * @example { * url: 'string', * attributes: [ * width: integer, * height: integer, * ] * } * * @since 4.0.0 */ public static function cleanImageURL($url) { $obj = new \stdClass(); $obj->attributes = [ 'width' => 0, 'height' => 0, ]; if ($url === null) { $url = ''; } if (!strpos($url, '?')) { $obj->url = $url; return $obj; } $mediaUri = new Uri($url); // Old image URL format if ($mediaUri->hasVar('joomla_image_height')) { $height = (int) $mediaUri->getVar('joomla_image_height'); $width = (int) $mediaUri->getVar('joomla_image_width'); $mediaUri->delVar('joomla_image_height'); $mediaUri->delVar('joomla_image_width'); } else { // New Image URL format $fragmentUri = new Uri($mediaUri->getFragment()); $width = (int) $fragmentUri->getVar('width', 0); $height = (int) $fragmentUri->getVar('height', 0); } if ($width > 0) { $obj->attributes['width'] = $width; } if ($height > 0) { $obj->attributes['height'] = $height; } $mediaUri->setFragment(''); $obj->url = $mediaUri->toString(); return $obj; } /** * Write a `` element * * @param string $file The relative or absolute URL to use for the src attribute. * @param string $alt The alt text. * @param array|string $attribs Attributes to be added to the `` element * @param boolean $relative Flag if the path to the file is relative to the /media folder (and searches in template). * @param integer $returnPath Defines the return value for the method: * -1: Returns a `` tag without looking for relative files * 0: Returns a `` tag while searching for relative files * 1: Returns the file path to the image while searching for relative files * * @return string|null HTML markup for the image, relative path to the image, or null if path is to be returned but image is not found * * @since 1.5 */ public static function image($file, $alt, $attribs = null, $relative = false, $returnPath = 0) { // Ensure is an integer $returnPath = (int) $returnPath; // The path of the file $path = $file; // The arguments of the file path $arguments = ''; // Get the arguments positions $pos1 = strpos($file, '?'); $pos2 = strpos($file, '#'); // Check if there are arguments if ($pos1 !== false || $pos2 !== false) { // Get the path only $path = substr($file, 0, min($pos1, $pos2)); // Determine the arguments is mostly the part behind the # $arguments = str_replace($path, '', $file); } // Get the relative file name when requested if ($returnPath !== -1) { // Search for relative file names $includes = static::includeRelativeFiles('images', $path, $relative, false, false); // Grab the first found path and if none exists default to null $path = \count($includes) ? $includes[0] : null; } // Compile the file name $file = ($path === null ? null : $path . $arguments); // If only the file is required, return here if ($returnPath === 1) { return $file; } // Ensure we have a valid default for concatenating if ($attribs === null || $attribs === false) { $attribs = []; } // When it is a string, we need convert it to an array if (is_string($attribs)) { $attributes = []; // Go through each argument foreach (explode(' ', $attribs) as $attribute) { // When an argument without a value, default to an empty string if (strpos($attribute, '=') === false) { $attributes[$attribute] = ''; continue; } // Set the attribute list($key, $value) = explode('=', $attribute); $attributes[$key] = trim($value, '"'); } // Add the attributes from the string to the original attributes $attribs = $attributes; } // Fill the attributes with the file and alt text $attribs['src'] = $file; $attribs['alt'] = $alt; // Render the layout with the attributes return LayoutHelper::render('joomla.html.image', $attribs); } /** * Write a `` element to load a CSS file * * @param string $file Path to file * @param array $options Array of options. Example: array('version' => 'auto', 'conditional' => 'lt IE 9') * @param array $attribs Array of attributes. Example: array('id' => 'scriptid', 'async' => 'async', 'data-test' => 1) * * @return array|string|null nothing if $returnPath is false, null, path or array of path if specific CSS browser files were detected * * @see Browser * @since 1.5 */ public static function stylesheet($file, $options = array(), $attribs = array()) { $options['relative'] = $options['relative'] ?? false; $options['pathOnly'] = $options['pathOnly'] ?? false; $options['detectBrowser'] = $options['detectBrowser'] ?? false; $options['detectDebug'] = $options['detectDebug'] ?? true; $includes = static::includeRelativeFiles('css', $file, $options['relative'], $options['detectBrowser'], $options['detectDebug']); // If only path is required if ($options['pathOnly']) { if (\count($includes) === 0) { return; } if (\count($includes) === 1) { return $includes[0]; } return $includes; } // If inclusion is required $document = Factory::getApplication()->getDocument(); foreach ($includes as $include) { // If there is already a version hash in the script reference (by using deprecated MD5SUM). if ($pos = strpos($include, '?') !== false) { $options['version'] = substr($include, $pos + 1); } $document->addStyleSheet($include, $options, $attribs); } } /** * Write a `