query("SELECT id, user FROM tbl_users WHERE user = '$got_user'"); $result_user_id = mysql_fetch_array($query_user_id); $got_user_id = $result_user_id['id']; $sql_request = $database->query("SELECT * FROM tbl_password_reset WHERE BINARY token = '$got_token' AND user_id = '$got_user_id'"); $count_request = mysql_num_rows($sql_request); if ($count_request > 0){ $token_info = mysql_fetch_array($sql_request); $request_id = $token_info['id']; /** Check if the token has been used already */ if ($token_info['used'] == '1') { /** Clean the ID to fix security holes */ $got_user_id = ''; $errorstate = 'token_used'; } /** Check if the token has expired. */ elseif (time() - strtotime($token_info['timestamp']) > 60*60*24) { $got_user_id = ''; $errorstate = 'token_expired'; } else { $show_form = 'enter_new_password'; } } else { $got_user_id = ''; $errorstate = 'token_invalid'; $show_form = 'none'; } } /** The form was submitted */ if ($_POST) { /** * Clean the posted form values. */ $form_type = encode_html($_POST['form_type']); switch ($form_type) { /** * The form submited contains a new token request */ case 'new_request': $reset_password_email = encode_html($_POST['reset_password_email']); $sql_user = $database->query("SELECT id, user, email FROM tbl_users WHERE email='$reset_password_email'"); $count_user = mysql_num_rows($sql_user); if ($count_user > 0){ /** Email exists on the database */ $row = mysql_fetch_array($sql_user); $id = $row['id']; $username = $row['user']; $email = $row['email']; $token = generateRandomString(32); /** * Count how many request were made by this user today. * No more than 3 unused should exist at a time. */ $sql_amount = $database->query("SELECT * FROM tbl_password_reset WHERE user_id = '$id' AND used = '0' AND timestamp > NOW() - INTERVAL 1 DAY"); $count_requests = mysql_num_rows($sql_amount); if ($count_requests >= 3){ $errorstate = 'too_many_today'; } else { $sql_pass = $database->query("INSERT INTO tbl_password_reset (user_id, token)" ."VALUES ('$id', '$token' )"); /** Send email */ $notify_user = new PSend_Email(); $email_arguments = array( 'type' => 'password_reset', 'address' => $email, 'username' => $username, 'token' => $token ); $notify_send = $notify_user->psend_send_email($email_arguments); if ($notify_send == 1){ $state['email'] = 1; } else { $state['email'] = 0; } } $show_form = 'none'; } else { $errorstate = 'email_not_found'; } break; /** * The form submited contains the new password */ case 'new_password': if (!empty($got_user_id)) { $reset_password_new = mysql_real_escape_string($_POST['reset_password_new']); /** Password checks */ $valid_me->validate('completed',$reset_password_new,$validation_no_pass); $valid_me->validate('password',$reset_password_new,$validation_valid_pass.' '.$validation_valid_chars); $valid_me->validate('pass_rules',$reset_password_new,$validation_rules_pass); $valid_me->validate('length',$reset_password_new,$validation_length_pass,MIN_PASS_CHARS,MAX_PASS_CHARS); if ($valid_me->return_val) { $enc_password = $hasher->HashPassword($reset_password_new); if (strlen($enc_password) >= 20) { $state['hash'] = 1; /** SQL queries */ $edit_pass_query = "UPDATE tbl_users SET password = '$enc_password' WHERE id = $got_user_id"; $sql_query = $database->query($edit_pass_query); if ($sql_query) { $state['reset'] = 1; $set_used_query = "UPDATE tbl_password_reset SET used = '1' WHERE id = $request_id"; $sql_query = $database->query($set_used_query); $show_form = 'none'; } else { $state['reset'] = 0; } } else { $state['hash'] = 0; } } } break; } } ?>

list_errors(); ?>

Close(); ob_end_flush(); ?>