Ticket #15729: 15729-8.diff

File 15729-8.diff, 7.8 KB (added by kapeels, 2 years ago)

This has got to be commitable now :D

Line 
1Index: wp-admin/setup-config.php
2===================================================================
3--- wp-admin/setup-config.php   (revision 16800)
4+++ wp-admin/setup-config.php   (working copy)
5@@ -88,7 +88,7 @@
6 <link rel="stylesheet" href="css/install.css" type="text/css" />
7 
8 </head>
9-<body>
10+<body onload="setFocus();">
11 <h1 id="logo"><img alt="WordPress" src="images/wordpress-logo.png" /></h1>
12 <?php
13 }//end function display_header();
14@@ -114,34 +114,64 @@
15        break;
16 
17        case 1:
18-               display_header();
19+            display_header();
20+            if ( isset( $_POST['setup_error'] ) ) {
21+                switch( $_POST['setup_error'] ) {
22+                    case 'db_connect_fail' :
23+                        $error_msg = "Cannot connect to the database server with the provided username,password and host combination.";
24+                        $focus_element = "uname";
25+                    break;
26+                    case 'db_select_fail' :
27+                        $error_msg = "Cannot select the database.";
28+                        $focus_element = "dbname";
29+                    break;
30+                    case 'invalid_prefix' :
31+                        $error_msg = "The table prefix can contain only letters, numbers, and underscores.";
32+                        $focus_element = "prefix";
33+                    break;
34+                }               
35+                ?>
36+                <script type="text/javascript">
37+                    function setFocus() {
38+                        document.getElementById( '<?php echo $focus_element; ?>' ).focus();
39+                    }
40+                </script>
41+                <p style="color:red;"><?php echo $error_msg; ?></p>
42+                <?php               
43+            }
44+            $dbname =   !empty( $_POST['dbname'] ) ? trim( $_POST['dbname'] ) : 'wordpress';
45+            $uname  =   !empty( $_POST['uname'] ) ? trim( $_POST['uname'] ) : 'username';
46+            // password can be left blank
47+            $password=   isset( $_POST['pwd'] ) ? $_POST['pwd'] : 'password';
48+            $dbhost =   !empty( $_POST['dbhost'] ) ? trim( $_POST['dbhost'] ) : 'localhost';
49+            $prefix =   !empty( $_POST['prefix'] ) ? trim( $_POST['prefix'] ) : 'wp_';           
50        ?>
51 <form method="post" action="setup-config.php?step=2">
52        <p>Below you should enter your database connection details. If you're not sure about these, contact your host. </p>
53        <table class="form-table">
54                <tr>
55                        <th scope="row"><label for="dbname">Database Name</label></th>
56-                       <td><input name="dbname" id="dbname" type="text" size="25" value="wordpress" /></td>
57+                       <td><input name="dbname" id="dbname" type="text" size="25" value="<?php echo htmlspecialchars( $dbname, ENT_QUOTES ); ?>" /></td>
58                        <td>The name of the database you want to run WP in. </td>
59                </tr>
60                <tr>
61                        <th scope="row"><label for="uname">User Name</label></th>
62-                       <td><input name="uname" id="uname" type="text" size="25" value="username" /></td>
63+                       <td><input name="uname" id="uname" type="text" size="25" value="<?php echo htmlspecialchars( $uname, ENT_QUOTES ); ?>" /></td>
64                        <td>Your MySQL username</td>
65                </tr>
66                <tr>
67                        <th scope="row"><label for="pwd">Password</label></th>
68-                       <td><input name="pwd" id="pwd" type="text" size="25" value="password" /></td>
69+                       <td><input name="pwd" id="pwd" type="text" size="25" value="<?php echo htmlspecialchars( $password, ENT_QUOTES ); ?>" /></td>
70                        <td>...and MySQL password.</td>
71                </tr>
72                <tr>
73                        <th scope="row"><label for="dbhost">Database Host</label></th>
74-                       <td><input name="dbhost" id="dbhost" type="text" size="25" value="localhost" /></td>
75+                       <td><input name="dbhost" id="dbhost" type="text" size="25" value="<?php echo htmlspecialchars( $dbhost, ENT_QUOTES ); ?>" /></td>
76                        <td>You should be able to get this info from your web host, if <code>localhost</code> does not work.</td>
77                </tr>
78                <tr>
79                        <th scope="row"><label for="prefix">Table Prefix</label></th>
80-                       <td><input name="prefix" id="prefix" type="text" id="prefix" value="wp_" size="25" /></td>
81+                       <td><input name="prefix" id="prefix" type="text" id="prefix" value="<?php echo htmlspecialchars( $prefix, ENT_QUOTES ); ?>" size="25" /></td>
82                        <td>If you want to run multiple WordPress installations in a single database, change this.</td>
83                </tr>
84        </table>
85@@ -154,33 +184,51 @@
86        case 2:
87        $dbname  = trim($_POST['dbname']);
88        $uname   = trim($_POST['uname']);
89-       $passwrd = trim($_POST['pwd']);
90+       $password = trim($_POST['pwd']);
91        $dbhost  = trim($_POST['dbhost']);
92        $prefix  = trim($_POST['prefix']);
93        if ( empty($prefix) )
94-               $prefix = 'wp_';
95-
96+               $prefix = 'wp_';       
97+        $setup_error = null;     
98        // Validate $prefix: it can only contain letters, numbers and underscores
99-       if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
100-               wp_die( /*WP_I18N_BAD_PREFIX*/'<strong>ERROR</strong>: "Table Prefix" can only contain numbers, letters, and underscores.'/*/WP_I18N_BAD_PREFIX*/ );
101+       if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) {               
102+                $prefix =   htmlspecialchars( $prefix, ENT_QUOTES );
103+                $setup_error = new WP_Error( 'invalid_prefix', '<strong>ERROR</strong>: "Table Prefix" can only contain numbers, letters, and underscores.' );
104+        }       
105+        // prefix is good, let's continue
106+        else {
107+            // Test the db connection.
108+            /**#@+
109+             * @ignore
110+             */
111+            define('DB_NAME', $dbname);
112+            define('DB_USER', $uname);
113+            define('DB_PASSWORD', $password);
114+            define('DB_HOST', $dbhost);
115+            /**#@-*/
116 
117-       // Test the db connection.
118-       /**#@+
119-        * @ignore
120-        */
121-       define('DB_NAME', $dbname);
122-       define('DB_USER', $uname);
123-       define('DB_PASSWORD', $passwrd);
124-       define('DB_HOST', $dbhost);
125-       /**#@-*/
126-
127-       // We'll fail here if the values are no good.
128-       require_wp_db();
129-       if ( ! empty( $wpdb->error ) ) {
130-               $back = '<p class="step"><a href="setup-config.php?step=1" onclick="javascript:history.go(-1);return false;" class="button">Try Again</a></p>';
131-               wp_die( $wpdb->error->get_error_message() . $back );
132-       }
133-
134+            // We'll fail here if the values are no good.
135+            require_wp_db();
136+            if ( ! empty( $wpdb->error ) ) {                                   
137+                $setup_error = $wpdb->error;
138+            }
139+        }
140+       
141+        // check if any error occured above       
142+        if ( is_wp_error( $setup_error ) ) {
143+            $try_again = '<p class="step">
144+            <form action="setup-config.php?step=1" method="post"> 
145+                <input name="setup_error" type="hidden" value="' . $setup_error->get_error_code() . '" />
146+                <input name="dbname" type="hidden" value="' . htmlspecialchars( $dbname, ENT_QUOTES ) . '" />         
147+                <input name="uname" type="hidden" value="' . htmlspecialchars( $uname, ENT_QUOTES ) . '" />           
148+                <input name="pwd" type="hidden" value="' . htmlspecialchars( $password, ENT_QUOTES ) . '" />                   
149+                <input name="dbhost" type="hidden" value="' . htmlspecialchars( $dbhost, ENT_QUOTES ) . '" />         
150+                <input name="prefix" type="hidden" id="prefix" value="' . $prefix . '" />   
151+                <input type="submit" class="button" value="Try Again" name="try-again" />
152+            </form>
153+                  </p>';
154+            wp_die( $setup_error->get_error_message() . $try_again );
155+        }     
156        // Fetch or generate keys and salts.
157        $no_api = isset( $_POST['noapi'] );
158        require_once( ABSPATH . WPINC . '/plugin.php' );
159@@ -223,7 +271,7 @@
160                                $configFile[$line_num] = str_replace("'username_here'", "'$uname'", $line);
161                                break;
162                        case "define('DB_PASSW":
163-                               $configFile[$line_num] = str_replace("'password_here'", "'$passwrd'", $line);
164+                               $configFile[$line_num] = str_replace("'password_here'", "'$password'", $line);
165                                break;
166                        case "define('DB_HOST'":
167                                $configFile[$line_num] = str_replace("localhost", $dbhost, $line);