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

/Quản lý website bán phụ tùng oto PHP/phpMyAdmin1/libraries/Theme_Manager.class.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 383 lines | 215 code | 56 blank | 112 comment | 32 complexity | a7144ac6c88ca84dabfb4c8b805f44c5 MD5 | raw file
  1. <?php
  2. /* $Id: Theme_Manager.class.php 9831 2007-01-09 09:49:30Z nijel $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4. require_once './libraries/Theme.class.php';
  5. class PMA_Theme_Manager {
  6. /**
  7. * @var string path to theme folder
  8. * @protected
  9. */
  10. var $_themes_path;
  11. /**
  12. * @var array available themes
  13. */
  14. var $themes = array();
  15. /**
  16. * @var string cookie name
  17. */
  18. var $cookie_name = 'pma_theme';
  19. /**
  20. * @var boolean
  21. */
  22. var $per_server = false;
  23. /**
  24. * @var string name of active theme
  25. */
  26. var $active_theme = '';
  27. /**
  28. * @var object PMA_Theme active theme
  29. */
  30. var $theme = null;
  31. /**
  32. * @var string
  33. */
  34. var $theme_default = 'original';
  35. function __construct()
  36. {
  37. $this->init();
  38. }
  39. /**
  40. * sets path to folder containing the themes
  41. *
  42. * @param string $path path to themes folder
  43. * @return boolean success
  44. */
  45. function setThemesPath($path)
  46. {
  47. if (! $this->_checkThemeFolder($path)) {
  48. return false;
  49. }
  50. $this->_themes_path = trim($path);
  51. return true;
  52. }
  53. /**
  54. * @public
  55. * @return string
  56. */
  57. function getThemesPath()
  58. {
  59. return $this->_themes_path;
  60. }
  61. /**
  62. * sets if there are different themes per server
  63. *
  64. * @param boolean $per_server
  65. */
  66. function setThemePerServer($per_server)
  67. {
  68. $this->per_server = (bool) $per_server;
  69. }
  70. function init()
  71. {
  72. $this->themes = array();
  73. $this->theme_default = 'original';
  74. $this->active_theme = '';
  75. if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
  76. return false;
  77. }
  78. $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
  79. $this->loadThemes();
  80. $this->theme = new PMA_Theme;
  81. if ( ! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
  82. $GLOBALS['PMA_errors'][] = sprintf( $GLOBALS['strThemeDefaultNotFound'],
  83. htmlspecialchars($GLOBALS['cfg']['ThemeDefault']));
  84. trigger_error(
  85. sprintf($GLOBALS['strThemeDefaultNotFound'],
  86. htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])),
  87. E_USER_WARNING);
  88. $GLOBALS['cfg']['ThemeDefault'] = false;
  89. }
  90. $this->theme_default = $GLOBALS['cfg']['ThemeDefault'];
  91. // check if user have a theme cookie
  92. if (! $this->getThemeCookie()
  93. || ! $this->setActiveTheme($this->getThemeCookie())) {
  94. // otherwise use default theme
  95. if ($GLOBALS['cfg']['ThemeDefault']) {
  96. $this->setActiveTheme($GLOBALS['cfg']['ThemeDefault']);
  97. } else {
  98. // or original theme
  99. $this->setActiveTheme('original');
  100. }
  101. }
  102. }
  103. function checkConfig()
  104. {
  105. if ($this->_themes_path != trim($GLOBALS['cfg']['ThemePath'])
  106. || $this->theme_default != $GLOBALS['cfg']['ThemeDefault']) {
  107. $this->init();
  108. } else {
  109. // at least the theme path needs to be checked every time for new
  110. // themes, as there is no other way at the moment to keep track of
  111. // new or removed themes
  112. $this->loadThemes();
  113. }
  114. }
  115. function setActiveTheme($theme = null)
  116. {
  117. if ( ! $this->checkTheme($theme)) {
  118. $GLOBALS['PMA_errors'][] = sprintf($GLOBALS['strThemeNotFound'],
  119. htmlspecialchars($theme));
  120. /* Following code can lead to path disclossure, because headers will be sent later */
  121. /* trigger_error(
  122. sprintf($GLOBALS['strThemeNotFound'], htmlspecialchars($theme)),
  123. E_USER_WARNING);*/
  124. return false;
  125. }
  126. $this->active_theme = $theme;
  127. $this->theme = $this->themes[$theme];
  128. // need to set later
  129. //$this->setThemeCookie();
  130. return true;
  131. }
  132. /**
  133. * @return string cookie name
  134. */
  135. function getThemeCookieName()
  136. {
  137. // Allow different theme per server
  138. if (isset($GLOBALS['server']) && $this->per_server) {
  139. return $this->cookie_name . '-' . $GLOBALS['server'];
  140. } else {
  141. return $this->cookie_name;
  142. }
  143. }
  144. /**
  145. * returns name of theme stored in the cookie
  146. * @return string theme name from cookie
  147. */
  148. function getThemeCookie()
  149. {
  150. if (isset($_COOKIE[$this->getThemeCookieName()])) {
  151. return $_COOKIE[$this->getThemeCookieName()];
  152. }
  153. return false;
  154. }
  155. /**
  156. * save theme in cookie
  157. *
  158. * @uses PMA_setCookie();
  159. * @uses PMA_Theme_Manager::getThemeCookieName()
  160. * @uses PMA_Theme_Manager::$theme
  161. * @uses PMA_Theme_Manager::$theme_default
  162. * @uses PMA_Theme::getId()
  163. */
  164. function setThemeCookie()
  165. {
  166. PMA_setCookie($this->getThemeCookieName(), $this->theme->id,
  167. $this->theme_default);
  168. return true;
  169. }
  170. /**
  171. * old PHP 4 constructor
  172. */
  173. function PMA_Theme_Manager()
  174. {
  175. $this->__construct();
  176. }
  177. /**
  178. * @private
  179. * @param string $folder
  180. * @return boolean
  181. */
  182. /*private*/ function _checkThemeFolder($folder)
  183. {
  184. if (! is_dir($folder)) {
  185. $GLOBALS['PMA_errors'][] =
  186. sprintf($GLOBALS['strThemePathNotFound'],
  187. htmlspecialchars($folder));
  188. trigger_error(
  189. sprintf($GLOBALS['strThemePathNotFound'],
  190. htmlspecialchars($folder)),
  191. E_USER_WARNING);
  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 enclsoed
  240. *
  241. * @param boolean $form wether 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. // load css for this them 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. ?>