]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/ms-load.php
Wordpress 4.6
[autoinstalls/wordpress.git] / wp-includes / ms-load.php
1 <?php
2 /**
3  * These functions are needed to load Multisite.
4  *
5  * @since 3.0.0
6  *
7  * @package WordPress
8  * @subpackage Multisite
9  */
10
11 /**
12  * Whether a subdomain configuration is enabled.
13  *
14  * @since 3.0.0
15  *
16  * @return bool True if subdomain configuration is enabled, false otherwise.
17  */
18 function is_subdomain_install() {
19         if ( defined('SUBDOMAIN_INSTALL') )
20                 return SUBDOMAIN_INSTALL;
21
22         return ( defined( 'VHOST' ) && VHOST == 'yes' );
23 }
24
25 /**
26  * Returns array of network plugin files to be included in global scope.
27  *
28  * The default directory is wp-content/plugins. To change the default directory
29  * manually, define `WP_PLUGIN_DIR` and `WP_PLUGIN_URL` in `wp-config.php`.
30  *
31  * @access private
32  * @since 3.1.0
33  *
34  * @return array Files to include.
35  */
36 function wp_get_active_network_plugins() {
37         $active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
38         if ( empty( $active_plugins ) )
39                 return array();
40
41         $plugins = array();
42         $active_plugins = array_keys( $active_plugins );
43         sort( $active_plugins );
44
45         foreach ( $active_plugins as $plugin ) {
46                 if ( ! validate_file( $plugin ) // $plugin must validate as file
47                         && '.php' == substr( $plugin, -4 ) // $plugin must end with '.php'
48                         && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist
49                         )
50                 $plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
51         }
52         return $plugins;
53 }
54
55 /**
56  * Checks status of current blog.
57  *
58  * Checks if the blog is deleted, inactive, archived, or spammed.
59  *
60  * Dies with a default message if the blog does not pass the check.
61  *
62  * To change the default message when a blog does not pass the check,
63  * use the wp-content/blog-deleted.php, blog-inactive.php and
64  * blog-suspended.php drop-ins.
65  *
66  * @since 3.0.0
67  *
68  * @return true|string Returns true on success, or drop-in file to include.
69  */
70 function ms_site_check() {
71
72         /**
73          * Filters checking the status of the current blog.
74          *
75          * @since 3.0.0
76          *
77          * @param bool null Whether to skip the blog status check. Default null.
78         */
79         $check = apply_filters( 'ms_site_check', null );
80         if ( null !== $check )
81                 return true;
82
83         // Allow super admins to see blocked sites
84         if ( is_super_admin() )
85                 return true;
86
87         $blog = get_blog_details();
88
89         if ( '1' == $blog->deleted ) {
90                 if ( file_exists( WP_CONTENT_DIR . '/blog-deleted.php' ) )
91                         return WP_CONTENT_DIR . '/blog-deleted.php';
92                 else
93                         wp_die( __( 'This site is no longer available.' ), '', array( 'response' => 410 ) );
94         }
95
96         if ( '2' == $blog->deleted ) {
97                 if ( file_exists( WP_CONTENT_DIR . '/blog-inactive.php' ) ) {
98                         return WP_CONTENT_DIR . '/blog-inactive.php';
99                 } else {
100                         $admin_email = str_replace( '@', ' AT ', get_site_option( 'admin_email', 'support@' . get_current_site()->domain ) );
101                         wp_die(
102                                 /* translators: %s: admin email link */
103                                 sprintf( __( 'This site has not been activated yet. If you are having problems activating your site, please contact %s.' ),
104                                         sprintf( '<a href="mailto:%s">%s</a>', $admin_email )
105                                 )
106                         );
107                 }
108         }
109
110         if ( $blog->archived == '1' || $blog->spam == '1' ) {
111                 if ( file_exists( WP_CONTENT_DIR . '/blog-suspended.php' ) )
112                         return WP_CONTENT_DIR . '/blog-suspended.php';
113                 else
114                         wp_die( __( 'This site has been archived or suspended.' ), '', array( 'response' => 410 ) );
115         }
116
117         return true;
118 }
119
120 /**
121  * Retrieve the closest matching network for a domain and path.
122  *
123  * @since 3.9.0
124  *
125  * @internal In 4.4.0, converted to a wrapper for WP_Network::get_by_path()
126  *
127  * @param string   $domain   Domain to check.
128  * @param string   $path     Path to check.
129  * @param int|null $segments Path segments to use. Defaults to null, or the full path.
130  * @return WP_Network|false Network object if successful. False when no network is found.
131  */
132 function get_network_by_path( $domain, $path, $segments = null ) {
133         return WP_Network::get_by_path( $domain, $path, $segments );
134 }
135
136 /**
137  * Retrieve an object containing information about the requested network.
138  *
139  * @since 3.9.0
140  *
141  * @internal In 4.6.0, converted to use get_network()
142  *
143  * @param object|int $network The network's database row or ID.
144  * @return WP_Network|false Object containing network information if found, false if not.
145  */
146 function wp_get_network( $network ) {
147         $network = get_network( $network );
148         if ( null === $network ) {
149                 return false;
150         }
151
152         return $network;
153 }
154
155 /**
156  * Retrieve a site object by its domain and path.
157  *
158  * @since 3.9.0
159  *
160  * @global wpdb $wpdb WordPress database abstraction object.
161  *
162  * @param string   $domain   Domain to check.
163  * @param string   $path     Path to check.
164  * @param int|null $segments Path segments to use. Defaults to null, or the full path.
165  * @return object|false Site object if successful. False when no site is found.
166  */
167 function get_site_by_path( $domain, $path, $segments = null ) {
168         $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
169
170         /**
171          * Filters the number of path segments to consider when searching for a site.
172          *
173          * @since 3.9.0
174          *
175          * @param int|null $segments The number of path segments to consider. WordPress by default looks at
176          *                           one path segment following the network path. The function default of
177          *                           null only makes sense when you know the requested path should match a site.
178          * @param string   $domain   The requested domain.
179          * @param string   $path     The requested path, in full.
180          */
181         $segments = apply_filters( 'site_by_path_segments_count', $segments, $domain, $path );
182
183         if ( null !== $segments && count( $path_segments ) > $segments ) {
184                 $path_segments = array_slice( $path_segments, 0, $segments );
185         }
186
187         $paths = array();
188
189         while ( count( $path_segments ) ) {
190                 $paths[] = '/' . implode( '/', $path_segments ) . '/';
191                 array_pop( $path_segments );
192         }
193
194         $paths[] = '/';
195
196         /**
197          * Determine a site by its domain and path.
198          *
199          * This allows one to short-circuit the default logic, perhaps by
200          * replacing it with a routine that is more optimal for your setup.
201          *
202          * Return null to avoid the short-circuit. Return false if no site
203          * can be found at the requested domain and path. Otherwise, return
204          * a site object.
205          *
206          * @since 3.9.0
207          *
208          * @param null|bool|object $site     Site value to return by path.
209          * @param string           $domain   The requested domain.
210          * @param string           $path     The requested path, in full.
211          * @param int|null         $segments The suggested number of paths to consult.
212          *                                   Default null, meaning the entire path was to be consulted.
213          * @param array            $paths    The paths to search for, based on $path and $segments.
214          */
215         $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
216         if ( null !== $pre ) {
217                 return $pre;
218         }
219
220         /*
221          * @todo
222          * get_blog_details(), caching, etc. Consider alternative optimization routes,
223          * perhaps as an opt-in for plugins, rather than using the pre_* filter.
224          * For example: The segments filter can expand or ignore paths.
225          * If persistent caching is enabled, we could query the DB for a path <> '/'
226          * then cache whether we can just always ignore paths.
227          */
228
229         // Either www or non-www is supported, not both. If a www domain is requested,
230         // query for both to provide the proper redirect.
231         $domains = array( $domain );
232         if ( 'www.' === substr( $domain, 0, 4 ) ) {
233                 $domains[] = substr( $domain, 4 );
234         }
235
236         $args = array(
237                 'domain__in' => $domains,
238                 'path__in' => $paths,
239                 'number' => 1,
240         );
241
242         if ( count( $domains ) > 1 ) {
243                 $args['orderby']['domain_length'] = 'DESC';
244         }
245
246         if ( count( $paths ) > 1 ) {
247                 $args['orderby']['path_length'] = 'DESC';
248         }
249
250         $result = get_sites( $args );
251         $site = array_shift( $result );
252
253         if ( $site ) {
254                 // @todo get_blog_details()
255                 return $site;
256         }
257
258         return false;
259 }
260
261 /**
262  * Identifies the network and site of a requested domain and path and populates the
263  * corresponding network and site global objects as part of the multisite bootstrap process.
264  *
265  * Prior to 4.6.0, this was a procedural block in `ms-settings.php`. It was wrapped into
266  * a function to facilitate unit tests. It should not be used outside of core.
267  *
268  * Usually, it's easier to query the site first, which then declares its network.
269  * In limited situations, we either can or must find the network first.
270  *
271  * If a network and site are found, a `true` response will be returned so that the
272  * request can continue.
273  *
274  * If neither a network or site is found, `false` or a URL string will be returned
275  * so that either an error can be shown or a redirect can occur.
276  *
277  * @since 4.6.0
278  * @access private
279  *
280  * @global wpdb       $wpdb         WordPress database abstraction object.
281  * @global WP_Network $current_site The current network.
282  * @global WP_Site    $current_blog The current site.
283  *
284  * @param string $domain    The requested domain.
285  * @param string $path      The requested path.
286  * @param bool   $subdomain Optional. Whether a subdomain (true) or subdirectory (false) configuration.
287  *                          Default false.
288  * @return bool|string True if bootstrap successfully populated `$current_blog` and `$current_site`.
289  *                     False if bootstrap could not be properly completed.
290  *                     Redirect URL if parts exist, but the request as a whole can not be fulfilled.
291  */
292 function ms_load_current_site_and_network( $domain, $path, $subdomain = false ) {
293         global $wpdb, $current_site, $current_blog;
294
295         // If the network is defined in wp-config.php, we can simply use that.
296         if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
297                 $current_site = new stdClass;
298                 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
299                 $current_site->domain = DOMAIN_CURRENT_SITE;
300                 $current_site->path = PATH_CURRENT_SITE;
301                 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
302                         $current_site->blog_id = BLOG_ID_CURRENT_SITE;
303                 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
304                         $current_site->blog_id = BLOGID_CURRENT_SITE;
305                 }
306
307                 if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
308                         $current_blog = get_site_by_path( $domain, $path );
309                 } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
310                         // If the current network has a path and also matches the domain and path of the request,
311                         // we need to look for a site using the first path segment following the network's path.
312                         $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
313                 } else {
314                         // Otherwise, use the first path segment (as usual).
315                         $current_blog = get_site_by_path( $domain, $path, 1 );
316                 }
317
318         } elseif ( ! $subdomain ) {
319                 /*
320                  * A "subdomain" install can be re-interpreted to mean "can support any domain".
321                  * If we're not dealing with one of these installs, then the important part is determining
322                  * the network first, because we need the network's path to identify any sites.
323                  */
324                 if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
325                         // Are there even two networks installed?
326                         $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
327                         if ( 1 === $wpdb->num_rows ) {
328                                 $current_site = new WP_Network( $one_network );
329                                 wp_cache_add( 'current_network', $current_site, 'site-options' );
330                         } elseif ( 0 === $wpdb->num_rows ) {
331                                 // A network not found hook should fire here.
332                                 return false;
333                         }
334                 }
335
336                 if ( empty( $current_site ) ) {
337                         $current_site = WP_Network::get_by_path( $domain, $path, 1 );
338                 }
339
340                 if ( empty( $current_site ) ) {
341                         /**
342                          * Fires when a network cannot be found based on the requested domain and path.
343                          *
344                          * At the time of this action, the only recourse is to redirect somewhere
345                          * and exit. If you want to declare a particular network, do so earlier.
346                          *
347                          * @since 4.4.0
348                          *
349                          * @param string $domain       The domain used to search for a network.
350                          * @param string $path         The path used to search for a path.
351                          */
352                         do_action( 'ms_network_not_found', $domain, $path );
353
354                         return false;
355                 } elseif ( $path === $current_site->path ) {
356                         $current_blog = get_site_by_path( $domain, $path );
357                 } else {
358                         // Search the network path + one more path segment (on top of the network path).
359                         $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
360                 }
361         } else {
362                 // Find the site by the domain and at most the first path segment.
363                 $current_blog = get_site_by_path( $domain, $path, 1 );
364                 if ( $current_blog ) {
365                         $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
366                 } else {
367                         // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
368                         $current_site = WP_Network::get_by_path( $domain, $path, 1 );
369                 }
370         }
371
372         // The network declared by the site trumps any constants.
373         if ( $current_blog && $current_blog->site_id != $current_site->id ) {
374                 $current_site = WP_Network::get_instance( $current_blog->site_id );
375         }
376
377         // No network has been found, bail.
378         if ( empty( $current_site ) ) {
379                 /** This action is documented in wp-includes/ms-settings.php */
380                 do_action( 'ms_network_not_found', $domain, $path );
381
382                 return false;
383         }
384
385         // During activation of a new subdomain, the requested site does not yet exist.
386         if ( empty( $current_blog ) && wp_installing() ) {
387                 $current_blog = new stdClass;
388                 $current_blog->blog_id = $blog_id = 1;
389                 $current_blog->public = 1;
390         }
391
392         // No site has been found, bail.
393         if ( empty( $current_blog ) ) {
394                 // We're going to redirect to the network URL, with some possible modifications.
395                 $scheme = is_ssl() ? 'https' : 'http';
396                 $destination = "$scheme://{$current_site->domain}{$current_site->path}";
397
398                 /**
399                  * Fires when a network can be determined but a site cannot.
400                  *
401                  * At the time of this action, the only recourse is to redirect somewhere
402                  * and exit. If you want to declare a particular site, do so earlier.
403                  *
404                  * @since 3.9.0
405                  *
406                  * @param object $current_site The network that had been determined.
407                  * @param string $domain       The domain used to search for a site.
408                  * @param string $path         The path used to search for a site.
409                  */
410                 do_action( 'ms_site_not_found', $current_site, $domain, $path );
411
412                 if ( $subdomain && ! defined( 'NOBLOGREDIRECT' ) ) {
413                         // For a "subdomain" install, redirect to the signup form specifically.
414                         $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
415                 } elseif ( $subdomain ) {
416                         // For a "subdomain" install, the NOBLOGREDIRECT constant
417                         // can be used to avoid a redirect to the signup form.
418                         // Using the ms_site_not_found action is preferred to the constant.
419                         if ( '%siteurl%' !== NOBLOGREDIRECT ) {
420                                 $destination = NOBLOGREDIRECT;
421                         }
422                 } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
423                         /*
424                          * If the domain we were searching for matches the network's domain,
425                          * it's no use redirecting back to ourselves -- it'll cause a loop.
426                          * As we couldn't find a site, we're simply not installed.
427                          */
428                         return false;
429                 }
430
431                 return $destination;
432         }
433
434         // Figure out the current network's main site.
435         if ( empty( $current_site->blog_id ) ) {
436                 if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
437                         $current_site->blog_id = $current_blog->blog_id;
438                 } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
439                         $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
440                                 $current_site->domain, $current_site->path ) );
441                         wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
442                 }
443         }
444
445         return true;
446 }
447
448 /**
449  * Displays a failure message.
450  *
451  * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
452  *
453  * @access private
454  * @since 3.0.0
455  * @since 4.4.0 The `$domain` and `$path` parameters were added.
456  *
457  * @global wpdb $wpdb WordPress database abstraction object.
458  *
459  * @param string $domain The requested domain for the error to reference.
460  * @param string $path   The requested path for the error to reference.
461  */
462 function ms_not_installed( $domain, $path ) {
463         global $wpdb;
464
465         if ( ! is_admin() ) {
466                 dead_db();
467         }
468
469         wp_load_translations_early();
470
471         $title = __( 'Error establishing a database connection' );
472
473         $msg  = '<h1>' . $title . '</h1>';
474         $msg .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
475         $msg .= ' ' . __( 'If you are the owner of this network please check that MySQL is running properly and all tables are error free.' ) . '</p>';
476         $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $wpdb->site ) );
477         if ( ! $wpdb->get_var( $query ) ) {
478                 $msg .= '<p>' . sprintf(
479                         /* translators: %s: table name */
480                         __( '<strong>Database tables are missing.</strong> This means that MySQL is not running, WordPress was not installed properly, or someone deleted %s. You really should look at your database now.' ),
481                         '<code>' . $wpdb->site . '</code>'
482                 ) . '</p>';
483         } else {
484                 $msg .= '<p>' . sprintf(
485                         /* translators: 1: site url, 2: table name, 3: database name */
486                         __( '<strong>Could not find site %1$s.</strong> Searched for table %2$s in database %3$s. Is that right?' ),
487                         '<code>' . rtrim( $domain . $path, '/' ) . '</code>',
488                         '<code>' . $wpdb->blogs . '</code>',
489                         '<code>' . DB_NAME . '</code>'
490                 ) . '</p>';
491         }
492         $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
493         /* translators: %s: Codex URL */
494         $msg .= sprintf( __( 'Read the <a href="%s" target="_blank">bug report</a> page. Some of the guidelines there may help you figure out what went wrong.' ),
495                 __( 'https://codex.wordpress.org/Debugging_a_WordPress_Network' )
496         );
497         $msg .= ' ' . __( 'If you&#8217;re still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
498         foreach ( $wpdb->tables('global') as $t => $table ) {
499                 if ( 'sitecategories' == $t )
500                         continue;
501                 $msg .= '<li>' . $table . '</li>';
502         }
503         $msg .= '</ul>';
504
505         wp_die( $msg, $title, array( 'response' => 500 ) );
506 }
507
508 /**
509  * This deprecated function formerly set the site_name property of the $current_site object.
510  *
511  * This function simply returns the object, as before.
512  * The bootstrap takes care of setting site_name.
513  *
514  * @access private
515  * @since 3.0.0
516  * @deprecated 3.9.0 Use get_current_site() instead.
517  *
518  * @param object $current_site
519  * @return object
520  */
521 function get_current_site_name( $current_site ) {
522         _deprecated_function( __FUNCTION__, '3.9.0', 'get_current_site()' );
523         return $current_site;
524 }
525
526 /**
527  * This deprecated function managed much of the site and network loading in multisite.
528  *
529  * The current bootstrap code is now responsible for parsing the site and network load as
530  * well as setting the global $current_site object.
531  *
532  * @access private
533  * @since 3.0.0
534  * @deprecated 3.9.0
535  *
536  * @global object $current_site
537  *
538  * @return object
539  */
540 function wpmu_current_site() {
541         global $current_site;
542         _deprecated_function( __FUNCTION__, '3.9.0' );
543         return $current_site;
544 }