+ // Is it valid?
+ if ((!isExtensionActive('theme')) || (getThemeId($ret) == '0')) {
+ // Fix it to default
+ $ret = 'default';
+ } // END - if
+ } elseif ((!isInstalled()) && ((isInstalling()) || (isHtmlOutputMode())) && ((isGetRequestParameterSet('theme')) || (isPostRequestParameterSet('theme')))) {
+ // Prepare filename for checking
+ $themeFile = sprintf("theme/%s/theme.php", getRequestParameter('theme'));
+
+ // Installation mode active
+ if ((isGetRequestParameterSet('theme')) && (isIncludeReadable($theme))) {
+ // Set cookie from URL data
+ setTheme(getRequestParameter('theme'));
+ } elseif (isIncludeReadable(sprintf("theme/%s/theme.php", secureString(postRequestParameter('theme'))))) {
+ // Set cookie from posted data
+ setTheme(secureString(postRequestParameter('theme')));
+ }
+
+ // Set return value
+ $ret = getSession('mxchange_theme');
+ } else {
+ // Invalid design, reset cookie
+ setTheme($ret);
+ }
+
+ // Return theme value
+ return $ret;
+}
+
+/**
+ * Setter for theme in session (This setter does return the success of
+ * setSession() which is required e.g. for destroySponsorSession().
+ */
+function setTheme ($newTheme) {
+ return setSession('mxchange_theme', $newTheme);
+}
+
+// Get id from theme
+function getThemeId ($name) {
+ // Default id
+ $id = '0';
+
+ // Is the cache entry there?
+ if (isset($GLOBALS['cache_array']['themes']['id'][$name])) {
+ // Get the version from cache
+ $id = $GLOBALS['cache_array']['themes']['id'][$name];
+
+ // Count up
+ incrementStatsEntry('cache_hits');
+ } elseif (isExtensionInstalledAndNewer('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), __FUNCTION__, __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;
+}
+
+//-----------------------------------------------------------------------------
+// Only filter functions
+//-----------------------------------------------------------------------------
+
+// Filter for generic handling of theme change
+function FILTER_HANDLE_THEME_CHANGE () {
+ // Check if new theme is selcted
+ if ((isPostRequestParameterSet('new_theme')) && (postRequestParameter('new_theme') != getCurrentTheme())) {
+ // Set new theme for guests
+ $newTheme = postRequestParameter('new_theme');
+
+ // Change to new theme
+ setTheme($newTheme);
+
+ // Remove current from array
+ removeIncludeFromPool('theme', sprintf("theme/%s/theme.php", getCurrentTheme()));
+
+ // Add new theme
+ addIncludeToPool('theme', sprintf("theme/%s/theme.php", $newTheme));
+
+ // Redirect to same URL
+ redirectToRequestUri();
+ } // END - if
+}
+
+// Filter for settings theme from user profile, must be executed only if FILTER_FETCH_USER_DATA() ran before
+function FILTER_SET_USERS_THEME () {
+ // Is the user data valid?
+ if (!isMember()) {
+ // Do only run for logged in members
+ debug_report_bug(__FUNCTION__, __LINE__, 'Please only run this filter for logged in users.');
+ } // END - if
+
+ // Change to new theme
+ setTheme(getUserData('curr_theme'));
+}