2 /* vim: set expandtab sw=4 ts=4 sts=4: */
11 require_once './libraries/Theme.class.php';
16 class PMA_Theme_Manager
19 * @var string path to theme folder
25 * @var array available themes
27 var $themes = array();
30 * @var string cookie name
32 var $cookie_name = 'pma_theme';
37 var $per_server = false;
40 * @var string name of active theme
42 var $active_theme = '';
45 * @var object PMA_Theme active theme
52 var $theme_default = 'original';
54 function __construct()
60 * sets path to folder containing the themes
62 * @param string $path path to themes folder
63 * @return boolean success
65 function setThemesPath($path)
67 if (! $this->_checkThemeFolder($path)) {
71 $this->_themes_path
= trim($path);
79 function getThemesPath()
81 return $this->_themes_path
;
85 * sets if there are different themes per server
87 * @param boolean $per_server
89 function setThemePerServer($per_server)
91 $this->per_server
= (bool) $per_server;
96 $this->themes
= array();
97 $this->theme_default
= 'original';
98 $this->active_theme
= '';
100 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
104 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
108 $this->theme
= new PMA_Theme
;
111 if (! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
112 $GLOBALS['PMA_errors'][] = sprintf($GLOBALS['strThemeDefaultNotFound'],
113 htmlspecialchars($GLOBALS['cfg']['ThemeDefault']));
115 sprintf($GLOBALS['strThemeDefaultNotFound'],
116 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])),
118 $GLOBALS['cfg']['ThemeDefault'] = false;
121 $this->theme_default
= $GLOBALS['cfg']['ThemeDefault'];
123 // check if user have a theme cookie
124 if (! $this->getThemeCookie()
125 ||
! $this->setActiveTheme($this->getThemeCookie())) {
126 // otherwise use default theme
127 if ($GLOBALS['cfg']['ThemeDefault']) {
128 $this->setActiveTheme($GLOBALS['cfg']['ThemeDefault']);
131 $this->setActiveTheme('original');
136 function checkConfig()
138 if ($this->_themes_path
!= trim($GLOBALS['cfg']['ThemePath'])
139 ||
$this->theme_default
!= $GLOBALS['cfg']['ThemeDefault']) {
142 // at least the theme path needs to be checked every time for new
143 // themes, as there is no other way at the moment to keep track of
144 // new or removed themes
149 function setActiveTheme($theme = null)
151 if (! $this->checkTheme($theme)) {
152 $GLOBALS['PMA_errors'][] = sprintf($GLOBALS['strThemeNotFound'],
153 htmlspecialchars($theme));
154 /* Following code can lead to path disclossure, because headers will be sent later */
156 sprintf($GLOBALS['strThemeNotFound'], htmlspecialchars($theme)),
161 $this->active_theme
= $theme;
162 $this->theme
= $this->themes
[$theme];
165 //$this->setThemeCookie();
171 * @return string cookie name
173 function getThemeCookieName()
175 // Allow different theme per server
176 if (isset($GLOBALS['server']) && $this->per_server
) {
177 return $this->cookie_name
. '-' . $GLOBALS['server'];
179 return $this->cookie_name
;
184 * returns name of theme stored in the cookie
185 * @return string theme name from cookie
187 function getThemeCookie()
189 if (isset($_COOKIE[$this->getThemeCookieName()])) {
190 return $_COOKIE[$this->getThemeCookieName()];
197 * save theme in cookie
199 * @uses PMA_setCookie();
200 * @uses PMA_Theme_Manager::getThemeCookieName()
201 * @uses PMA_Theme_Manager::$theme
202 * @uses PMA_Theme_Manager::$theme_default
203 * @uses PMA_Theme::getId()
205 function setThemeCookie()
207 PMA_setCookie($this->getThemeCookieName(), $this->theme
->id
,
208 $this->theme_default
);
209 // force a change of a dummy session variable to avoid problems
210 // with the caching of phpmyadmin.css.php
211 $_SESSION['PMA_Config']->set('theme-update', $this->theme
->id
);
216 * old PHP 4 constructor
218 function PMA_Theme_Manager()
220 $this->__construct();
225 * @param string $folder
228 /*private*/ function _checkThemeFolder($folder)
230 if (! is_dir($folder)) {
231 $GLOBALS['PMA_errors'][] =
232 sprintf($GLOBALS['strThemePathNotFound'],
233 htmlspecialchars($folder));
235 sprintf($GLOBALS['strThemePathNotFound'],
236 htmlspecialchars($folder)),
247 function loadThemes()
249 $this->themes
= array();
251 if ($handleThemes = opendir($this->getThemesPath())) {
252 // check for themes directory
253 while (false !== ($PMA_Theme = readdir($handleThemes))) {
254 if (array_key_exists($PMA_Theme, $this->themes
)) {
255 // this does nothing!
256 //$this->themes[$PMA_Theme] = $this->themes[$PMA_Theme];
259 $new_theme = PMA_Theme
::load($this->getThemesPath() . '/' . $PMA_Theme);
261 $new_theme->setId($PMA_Theme);
262 $this->themes
[$PMA_Theme] = $new_theme;
265 closedir($handleThemes);
268 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
271 } // end check for themes directory
273 ksort($this->themes
);
278 * checks if given theme name is a known theme
280 * @param string $theme name fo theme to check for
282 function checkTheme($theme)
284 if (! array_key_exists($theme, $this->themes
)) {
292 * returns HTML selectbox, with or without form enclsoed
294 * @param boolean $form wether enclosed by from tags or not
296 function getHtmlSelectBox($form = true)
301 $select_box .= '<form name="setTheme" method="post" action="index.php"'
302 .' target="_parent">';
303 $select_box .= PMA_generate_common_hidden_inputs();
306 $theme_selected = FALSE;
307 $theme_preview_path= './themes.php';
308 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" onclick="'
309 . "window.open('" . $theme_preview_path . "','themes','left=10,top=20,width=510,height=350,scrollbars=yes,status=yes,resizable=yes');"
311 $select_box .= $theme_preview_href . $GLOBALS['strTheme'] . '</a>:' . "\n";
313 $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr"'
314 .' onchange="this.form.submit();" >';
315 foreach ($this->themes
as $each_theme_id => $each_theme) {
316 $select_box .= '<option value="' . $each_theme_id . '"';
317 if ($this->active_theme
=== $each_theme_id) {
318 $select_box .= ' selected="selected"';
320 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
322 $select_box .= '</select>';
325 $select_box .= '<noscript><input type="submit" value="' . $GLOBALS['strGo'] . '" /></noscript>';
326 $select_box .= '</form>';
333 * enables backward compatibility
337 $GLOBALS['theme'] = $this->theme
->getId();
338 $GLOBALS['pmaThemePath'] = $this->theme
->getPath();
339 $GLOBALS['pmaThemeImage'] = $this->theme
->getImgPath();
342 * load layout file if exists
344 if (@file_exists
($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
345 include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
352 * prints out preview for every theme
354 * @uses $this->themes
355 * @uses PMA_Theme::printPreview()
357 function printPreviews()
359 foreach ($this->themes
as $each_theme) {
360 $each_theme->printPreview();
361 } // end 'open themes'
365 * returns PMA_Theme object for fall back theme
366 * @return object PMA_Theme
368 function getFallBackTheme()
370 if (isset($this->themes
['original'])) {
371 return $this->themes
['original'];
380 function printCss($type)
382 if ($this->theme
->loadCss($type)) {
386 // load css for this them failed, try default theme css
387 $fallback_theme = $this->getFallBackTheme();
388 if ($fallback_theme && $fallback_theme->loadCss($type)) {