wp/wp-includes/ms-settings.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     9  * @subpackage Multisite
     9  * @subpackage Multisite
    10  * @since 3.0.0
    10  * @since 3.0.0
    11  */
    11  */
    12 
    12 
    13 /** Include Multisite initialization functions */
    13 /** Include Multisite initialization functions */
    14 require( ABSPATH . WPINC . '/ms-load.php' );
    14 require_once( ABSPATH . WPINC . '/ms-load.php' );
    15 require( ABSPATH . WPINC . '/ms-default-constants.php' );
    15 require_once( ABSPATH . WPINC . '/ms-default-constants.php' );
    16 
    16 
    17 if ( defined( 'SUNRISE' ) )
    17 if ( defined( 'SUNRISE' ) ) {
    18 	include_once( WP_CONTENT_DIR . '/sunrise.php' );
    18 	include_once( WP_CONTENT_DIR . '/sunrise.php' );
       
    19 }
    19 
    20 
    20 /** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */
    21 /** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */
    21 ms_subdomain_constants();
    22 ms_subdomain_constants();
    22 
    23 
    23 if ( !isset( $current_site ) || !isset( $current_blog ) ) {
    24 if ( !isset( $current_site ) || !isset( $current_blog ) ) {
    24 
    25 
    25 	$domain = addslashes( $_SERVER['HTTP_HOST'] );
    26 	// Given the domain and path, let's try to identify the network and site.
    26 	if ( false !== strpos( $domain, ':' ) ) {
    27 	// Usually, it's easier to query the site first, which declares its network.
    27 		if ( substr( $domain, -3 ) == ':80' ) {
    28 	// In limited situations, though, we either can or must find the network first.
    28 			$domain = substr( $domain, 0, -3 );
    29 
    29 			$_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
    30 	$domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
    30 		} elseif ( substr( $domain, -4 ) == ':443' ) {
    31 	if ( substr( $domain, -3 ) == ':80' ) {
    31 			$domain = substr( $domain, 0, -4 );
    32 		$domain = substr( $domain, 0, -3 );
    32 			$_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
    33 		$_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
       
    34 	} elseif ( substr( $domain, -4 ) == ':443' ) {
       
    35 		$domain = substr( $domain, 0, -4 );
       
    36 		$_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
       
    37 	}
       
    38 
       
    39 	$path = stripslashes( $_SERVER['REQUEST_URI'] );
       
    40 	if ( is_admin() ) {
       
    41 		$path = preg_replace( '#(.*)/wp-admin/.*#', '$1/', $path );
       
    42 	}
       
    43 	list( $path ) = explode( '?', $path );
       
    44 
       
    45 	// If the network is defined in wp-config.php, we can simply use that.
       
    46 	if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
       
    47 		$current_site = new stdClass;
       
    48 		$current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
       
    49 		$current_site->domain = DOMAIN_CURRENT_SITE;
       
    50 		$current_site->path = PATH_CURRENT_SITE;
       
    51 		if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
       
    52 			$current_site->blog_id = BLOG_ID_CURRENT_SITE;
       
    53 		} elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
       
    54 			$current_site->blog_id = BLOGID_CURRENT_SITE;
       
    55 		}
       
    56 
       
    57 		if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
       
    58 			$current_blog = get_site_by_path( $domain, $path );
       
    59 		} elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
       
    60 			// If the current network has a path and also matches the domain and path of the request,
       
    61 			// we need to look for a site using the first path segment following the network's path.
       
    62 			$current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
    33 		} else {
    63 		} else {
    34 			wp_load_translations_early();
    64 			// Otherwise, use the first path segment (as usual).
    35 			wp_die( __( 'Multisite only works without the port number in the URL.' ) );
    65 			$current_blog = get_site_by_path( $domain, $path, 1 );
    36 		}
    66 		}
    37 	}
    67 
    38 
    68 	} elseif ( ! is_subdomain_install() ) {
    39 	$domain = rtrim( $domain, '.' );
    69 		/*
    40 	$cookie_domain = $domain;
    70 		 * A "subdomain" install can be re-interpreted to mean "can support any domain".
    41 	if ( substr( $cookie_domain, 0, 4 ) == 'www.' )
    71 		 * If we're not dealing with one of these installs, then the important part is determining
    42 		$cookie_domain = substr( $cookie_domain, 4 );
    72 		 * the network first, because we need the network's path to identify any sites.
    43 
    73 		 */
    44 	$path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $_SERVER['REQUEST_URI'] );
    74 		if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
    45 	$path = str_replace ( '/wp-admin/', '/', $path );
    75 			// Are there even two networks installed?
    46 	$path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
    76 			$one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
    47 
    77 			if ( 1 === $wpdb->num_rows ) {
    48 	$current_site = wpmu_current_site();
    78 				$current_site = wp_get_network( $one_network );
    49 	if ( ! isset( $current_site->blog_id ) )
    79 				wp_cache_add( 'current_network', $current_site, 'site-options' );
    50 		$current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) );
    80 			} elseif ( 0 === $wpdb->num_rows ) {
    51 
    81 				ms_not_installed();
    52 	if ( is_subdomain_install() ) {
    82 			}
    53 		$current_blog = wp_cache_get( 'current_blog_' . $domain, 'site-options' );
    83 		}
    54 		if ( !$current_blog ) {
    84 		if ( empty( $current_site ) ) {
    55 			$current_blog = get_blog_details( array( 'domain' => $domain ), false );
    85 			$current_site = get_network_by_path( $domain, $path, 1 );
    56 			if ( $current_blog )
    86 		}
    57 				wp_cache_set( 'current_blog_' . $domain, $current_blog, 'site-options' );
    87 
    58 		}
    88 		if ( empty( $current_site ) ) {
    59 		if ( $current_blog && $current_blog->site_id != $current_site->id ) {
    89 			ms_not_installed();
    60 			$current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $current_blog->site_id ) );
    90 		} elseif ( $path === $current_site->path ) {
    61 			if ( ! isset( $current_site->blog_id ) )
    91 			$current_blog = get_site_by_path( $domain, $path );
    62 				$current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) );
    92 		} else {
    63 		} else
    93 			// Search the network path + one more path segment (on top of the network path).
    64 			$blogname = substr( $domain, 0, strpos( $domain, '.' ) );
    94 			$current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
       
    95 		}
    65 	} else {
    96 	} else {
    66 		$blogname = htmlspecialchars( substr( $_SERVER[ 'REQUEST_URI' ], strlen( $path ) ) );
    97 		// Find the site by the domain and at most the first path segment.
    67 		if ( false !== strpos( $blogname, '/' ) )
    98 		$current_blog = get_site_by_path( $domain, $path, 1 );
    68 			$blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
    99 		if ( $current_blog ) {
    69 		if ( false !== strpos( $blogname, '?' ) )
   100 			$current_site = wp_get_network( $current_blog->site_id ? $current_blog->site_id : 1 );
    70 			$blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
       
    71 		$reserved_blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
       
    72 		if ( $blogname != '' && ! in_array( $blogname, $reserved_blognames ) && ! is_file( $blogname ) )
       
    73 			$path .= $blogname . '/';
       
    74 		$current_blog = wp_cache_get( 'current_blog_' . $domain . $path, 'site-options' );
       
    75 		if ( ! $current_blog ) {
       
    76 			$current_blog = get_blog_details( array( 'domain' => $domain, 'path' => $path ), false );
       
    77 			if ( $current_blog )
       
    78 				wp_cache_set( 'current_blog_' . $domain . $path, $current_blog, 'site-options' );
       
    79 		}
       
    80 		unset($reserved_blognames);
       
    81 	}
       
    82 
       
    83 	if ( ! defined( 'WP_INSTALLING' ) && is_subdomain_install() && ! is_object( $current_blog ) ) {
       
    84 		if ( defined( 'NOBLOGREDIRECT' ) ) {
       
    85 			$destination = NOBLOGREDIRECT;
       
    86 			if ( '%siteurl%' == $destination )
       
    87 				$destination = "http://" . $current_site->domain . $current_site->path;
       
    88 		} else {
   101 		} else {
    89 			$destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
   102 			// If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
    90 		}
   103 			$current_site = get_network_by_path( $domain, $path, 1 );
       
   104 		}
       
   105 	}
       
   106 
       
   107 	// The network declared by the site trumps any constants.
       
   108 	if ( $current_blog && $current_blog->site_id != $current_site->id ) {
       
   109 		$current_site = wp_get_network( $current_blog->site_id );
       
   110 	}
       
   111 
       
   112 	// No network has been found, bail.
       
   113 	if ( empty( $current_site ) ) {
       
   114 		ms_not_installed();
       
   115 	}
       
   116 
       
   117 	// @todo Investigate when exactly this can occur.
       
   118 	if ( empty( $current_blog ) && defined( 'WP_INSTALLING' ) ) {
       
   119 		$current_blog = new stdClass;
       
   120 		$current_blog->blog_id = $blog_id = 1;
       
   121 	}
       
   122 
       
   123 	// No site has been found, bail.
       
   124 	if ( empty( $current_blog ) ) {
       
   125 		// We're going to redirect to the network URL, with some possible modifications.
       
   126 		$scheme = is_ssl() ? 'https' : 'http';
       
   127 		$destination = "$scheme://{$current_site->domain}{$current_site->path}";
       
   128 
       
   129 		/**
       
   130 		 * Fires when a network can be determined but a site cannot.
       
   131 		 *
       
   132 		 * At the time of this action, the only recourse is to redirect somewhere
       
   133 		 * and exit. If you want to declare a particular site, do so earlier.
       
   134 		 *
       
   135 		 * @since 3.9.0
       
   136 		 *
       
   137 		 * @param object $current_site The network that had been determined.
       
   138 		 * @param string $domain       The domain used to search for a site.
       
   139 		 * @param string $path         The path used to search for a site.
       
   140 		 */
       
   141 		do_action( 'ms_site_not_found', $current_site, $domain, $path );
       
   142 
       
   143 		if ( is_subdomain_install() && ! defined( 'NOBLOGREDIRECT' ) ) {
       
   144 			// For a "subdomain" install, redirect to the signup form specifically.
       
   145 			$destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
       
   146 		} elseif ( is_subdomain_install() ) {
       
   147 			// For a "subdomain" install, the NOBLOGREDIRECT constant
       
   148 			// can be used to avoid a redirect to the signup form.
       
   149 			// Using the ms_site_not_found action is preferred to the constant.
       
   150 			if ( '%siteurl%' !== NOBLOGREDIRECT ) {
       
   151 				$destination = NOBLOGREDIRECT;
       
   152 			}
       
   153 		} elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
       
   154 			/*
       
   155 			 * If the domain we were searching for matches the network's domain,
       
   156 			 * it's no use redirecting back to ourselves -- it'll cause a loop.
       
   157 			 * As we couldn't find a site, we're simply not installed.
       
   158 			 */
       
   159 			ms_not_installed();
       
   160 		}
       
   161 
    91 		header( 'Location: ' . $destination );
   162 		header( 'Location: ' . $destination );
    92 		die();
   163 		exit;
    93 	}
   164 	}
    94 
   165 
    95 	if ( ! defined( 'WP_INSTALLING' ) ) {
   166 	// @todo What if the domain of the network doesn't match the current site?
    96 		if ( $current_site && ! $current_blog ) {
   167 	$current_site->cookie_domain = $current_site->domain;
    97 			if ( $current_site->domain != $_SERVER[ 'HTTP_HOST' ] ) {
   168 	if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) {
    98 				header( 'Location: http://' . $current_site->domain . $current_site->path );
   169 		$current_site->cookie_domain = substr( $current_site->cookie_domain, 4 );
    99 				exit;
   170 	}
   100 			}
   171 
   101 			$current_blog = get_blog_details( array( 'domain' => $current_site->domain, 'path' => $current_site->path ), false );
   172 	// Figure out the current network's main site.
   102 		}
   173 	if ( ! isset( $current_site->blog_id ) ) {
   103 		if ( ! $current_blog || ! $current_site )
   174 		if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
   104 			ms_not_installed();
   175 			$current_site->blog_id = $current_blog->blog_id;
       
   176 		} elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
       
   177 			$current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
       
   178 				$current_site->domain, $current_site->path ) );
       
   179 			wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
       
   180 		}
   105 	}
   181 	}
   106 
   182 
   107 	$blog_id = $current_blog->blog_id;
   183 	$blog_id = $current_blog->blog_id;
   108 	$public  = $current_blog->public;
   184 	$public  = $current_blog->public;
   109 
   185 
   110 	if ( empty( $current_blog->site_id ) )
   186 	if ( empty( $current_blog->site_id ) ) {
       
   187 		// This dates to [MU134] and shouldn't be relevant anymore,
       
   188 		// but it could be possible for arguments passed to insert_blog() etc.
   111 		$current_blog->site_id = 1;
   189 		$current_blog->site_id = 1;
       
   190 	}
       
   191 
   112 	$site_id = $current_blog->site_id;
   192 	$site_id = $current_blog->site_id;
   113 
   193 	wp_load_core_site_options( $site_id );
   114 	$current_site = get_current_site_name( $current_site );
       
   115 
       
   116 	if ( ! $blog_id ) {
       
   117 		if ( defined( 'WP_INSTALLING' ) ) {
       
   118 			$current_blog->blog_id = $blog_id = 1;
       
   119 		} else {
       
   120 			wp_load_translations_early();
       
   121 			$msg = ! $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->site'" ) ? ' ' . __( 'Database tables are missing.' ) : '';
       
   122 			wp_die( __( 'No site by that name on this system.' ) . $msg );
       
   123 		}
       
   124 	}
       
   125 }
   194 }
       
   195 
   126 $wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php
   196 $wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php
   127 $wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
   197 $wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
   128 $table_prefix = $wpdb->get_blog_prefix();
   198 $table_prefix = $wpdb->get_blog_prefix();
   129 $_wp_switched_stack = array();
   199 $_wp_switched_stack = array();
   130 $switched = false;
   200 $switched = false;
   131 
   201 
   132 // need to init cache again after blog_id is set
   202 // need to init cache again after blog_id is set
   133 wp_start_object_cache();
   203 wp_start_object_cache();
   134 
   204 
       
   205 if ( ! isset( $current_site->site_name ) ) {
       
   206 	$current_site->site_name = get_site_option( 'site_name' );
       
   207 	if ( ! $current_site->site_name ) {
       
   208 		$current_site->site_name = ucfirst( $current_site->domain );
       
   209 	}
       
   210 }
       
   211 
   135 // Define upload directory constants
   212 // Define upload directory constants
   136 ms_upload_constants();
   213 ms_upload_constants();