PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyadmin/libraries/Theme_Manager.class.php

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