Index: switchtheme.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/switchtheme/switchtheme.module,v
retrieving revision 1.2.2.4
diff -u -p -r1.2.2.4 switchtheme.module
--- switchtheme.module	31 Aug 2007 14:57:42 -0000	1.2.2.4
+++ switchtheme.module	13 Apr 2008 19:42:39 -0000
@@ -29,15 +29,6 @@ function switchtheme_perm() {
 function switchtheme_menu($may_cache) {
   global $custom_theme, $user;
   $items = array();
-  // plays nice with others, if $custom_theme has already been defined, use it
-  // otherwise if session info is set use that
-  if ($_GET['theme']) {
-    $form_values = array('custom_theme' => $_GET['theme']);
-    switchtheme_switch_form_submit('', $form_values);
-  }
-  if (isset($_SESSION['custom_theme']) && !isset($custom_theme)) {
-    $custom_theme = $_SESSION['custom_theme'];
-  }
 
   if ($may_cache) {
     $items[] = array(
@@ -51,6 +42,17 @@ function switchtheme_menu($may_cache) {
   }
   else {
     drupal_add_css(drupal_get_path('module', 'switchtheme') .'/switchtheme.css');
+
+    // If query parameter 'theme' is set, we always assign a the new theme.
+    if ($_REQUEST['theme']) {
+      $form_values = array('theme' => $_REQUEST['theme']);
+      switchtheme_switch_form_submit('', $form_values);
+    }
+    // Other modules may already have set $custom_theme, so we actually switch
+    // the theme only if $custom_theme has not been set yet.
+    if (isset($_SESSION['custom_theme']) && !isset($custom_theme)) {
+      $custom_theme = $_SESSION['custom_theme'];
+    }
   }
   return $items;
 }
@@ -103,7 +105,7 @@ function switchtheme_display_random_bloc
   foreach ($themes as $key => $theme) {
     $theme->screenshot = dirname($theme->filename) . '/screenshot.png';
     if (file_exists($theme->screenshot)) { //return the first one with a screenie
-      $output = l("<img src=\"/$theme->screenshot\" alt=\"preview of $theme->name\"/>", $_GET['q'], NULL, 'theme='.$theme->name, NULL, FALSE, TRUE);
+      $output = l("<img src=\"". base_path() ."$theme->screenshot\" alt=\"preview of $theme->name\" />", $_GET['q'], NULL, 'theme='.$theme->name, NULL, FALSE, TRUE);
       return $output;
     }
   }
@@ -114,10 +116,10 @@ function switchtheme_display_random_bloc
 
 
 function switchtheme_switch_form() {
+  global $user, $custom_theme;
 
-global $user, $custom_theme;
-
-  $form['custom_theme'] = array(
+  $form = array();
+  $form['theme'] = array(
     '#type' => 'select',
     '#default_value' => !empty($custom_theme) ? $custom_theme : $user->theme,
     '#attributes' => array('title' => t('Change the way this site looks.')),
@@ -140,13 +142,18 @@ function theme_switchtheme_block_form($f
 function switchtheme_switch_form_submit($form_id, $form_values) {
   global $user;
 
-  // save the setting in the db for logged in users
-  // save the setting in the variable for all others
   if ($user->uid > 0) {
-    variable_set('theme_default', $form_values['custom_theme']);
-    if (user_save($user, array('theme' => $form_values['custom_theme']))) {
-      $user->theme = $form_values['custom_theme'];
+    // Save the setting in the db for logged in users.
+    // We do not validate the input here, because that is done in init_theme()
+    // already.
+    if (user_save($user, array('theme' => $form_values['theme']))) {
+      $user->theme = $form_values['theme'];
     }
+    $_SESSION['custom_theme'] = $form_values['theme'];
+  }
+  elseif (user_access('switch theme')) {
+    // save the setting in the variable for all others
+    $_SESSION['custom_theme'] = $form_values['theme'];
   }
 }
 
