ms-load.php 19 KB

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