1<?php 2/** 3 * phpMyAdmin theme manager 4 */ 5 6declare(strict_types=1); 7 8namespace PhpMyAdmin; 9 10use const DIRECTORY_SEPARATOR; 11use const E_USER_ERROR; 12use const E_USER_WARNING; 13use function array_key_exists; 14use function closedir; 15use function htmlspecialchars; 16use function is_dir; 17use function ksort; 18use function opendir; 19use function readdir; 20use function sprintf; 21use function trigger_error; 22 23/** 24 * phpMyAdmin theme manager 25 */ 26class ThemeManager 27{ 28 /** 29 * ThemeManager instance 30 * 31 * @access private 32 * @static 33 * @var ThemeManager 34 */ 35 private static $instance; 36 37 /** 38 * @var string file-system path to the theme folder 39 * @access protected 40 */ 41 private $themesPath; 42 43 /** @var string path to theme folder as an URL */ 44 private $themesPathUrl; 45 46 /** @var array available themes */ 47 public $themes = []; 48 49 /** @var string cookie name */ 50 public $cookieName = 'pma_theme'; 51 52 /** @var bool */ 53 public $perServer = false; 54 55 /** @var string name of active theme */ 56 public $activeTheme = ''; 57 58 /** @var Theme Theme active theme */ 59 public $theme = null; 60 61 /** @var string */ 62 public $themeDefault; 63 64 /** 65 * @const string The name of the fallback theme 66 */ 67 public const FALLBACK_THEME = 'pmahomme'; 68 69 public function __construct() 70 { 71 $this->themes = []; 72 $this->themeDefault = self::FALLBACK_THEME; 73 $this->activeTheme = ''; 74 $this->themesPath = self::getThemesFsDir(); 75 $this->themesPathUrl = self::getThemesDir(); 76 77 if (! $this->checkThemeFolder($this->themesPath)) { 78 return; 79 } 80 81 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']); 82 83 $this->loadThemes(); 84 85 $this->theme = new Theme(); 86 87 $config_theme_exists = true; 88 89 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) { 90 trigger_error( 91 sprintf( 92 __('Default theme %s not found!'), 93 htmlspecialchars($GLOBALS['cfg']['ThemeDefault']) 94 ), 95 E_USER_ERROR 96 ); 97 $config_theme_exists = false; 98 } else { 99 $this->themeDefault = $GLOBALS['cfg']['ThemeDefault']; 100 } 101 102 // check if user have a theme cookie 103 $cookie_theme = $this->getThemeCookie(); 104 if ($cookie_theme && $this->setActiveTheme($cookie_theme)) { 105 return; 106 } 107 108 if ($config_theme_exists) { 109 // otherwise use default theme 110 $this->setActiveTheme($this->themeDefault); 111 } else { 112 // or fallback theme 113 $this->setActiveTheme(self::FALLBACK_THEME); 114 } 115 } 116 117 /** 118 * Returns the singleton ThemeManager object 119 * 120 * @return ThemeManager The instance 121 */ 122 public static function getInstance(): ThemeManager 123 { 124 if (empty(self::$instance)) { 125 self::$instance = new ThemeManager(); 126 } 127 128 return self::$instance; 129 } 130 131 /** 132 * sets if there are different themes per server 133 * 134 * @param bool $per_server Whether to enable per server flag 135 * 136 * @access public 137 */ 138 public function setThemePerServer($per_server): void 139 { 140 $this->perServer = (bool) $per_server; 141 } 142 143 /** 144 * Sets active theme 145 * 146 * @param string|null $theme theme name 147 * 148 * @return bool true on success 149 * 150 * @access public 151 */ 152 public function setActiveTheme(?string $theme): bool 153 { 154 if (! $this->checkTheme($theme)) { 155 trigger_error( 156 sprintf( 157 __('Theme %s not found!'), 158 htmlspecialchars((string) $theme) 159 ), 160 E_USER_ERROR 161 ); 162 163 return false; 164 } 165 166 $this->activeTheme = $theme; 167 $this->theme = $this->themes[$theme]; 168 169 // need to set later 170 //$this->setThemeCookie(); 171 172 return true; 173 } 174 175 /** 176 * Returns name for storing theme 177 * 178 * @return string cookie name 179 * 180 * @access public 181 */ 182 public function getThemeCookieName() 183 { 184 // Allow different theme per server 185 if (isset($GLOBALS['server']) && $this->perServer) { 186 return $this->cookieName . '-' . $GLOBALS['server']; 187 } 188 189 return $this->cookieName; 190 } 191 192 /** 193 * returns name of theme stored in the cookie 194 * 195 * @return string|false theme name from cookie or false 196 * 197 * @access public 198 */ 199 public function getThemeCookie() 200 { 201 global $PMA_Config; 202 203 $name = $this->getThemeCookieName(); 204 if ($PMA_Config->issetCookie($name)) { 205 return $PMA_Config->getCookie($name); 206 } 207 208 return false; 209 } 210 211 /** 212 * save theme in cookie 213 * 214 * @return true 215 * 216 * @access public 217 */ 218 public function setThemeCookie(): bool 219 { 220 $themeId = $this->theme !== null ? (string) $this->theme->id : ''; 221 $GLOBALS['PMA_Config']->setCookie( 222 $this->getThemeCookieName(), 223 $themeId, 224 $this->themeDefault 225 ); 226 // force a change of a dummy session variable to avoid problems 227 // with the caching of phpmyadmin.css.php 228 $GLOBALS['PMA_Config']->set('theme-update', $themeId); 229 230 return true; 231 } 232 233 /** 234 * Checks whether folder is valid for storing themes 235 * 236 * @param string $folder Folder name to test 237 * 238 * @access private 239 */ 240 private function checkThemeFolder($folder): bool 241 { 242 if (! is_dir($folder)) { 243 trigger_error( 244 sprintf( 245 __('Theme path not found for theme %s!'), 246 htmlspecialchars($folder) 247 ), 248 E_USER_ERROR 249 ); 250 251 return false; 252 } 253 254 return true; 255 } 256 257 /** 258 * read all themes 259 * 260 * @access public 261 */ 262 public function loadThemes(): bool 263 { 264 $this->themes = []; 265 $handleThemes = opendir($this->themesPath); 266 267 if ($handleThemes === false) { 268 trigger_error( 269 'phpMyAdmin-ERROR: cannot open themes folder: ' 270 . $this->themesPath, 271 E_USER_WARNING 272 ); 273 274 return false; 275 } 276 277 // check for themes directory 278 while (($PMA_Theme = readdir($handleThemes)) !== false) { 279 // Skip non dirs, . and .. 280 if ($PMA_Theme === '.' 281 || $PMA_Theme === '..' 282 || ! @is_dir($this->themesPath . $PMA_Theme) 283 ) { 284 continue; 285 } 286 if (array_key_exists($PMA_Theme, $this->themes)) { 287 continue; 288 } 289 $new_theme = Theme::load( 290 $this->themesPathUrl . $PMA_Theme, 291 $this->themesPath . $PMA_Theme . DIRECTORY_SEPARATOR 292 ); 293 if (! $new_theme) { 294 continue; 295 } 296 297 $new_theme->setId($PMA_Theme); 298 $this->themes[$PMA_Theme] = $new_theme; 299 } 300 closedir($handleThemes); 301 302 ksort($this->themes); 303 304 return true; 305 } 306 307 /** 308 * checks if given theme name is a known theme 309 * 310 * @param string|null $theme name fo theme to check for 311 * 312 * @access public 313 */ 314 public function checkTheme(?string $theme): bool 315 { 316 return array_key_exists($theme ?? '', $this->themes); 317 } 318 319 /** 320 * returns HTML selectbox 321 * 322 * @access public 323 */ 324 public function getHtmlSelectBox(): string 325 { 326 $select_box = ''; 327 328 $select_box .= '<form name="setTheme" method="post"'; 329 $select_box .= ' action="index.php?route=/set-theme" class="disableAjax">'; 330 $select_box .= Url::getHiddenInputs(); 331 332 $theme_preview_href = '<a href="' 333 . Url::getFromRoute('/themes') . '" target="themes" class="themeselect">'; 334 $select_box .= $theme_preview_href . __('Theme:') . '</a>' . "\n"; 335 336 $select_box .= '<select name="set_theme" lang="en" dir="ltr"' 337 . ' class="autosubmit">'; 338 foreach ($this->themes as $each_theme_id => $each_theme) { 339 $select_box .= '<option value="' . $each_theme_id . '"'; 340 if ($this->activeTheme === $each_theme_id) { 341 $select_box .= ' selected="selected"'; 342 } 343 $select_box .= '>' . htmlspecialchars($each_theme->getName()) 344 . '</option>'; 345 } 346 $select_box .= '</select>'; 347 $select_box .= '</form>'; 348 349 return $select_box; 350 } 351 352 /** 353 * Renders the previews for all themes 354 * 355 * @access public 356 */ 357 public function getPrintPreviews(): string 358 { 359 $retval = ''; 360 foreach ($this->themes as $each_theme) { 361 $retval .= $each_theme->getPrintPreview(); 362 } 363 364 return $retval; 365 } 366 367 public static function initializeTheme(): ?Theme 368 { 369 $themeManager = self::getInstance(); 370 371 return $themeManager->theme; 372 } 373 374 /** 375 * Return the themes directory with a trailing slash 376 */ 377 public static function getThemesFsDir(): string 378 { 379 return ROOT_PATH . 'themes' . DIRECTORY_SEPARATOR; 380 } 381 382 /** 383 * Return the themes directory with a trailing slash as a relative public path 384 */ 385 public static function getThemesDir(): string 386 { 387 return './themes/';// This is an URL 388 } 389} 390