PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/setup-config.php

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