WordPress.org

Make WordPress Core

Changeset 13655


Ignore:
Timestamp:
03/11/10 01:51:24 (4 years ago)
Author:
nacin
Message:

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

Location:
trunk/wp-admin
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/schema.php

    r13642 r13655  
    620620 * 
    621621 * @param int $network_id id of network to populate 
     622 * @return bool|WP_Error True on success, or WP_Error on warning (with the install otherwise successful, 
     623 *  so the error code must be checked) or failure. 
    622624 */ 
    623625function populate_network( $network_id = 1, $domain = '', $email = '', $site_name = '', $path = '/', $subdomain_install = false ) { 
    624626    global $wpdb, $current_site, $wp_db_version, $wp_rewrite; 
    625627 
    626     $msg = ''; 
    627     //@todo: turn these checks into returned messages 
    628     if ( $domain == '' ) 
    629         die( 'You must provide a domain name!' ); 
    630     if ( $site_name == '' ) 
    631         die( 'You must provide a site name!' ); 
     628    $errors = new WP_Error(); 
     629    if ( '' == $domain ) 
     630        $errors->add( 'empty_domain', __( 'You must provide a domain name.' ) ); 
     631    if ( '' == $site_name ) 
     632        $errors->add( 'empty_sitename', __( 'You must provide a name for your network of sites.' ) ); 
    632633 
    633634    // check for network collision 
    634     $existing_network = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE id = %d", $network_id ) ); 
    635     if ( $existing_network == $network_id ) 
    636         die( 'That network already exists!' ); 
     635    if ( $network_id == $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE id = %d", $network_id ) ) ) 
     636        $errors->add( 'siteid_exists', __( 'The network already exists.' ) ); 
    637637 
    638638    $site_user = get_user_by_email( $email ); 
    639     if ( !$site_user ) 
    640         die( 'You must provide an email address!' ); 
     639    if ( ! is_email( $email ) ) 
     640        $errors->add( 'invalid_email', __( 'You must provide a valid e-mail address.' ) ); 
     641 
     642    if ( $errors->get_error_code() ) 
     643        return $errors; 
     644 
    641645    // set up site tables 
    642646    $template = get_option( 'template' ); 
     
    743747    if ( $subdomain_install ) { 
    744748        $vhost_ok = false; 
     749        $errstr = ''; 
    745750        $hostname = substr( md5( time() ), 0, 6 ) . '.' . $domain; // Very random hostname! 
    746751        $page = wp_remote_get( 'http://' . $hostname, array( 'timeout' => 5, 'httpversion' => '1.1' ) ); 
    747         if ( is_wp_error( $page ) ) { 
    748             foreach ( $page->get_error_messages() as $err ) { 
    749                 $errstr = $err; 
    750             } 
    751         } elseif( $page[ 'response' ][ 'code' ] == 200 ) { 
     752        if ( is_wp_error( $page ) ) 
     753            $errstr = $page->get_error_message(); 
     754        elseif ( 200 == $page['response']['code'] ) 
    752755                $vhost_ok = true; 
     756 
     757        if ( ! $vhost_ok ) { 
     758            $msg = '<p><strong>' . __( 'Warning! Wildcard DNS may not be configured correctly!' ) . '</strong></p>'; 
     759            $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 ); 
     760            if ( ! empty ( $errstr ) ) 
     761                $msg .= ' ' . sprintf( __( 'This resulted in an error message: %s' ), $errstr ); 
     762            $msg .= '</p>'; 
     763            $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>'; 
     764            $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>'; 
     765            return new WP_Error( 'no_wildcard_dns', $msg ); 
    753766        } 
    754         if ( ! $vhost_ok ) { 
    755             // @todo Update this to reflect the merge. Also: Multisite readme file, or remove the <blockquote> tags. 
    756             $msg = '<h2>' . esc_html__( 'Warning! Wildcard DNS may not be configured correctly!' ) . '</h2>'; 
    757             $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 />'; 
    758             $msg .= '<br/><strong>' . $errstr . '</strong></p>'; 
    759             $msg .= '<p>' . __( 'From the README.txt:' ) . '</p>'; 
    760             $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>'; 
    761             $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>'; 
    762         } 
    763     } 
    764     return $msg; 
     767    } 
     768 
     769    return true; 
    765770} 
    766771 
  • trunk/wp-admin/network.php

    r13644 r13655  
    6363<h2><?php echo esc_html( $title ); ?></h2> 
    6464 
    65 <form method="post" action="network.php"> 
     65<form method="post"> 
    6666<?php 
    6767/** 
     
    7373 * @since 3.0.0 
    7474 */ 
    75 function network_step1() { 
     75function network_step1( $errors = false ) { 
    7676 
    7777    $active_plugins = get_option( 'active_plugins' ); 
     
    9696    } 
    9797 
     98    wp_nonce_field( 'install-network-1' ); 
     99 
     100    $error_codes = array(); 
     101    if ( is_wp_error( $errors ) ) { 
     102        echo '<div class="error"><p><strong>' . __( 'ERROR: The network could not be created.' ) . '</strong></p>'; 
     103        foreach ( $errors->get_error_messages() as $error ) 
     104            echo "<p>$error</p>"; 
     105        echo '</div>'; 
     106        $error_codes = $errors->get_error_codes(); 
     107    } 
     108 
     109    $site_name = ( ! empty( $_POST['sitename'] ) && ! in_array( 'empty_sitename', $error_codes ) ) ? $_POST['sitename'] : sprintf( _x('%s Sites', 'Default network name' ), get_option( 'blogname' ) ); 
     110    $admin_email = ( ! empty( $_POST['email'] ) && ! in_array( 'invalid_email', $error_codes ) ) ? $_POST['email'] : get_option( 'admin_email' ); 
    98111    ?> 
    99112    <p><?php _e( 'Welcome to the Network installation process!' ); ?></p> 
     
    102115 
    103116    // @todo IIS... 
    104     if ( apache_mod_loaded('mod_rewrite') ) { // assume nothing 
    105         $rewrite_enabled = true; 
     117    if ( ! empty( $_POST['subdomain_install'] ) ) { 
     118        $subdomain_install = (bool) $_POST['subdomain_install']; 
     119    } elseif ( apache_mod_loaded('mod_rewrite') ) { // assume nothing 
     120        $subdomain_install = true; 
    106121    } else { 
    107         $rewrite_enabled = false; 
     122        $subdomain_install = false; 
    108123        if ( got_mod_rewrite() ) // dangerous assumptions 
    109124            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>'; 
     
    113128    } 
    114129 
    115     wp_nonce_field( 'install-network-1' ); 
    116  
    117130    if ( 'localhost' != $hostname ) : ?> 
    118131        <h3><?php esc_html_e( 'Addresses of Sites in your Network' ); ?></h3> 
     
    120133        <p><?php _e( "You will need a wildcard DNS record if you're going to use the virtual host (sub-domain) functionality." ); ?></p> 
    121134        <?php /* @todo: Link to an MS readme? */ ?> 
    122         <?php if ( ! $rewrite_enabled ) { ?> 
     135        <?php if ( ! $subdomain_install ) { ?> 
    123136        <p><?php _e( '<strong>Note</strong> It looks like <code>mod_rewrite</code> is not installed.' ); ?></p> 
    124137        <?php } ?> 
    125138        <table class="form-table"> 
    126139            <tr> 
    127                 <th><label><input type='radio' name='subdomain_install' value='1'<?php checked( $rewrite_enabled ); ?> /> Sub-domains</label></th> 
     140                <th><label><input type='radio' name='subdomain_install' value='1'<?php checked( $subdomain_install ); ?> /> Sub-domains</label></th> 
    128141                <td><?php _e('like <code>site1.example.com</code> and <code>site2.example.com</code>'); ?></td> 
    129142            </tr> 
    130143            <tr> 
    131                 <th><label><input type='radio' name='subdomain_install' value='0'<?php checked( ! $rewrite_enabled ); ?> /> Sub-directories</label></th> 
     144                <th><label><input type='radio' name='subdomain_install' value='0'<?php checked( ! $subdomain_install ); ?> /> Sub-directories</label></th> 
    132145                <td><?php _e('like <code>example.com/site1</code> and <code>example.com/site2</code>'); ?></td> 
    133146            </tr> 
     
    137150    endif; 
    138151 
    139         $is_www = ( substr( $hostname, 0, 4 ) == 'www.' ); 
     152        $is_www = ( 0 === strpos( $hostname, 'www.' ) ); 
    140153        if ( $is_www ) : 
    141154        ?> 
     
    147160                <td> 
    148161                    <?php printf( __( 'The Internet address of your network will be <code>%s</code>.' ), $hostname ); ?> 
    149                     <input type='hidden' name='basedomain' value='<?php echo esc_attr( $hostname ); ?>' /> 
    150162                </td> 
    151163            </tr> 
     
    172184                <th scope='row'><?php esc_html_e( 'Network Title' ); ?></th> 
    173185                <td> 
    174                     <input name='weblog_title' type='text' size='45' value='<?php echo esc_attr( sprintf( __('%s Sites'), get_option( 'blogname' ) ) ); ?>' /> 
     186                    <input name='sitename' type='text' size='45' value='<?php echo esc_attr( $site_name ); ?>' /> 
    175187                    <br /><?php _e( 'What would you like to call your network?' ); ?> 
    176188                </td> 
     
    179191                <th scope='row'><?php esc_html_e( 'Admin E-mail Address' ); ?></th> 
    180192                <td> 
    181                     <input name='email' type='text' size='45' value='<?php echo esc_attr( get_option( 'admin_email' ) ); ?>' /> 
     193                    <input name='email' type='text' size='45' value='<?php echo esc_attr( $admin_email ); ?>' /> 
    182194                    <br /><?php _e( 'Your email address.' ); ?> 
    183195                </td> 
     
    193205 * @since 3.0.0 
    194206 */ 
    195 function network_step2() { 
     207function network_step2( $errors = false ) { 
    196208    global $base, $wpdb; 
    197209    $hostname = get_clean_basedomain(); 
     210 
     211    // Wildcard DNS message. 
     212    if ( is_wp_error( $errors ) ) 
     213        echo '<div class="error">' . $errors->get_error_message() . '</div>'; 
     214 
    198215    if ( $_POST ) { 
    199216        $vhost = 'localhost' == $hostname ? false : (bool) $_POST['subdomain_install']; 
     
    264281<?php 
    265282 
    266     // remove ending slash from $base and $url 
    267     $htaccess = ''; 
    268     $base = rtrim( $base, '/' ); 
    269  
    270     $htaccess_file = 'RewriteEngine On 
    271 RewriteBase ' . $base . '/ 
    272  
    273 #uploaded files 
     283// @todo custom content dir 
     284$htaccess_file = 'RewriteEngine On 
     285RewriteBase ' . $base . ' 
     286 
     287# uploaded files 
    274288RewriteRule ^(.*/)?files/$ index.php [L] 
    275289RewriteCond %{REQUEST_URI} !.*wp-content/plugins.* 
     
    308322    $hostname = get_clean_basedomain(); 
    309323    $subdomain_install = 'localhost' == $hostname ? false : (bool) $_POST['subdomain_install']; 
    310     if ( ! network_domain_check() ) 
    311         populate_network( 1, get_clean_basedomain(), sanitize_email( $_POST['email'] ), $_POST['weblog_title'], $base, $subdomain_install ); 
    312     // create wp-config.php / htaccess 
    313     network_step2(); 
     324    if ( ! network_domain_check() ) { 
     325        $result = populate_network( 1, get_clean_basedomain(), sanitize_email( $_POST['email'] ), $_POST['sitename'], $base, $subdomain_install ); 
     326        if ( is_wp_error( $result ) ) { 
     327            if ( 1 == count( $result->get_error_codes() ) && 'no_wildcard_dns' == $result->get_error_code() ) 
     328                network_step2( $result ); 
     329            else 
     330                network_step1( $result ); 
     331        } else { 
     332            network_step2(); 
     333        } 
     334    } else { 
     335        network_step2(); 
     336    } 
    314337} elseif ( is_multisite() || network_domain_check() ) { 
    315338    network_step2(); 
Note: See TracChangeset for help on using the changeset viewer.