]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/ms-load.php
WordPress 4.1.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         while ( count( $path_segments ) ) {
312                 $paths[] = '/' . implode( '/', $path_segments ) . '/';
313                 array_pop( $path_segments );
314         }
315
316         $paths[] = '/';
317
318         /**
319          * Determine a site by its domain and path.
320          *
321          * This allows one to short-circuit the default logic, perhaps by
322          * replacing it with a routine that is more optimal for your setup.
323          *
324          * Return null to avoid the short-circuit. Return false if no site
325          * can be found at the requested domain and path. Otherwise, return
326          * a site object.
327          *
328          * @since 3.9.0
329          *
330          * @param null|bool|object $site     Site value to return by path.
331          * @param string           $domain   The requested domain.
332          * @param string           $path     The requested path, in full.
333          * @param int|null         $segments The suggested number of paths to consult.
334          *                                   Default null, meaning the entire path was to be consulted.
335          * @param array            $paths    The paths to search for, based on $path and $segments.
336          */
337         $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
338         if ( null !== $pre ) {
339                 return $pre;
340         }
341
342         /*
343          * @todo
344          * get_blog_details(), caching, etc. Consider alternative optimization routes,
345          * perhaps as an opt-in for plugins, rather than using the pre_* filter.
346          * For example: The segments filter can expand or ignore paths.
347          * If persistent caching is enabled, we could query the DB for a path <> '/'
348          * then cache whether we can just always ignore paths.
349          */
350
351         // Either www or non-www is supported, not both. If a www domain is requested,
352         // query for both to provide the proper redirect.
353         $domains = array( $domain );
354         if ( 'www.' === substr( $domain, 0, 4 ) ) {
355                 $domains[] = substr( $domain, 4 );
356                 $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
357         }
358
359         if ( count( $paths ) > 1 ) {
360                 $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
361         }
362
363         if ( count( $domains ) > 1 && count( $paths ) > 1 ) {
364                 $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" );
365         } elseif ( count( $domains ) > 1 ) {
366                 $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE path = %s", $paths[0] );
367                 $sql .= " AND domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1";
368                 $site = $wpdb->get_row( $sql );
369         } elseif ( count( $paths ) > 1 ) {
370                 $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $domains[0] );
371                 $sql .= " AND path IN ($search_paths) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1";
372                 $site = $wpdb->get_row( $sql );
373         } else {
374                 $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domains[0], $paths[0] ) );
375         }
376
377         if ( $site ) {
378                 // @todo get_blog_details()
379                 return $site;
380         }
381
382         return false;
383 }
384
385 /**
386  * Displays a failure message.
387  *
388  * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
389  *
390  * @access private
391  * @since 3.0.0
392  */
393 function ms_not_installed() {
394         global $wpdb, $domain, $path;
395
396         wp_load_translations_early();
397
398         $title = __( 'Error establishing a database connection' );
399         $msg  = '<h1>' . $title . '</h1>';
400         if ( ! is_admin() ) {
401                 die( $msg );
402         }
403         $msg .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
404         $msg .= ' ' . __( 'If you are the owner of this network please check that MySQL is running properly and all tables are error free.' ) . '</p>';
405         $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $wpdb->site ) );
406         if ( ! $wpdb->get_var( $query ) ) {
407                 $msg .= '<p>' . sprintf( __( '<strong>Database tables are missing.</strong> This means that MySQL is not running, WordPress was not installed properly, or someone deleted <code>%s</code>. You really should look at your database now.' ), $wpdb->site ) . '</p>';
408         } else {
409                 $msg .= '<p>' . sprintf( __( '<strong>Could not find site <code>%1$s</code>.</strong> Searched for table <code>%2$s</code> in database <code>%3$s</code>. Is that right?' ), rtrim( $domain . $path, '/' ), $wpdb->blogs, DB_NAME ) . '</p>';
410         }
411         $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
412         $msg .= __( 'Read the <a target="_blank" href="http://codex.wordpress.org/Debugging_a_WordPress_Network">bug report</a> page. Some of the guidelines there may help you figure out what went wrong.' );
413         $msg .= ' ' . __( 'If you&#8217;re still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
414         foreach ( $wpdb->tables('global') as $t => $table ) {
415                 if ( 'sitecategories' == $t )
416                         continue;
417                 $msg .= '<li>' . $table . '</li>';
418         }
419         $msg .= '</ul>';
420
421         wp_die( $msg, $title );
422 }
423
424 /**
425  * This deprecated function formerly set the site_name property of the $current_site object.
426  *
427  * This function simply returns the object, as before.
428  * The bootstrap takes care of setting site_name.
429  *
430  * @access private
431  * @since 3.0.0
432  * @deprecated 3.9.0 Use get_current_site() instead.
433  *
434  * @param object $current_site
435  * @return object
436  */
437 function get_current_site_name( $current_site ) {
438         _deprecated_function( __FUNCTION__, '3.9', 'get_current_site()' );
439         return $current_site;
440 }
441
442 /**
443  * This deprecated function managed much of the site and network loading in multisite.
444  *
445  * The current bootstrap code is now responsible for parsing the site and network load as
446  * well as setting the global $current_site object.
447  *
448  * @access private
449  * @since 3.0.0
450  * @deprecated 3.9.0
451  *
452  * @return object
453  */
454 function wpmu_current_site() {
455         global $current_site;
456         _deprecated_function( __FUNCTION__, '3.9' );
457         return $current_site;
458 }