Fixes theme helper to use the new theme assets helper

main
Yasen Pramatarov 2025-06-20 13:55:08 +03:00
parent 37566b5122
commit 55829faf85
1 changed files with 54 additions and 10 deletions

View File

@ -23,6 +23,7 @@ class Theme
*/ */
private static $config; private static $config;
/** /**
* Get the theme configuration * Get the theme configuration
* *
@ -35,11 +36,13 @@ class Theme
return self::$config; return self::$config;
} }
/** /**
* @var string Current theme name * @var string Current theme name
*/ */
private static $currentTheme; private static $currentTheme;
/** /**
* Initialize the theme system * Initialize the theme system
*/ */
@ -52,6 +55,7 @@ class Theme
self::$currentTheme = self::getCurrentThemeName(); self::$currentTheme = self::getCurrentThemeName();
} }
/** /**
* Get the current theme name * Get the current theme name
* *
@ -69,20 +73,55 @@ class Theme
return self::$currentTheme; return self::$currentTheme;
} }
// Get from session if available // Try to get from session first
if (Session::isValidSession() && isset($_SESSION['user_theme'])) { $sessionTheme = Session::get('theme');
$theme = $_SESSION['user_theme']; if ($sessionTheme && isset(self::$config['available_themes'][$sessionTheme])) {
if (self::themeExists($theme)) { self::$currentTheme = $sessionTheme;
self::$currentTheme = $theme; } else {
return $theme; // Fall back to default theme
} self::$currentTheme = self::$config['active_theme'];
} }
// Default to 'default' theme which uses app/templates return self::$currentTheme;
self::$currentTheme = 'default';
return 'default';
} }
/**
* Get the URL for a theme asset
*
* @param string $themeId Theme ID
* @param string $assetPath Path to the asset relative to theme directory (e.g., 'css/style.css')
* @return string|null URL to the asset or null if not found
*/
public static function getAssetUrl($themeId, $assetPath = '')
{
// Clean and validate the asset path
$assetPath = ltrim($assetPath, '/');
if (empty($assetPath)) {
return null;
}
// Only allow alphanumeric, hyphen, underscore, dot, and forward slash
if (!preg_match('/^[a-zA-Z0-9_\-\.\/]+$/', $assetPath)) {
return null;
}
// Prevent directory traversal
if (strpos($assetPath, '..') !== false) {
return null;
}
$fullPath = __DIR__ . "/../../themes/$themeId/$assetPath";
if (!file_exists($fullPath) || !is_readable($fullPath)) {
return null;
}
// Use the router to generate the URL
global $app_root;
return "$app_root/app/helpers/theme-asset.php?theme=" . urlencode($themeId) . "&path=" . urlencode($assetPath);
}
/** /**
* Set the current theme for the session * Set the current theme for the session
* *
@ -125,6 +164,7 @@ class Theme
return true; return true;
} }
/** /**
* Check if a theme exists * Check if a theme exists
* *
@ -142,6 +182,7 @@ class Theme
return is_dir($themePath) && file_exists("$themePath/config.php"); return is_dir($themePath) && file_exists("$themePath/config.php");
} }
/** /**
* Get the path to a theme * Get the path to a theme
* *
@ -155,6 +196,7 @@ class Theme
return rtrim($config['paths']['themes'], '/') . "/$themeName"; return rtrim($config['paths']['themes'], '/') . "/$themeName";
} }
/** /**
* Get the URL for a theme asset * Get the URL for a theme asset
* *
@ -185,6 +227,7 @@ class Theme
return $baseUrl . $assetPath; return $baseUrl . $assetPath;
} }
/** /**
* Include a theme template file * Include a theme template file
* *
@ -226,6 +269,7 @@ class Theme
error_log("Template not found: {$template} in theme: {$themeName}"); error_log("Template not found: {$template} in theme: {$themeName}");
} }
/** /**
* Get all available themes * Get all available themes
* *