ms-load.php 19 KB

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