ms-load.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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' ) && 'yes' === VHOST );
  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 string[] Array of absolute paths to 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 $check 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 ( '1' == $blog->archived || '1' == $blog->spam ) {
  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. * Retrieves 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. * @param string $domain Domain to check.
  143. * @param string $path Path to check.
  144. * @param int|null $segments Path segments to use. Defaults to null, or the full path.
  145. * @return WP_Site|false Site object if successful. False when no site is found.
  146. */
  147. function get_site_by_path( $domain, $path, $segments = null ) {
  148. $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
  149. /**
  150. * Filters the number of path segments to consider when searching for a site.
  151. *
  152. * @since 3.9.0
  153. *
  154. * @param int|null $segments The number of path segments to consider. WordPress by default looks at
  155. * one path segment following the network path. The function default of
  156. * null only makes sense when you know the requested path should match a site.
  157. * @param string $domain The requested domain.
  158. * @param string $path The requested path, in full.
  159. */
  160. $segments = apply_filters( 'site_by_path_segments_count', $segments, $domain, $path );
  161. if ( null !== $segments && count( $path_segments ) > $segments ) {
  162. $path_segments = array_slice( $path_segments, 0, $segments );
  163. }
  164. $paths = array();
  165. while ( count( $path_segments ) ) {
  166. $paths[] = '/' . implode( '/', $path_segments ) . '/';
  167. array_pop( $path_segments );
  168. }
  169. $paths[] = '/';
  170. /**
  171. * Determines a site by its domain and path.
  172. *
  173. * This allows one to short-circuit the default logic, perhaps by
  174. * replacing it with a routine that is more optimal for your setup.
  175. *
  176. * Return null to avoid the short-circuit. Return false if no site
  177. * can be found at the requested domain and path. Otherwise, return
  178. * a site object.
  179. *
  180. * @since 3.9.0
  181. *
  182. * @param null|false|WP_Site $site Site value to return by path. Default null
  183. * to continue retrieving the site.
  184. * @param string $domain The requested domain.
  185. * @param string $path The requested path, in full.
  186. * @param int|null $segments The suggested number of paths to consult.
  187. * Default null, meaning the entire path was to be consulted.
  188. * @param string[] $paths The paths to search for, based on $path and $segments.
  189. */
  190. $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
  191. if ( null !== $pre ) {
  192. if ( false !== $pre && ! $pre instanceof WP_Site ) {
  193. $pre = new WP_Site( $pre );
  194. }
  195. return $pre;
  196. }
  197. /*
  198. * @todo
  199. * Caching, etc. Consider alternative optimization routes,
  200. * perhaps as an opt-in for plugins, rather than using the pre_* filter.
  201. * For example: The segments filter can expand or ignore paths.
  202. * If persistent caching is enabled, we could query the DB for a path <> '/'
  203. * then cache whether we can just always ignore paths.
  204. */
  205. // Either www or non-www is supported, not both. If a www domain is requested,
  206. // query for both to provide the proper redirect.
  207. $domains = array( $domain );
  208. if ( 'www.' === substr( $domain, 0, 4 ) ) {
  209. $domains[] = substr( $domain, 4 );
  210. }
  211. $args = array(
  212. 'number' => 1,
  213. 'update_site_meta_cache' => false,
  214. );
  215. if ( count( $domains ) > 1 ) {
  216. $args['domain__in'] = $domains;
  217. $args['orderby']['domain_length'] = 'DESC';
  218. } else {
  219. $args['domain'] = array_shift( $domains );
  220. }
  221. if ( count( $paths ) > 1 ) {
  222. $args['path__in'] = $paths;
  223. $args['orderby']['path_length'] = 'DESC';
  224. } else {
  225. $args['path'] = array_shift( $paths );
  226. }
  227. $result = get_sites( $args );
  228. $site = array_shift( $result );
  229. if ( $site ) {
  230. return $site;
  231. }
  232. return false;
  233. }
  234. /**
  235. * Identifies the network and site of a requested domain and path and populates the
  236. * corresponding network and site global objects as part of the multisite bootstrap process.
  237. *
  238. * Prior to 4.6.0, this was a procedural block in `ms-settings.php`. It was wrapped into
  239. * a function to facilitate unit tests. It should not be used outside of core.
  240. *
  241. * Usually, it's easier to query the site first, which then declares its network.
  242. * In limited situations, we either can or must find the network first.
  243. *
  244. * If a network and site are found, a `true` response will be returned so that the
  245. * request can continue.
  246. *
  247. * If neither a network or site is found, `false` or a URL string will be returned
  248. * so that either an error can be shown or a redirect can occur.
  249. *
  250. * @since 4.6.0
  251. * @access private
  252. *
  253. * @global WP_Network $current_site The current network.
  254. * @global WP_Site $current_blog The current site.
  255. *
  256. * @param string $domain The requested domain.
  257. * @param string $path The requested path.
  258. * @param bool $subdomain Optional. Whether a subdomain (true) or subdirectory (false) configuration.
  259. * Default false.
  260. * @return bool|string True if bootstrap successfully populated `$current_blog` and `$current_site`.
  261. * False if bootstrap could not be properly completed.
  262. * Redirect URL if parts exist, but the request as a whole can not be fulfilled.
  263. */
  264. function ms_load_current_site_and_network( $domain, $path, $subdomain = false ) {
  265. global $current_site, $current_blog;
  266. // If the network is defined in wp-config.php, we can simply use that.
  267. if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
  268. $current_site = new stdClass;
  269. $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
  270. $current_site->domain = DOMAIN_CURRENT_SITE;
  271. $current_site->path = PATH_CURRENT_SITE;
  272. if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
  273. $current_site->blog_id = BLOG_ID_CURRENT_SITE;
  274. } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // Deprecated.
  275. $current_site->blog_id = BLOGID_CURRENT_SITE;
  276. }
  277. if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
  278. $current_blog = get_site_by_path( $domain, $path );
  279. } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
  280. // If the current network has a path and also matches the domain and path of the request,
  281. // we need to look for a site using the first path segment following the network's path.
  282. $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
  283. } else {
  284. // Otherwise, use the first path segment (as usual).
  285. $current_blog = get_site_by_path( $domain, $path, 1 );
  286. }
  287. } elseif ( ! $subdomain ) {
  288. /*
  289. * A "subdomain" installation can be re-interpreted to mean "can support any domain".
  290. * If we're not dealing with one of these installations, then the important part is determining
  291. * the network first, because we need the network's path to identify any sites.
  292. */
  293. $current_site = wp_cache_get( 'current_network', 'site-options' );
  294. if ( ! $current_site ) {
  295. // Are there even two networks installed?
  296. $networks = get_networks( array( 'number' => 2 ) );
  297. if ( count( $networks ) === 1 ) {
  298. $current_site = array_shift( $networks );
  299. wp_cache_add( 'current_network', $current_site, 'site-options' );
  300. } elseif ( empty( $networks ) ) {
  301. // A network not found hook should fire here.
  302. return false;
  303. }
  304. }
  305. if ( empty( $current_site ) ) {
  306. $current_site = WP_Network::get_by_path( $domain, $path, 1 );
  307. }
  308. if ( empty( $current_site ) ) {
  309. /**
  310. * Fires when a network cannot be found based on the requested domain and path.
  311. *
  312. * At the time of this action, the only recourse is to redirect somewhere
  313. * and exit. If you want to declare a particular network, do so earlier.
  314. *
  315. * @since 4.4.0
  316. *
  317. * @param string $domain The domain used to search for a network.
  318. * @param string $path The path used to search for a path.
  319. */
  320. do_action( 'ms_network_not_found', $domain, $path );
  321. return false;
  322. } elseif ( $path === $current_site->path ) {
  323. $current_blog = get_site_by_path( $domain, $path );
  324. } else {
  325. // Search the network path + one more path segment (on top of the network path).
  326. $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
  327. }
  328. } else {
  329. // Find the site by the domain and at most the first path segment.
  330. $current_blog = get_site_by_path( $domain, $path, 1 );
  331. if ( $current_blog ) {
  332. $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
  333. } else {
  334. // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
  335. $current_site = WP_Network::get_by_path( $domain, $path, 1 );
  336. }
  337. }
  338. // The network declared by the site trumps any constants.
  339. if ( $current_blog && $current_blog->site_id != $current_site->id ) {
  340. $current_site = WP_Network::get_instance( $current_blog->site_id );
  341. }
  342. // No network has been found, bail.
  343. if ( empty( $current_site ) ) {
  344. /** This action is documented in wp-includes/ms-settings.php */
  345. do_action( 'ms_network_not_found', $domain, $path );
  346. return false;
  347. }
  348. // During activation of a new subdomain, the requested site does not yet exist.
  349. if ( empty( $current_blog ) && wp_installing() ) {
  350. $current_blog = new stdClass;
  351. $current_blog->blog_id = 1;
  352. $blog_id = 1;
  353. $current_blog->public = 1;
  354. }
  355. // No site has been found, bail.
  356. if ( empty( $current_blog ) ) {
  357. // We're going to redirect to the network URL, with some possible modifications.
  358. $scheme = is_ssl() ? 'https' : 'http';
  359. $destination = "$scheme://{$current_site->domain}{$current_site->path}";
  360. /**
  361. * Fires when a network can be determined but a site cannot.
  362. *
  363. * At the time of this action, the only recourse is to redirect somewhere
  364. * and exit. If you want to declare a particular site, do so earlier.
  365. *
  366. * @since 3.9.0
  367. *
  368. * @param WP_Network $current_site The network that had been determined.
  369. * @param string $domain The domain used to search for a site.
  370. * @param string $path The path used to search for a site.
  371. */
  372. do_action( 'ms_site_not_found', $current_site, $domain, $path );
  373. if ( $subdomain && ! defined( 'NOBLOGREDIRECT' ) ) {
  374. // For a "subdomain" installation, redirect to the signup form specifically.
  375. $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
  376. } elseif ( $subdomain ) {
  377. /*
  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. */
  382. if ( '%siteurl%' !== NOBLOGREDIRECT ) {
  383. $destination = NOBLOGREDIRECT;
  384. }
  385. } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
  386. /*
  387. * If the domain we were searching for matches the network's domain,
  388. * it's no use redirecting back to ourselves -- it'll cause a loop.
  389. * As we couldn't find a site, we're simply not installed.
  390. */
  391. return false;
  392. }
  393. return $destination;
  394. }
  395. // Figure out the current network's main site.
  396. if ( empty( $current_site->blog_id ) ) {
  397. $current_site->blog_id = get_main_site_id( $current_site->id );
  398. }
  399. return true;
  400. }
  401. /**
  402. * Displays a failure message.
  403. *
  404. * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
  405. *
  406. * @access private
  407. * @since 3.0.0
  408. * @since 4.4.0 The `$domain` and `$path` parameters were added.
  409. *
  410. * @global wpdb $wpdb WordPress database abstraction object.
  411. *
  412. * @param string $domain The requested domain for the error to reference.
  413. * @param string $path The requested path for the error to reference.
  414. */
  415. function ms_not_installed( $domain, $path ) {
  416. global $wpdb;
  417. if ( ! is_admin() ) {
  418. dead_db();
  419. }
  420. wp_load_translations_early();
  421. $title = __( 'Error establishing a database connection' );
  422. $msg = '<h1>' . $title . '</h1>';
  423. $msg .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
  424. $msg .= ' ' . __( 'If you are the owner of this network please check that your host&#8217;s database server is running properly and all tables are error free.' ) . '</p>';
  425. $query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->site ) );
  426. if ( ! $wpdb->get_var( $query ) ) {
  427. $msg .= '<p>' . sprintf(
  428. /* translators: %s: Table name. */
  429. __( '<strong>Database tables are missing.</strong> This means that your host&#8217;s database server is not running, WordPress was not installed properly, or someone deleted %s. You really should look at your database now.' ),
  430. '<code>' . $wpdb->site . '</code>'
  431. ) . '</p>';
  432. } else {
  433. $msg .= '<p>' . sprintf(
  434. /* translators: 1: Site URL, 2: Table name, 3: Database name. */
  435. __( '<strong>Could not find site %1$s.</strong> Searched for table %2$s in database %3$s. Is that right?' ),
  436. '<code>' . rtrim( $domain . $path, '/' ) . '</code>',
  437. '<code>' . $wpdb->blogs . '</code>',
  438. '<code>' . DB_NAME . '</code>'
  439. ) . '</p>';
  440. }
  441. $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
  442. $msg .= sprintf(
  443. /* translators: %s: Documentation URL. */
  444. __( 'Read the <a href="%s" target="_blank">Debugging a WordPress Network</a> article. Some of the suggestions there may help you figure out what went wrong.' ),
  445. __( 'https://wordpress.org/support/article/debugging-a-wordpress-network/' )
  446. );
  447. $msg .= ' ' . __( 'If you are still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
  448. foreach ( $wpdb->tables( 'global' ) as $t => $table ) {
  449. if ( 'sitecategories' === $t ) {
  450. continue;
  451. }
  452. $msg .= '<li>' . $table . '</li>';
  453. }
  454. $msg .= '</ul>';
  455. wp_die( $msg, $title, array( 'response' => 500 ) );
  456. }
  457. /**
  458. * This deprecated function formerly set the site_name property of the $current_site object.
  459. *
  460. * This function simply returns the object, as before.
  461. * The bootstrap takes care of setting site_name.
  462. *
  463. * @access private
  464. * @since 3.0.0
  465. * @deprecated 3.9.0 Use get_current_site() instead.
  466. *
  467. * @param WP_Network $current_site
  468. * @return WP_Network
  469. */
  470. function get_current_site_name( $current_site ) {
  471. _deprecated_function( __FUNCTION__, '3.9.0', 'get_current_site()' );
  472. return $current_site;
  473. }
  474. /**
  475. * This deprecated function managed much of the site and network loading in multisite.
  476. *
  477. * The current bootstrap code is now responsible for parsing the site and network load as
  478. * well as setting the global $current_site object.
  479. *
  480. * @access private
  481. * @since 3.0.0
  482. * @deprecated 3.9.0
  483. *
  484. * @global WP_Network $current_site
  485. *
  486. * @return WP_Network
  487. */
  488. function wpmu_current_site() {
  489. global $current_site;
  490. _deprecated_function( __FUNCTION__, '3.9.0' );
  491. return $current_site;
  492. }
  493. /**
  494. * Retrieves an object containing information about the requested network.
  495. *
  496. * @since 3.9.0
  497. * @deprecated 4.7.0 Use get_network()
  498. * @see get_network()
  499. *
  500. * @internal In 4.6.0, converted to use get_network()
  501. *
  502. * @param object|int $network The network's database row or ID.
  503. * @return WP_Network|false Object containing network information if found, false if not.
  504. */
  505. function wp_get_network( $network ) {
  506. _deprecated_function( __FUNCTION__, '4.7.0', 'get_network()' );
  507. $network = get_network( $network );
  508. if ( null === $network ) {
  509. return false;
  510. }
  511. return $network;
  512. }