refactor asset rendering with HTML escaping
Improve security by escaping asset attributes and URLs in phpb_registered_assets. Refactor Extensions class to render assets as HTML strings and add getAssetsArray for raw access. Update phpb_e helper to accept nullable strings.
This commit is contained in:
@@ -241,7 +241,7 @@ class Extensions
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all header assets.
|
||||
* Get all html blocks for the given location.
|
||||
*/
|
||||
public static function getHtmlBlocks(string $location = 'head'): string
|
||||
{
|
||||
@@ -249,31 +249,63 @@ class Extensions
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all header assets.
|
||||
* Render assets as HTML string.
|
||||
*/
|
||||
public static function getHeaderAssets(): array
|
||||
protected static function renderAssets(array $assets): string
|
||||
{
|
||||
return self::$assets['header'];
|
||||
$html = '';
|
||||
foreach ($assets as $asset) {
|
||||
$attributes = '';
|
||||
foreach ($asset['attributes'] as $key => $value) {
|
||||
$attributes .= ' ' . htmlspecialchars($key, ENT_QUOTES, 'UTF-8') . '="' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '"';
|
||||
}
|
||||
|
||||
if ($asset['type'] === 'style') {
|
||||
$html .= '<link rel="stylesheet" href="' . htmlspecialchars($asset['src'], ENT_QUOTES, 'UTF-8') . '"' . $attributes . '/>';
|
||||
} else {
|
||||
$html .= '<script type="text/javascript" src="' . htmlspecialchars($asset['src'], ENT_QUOTES, 'UTF-8') . '"' . $attributes . '></script>';
|
||||
}
|
||||
}
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all footer assets.
|
||||
* Get all header assets as HTML string.
|
||||
*/
|
||||
public static function getFooterAssets() {
|
||||
return self::$assets['footer'];
|
||||
public static function getHeaderAssets(): string
|
||||
{
|
||||
return self::renderAssets(self::$assets['header']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all admin header assets.
|
||||
* Get all footer assets as HTML string.
|
||||
*/
|
||||
public static function getAdminHeaderAssets() {
|
||||
return self::$assets['admin-header'];
|
||||
public static function getFooterAssets(): string
|
||||
{
|
||||
return self::renderAssets(self::$assets['footer']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all admin footer assets.
|
||||
* Get all admin header assets as HTML string.
|
||||
*/
|
||||
public static function getAdminFooterAssets() {
|
||||
return self::$assets['admin-footer'];
|
||||
public static function getAdminHeaderAssets(): string
|
||||
{
|
||||
return self::renderAssets(self::$assets['admin-header']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all admin footer assets as HTML string.
|
||||
*/
|
||||
public static function getAdminFooterAssets(): string
|
||||
{
|
||||
return self::renderAssets(self::$assets['admin-footer']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get raw assets array for internal use by phpb_registered_assets().
|
||||
*/
|
||||
public static function getAssetsArray(string $location): array
|
||||
{
|
||||
return self::$assets[$location] ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user