security fixes
[phpmyadmin/crack.git] / libraries / Theme_Manager.class.php
blob7df805a8fb8516ba5546f0e31e00f4fc56ac5d96
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 require_once './libraries/Theme.class.php';
7 class PMA_Theme_Manager {
9 /**
10 * @var string path to theme folder
11 * @protected
13 var $_themes_path;
15 /**
16 * @var array available themes
18 var $themes = array();
20 /**
21 * @var string cookie name
23 var $cookie_name = 'pma_theme';
25 /**
26 * @var boolean
28 var $per_server = false;
30 /**
31 * @var string name of active theme
33 var $active_theme = '';
35 /**
36 * @var object PMA_Theme active theme
38 var $theme = null;
40 /**
41 * @var string
43 var $theme_default = 'original';
45 function __construct()
47 $this->init();
50 /**
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)) {
59 return false;
62 $this->_themes_path = trim($path);
63 return true;
66 /**
67 * @public
68 * @return string
70 function getThemesPath()
72 return $this->_themes_path;
75 /**
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;
85 function init()
87 $this->themes = array();
88 $this->theme_default = 'original';
89 $this->active_theme = '';
91 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
92 return false;
95 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
97 $this->loadThemes();
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']));
105 trigger_error(
106 sprintf($GLOBALS['strThemeDefaultNotFound'],
107 htmlspecialchars($GLOBALS['cfg']['ThemeDefault'])),
108 E_USER_WARNING);
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']);
120 } else {
121 // or original theme
122 $this->setActiveTheme('original');
127 function checkConfig()
129 if ($this->_themes_path != trim($GLOBALS['cfg']['ThemePath'])
130 || $this->theme_default != $GLOBALS['cfg']['ThemeDefault']) {
131 $this->init();
132 } else {
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
136 $this->loadThemes();
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 */
146 /* trigger_error(
147 sprintf($GLOBALS['strThemeNotFound'], htmlspecialchars($theme)),
148 E_USER_WARNING);*/
149 return false;
152 $this->active_theme = $theme;
153 $this->theme = $this->themes[$theme];
155 // need to set later
156 //$this->setThemeCookie();
158 return true;
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'];
169 } else {
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()];
184 return false;
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);
200 return true;
204 * old PHP 4 constructor
206 function PMA_Theme_Manager()
208 $this->__construct();
212 * @private
213 * @param string $folder
214 * @return boolean
216 /*private*/ function _checkThemeFolder($folder)
218 if (! is_dir($folder)) {
219 $GLOBALS['PMA_errors'][] =
220 sprintf($GLOBALS['strThemePathNotFound'],
221 htmlspecialchars($folder));
222 trigger_error(
223 sprintf($GLOBALS['strThemePathNotFound'],
224 htmlspecialchars($folder)),
225 E_USER_WARNING);
226 return false;
229 return true;
233 * read all themes
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];
245 continue;
247 $new_theme = PMA_Theme::load($this->getThemesPath() . '/' . $PMA_Theme);
248 if ($new_theme) {
249 $new_theme->setId($PMA_Theme);
250 $this->themes[$PMA_Theme] = $new_theme;
252 } // end get themes
253 closedir($handleThemes);
254 } else {
255 trigger_error(
256 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
257 E_USER_WARNING);
258 return false;
259 } // end check for themes directory
261 ksort($this->themes);
262 return true;
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)) {
273 return false;
276 return true;
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)
286 $select_box = '';
288 if ($form) {
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');"
298 . '">';
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>';
312 if ($form) {
313 $select_box .= '<noscript><input type="submit" value="' . $GLOBALS['strGo'] . '" /></noscript>';
314 $select_box .= '</form>';
317 return $select_box;
321 * enables backward compatibility
323 function makeBc()
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'];
362 return false;
366 * prints css data
368 function printCss($type)
370 if ($this->theme->loadCss($type)) {
371 return true;
374 // load css for this them failed, try default theme css
375 $fallback_theme = $this->getFallBackTheme();
376 if ($fallback_theme && $fallback_theme->loadCss($type)) {
377 return true;
380 return false;