PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/manage/phpmyadminlite/libraries/Theme_Manager.class.php

https://gitlab.com/albert925/lading-ach
PHP | 381 lines | 209 code | 54 blank | 118 comment | 32 complexity | 93e50a1368dfe0dfde1c0b8a9dc026e0 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @version $Id$
  6. * @package phpMyAdmin
  7. */
  8. /**
  9. *
  10. */
  11. require_once './libraries/Theme.class.php';
  12. /**
  13. *
  14. * @package phpMyAdmin
  15. */
  16. class PMA_Theme_Manager
  17. {
  18. /**
  19. * @var string path to theme folder
  20. * @access protected
  21. */
  22. var $_themes_path;
  23. /**
  24. * @var array available themes
  25. */
  26. var $themes = array();
  27. /**
  28. * @var string cookie name
  29. */
  30. var $cookie_name = 'pma_theme';
  31. /**
  32. * @var boolean
  33. */
  34. var $per_server = false;
  35. /**
  36. * @var string name of active theme
  37. */
  38. var $active_theme = '';
  39. /**
  40. * @var object PMA_Theme active theme
  41. */
  42. var $theme = null;
  43. /**
  44. * @var string
  45. */
  46. var $theme_default = 'original';
  47. function __construct()
  48. {
  49. $this->init();
  50. }
  51. /**
  52. * sets path to folder containing the themes
  53. *
  54. * @param string $path path to themes folder
  55. * @return boolean success
  56. */
  57. function setThemesPath($path)
  58. {
  59. if (! $this->_checkThemeFolder($path)) {
  60. return false;
  61. }
  62. $this->_themes_path = trim($path);
  63. return true;
  64. }
  65. /**
  66. * @public
  67. * @return string
  68. */
  69. function getThemesPath()
  70. {
  71. return $this->_themes_path;
  72. }
  73. /**
  74. * sets if there are different themes per server
  75. *
  76. * @param boolean $per_server
  77. */
  78. function setThemePerServer($per_server)
  79. {
  80. $this->per_server = (bool) $per_server;
  81. }
  82. function init()
  83. {
  84. $this->themes = array();
  85. $this->theme_default = 'original';
  86. $this->active_theme = '';
  87. if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
  88. return false;
  89. }
  90. $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
  91. $this->loadThemes();
  92. $this->theme = new PMA_Theme;
  93. if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
  94. trigger_error(
  95. sprintf($GLOBALS['strThemeDefaultNotFound'],
  96. htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])),
  97. E_USER_ERROR);
  98. $GLOBALS['cfg']['ThemeDefault'] = false;
  99. }
  100. $this->theme_default = $GLOBALS['cfg']['ThemeDefault'];
  101. // check if user have a theme cookie
  102. if (! $this->getThemeCookie()
  103. || ! $this->setActiveTheme($this->getThemeCookie())) {
  104. // otherwise use default theme
  105. if ($GLOBALS['cfg']['ThemeDefault']) {
  106. $this->setActiveTheme($GLOBALS['cfg']['ThemeDefault']);
  107. } else {
  108. // or original theme
  109. $this->setActiveTheme('original');
  110. }
  111. }
  112. }
  113. function checkConfig()
  114. {
  115. if ($this->_themes_path != trim($GLOBALS['cfg']['ThemePath'])
  116. || $this->theme_default != $GLOBALS['cfg']['ThemeDefault']) {
  117. $this->init();
  118. } else {
  119. // at least the theme path needs to be checked every time for new
  120. // themes, as there is no other way at the moment to keep track of
  121. // new or removed themes
  122. $this->loadThemes();
  123. }
  124. }
  125. function setActiveTheme($theme = null)
  126. {
  127. if (! $this->checkTheme($theme)) {
  128. trigger_error(
  129. sprintf($GLOBALS['strThemeNotFound'], htmlspecialchars($theme)),
  130. E_USER_ERROR);
  131. return false;
  132. }
  133. $this->active_theme = $theme;
  134. $this->theme = $this->themes[$theme];
  135. // need to set later
  136. //$this->setThemeCookie();
  137. return true;
  138. }
  139. /**
  140. * @return string cookie name
  141. */
  142. function getThemeCookieName()
  143. {
  144. // Allow different theme per server
  145. if (isset($GLOBALS['server']) && $this->per_server) {
  146. return $this->cookie_name . '-' . $GLOBALS['server'];
  147. } else {
  148. return $this->cookie_name;
  149. }
  150. }
  151. /**
  152. * returns name of theme stored in the cookie
  153. * @return string theme name from cookie
  154. */
  155. function getThemeCookie()
  156. {
  157. if (isset($_COOKIE[$this->getThemeCookieName()])) {
  158. return $_COOKIE[$this->getThemeCookieName()];
  159. }
  160. return false;
  161. }
  162. /**
  163. * save theme in cookie
  164. *
  165. * @uses PMA_setCookie();
  166. * @uses PMA_Theme_Manager::getThemeCookieName()
  167. * @uses PMA_Theme_Manager::$theme
  168. * @uses PMA_Theme_Manager::$theme_default
  169. * @uses PMA_Theme::getId()
  170. */
  171. function setThemeCookie()
  172. {
  173. PMA_setCookie($this->getThemeCookieName(), $this->theme->id,
  174. $this->theme_default);
  175. // force a change of a dummy session variable to avoid problems
  176. // with the caching of phpmyadmin.css.php
  177. $_SESSION['PMA_Config']->set('theme-update', $this->theme->id);
  178. return true;
  179. }
  180. /**
  181. * @private
  182. * @param string $folder
  183. * @return boolean
  184. */
  185. /*private*/ function _checkThemeFolder($folder)
  186. {
  187. if (! is_dir($folder)) {
  188. trigger_error(
  189. sprintf($GLOBALS['strThemePathNotFound'],
  190. htmlspecialchars($folder)),
  191. E_USER_ERROR);
  192. return false;
  193. }
  194. return true;
  195. }
  196. /**
  197. * read all themes
  198. */
  199. function loadThemes()
  200. {
  201. $this->themes = array();
  202. if ($handleThemes = opendir($this->getThemesPath())) {
  203. // check for themes directory
  204. while (false !== ($PMA_Theme = readdir($handleThemes))) {
  205. if (array_key_exists($PMA_Theme, $this->themes)) {
  206. // this does nothing!
  207. //$this->themes[$PMA_Theme] = $this->themes[$PMA_Theme];
  208. continue;
  209. }
  210. $new_theme = PMA_Theme::load($this->getThemesPath() . '/' . $PMA_Theme);
  211. if ($new_theme) {
  212. $new_theme->setId($PMA_Theme);
  213. $this->themes[$PMA_Theme] = $new_theme;
  214. }
  215. } // end get themes
  216. closedir($handleThemes);
  217. } else {
  218. trigger_error(
  219. 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
  220. E_USER_WARNING);
  221. return false;
  222. } // end check for themes directory
  223. ksort($this->themes);
  224. return true;
  225. }
  226. /**
  227. * checks if given theme name is a known theme
  228. *
  229. * @param string $theme name fo theme to check for
  230. */
  231. function checkTheme($theme)
  232. {
  233. if (! array_key_exists($theme, $this->themes)) {
  234. return false;
  235. }
  236. return true;
  237. }
  238. /**
  239. * returns HTML selectbox, with or without form enclosed
  240. *
  241. * @param boolean $form whether enclosed by from tags or not
  242. */
  243. function getHtmlSelectBox($form = true)
  244. {
  245. $select_box = '';
  246. if ($form) {
  247. $select_box .= '<form name="setTheme" method="post" action="index.php"'
  248. .' target="_parent">';
  249. $select_box .= PMA_generate_common_hidden_inputs();
  250. }
  251. $theme_selected = FALSE;
  252. $theme_preview_path= './themes.php';
  253. $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" onclick="'
  254. . "window.open('" . $theme_preview_path . "','themes','left=10,top=20,width=510,height=350,scrollbars=yes,status=yes,resizable=yes');"
  255. . '">';
  256. $select_box .= $theme_preview_href . $GLOBALS['strTheme'] . '</a>:' . "\n";
  257. $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr"'
  258. .' onchange="this.form.submit();" >';
  259. foreach ($this->themes as $each_theme_id => $each_theme) {
  260. $select_box .= '<option value="' . $each_theme_id . '"';
  261. if ($this->active_theme === $each_theme_id) {
  262. $select_box .= ' selected="selected"';
  263. }
  264. $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
  265. }
  266. $select_box .= '</select>';
  267. if ($form) {
  268. $select_box .= '<noscript><input type="submit" value="' . $GLOBALS['strGo'] . '" /></noscript>';
  269. $select_box .= '</form>';
  270. }
  271. return $select_box;
  272. }
  273. /**
  274. * enables backward compatibility
  275. */
  276. function makeBc()
  277. {
  278. $GLOBALS['theme'] = $this->theme->getId();
  279. $GLOBALS['pmaThemePath'] = $this->theme->getPath();
  280. $GLOBALS['pmaThemeImage'] = $this->theme->getImgPath();
  281. /**
  282. * load layout file if exists
  283. */
  284. if (@file_exists($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
  285. include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
  286. }
  287. }
  288. /**
  289. * prints out preview for every theme
  290. *
  291. * @uses $this->themes
  292. * @uses PMA_Theme::printPreview()
  293. */
  294. function printPreviews()
  295. {
  296. foreach ($this->themes as $each_theme) {
  297. $each_theme->printPreview();
  298. } // end 'open themes'
  299. }
  300. /**
  301. * returns PMA_Theme object for fall back theme
  302. * @return object PMA_Theme
  303. */
  304. function getFallBackTheme()
  305. {
  306. if (isset($this->themes['original'])) {
  307. return $this->themes['original'];
  308. }
  309. return false;
  310. }
  311. /**
  312. * prints css data
  313. */
  314. function printCss($type)
  315. {
  316. if ($this->theme->loadCss($type)) {
  317. return true;
  318. }
  319. // if loading css for this theme failed, try default theme css
  320. $fallback_theme = $this->getFallBackTheme();
  321. if ($fallback_theme && $fallback_theme->loadCss($type)) {
  322. return true;
  323. }
  324. return false;
  325. }
  326. }
  327. ?>