3 // vim: expandtab sw=4 ts=4 sts=4:
5 require_once './libraries/Theme.class.php';
7 class PMA_Theme_Manager
{
10 * @var string path to theme folder
16 * @var array available themes
18 var $themes = array();
21 * @var string cookie name
23 var $cookie_name = 'pma_theme';
28 var $per_server = false;
31 * @var string name of active theme
33 var $active_theme = '';
36 * @var object PMA_Theme active theme
43 var $theme_default = 'original';
45 function __construct()
51 * sets path to folder containing the themes
53 * @param string $path path to themes folder
54 * @return boolean success
56 function setThemesPath($path)
58 if (! $this->_checkThemeFolder($path)) {
62 $this->_themes_path
= trim($path);
70 function getThemesPath()
72 return $this->_themes_path
;
76 * sets if there are different themes per server
78 * @param boolean $per_server
80 function setThemePerServer($per_server)
82 $this->per_server
= (bool) $per_server;
87 $this->themes
= array();
88 $this->theme_default
= 'original';
89 $this->active_theme
= '';
91 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
95 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
99 $this->theme
= new PMA_Theme
;
102 if ( ! $this->checkTheme($GLOBALS['cfg']['ThemeDefault'])) {
103 $GLOBALS['PMA_errors'][] = sprintf( $GLOBALS['strThemeDefaultNotFound'],
104 htmlspecialchars($GLOBALS['cfg']['ThemeDefault']));
106 sprintf($GLOBALS['strThemeDefaultNotFound'],
107 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])),
109 $GLOBALS['cfg']['ThemeDefault'] = false;
112 $this->theme_default
= $GLOBALS['cfg']['ThemeDefault'];
114 // check if user have a theme cookie
115 if (! $this->getThemeCookie()
116 ||
! $this->setActiveTheme($this->getThemeCookie())) {
117 // otherwise use default theme
118 if ($GLOBALS['cfg']['ThemeDefault']) {
119 $this->setActiveTheme($GLOBALS['cfg']['ThemeDefault']);
122 $this->setActiveTheme('original');
127 function checkConfig()
129 if ($this->_themes_path
!= trim($GLOBALS['cfg']['ThemePath'])
130 ||
$this->theme_default
!= $GLOBALS['cfg']['ThemeDefault']) {
133 // at least the theme path needs to be checked every time for new
134 // themes, as there is no other way at the moment to keep track of
135 // new or removed themes
140 function setActiveTheme($theme = null)
142 if ( ! $this->checkTheme($theme)) {
143 $GLOBALS['PMA_errors'][] = sprintf($GLOBALS['strThemeNotFound'],
144 htmlspecialchars($theme));
145 /* Following code can lead to path disclossure, because headers will be sent later */
147 sprintf($GLOBALS['strThemeNotFound'], htmlspecialchars($theme)),
152 $this->active_theme
= $theme;
153 $this->theme
= $this->themes
[$theme];
156 //$this->setThemeCookie();
162 * @return string cookie name
164 function getThemeCookieName()
166 // Allow different theme per server
167 if (isset($GLOBALS['server']) && $this->per_server
) {
168 return $this->cookie_name
. '-' . $GLOBALS['server'];
170 return $this->cookie_name
;
175 * returns name of theme stored in the cookie
176 * @return string theme name from cookie
178 function getThemeCookie()
180 if (isset($_COOKIE[$this->getThemeCookieName()])) {
181 return $_COOKIE[$this->getThemeCookieName()];
188 * save theme in cookie
190 * @uses PMA_setCookie();
191 * @uses PMA_Theme_Manager::getThemeCookieName()
192 * @uses PMA_Theme_Manager::$theme
193 * @uses PMA_Theme_Manager::$theme_default
194 * @uses PMA_Theme::getId()
196 function setThemeCookie()
198 PMA_setCookie($this->getThemeCookieName(), $this->theme
->id
,
199 $this->theme_default
);
204 * old PHP 4 constructor
206 function PMA_Theme_Manager()
208 $this->__construct();
213 * @param string $folder
216 /*private*/ function _checkThemeFolder($folder)
218 if (! is_dir($folder)) {
219 $GLOBALS['PMA_errors'][] =
220 sprintf($GLOBALS['strThemePathNotFound'],
221 htmlspecialchars($folder));
223 sprintf($GLOBALS['strThemePathNotFound'],
224 htmlspecialchars($folder)),
235 function loadThemes()
237 $this->themes
= array();
239 if ($handleThemes = opendir($this->getThemesPath())) {
240 // check for themes directory
241 while (false !== ($PMA_Theme = readdir($handleThemes))) {
242 if (array_key_exists($PMA_Theme, $this->themes
)) {
243 // this does nothing!
244 //$this->themes[$PMA_Theme] = $this->themes[$PMA_Theme];
247 $new_theme = PMA_Theme
::load($this->getThemesPath() . '/' . $PMA_Theme);
249 $new_theme->setId($PMA_Theme);
250 $this->themes
[$PMA_Theme] = $new_theme;
253 closedir($handleThemes);
256 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
259 } // end check for themes directory
261 ksort($this->themes
);
266 * checks if given theme name is a known theme
268 * @param string $theme name fo theme to check for
270 function checkTheme($theme)
272 if (! array_key_exists($theme, $this->themes
)) {
280 * returns HTML selectbox, with or without form enclsoed
282 * @param boolean $form wether enclosed by from tags or not
284 function getHtmlSelectBox($form = true)
289 $select_box .= '<form name="setTheme" method="post" action="index.php"'
290 .' target="_parent">';
291 $select_box .= PMA_generate_common_hidden_inputs();
294 $theme_selected = FALSE;
295 $theme_preview_path= './themes.php';
296 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" onclick="'
297 . "window.open('" . $theme_preview_path . "','themes','left=10,top=20,width=510,height=350,scrollbars=yes,status=yes,resizable=yes');"
299 $select_box .= $theme_preview_href . $GLOBALS['strTheme'] . '</a>:' . "\n";
301 $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr"'
302 .' onchange="this.form.submit();" >';
303 foreach ($this->themes
as $each_theme_id => $each_theme) {
304 $select_box .= '<option value="' . $each_theme_id . '"';
305 if ($this->active_theme
=== $each_theme_id) {
306 $select_box .= ' selected="selected"';
308 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
310 $select_box .= '</select>';
313 $select_box .= '<noscript><input type="submit" value="' . $GLOBALS['strGo'] . '" /></noscript>';
314 $select_box .= '</form>';
321 * enables backward compatibility
325 $GLOBALS['theme'] = $this->theme
->getId();
326 $GLOBALS['pmaThemePath'] = $this->theme
->getPath();
327 $GLOBALS['pmaThemeImage'] = $this->theme
->getImgPath();
330 * load layout file if exists
332 if (@file_exists
($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
333 include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
340 * prints out preview for every theme
342 * @uses $this->themes
343 * @uses PMA_Theme::printPreview()
345 function printPreviews()
347 foreach ($this->themes
as $each_theme) {
348 $each_theme->printPreview();
349 } // end 'open themes'
353 * returns PMA_Theme object for fall back theme
354 * @return object PMA_Theme
356 function getFallBackTheme()
358 if (isset($this->themes
['original'])) {
359 return $this->themes
['original'];
368 function printCss($type)
370 if ($this->theme
->loadCss($type)) {
374 // load css for this them failed, try default theme css
375 $fallback_theme = $this->getFallBackTheme();
376 if ($fallback_theme && $fallback_theme->loadCss($type)) {