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