setup-config.php (17004B)
1 <?php 2 /** 3 * Retrieves and creates the wp-config.php file. 4 * 5 * The permissions for the base directory must allow for writing files in order 6 * for the wp-config.php to be created using this page. 7 * 8 * @package WordPress 9 * @subpackage Administration 10 */ 11 12 /** 13 * We are installing. 14 */ 15 define( 'WP_INSTALLING', true ); 16 17 /** 18 * We are blissfully unaware of anything. 19 */ 20 define( 'WP_SETUP_CONFIG', true ); 21 22 /** 23 * Disable error reporting 24 * 25 * Set this to error_reporting( -1 ) for debugging 26 */ 27 error_reporting( 0 ); 28 29 if ( ! defined( 'ABSPATH' ) ) { 30 define( 'ABSPATH', dirname( __DIR__ ) . '/' ); 31 } 32 33 require ABSPATH . 'wp-settings.php'; 34 35 /** Load WordPress Administration Upgrade API */ 36 require_once ABSPATH . 'wp-admin/includes/upgrade.php'; 37 38 /** Load WordPress Translation Installation API */ 39 require_once ABSPATH . 'wp-admin/includes/translation-install.php'; 40 41 nocache_headers(); 42 43 // Support wp-config-sample.php one level up, for the develop repo. 44 if ( file_exists( ABSPATH . 'wp-config-sample.php' ) ) { 45 $config_file = file( ABSPATH . 'wp-config-sample.php' ); 46 } elseif ( file_exists( dirname( ABSPATH ) . '/wp-config-sample.php' ) ) { 47 $config_file = file( dirname( ABSPATH ) . '/wp-config-sample.php' ); 48 } else { 49 wp_die( 50 sprintf( 51 /* translators: %s: wp-config-sample.php */ 52 __( 'Sorry, I need a %s file to work from. Please re-upload this file to your WordPress installation.' ), 53 '<code>wp-config-sample.php</code>' 54 ) 55 ); 56 } 57 58 // Check if wp-config.php has been created. 59 if ( file_exists( ABSPATH . 'wp-config.php' ) ) { 60 wp_die( 61 '<p>' . sprintf( 62 /* translators: 1: wp-config.php, 2: install.php */ 63 __( 'The file %1$s already exists. If you need to reset any of the configuration items in this file, please delete it first. You may try <a href="%2$s">installing now</a>.' ), 64 '<code>wp-config.php</code>', 65 'install.php' 66 ) . '</p>', 67 409 68 ); 69 } 70 71 // Check if wp-config.php exists above the root directory but is not part of another installation. 72 if ( @file_exists( ABSPATH . '../wp-config.php' ) && ! @file_exists( ABSPATH . '../wp-settings.php' ) ) { 73 wp_die( 74 '<p>' . sprintf( 75 /* translators: 1: wp-config.php, 2: install.php */ 76 __( 'The file %1$s already exists one level above your WordPress installation. If you need to reset any of the configuration items in this file, please delete it first. You may try <a href="%2$s">installing now</a>.' ), 77 '<code>wp-config.php</code>', 78 'install.php' 79 ) . '</p>', 80 409 81 ); 82 } 83 84 $step = isset( $_GET['step'] ) ? (int) $_GET['step'] : -1; 85 86 /** 87 * Display setup wp-config.php file header. 88 * 89 * @ignore 90 * @since 2.3.0 91 * 92 * @global string $wp_local_package Locale code of the package. 93 * @global WP_Locale $wp_locale WordPress date and time locale object. 94 * 95 * @param string|string[] $body_classes Class attribute values for the body tag. 96 */ 97 function setup_config_display_header( $body_classes = array() ) { 98 $body_classes = (array) $body_classes; 99 $body_classes[] = 'wp-core-ui'; 100 $dir_attr = ''; 101 if ( is_rtl() ) { 102 $body_classes[] = 'rtl'; 103 $dir_attr = ' dir="rtl"'; 104 } 105 106 header( 'Content-Type: text/html; charset=utf-8' ); 107 ?> 108 <!DOCTYPE html> 109 <html<?php echo $dir_attr; ?>> 110 <head> 111 <meta name="viewport" content="width=device-width" /> 112 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 113 <meta name="robots" content="noindex,nofollow" /> 114 <title><?php _e( 'WordPress › Setup Configuration File' ); ?></title> 115 <?php wp_admin_css( 'install', true ); ?> 116 </head> 117 <body class="<?php echo implode( ' ', $body_classes ); ?>"> 118 <p id="logo"><?php _e( 'WordPress' ); ?></p> 119 <?php 120 } // End function setup_config_display_header(); 121 122 $language = ''; 123 if ( ! empty( $_REQUEST['language'] ) ) { 124 $language = preg_replace( '/[^a-zA-Z0-9_]/', '', $_REQUEST['language'] ); 125 } elseif ( isset( $GLOBALS['wp_local_package'] ) ) { 126 $language = $GLOBALS['wp_local_package']; 127 } 128 129 switch ( $step ) { 130 case -1: 131 if ( wp_can_install_language_pack() && empty( $language ) ) { 132 $languages = wp_get_available_translations(); 133 if ( $languages ) { 134 setup_config_display_header( 'language-chooser' ); 135 echo '<h1 class="screen-reader-text">Select a default language</h1>'; 136 echo '<form id="setup" method="post" action="?step=0">'; 137 wp_install_language_form( $languages ); 138 echo '</form>'; 139 break; 140 } 141 } 142 143 // Deliberately fall through if we can't reach the translations API. 144 145 case 0: 146 if ( ! empty( $language ) ) { 147 $loaded_language = wp_download_language_pack( $language ); 148 if ( $loaded_language ) { 149 load_default_textdomain( $loaded_language ); 150 $GLOBALS['wp_locale'] = new WP_Locale(); 151 } 152 } 153 154 setup_config_display_header(); 155 $step_1 = 'setup-config.php?step=1'; 156 if ( isset( $_REQUEST['noapi'] ) ) { 157 $step_1 .= '&noapi'; 158 } 159 if ( ! empty( $loaded_language ) ) { 160 $step_1 .= '&language=' . $loaded_language; 161 } 162 ?> 163 <h1 class="screen-reader-text"><?php _e( 'Before getting started' ); ?></h1> 164 <p><?php _e( 'Welcome to WordPress. Before getting started, we need some information on the database. You will need to know the following items before proceeding.' ); ?></p> 165 <ol> 166 <li><?php _e( 'Database name' ); ?></li> 167 <li><?php _e( 'Database username' ); ?></li> 168 <li><?php _e( 'Database password' ); ?></li> 169 <li><?php _e( 'Database host' ); ?></li> 170 <li><?php _e( 'Table prefix (if you want to run more than one WordPress in a single database)' ); ?></li> 171 </ol> 172 <p> 173 <?php 174 printf( 175 /* translators: %s: wp-config.php */ 176 __( 'We’re going to use this information to create a %s file.' ), 177 '<code>wp-config.php</code>' 178 ); 179 ?> 180 <strong> 181 <?php 182 printf( 183 /* translators: 1: wp-config-sample.php, 2: wp-config.php */ 184 __( 'If for any reason this automatic file creation doesn’t work, don’t worry. All this does is fill in the database information to a configuration file. You may also simply open %1$s in a text editor, fill in your information, and save it as %2$s.' ), 185 '<code>wp-config-sample.php</code>', 186 '<code>wp-config.php</code>' 187 ); 188 ?> 189 </strong> 190 <?php 191 printf( 192 /* translators: %s: Documentation URL. */ 193 __( 'Need more help? <a href="%s">We got it</a>.' ), 194 __( 'https://wordpress.org/support/article/editing-wp-config-php/' ) 195 ); 196 ?> 197 </p> 198 <p><?php _e( 'In all likelihood, these items were supplied to you by your Web Host. If you don’t have this information, then you will need to contact them before you can continue. If you’re all ready…' ); ?></p> 199 200 <p class="step"><a href="<?php echo $step_1; ?>" class="button button-large"><?php _e( 'Let’s go!' ); ?></a></p> 201 <?php 202 break; 203 204 case 1: 205 load_default_textdomain( $language ); 206 $GLOBALS['wp_locale'] = new WP_Locale(); 207 208 setup_config_display_header(); 209 210 $autofocus = wp_is_mobile() ? '' : ' autofocus'; 211 ?> 212 <h1 class="screen-reader-text"><?php _e( 'Set up your database connection' ); ?></h1> 213 <form method="post" action="setup-config.php?step=2"> 214 <p><?php _e( 'Below you should enter your database connection details. If you’re not sure about these, contact your host.' ); ?></p> 215 <table class="form-table" role="presentation"> 216 <tr> 217 <th scope="row"><label for="dbname"><?php _e( 'Database Name' ); ?></label></th> 218 <td><input name="dbname" id="dbname" type="text" aria-describedby="dbname-desc" size="25" value="wordpress"<?php echo $autofocus; ?>/></td> 219 <td id="dbname-desc"><?php _e( 'The name of the database you want to use with WordPress.' ); ?></td> 220 </tr> 221 <tr> 222 <th scope="row"><label for="uname"><?php _e( 'Username' ); ?></label></th> 223 <td><input name="uname" id="uname" type="text" aria-describedby="uname-desc" size="25" value="<?php echo htmlspecialchars( _x( 'username', 'example username' ), ENT_QUOTES ); ?>" /></td> 224 <td id="uname-desc"><?php _e( 'Your database username.' ); ?></td> 225 </tr> 226 <tr> 227 <th scope="row"><label for="pwd"><?php _e( 'Password' ); ?></label></th> 228 <td><input name="pwd" id="pwd" type="text" aria-describedby="pwd-desc" size="25" value="<?php echo htmlspecialchars( _x( 'password', 'example password' ), ENT_QUOTES ); ?>" autocomplete="off" /></td> 229 <td id="pwd-desc"><?php _e( 'Your database password.' ); ?></td> 230 </tr> 231 <tr> 232 <th scope="row"><label for="dbhost"><?php _e( 'Database Host' ); ?></label></th> 233 <td><input name="dbhost" id="dbhost" type="text" aria-describedby="dbhost-desc" size="25" value="localhost" /></td> 234 <td id="dbhost-desc"> 235 <?php 236 /* translators: %s: localhost */ 237 printf( __( 'You should be able to get this info from your web host, if %s doesn’t work.' ), '<code>localhost</code>' ); 238 ?> 239 </td> 240 </tr> 241 <tr> 242 <th scope="row"><label for="prefix"><?php _e( 'Table Prefix' ); ?></label></th> 243 <td><input name="prefix" id="prefix" type="text" aria-describedby="prefix-desc" value="wp_" size="25" /></td> 244 <td id="prefix-desc"><?php _e( 'If you want to run multiple WordPress installations in a single database, change this.' ); ?></td> 245 </tr> 246 </table> 247 <?php 248 if ( isset( $_GET['noapi'] ) ) { 249 ?> 250 <input name="noapi" type="hidden" value="1" /><?php } ?> 251 <input type="hidden" name="language" value="<?php echo esc_attr( $language ); ?>" /> 252 <p class="step"><input name="submit" type="submit" value="<?php echo htmlspecialchars( __( 'Submit' ), ENT_QUOTES ); ?>" class="button button-large" /></p> 253 </form> 254 <?php 255 break; 256 257 case 2: 258 load_default_textdomain( $language ); 259 $GLOBALS['wp_locale'] = new WP_Locale(); 260 261 $dbname = trim( wp_unslash( $_POST['dbname'] ) ); 262 $uname = trim( wp_unslash( $_POST['uname'] ) ); 263 $pwd = trim( wp_unslash( $_POST['pwd'] ) ); 264 $dbhost = trim( wp_unslash( $_POST['dbhost'] ) ); 265 $prefix = trim( wp_unslash( $_POST['prefix'] ) ); 266 267 $step_1 = 'setup-config.php?step=1'; 268 $install = 'install.php'; 269 if ( isset( $_REQUEST['noapi'] ) ) { 270 $step_1 .= '&noapi'; 271 } 272 273 if ( ! empty( $language ) ) { 274 $step_1 .= '&language=' . $language; 275 $install .= '?language=' . $language; 276 } else { 277 $install .= '?language=en_US'; 278 } 279 280 $tryagain_link = '</p><p class="step"><a href="' . $step_1 . '" onclick="javascript:history.go(-1);return false;" class="button button-large">' . __( 'Try Again' ) . '</a>'; 281 282 if ( empty( $prefix ) ) { 283 wp_die( __( '<strong>Error</strong>: "Table Prefix" must not be empty.' ) . $tryagain_link ); 284 } 285 286 // Validate $prefix: it can only contain letters, numbers and underscores. 287 if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) { 288 wp_die( __( '<strong>Error</strong>: "Table Prefix" can only contain numbers, letters, and underscores.' ) . $tryagain_link ); 289 } 290 291 // Test the DB connection. 292 /**#@+ 293 * 294 * @ignore 295 */ 296 define( 'DB_NAME', $dbname ); 297 define( 'DB_USER', $uname ); 298 define( 'DB_PASSWORD', $pwd ); 299 define( 'DB_HOST', $dbhost ); 300 /**#@-*/ 301 302 // Re-construct $wpdb with these new values. 303 unset( $wpdb ); 304 require_wp_db(); 305 306 /* 307 * The wpdb constructor bails when WP_SETUP_CONFIG is set, so we must 308 * fire this manually. We'll fail here if the values are no good. 309 */ 310 $wpdb->db_connect(); 311 312 if ( ! empty( $wpdb->error ) ) { 313 wp_die( $wpdb->error->get_error_message() . $tryagain_link ); 314 } 315 316 $errors = $wpdb->hide_errors(); 317 $wpdb->query( "SELECT $prefix" ); 318 $wpdb->show_errors( $errors ); 319 if ( ! $wpdb->last_error ) { 320 // MySQL was able to parse the prefix as a value, which we don't want. Bail. 321 wp_die( __( '<strong>Error</strong>: "Table Prefix" is invalid.' ) ); 322 } 323 324 // Generate keys and salts using secure CSPRNG; fallback to API if enabled; further fallback to original wp_generate_password(). 325 try { 326 $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|'; 327 $max = strlen( $chars ) - 1; 328 for ( $i = 0; $i < 8; $i++ ) { 329 $key = ''; 330 for ( $j = 0; $j < 64; $j++ ) { 331 $key .= substr( $chars, random_int( 0, $max ), 1 ); 332 } 333 $secret_keys[] = $key; 334 } 335 } catch ( Exception $ex ) { 336 $no_api = isset( $_POST['noapi'] ); 337 338 if ( ! $no_api ) { 339 $secret_keys = wp_remote_get( 'https://api.wordpress.org/secret-key/1.1/salt/' ); 340 } 341 342 if ( $no_api || is_wp_error( $secret_keys ) ) { 343 $secret_keys = array(); 344 for ( $i = 0; $i < 8; $i++ ) { 345 $secret_keys[] = wp_generate_password( 64, true, true ); 346 } 347 } else { 348 $secret_keys = explode( "\n", wp_remote_retrieve_body( $secret_keys ) ); 349 foreach ( $secret_keys as $k => $v ) { 350 $secret_keys[ $k ] = substr( $v, 28, 64 ); 351 } 352 } 353 } 354 355 $key = 0; 356 foreach ( $config_file as $line_num => $line ) { 357 if ( '$table_prefix =' === substr( $line, 0, 15 ) ) { 358 $config_file[ $line_num ] = '$table_prefix = \'' . addcslashes( $prefix, "\\'" ) . "';\r\n"; 359 continue; 360 } 361 362 if ( ! preg_match( '/^define\(\s*\'([A-Z_]+)\',([ ]+)/', $line, $match ) ) { 363 continue; 364 } 365 366 $constant = $match[1]; 367 $padding = $match[2]; 368 369 switch ( $constant ) { 370 case 'DB_NAME': 371 case 'DB_USER': 372 case 'DB_PASSWORD': 373 case 'DB_HOST': 374 $config_file[ $line_num ] = "define( '" . $constant . "'," . $padding . "'" . addcslashes( constant( $constant ), "\\'" ) . "' );\r\n"; 375 break; 376 case 'DB_CHARSET': 377 if ( 'utf8mb4' === $wpdb->charset || ( ! $wpdb->charset && $wpdb->has_cap( 'utf8mb4' ) ) ) { 378 $config_file[ $line_num ] = "define( '" . $constant . "'," . $padding . "'utf8mb4' );\r\n"; 379 } 380 break; 381 case 'AUTH_KEY': 382 case 'SECURE_AUTH_KEY': 383 case 'LOGGED_IN_KEY': 384 case 'NONCE_KEY': 385 case 'AUTH_SALT': 386 case 'SECURE_AUTH_SALT': 387 case 'LOGGED_IN_SALT': 388 case 'NONCE_SALT': 389 $config_file[ $line_num ] = "define( '" . $constant . "'," . $padding . "'" . $secret_keys[ $key++ ] . "' );\r\n"; 390 break; 391 } 392 } 393 unset( $line ); 394 395 if ( ! is_writable( ABSPATH ) ) : 396 setup_config_display_header(); 397 ?> 398 <p> 399 <?php 400 /* translators: %s: wp-config.php */ 401 printf( __( 'Unable to write to %s file.' ), '<code>wp-config.php</code>' ); 402 ?> 403 </p> 404 <p> 405 <?php 406 /* translators: %s: wp-config.php */ 407 printf( __( 'You can create the %s file manually and paste the following text into it.' ), '<code>wp-config.php</code>' ); 408 409 $config_text = ''; 410 411 foreach ( $config_file as $line ) { 412 $config_text .= htmlentities( $line, ENT_COMPAT, 'UTF-8' ); 413 } 414 ?> 415 </p> 416 <textarea id="wp-config" cols="98" rows="15" class="code" readonly="readonly"><?php echo $config_text; ?></textarea> 417 <p><?php _e( 'After you’ve done that, click “Run the installation”.' ); ?></p> 418 <p class="step"><a href="<?php echo $install; ?>" class="button button-large"><?php _e( 'Run the installation' ); ?></a></p> 419 <script> 420 (function(){ 421 if ( ! /iPad|iPod|iPhone/.test( navigator.userAgent ) ) { 422 var el = document.getElementById('wp-config'); 423 el.focus(); 424 el.select(); 425 } 426 })(); 427 </script> 428 <?php 429 else : 430 /* 431 * If this file doesn't exist, then we are using the wp-config-sample.php 432 * file one level up, which is for the develop repo. 433 */ 434 if ( file_exists( ABSPATH . 'wp-config-sample.php' ) ) { 435 $path_to_wp_config = ABSPATH . 'wp-config.php'; 436 } else { 437 $path_to_wp_config = dirname( ABSPATH ) . '/wp-config.php'; 438 } 439 440 $error_message = ''; 441 $handle = fopen( $path_to_wp_config, 'w' ); 442 /* 443 * Why check for the absence of false instead of checking for resource with is_resource()? 444 * To future-proof the check for when fopen returns object instead of resource, i.e. a known 445 * change coming in PHP. 446 */ 447 if ( false !== $handle ) { 448 foreach ( $config_file as $line ) { 449 fwrite( $handle, $line ); 450 } 451 fclose( $handle ); 452 } else { 453 $wp_config_perms = fileperms( $path_to_wp_config ); 454 if ( ! empty( $wp_config_perms ) && ! is_writable( $path_to_wp_config ) ) { 455 $error_message = sprintf( 456 /* translators: 1: wp-config.php, 2: Documentation URL. */ 457 __( 'You need to make the file %1$s writable before you can save your changes. See <a href="%2$s">Changing File Permissions</a> for more information.' ), 458 '<code>wp-config.php</code>', 459 __( 'https://wordpress.org/support/article/changing-file-permissions/' ) 460 ); 461 } else { 462 $error_message = sprintf( 463 /* translators: %s: wp-config.php */ 464 __( 'Unable to write to %s file.' ), 465 '<code>wp-config.php</code>' 466 ); 467 } 468 } 469 470 chmod( $path_to_wp_config, 0666 ); 471 setup_config_display_header(); 472 473 if ( false !== $handle ) : 474 ?> 475 <h1 class="screen-reader-text"><?php _e( 'Successful database connection' ); ?></h1> 476 <p><?php _e( 'All right, sparky! You’ve made it through this part of the installation. WordPress can now communicate with your database. If you are ready, time now to…' ); ?></p> 477 478 <p class="step"><a href="<?php echo $install; ?>" class="button button-large"><?php _e( 'Run the installation' ); ?></a></p> 479 <?php 480 else : 481 printf( '<p>%s</p>', $error_message ); 482 endif; 483 endif; 484 break; 485 } // End of the steps switch. 486 ?> 487 <?php wp_print_scripts( 'language-chooser' ); ?> 488 </body> 489 </html>