<commit_before><?php
namespace wcf\form;
use wcf\data\user\User;
use wcf\data\user\UserAction;
use wcf\data\user\UserEditor;
use wcf\system\exception\IllegalLinkException;
use wcf\system\exception\NamedUserException;
use wcf\system\exception\PermissionDeniedException;
use wcf\system\exception\UserInputException;
use wcf\system\request\LinkHandler;
use wcf\system\WCF;
use wcf\util\HeaderUtil;
use wcf\util\StringUtil;
use wcf\util\UserRegistrationUtil;

/**
 * Shows the new password form.
 * 
 * @author	<NAME>
 * @copyright	2001-2019 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\Form
 */
class NewPasswordForm extends AbstractForm {
	const AVAILABLE_DURING_OFFLINE_MODE = true;
	
	/**
	 * user id
	 * @var	integer
	 */
	public $userID = 0;
	
	/**
	 * lost password key
	 * @var	string
	 */
	public $lostPasswordKey = '';
	
	/**
	 * User object
	 * @var	User
	 */
	public $user;
	
	/**
	 * new password
	 * @var	string
	 */
	public $newPassword = '';
	
	/**
	 * confirmed new password
	 * @var	string
	 */
	public $confirmNewPassword = '';
	
	/**
	 * @inheritDoc
	 */
	public function readParameters() {
		parent::readParameters();
		
		if (isset($_GET['id']) && isset($_GET['k'])) {
			$this->userID = intval($_GET['id']);
			$this->lostPasswordKey = StringUtil::trim($_GET['k']);
			if (!$this->userID || !$this->lostPasswordKey) throw new IllegalLinkException();
			
			$this->user = new User($this->userID);
			if (!$this->user->userID) throw new IllegalLinkException();
			
			if (!$this->user->lostPasswordKey) {
				$this->throwInvalidLinkException(); 
			}
			if (!\hash_equals($this->user->lostPasswordKey, $this->lostPasswordKey)) {
				$this->throwInvalidLinkException();
			}
			// expire lost password requests after a day
			if ($this->user->lastLostPasswordRequestTime < TIME_NOW - 86400) {
				$this->throwInvalidLinkException();
			}
			
			(new UserEditor($this->user))->update([
				'lastLostPasswordRequestTime' => 0,
				'lostPasswordKey' => null
			]);
			WCF::getSession()->register('lostPasswordRequest', $this->user->userID);
		}
		else {
			if (!WCF::getSession()->getVar('lostPasswordRequest')) throw new PermissionDeniedException();
			$this->userID = intval(WCF::getSession()->getVar('lostPasswordRequest'));
			
			$this->user = new User($this->userID);
			if (!$this->user->userID) throw new IllegalLinkException();
		}
	}
	
	/**
	 * @inheritDoc
	 */
	public function readFormParameters() {
		parent::readFormParameters();
		
		if (isset($_POST['newPassword'])) $this->newPassword = $_POST['newPassword'];
		if (isset($_POST['confirmNewPassword'])) $this->confirmNewPassword = $_POST['confirmNewPassword'];
	}
	
	/**
	 * @inheritDoc
	 */
	public function validate() {
		parent::validate();
		
		if (empty($this->newPassword)) {
			throw new UserInputException('newPassword');
		}
		
		if (empty($this->confirmNewPassword)) {
			throw new UserInputException('confirmNewPassword');<commit_msg>Allow multiple calls of the link to set a new password See #2923<commit_after><?php
namespace wcf\form;
use wcf\data\user\User;
use wcf\data\user\UserAction;
use wcf\system\exception\IllegalLinkException;
use wcf\system\exception\NamedUserException;
use wcf\system\exception\PermissionDeniedException;
use wcf\system\exception\UserInputException;
use wcf\system\request\LinkHandler;
use wcf\system\WCF;
use wcf\util\HeaderUtil;
use wcf\util\StringUtil;
use wcf\util\UserRegistrationUtil;

/**
 * Shows the new password form.
 * 
 * @author	<NAME>
 * @copyright	2001-2019 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\Form
 */
class NewPasswordForm extends AbstractForm {
	const AVAILABLE_DURING_OFFLINE_MODE = true;
	
	/**
	 * user id
	 * @var	integer
	 */
	public $userID = 0;
	
	/**
	 * lost password key
	 * @var	string
	 */
	public $lostPasswordKey = '';
	
	/**
	 * User object
	 * @var	User
	 */
	public $user;
	
	/**
	 * new password
	 * @var	string
	 */
	public $newPassword = '';
	
	/**
	 * confirmed new password
	 * @var	string
	 */
	public $confirmNewPassword = '';
	
	/**
	 * @inheritDoc
	 */
	public function readParameters() {
		parent::readParameters();
		
		if (isset($_GET['id']) && isset($_GET['k'])) {
			$this->userID = intval($_GET['id']);
			$this->lostPasswordKey = StringUtil::trim($_GET['k']);
			if (!$this->userID || !$this->lostPasswordKey) throw new IllegalLinkException();
			
			$this->user = new User($this->userID);
			if (!$this->user->userID) throw new IllegalLinkException();
			
			if (!$this->user->lostPasswordKey) {
				$this->throwInvalidLinkException(); 
			}
			if (!\hash_equals($this->user->lostPasswordKey, $this->lostPasswordKey)) {
				$this->throwInvalidLinkException();
			}
			// expire lost password requests after a day
			if ($this->user->lastLostPasswordRequestTime < TIME_NOW - 86400) {
				$this->throwInvalidLinkException();
			}
			
			WCF::getSession()->register('lostPasswordRequest', [
				'userID' => $this->user->userID,
				'key' => $this->user->lostPasswordKey
			]);
		}
		else {
			if (!is_array(WCF::getSession()->getVar('lostPasswordRequest'))) throw new PermissionDeniedException();
			$this->userID = intval(WCF::getSession()->getVar('lostPasswordRequest')['userID']);
			
			$this->user = new User($this->userID);
			if (!$this->user->userID) throw new IllegalLinkException();
			if (!\hash_equals($this->user->lostPasswordKey, WCF::getSession()->getVar('lostPasswordRequest')['key'])) {
				$this->throwInvalidLinkException();
			}
		}
	}
	
	/**
	 * @inheritDoc
	 */
	public function readFormParameters() {
		parent::readFormParameters();
		
		if (isset($_POST['newPassword'])) $this->newPassword = $_POST['newPassword'];
		if (isset($_POST['confirmNewPassword'])) $this->confirmNewPassword = $_POST['confirmNewPassword'];
	}
	
	/**
	 * @inheritDoc
	 */
	public function validate() {
		parent::validate();
		
		if (empty($this->newPassword)) {
			throw new UserInputException('newPassword');
		}
		
		if (empty($this->confirmNewPassword)) {
			throw new UserInputException('confirmNewPassword');
{'fraction_non_alphanumeric': 0.16437469665102733, 'fraction_numerical': 0.005338941918783369, 'mean_word_length': 4.186241610738255, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3831766', 'n_tokens_mistral': 2239, 'n_tokens_neox': 1919, 'n_words': 378}