Index: switchtheme.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/switchtheme/switchtheme.module,v
retrieving revision 1.10
diff -u -p -r1.10 switchtheme.module
--- switchtheme.module	31 Aug 2007 14:56:14 -0000	1.10
+++ switchtheme.module	16 Apr 2008 00:39:35 -0000
@@ -29,16 +29,17 @@ function switchtheme_perm() {
 function switchtheme_init() {
   global $custom_theme, $user;
   drupal_add_css(drupal_get_path('module', 'switchtheme') .'/switchtheme.css');
-  // plays nice with others, if $custom_theme has already been defined, use it
-  // otherwise if session info is set use that
-  if (isset($_GET['theme'])) {
-    $form['custom_theme'] = array('#value' => $_GET['theme']);
-    drupal_execute('switchtheme_switch_form', $form);
+
+  // If query parameter 'theme' is set, we always assign a new theme.
+  if ($_REQUEST['theme']) {
+    $form = array('values' => array('theme' => $_REQUEST['theme']));
+    switchtheme_switch_form_submit('', $form);
   }
+  // 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'];
   }
-
 }
 
 /**
@@ -113,7 +114,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'], array('query' => 'theme='. $theme->name, 'html' => TRUE));
+      $output = l("<img src=\"". base_path() ."$theme->screenshot\" alt=\"preview of $theme->name\"/>", $_GET['q'], array('query' => 'theme='. $theme->name, 'html' => TRUE));
       return $output;
     }
   }
@@ -122,7 +123,8 @@ function switchtheme_display_random_bloc
 function switchtheme_switch_form() {
   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.')),
@@ -145,13 +147,18 @@ function theme_switchtheme_block_form($f
 function switchtheme_switch_form_submit($form, &$form_state) {
   global $user;
 
-  // save the setting in the db for logged in users
-  // save the setting in the session for all others
   if ($user->uid > 0) {
-    variable_set('theme_default', $form_values['custom_theme']);
-    if (user_save($user, array('theme' => $form_state['values']['custom_theme']))) {
-      $user->theme = $form_state['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_state['values']['theme']))) {
+      $user->theme = $form_state['values']['theme'];
     }
+    $_SESSION['custom_theme'] = $form_state['values']['theme'];
+  }
+  elseif (user_access('switch theme')) {
+    // Save the setting in the session for anonymous users.
+    $_SESSION['custom_theme'] = $form_state['values']['theme'];
   }
 }
 
