network.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <?php
  2. /**
  3. * WordPress Network Administration API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Check for an existing network.
  11. *
  12. * @since 3.0.0
  13. *
  14. * @global wpdb $wpdb WordPress database abstraction object.
  15. *
  16. * @return Whether a network exists.
  17. */
  18. function network_domain_check() {
  19. global $wpdb;
  20. $sql = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $wpdb->site ) );
  21. if ( $wpdb->get_var( $sql ) ) {
  22. return $wpdb->get_var( "SELECT domain FROM $wpdb->site ORDER BY id ASC LIMIT 1" );
  23. }
  24. return false;
  25. }
  26. /**
  27. * Allow subdomain installation
  28. *
  29. * @since 3.0.0
  30. * @return bool Whether subdomain installation is allowed
  31. */
  32. function allow_subdomain_install() {
  33. $domain = preg_replace( '|https?://([^/]+)|', '$1', get_option( 'home' ) );
  34. if ( parse_url( get_option( 'home' ), PHP_URL_PATH ) || 'localhost' == $domain || preg_match( '|^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$|', $domain ) )
  35. return false;
  36. return true;
  37. }
  38. /**
  39. * Allow subdirectory installation.
  40. *
  41. * @since 3.0.0
  42. *
  43. * @global wpdb $wpdb WordPress database abstraction object.
  44. *
  45. * @return bool Whether subdirectory installation is allowed
  46. */
  47. function allow_subdirectory_install() {
  48. global $wpdb;
  49. /**
  50. * Filters whether to enable the subdirectory installation feature in Multisite.
  51. *
  52. * @since 3.0.0
  53. *
  54. * @param bool $allow Whether to enable the subdirectory installation feature in Multisite. Default is false.
  55. */
  56. if ( apply_filters( 'allow_subdirectory_install', false ) )
  57. return true;
  58. if ( defined( 'ALLOW_SUBDIRECTORY_INSTALL' ) && ALLOW_SUBDIRECTORY_INSTALL )
  59. return true;
  60. $post = $wpdb->get_row( "SELECT ID FROM $wpdb->posts WHERE post_date < DATE_SUB(NOW(), INTERVAL 1 MONTH) AND post_status = 'publish'" );
  61. if ( empty( $post ) )
  62. return true;
  63. return false;
  64. }
  65. /**
  66. * Get base domain of network.
  67. *
  68. * @since 3.0.0
  69. * @return string Base domain.
  70. */
  71. function get_clean_basedomain() {
  72. if ( $existing_domain = network_domain_check() )
  73. return $existing_domain;
  74. $domain = preg_replace( '|https?://|', '', get_option( 'siteurl' ) );
  75. if ( $slash = strpos( $domain, '/' ) )
  76. $domain = substr( $domain, 0, $slash );
  77. return $domain;
  78. }
  79. /**
  80. * Prints step 1 for Network installation process.
  81. *
  82. * @todo Realistically, step 1 should be a welcome screen explaining what a Network is and such. Navigating to Tools > Network
  83. * should not be a sudden "Welcome to a new install process! Fill this out and click here." See also contextual help todo.
  84. *
  85. * @since 3.0.0
  86. *
  87. * @global bool $is_apache
  88. *
  89. * @param WP_Error $errors
  90. */
  91. function network_step1( $errors = false ) {
  92. global $is_apache;
  93. if ( defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) {
  94. echo '<div class="error"><p><strong>' . __( 'ERROR:' ) . '</strong> ' . sprintf(
  95. /* translators: %s: DO_NOT_UPGRADE_GLOBAL_TABLES */
  96. __( 'The constant %s cannot be defined when creating a network.' ),
  97. '<code>DO_NOT_UPGRADE_GLOBAL_TABLES</code>'
  98. ) . '</p></div>';
  99. echo '</div>';
  100. include( ABSPATH . 'wp-admin/admin-footer.php' );
  101. die();
  102. }
  103. $active_plugins = get_option( 'active_plugins' );
  104. if ( ! empty( $active_plugins ) ) {
  105. echo '<div class="updated"><p><strong>' . __( 'Warning:' ) . '</strong> ' . sprintf(
  106. /* translators: %s: Plugins screen URL */
  107. __( 'Please <a href="%s">deactivate your plugins</a> before enabling the Network feature.' ),
  108. admin_url( 'plugins.php?plugin_status=active' )
  109. ) . '</p></div>';
  110. echo '<p>' . __( 'Once the network is created, you may reactivate your plugins.' ) . '</p>';
  111. echo '</div>';
  112. include( ABSPATH . 'wp-admin/admin-footer.php' );
  113. die();
  114. }
  115. $hostname = get_clean_basedomain();
  116. $has_ports = strstr( $hostname, ':' );
  117. if ( ( false !== $has_ports && ! in_array( $has_ports, array( ':80', ':443' ) ) ) ) {
  118. echo '<div class="error"><p><strong>' . __( 'ERROR:' ) . '</strong> ' . __( 'You cannot install a network of sites with your server address.' ) . '</p></div>';
  119. echo '<p>' . sprintf(
  120. /* translators: %s: port number */
  121. __( 'You cannot use port numbers such as %s.' ),
  122. '<code>' . $has_ports . '</code>'
  123. ) . '</p>';
  124. echo '<a href="' . esc_url( admin_url() ) . '">' . __( 'Return to Dashboard' ) . '</a>';
  125. echo '</div>';
  126. include( ABSPATH . 'wp-admin/admin-footer.php' );
  127. die();
  128. }
  129. echo '<form method="post">';
  130. wp_nonce_field( 'install-network-1' );
  131. $error_codes = array();
  132. if ( is_wp_error( $errors ) ) {
  133. echo '<div class="error"><p><strong>' . __( 'ERROR: The network could not be created.' ) . '</strong></p>';
  134. foreach ( $errors->get_error_messages() as $error )
  135. echo "<p>$error</p>";
  136. echo '</div>';
  137. $error_codes = $errors->get_error_codes();
  138. }
  139. if ( ! empty( $_POST['sitename'] ) && ! in_array( 'empty_sitename', $error_codes ) ) {
  140. $site_name = $_POST['sitename'];
  141. } else {
  142. /* translators: %s: Default network name */
  143. $site_name = sprintf( __( '%s Sites' ), get_option( 'blogname' ) );
  144. }
  145. if ( ! empty( $_POST['email'] ) && ! in_array( 'invalid_email', $error_codes ) ) {
  146. $admin_email = $_POST['email'];
  147. } else {
  148. $admin_email = get_option( 'admin_email' );
  149. }
  150. ?>
  151. <p><?php _e( 'Welcome to the Network installation process!' ); ?></p>
  152. <p><?php _e( 'Fill in the information below and you&#8217;ll be on your way to creating a network of WordPress sites. We will create configuration files in the next step.' ); ?></p>
  153. <?php
  154. if ( isset( $_POST['subdomain_install'] ) ) {
  155. $subdomain_install = (bool) $_POST['subdomain_install'];
  156. } elseif ( apache_mod_loaded('mod_rewrite') ) { // assume nothing
  157. $subdomain_install = true;
  158. } elseif ( !allow_subdirectory_install() ) {
  159. $subdomain_install = true;
  160. } else {
  161. $subdomain_install = false;
  162. if ( $got_mod_rewrite = got_mod_rewrite() ) { // dangerous assumptions
  163. echo '<div class="updated inline"><p><strong>' . __( 'Note:' ) . '</strong> ';
  164. /* translators: %s: mod_rewrite */
  165. printf( __( 'Please make sure the Apache %s module is installed as it will be used at the end of this installation.' ),
  166. '<code>mod_rewrite</code>'
  167. );
  168. echo '</p>';
  169. } elseif ( $is_apache ) {
  170. echo '<div class="error inline"><p><strong>' . __( 'Warning:' ) . '</strong> ';
  171. /* translators: %s: mod_rewrite */
  172. printf( __( 'It looks like the Apache %s module is not installed.' ),
  173. '<code>mod_rewrite</code>'
  174. );
  175. echo '</p>';
  176. }
  177. if ( $got_mod_rewrite || $is_apache ) { // Protect against mod_rewrite mimicry (but ! Apache)
  178. echo '<p>';
  179. /* translators: 1: mod_rewrite, 2: mod_rewrite documentation URL, 3: Google search for mod_rewrite */
  180. printf( __( 'If %1$s is disabled, ask your administrator to enable that module, or look at the <a href="%2$s">Apache documentation</a> or <a href="%3$s">elsewhere</a> for help setting it up.' ),
  181. '<code>mod_rewrite</code>',
  182. 'https://httpd.apache.org/docs/mod/mod_rewrite.html',
  183. 'https://www.google.com/search?q=apache+mod_rewrite'
  184. );
  185. echo '</p></div>';
  186. }
  187. }
  188. if ( allow_subdomain_install() && allow_subdirectory_install() ) : ?>
  189. <h3><?php esc_html_e( 'Addresses of Sites in your Network' ); ?></h3>
  190. <p><?php _e( 'Please choose whether you would like sites in your WordPress network to use sub-domains or sub-directories.' ); ?>
  191. <strong><?php _e( 'You cannot change this later.' ); ?></strong></p>
  192. <p><?php _e( 'You will need a wildcard DNS record if you are going to use the virtual host (sub-domain) functionality.' ); ?></p>
  193. <?php // @todo: Link to an MS readme? ?>
  194. <table class="form-table">
  195. <tr>
  196. <th><label><input type="radio" name="subdomain_install" value="1"<?php checked( $subdomain_install ); ?> /> <?php _e( 'Sub-domains' ); ?></label></th>
  197. <td><?php printf(
  198. /* translators: 1: hostname */
  199. _x( 'like <code>site1.%1$s</code> and <code>site2.%1$s</code>', 'subdomain examples' ),
  200. $hostname
  201. ); ?></td>
  202. </tr>
  203. <tr>
  204. <th><label><input type="radio" name="subdomain_install" value="0"<?php checked( ! $subdomain_install ); ?> /> <?php _e( 'Sub-directories' ); ?></label></th>
  205. <td><?php printf(
  206. /* translators: 1: hostname */
  207. _x( 'like <code>%1$s/site1</code> and <code>%1$s/site2</code>', 'subdirectory examples' ),
  208. $hostname
  209. ); ?></td>
  210. </tr>
  211. </table>
  212. <?php
  213. endif;
  214. if ( WP_CONTENT_DIR != ABSPATH . 'wp-content' && ( allow_subdirectory_install() || ! allow_subdomain_install() ) )
  215. echo '<div class="error inline"><p><strong>' . __( 'Warning:' ) . '</strong> ' . __( 'Subdirectory networks may not be fully compatible with custom wp-content directories.' ) . '</p></div>';
  216. $is_www = ( 0 === strpos( $hostname, 'www.' ) );
  217. if ( $is_www ) :
  218. ?>
  219. <h3><?php esc_html_e( 'Server Address' ); ?></h3>
  220. <p><?php printf(
  221. /* translators: 1: site url 2: host name 3. www */
  222. __( 'We recommend you change your siteurl to %1$s before enabling the network feature. It will still be possible to visit your site using the %3$s prefix with an address like %2$s but any links will not have the %3$s prefix.' ),
  223. '<code>' . substr( $hostname, 4 ) . '</code>',
  224. '<code>' . $hostname . '</code>',
  225. '<code>www</code>'
  226. ); ?></p>
  227. <table class="form-table">
  228. <tr>
  229. <th scope='row'><?php esc_html_e( 'Server Address' ); ?></th>
  230. <td>
  231. <?php printf(
  232. /* translators: %s: host name */
  233. __( 'The internet address of your network will be %s.' ),
  234. '<code>' . $hostname . '</code>'
  235. ); ?>
  236. </td>
  237. </tr>
  238. </table>
  239. <?php endif; ?>
  240. <h3><?php esc_html_e( 'Network Details' ); ?></h3>
  241. <table class="form-table">
  242. <?php if ( 'localhost' == $hostname ) : ?>
  243. <tr>
  244. <th scope="row"><?php esc_html_e( 'Sub-directory Installation' ); ?></th>
  245. <td><?php
  246. printf(
  247. /* translators: 1: localhost 2: localhost.localdomain */
  248. __( 'Because you are using %1$s, the sites in your WordPress network must use sub-directories. Consider using %2$s if you wish to use sub-domains.' ),
  249. '<code>localhost</code>',
  250. '<code>localhost.localdomain</code>'
  251. );
  252. // Uh oh:
  253. if ( !allow_subdirectory_install() )
  254. echo ' <strong>' . __( 'Warning:' ) . ' ' . __( 'The main site in a sub-directory installation will need to use a modified permalink structure, potentially breaking existing links.' ) . '</strong>';
  255. ?></td>
  256. </tr>
  257. <?php elseif ( !allow_subdomain_install() ) : ?>
  258. <tr>
  259. <th scope="row"><?php esc_html_e( 'Sub-directory Installation' ); ?></th>
  260. <td><?php
  261. _e( 'Because your installation is in a directory, the sites in your WordPress network must use sub-directories.' );
  262. // Uh oh:
  263. if ( !allow_subdirectory_install() )
  264. echo ' <strong>' . __( 'Warning:' ) . ' ' . __( 'The main site in a sub-directory installation will need to use a modified permalink structure, potentially breaking existing links.' ) . '</strong>';
  265. ?></td>
  266. </tr>
  267. <?php elseif ( !allow_subdirectory_install() ) : ?>
  268. <tr>
  269. <th scope="row"><?php esc_html_e( 'Sub-domain Installation' ); ?></th>
  270. <td><?php _e( 'Because your installation is not new, the sites in your WordPress network must use sub-domains.' );
  271. echo ' <strong>' . __( 'The main site in a sub-directory installation will need to use a modified permalink structure, potentially breaking existing links.' ) . '</strong>';
  272. ?></td>
  273. </tr>
  274. <?php endif; ?>
  275. <?php if ( ! $is_www ) : ?>
  276. <tr>
  277. <th scope='row'><?php esc_html_e( 'Server Address' ); ?></th>
  278. <td>
  279. <?php printf(
  280. /* translators: %s: host name */
  281. __( 'The internet address of your network will be %s.' ),
  282. '<code>' . $hostname . '</code>'
  283. ); ?>
  284. </td>
  285. </tr>
  286. <?php endif; ?>
  287. <tr>
  288. <th scope='row'><?php esc_html_e( 'Network Title' ); ?></th>
  289. <td>
  290. <input name='sitename' type='text' size='45' value='<?php echo esc_attr( $site_name ); ?>' />
  291. <p class="description">
  292. <?php _e( 'What would you like to call your network?' ); ?>
  293. </p>
  294. </td>
  295. </tr>
  296. <tr>
  297. <th scope='row'><?php esc_html_e( 'Network Admin Email' ); ?></th>
  298. <td>
  299. <input name='email' type='text' size='45' value='<?php echo esc_attr( $admin_email ); ?>' />
  300. <p class="description">
  301. <?php _e( 'Your email address.' ); ?>
  302. </p>
  303. </td>
  304. </tr>
  305. </table>
  306. <?php submit_button( __( 'Install' ), 'primary', 'submit' ); ?>
  307. </form>
  308. <?php
  309. }
  310. /**
  311. * Prints step 2 for Network installation process.
  312. *
  313. * @since 3.0.0
  314. *
  315. * @global wpdb $wpdb WordPress database abstraction object.
  316. *
  317. * @param WP_Error $errors
  318. */
  319. function network_step2( $errors = false ) {
  320. global $wpdb;
  321. $hostname = get_clean_basedomain();
  322. $slashed_home = trailingslashit( get_option( 'home' ) );
  323. $base = parse_url( $slashed_home, PHP_URL_PATH );
  324. $document_root_fix = str_replace( '\\', '/', realpath( $_SERVER['DOCUMENT_ROOT'] ) );
  325. $abspath_fix = str_replace( '\\', '/', ABSPATH );
  326. $home_path = 0 === strpos( $abspath_fix, $document_root_fix ) ? $document_root_fix . $base : get_home_path();
  327. $wp_siteurl_subdir = preg_replace( '#^' . preg_quote( $home_path, '#' ) . '#', '', $abspath_fix );
  328. $rewrite_base = ! empty( $wp_siteurl_subdir ) ? ltrim( trailingslashit( $wp_siteurl_subdir ), '/' ) : '';
  329. $location_of_wp_config = $abspath_fix;
  330. if ( ! file_exists( ABSPATH . 'wp-config.php' ) && file_exists( dirname( ABSPATH ) . '/wp-config.php' ) ) {
  331. $location_of_wp_config = dirname( $abspath_fix );
  332. }
  333. $location_of_wp_config = trailingslashit( $location_of_wp_config );
  334. // Wildcard DNS message.
  335. if ( is_wp_error( $errors ) )
  336. echo '<div class="error">' . $errors->get_error_message() . '</div>';
  337. if ( $_POST ) {
  338. if ( allow_subdomain_install() )
  339. $subdomain_install = allow_subdirectory_install() ? ! empty( $_POST['subdomain_install'] ) : true;
  340. else
  341. $subdomain_install = false;
  342. } else {
  343. if ( is_multisite() ) {
  344. $subdomain_install = is_subdomain_install();
  345. ?>
  346. <p><?php _e( 'The original configuration steps are shown here for reference.' ); ?></p>
  347. <?php
  348. } else {
  349. $subdomain_install = (bool) $wpdb->get_var( "SELECT meta_value FROM $wpdb->sitemeta WHERE site_id = 1 AND meta_key = 'subdomain_install'" );
  350. ?>
  351. <div class="error"><p><strong><?php _e( 'Warning:' ); ?></strong> <?php _e( 'An existing WordPress network was detected.' ); ?></p></div>
  352. <p><?php _e( 'Please complete the configuration steps. To create a new network, you will need to empty or remove the network database tables.' ); ?></p>
  353. <?php
  354. }
  355. }
  356. $subdir_match = $subdomain_install ? '' : '([_0-9a-zA-Z-]+/)?';
  357. $subdir_replacement_01 = $subdomain_install ? '' : '$1';
  358. $subdir_replacement_12 = $subdomain_install ? '$1' : '$2';
  359. if ( $_POST || ! is_multisite() ) {
  360. ?>
  361. <h3><?php esc_html_e( 'Enabling the Network' ); ?></h3>
  362. <p><?php _e( 'Complete the following steps to enable the features for creating a network of sites.' ); ?></p>
  363. <div class="updated inline"><p><?php
  364. if ( file_exists( $home_path . '.htaccess' ) ) {
  365. echo '<strong>' . __( 'Caution:' ) . '</strong> ';
  366. printf(
  367. /* translators: 1: wp-config.php 2: .htaccess */
  368. __( 'We recommend you back up your existing %1$s and %2$s files.' ),
  369. '<code>wp-config.php</code>',
  370. '<code>.htaccess</code>'
  371. );
  372. } elseif ( file_exists( $home_path . 'web.config' ) ) {
  373. echo '<strong>' . __( 'Caution:' ) . '</strong> ';
  374. printf(
  375. /* translators: 1: wp-config.php 2: web.config */
  376. __( 'We recommend you back up your existing %1$s and %2$s files.' ),
  377. '<code>wp-config.php</code>',
  378. '<code>web.config</code>'
  379. );
  380. } else {
  381. echo '<strong>' . __( 'Caution:' ) . '</strong> ';
  382. printf(
  383. /* translators: 1: wp-config.php */
  384. __( 'We recommend you back up your existing %s file.' ),
  385. '<code>wp-config.php</code>'
  386. );
  387. }
  388. ?></p></div>
  389. <?php
  390. }
  391. ?>
  392. <ol>
  393. <li><p><?php printf(
  394. /* translators: 1: wp-config.php 2: location of wp-config file, 3: translated version of "That's all, stop editing! Happy blogging." */
  395. __( 'Add the following to your %1$s file in %2$s <strong>above</strong> the line reading %3$s:' ),
  396. '<code>wp-config.php</code>',
  397. '<code>' . $location_of_wp_config . '</code>',
  398. /*
  399. * translators: This string should only be translated if wp-config-sample.php is localized.
  400. * You can check the localized release package or
  401. * https://i18n.svn.wordpress.org/<locale code>/branches/<wp version>/dist/wp-config-sample.php
  402. */
  403. '<code>/* ' . __( 'That&#8217;s all, stop editing! Happy blogging.' ) . ' */</code>'
  404. ); ?></p>
  405. <textarea class="code" readonly="readonly" cols="100" rows="7">
  406. define('MULTISITE', true);
  407. define('SUBDOMAIN_INSTALL', <?php echo $subdomain_install ? 'true' : 'false'; ?>);
  408. define('DOMAIN_CURRENT_SITE', '<?php echo $hostname; ?>');
  409. define('PATH_CURRENT_SITE', '<?php echo $base; ?>');
  410. define('SITE_ID_CURRENT_SITE', 1);
  411. define('BLOG_ID_CURRENT_SITE', 1);
  412. </textarea>
  413. <?php
  414. $keys_salts = array( 'AUTH_KEY' => '', 'SECURE_AUTH_KEY' => '', 'LOGGED_IN_KEY' => '', 'NONCE_KEY' => '', 'AUTH_SALT' => '', 'SECURE_AUTH_SALT' => '', 'LOGGED_IN_SALT' => '', 'NONCE_SALT' => '' );
  415. foreach ( $keys_salts as $c => $v ) {
  416. if ( defined( $c ) )
  417. unset( $keys_salts[ $c ] );
  418. }
  419. if ( ! empty( $keys_salts ) ) {
  420. $keys_salts_str = '';
  421. $from_api = wp_remote_get( 'https://api.wordpress.org/secret-key/1.1/salt/' );
  422. if ( is_wp_error( $from_api ) ) {
  423. foreach ( $keys_salts as $c => $v ) {
  424. $keys_salts_str .= "\ndefine( '$c', '" . wp_generate_password( 64, true, true ) . "' );";
  425. }
  426. } else {
  427. $from_api = explode( "\n", wp_remote_retrieve_body( $from_api ) );
  428. foreach ( $keys_salts as $c => $v ) {
  429. $keys_salts_str .= "\ndefine( '$c', '" . substr( array_shift( $from_api ), 28, 64 ) . "' );";
  430. }
  431. }
  432. $num_keys_salts = count( $keys_salts );
  433. ?>
  434. <p>
  435. <?php
  436. if ( 1 == $num_keys_salts ) {
  437. printf(
  438. /* translators: 1: wp-config.php */
  439. __( 'This unique authentication key is also missing from your %s file.' ),
  440. '<code>wp-config.php</code>'
  441. );
  442. } else {
  443. printf(
  444. /* translators: 1: wp-config.php */
  445. __( 'These unique authentication keys are also missing from your %s file.' ),
  446. '<code>wp-config.php</code>'
  447. );
  448. }
  449. ?>
  450. <?php _e( 'To make your installation more secure, you should also add:' ); ?>
  451. </p>
  452. <textarea class="code" readonly="readonly" cols="100" rows="<?php echo $num_keys_salts; ?>"><?php echo esc_textarea( $keys_salts_str ); ?></textarea>
  453. <?php
  454. }
  455. ?>
  456. </li>
  457. <?php
  458. if ( iis7_supports_permalinks() ) :
  459. // IIS doesn't support RewriteBase, all your RewriteBase are belong to us
  460. $iis_subdir_match = ltrim( $base, '/' ) . $subdir_match;
  461. $iis_rewrite_base = ltrim( $base, '/' ) . $rewrite_base;
  462. $iis_subdir_replacement = $subdomain_install ? '' : '{R:1}';
  463. $web_config_file = '<?xml version="1.0" encoding="UTF-8"?>
  464. <configuration>
  465. <system.webServer>
  466. <rewrite>
  467. <rules>
  468. <rule name="WordPress Rule 1" stopProcessing="true">
  469. <match url="^index\.php$" ignoreCase="false" />
  470. <action type="None" />
  471. </rule>';
  472. if ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) {
  473. $web_config_file .= '
  474. <rule name="WordPress Rule for Files" stopProcessing="true">
  475. <match url="^' . $iis_subdir_match . 'files/(.+)" ignoreCase="false" />
  476. <action type="Rewrite" url="' . $iis_rewrite_base . WPINC . '/ms-files.php?file={R:1}" appendQueryString="false" />
  477. </rule>';
  478. }
  479. $web_config_file .= '
  480. <rule name="WordPress Rule 2" stopProcessing="true">
  481. <match url="^' . $iis_subdir_match . 'wp-admin$" ignoreCase="false" />
  482. <action type="Redirect" url="' . $iis_subdir_replacement . 'wp-admin/" redirectType="Permanent" />
  483. </rule>
  484. <rule name="WordPress Rule 3" stopProcessing="true">
  485. <match url="^" ignoreCase="false" />
  486. <conditions logicalGrouping="MatchAny">
  487. <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
  488. <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
  489. </conditions>
  490. <action type="None" />
  491. </rule>
  492. <rule name="WordPress Rule 4" stopProcessing="true">
  493. <match url="^' . $iis_subdir_match . '(wp-(content|admin|includes).*)" ignoreCase="false" />
  494. <action type="Rewrite" url="' . $iis_rewrite_base . '{R:1}" />
  495. </rule>
  496. <rule name="WordPress Rule 5" stopProcessing="true">
  497. <match url="^' . $iis_subdir_match . '([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
  498. <action type="Rewrite" url="' . $iis_rewrite_base . '{R:2}" />
  499. </rule>
  500. <rule name="WordPress Rule 6" stopProcessing="true">
  501. <match url="." ignoreCase="false" />
  502. <action type="Rewrite" url="index.php" />
  503. </rule>
  504. </rules>
  505. </rewrite>
  506. </system.webServer>
  507. </configuration>
  508. ';
  509. echo '<li><p>';
  510. printf(
  511. /* translators: 1: a filename like .htaccess. 2: a file path. */
  512. __( 'Add the following to your %1$s file in %2$s, <strong>replacing</strong> other WordPress rules:' ),
  513. '<code>web.config</code>',
  514. '<code>' . $home_path . '</code>'
  515. );
  516. echo '</p>';
  517. if ( ! $subdomain_install && WP_CONTENT_DIR != ABSPATH . 'wp-content' )
  518. echo '<p><strong>' . __( 'Warning:' ) . ' ' . __( 'Subdirectory networks may not be fully compatible with custom wp-content directories.' ) . '</strong></p>';
  519. ?>
  520. <textarea class="code" readonly="readonly" cols="100" rows="20"><?php echo esc_textarea( $web_config_file ); ?>
  521. </textarea></li>
  522. </ol>
  523. <?php else : // end iis7_supports_permalinks(). construct an htaccess file instead:
  524. $ms_files_rewriting = '';
  525. if ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) {
  526. $ms_files_rewriting = "\n# uploaded files\nRewriteRule ^";
  527. $ms_files_rewriting .= $subdir_match . "files/(.+) {$rewrite_base}" . WPINC . "/ms-files.php?file={$subdir_replacement_12} [L]" . "\n";
  528. }
  529. $htaccess_file = <<<EOF
  530. RewriteEngine On
  531. RewriteBase {$base}
  532. RewriteRule ^index\.php$ - [L]
  533. {$ms_files_rewriting}
  534. # add a trailing slash to /wp-admin
  535. RewriteRule ^{$subdir_match}wp-admin$ {$subdir_replacement_01}wp-admin/ [R=301,L]
  536. RewriteCond %{REQUEST_FILENAME} -f [OR]
  537. RewriteCond %{REQUEST_FILENAME} -d
  538. RewriteRule ^ - [L]
  539. RewriteRule ^{$subdir_match}(wp-(content|admin|includes).*) {$rewrite_base}{$subdir_replacement_12} [L]
  540. RewriteRule ^{$subdir_match}(.*\.php)$ {$rewrite_base}$subdir_replacement_12 [L]
  541. RewriteRule . index.php [L]
  542. EOF;
  543. echo '<li><p>';
  544. printf(
  545. /* translators: 1: a filename like .htaccess. 2: a file path. */
  546. __( 'Add the following to your %1$s file in %2$s, <strong>replacing</strong> other WordPress rules:' ),
  547. '<code>.htaccess</code>',
  548. '<code>' . $home_path . '</code>'
  549. );
  550. echo '</p>';
  551. if ( ! $subdomain_install && WP_CONTENT_DIR != ABSPATH . 'wp-content' )
  552. echo '<p><strong>' . __( 'Warning:' ) . ' ' . __( 'Subdirectory networks may not be fully compatible with custom wp-content directories.' ) . '</strong></p>';
  553. ?>
  554. <textarea class="code" readonly="readonly" cols="100" rows="<?php echo substr_count( $htaccess_file, "\n" ) + 1; ?>">
  555. <?php echo esc_textarea( $htaccess_file ); ?></textarea></li>
  556. </ol>
  557. <?php endif; // end IIS/Apache code branches.
  558. if ( !is_multisite() ) { ?>
  559. <p><?php _e( 'Once you complete these steps, your network is enabled and configured. You will have to log in again.' ); ?> <a href="<?php echo esc_url( wp_login_url() ); ?>"><?php _e( 'Log In' ); ?></a></p>
  560. <?php
  561. }
  562. }