source: www/mysql/libraries/Theme_Manager.class.php@ 1

Last change on this file since 1 was 1, checked in by george, 17 years ago

Prvotní import všeho

File size: 10.3 KB
Line 
1<?php
2/* $Id: Theme_Manager.class.php 9839 2007-01-12 18:41:38Z lem9 $ */
3// vim: expandtab sw=4 ts=4 sts=4:
4
5require_once './libraries/Theme.class.php';
6
7class PMA_Theme_Manager {
8
9 /**
10 * @var string path to theme folder
11 * @protected
12 */
13 var $_themes_path;
14
15 /**
16 * @var array available themes
17 */
18 var $themes = array();
19
20 /**
21 * @var string cookie name
22 */
23 var $cookie_name = 'pma_theme';
24
25 /**
26 * @var boolean
27 */
28 var $per_server = false;
29
30 /**
31 * @var string name of active theme
32 */
33 var $active_theme = '';
34
35 /**
36 * @var object PMA_Theme active theme
37 */
38 var $theme = null;
39
40 /**
41 * @var string
42 */
43 var $theme_default = 'original';
44
45 function __construct()
46 {
47 $this->init();
48 }
49
50 /**
51 * sets path to folder containing the themes
52 *
53 * @param string $path path to themes folder
54 * @return boolean success
55 */
56 function setThemesPath($path)
57 {
58 if (! $this->_checkThemeFolder($path)) {
59 return false;
60 }
61
62 $this->_themes_path = trim($path);
63 return true;
64 }
65
66 /**
67 * @public
68 * @return string
69 */
70 function getThemesPath()
71 {
72 return $this->_themes_path;
73 }
74
75 /**
76 * sets if there are different themes per server
77 *
78 * @param boolean $per_server
79 */
80 function setThemePerServer($per_server)
81 {
82 $this->per_server = (bool) $per_server;
83 }
84
85 function init()
86 {
87 $this->themes = array();
88 $this->theme_default = 'original';
89 $this->active_theme = '';
90
91 if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
92 return false;
93 }
94
95 $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
96
97 $this->loadThemes();
98
99 $this->theme = new PMA_Theme;
100
101
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;
110 }
111
112 $this->theme_default = $GLOBALS['cfg']['ThemeDefault'];
113
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');
123 }
124 }
125 }
126
127 function checkConfig()
128 {
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();
137 }
138 }
139
140 function setActiveTheme($theme = null)
141 {
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;
150 }
151
152 $this->active_theme = $theme;
153 $this->theme = $this->themes[$theme];
154
155 // need to set later
156 //$this->setThemeCookie();
157
158 return true;
159 }
160
161 /**
162 * @return string cookie name
163 */
164 function getThemeCookieName()
165 {
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;
171 }
172 }
173
174 /**
175 * returns name of theme stored in the cookie
176 * @return string theme name from cookie
177 */
178 function getThemeCookie()
179 {
180 if (isset($_COOKIE[$this->getThemeCookieName()])) {
181 return $_COOKIE[$this->getThemeCookieName()];
182 }
183
184 return false;
185 }
186
187 /**
188 * save theme in cookie
189 *
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()
195 */
196 function setThemeCookie()
197 {
198 PMA_setCookie($this->getThemeCookieName(), $this->theme->id,
199 $this->theme_default);
200 // force a change of a dummy session variable to avoid problems
201 // with the caching of phpmyadmin.css.php
202 $_SESSION['PMA_Config']->set('theme-update', $this->theme->id);
203 return true;
204 }
205
206 /**
207 * old PHP 4 constructor
208 */
209 function PMA_Theme_Manager()
210 {
211 $this->__construct();
212 }
213
214 /**
215 * @private
216 * @param string $folder
217 * @return boolean
218 */
219 /*private*/ function _checkThemeFolder($folder)
220 {
221 if (! is_dir($folder)) {
222 $GLOBALS['PMA_errors'][] =
223 sprintf($GLOBALS['strThemePathNotFound'],
224 htmlspecialchars($folder));
225 trigger_error(
226 sprintf($GLOBALS['strThemePathNotFound'],
227 htmlspecialchars($folder)),
228 E_USER_WARNING);
229 return false;
230 }
231
232 return true;
233 }
234
235 /**
236 * read all themes
237 */
238 function loadThemes()
239 {
240 $this->themes = array();
241
242 if ($handleThemes = opendir($this->getThemesPath())) {
243 // check for themes directory
244 while (false !== ($PMA_Theme = readdir($handleThemes))) {
245 if (array_key_exists($PMA_Theme, $this->themes)) {
246 // this does nothing!
247 //$this->themes[$PMA_Theme] = $this->themes[$PMA_Theme];
248 continue;
249 }
250 $new_theme = PMA_Theme::load($this->getThemesPath() . '/' . $PMA_Theme);
251 if ($new_theme) {
252 $new_theme->setId($PMA_Theme);
253 $this->themes[$PMA_Theme] = $new_theme;
254 }
255 } // end get themes
256 closedir($handleThemes);
257 } else {
258 trigger_error(
259 'phpMyAdmin-ERROR: cannot open themes folder: ' . $this->getThemesPath(),
260 E_USER_WARNING);
261 return false;
262 } // end check for themes directory
263
264 ksort($this->themes);
265 return true;
266 }
267
268 /**
269 * checks if given theme name is a known theme
270 *
271 * @param string $theme name fo theme to check for
272 */
273 function checkTheme($theme)
274 {
275 if (! array_key_exists($theme, $this->themes)) {
276 return false;
277 }
278
279 return true;
280 }
281
282 /**
283 * returns HTML selectbox, with or without form enclsoed
284 *
285 * @param boolean $form wether enclosed by from tags or not
286 */
287 function getHtmlSelectBox($form = true)
288 {
289 $select_box = '';
290
291 if ($form) {
292 $select_box .= '<form name="setTheme" method="post" action="index.php"'
293 .' target="_parent">';
294 $select_box .= PMA_generate_common_hidden_inputs();
295 }
296
297 $theme_selected = FALSE;
298 $theme_preview_path= './themes.php';
299 $theme_preview_href = '<a href="' . $theme_preview_path . '" target="themes" onclick="'
300 . "window.open('" . $theme_preview_path . "','themes','left=10,top=20,width=510,height=350,scrollbars=yes,status=yes,resizable=yes');"
301 . '">';
302 $select_box .= $theme_preview_href . $GLOBALS['strTheme'] . '</a>:' . "\n";
303
304 $select_box .= '<select name="set_theme" xml:lang="en" dir="ltr"'
305 .' onchange="this.form.submit();" >';
306 foreach ($this->themes as $each_theme_id => $each_theme) {
307 $select_box .= '<option value="' . $each_theme_id . '"';
308 if ($this->active_theme === $each_theme_id) {
309 $select_box .= ' selected="selected"';
310 }
311 $select_box .= '>' . htmlspecialchars($each_theme->getName()) . '</option>';
312 }
313 $select_box .= '</select>';
314
315 if ($form) {
316 $select_box .= '<noscript><input type="submit" value="' . $GLOBALS['strGo'] . '" /></noscript>';
317 $select_box .= '</form>';
318 }
319
320 return $select_box;
321 }
322
323 /**
324 * enables backward compatibility
325 */
326 function makeBc()
327 {
328 $GLOBALS['theme'] = $this->theme->getId();
329 $GLOBALS['pmaThemePath'] = $this->theme->getPath();
330 $GLOBALS['pmaThemeImage'] = $this->theme->getImgPath();
331
332 /**
333 * load layout file if exists
334 */
335 if (@file_exists($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
336 include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
337 }
338
339
340 }
341
342 /**
343 * prints out preview for every theme
344 *
345 * @uses $this->themes
346 * @uses PMA_Theme::printPreview()
347 */
348 function printPreviews()
349 {
350 foreach ($this->themes as $each_theme) {
351 $each_theme->printPreview();
352 } // end 'open themes'
353 }
354
355 /**
356 * returns PMA_Theme object for fall back theme
357 * @return object PMA_Theme
358 */
359 function getFallBackTheme()
360 {
361 if (isset($this->themes['original'])) {
362 return $this->themes['original'];
363 }
364
365 return false;
366 }
367
368 /**
369 * prints css data
370 */
371 function printCss($type)
372 {
373 if ($this->theme->loadCss($type)) {
374 return true;
375 }
376
377 // load css for this them failed, try default theme css
378 $fallback_theme = $this->getFallBackTheme();
379 if ($fallback_theme && $fallback_theme->loadCss($type)) {
380 return true;
381 }
382
383 return false;
384 }
385}
386?>
Note: See TracBrowser for help on using the repository browser.