ThemeManager.php 9.9 KB

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