3 * Used to set up and fix common variables and include
4 * the Multisite procedural and class library.
6 * Allows for some configuration in wp-config.php (see ms-default-constants.php)
9 * @subpackage Multisite
13 /** Include Multisite initialization functions */
14 require( ABSPATH . WPINC . '/ms-load.php' );
15 require( ABSPATH . WPINC . '/ms-default-constants.php' );
17 if ( defined( 'SUNRISE' ) )
18 include_once( WP_CONTENT_DIR . '/sunrise.php' );
20 /** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */
21 ms_subdomain_constants();
23 if ( !isset( $current_site ) || !isset( $current_blog ) ) {
25 $domain = addslashes( $_SERVER['HTTP_HOST'] );
26 if ( false !== strpos( $domain, ':' ) ) {
27 if ( substr( $domain, -3 ) == ':80' ) {
28 $domain = substr( $domain, 0, -3 );
29 $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
30 } elseif ( substr( $domain, -4 ) == ':443' ) {
31 $domain = substr( $domain, 0, -4 );
32 $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
34 wp_load_translations_early();
35 wp_die( __( 'Multisite only works without the port number in the URL.' ) );
39 $domain = rtrim( $domain, '.' );
40 $cookie_domain = $domain;
41 if ( substr( $cookie_domain, 0, 4 ) == 'www.' )
42 $cookie_domain = substr( $cookie_domain, 4 );
44 $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $_SERVER['REQUEST_URI'] );
45 $path = str_replace ( '/wp-admin/', '/', $path );
46 $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
48 $current_site = wpmu_current_site();
49 if ( ! isset( $current_site->blog_id ) )
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 ) );
52 if ( is_subdomain_install() ) {
53 $current_blog = wp_cache_get( 'current_blog_' . $domain, 'site-options' );
54 if ( !$current_blog ) {
55 $current_blog = get_blog_details( array( 'domain' => $domain ), false );
57 wp_cache_set( 'current_blog_' . $domain, $current_blog, 'site-options' );
59 if ( $current_blog && $current_blog->site_id != $current_site->id ) {
60 $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $current_blog->site_id ) );
61 if ( ! isset( $current_site->blog_id ) )
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 ) );
64 $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
66 $blogname = htmlspecialchars( substr( $_SERVER[ 'REQUEST_URI' ], strlen( $path ) ) );
67 if ( false !== strpos( $blogname, '/' ) )
68 $blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
69 if ( false !== strpos( $blogname, '?' ) )
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 );
78 wp_cache_set( 'current_blog_' . $domain . $path, $current_blog, 'site-options' );
80 unset($reserved_blognames);
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;
89 $destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
91 header( 'Location: ' . $destination );
95 if ( ! defined( 'WP_INSTALLING' ) ) {
96 if ( $current_site && ! $current_blog ) {
97 if ( $current_site->domain != $_SERVER[ 'HTTP_HOST' ] ) {
98 header( 'Location: http://' . $current_site->domain . $current_site->path );
101 $current_blog = get_blog_details( array( 'domain' => $current_site->domain, 'path' => $current_site->path ), false );
103 if ( ! $current_blog || ! $current_site )
107 $blog_id = $current_blog->blog_id;
108 $public = $current_blog->public;
110 if ( empty( $current_blog->site_id ) )
111 $current_blog->site_id = 1;
112 $site_id = $current_blog->site_id;
114 $current_site = get_current_site_name( $current_site );
117 if ( defined( 'WP_INSTALLING' ) ) {
118 $current_blog->blog_id = $blog_id = 1;
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 );
126 $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 );
128 $table_prefix = $wpdb->get_blog_prefix();
130 // need to init cache again after blog_id is set
131 wp_start_object_cache();
133 // Define upload directory constants
134 ms_upload_constants();