ThemeManager.php 9.6 KB

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