First pass at error handling for populate_network() and network.php. see #11816

git-svn-id: http://svn.automattic.com/wordpress/trunk@13655 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin 2010-03-11 01:51:24 +00:00
parent 622a4084fc
commit 00c591591c
2 changed files with 79 additions and 51 deletions

View File

@ -619,25 +619,29 @@ function populate_roles_300() {
* @since 3.0 * @since 3.0
* *
* @param int $network_id id of network to populate * @param int $network_id id of network to populate
* @return bool|WP_Error True on success, or WP_Error on warning (with the install otherwise successful,
* so the error code must be checked) or failure.
*/ */
function populate_network( $network_id = 1, $domain = '', $email = '', $site_name = '', $path = '/', $subdomain_install = false ) { function populate_network( $network_id = 1, $domain = '', $email = '', $site_name = '', $path = '/', $subdomain_install = false ) {
global $wpdb, $current_site, $wp_db_version, $wp_rewrite; global $wpdb, $current_site, $wp_db_version, $wp_rewrite;
$msg = ''; $errors = new WP_Error();
//@todo: turn these checks into returned messages if ( '' == $domain )
if ( $domain == '' ) $errors->add( 'empty_domain', __( 'You must provide a domain name.' ) );
die( 'You must provide a domain name!' ); if ( '' == $site_name )
if ( $site_name == '' ) $errors->add( 'empty_sitename', __( 'You must provide a name for your network of sites.' ) );
die( 'You must provide a site name!' );
// check for network collision // check for network collision
$existing_network = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE id = %d", $network_id ) ); if ( $network_id == $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE id = %d", $network_id ) ) )
if ( $existing_network == $network_id ) $errors->add( 'siteid_exists', __( 'The network already exists.' ) );
die( 'That network already exists!' );
$site_user = get_user_by_email( $email ); $site_user = get_user_by_email( $email );
if ( !$site_user ) if ( ! is_email( $email ) )
die( 'You must provide an email address!' ); $errors->add( 'invalid_email', __( 'You must provide a valid e-mail address.' ) );
if ( $errors->get_error_code() )
return $errors;
// set up site tables // set up site tables
$template = get_option( 'template' ); $template = get_option( 'template' );
$stylesheet = get_option( 'stylesheet' ); $stylesheet = get_option( 'stylesheet' );
@ -742,26 +746,27 @@ Thanks!
if ( $subdomain_install ) { if ( $subdomain_install ) {
$vhost_ok = false; $vhost_ok = false;
$errstr = '';
$hostname = substr( md5( time() ), 0, 6 ) . '.' . $domain; // Very random hostname! $hostname = substr( md5( time() ), 0, 6 ) . '.' . $domain; // Very random hostname!
$page = wp_remote_get( 'http://' . $hostname, array( 'timeout' => 5, 'httpversion' => '1.1' ) ); $page = wp_remote_get( 'http://' . $hostname, array( 'timeout' => 5, 'httpversion' => '1.1' ) );
if ( is_wp_error( $page ) ) { if ( is_wp_error( $page ) )
foreach ( $page->get_error_messages() as $err ) { $errstr = $page->get_error_message();
$errstr = $err; elseif ( 200 == $page['response']['code'] )
}
} elseif( $page[ 'response' ][ 'code' ] == 200 ) {
$vhost_ok = true; $vhost_ok = true;
}
if ( ! $vhost_ok ) { if ( ! $vhost_ok ) {
// @todo Update this to reflect the merge. Also: Multisite readme file, or remove the <blockquote> tags. $msg = '<p><strong>' . __( 'Warning! Wildcard DNS may not be configured correctly!' ) . '</strong></p>';
$msg = '<h2>' . esc_html__( 'Warning! Wildcard DNS may not be configured correctly!' ) . '</h2>'; $msg .= '<p>' . sprintf( __( 'To use a subdomain configuration, you must have a wildcard entry in your DNS. The installer attempted to contact a random hostname (<code>%1$s</code>) on your domain.' ), $hostname );
$msg .= '<p>' . __( 'To use the subdomain feature of WordPress MU you must have a wildcard entry in your dns. The installer attempted to contact a random hostname ($hostname) on your domain but failed. It returned this error message:' ) . '<br />'; if ( ! empty ( $errstr ) )
$msg .= '<br/><strong>' . $errstr . '</strong></p>'; $msg .= ' ' . sprintf( __( 'This resulted in an error message: %s' ), $errstr );
$msg .= '<p>' . __( 'From the README.txt:' ) . '</p>'; $msg .= '</p>';
$msg .= '<blockquote><p>' . __( "If you want to host blogs of the form http://blog.domain.tld/ where domain.tld is the domain name of your machine then you must add a wildcard record to your DNS records. This usually means adding a '*' hostname record pointing at your webserver in your DNS configuration tool. Matt has a more detailed <a href='http://ma.tt/2003/10/10/wildcard-dns-and-sub-domains/'>explanation</a> on his blog. If you still have problems, these <a href='http://mu.wordpress.org/forums/tags/wildcard'>forum messages</a> may help." ) . '</p></blockquote>'; $msg .= '<p>' . __( 'If you want to host sites in the form of <code>http://site1.example.com</code> then you must add a wildcard record to your DNS records. This usually means adding a <code>*</code> hostname record pointing at your web server in your DNS configuration tool.' ) . '</p>';
$msg .= '<p>' . __( 'You can still use your site but any subdomain you create may not be accessible. This check is not foolproof so ignore if you know your dns is correct.' ) . '</p>'; $msg .= '<p>' . __( 'You can still use your site but any subdomain you create may not be accessible. If you know your DNS is correct, ignore this message.' ) . '</p>';
return new WP_Error( 'no_wildcard_dns', $msg );
} }
} }
return $msg;
return true;
} }
?> ?>

View File

@ -62,7 +62,7 @@ include( './admin-header.php' );
<?php screen_icon(); ?> <?php screen_icon(); ?>
<h2><?php echo esc_html( $title ); ?></h2> <h2><?php echo esc_html( $title ); ?></h2>
<form method="post" action="network.php"> <form method="post">
<?php <?php
/** /**
* Prints step 1 for Network installation process. * Prints step 1 for Network installation process.
@ -72,7 +72,7 @@ include( './admin-header.php' );
* *
* @since 3.0.0 * @since 3.0.0
*/ */
function network_step1() { function network_step1( $errors = false ) {
$active_plugins = get_option( 'active_plugins' ); $active_plugins = get_option( 'active_plugins' );
if ( ! empty( $active_plugins ) ) { if ( ! empty( $active_plugins ) ) {
@ -95,16 +95,31 @@ function network_step1() {
die(); die();
} }
wp_nonce_field( 'install-network-1' );
$error_codes = array();
if ( is_wp_error( $errors ) ) {
echo '<div class="error"><p><strong>' . __( 'ERROR: The network could not be created.' ) . '</strong></p>';
foreach ( $errors->get_error_messages() as $error )
echo "<p>$error</p>";
echo '</div>';
$error_codes = $errors->get_error_codes();
}
$site_name = ( ! empty( $_POST['sitename'] ) && ! in_array( 'empty_sitename', $error_codes ) ) ? $_POST['sitename'] : sprintf( _x('%s Sites', 'Default network name' ), get_option( 'blogname' ) );
$admin_email = ( ! empty( $_POST['email'] ) && ! in_array( 'invalid_email', $error_codes ) ) ? $_POST['email'] : get_option( 'admin_email' );
?> ?>
<p><?php _e( 'Welcome to the Network installation process!' ); ?></p> <p><?php _e( 'Welcome to the Network installation process!' ); ?></p>
<p><?php _e( "Fill in the information below and you'll be on your way to creating a network of WordPress sites. We'll create configuration files in the next step." ); ?></p> <p><?php _e( "Fill in the information below and you'll be on your way to creating a network of WordPress sites. We'll create configuration files in the next step." ); ?></p>
<?php <?php
// @todo IIS... // @todo IIS...
if ( apache_mod_loaded('mod_rewrite') ) { // assume nothing if ( ! empty( $_POST['subdomain_install'] ) ) {
$rewrite_enabled = true; $subdomain_install = (bool) $_POST['subdomain_install'];
} elseif ( apache_mod_loaded('mod_rewrite') ) { // assume nothing
$subdomain_install = true;
} else { } else {
$rewrite_enabled = false; $subdomain_install = false;
if ( got_mod_rewrite() ) // dangerous assumptions if ( got_mod_rewrite() ) // dangerous assumptions
echo '<p>' . __( 'Please make sure the Apache <code>mod_rewrite</code> module is installed as it will be used at the end of this install.' ) . '</p>'; echo '<p>' . __( 'Please make sure the Apache <code>mod_rewrite</code> module is installed as it will be used at the end of this install.' ) . '</p>';
else else
@ -112,23 +127,21 @@ function network_step1() {
echo '<p>' . __( 'If <code>mod_rewrite</code> is disabled ask your administrator to enable that module, or look at the <a href="http://httpd.apache.org/docs/mod/mod_rewrite.html">Apache documentation</a> or <a href="http://www.google.com/search?q=apache+mod_rewrite">elsewhere</a> for help setting it up.' ) . '</p>'; echo '<p>' . __( 'If <code>mod_rewrite</code> is disabled ask your administrator to enable that module, or look at the <a href="http://httpd.apache.org/docs/mod/mod_rewrite.html">Apache documentation</a> or <a href="http://www.google.com/search?q=apache+mod_rewrite">elsewhere</a> for help setting it up.' ) . '</p>';
} }
wp_nonce_field( 'install-network-1' );
if ( 'localhost' != $hostname ) : ?> if ( 'localhost' != $hostname ) : ?>
<h3><?php esc_html_e( 'Addresses of Sites in your Network' ); ?></h3> <h3><?php esc_html_e( 'Addresses of Sites in your Network' ); ?></h3>
<p><?php _e( 'Please choose whether you would like sites in your WordPress network to use sub-domains or sub-directories. <strong>You cannot change this later.</strong>' ); ?></p> <p><?php _e( 'Please choose whether you would like sites in your WordPress network to use sub-domains or sub-directories. <strong>You cannot change this later.</strong>' ); ?></p>
<p><?php _e( "You will need a wildcard DNS record if you're going to use the virtual host (sub-domain) functionality." ); ?></p> <p><?php _e( "You will need a wildcard DNS record if you're going to use the virtual host (sub-domain) functionality." ); ?></p>
<?php /* @todo: Link to an MS readme? */ ?> <?php /* @todo: Link to an MS readme? */ ?>
<?php if ( ! $rewrite_enabled ) { ?> <?php if ( ! $subdomain_install ) { ?>
<p><?php _e( '<strong>Note</strong> It looks like <code>mod_rewrite</code> is not installed.' ); ?></p> <p><?php _e( '<strong>Note</strong> It looks like <code>mod_rewrite</code> is not installed.' ); ?></p>
<?php } ?> <?php } ?>
<table class="form-table"> <table class="form-table">
<tr> <tr>
<th><label><input type='radio' name='subdomain_install' value='1'<?php checked( $rewrite_enabled ); ?> /> Sub-domains</label></th> <th><label><input type='radio' name='subdomain_install' value='1'<?php checked( $subdomain_install ); ?> /> Sub-domains</label></th>
<td><?php _e('like <code>site1.example.com</code> and <code>site2.example.com</code>'); ?></td> <td><?php _e('like <code>site1.example.com</code> and <code>site2.example.com</code>'); ?></td>
</tr> </tr>
<tr> <tr>
<th><label><input type='radio' name='subdomain_install' value='0'<?php checked( ! $rewrite_enabled ); ?> /> Sub-directories</label></th> <th><label><input type='radio' name='subdomain_install' value='0'<?php checked( ! $subdomain_install ); ?> /> Sub-directories</label></th>
<td><?php _e('like <code>example.com/site1</code> and <code>example.com/site2</code>'); ?></td> <td><?php _e('like <code>example.com/site1</code> and <code>example.com/site2</code>'); ?></td>
</tr> </tr>
</table> </table>
@ -136,7 +149,7 @@ function network_step1() {
<?php <?php
endif; endif;
$is_www = ( substr( $hostname, 0, 4 ) == 'www.' ); $is_www = ( 0 === strpos( $hostname, 'www.' ) );
if ( $is_www ) : if ( $is_www ) :
?> ?>
<h3><?php esc_html_e( 'Server Address' ); ?></h3> <h3><?php esc_html_e( 'Server Address' ); ?></h3>
@ -146,7 +159,6 @@ function network_step1() {
<th scope='row'><?php esc_html_e( 'Server Address' ); ?></th> <th scope='row'><?php esc_html_e( 'Server Address' ); ?></th>
<td> <td>
<?php printf( __( 'The Internet address of your network will be <code>%s</code>.' ), $hostname ); ?> <?php printf( __( 'The Internet address of your network will be <code>%s</code>.' ), $hostname ); ?>
<input type='hidden' name='basedomain' value='<?php echo esc_attr( $hostname ); ?>' />
</td> </td>
</tr> </tr>
</table> </table>
@ -171,14 +183,14 @@ function network_step1() {
<tr> <tr>
<th scope='row'><?php esc_html_e( 'Network Title' ); ?></th> <th scope='row'><?php esc_html_e( 'Network Title' ); ?></th>
<td> <td>
<input name='weblog_title' type='text' size='45' value='<?php echo esc_attr( sprintf( __('%s Sites'), get_option( 'blogname' ) ) ); ?>' /> <input name='sitename' type='text' size='45' value='<?php echo esc_attr( $site_name ); ?>' />
<br /><?php _e( 'What would you like to call your network?' ); ?> <br /><?php _e( 'What would you like to call your network?' ); ?>
</td> </td>
</tr> </tr>
<tr> <tr>
<th scope='row'><?php esc_html_e( 'Admin E-mail Address' ); ?></th> <th scope='row'><?php esc_html_e( 'Admin E-mail Address' ); ?></th>
<td> <td>
<input name='email' type='text' size='45' value='<?php echo esc_attr( get_option( 'admin_email' ) ); ?>' /> <input name='email' type='text' size='45' value='<?php echo esc_attr( $admin_email ); ?>' />
<br /><?php _e( 'Your email address.' ); ?> <br /><?php _e( 'Your email address.' ); ?>
</td> </td>
</tr> </tr>
@ -192,9 +204,14 @@ function network_step1() {
* *
* @since 3.0.0 * @since 3.0.0
*/ */
function network_step2() { function network_step2( $errors = false ) {
global $base, $wpdb; global $base, $wpdb;
$hostname = get_clean_basedomain(); $hostname = get_clean_basedomain();
// Wildcard DNS message.
if ( is_wp_error( $errors ) )
echo '<div class="error">' . $errors->get_error_message() . '</div>';
if ( $_POST ) { if ( $_POST ) {
$vhost = 'localhost' == $hostname ? false : (bool) $_POST['subdomain_install']; $vhost = 'localhost' == $hostname ? false : (bool) $_POST['subdomain_install'];
} else { } else {
@ -263,14 +280,11 @@ define( 'BLOG_ID_CURRENT_SITE', 1 );</textarea>
</li> </li>
<?php <?php
// remove ending slash from $base and $url // @todo custom content dir
$htaccess = ''; $htaccess_file = 'RewriteEngine On
$base = rtrim( $base, '/' ); RewriteBase ' . $base . '
$htaccess_file = 'RewriteEngine On # uploaded files
RewriteBase ' . $base . '/
#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L] RewriteRule ^(.*/)?files/$ index.php [L]
RewriteCond %{REQUEST_URI} !.*wp-content/plugins.* RewriteCond %{REQUEST_URI} !.*wp-content/plugins.*
RewriteRule ^(.*/)?files/(.*) wp-includes/ms-files.php?file=$2 [L] RewriteRule ^(.*/)?files/(.*) wp-includes/ms-files.php?file=$2 [L]
@ -307,10 +321,19 @@ if ( $_POST ) {
install_network(); install_network();
$hostname = get_clean_basedomain(); $hostname = get_clean_basedomain();
$subdomain_install = 'localhost' == $hostname ? false : (bool) $_POST['subdomain_install']; $subdomain_install = 'localhost' == $hostname ? false : (bool) $_POST['subdomain_install'];
if ( ! network_domain_check() ) if ( ! network_domain_check() ) {
populate_network( 1, get_clean_basedomain(), sanitize_email( $_POST['email'] ), $_POST['weblog_title'], $base, $subdomain_install ); $result = populate_network( 1, get_clean_basedomain(), sanitize_email( $_POST['email'] ), $_POST['sitename'], $base, $subdomain_install );
// create wp-config.php / htaccess if ( is_wp_error( $result ) ) {
network_step2(); if ( 1 == count( $result->get_error_codes() ) && 'no_wildcard_dns' == $result->get_error_code() )
network_step2( $result );
else
network_step1( $result );
} else {
network_step2();
}
} else {
network_step2();
}
} elseif ( is_multisite() || network_domain_check() ) { } elseif ( is_multisite() || network_domain_check() ) {
network_step2(); network_step2();
} else { } else {