]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/ms-load.php
WordPress 4.3-scripts
[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         $blog = get_blog_details();
72
73         /**
74          * Filter checking the status of the current blog.
75          *
76          * @since 3.0.0
77          *
78          * @param bool null Whether to skip the blog status check. Default null.
79         */
80         $check = apply_filters( 'ms_site_check', null );
81         if ( null !== $check )
82                 return true;
83
84         // Allow super admins to see blocked sites
85         if ( is_super_admin() )
86                 return true;
87
88         if ( '1' == $blog->deleted ) {
89                 if ( file_exists( WP_CONTENT_DIR . '/blog-deleted.php' ) )
90                         return WP_CONTENT_DIR . '/blog-deleted.php';
91                 else
92                         wp_die( __( 'This site is no longer available.' ), '', array( 'response' => 410 ) );
93         }
94
95         if ( '2' == $blog->deleted ) {
96                 if ( file_exists( WP_CONTENT_DIR . '/blog-inactive.php' ) )
97                         return WP_CONTENT_DIR . '/blog-inactive.php';
98                 else
99                         wp_die( sprintf( __( 'This site has not been activated yet. If you are having problems activating your site, please contact <a href="mailto:%1$s">%1$s</a>.' ), str_replace( '@', ' AT ', get_site_option( 'admin_email', 'support@' . get_current_site()->domain ) ) ) );
100         }
101
102         if ( $blog->archived == '1' || $blog->spam == '1' ) {
103                 if ( file_exists( WP_CONTENT_DIR . '/blog-suspended.php' ) )
104                         return WP_CONTENT_DIR . '/blog-suspended.php';
105                 else
106                         wp_die( __( 'This site has been archived or suspended.' ), '', array( 'response' => 410 ) );
107         }
108
109         return true;
110 }
111
112 /**
113  * Retrieve a network object by its domain and path.
114  *
115  * @since 3.9.0
116  *
117  * @global wpdb $wpdb
118  *
119  * @param string   $domain   Domain to check.
120  * @param string   $path     Path to check.
121  * @param int|null $segments Path segments to use. Defaults to null, or the full path.
122  * @return object|false Network object if successful. False when no network is found.
123  */
124 function get_network_by_path( $domain, $path, $segments = null ) {
125         global $wpdb;
126
127         $domains = array( $domain );
128         $pieces = explode( '.', $domain );
129
130         /*
131          * It's possible one domain to search is 'com', but it might as well
132          * be 'localhost' or some other locally mapped domain.
133          */
134         while ( array_shift( $pieces ) ) {
135                 if ( $pieces ) {
136                         $domains[] = implode( '.', $pieces );
137                 }
138         }
139
140         /*
141          * If we've gotten to this function during normal execution, there is
142          * more than one network installed. At this point, who knows how many
143          * we have. Attempt to optimize for the situation where networks are
144          * only domains, thus meaning paths never need to be considered.
145          *
146          * This is a very basic optimization; anything further could have drawbacks
147          * depending on the setup, so this is best done per-install.
148          */
149         $using_paths = true;
150         if ( wp_using_ext_object_cache() ) {
151                 $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
152                 if ( false === $using_paths ) {
153                         $using_paths = (bool) $wpdb->get_var( "SELECT id FROM $wpdb->site WHERE path <> '/' LIMIT 1" );
154                         wp_cache_add( 'networks_have_paths', (int) $using_paths, 'site-options'  );
155                 }
156         }
157
158         $paths = array();
159         if ( $using_paths ) {
160                 $path_segments = array_filter( explode( '/', trim( $path, "/" ) ) );
161
162                 /**
163                  * Filter the number of path segments to consider when searching for a site.
164                  *
165                  * @since 3.9.0
166                  *
167                  * @param int|null $segments The number of path segments to consider. WordPress by default looks at
168                  *                           one path segment. The function default of null only makes sense when you
169                  *                           know the requested path should match a network.
170                  * @param string   $domain   The requested domain.
171                  * @param string   $path     The requested path, in full.
172                  */
173                 $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
174
175                 if ( null !== $segments && count($path_segments ) > $segments ) {
176                         $path_segments = array_slice( $path_segments, 0, $segments );
177                 }
178
179                 while ( count( $path_segments ) ) {
180                         $paths[] = '/' . implode( '/', $path_segments ) . '/';
181                         array_pop( $path_segments );
182                 }
183
184                 $paths[] = '/';
185         }
186
187         /**
188          * Determine a network by its domain and path.
189          *
190          * This allows one to short-circuit the default logic, perhaps by
191          * replacing it with a routine that is more optimal for your setup.
192          *
193          * Return null to avoid the short-circuit. Return false if no network
194          * can be found at the requested domain and path. Otherwise, return
195          * an object from wp_get_network().
196          *
197          * @since 3.9.0
198          *
199          * @param null|bool|object $network  Network value to return by path.
200          * @param string           $domain   The requested domain.
201          * @param string           $path     The requested path, in full.
202          * @param int|null         $segments The suggested number of paths to consult.
203          *                                   Default null, meaning the entire path was to be consulted.
204          * @param array            $paths    The paths to search for, based on $path and $segments.
205          */
206         $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
207         if ( null !== $pre ) {
208                 return $pre;
209         }
210
211         // @todo Consider additional optimization routes, perhaps as an opt-in for plugins.
212         // We already have paths covered. What about how far domains should be drilled down (including www)?
213
214         $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
215
216         if ( ! $using_paths ) {
217                 $network = $wpdb->get_row( "SELECT id, domain, path FROM $wpdb->site
218                         WHERE domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1" );
219                 if ( $network ) {
220                         return wp_get_network( $network );
221                 }
222                 return false;
223
224         } else {
225                 $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
226                 $networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site
227                         WHERE domain IN ($search_domains) AND path IN ($search_paths)
228                         ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" );
229         }
230
231         /*
232          * Domains are sorted by length of domain, then by length of path.
233          * The domain must match for the path to be considered. Otherwise,
234          * a network with the path of / will suffice.
235          */
236         $found = false;
237         foreach ( $networks as $network ) {
238                 if ( $network->domain === $domain || "www.$network->domain" === $domain ) {
239                         if ( in_array( $network->path, $paths, true ) ) {
240                                 $found = true;
241                                 break;
242                         }
243                 }
244                 if ( $network->path === '/' ) {
245                         $found = true;
246                         break;
247                 }
248         }
249
250         if ( $found ) {
251                 return wp_get_network( $network );
252         }
253
254         return false;
255 }
256
257 /**
258  * Retrieve an object containing information about the requested network.
259  *
260  * @since 3.9.0
261  *
262  * @global wpdb $wpdb
263  *
264  * @param object|int $network The network's database row or ID.
265  * @return object|false Object containing network information if found, false if not.
266  */
267 function wp_get_network( $network ) {
268         global $wpdb;
269
270         if ( ! is_object( $network ) ) {
271                 $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network ) );
272                 if ( ! $network ) {
273                         return false;
274                 }
275         }
276
277         return $network;
278 }
279
280 /**
281  * Retrieve a site object by its domain and path.
282  *
283  * @since 3.9.0
284  *
285  * @global wpdb $wpdb
286  *
287  * @param string   $domain   Domain to check.
288  * @param string   $path     Path to check.
289  * @param int|null $segments Path segments to use. Defaults to null, or the full path.
290  * @return object|false Site object if successful. False when no site is found.
291  */
292 function get_site_by_path( $domain, $path, $segments = null ) {
293         global $wpdb;
294
295         $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
296
297         /**
298          * Filter the number of path segments to consider when searching for a site.
299          *
300          * @since 3.9.0
301          *
302          * @param int|null $segments The number of path segments to consider. WordPress by default looks at
303          *                           one path segment following the network path. The function default of
304          *                           null only makes sense when you know the requested path should match a site.
305          * @param string   $domain   The requested domain.
306          * @param string   $path     The requested path, in full.
307          */
308         $segments = apply_filters( 'site_by_path_segments_count', $segments, $domain, $path );
309
310         if ( null !== $segments && count( $path_segments ) > $segments ) {
311                 $path_segments = array_slice( $path_segments, 0, $segments );
312         }
313
314         $paths = array();
315
316         while ( count( $path_segments ) ) {
317                 $paths[] = '/' . implode( '/', $path_segments ) . '/';
318                 array_pop( $path_segments );
319         }
320
321         $paths[] = '/';
322
323         /**
324          * Determine a site by its domain and path.
325          *
326          * This allows one to short-circuit the default logic, perhaps by
327          * replacing it with a routine that is more optimal for your setup.
328          *
329          * Return null to avoid the short-circuit. Return false if no site
330          * can be found at the requested domain and path. Otherwise, return
331          * a site object.
332          *
333          * @since 3.9.0
334          *
335          * @param null|bool|object $site     Site value to return by path.
336          * @param string           $domain   The requested domain.
337          * @param string           $path     The requested path, in full.
338          * @param int|null         $segments The suggested number of paths to consult.
339          *                                   Default null, meaning the entire path was to be consulted.
340          * @param array            $paths    The paths to search for, based on $path and $segments.
341          */
342         $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
343         if ( null !== $pre ) {
344                 return $pre;
345         }
346
347         /*
348          * @todo
349          * get_blog_details(), caching, etc. Consider alternative optimization routes,
350          * perhaps as an opt-in for plugins, rather than using the pre_* filter.
351          * For example: The segments filter can expand or ignore paths.
352          * If persistent caching is enabled, we could query the DB for a path <> '/'
353          * then cache whether we can just always ignore paths.
354          */
355
356         // Either www or non-www is supported, not both. If a www domain is requested,
357         // query for both to provide the proper redirect.
358         $domains = array( $domain );
359         if ( 'www.' === substr( $domain, 0, 4 ) ) {
360                 $domains[] = substr( $domain, 4 );
361                 $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
362         }
363
364         if ( count( $paths ) > 1 ) {
365                 $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
366         }
367
368         if ( count( $domains ) > 1 && count( $paths ) > 1 ) {
369                 $site = $wpdb->get_row( "SELECT * FROM $wpdb->blogs WHERE domain IN ($search_domains) AND path IN ($search_paths) ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC LIMIT 1" );
370         } elseif ( count( $domains ) > 1 ) {
371                 $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE path = %s", $paths[0] );
372                 $sql .= " AND domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1";
373                 $site = $wpdb->get_row( $sql );
374         } elseif ( count( $paths ) > 1 ) {
375                 $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $domains[0] );
376                 $sql .= " AND path IN ($search_paths) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1";
377                 $site = $wpdb->get_row( $sql );
378         } else {
379                 $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domains[0], $paths[0] ) );
380         }
381
382         if ( $site ) {
383                 // @todo get_blog_details()
384                 return $site;
385         }
386
387         return false;
388 }
389
390 /**
391  * Displays a failure message.
392  *
393  * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
394  *
395  * @access private
396  * @since 3.0.0
397  *
398  * @global wpdb   $wpdb
399  * @global string $domain
400  * @global string $path
401  */
402 function ms_not_installed() {
403         global $wpdb, $domain, $path;
404
405         if ( ! is_admin() ) {
406                 dead_db();
407         }
408
409         wp_load_translations_early();
410
411         $title = __( 'Error establishing a database connection' );
412
413         $msg  = '<h1>' . $title . '</h1>';
414         $msg .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
415         $msg .= ' ' . __( 'If you are the owner of this network please check that MySQL is running properly and all tables are error free.' ) . '</p>';
416         $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $wpdb->site ) );
417         if ( ! $wpdb->get_var( $query ) ) {
418                 $msg .= '<p>' . sprintf(
419                         /* translators: %s: table name */
420                         __( '<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.' ),
421                         '<code>' . $wpdb->site . '</code>'
422                 ) . '</p>';
423         } else {
424                 $msg .= '<p>' . sprintf(
425                         /* translators: 1: site url, 2: table name, 3: database name */
426                         __( '<strong>Could not find site %1$s.</strong> Searched for table %2$s in database %3$s. Is that right?' ),
427                         '<code>' . rtrim( $domain . $path, '/' ) . '</code>',
428                         '<code>' . $wpdb->blogs . '</code>',
429                         '<code>' . DB_NAME . '</code>'
430                 ) . '</p>';
431         }
432         $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
433         $msg .= __( 'Read the <a target="_blank" href="https://codex.wordpress.org/Debugging_a_WordPress_Network">bug report</a> page. Some of the guidelines there may help you figure out what went wrong.' );
434         $msg .= ' ' . __( 'If you&#8217;re still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
435         foreach ( $wpdb->tables('global') as $t => $table ) {
436                 if ( 'sitecategories' == $t )
437                         continue;
438                 $msg .= '<li>' . $table . '</li>';
439         }
440         $msg .= '</ul>';
441
442         wp_die( $msg, $title, array( 'response' => 500 ) );
443 }
444
445 /**
446  * This deprecated function formerly set the site_name property of the $current_site object.
447  *
448  * This function simply returns the object, as before.
449  * The bootstrap takes care of setting site_name.
450  *
451  * @access private
452  * @since 3.0.0
453  * @deprecated 3.9.0 Use get_current_site() instead.
454  *
455  * @param object $current_site
456  * @return object
457  */
458 function get_current_site_name( $current_site ) {
459         _deprecated_function( __FUNCTION__, '3.9', 'get_current_site()' );
460         return $current_site;
461 }
462
463 /**
464  * This deprecated function managed much of the site and network loading in multisite.
465  *
466  * The current bootstrap code is now responsible for parsing the site and network load as
467  * well as setting the global $current_site object.
468  *
469  * @access private
470  * @since 3.0.0
471  * @deprecated 3.9.0
472  *
473  * @global object $current_site
474  *
475  * @return object
476  */
477 function wpmu_current_site() {
478         global $current_site;
479         _deprecated_function( __FUNCTION__, '3.9' );
480         return $current_site;
481 }