1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin;
use function __;
use function array_key_exists;
use function closedir;
use function htmlspecialchars;
use function is_dir;
use function ksort;
use function opendir;
use function readdir;
use function sprintf;
use function trigger_error;
use const DIRECTORY_SEPARATOR;
use const E_USER_ERROR;
use const E_USER_WARNING;
/**
* phpMyAdmin theme manager
*/
class ThemeManager
{
/**
* ThemeManager instance
*
* @static
* @var ThemeManager
*/
private static $instance;
/** @var string file-system path to the theme folder */
private $themesPath;
/** @var string path to theme folder as an URL */
private $themesPathUrl;
/** @var array<string,Theme> available themes */
public $themes = [];
/** @var string cookie name */
public $cookieName = 'pma_theme';
/** @var bool */
public $perServer = false;
/** @var string name of active theme */
public $activeTheme = '';
/** @var Theme Theme active theme */
public $theme = null;
/** @var string */
public $themeDefault;
/**
* @const string The name of the fallback theme
*/
public const FALLBACK_THEME = 'pmahomme';
public function __construct()
{
$this->themes = [];
$this->themeDefault = self::FALLBACK_THEME;
$this->activeTheme = '';
$this->themesPath = self::getThemesFsDir();
$this->themesPathUrl = self::getThemesDir();
$this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
$this->loadThemes();
$this->theme = new Theme();
$configThemeExists = true;
if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
trigger_error(
sprintf(
__('Default theme %s not found!'),
htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])
),
E_USER_ERROR
);
$configThemeExists = false;
} else {
$this->themeDefault = $GLOBALS['cfg']['ThemeDefault'];
}
// check if user have a theme cookie
$cookieTheme = $this->getThemeCookie();
if ($cookieTheme && $this->setActiveTheme($cookieTheme)) {
return;
}
if ($configThemeExists) {
// otherwise use default theme
$this->setActiveTheme($this->themeDefault);
} else {
// or fallback theme
$this->setActiveTheme(self::FALLBACK_THEME);
}
}
/**
* Returns the singleton ThemeManager object
*
* @return ThemeManager The instance
*/
public static function getInstance(): ThemeManager
{
if (empty(self::$instance)) {
self::$instance = new ThemeManager();
}
return self::$instance;
}
/**
* sets if there are different themes per server
*
* @param bool $perServer Whether to enable per server flag
*/
public function setThemePerServer($perServer): void
{
$this->perServer = (bool) $perServer;
}
/**
* Sets active theme
*
* @param string|null $theme theme name
*/
public function setActiveTheme(?string $theme): bool
{
if (! $this->checkTheme($theme)) {
trigger_error(
sprintf(
__('Theme %s not found!'),
htmlspecialchars((string) $theme)
),
E_USER_ERROR
);
return false;
}
$this->activeTheme = $theme;
$this->theme = $this->themes[$theme];
// need to set later
//$this->setThemeCookie();
return true;
}
/**
* Returns name for storing theme
*
* @return string cookie name
*/
public function getThemeCookieName()
{
// Allow different theme per server
if (isset($GLOBALS['server']) && $this->perServer) {
return $this->cookieName . '-' . $GLOBALS['server'];
}
return $this->cookieName;
}
/**
* returns name of theme stored in the cookie
*
* @return string|false theme name from cookie or false
*/
public function getThemeCookie()
{
global $config;
$name = $this->getThemeCookieName();
if ($config->issetCookie($name)) {
return $config->getCookie($name);
}
return false;
}
/**
* save theme in cookie
*
* @return true
*/
public function setThemeCookie(): bool
{
$themeId = $this->theme !== null ? (string) $this->theme->id : '';
$GLOBALS['config']->setCookie(
$this->getThemeCookieName(),
$themeId,
$this->themeDefault
);
// force a change of a dummy session variable to avoid problems
// with the caching of phpmyadmin.css.php
$GLOBALS['config']->set('theme-update', $themeId);
return true;
}
public function loadThemes(): void
{
$this->themes = [];
$dirHandle = opendir($this->themesPath);
if ($dirHandle === false) {
trigger_error('Error: cannot open themes folder: ./themes', E_USER_WARNING);
return;
}
while (($dir = readdir($dirHandle)) !== false) {
if ($dir === '.' || $dir === '..' || ! @is_dir($this->themesPath . $dir)) {
continue;
}
if (array_key_exists($dir, $this->themes)) {
continue;
}
$newTheme = Theme::load($this->themesPathUrl . $dir, $this->themesPath . $dir . DIRECTORY_SEPARATOR, $dir);
if (! $newTheme instanceof Theme) {
continue;
}
$this->themes[$dir] = $newTheme;
}
closedir($dirHandle);
ksort($this->themes);
}
/**
* checks if given theme name is a known theme
*
* @param string|null $theme name fo theme to check for
*/
public function checkTheme(?string $theme): bool
{
return array_key_exists($theme ?? '', $this->themes);
}
public function getThemesArray(): array
{
$themes = [];
foreach ($this->themes as $theme) {
$themes[] = [
'id' => $theme->getId(),
'name' => $theme->getName(),
'version' => $theme->getVersion(),
'is_active' => $theme->getId() === $this->activeTheme,
];
}
return $themes;
}
public static function initializeTheme(): ?Theme
{
$themeManager = self::getInstance();
return $themeManager->theme;
}
/**
* Return the themes directory with a trailing slash
*/
public static function getThemesFsDir(): string
{
return ROOT_PATH . 'themes' . DIRECTORY_SEPARATOR;
}
/**
* Return the themes directory with a trailing slash as a relative public path
*/
public static function getThemesDir(): string
{
return './themes/';// This is an URL
}
}
|