Let's again obey TextareaRows (bug #1465906).
[phpmyadmin/crack.git] / libraries / Theme_Manager.class.php
blobbebf690163346ee23d86f36687ad22b5e2731319
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 trigger_error(
146 sprintf($GLOBALS['strThemeNotFound'], htmlspecialchars($theme)),
147 E_USER_WARNING);
148 return false;
151 $this->active_theme = $theme;
152 $this->theme = $this->themes[$theme];
154 // need to set later
155 //$this->setThemeCookie();
157 return true;
161 * @return string cookie name
163 function getThemeCookieName()
165 // Allow different theme per server
166 if (isset($GLOBALS['server']) && $this->per_server) {
167 return $this->cookie_name . '-' . $GLOBALS['server'];
168 } else {
169 return $this->cookie_name;
174 * returns name of theme stored in the cookie
175 * @return string theme name from cookie
177 function getThemeCookie()
179 if (isset($_COOKIE[$this->getThemeCookieName()])) {
180 return $_COOKIE[$this->getThemeCookieName()];
183 return false;
187 * save theme in cookie
189 * @uses PMA_setCookie();
190 * @uses PMA_Theme_Manager::getThemeCookieName()
191 * @uses PMA_Theme_Manager::$theme
192 * @uses PMA_Theme_Manager::$theme_default
193 * @uses PMA_Theme::getId()
195 function setThemeCookie()
197 PMA_setCookie($this->getThemeCookieName(), $this->theme->id,
198 $this->theme_default);
199 return true;
203 * old PHP 4 constructor
205 function PMA_Theme_Manager()
207 $this->__construct();
211 * @private
212 * @param string $folder
213 * @return boolean
215 /*private*/ function _checkThemeFolder($folder)
217 if (! is_dir($folder)) {
218 $GLOBALS['PMA_errors'][] =
219 sprintf($GLOBALS['strThemePathNotFound'],
220 htmlspecialchars($folder));
221 trigger_error(
222 sprintf($GLOBALS['strThemePathNotFound'],
223 htmlspecialchars($folder)),
224 E_USER_WARNING);
225 return false;
228 return true;
232 * read all themes
234 function loadThemes()
236 $this->themes = array();
238 if ($handleThemes = opendir($this->getThemesPath())) {
239 // check for themes directory
240 while (false !== ($PMA_Theme = readdir($handleThemes))) {
241 if (array_key_exists($PMA_Theme, $this->themes)) {
242 // this does nothing!
243 //$this->themes[$PMA_Theme] = $this->themes[$PMA_Theme];
244 continue;
246 $new_theme = PMA_Theme::load($this->getThemesPath() . '/' . $PMA_Theme);
247 if ($new_theme) {
248 $new_theme->setId($PMA_Theme);
249 $this->themes[$PMA_Theme] = $new_theme;
251 } // end get themes
252 closedir($handleThemes);
253 } else {
254 trigger_error(
255 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
256 E_USER_WARNING);
257 return false;
258 } // end check for themes directory
260 ksort($this->themes);
261 return true;
265 * checks if given theme name is a known theme
267 * @param string $theme name fo theme to check for
269 function checkTheme($theme)
271 if (! array_key_exists($theme, $this->themes)) {
272 return false;
275 return true;
279 * returns HTML selectbox, with or without form enclsoed
281 * @param boolean $form wether enclosed by from tags or not
283 function getHtmlSelectBox($form = true)
285 $select_box = '';
287 if ($form) {
288 $select_box .= '<form name="setTheme" method="post" action="index.php"'
289 .' target="_parent">';
290 $select_box .= PMA_generate_common_hidden_inputs();
293 $theme_selected = FALSE;
294 $theme_preview_path= './themes.php';
295 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" onclick="'
296 . "window.open('" . $theme_preview_path . "','themes','left=10,top=20,width=510,height=350,scrollbars=yes,status=yes,resizable=yes');"
297 . '">';
298 $select_box .= $theme_preview_href . $GLOBALS['strTheme'] . '</a>:' . "\n";
300 $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr"'
301 .' onchange="this.form.submit();" >';
302 foreach ($this->themes as $each_theme_id => $each_theme) {
303 $select_box .= '<option value="' . $each_theme_id . '"';
304 if ($this->active_theme === $each_theme_id) {
305 $select_box .= ' selected="selected"';
307 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
309 $select_box .= '</select>';
311 if ($form) {
312 $select_box .= '<noscript><input type="submit" value="' . $GLOBALS['strGo'] . '" /></noscript>';
313 $select_box .= '</form>';
316 return $select_box;
320 * enables backward compatibility
322 function makeBc()
324 $GLOBALS['theme'] = $this->theme->getId();
325 $GLOBALS['pmaThemePath'] = $this->theme->getPath();
326 $GLOBALS['pmaThemeImage'] = $this->theme->getImgPath();
329 * load layout file if exists
331 if (@file_exists($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
332 include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
339 * prints out preview for every theme
341 * @uses $this->themes
342 * @uses PMA_Theme::printPreview()
344 function printPreviews()
346 foreach ($this->themes as $each_theme) {
347 $each_theme->printPreview();
348 } // end 'open themes'
352 * returns PMA_Theme object for fall back theme
353 * @return object PMA_Theme
355 function getFallBackTheme()
357 if (isset($this->themes['original'])) {
358 return $this->themes['original'];
361 return false;
365 * prints css data
367 function printCss($type)
369 if ($this->theme->loadCss($type)) {
370 return true;
373 // load css for this them failed, try default theme css
374 $fallback_theme = $this->getFallBackTheme();
375 if ($fallback_theme && $fallback_theme->loadCss($type)) {
376 return true;
379 return false;