Merge branch 'QA_4_4' into QA_4_5
[phpmyadmin.git] / libraries / Theme.class.php
blob4a9d8db122b4f8d88666a3e38c4941621564c16f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * hold PMA_Theme class
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * handles theme
15 * @todo add the possibility to make a theme depend on another theme
16 * and by default on original
17 * @todo make all components optional - get missing components from 'parent' theme
19 * @package PhpMyAdmin
21 class PMA_Theme
23 /**
24 * @var string theme version
25 * @access protected
27 var $version = '0.0.0.0';
29 /**
30 * @var string theme name
31 * @access protected
33 var $name = '';
35 /**
36 * @var string theme id
37 * @access protected
39 var $id = '';
41 /**
42 * @var string theme path
43 * @access protected
45 var $path = '';
47 /**
48 * @var string image path
49 * @access protected
51 var $img_path = '';
53 /**
54 * @var integer last modification time for info file
55 * @access protected
57 var $mtime_info = 0;
59 /**
60 * needed because sometimes, the mtime for different themes
61 * is identical
62 * @var integer filesize for info file
63 * @access protected
65 var $filesize_info = 0;
67 /**
68 * @var array List of css files to load
69 * @access private
71 private $_cssFiles = array(
72 'common',
73 'enum_editor',
74 'gis',
75 'navigation',
76 'pmd',
77 'rte',
78 'codemirror',
79 'jqplot',
80 'resizable-menu'
83 /**
84 * Loads theme information
86 * @return boolean whether loading them info was successful or not
87 * @access public
89 function loadInfo()
91 if (! file_exists($this->getPath() . '/info.inc.php')) {
92 return false;
95 if ($this->mtime_info === filemtime($this->getPath() . '/info.inc.php')) {
96 return true;
99 @include $this->getPath() . '/info.inc.php';
101 // was it set correctly?
102 if (! isset($theme_name)) {
103 return false;
106 $this->mtime_info = filemtime($this->getPath() . '/info.inc.php');
107 $this->filesize_info = filesize($this->getPath() . '/info.inc.php');
109 if (isset($theme_full_version)) {
110 $this->setVersion($theme_full_version);
111 } elseif (isset($theme_generation, $theme_version)) {
112 $this->setVersion($theme_generation . '.' . $theme_version);
114 $this->setName($theme_name);
116 return true;
120 * returns theme object loaded from given folder
121 * or false if theme is invalid
123 * @param string $folder path to theme
125 * @return PMA_Theme|false
126 * @static
127 * @access public
129 static public function load($folder)
131 $theme = new PMA_Theme();
133 $theme->setPath($folder);
135 if (! $theme->loadInfo()) {
136 return false;
139 $theme->checkImgPath();
141 return $theme;
145 * checks image path for existence - if not found use img from fallback theme
147 * @access public
148 * @return bool
150 public function checkImgPath()
152 // try current theme first
153 if (is_dir($this->getPath() . '/img/')) {
154 $this->setImgPath($this->getPath() . '/img/');
155 return true;
158 // try fallback theme
159 $fallback = $GLOBALS['cfg']['ThemePath'] . '/'
160 . PMA_Theme_Manager::FALLBACK_THEME
161 . '/img/';
162 if (is_dir($fallback)) {
163 $this->setImgPath($fallback);
164 return true;
167 // we failed
168 trigger_error(
169 sprintf(
170 __('No valid image path for theme %s found!'),
171 $this->getName()
173 E_USER_ERROR
175 return false;
179 * returns path to theme
181 * @access public
182 * @return string path to theme
184 public function getPath()
186 return $this->path;
190 * returns layout file
192 * @access public
193 * @return string layout file
195 public function getLayoutFile()
197 return $this->getPath() . '/layout.inc.php';
201 * set path to theme
203 * @param string $path path to theme
205 * @return void
206 * @access public
208 public function setPath($path)
210 $this->path = trim($path);
214 * sets version
216 * @param string $version version to set
218 * @return void
219 * @access public
221 public function setVersion($version)
223 $this->version = trim($version);
227 * returns version
229 * @return string version
230 * @access public
232 public function getVersion()
234 return $this->version;
238 * checks theme version against $version
239 * returns true if theme version is equal or higher to $version
241 * @param string $version version to compare to
243 * @return boolean true if theme version is equal or higher to $version
244 * @access public
246 public function checkVersion($version)
248 return version_compare($this->getVersion(), $version, 'lt');
252 * sets name
254 * @param string $name name to set
256 * @return void
257 * @access public
259 public function setName($name)
261 $this->name = trim($name);
265 * returns name
267 * @access public
268 * @return string name
270 public function getName()
272 return $this->name;
276 * sets id
278 * @param string $id new id
280 * @return void
281 * @access public
283 public function setId($id)
285 $this->id = trim($id);
289 * returns id
291 * @return string id
292 * @access public
294 public function getId()
296 return $this->id;
300 * Sets path to images for the theme
302 * @param string $path path to images for this theme
304 * @return void
305 * @access public
307 public function setImgPath($path)
309 $this->img_path = $path;
313 * Returns the path to image for the theme.
314 * If filename is given, it possibly fallbacks to fallback
315 * theme for it if image does not exist.
317 * @param string $file file name for image
319 * @access public
320 * @return string image path for this theme
322 public function getImgPath($file = null)
324 if (is_null($file)) {
325 return $this->img_path;
328 if (is_readable($this->img_path . $file)) {
329 return $this->img_path . $file;
332 return $GLOBALS['cfg']['ThemePath'] . '/'
333 . PMA_Theme_Manager::FALLBACK_THEME . '/img/' . $file;
337 * load css (send to stdout, normally the browser)
339 * @return bool
340 * @access public
342 public function loadCss()
344 $success = true;
346 if ($GLOBALS['text_dir'] === 'ltr') {
347 $right = 'right';
348 $left = 'left';
349 } else {
350 $right = 'left';
351 $left = 'right';
354 foreach ($this->_cssFiles as $file) {
355 $path = $this->getPath() . "/css/$file.css.php";
356 $fallback = "./themes/"
357 . PMA_Theme_Manager::FALLBACK_THEME . "/css/$file.css.php";
359 if (is_readable($path)) {
360 echo "\n/* FILE: $file.css.php */\n";
361 include $path;
362 } else if (is_readable($fallback)) {
363 echo "\n/* FILE: $file.css.php */\n";
364 include $fallback;
365 } else {
366 $success = false;
370 include './themes/sprites.css.php';
372 return $success;
376 * Renders the preview for this theme
378 * @return string
379 * @access public
381 public function getPrintPreview()
383 $url_params = array('set_theme' => $this->getId());
384 $url = 'index.php' . PMA_URL_getCommon($url_params);
386 $retval = '<div class="theme_preview">';
387 $retval .= '<h2>';
388 $retval .= htmlspecialchars($this->getName());
389 $retval .= ' (' . htmlspecialchars($this->getVersion()) . ') ';
390 $retval .= '</h2>';
391 $retval .= '<p>';
392 $retval .= '<a class="take_theme" ';
393 $retval .= 'name="' . htmlspecialchars($this->getId()) . '" ';
394 $retval .= 'href="' . $url . '">';
395 if (@file_exists($this->getPath() . '/screen.png')) {
396 // if screen exists then output
397 $retval .= '<img src="' . $this->getPath() . '/screen.png" border="1"';
398 $retval .= ' alt="' . htmlspecialchars($this->getName()) . '"';
399 $retval .= ' title="' . htmlspecialchars($this->getName()) . '" />';
400 $retval .= '<br />';
401 } else {
402 $retval .= __('No preview available.');
404 $retval .= '[ <strong>' . __('take it') . '</strong> ]';
405 $retval .= '</a>';
406 $retval .= '</p>';
407 $retval .= '</div>';
408 return $retval;
412 * Remove filter for IE.
414 * @return string CSS code.
416 function getCssIEClearFilter()
418 return PMA_USR_BROWSER_AGENT == 'IE'
419 && PMA_USR_BROWSER_VER >= 6
420 && PMA_USR_BROWSER_VER <= 8
421 ? 'filter: none'
422 : '';
426 * Gets currently configured font size.
428 * @return String with font size.
430 function getFontSize()
432 $fs = $GLOBALS['PMA_Config']->get('fontsize');
433 if (!is_null($fs)) {
434 return $fs;
436 if (isset($_COOKIE['pma_fontsize'])) {
437 return htmlspecialchars($_COOKIE['pma_fontsize']);
439 return '82%';
443 * Generates code for CSS gradient using various browser extensions.
445 * @param string $start_color Color of gradient start, hex value without #
446 * @param string $end_color Color of gradient end, hex value without #
448 * @return string CSS code.
450 function getCssGradient($start_color, $end_color)
452 $result = array();
453 // Opera 9.5+, IE 9
454 $result[] = 'background-image: url(./themes/svg_gradient.php?from='
455 . $start_color . '&to=' . $end_color . ');';
456 $result[] = 'background-size: 100% 100%;';
457 // Safari 4-5, Chrome 1-9
458 $result[] = 'background: '
459 . '-webkit-gradient(linear, left top, left bottom, from(#'
460 . $start_color . '), to(#' . $end_color . '));';
461 // Safari 5.1, Chrome 10+
462 $result[] = 'background: -webkit-linear-gradient(top, #'
463 . $start_color . ', #' . $end_color . ');';
464 // Firefox 3.6+
465 $result[] = 'background: -moz-linear-gradient(top, #'
466 . $start_color . ', #' . $end_color . ');';
467 // IE 10
468 $result[] = 'background: -ms-linear-gradient(top, #'
469 . $start_color . ', #' . $end_color . ');';
470 // Opera 11.10
471 $result[] = 'background: -o-linear-gradient(top, #'
472 . $start_color . ', #' . $end_color . ');';
473 // IE 6-8
474 if (PMA_USR_BROWSER_AGENT == 'IE'
475 && PMA_USR_BROWSER_VER >= 6
476 && PMA_USR_BROWSER_VER <= 8
478 $result[] = 'filter: '
479 . 'progid:DXImageTransform.Microsoft.gradient(startColorstr="#'
480 . $start_color . '", endColorstr="#' . $end_color . '");';
482 return implode("\n", $result);