2.11.11.3 release
[phpmyadmin/arisferyanto.git] / libraries / Theme_Manager.class.php
blob064ed2e9a777ea596911763da69d39a9d09449b7
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 */
8 /**
11 require_once './libraries/Theme.class.php';
13 /**
16 class PMA_Theme_Manager
18 /**
19 * @var string path to theme folder
20 * @protected
22 var $_themes_path;
24 /**
25 * @var array available themes
27 var $themes = array();
29 /**
30 * @var string cookie name
32 var $cookie_name = 'pma_theme';
34 /**
35 * @var boolean
37 var $per_server = false;
39 /**
40 * @var string name of active theme
42 var $active_theme = '';
44 /**
45 * @var object PMA_Theme active theme
47 var $theme = null;
49 /**
50 * @var string
52 var $theme_default = 'original';
54 function __construct()
56 $this->init();
59 /**
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)) {
68 return false;
71 $this->_themes_path = trim($path);
72 return true;
75 /**
76 * @public
77 * @return string
79 function getThemesPath()
81 return $this->_themes_path;
84 /**
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;
94 function init()
96 $this->themes = array();
97 $this->theme_default = 'original';
98 $this->active_theme = '';
100 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
101 return false;
104 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
106 $this->loadThemes();
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']));
114 trigger_error(
115 sprintf($GLOBALS['strThemeDefaultNotFound'],
116 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])),
117 E_USER_WARNING);
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']);
129 } else {
130 // or original theme
131 $this->setActiveTheme('original');
136 function checkConfig()
138 if ($this->_themes_path != trim($GLOBALS['cfg']['ThemePath'])
139 || $this->theme_default != $GLOBALS['cfg']['ThemeDefault']) {
140 $this->init();
141 } else {
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
145 $this->loadThemes();
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 */
155 /* trigger_error(
156 sprintf($GLOBALS['strThemeNotFound'], htmlspecialchars($theme)),
157 E_USER_WARNING);*/
158 return false;
161 $this->active_theme = $theme;
162 $this->theme = $this->themes[$theme];
164 // need to set later
165 //$this->setThemeCookie();
167 return true;
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'];
178 } else {
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()];
193 return false;
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);
212 return true;
216 * old PHP 4 constructor
218 function PMA_Theme_Manager()
220 $this->__construct();
224 * @private
225 * @param string $folder
226 * @return boolean
228 /*private*/ function _checkThemeFolder($folder)
230 if (! is_dir($folder)) {
231 $GLOBALS['PMA_errors'][] =
232 sprintf($GLOBALS['strThemePathNotFound'],
233 htmlspecialchars($folder));
234 trigger_error(
235 sprintf($GLOBALS['strThemePathNotFound'],
236 htmlspecialchars($folder)),
237 E_USER_WARNING);
238 return false;
241 return true;
245 * read all themes
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];
257 continue;
259 $new_theme = PMA_Theme::load($this->getThemesPath() . '/' . $PMA_Theme);
260 if ($new_theme) {
261 $new_theme->setId($PMA_Theme);
262 $this->themes[$PMA_Theme] = $new_theme;
264 } // end get themes
265 closedir($handleThemes);
266 } else {
267 trigger_error(
268 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
269 E_USER_WARNING);
270 return false;
271 } // end check for themes directory
273 ksort($this->themes);
274 return true;
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)) {
285 return false;
288 return true;
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)
298 $select_box = '';
300 if ($form) {
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');"
310 . '">';
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>';
324 if ($form) {
325 $select_box .= '<noscript><input type="submit" value="' . $GLOBALS['strGo'] . '" /></noscript>';
326 $select_box .= '</form>';
329 return $select_box;
333 * enables backward compatibility
335 function makeBc()
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'];
374 return false;
378 * prints css data
380 function printCss($type)
382 if ($this->theme->loadCss($type)) {
383 return true;
386 // load css for this them failed, try default theme css
387 $fallback_theme = $this->getFallBackTheme();
388 if ($fallback_theme && $fallback_theme->loadCss($type)) {
389 return true;
392 return false;