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