WordPress.org

Make WordPress Core

Ticket #27003: 27003.11.diff

File 27003.11.diff, 13.2 KB (added by nacin, 6 years ago)
  • src/wp-includes/ms-load.php

     
    145145function get_network_by_path( $domain, $path ) {
    146146        global $wpdb;
    147147
    148         $network_id = false;
    149148
    150149        $domains = $exact_domains = array( $domain );
    151150        $pieces = explode( '.', $domain );
     
    191190        }
    192191
    193192        if ( $found ) {
    194                 $network = wp_get_network( $network );
    195 
    196                 return $network;
     193                return wp_get_network( $network );
    197194        }
    198195
    199196        return false;
     
    220217        return $network;
    221218}
    222219
    223 /**
    224  * Sets current_site object.
    225  *
    226  * @access private
    227  * @since 3.0.0
    228  * @return object $current_site object
    229  */
    230 function wpmu_current_site() {
    231         global $wpdb, $current_site, $domain, $path;
     220function get_site_by_path( $domain, $path ) {
     221        global $wpdb;
    232222
    233         if ( empty( $current_site ) )
    234                 $current_site = new stdClass;
     223        if ( is_array( $path ) ) {
     224                $paths = $path;
     225                $path = $paths[0];
     226        } else {
     227                $paths = array( '/', $path );
     228        }
    235229
    236         // 1. If constants are defined, that's our network.
    237         if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
    238                 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
    239                 $current_site->domain = DOMAIN_CURRENT_SITE;
    240                 $current_site->path   = $path = PATH_CURRENT_SITE;
    241                 if ( defined( 'BLOG_ID_CURRENT_SITE' ) )
    242                         $current_site->blog_id = BLOG_ID_CURRENT_SITE;
    243                 elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated.
    244                         $current_site->blog_id = BLOGID_CURRENT_SITE;
     230        // @todo get_blog_details(), caching, etc.
     231        if ( count( $paths ) > 1 ) {
     232                $paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
     233                $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs
     234                        WHERE domain = %s AND path IN ($paths) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1", $domain ) );
     235        } else {
     236                $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s and path = %s", $domain, $path ) );
     237        }
    245238
    246         // 2. Pull the network from cache, if possible.
    247         } elseif ( ! $current_site = wp_cache_get( 'current_site', 'site-options' ) ) {
    248 
    249                 // 3. See if they have only one network.
    250                 $networks = $wpdb->get_col( "SELECT id FROM $wpdb->site LIMIT 2" );
    251 
    252                 if ( count( $networks ) <= 1 ) {
    253                         $current_site = wp_get_network( $networks[0]->id );
    254 
    255                         $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id
    256                                 FROM $wpdb->blogs WHERE domain = %s AND path = %s",
    257                                 $current_site->domain, $current_site->path ) );
    258 
    259                         wp_cache_set( 'current_site', 'site-options' );
    260 
    261                 // 4. Multiple networks are in play. Determine which via domain and path.
    262                 } else {
    263                         // Find the first path segment.
    264                         $path = substr( $_SERVER['REQUEST_URI'], 0, 1 + strpos( $_SERVER['REQUEST_URI'], '/', 1 ) );
    265                         $current_site = get_network_by_path( $domain, $path );
    266 
    267                         // Option 1. We did not find anything.
    268                         if ( ! $current_site ) {
    269                                 wp_load_translations_early();
    270                                 wp_die( __( 'No site defined on this host. If you are the owner of this site, please check <a href="http://codex.wordpress.org/Debugging_a_WordPress_Network">Debugging a WordPress Network</a> for help.' ) );
    271                         }
    272                 }
     239        if ( $site ) {
     240                // @todo get_blog_details()
     241                return $site;
    273242        }
    274243
    275         // Option 2. We found something. Load up site meta and return.
    276         wp_load_core_site_options();
    277         $current_site = get_current_site_name( $current_site );
    278         return $current_site;
     244        return false;
    279245}
    280246
    281247/**
  • src/wp-includes/ms-settings.php

     
    2222
    2323if ( !isset( $current_site ) || !isset( $current_blog ) ) {
    2424
    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 );
     25        // Given the domain and path, let's try to identify the network and site.
     26        // Usually, it's easier to query the site first, which declares its network.
     27        // In limited situations, though, we either can or must find the network first.
     28
     29        $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
     30        $path = stripslashes( $_SERVER['REQUEST_URI'] );
     31        if ( is_admin() ) {
     32                $path = preg_replace( '#(.*)/wp-admin/.*#', '$1', $path );
     33        }
     34        // If the network is defined in wp-config.php, we can simply use that.
     35        if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
     36                $current_site = new stdClass;
     37                $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
     38                $current_site->domain = DOMAIN_CURRENT_SITE;
     39                $current_site->path = PATH_CURRENT_SITE;
     40                if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
     41                        $current_site->blog_id = BLOG_ID_CURRENT_SITE;
     42                } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
     43                        $current_site->blog_id = BLOGID_CURRENT_SITE;
     44                }
     45
     46                if ( '/' !== $current_site->path && $current_site->domain === $domain && 0 === strpos( $path, $current_site->path ) ) {
     47                        // If the current network has a path and also matches the domain and path of the request,
     48                        // we need chop off the network's path when looking for a site.
     49                        $current_blog = get_site_by_path( $domain, array( $path, substr( $path, strlen( $current_site->path ) - 1 ) ) );
    3350                } else {
    34                         wp_load_translations_early();
    35                         wp_die( __( 'Multisite only works without the port number in the URL.' ) );
     51                        // Otherwise, use the first path segment (as usual).
     52                        $current_blog = get_site_by_path( $domain, substr( $path, 0, 1 + strpos( $path, '/', 1 ) ) );
    3653                }
     54
     55        } elseif ( ! is_subdomain_install() ) {
     56                /*
     57                 * A "subdomain" install can be re-interpreted to mean "can support any domain".
     58                 * If we're not dealing with one of these installs, then the important part is determing
     59                 * the network first, because we need the network's path to identify any sites.
     60                 */
     61                if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
     62                        // Are there even two networks installed?
     63                        $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
     64                        if ( $wpdb->num_rows === 1 ) {
     65                                $current_site = wp_get_network( $one_network );
     66                                wp_cache_set( 'current_network', 'site-options' );
     67                        }
     68                }
     69                if ( empty( $current_site ) ) {
     70                        $current_site = get_network_by_path( $domain, substr( $path, 0, 1 + strpos( $path, '/', 1 ) ) );
     71                }
     72                // Search the network path + one more path segment (and also the network path).
     73                $current_blog = get_site_by_path( $domain, array( $path, substr( $path, strlen( $current_site->path ) - 1 ) ) );
     74        } else {
     75                // Find the site by the domain and at most the first path segment.
     76                $current_blog = get_site_by_path( $domain, substr( $path, 0, 1 + strpos( $path, '/', 1 ) ) );
     77                if ( $current_blog ) {
     78                        $current_site = wp_get_network( $current_blog->site_id ? $current_blog->site_id : 1 );
     79                } else {
     80                        // If you don't have a site with the same domain/path as a network, you're pretty screwed, but what the hell:
     81                        $current_site = get_network_by_path( $domain, substr( $path, 0, 1 + strpos( $path, '/', 1 ) ) );
     82                }
    3783        }
    3884
    39         $domain = rtrim( $domain, '.' );
     85        // If we don't have a network by now, we have a problem.
     86        if ( empty( $current_site ) ) {
     87                ms_not_installed();
     88        }
    4089
    41         $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $_SERVER['REQUEST_URI'] );
    42         $path = str_replace ( '/wp-admin/', '/', $path );
    43         $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
    44 
    45         $current_site = wpmu_current_site();
     90        // @todo What if the domain of the network doesn't match the current site?
    4691        $current_site->cookie_domain = $current_site->domain;
    4792        if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) {
    4893                $current_site->cookie_domain = substr( $current_site->cookie_domain, 4 );
    4994        }
    5095
    51         if ( ! isset( $current_site->blog_id ) )
    52                 $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 ) );
    53 
    54         if ( is_subdomain_install() ) {
    55                 $current_blog = wp_cache_get( 'current_blog_' . $domain, 'site-options' );
    56                 if ( !$current_blog ) {
    57                         $current_blog = get_blog_details( array( 'domain' => $domain ), false );
    58                         if ( $current_blog )
    59                                 wp_cache_set( 'current_blog_' . $domain, $current_blog, 'site-options' );
     96        // Figure out the current network's main site.
     97        if ( ! isset( $current_site->blog_id ) ) {
     98                if ( $current_blog && $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
     99                        $current_site->blog_id = $current_blog->blog_id;
     100                } else {
     101                        // @todo we should be able to cache the blog ID of a network's main site easily.
     102                        $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
     103                                $current_site->domain, $current_site->path ) );
    60104                }
    61                 if ( $current_blog && $current_blog->site_id != $current_site->id ) {
    62                         $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $current_blog->site_id ) );
    63                         if ( ! isset( $current_site->blog_id ) )
    64                                 $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 ) );
    65                 } else
    66                         $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
    67         } else {
    68                 $blogname = htmlspecialchars( substr( $_SERVER[ 'REQUEST_URI' ], strlen( $path ) ) );
    69                 if ( false !== strpos( $blogname, '/' ) )
    70                         $blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
    71                 if ( false !== strpos( $blogname, '?' ) )
    72                         $blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
    73                 $reserved_blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
    74                 if ( $blogname != '' && ! in_array( $blogname, $reserved_blognames ) && ! is_file( $blogname ) )
    75                         $path .= $blogname . '/';
    76                 $current_blog = wp_cache_get( 'current_blog_' . $domain . $path, 'site-options' );
    77                 if ( ! $current_blog ) {
    78                         $current_blog = get_blog_details( array( 'domain' => $domain, 'path' => $path ), false );
    79                         if ( $current_blog )
    80                                 wp_cache_set( 'current_blog_' . $domain . $path, $current_blog, 'site-options' );
    81                 }
    82                 unset($reserved_blognames);
    83105        }
    84106
    85         if ( ! defined( 'WP_INSTALLING' ) && is_subdomain_install() && ! is_object( $current_blog ) ) {
    86                 if ( defined( 'NOBLOGREDIRECT' ) ) {
    87                         $destination = NOBLOGREDIRECT;
    88                         if ( '%siteurl%' == $destination )
    89                                 $destination = "http://" . $current_site->domain . $current_site->path;
     107        // If we haven't figured out our site, the hell, man.
     108        if ( empty( $current_blog ) ) {
     109                if ( defined( 'WP_INSTALLING' ) ) {
     110                        $current_blog->blog_id = $blog_id = 1;
     111
     112                } elseif ( is_subdomain_install() ) {
     113                        // @todo This is only for an open registration subdomain network.
     114                        if ( defined( 'NOBLOGREDIRECT' ) ) {
     115                                if ( '%siteurl%' === NOBLOGREDIRECT ) {
     116                                        $destination = "http://" . $current_site->domain . $current_site->path;
     117                                } else {
     118                                        $destination = NOBLOGREDIRECT;
     119                                }
     120                        } else {
     121                                $destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
     122                        }
     123                        header( 'Location: ' . $destination );
     124                        exit;
     125
    90126                } else {
    91                         $destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
    92                 }
    93                 header( 'Location: ' . $destination );
    94                 die();
    95         }
    96 
    97         if ( ! defined( 'WP_INSTALLING' ) ) {
    98                 if ( $current_site && ! $current_blog ) {
    99                         if ( $current_site->domain != $_SERVER[ 'HTTP_HOST' ] ) {
     127                        if ( 0 !== strcasecmp( $current_site->domain, $domain ) ) {
    100128                                header( 'Location: http://' . $current_site->domain . $current_site->path );
    101129                                exit;
    102130                        }
    103                         $current_blog = get_blog_details( array( 'domain' => $current_site->domain, 'path' => $current_site->path ), false );
     131                        ms_not_installed();
    104132                }
    105                 if ( ! $current_blog || ! $current_site )
    106                         ms_not_installed();
    107133        }
    108134
    109135        $blog_id = $current_blog->blog_id;
    110136        $public  = $current_blog->public;
    111137
    112         if ( empty( $current_blog->site_id ) )
     138        if ( empty( $current_blog->site_id ) ) {
     139                // This dates to [MU134] and shouldn't be relevant anymore,
     140                // but it could be possible for arguments passed to insert_blog() etc.
    113141                $current_blog->site_id = 1;
     142        }
     143
    114144        $site_id = $current_blog->site_id;
    115 
     145        wp_load_core_site_options();
    116146        $current_site = get_current_site_name( $current_site );
     147}
    117148
    118         if ( ! $blog_id ) {
    119                 if ( defined( 'WP_INSTALLING' ) ) {
    120                         $current_blog->blog_id = $blog_id = 1;
    121                 } else {
    122                         wp_load_translations_early();
    123                         $msg = ! $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->site'" ) ? ' ' . __( 'Database tables are missing.' ) : '';
    124                         wp_die( __( 'No site by that name on this system.' ) . $msg );
    125                 }
    126         }
    127 }
    128149$wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php
    129150$wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
    130151$table_prefix = $wpdb->get_blog_prefix();