]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/recoverpassword.php
add Net Socket
[quix0rs-gnu-social.git] / actions / recoverpassword.php
index 0d85163d74d03bbaf0b640ff7773c108a6df51c1..38c42f41d1b056ac303f723dd324ce7391d3fd82 100644 (file)
@@ -25,10 +25,6 @@ define(MAX_RECOVERY_TIME, 24 * 60 * 60);
 
 class RecoverpasswordAction extends Action {
 
-       function is_readonly() {
-               return false;
-       }
-
     function handle($args) {
         parent::handle($args);
         if (common_logged_in()) {
@@ -54,7 +50,7 @@ class RecoverpasswordAction extends Action {
        function check_code() {
 
                $code = $this->trimmed('code');
-               $confirm = Confirm_address::staticGet($code);
+               $confirm = Confirm_address::staticGet('code', $code);
 
                if (!$confirm) {
                        $this->client_error(_('No such recovery code.'));
@@ -73,6 +69,7 @@ class RecoverpasswordAction extends Action {
                }
 
                $touched = strtotime($confirm->modified);
+               $email = $confirm->address;
 
                # Burn this code
 
@@ -88,11 +85,28 @@ class RecoverpasswordAction extends Action {
                # Note: it's still deleted; let's avoid a second attempt!
 
                if ((time() - $touched) > MAX_RECOVERY_TIME) {
+                       common_log(LOG_WARNING, 
+                                          'Attempted redemption on recovery code ' .
+                                          'that is ' . $touched . ' seconds old. ');
                        $this->client_error(_('This confirmation code is too old. ' .
                                               'Please start again.'));
                        return;
                }
 
+               # If we used an outstanding confirmation to send the email,
+               # it's been confirmed at this point.
+
+               if (!$user->email) {
+                       $orig = clone($user);
+                       $user->email = $email;
+                       $result = $user->updateKeys($orig);
+                       if (!$result) {
+                               common_log_db_error($user, 'UPDATE', __FILE__);
+                               $this->server_error(_('Could not update user with confirmed email address.'));
+                               return;
+                       }
+               }
+
                # Success!
 
                $this->set_temp_user($user);
@@ -122,11 +136,13 @@ class RecoverpasswordAction extends Action {
                if ($msg) {
             common_element('div', 'error', $msg);
                } else {
-                       common_element('div', 'instructions',
+                       common_element_start('div', 'instructions');
+                       common_element('p', NULL, 
                                                   _('If you\'ve forgotten or lost your' .
-                                                     ' password, you can get a new one sent ' .
+                                                     ' password, you can get a new one sent to' .
                                                      ' the email address you have stored ' .
                                                      ' in your account.'));
+                       common_element_end('div');
                }
        }
 
@@ -165,6 +181,7 @@ class RecoverpasswordAction extends Action {
                common_element_start('form', array('method' => 'post',
                                                                                   'id' => 'recoverpassword',
                                                                                   'action' => common_local_url('recoverpassword')));
+               common_hidden('token', common_session_token());
                common_password('newpassword', _('New password'),
                                                _('6 or more characters, and don\'t forget it!'));
                common_password('confirm', _('Confirm'),
@@ -180,25 +197,49 @@ class RecoverpasswordAction extends Action {
                        $this->show_form(_('Enter a nickname or email address.'));
                        return;
                }
+
                $user = User::staticGet('email', common_canonical_email($nore));
+
                if (!$user) {
                        $user = User::staticGet('nickname', common_canonical_nickname($nore));
                }
 
+               # See if it's an unconfirmed email address
+
                if (!$user) {
-                       $this->show_form(_('No such user.'));
+                       $confirm_email = Confirm_address::staticGet('address', common_canonical_email($nore));
+                       if ($confirm_email && $confirm_email->address_type == 'email') {
+                               $user = User::staticGet($confirm_email->user_id);
+                       }
+               }
+
+               if (!$user) {
+                       $this->show_form(_('No user with that email address or username.'));
                        return;
                }
-               if (!$user->email) {
+
+               # Try to get an unconfirmed email address if they used a user name
+
+               if (!$user->email && !$confirm_email) {
+                       $confirm_email = Confirm_address::staticGet('user_id', $user->id);
+                       if ($confirm_email && $confirm_email->address_type != 'email') {
+                               # Skip non-email confirmations
+                               $confirm_email = NULL;
+                       }
+               }
+
+               if (!$user->email && !$confirm_email) {
                        $this->client_error(_('No registered email address for that user.'));
                        return;
                }
 
+               # Success! We have a valid user and a confirmed or unconfirmed email address
+
                $confirm = new Confirm_address();
                $confirm->code = common_confirmation_code(128);
                $confirm->address_type = 'recover';
                $confirm->user_id = $user->id;
-               $confirm->address = $user->email;
+               $confirm->address = (isset($user->email)) ? $user->email : $confirm_email->address;
 
                if (!$confirm->insert()) {
                        common_log_db_error($confirm, 'INSERT', __FILE__);
@@ -223,7 +264,7 @@ class RecoverpasswordAction extends Action {
                $body .= common_config('site', 'name');
                $body .= "\n";
 
-               mail_to_user($user, _('Password recovery requested'), $body);
+               mail_to_user($user, _('Password recovery requested'), $body, $confirm->address);
 
                common_show_header(_('Password recovery requested'));
                common_element('p', NULL,
@@ -235,6 +276,13 @@ class RecoverpasswordAction extends Action {
 
        function reset_password() {
 
+               # CSRF protection
+               $token = $this->trimmed('token');
+               if (!$token || $token != common_session_token()) {
+                       $this->show_form(_('There was a problem with your session token. Try again, please.'));
+                       return;
+               }
+
                $user = $this->get_temp_user();
 
                if (!$user) {