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