Index: wp-includes/theme.php
===================================================================
--- wp-includes/theme.php	(revision 19969)
+++ wp-includes/theme.php	(working copy)
@@ -578,8 +578,7 @@
 	if ( empty( $wp_theme_directories ) )
 		return false;
 
-	$theme_files = array();
-	$wp_broken_themes = array();
+	$found_themes = $wp_broken_themes = array();
 
 	/* Loop the registered theme directories and extract all themes */
 	foreach ( (array) $wp_theme_directories as $theme_root ) {
@@ -587,65 +586,53 @@
 
 		/* We don't want to replace all forward slashes, see Trac #4541 */
 		if ( '/' != WP_CONTENT_DIR )
-			$theme_loc = str_replace(WP_CONTENT_DIR, '', $theme_root);
+			$theme_loc = str_replace( WP_CONTENT_DIR, '', $theme_root );
 
 		/* Files in the root of the current theme directory and one subdir down */
-		$themes_dir = @ opendir($theme_root);
-
-		if ( !$themes_dir )
+		$dirs = @ scandir( $theme_root );
+		if ( ! $dirs )
 			return false;
-
-		while ( ($theme_dir = readdir($themes_dir)) !== false ) {
-			if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
-				if ( $theme_dir[0] == '.' || $theme_dir == 'CVS' )
-					continue;
-
-				$stylish_dir = @opendir($theme_root . '/' . $theme_dir);
-				$found_stylesheet = false;
-
-				while ( ($theme_file = readdir($stylish_dir)) !== false ) {
-					if ( $theme_file == 'style.css' ) {
-						$theme_files[$theme_dir] = array( 'theme_file' => $theme_dir . '/' . $theme_file, 'theme_root' => $theme_root );
-						$found_stylesheet = true;
-						break;
-					}
+		foreach ( $dirs as $dir ) {
+			if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' )
+				continue;
+			if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) {
+				// wp-content/themes/a-single-theme
+				// wp-content/themes is $theme_root, a-single-theme is $dir
+				$found_themes[ $dir ] = array(
+					'theme_file' => $dir . '/style.css',
+					'theme_root' => $theme_root,
+				);
+			} else {
+				$found_theme = false;
+				// wp-content/themes/a-folder-of-themes/*
+				// wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs
+				$sub_dirs = @ scandir( $theme_root . '/' . $dir );
+				if ( ! $sub_dirs )
+					return false;
+				foreach ( $sub_dirs as $sub_dir ) {
+					if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' )
+						continue;
+					if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) )
+						continue;
+					$found_themes[ $dir . '/' . $sub_dir ] = array(
+						'theme_file' => $dir . '/' . $sub_dir . '/style.css',
+						'theme_root' => $theme_root,
+					);
+					$found_theme = true;
 				}
-				@closedir($stylish_dir);
-
-				if ( !$found_stylesheet ) { // look for themes in that dir
-					$subdir = "$theme_root/$theme_dir";
-					$subdir_name = $theme_dir;
-					$theme_subdirs = @opendir( $subdir );
-
-					$found_subdir_themes = false;
-					while ( ($theme_subdir = readdir($theme_subdirs)) !== false ) {
-						if ( is_dir( $subdir . '/' . $theme_subdir) && is_readable($subdir . '/' . $theme_subdir) ) {
-							if ( $theme_subdir[0] == '.' || $theme_subdir == 'CVS' )
-								continue;
-
-							$stylish_dir = @opendir($subdir . '/' . $theme_subdir);
-							$found_stylesheet = false;
-
-							while ( ($theme_file = readdir($stylish_dir)) !== false ) {
-								if ( $theme_file == 'style.css' ) {
-									$theme_files["$theme_dir/$theme_subdir"] = array( 'theme_file' => $subdir_name . '/' . $theme_subdir . '/' . $theme_file, 'theme_root' => $theme_root );
-									$found_stylesheet = true;
-									$found_subdir_themes = true;
-									break;
-								}
-							}
-							@closedir($stylish_dir);
-						}
-					}
-					@closedir($theme_subdirs);
-					if ( !$found_subdir_themes )
-						$wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
+				// Never mind the above, it's just a theme missing a style.css.
+				if ( ! $found_theme ) {
+					$wp_broken_themes[ $dir ] = array(
+						'Name' => $dir,
+						'Title' => $dir,
+						'Description' => __( 'Stylesheet is missing.' ),
+					);
 				}
 			}
 		}
-		@closedir( $themes_dir );
 	}
-	return $theme_files;
+
+	return $found_themes;
 }
 
 /**
