<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[15710] trunk/wp-login.php: Streamline password reset process.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/15710">15710</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2010-10-04 15:32:54 +0000 (Mon, 04 Oct 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Streamline password reset process. Allow user to input new password. Props scribu. see <a href="http://trac.wordpress.org/ticket/5919">#5919</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwploginphp">trunk/wp-login.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwploginphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-login.php (15709 => 15710)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-login.php        2010-10-04 14:17:34 UTC (rev 15709)
+++ trunk/wp-login.php        2010-10-04 15:32:54 UTC (rev 15710)
</span><span class="lines">@@ -115,6 +115,31 @@
</span><span class="cx">                         echo '<p class="message">' . apply_filters('login_messages', $messages) . "</p>\n";
</span><span class="cx">         }
</span><span class="cx"> } // End of login_header()
</span><ins>+
+/**
+ * Outputs the footer for the login page.
+ *
+ * @param string $input_id Which input to auto-focus
+ */
+function login_footer($input_id = '') {
+        echo "</div>\n";
+
+        if ( !empty($input_id) ) {
+?>
+<script type="text/javascript">
+try{document.getElementById('<?php echo $input_id; ?>').focus();}catch(e){}
+if(typeof wpOnload=='function')wpOnload();
+</script>
+<?php
+        }
+?>
+<p id="backtoblog"><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('&larr; Back to %s'), get_bloginfo('title', 'display' )); ?></a></p>
+
+</body>
+</html>
+<?php
+}
+
</ins><span class="cx"> function wp_shake_js() {
</span><span class="cx">         global $is_iphone;
</span><span class="cx">         if ( $is_iphone )
</span><span class="lines">@@ -211,14 +236,16 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Handles resetting the user's password.
</del><ins>+ * Retrieves a user row based on password reset key and login
</ins><span class="cx"> *
</span><span class="cx"> * @uses $wpdb WordPress Database object
</span><span class="cx"> *
</span><span class="cx"> * @param string $key Hash to validate sending user's password
</span><del>- * @return bool|WP_Error
</del><ins>+ * @param string $login The user login
+ *
+ * @return object|WP_Error
</ins><span class="cx"> */
</span><del>-function reset_password($key, $login) {
</del><ins>+function check_password_reset_key($key, $login) {
</ins><span class="cx">         global $wpdb;
</span><span class="cx">
</span><span class="cx">         $key = preg_replace('/[^a-z0-9]/i', '', $key);
</span><span class="lines">@@ -230,38 +257,26 @@
</span><span class="cx">                 return new WP_Error('invalid_key', __('Invalid key'));
</span><span class="cx">
</span><span class="cx">         $user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_activation_key = %s AND user_login = %s", $key, $login));
</span><ins>+
</ins><span class="cx">         if ( empty( $user ) )
</span><span class="cx">                 return new WP_Error('invalid_key', __('Invalid key'));
</span><ins>+                
+        return $user;
+}
</ins><span class="cx">
</span><del>-        // Generate something random for a password...
-        $new_pass = wp_generate_password();
-
</del><ins>+/**
+ * Handles resetting the user's password.
+ *
+ * @uses $wpdb WordPress Database object
+ *
+ * @param string $key Hash to validate sending user's password
+ */
+function reset_password($user, $new_pass) {
</ins><span class="cx">         do_action('password_reset', $user, $new_pass);
</span><span class="cx">
</span><span class="cx">         wp_set_password($new_pass, $user->ID);
</span><del>-        update_user_option($user->ID, 'default_password_nag', true, true); //Set up the Password change nag.
-        $message = sprintf(__('Username: %s'), $user->user_login) . "\r\n";
-        $message .= sprintf(__('Password: %s'), $new_pass) . "\r\n";
-        $message .= site_url('wp-login.php', 'login') . "\r\n";
</del><span class="cx">
</span><del>-        if ( is_multisite() )
-                $blogname = $GLOBALS['current_site']->site_name;
-        else
-                // The blogname option is escaped with esc_html on the way into the database in sanitize_option
-                // we want to reverse this for the plain text arena of emails.
-                $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
-
-        $title = sprintf( __('[%s] Your new password'), $blogname );
-
-        $title = apply_filters('password_reset_title', $title);
-        $message = apply_filters('password_reset_message', $message, $new_pass);
-
-        if ( $message && !wp_mail($user->user_email, $title, $message) )
-                 wp_die( __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') );
-
</del><span class="cx">         wp_password_change_notification($user);
</span><del>-
-        return true;
</del><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -398,39 +413,60 @@
</span><span class="cx"> </form>
</span><span class="cx">
</span><span class="cx"> <p id="nav">
</span><ins>+<a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a>
</ins><span class="cx"> <?php if (get_option('users_can_register')) : ?>
</span><del>-<a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a> |
-<a href="<?php echo site_url('wp-login.php?action=register', 'login') ?>"><?php _e('Register') ?></a>
-<?php else : ?>
-<a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a>
</del><ins>+ | <a href="<?php echo site_url('wp-login.php?action=register', 'login') ?>"><?php _e('Register') ?></a>
</ins><span class="cx"> <?php endif; ?>
</span><span class="cx"> </p>
</span><span class="cx">
</span><del>-</div>
-
-<p id="backtoblog"><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('&larr; Back to %s'), get_bloginfo('title', 'display' )); ?></a></p>
-
-<script type="text/javascript">
-try{document.getElementById('user_login').focus();}catch(e){}
-if(typeof wpOnload=='function')wpOnload();
-</script>
-</body>
-</html>
</del><span class="cx"> <?php
</span><ins>+login_footer('user_login');
</ins><span class="cx"> break;
</span><span class="cx">
</span><span class="cx"> case 'resetpass' :
</span><span class="cx"> case 'rp' :
</span><del>-        $errors = reset_password($_GET['key'], $_GET['login']);
</del><ins>+        $user = check_password_reset_key($_GET['key'], $_GET['login']);
</ins><span class="cx">
</span><del>-        if ( ! is_wp_error($errors) ) {
-                wp_redirect('wp-login.php?checkemail=newpass');
-                exit();
</del><ins>+        if ( is_wp_error($user) ) {
+                wp_redirect('wp-login.php?action=lostpassword&error=invalidkey');
+                exit;
</ins><span class="cx">         }
</span><span class="cx">
</span><del>-        wp_redirect('wp-login.php?action=lostpassword&error=invalidkey');
-        exit();
</del><ins>+        $errors = '';
</ins><span class="cx">
</span><ins>+        if ( isset($_POST['pass1']) && $_POST['pass1'] != $_POST['pass2'] ) {
+                $errors = new WP_Error('password_reset_mismatch', __('The passwords do not match.'));
+        } elseif ( isset($_POST['pass1']) && !empty($_POST['pass1']) ) {
+                reset_password($user, $_POST['pass']);
+                login_header(__('Password Reset'), '<p class="message reset-pass">' . __('Your password has been reset.') . ' <a href="' . site_url('wp-login.php', 'login') . '">' . __('Log in') . '</a></p>');
+                login_footer();
+                exit;
+        }
+
+        login_header(__('Reset Password'), '<p class="message reset-pass">' . __('Reset your password') . '</p>', $errors );
+?>
+<form name="resetpassform" id="resetpassform" action="<?php echo site_url('wp-login.php?action=resetpass&key=' . urlencode($_GET['key']) . '&login=' . urlencode($_GET['login']), 'login_post') ?>" method="post">
+        <p>
+                <label><?php _e('New Password') ?><br />
+                <input type="password" name="pass1" id="user_pass" class="input" size="20" value="" autocomplete="off" /></label>
+        </p>
+        <p>
+                <label><?php _e('New Password Again') ?><br />
+                <input type="password" name="pass2" id="user_pass" class="input" size="20" value="" autocomplete="off" /></label>
+        </p>
+        <br class="clear" />
+        <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" class="button-primary" value="<?php esc_attr_e('Reset Password'); ?>" tabindex="100" /></p>
+</form>
+
+<p id="nav">
+<a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a>
+<?php if (get_option('users_can_register')) : ?>
+ | <a href="<?php echo site_url('wp-login.php?action=register', 'login') ?>"><?php _e('Register') ?></a>
+<?php endif; ?>
+</p>
+
+<?php
+login_footer('user_pass');
</ins><span class="cx"> break;
</span><span class="cx">
</span><span class="cx"> case 'register' :
</span><span class="lines">@@ -485,17 +521,8 @@
</span><span class="cx"> <a href="<?php echo site_url('wp-login.php?action=lostpassword', 'login') ?>" title="<?php _e('Password Lost and Found') ?>"><?php _e('Lost your password?') ?></a>
</span><span class="cx"> </p>
</span><span class="cx">
</span><del>-</div>
-
-<p id="backtoblog"><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('&larr; Back to %s'), get_bloginfo('title', 'display' )); ?></a></p>
-
-<script type="text/javascript">
-try{document.getElementById('user_login').focus();}catch(e){}
-if(typeof wpOnload=='function')wpOnload();
-</script>
-</body>
-</html>
</del><span class="cx"> <?php
</span><ins>+login_footer('user_login');
</ins><span class="cx"> break;
</span><span class="cx">
</span><span class="cx"> case 'login' :
</span></span></pre>
</div>
</div>
</body>
</html>