More fixes for admin functions, thanks to Piter01
[mailer.git] / inc / theme-manager.php
index c188a19663ec6eddfa515822c669825101e61147..44b82d17ddaae68eac654c5f3efdb598ce073d55 100644 (file)
  ************************************************************************/
 
 // Some security stuff...
-if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
-{
+if (!defined('__SECURITY')) {
        $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
        require($INC);
 }
 
-//
+// Always make sure the session management is initialized first
+require_once(PATH."inc/session.php");
+
+// Get current theme name
 function GET_CURR_THEME() {
-       global $INC_POOL, $_CONFIG, $CSS;
+       global $INC_POOL, $_CONFIG, $CSS, $cacheArray;
 
        // The default theme is 'default'... ;-)
        $ret = "default";
@@ -48,55 +50,51 @@ function GET_CURR_THEME() {
        // Load default theme if not empty from configuration
        if (!empty($_CONFIG['default_theme'])) $ret = $_CONFIG['default_theme'];
 
-       if (empty($_COOKIE['mxchange_theme'])) {
+       if (!isSessionVariableSet('mxchange_theme')) {
                // Set default theme
-               @setcookie("mxchange_theme", $ret, (time() + 60*60*24*365), COOKIE_PATH);
-       } elseif ((!empty($_COOKIE['mxchange_theme'])) && (GET_EXT_VERSION("sql_patches") >= "0.1.4")) {
+               set_session("mxchange_theme", $ret);
+       } elseif ((isSessionVariableSet('mxchange_theme')) && (GET_EXT_VERSION("sql_patches") >= "0.1.4")) {
+               //die("<pre>".print_r($cacheArray['themes'], true)."</pre>");
                // Get theme from cookie
-               $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_themes WHERE theme_path='%s' LIMIT 1", array($_COOKIE['mxchange_theme']), __FILE__, __LINE__);
-               if (SQL_NUMROWS($result) == 1) {
-                       // Design is valid!
-                       $ret = $_COOKIE['mxchange_theme'];
-               }
+               $ret = get_session('mxchange_theme');
 
-               // Free memory
-               SQL_FREERESULT($result);
-       } elseif ((!mxchange_installed) && ((mxchange_installing) || ($CSS == true)) && ((!empty($_GET['theme'])) || (!empty($_POST['theme'])))) {
+               // Is it valid?
+               if (THEME_GET_ID($ret) == 0) {
+                       // Fix it to default
+                       $ret = "default";
+               } // END - if
+       } elseif ((!isBooleanConstantAndTrue('mxchange_installed')) && ((isBooleanConstantAndTrue('mxchange_installing')) || ($CSS == true)) && ((!empty($_GET['theme'])) || (!empty($_POST['theme'])))) {
                // Prepare FQFN for checking
-               $theme = sprintf("%stheme/%s/theme.php", PATH, $_GET['theme']);
+               $theme = sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($_GET['theme']));
 
                // Installation mode active
-               if ((!empty($_GET['theme'])) && (file_exists($theme)) && (is_readable($theme))) {
+               if ((!empty($_GET['theme'])) && (FILE_READABLE($theme))) {
                        // Set cookie from URL data
-                       @setcookie("mxchange_theme", $_GET['theme'], (time() + 60*60*24*365), COOKIE_PATH);
-                       $_COOKIE['mxchange_theme'] = $_GET['theme'];
-               } elseif (file_exists(PATH."theme/".$_POST['theme']."/theme.php")) {
+                       set_session("mxchange_theme", $_GET['theme']);
+               } elseif (FILE_READABLE(sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($_POST['theme'])))) {
                        // Set cookie from posted data
-                       @setcookie("mxchange_theme", $_POST['theme'], (time() + 60*60*24*365), COOKIE_PATH);
-                       $_COOKIE['mxchange_theme'] = $_POST['theme'];
+                       set_session("mxchange_theme", $_POST['theme']);
                }
 
                // Set return value
-               $ret = $_COOKIE['mxchange_theme'];
+               $ret = get_session('mxchange_theme');
        } else {
                // Invalid design, reset cookie
-               @setcookie("mxchange_theme", $ret, (time() + 60*60*24*365), COOKIE_PATH);
+               set_session("mxchange_theme", $ret);
        }
 
        // Add (maybe) found theme.php file to inclusion list
        $theme = sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($ret));
 
        // Try to load the requested include file
-       if ((@file_exists($theme)) && (is_readable($theme))) $INC_POOL[] = $theme;
+       if (FILE_READABLE($theme)) $INC_POOL[] = $theme;
 
        // Return theme value
        return $ret;
 }
 
-function THEME_SELECTION_BOX($mod, $act, $wht, $result)
-{
+function THEME_SELECTION_BOX($mod, $act, $wht, $result) {
        // Construction URL
-       global $CurrTheme;
        $FORM = URL."/modules.php?module=".$mod;
        if (!empty($act)) $FORM .= "&amp;action=".$act;
        if (!empty($wht))   $FORM .= "&amp;what=".$wht;
@@ -109,30 +107,27 @@ function THEME_SELECTION_BOX($mod, $act, $wht, $result)
        );
 
        // Load all themes
-       while(list($theme) = SQL_FETCHROW($result))
-       {
+       while(list($theme) = SQL_FETCHROW($result)) {
                // Load it's theme.php file
-               $INC = PATH."theme/".$theme."/theme.php";
-               if (file_exists($INC))
-               {
+               $INC = sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($theme));
+               if (FILE_READABLE($INC)) {
                        // And save all data in array
-                       include($INC);
+                       require($INC);
                        $THEMES['theme_unix'][] = $theme;
                        $THEMES['theme_name'][] = $THEME_NAME;
-               }
-       }
+               } // END - if
+       } // END - while
 
        // Sort whole array by title
        array_pk_sort($THEMES, array("theme_name"));
 
        // Construct selection form for the box template
        $OUT = "";
-       foreach ($THEMES['theme_unix'] as $key=>$theme)
-       {
+       foreach ($THEMES['theme_unix'] as $key => $theme) {
                $OUT .= "  <OPTION value=\"".$theme."\"";
-               if ($theme == $CurrTheme) $OUT .= " selected=\"selected\"";
+               if ($theme == GET_CURR_THEME()) $OUT .= " selected=\"selected\"";
                $OUT .= ">".$THEMES['theme_name'][$key]."</OPTION>\n";
-       }
+       } // END - foreach
 
        // Return generated selection
        define('__THEME_SELECTION_OPTIONS', $OUT);
@@ -140,27 +135,154 @@ function THEME_SELECTION_BOX($mod, $act, $wht, $result)
        return $OUT;
 }
 
+// Get version from name
+function THEME_GET_VERSION ($name) {
+       global $cacheArray, $_CONFIG;
+
+       // Default version "number"
+       $cver = "-.-";
+
+       // Is the cache entry there?
+       if (isset($cacheArray['themes']['theme_ver'][$name])) {
+               // Get the version from cache
+               $cver = $cacheArray['themes']['theme_ver'][$name];
+
+               // Count up
+               if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
+       } elseif (GET_EXT_VERSION("cache") != "0.1.8") {
+               // Load version from database
+               $result = SQL_QUERY_ESC("SELECT theme_ver FROM "._MYSQL_PREFIX."_themes WHERE theme_path='%s' LIMIT 1",
+                       array($name), __FILE__, __LINE__);
+
+               // Entry found?
+               if (SQL_NUMROWS($result) == 1) {
+                       // Fetch data
+                       list($cver) = SQL_FETCHROW($result);
+               } // END - if
+
+               // Free result
+               SQL_FREERESULT($result);
+       }
+
+       // Return version
+       return $cver;
+}
+
+// Get id from theme
+function THEME_GET_ID ($name) {
+       global $cacheArray, $_CONFIG;
+
+       // Default id
+       $id = 0;
+
+       // Is the cache entry there?
+       if (isset($cacheArray['themes']['id'][$name])) {
+               // Get the version from cache
+               $id = $cacheArray['themes']['id'][$name];
+
+               // Count up
+               if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
+       } elseif (GET_EXT_VERSION("cache") != "0.1.8") {
+               // Check if current theme is already imported or not
+               $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_themes WHERE theme_path='%s' LIMIT 1",
+                       array($name), __FILE__, __LINE__);
+
+               // Entry found?
+               if (SQL_NUMROWS($result) == 1) {
+                       // Fetch data
+                       list($id) = SQL_FETCHROW($result);
+               } // END - if
+
+               // Free result
+               SQL_FREERESULT($result);
+       }
+
+       // Return id
+       return $id;
+}
+
+// Checks wether a theme is found in db
+function THEME_CHECK_EXIST ($name) {
+       // Get theme and is it not nul?
+       return (THEME_GET_ID($name) > 0);
+}
+
+// Checks if a theme is active
+function THEME_IS_ACTIVE ($name) {
+       global $cacheArray, $_CONFIG;
+
+       // Default is nothing active
+       $active = false;
+
+       // Is the cache entry there?
+       if (isset($cacheArray['themes']['theme_active'][$name])) {
+               // Get the version from cache
+               $active = ($cacheArray['themes']['theme_active'][$name] == "Y");
+
+               // Count up
+               if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
+       } elseif (GET_EXT_VERSION("cache") != "0.1.8") {
+               // Check if current theme is already imported or not
+               $result = SQL_QUERY_ESC("SELECT theme_active FROM "._MYSQL_PREFIX."_themes WHERE theme_path='%s' AND theme_active='Y' LIMIT 1",
+                       array($name), __FILE__, __LINE__);
+
+               // Is the theme active and installed?
+               $active = (SQL_NUMROWS($result) == 1);
+
+               // Free result
+               SQL_FREERESULT($result);
+       }
+
+       // Return result
+       return $active;
+}
+
+// Gets current human-readable theme name
+function GET_CURR_THEME_NAME () {
+       global $cacheArray, $_CONFIG;
+
+       // Get the Uni* name
+       $name = GET_CURR_THEME();
+
+       // Is the cache entry there?
+       if (isset($cacheArray['themes']['theme_name'][$name])) {
+               // Get the version from cache
+               $name = $cacheArray['themes']['theme_name'][$name];
+
+               // Count up
+               if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
+       } elseif (GET_EXT_VERSION("cache") != "0.1.8") {
+               // Check if current theme is already imported or not
+               $result = SQL_QUERY_ESC("SELECT theme_name FROM "._MYSQL_PREFIX."_themes WHERE theme_path='%s' AND theme_active='Y' LIMIT 1",
+                       array($name), __FILE__, __LINE__);
+
+               // Is the theme active and installed?
+               $name = (SQL_NUMROWS($result) == 1);
+
+               // Free result
+               SQL_FREERESULT($result);
+       }
+
+       // Return name
+       return $name;
+}
+
 // Initialize variables
-$CurrTheme = GET_CURR_THEME();
-if (empty($_POST['new_theme'])) $_POST['new_theme'] = "";
+$currTheme = GET_CURR_THEME();
 
 // Check if new theme is selcted
-if ((!empty($_POST['new_theme'])) && ($_POST['new_theme'] != $CurrTheme))
-{
+if ((!empty($_POST['new_theme'])) && ($_POST['new_theme'] != $currTheme)) {
        // Set new theme for guests
-       $NewTheme = $_POST['new_theme'];
+       $newTheme = $_POST['new_theme'];
 
        // Change to new theme
-       @setcookie("mxchange_theme", $NewTheme, (time() + 60*60*24*365), COOKIE_PATH);
+       set_session("mxchange_theme", $newTheme);
 
        // Remove current from array and set new
-       $theme = PATH."theme/".$CurrTheme."/theme.php";
+       $theme = sprintf("%stheme/%s/theme.php", PATH, $currTheme);
        unset($INC_POOL[array_search($theme, $INC_POOL)]);
-       $INC_POOL[] = PATH."theme/".$NewTheme."/theme.php";
-}
-
-// Remove variable again
-unset($_POST['new_theme']);
+       $INC_POOL[] = sprintf("%stheme/%s/theme.php", PATH, $newTheme);
+} // END - if
 
 //
 ?>