PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/pma/libraries/Theme_Manager.class.php

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