| 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 ) ) ); |
| | 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 | } |
| 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 ) ); |
| 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); |
| 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 | |