home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / libraries / Theme_Manager.class.php < prev    next >
Encoding:
PHP Script  |  2008-06-23  |  10.3 KB  |  396 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  *
  5.  * @version $Id: Theme_Manager.class.php 10240 2007-04-01 11:02:46Z cybot_tm $
  6.  */
  7.  
  8. /**
  9.  *
  10.  */
  11. require_once './libraries/Theme.class.php';
  12.  
  13. /**
  14.  *
  15.  */
  16. class PMA_Theme_Manager
  17. {
  18.     /**
  19.      * @var string path to theme folder
  20.      * @protected
  21.      */
  22.     var $_themes_path;
  23.  
  24.     /**
  25.      * @var array available themes
  26.      */
  27.     var $themes = array();
  28.  
  29.     /**
  30.      * @var string  cookie name
  31.      */
  32.     var $cookie_name = 'pma_theme';
  33.  
  34.     /**
  35.      * @var boolean
  36.      */
  37.     var $per_server = false;
  38.  
  39.     /**
  40.      * @var string name of active theme
  41.      */
  42.     var $active_theme = '';
  43.  
  44.     /**
  45.      * @var object PMA_Theme active theme
  46.      */
  47.     var $theme = null;
  48.  
  49.     /**
  50.      * @var string
  51.      */
  52.     var $theme_default = 'original';
  53.  
  54.     function __construct()
  55.     {
  56.         $this->init();
  57.     }
  58.  
  59.     /**
  60.      * sets path to folder containing the themes
  61.      *
  62.      * @param   string  $path   path to themes folder
  63.      * @return  boolean success
  64.      */
  65.     function setThemesPath($path)
  66.     {
  67.         if (! $this->_checkThemeFolder($path)) {
  68.             return false;
  69.         }
  70.  
  71.         $this->_themes_path = trim($path);
  72.         return true;
  73.     }
  74.  
  75.     /**
  76.      * @public
  77.      * @return  string
  78.      */
  79.     function getThemesPath()
  80.     {
  81.         return $this->_themes_path;
  82.     }
  83.  
  84.     /**
  85.      * sets if there are different themes per server
  86.      *
  87.      * @param   boolean $per_server
  88.      */
  89.     function setThemePerServer($per_server)
  90.     {
  91.         $this->per_server  = (bool) $per_server;
  92.     }
  93.  
  94.     function init()
  95.     {
  96.         $this->themes = array();
  97.         $this->theme_default = 'original';
  98.         $this->active_theme = '';
  99.  
  100.         if (! $this->setThemesPath($GLOBALS['cfg']['ThemePath'])) {
  101.             return false;
  102.         }
  103.  
  104.         $this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
  105.  
  106.         $this->loadThemes();
  107.  
  108.         $this->theme = new PMA_Theme;
  109.  
  110.  
  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;
  119.         }
  120.  
  121.         $this->theme_default = $GLOBALS['cfg']['ThemeDefault'];
  122.  
  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');
  132.             }
  133.         }
  134.     }
  135.  
  136.     function checkConfig()
  137.     {
  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();
  146.         }
  147.     }
  148.  
  149.     function setActiveTheme($theme = null)
  150.     {
  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;
  159.         }
  160.  
  161.         $this->active_theme = $theme;
  162.         $this->theme = $this->themes[$theme];
  163.  
  164.         // need to set later
  165.         //$this->setThemeCookie();
  166.  
  167.         return true;
  168.     }
  169.  
  170.     /**
  171.      * @return  string  cookie name
  172.      */
  173.     function getThemeCookieName()
  174.     {
  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;
  180.         }
  181.     }
  182.  
  183.     /**
  184.      * returns name of theme stored in the cookie
  185.      * @return  string  theme name from cookie
  186.      */
  187.     function getThemeCookie()
  188.     {
  189.         if (isset($_COOKIE[$this->getThemeCookieName()])) {
  190.             return $_COOKIE[$this->getThemeCookieName()];
  191.         }
  192.  
  193.         return false;
  194.     }
  195.  
  196.     /**
  197.      * save theme in cookie
  198.      *
  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()
  204.      */
  205.     function setThemeCookie()
  206.     {
  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;
  213.     }
  214.  
  215.     /**
  216.      * old PHP 4 constructor
  217.      */
  218.     function PMA_Theme_Manager()
  219.     {
  220.         $this->__construct();
  221.     }
  222.  
  223.     /**
  224.      * @private
  225.      * @param   string $folder
  226.      * @return  boolean
  227.      */
  228.     /*private*/ function _checkThemeFolder($folder)
  229.     {
  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;
  239.         }
  240.  
  241.         return true;
  242.     }
  243.  
  244.     /**
  245.      * read all themes
  246.      */
  247.     function loadThemes()
  248.     {
  249.         $this->themes = array();
  250.  
  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;
  258.                 }
  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;
  263.                 }
  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
  272.  
  273.         ksort($this->themes);
  274.         return true;
  275.     }
  276.  
  277.     /**
  278.      * checks if given theme name is a known theme
  279.      *
  280.      * @param   string  $theme  name fo theme to check for
  281.      */
  282.     function checkTheme($theme)
  283.     {
  284.         if (! array_key_exists($theme, $this->themes)) {
  285.             return false;
  286.         }
  287.  
  288.         return true;
  289.     }
  290.  
  291.     /**
  292.      * returns HTML selectbox, with or without form enclsoed
  293.      *
  294.      * @param   boolean $form   wether enclosed by from tags or not
  295.      */
  296.     function getHtmlSelectBox($form = true)
  297.     {
  298.         $select_box = '';
  299.  
  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();
  304.         }
  305.  
  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";
  312.  
  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"';
  319.             }
  320.             $select_box .=  '>' . htmlspecialchars($each_theme->getName()) . '</option>';
  321.         }
  322.         $select_box .=  '</select>';
  323.  
  324.         if ($form) {
  325.             $select_box .=  '<noscript><input type="submit" value="' . $GLOBALS['strGo'] . '" /></noscript>';
  326.             $select_box .=  '</form>';
  327.         }
  328.  
  329.         return $select_box;
  330.     }
  331.  
  332.     /**
  333.      * enables backward compatibility
  334.      */
  335.     function makeBc()
  336.     {
  337.         $GLOBALS['theme']           = $this->theme->getId();
  338.         $GLOBALS['pmaThemePath']    = $this->theme->getPath();
  339.         $GLOBALS['pmaThemeImage']   = $this->theme->getImgPath();
  340.  
  341.         /**
  342.          * load layout file if exists
  343.          */
  344.         if (@file_exists($GLOBALS['pmaThemePath'] . 'layout.inc.php')) {
  345.             include $GLOBALS['pmaThemePath'] . 'layout.inc.php';
  346.         }
  347.  
  348.  
  349.     }
  350.  
  351.     /**
  352.      * prints out preview for every theme
  353.      *
  354.      * @uses    $this->themes
  355.      * @uses    PMA_Theme::printPreview()
  356.      */
  357.     function printPreviews()
  358.     {
  359.         foreach ($this->themes as $each_theme) {
  360.             $each_theme->printPreview();
  361.         } // end 'open themes'
  362.     }
  363.  
  364.     /**
  365.      * returns PMA_Theme object for fall back theme
  366.      * @return object   PMA_Theme
  367.      */
  368.     function getFallBackTheme()
  369.     {
  370.         if (isset($this->themes['original'])) {
  371.             return $this->themes['original'];
  372.         }
  373.  
  374.         return false;
  375.     }
  376.  
  377.     /**
  378.      * prints css data
  379.      */
  380.     function printCss($type)
  381.     {
  382.         if ($this->theme->loadCss($type)) {
  383.             return true;
  384.         }
  385.  
  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;
  390.         }
  391.  
  392.         return false;
  393.     }
  394. }
  395. ?>
  396.