You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

559 lines
19 KiB

  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 wpdb $wpdb WordPress database abstraction object.
  242. * @global WP_Network $current_site The current network.
  243. * @global WP_Site $current_blog The current site.
  244. *
  245. * @param string $domain The requested domain.
  246. * @param string $path The requested path.
  247. * @param bool $subdomain Optional. Whether a subdomain (true) or subdirectory (false) configuration.
  248. * Default false.
  249. * @return bool|string True if bootstrap successfully populated `$current_blog` and `$current_site`.
  250. * False if bootstrap could not be properly completed.
  251. * Redirect URL if parts exist, but the request as a whole can not be fulfilled.
  252. */
  253. function ms_load_current_site_and_network( $domain, $path, $subdomain = false ) {
  254. global $wpdb, $current_site, $current_blog;
  255. // If the network is defined in wp-config.php, we can simply use that.
  256. if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
  257. $current_site = new stdClass;
  258. $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
  259. $current_site->domain = DOMAIN_CURRENT_SITE;
  260. $current_site->path = PATH_CURRENT_SITE;
  261. if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
  262. $current_site->blog_id = BLOG_ID_CURRENT_SITE;
  263. } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
  264. $current_site->blog_id = BLOGID_CURRENT_SITE;
  265. }
  266. if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
  267. $current_blog = get_site_by_path( $domain, $path );
  268. } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
  269. // If the current network has a path and also matches the domain and path of the request,
  270. // we need to look for a site using the first path segment following the network's path.
  271. $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
  272. } else {
  273. // Otherwise, use the first path segment (as usual).
  274. $current_blog = get_site_by_path( $domain, $path, 1 );
  275. }
  276. } elseif ( ! $subdomain ) {
  277. /*
  278. * A "subdomain" install can be re-interpreted to mean "can support any domain".
  279. * If we're not dealing with one of these installs, then the important part is determining
  280. * the network first, because we need the network's path to identify any sites.
  281. */
  282. if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
  283. // Are there even two networks installed?
  284. $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
  285. if ( 1 === $wpdb->num_rows ) {
  286. $current_site = new WP_Network( $one_network );
  287. wp_cache_add( 'current_network', $current_site, 'site-options' );
  288. } elseif ( 0 === $wpdb->num_rows ) {
  289. // A network not found hook should fire here.
  290. return false;
  291. }
  292. }
  293. if ( empty( $current_site ) ) {
  294. $current_site = WP_Network::get_by_path( $domain, $path, 1 );
  295. }
  296. if ( empty( $current_site ) ) {
  297. /**
  298. * Fires when a network cannot be found based on the requested domain and path.
  299. *
  300. * At the time of this action, the only recourse is to redirect somewhere
  301. * and exit. If you want to declare a particular network, do so earlier.
  302. *
  303. * @since 4.4.0
  304. *
  305. * @param string $domain The domain used to search for a network.
  306. * @param string $path The path used to search for a path.
  307. */
  308. do_action( 'ms_network_not_found', $domain, $path );
  309. return false;
  310. } elseif ( $path === $current_site->path ) {
  311. $current_blog = get_site_by_path( $domain, $path );
  312. } else {
  313. // Search the network path + one more path segment (on top of the network path).
  314. $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
  315. }
  316. } else {
  317. // Find the site by the domain and at most the first path segment.
  318. $current_blog = get_site_by_path( $domain, $path, 1 );
  319. if ( $current_blog ) {
  320. $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
  321. } else {
  322. // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
  323. $current_site = WP_Network::get_by_path( $domain, $path, 1 );
  324. }
  325. }
  326. // The network declared by the site trumps any constants.
  327. if ( $current_blog && $current_blog->site_id != $current_site->id ) {
  328. $current_site = WP_Network::get_instance( $current_blog->site_id );
  329. }
  330. // No network has been found, bail.
  331. if ( empty( $current_site ) ) {
  332. /** This action is documented in wp-includes/ms-settings.php */
  333. do_action( 'ms_network_not_found', $domain, $path );
  334. return false;
  335. }
  336. // During activation of a new subdomain, the requested site does not yet exist.
  337. if ( empty( $current_blog ) && wp_installing() ) {
  338. $current_blog = new stdClass;
  339. $current_blog->blog_id = $blog_id = 1;
  340. $current_blog->public = 1;
  341. }
  342. // No site has been found, bail.
  343. if ( empty( $current_blog ) ) {
  344. // We're going to redirect to the network URL, with some possible modifications.
  345. $scheme = is_ssl() ? 'https' : 'http';
  346. $destination = "$scheme://{$current_site->domain}{$current_site->path}";
  347. /**
  348. * Fires when a network can be determined but a site cannot.
  349. *
  350. * At the time of this action, the only recourse is to redirect somewhere
  351. * and exit. If you want to declare a particular site, do so earlier.
  352. *
  353. * @since 3.9.0
  354. *
  355. * @param object $current_site The network that had been determined.
  356. * @param string $domain The domain used to search for a site.
  357. * @param string $path The path used to search for a site.
  358. */
  359. do_action( 'ms_site_not_found', $current_site, $domain, $path );
  360. if ( $subdomain && ! defined( 'NOBLOGREDIRECT' ) ) {
  361. // For a "subdomain" install, redirect to the signup form specifically.
  362. $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
  363. } elseif ( $subdomain ) {
  364. // For a "subdomain" install, the NOBLOGREDIRECT constant
  365. // can be used to avoid a redirect to the signup form.
  366. // Using the ms_site_not_found action is preferred to the constant.
  367. if ( '%siteurl%' !== NOBLOGREDIRECT ) {
  368. $destination = NOBLOGREDIRECT;
  369. }
  370. } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
  371. /*
  372. * If the domain we were searching for matches the network's domain,
  373. * it's no use redirecting back to ourselves -- it'll cause a loop.
  374. * As we couldn't find a site, we're simply not installed.
  375. */
  376. return false;
  377. }
  378. return $destination;
  379. }
  380. // Figure out the current network's main site.
  381. if ( empty( $current_site->blog_id ) ) {
  382. if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
  383. $current_site->blog_id = $current_blog->blog_id;
  384. } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
  385. $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
  386. $current_site->domain, $current_site->path ) );
  387. wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
  388. }
  389. }
  390. return true;
  391. }
  392. /**
  393. * Displays a failure message.
  394. *
  395. * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
  396. *
  397. * @access private
  398. * @since 3.0.0
  399. * @since 4.4.0 The `$domain` and `$path` parameters were added.
  400. *
  401. * @global wpdb $wpdb WordPress database abstraction object.
  402. *
  403. * @param string $domain The requested domain for the error to reference.
  404. * @param string $path The requested path for the error to reference.
  405. */
  406. function ms_not_installed( $domain, $path ) {
  407. global $wpdb;
  408. if ( ! is_admin() ) {
  409. dead_db();
  410. }
  411. wp_load_translations_early();
  412. $title = __( 'Error establishing a database connection' );
  413. $msg = '<h1>' . $title . '</h1>';
  414. $msg .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
  415. $msg .= ' ' . __( 'If you are the owner of this network please check that MySQL is running properly and all tables are error free.' ) . '</p>';
  416. $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $wpdb->site ) );
  417. if ( ! $wpdb->get_var( $query ) ) {
  418. $msg .= '<p>' . sprintf(
  419. /* translators: %s: table name */
  420. __( '<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.' ),
  421. '<code>' . $wpdb->site . '</code>'
  422. ) . '</p>';
  423. } else {
  424. $msg .= '<p>' . sprintf(
  425. /* translators: 1: site url, 2: table name, 3: database name */
  426. __( '<strong>Could not find site %1$s.</strong> Searched for table %2$s in database %3$s. Is that right?' ),
  427. '<code>' . rtrim( $domain . $path, '/' ) . '</code>',
  428. '<code>' . $wpdb->blogs . '</code>',
  429. '<code>' . DB_NAME . '</code>'
  430. ) . '</p>';
  431. }
  432. $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
  433. /* translators: %s: Codex URL */
  434. $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.' ),
  435. __( 'https://codex.wordpress.org/Debugging_a_WordPress_Network' )
  436. );
  437. $msg .= ' ' . __( 'If you&#8217;re still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
  438. foreach ( $wpdb->tables('global') as $t => $table ) {
  439. if ( 'sitecategories' == $t )
  440. continue;
  441. $msg .= '<li>' . $table . '</li>';
  442. }
  443. $msg .= '</ul>';
  444. wp_die( $msg, $title, array( 'response' => 500 ) );
  445. }
  446. /**
  447. * This deprecated function formerly set the site_name property of the $current_site object.
  448. *
  449. * This function simply returns the object, as before.
  450. * The bootstrap takes care of setting site_name.
  451. *
  452. * @access private
  453. * @since 3.0.0
  454. * @deprecated 3.9.0 Use get_current_site() instead.
  455. *
  456. * @param object $current_site
  457. * @return object
  458. */
  459. function get_current_site_name( $current_site ) {
  460. _deprecated_function( __FUNCTION__, '3.9.0', 'get_current_site()' );
  461. return $current_site;
  462. }
  463. /**
  464. * This deprecated function managed much of the site and network loading in multisite.
  465. *
  466. * The current bootstrap code is now responsible for parsing the site and network load as
  467. * well as setting the global $current_site object.
  468. *
  469. * @access private
  470. * @since 3.0.0
  471. * @deprecated 3.9.0
  472. *
  473. * @global object $current_site
  474. *
  475. * @return object
  476. */
  477. function wpmu_current_site() {
  478. global $current_site;
  479. _deprecated_function( __FUNCTION__, '3.9.0' );
  480. return $current_site;
  481. }
  482. /**
  483. * Retrieve an object containing information about the requested network.
  484. *
  485. * @since 3.9.0
  486. * @deprecated 4.7.0 Use `get_network()`
  487. * @see get_network()
  488. *
  489. * @internal In 4.6.0, converted to use get_network()
  490. *
  491. * @param object|int $network The network's database row or ID.
  492. * @return WP_Network|false Object containing network information if found, false if not.
  493. */
  494. function wp_get_network( $network ) {
  495. _deprecated_function( __FUNCTION__, '4.7.0', 'get_network()' );
  496. $network = get_network( $network );
  497. if ( null === $network ) {
  498. return false;
  499. }
  500. return $network;
  501. }