From dbd245932d8df7e6ca90b3d06c56e85941de7e86 Mon Sep 17 00:00:00 2001
From: wpmuguru <wpmuguru@1a063a9b-81f0-0310-95a4-ce76da25c4cd>
Date: Sun, 10 Jan 2010 18:10:51 +0000
Subject: [PATCH] multi-site startup, See #11644

git-svn-id: http://svn.automattic.com/wordpress/trunk@12688 1a063a9b-81f0-0310-95a4-ce76da25c4cd
---
 wp-includes/ms-load.php | 164 ++++++++++++++++++++++++++++++++++++++++
 wp-settings.php         |  92 ++++++++++++++++------
 2 files changed, 232 insertions(+), 24 deletions(-)
 create mode 100644 wp-includes/ms-load.php

diff --git a/wp-includes/ms-load.php b/wp-includes/ms-load.php
new file mode 100644
index 0000000000..efdcb96b53
--- /dev/null
+++ b/wp-includes/ms-load.php
@@ -0,0 +1,164 @@
+<?php
+/**
+ * Used to setup and fix common variables and include
+ * the WordPress procedural and class library.
+ *
+ * You should not have to change this file and allows
+ * for some configuration in wp-config.php.
+ *
+ * @since 3.0
+ *
+ * @package WordPress
+ */
+if( defined( 'SUNRISE' ) )
+	include_once( WP_CONTENT_DIR . '/sunrise.php' );
+
+    require (ABSPATH . WPINC . '/ms-settings.php');
+$wpdb->blogid           = $current_blog->blog_id;
+$wpdb->siteid           = $current_blog->site_id;
+$wpdb->set_prefix($table_prefix); // set up blog tables
+$table_prefix = $table_prefix . $blog_id . '_';
+
+// Fix empty PHP_SELF
+$PHP_SELF = $_SERVER['PHP_SELF'];
+if ( empty($PHP_SELF) || ( empty($PHP_SELF) && constant( 'VHOST' ) == 'no' && $current_blog->path != '/' ) )
+	$_SERVER['PHP_SELF'] = $PHP_SELF = preg_replace("/(\?.*)?$/",'',$_SERVER["REQUEST_URI"]);
+
+wp_cache_init(); // need to init cache again after blog_id is set
+if ( function_exists('wp_cache_add_global_groups') ) { // need to add these again. Yes, it's an ugly hack
+	wp_cache_add_global_groups(array ('users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss'));
+	wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
+}
+
+if( !defined( "UPLOADBLOGSDIR" ) )
+	define( "UPLOADBLOGSDIR", 'wp-content/blogs.dir' );
+
+if( !defined( "UPLOADS" ) )
+	define( "UPLOADS", UPLOADBLOGSDIR . "/{$wpdb->blogid}/files/" );
+
+if( !defined( "BLOGUPLOADDIR" ) )
+	define( "BLOGUPLOADDIR", WP_CONTENT_DIR . "/blogs.dir/{$wpdb->blogid}/files/" );
+
+function ms_network_settings() {
+        global $wpdb, $current_site, $cookiehash;
+
+        if( !isset($current_site->site_name) )
+                $current_site->site_name = get_site_option('site_name');
+
+        if( $current_site->site_name == false )
+                $current_site->site_name = ucfirst( $current_site->domain );
+
+        if ( ! defined('WP_INSTALLING') ) {
+                // Used to guarantee unique hash cookies
+                if ( !isset($cookiehash) )
+                        $cookiehash = '';
+
+                /**
+                 * Used to guarantee unique hash cookies
+                 * @since 1.5
+                 */
+                if ( !defined('COOKIEHASH') )
+                        define( 'COOKIEHASH', $cookiehash );
+        }
+
+        $wpdb->hide_errors();
+}
+
+function ms_network_plugins() {
+        $wpmu_sitewide_plugins = (array) maybe_unserialize( get_site_option( 'wpmu_sitewide_plugins' ) );
+        foreach( $wpmu_sitewide_plugins as $plugin_file => $activation_time ) {
+                if ( !$plugin_file )
+                        continue;
+
+                if ( !file_exists( WP_PLUGIN_DIR . '/' . $plugin_file ) ) {
+                        $deleted_sitewide_plugins[] = $plugin_file;
+                } else {
+                        include_once( WP_PLUGIN_DIR . '/' . $plugin_file );
+                }
+        }
+
+        if ( isset( $deleted_sitewide_plugins ) ) {
+                $active_sitewide_plugins = maybe_unserialize( get_site_option( 'active_sitewide_plugins' ) );
+
+                /* Remove any deleted plugins from the wpmu_sitewide_plugins array */
+                foreach( $deleted_sitewide_plugins as $plugin_file ) {
+                        unset( $wpmu_sitewide_plugins[$plugin_file] );
+                        unset( $active_sitewide_plugins[$plugin_file] );
+                }
+
+                update_site_option( 'wpmu_sitewide_plugins', $wpmu_sitewide_plugins );
+                update_site_option( 'active_sitewide_plugins', $wpmu_sitewide_plugins );
+        }
+
+}
+
+function ms_site_check() {
+        global $wpdb, $current_blog;
+
+        $wpdb->show_errors();
+
+        if ( '1' == $current_blog->deleted ) {
+                if ( file_exists( WP_CONTENT_DIR . '/blog-deleted.php' ) ) {
+                        require_once( WP_CONTENT_DIR . '/blog-deleted.php' );
+                        die();
+                } else {
+                        header('HTTP/1.1 410 Gone');
+                        graceful_fail(__('This user has elected to delete their account and the content is no longer available.'));
+                }
+        }
+
+        if ( '2' == $current_blog->deleted ) {
+                if ( file_exists( WP_CONTENT_DIR . '/blog-inactive.php' ) ) {
+                        require_once( WP_CONTENT_DIR . '/blog-inactive.php' );
+                        die();
+                } else {
+                        graceful_fail( sprintf( __( 'This blog has not been activated yet. If you are having problems activating your blog, please contact <a href="mailto:%1$s">%1$s</a>.' ), str_replace( '@', ' AT ', get_site_option( 'admin_email', "support@{$current_site->domain}" ) ) ) );
+                }
+        }
+
+        if( $current_blog->archived == '1' || $current_blog->spam == '1' ) {
+                if ( file_exists( WP_CONTENT_DIR . '/blog-suspended.php' ) ) {
+                        require_once( WP_CONTENT_DIR . '/blog-suspended.php' );
+                        die();
+                } else {
+                        header('HTTP/1.1 410 Gone');
+                        graceful_fail(__('This blog has been archived or suspended.'));
+                }
+        }
+}
+
+function ms_network_cookies() {
+        global $current_site;
+        /**
+         * It is possible to define this in wp-config.php
+         * @since 1.2.0
+         */
+        if ( !defined('COOKIEPATH') )
+                define('COOKIEPATH', $current_site->path );
+
+        /**
+         * It is possible to define this in wp-config.php
+         * @since 1.5.0
+         */
+        if ( !defined('SITECOOKIEPATH') )
+                define('SITECOOKIEPATH', $current_site->path );
+
+        /**
+         * It is possible to define this in wp-config.php
+         * @since 2.6.0
+         */
+        if ( !defined('ADMIN_COOKIE_PATH') ) {
+                if( !is_subdomain_install() ) {
+                        define( 'ADMIN_COOKIE_PATH', SITECOOKIEPATH );
+                } else {
+                        define( 'ADMIN_COOKIE_PATH', SITECOOKIEPATH . 'wp-admin' );
+                }
+        }
+        /**
+         * It is possible to define this in wp-config.php
+         * @since 2.0.0
+         */
+        if ( !defined('COOKIE_DOMAIN') )
+                define('COOKIE_DOMAIN', '.' . $current_site->domain);
+}
+?>
diff --git a/wp-settings.php b/wp-settings.php
index 9c3cd1e0b6..900e49378d 100644
--- a/wp-settings.php
+++ b/wp-settings.php
@@ -9,8 +9,28 @@
  * @package WordPress
  */
 
-if ( !defined('WP_MEMORY_LIMIT') )
-	define('WP_MEMORY_LIMIT', '32M');
+
+/**
+ * Whether Multisite support is enabled
+ *
+ * @since 3.0
+ *
+ * @return bool True if multisite is enabled, false otherwise.
+ */
+function is_multisite() {
+	if ( ( defined('MULTISITE') && MULTISITE ) || defined('VHOST') || defined('SUNRISE') )
+		return true;
+
+	return false;
+}
+
+if ( !defined('WP_MEMORY_LIMIT') ) {
+        if( is_multisite() ) {
+        	define('WP_MEMORY_LIMIT', '64M');
+        } else {
+        	define('WP_MEMORY_LIMIT', '32M');
+        }
+}
 
 if ( function_exists('memory_get_usage') && ( (int) @ini_get('memory_limit') < abs(intval(WP_MEMORY_LIMIT)) ) )
 	@ini_set('memory_limit', WP_MEMORY_LIMIT);
@@ -325,20 +345,6 @@ function is_admin() {
 	return false;
 }
 
-/**
- * Whether Multisite support is enabled
- * 
- * @since 3.0
- *
- * @return bool True if multisite is enabled, false otherwise.
- */
-function is_multisite() {
-	if ( ( defined('MULTISITE') && MULTISITE ) || defined('VHOST') )
-		return true;
-
-	return false;
-}
-
 if ( file_exists(WP_CONTENT_DIR . '/object-cache.php') ) {
 	require_once (WP_CONTENT_DIR . '/object-cache.php');
 	$_wp_using_ext_object_cache = true;
@@ -349,16 +355,32 @@ if ( file_exists(WP_CONTENT_DIR . '/object-cache.php') ) {
 
 wp_cache_init();
 if ( function_exists('wp_cache_add_global_groups') ) {
-	wp_cache_add_global_groups(array ('users', 'userlogins', 'usermeta', 'site-transient'));
+        if( is_multisite() ) {
+                wp_cache_add_global_groups(array ('users', 'userlogins', 'usermeta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss'));
+        } else {
+        	wp_cache_add_global_groups(array ('users', 'userlogins', 'usermeta', 'site-transient'));
+        }
 	wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
 }
 
+if( is_multisite() ) {
+    require (ABSPATH . WPINC . '/ms-load.php');
+}
+
 require (ABSPATH . WPINC . '/plugin.php');
 require (ABSPATH . WPINC . '/default-filters.php');
 include_once(ABSPATH . WPINC . '/pomo/mo.php');
+
+if( is_multisite() && defined( "SHORTINIT" ) && SHORTINIT ) // stop most of WP being loaded, we just want the basics
+	return false;
+
 require_once (ABSPATH . WPINC . '/l10n.php');
 
-if ( !is_blog_installed() && (strpos($_SERVER['PHP_SELF'], 'install.php') === false && !defined('WP_INSTALLING')) ) {
+if( is_multisite() ) {
+        if ( !is_blog_installed() && !defined('WP_INSTALLING') ) {
+                die( __( 'The blog you have requested is not installed properly. Please contact the system administrator.' ) ); // have to die here ~ Mark
+        }
+} elseif ( !is_blog_installed() && (strpos($_SERVER['PHP_SELF'], 'install.php') === false && !defined('WP_INSTALLING')) ) {
 	if ( defined('WP_SITEURL') )
 		$link = WP_SITEURL . '/wp-admin/install.php';
 	elseif (strpos($_SERVER['PHP_SELF'], 'wp-admin') !== false)
@@ -437,6 +459,8 @@ if ( !defined('WP_PLUGIN_URL') )
 if ( !defined('PLUGINDIR') )
 	define( 'PLUGINDIR', 'wp-content/plugins' ); // Relative to ABSPATH.  For back compat.
 
+if( is_multisite() )
+        ms_network_settings();
 /**
  * Allows for the mu-plugins directory to be moved from the default location.
  *
@@ -463,20 +487,40 @@ if ( !defined( 'MUPLUGINDIR' ) )
 
 if ( is_dir( WPMU_PLUGIN_DIR ) ) {
 	if ( $dh = opendir( WPMU_PLUGIN_DIR ) ) {
-		while ( ( $plugin = readdir( $dh ) ) !== false ) {
-			if ( substr( $plugin, -4 ) == '.php' ) {
-				include_once( WPMU_PLUGIN_DIR . '/' . $plugin );
-			}
-		}
+		$mu_plugins = array ();
+		while ( ( $plugin = readdir( $dh ) ) !== false )
+			if ( substr( $plugin, -4 ) == '.php' )
+				$mu_plugins[] = $plugin;
+		closedir( $dh );
+                if( is_multisite() )
+        		sort( $mu_plugins );
+		foreach( $mu_plugins as $mu_plugin )
+			include_once( WPMU_PLUGIN_DIR . '/' . $mu_plugin );
 	}
 }
+/**
+ * Used to load network wide plugins
+ * @since 3.0
+ */
+if( is_multisite() )
+        ms_network_plugins();
+
 do_action('muplugins_loaded');
 
+/**
+ * Used to check site status
+ * @since 3.0
+ */
+if( is_multisite() ) {
+    ms_site_check();
+    ms_network_cookies();
+}
 /**
  * Used to guarantee unique hash cookies
  * @since 1.5
  */
-define('COOKIEHASH', md5(get_option('siteurl')));
+if( !defined('COOKIEHASH') )
+        define('COOKIEHASH', md5(get_option('siteurl')));
 
 /**
  * Should be exactly the same as the default value of SECRET_KEY in wp-config-sample.php