<?php namespace ProcessWire;

/**
 * An Inputfield for handling a password
 * 
 * ProcessWire 3.x, Copyright 2016 by Ryan Cramer
 * https://processwire.com
 * 
 * @property array $requirements Array of requirements (See require* constants)
 * @property array $requirementsLabels Text labels used for requirements
 * @property float $complexifyFactor Complexify factor 
 * @property string $complexifyBanMode Complexify ban mode (default='loose')
 * @property bool $showPass Allow password to be rendered in renderValue and/or re-populated in form?
 * @property string $defaultLabel Default label for field (default='Set Password'). Used when no 'label' has been set.
 *
 */
class InputfieldPassword extends InputfieldText {

	public static function getModuleInfo() {
		return array(
			'title' => __('Password', __FILE__), // Module Title
			'summary' => __("Password input with confirmation field that doesn't ever echo the input back.", __FILE__), // Module Summary
			'version' => 101,
			'permanent' => true, 
			);
	}

	/**
	 * Requirements: letter required
	 * 
	 */
	const requireLetter = 'letter';
	
	/**
	 * Requirements: lowercase letter required
	 *
	 */
	const requireLowerLetter = 'lower';
	
	/**
	 * Requirements: uppercase letter required
	 *
	 */
	const requireUpperLetter = 'upper';
	
	/**
	 * Requirements: digit required
	 *
	 */
	const requireDigit = 'digit';
	
	/**
	 * Requirements: other character (symbol) required
	 *
	 */
	const requireOther = 'other';

	/**
	 * Page being edited, when applicable
	 * 
	 * @var User|Page|null
	 * 
	 */
	protected $_page = null;

	/**
	 * Construct and establish default settings
	 * 
	 */
	public function __construct() {
		parent::__construct();
		$this->attr('type', 'password'); 
		$this->attr('size', 30); 
		$this->attr('maxlength', 256); 
		$this->attr('minlength', 6); 
		$this->set('requirements', array(self::requireLetter, self::requireDigit));
		$this->set('complexifyFactor', 0.7);
		$this->set('complexifyBanMode', 'loose'); 
		$this->set('requirementsLabels', array(
			self::requireLetter => $this->_('letter'),
			self::requireLowerLetter => $this->_('lowercase letter'),
			self::requireUpperLetter => $this->_('uppercase letter'),
			self::requireDigit => $this->_('digit'),
			self::requireOther => $this->_('symbol/punctuation'), 
		));
		$this->set('showPass', false); // allow password to be rendered in renderValue and/or re-populated in form?
	}

	/**
	 * Init Inputfield, establishing the label if none has been set
	 * 
	 */
	public function init() {
		parent::init();
		$this->set('defaultLabel', $this->_('Set Password'));
		$this->label = $this->defaultLabel;
	}

	/**
	 * Sets the page being edited, not always applicable
	 * 
	 * @param Page $page
	 * 
	 */
	public function setPage(Page $page) { 
		$this->_page = $page; 
		if($page->hasStatus(Page::statusUnpublished)) $this->required = true; 
	}

	/**
	 * Called before render
	 * 
	 * @param Inputfield $parent
	 * @param bool $renderValueMode
	 * @return bool
	 * @throws WireException
	 * 
	 */
	public function renderReady(Inputfield $parent = null, $renderValueMode = false) {
		if($this->label == 'Set Password') $this->label = $this->defaultLabel;
		$config = $this->wire('config');
		$url = $config->urls->InputfieldPassword . 'complexify/';
		$config->scripts->add($url . 'jquery.complexify.min.js');
		$config->scripts->add($url . 'jquery.complexify.banlist.js');
		$this->wire('modules')->get('JqueryCore')->use('xregexp');
		$page = $this->wire('page');
		if($page && $page->template == 'admin') $this->attr('autocomplete', 'off'); // ProcessProfile and ProcessUser
		return parent::renderReady($parent, $renderValueMode);
	}

	/**
	 * Render Password input(s)
	 * 
	 * @return string
	 * 
	 */
	public function ___render() {
		
		$description = $this->getSetting('description');
		if($description) $this->notes = $description;
		
		$minlength = (int) $this->attr('minlength');
		$requirements = array();
		if($minlength) {
			$requirements['minlength'] = "[span.pass-require.pass-require-minlength]" . 
				sprintf($this->_('at least %d characters long'), $minlength) . "[/span]";
		}
		$labels = $this->getSetting('requirementsLabels');
		foreach($this->getSetting('requirements') as $name) {
			$requirements[$name] = "[span.pass-require.pass-require-$name]" . $labels[$name] . "[/span]";
		}
		if(isset($requirements['upper']) || isset($requirements['lower'])) {
			unset($requirements['letter']); 
		}
		if(count($requirements)) {
			$description = implode(", ", $requirements);
			$description = sprintf($this->_('Minimum requirements: %s.'), $description);
			$this->set('description', $description);
		}
	
		$value = $this->attr('value'); 
		$confirmValue = '';
		
		$trackChanges = $this->trackChanges();
		if($trackChanges) $this->setTrackChanges(false);
		
		if(!$this->getSetting('showPass')) {
			$this->attr('value', '');
		} else {
			$confirmValue = $this->wire('sanitizer')->entities($value);
		}
		
		$this->attr('data-banMode', $this->complexifyBanMode ? $this->complexifyBanMode : 'loose');
		$this->attr('data-factor', $this->complexifyFactor ? $this->complexifyFactor : '0.7');
		
		$inputClass = $this->wire('sanitizer')->entities($this->attr('class'));
		$this->addClass('InputfieldPasswordComplexify');
		
		$failIcon = "<i class='fa fa-fw fa-frown-o'></i>";
		$okIcon = "<i class='fa fa-fw fa-meh-o'></i>";
		$goodIcon = "<i class='fa fa-fw fa-smile-o'></i>";
		
		$out =	
			"<p>" . 
				"<input " . $this->getAttributesString() . " /> " . 
				"<span class='detail pass-scores' data-requirements='" . implode(' ', array_keys($requirements)) . "'>" .
					"<span class='pass-fail'>$failIcon" . $this->_('Not yet valid') . "</span>" .
					"<span class='pass-invalid'>$failIcon" . $this->_('Invalid') . "</span>" .
					"<span class='pass-short'>$failIcon" . $this->_('Too short') . "</span>" .
					"<span class='pass-common'>$failIcon" . $this->_('Too common') . "</span>" . 
					"<span class='pass-weak'>$okIcon" . $this->_('Weak') . "</span>" .
					"<span class='pass-medium'>$okIcon" . $this->_('Ok') . "</span>" .
					"<span class='pass-good'>$goodIcon" . $this->_('Good') . "</span>" .
					"<span class='pass-excellent'>$goodIcon" . $this->_('Excellent') . "</span>" . 
				"</span>" . 
			"</p>" . 
			"<p>" . 
				"<input class='InputfieldPasswordConfirm $inputClass' type='password' size='{$this->size}' name='_{$this->name}' value='$confirmValue' /> " . 
				"<span class='pass-confirm detail'>" . 
					"<span class='confirm-pending on'><i class='fa fa-fw fa-angle-left'></i>" . $this->_('Confirm') . "</span>" . 
					"<span class='confirm-yes'>$goodIcon" . $this->_('Matches') . "</span>" .
					"<span class='confirm-no'>$failIcon" . $this->_('Does not match') . "</span>" . 
					"<span class='confirm-qty'>$okIcon<span></span></span>" . 
				"</span>" . 
			"</p>";
		
		$this->attr('value', $value);
		if($trackChanges) $this->setTrackChanges(true);
		
		return $out; 
	}

	/**
	 * Set Inputfield setting
	 * 
	 * @param string $key
	 * @param mixed $value
	 * @return Inputfield|InputfieldPassword
	 * 
	 */
	public function set($key, $value) {
		if($key == 'collapsed' && $this->_page && $this->_page->hasStatus(Page::statusUnpublished)) {
			// prevent collapse of field when pass is for unpublished user
			$value = Inputfield::collapsedNo; 
		}
		return parent::set($key, $value);
	}

	/**
	 * Render non-editable Inputfield
	 * 
	 * @return string
	 * 
	 */
	public function ___renderValue() {
		if(!$this->getSetting('showPass')) {
			$value = strlen($this->attr('value')) ? '******' : '';
		} else {
			$value = $this->wire('sanitizer')->entities($this->attr('value'));
		}
		$value = strlen($value) ? "<p>$value</p>" : "";
		return $value; 
	}

	/**
	 * Process input
	 * 
	 * @param WireInputData $input
	 * @return $this
	 * 
	 */
	public function ___processInput(WireInputData $input) {

		parent::___processInput($input);
		
		$key = $this->attr('name'); 
		$value = $this->attr('value'); 
		if($value) {}

		if(isset($input->$key)) {
			// form was submitted
			$pass = $input->$key;

			if(strlen($pass)) { 
				// password was submitted
				
				$confirmKey = "_" . $key;

				if($input->$confirmKey != $pass) {
					$this->error($this->_("Passwords do not match"));
				}
				
				$this->isValidPassword($pass);
				
			} else if($this->required) {
				$this->error($this->_("Required password was not specified")); 
			}

			if(count($this->getErrors())) {
				$this->attr('value', ''); 
				$this->resetTrackChanges(); // don't record a change
			}
		}

		return $this;
	}

	/**
	 * Return whether or not the given password is valid according to configured requirements
	 * 
	 * Exact error messages can be retrieved with $this->getErrors().
	 * 
	 * @param string $value Password to validate
	 * @return bool
	 * 
	 */
	public function isValidPassword($value) {
		
		$numErrors = 0;
		$requirements = $this->getSetting('requirements');
		
		if(preg_match('/[\t\r\n]/', $value)) {
			$this->error($this->_("Password contained invalid whitespace"));
			$numErrors++;
		}

		if(strlen($value) < $this->attr('minlength')) {
			$this->error($this->_("Password is less than required number of characters"));
			$numErrors++;
		}

		if(in_array(self::requireLetter, $requirements)) {
			// if(!preg_match('/[a-zA-Z]/', $value)) {
			if(!preg_match('/\p{L}/', $value)) {
				$this->error($this->_("Password does not contain at least one letter (a-z A-Z)"));
				$numErrors++;
			}
		}

		if(in_array(self::requireLowerLetter, $requirements)) {
			if(!preg_match('/\p{Ll}/', $value)) {
				$this->error($this->_("Password must have at least one lowercase letter (a-z)"));
				$numErrors++;
			}
		}

		if(in_array(self::requireUpperLetter, $requirements)) {
			if(!preg_match('/\p{Lu}/', $value)) {
				$this->error($this->_("Password must have at least one uppercase letter (A-Z)"));
				$numErrors++;
			}
		}

		if(in_array(self::requireDigit, $requirements)) {
			if(!preg_match('/\p{N}/', $value)) {
				$this->error($this->_("Password does not contain at least one digit (0-9)"));
				$numErrors++;
			}
		}

		if(in_array(self::requireOther, $requirements)) {
			if(!preg_match('/\p{P}/', $value) && !preg_match('/\p{S}/', $value)) {
				$this->error($this->_("Password must have at least one non-letter, non-digit character (like punctuation)"));
				$numErrors++;
			}	
		}
		
		return $numErrors === 0;
	}

	/**
	 * Return the fields required to configure an instance of InputfieldPassword
	 * 
	 * @return InputfieldWrapper
	 *
	 */
	public function ___getConfigInputfields() {
		
		$inputfields = parent::___getConfigInputfields();
		$skips = array(
			'collapsed', 
			'showIf', 
			'placeholder', 
			'stripTags', 
			'pattern', 
			'visibility', 
			'minlength', 
			'maxlength', 
			'showCount',
			'size',
			);
		
		foreach($skips as $name) {
			$f = $inputfields->get($name);
			if($f) $inputfields->remove($f); 
		}
		
		$f = $this->wire('modules')->get('InputfieldCheckboxes'); 
		$f->attr('name', 'requirements');
		$f->label = $this->_('Password requirements');
		foreach($this->getSetting('requirementsLabels') as $value => $label) {
			$f->addOption($value, $label);
		}
		$f->attr('value', $this->getSetting('requirements')); 
		$f->columnWidth = 50;
		$inputfields->add($f);
		
		$f = $this->wire('modules')->get('InputfieldRadios');
		$f->attr('name', 'complexifyBanMode');
		$f->label = $this->_('Word ban mode');
		$f->description = $this->_('If you choose the strict mode, many passwords containing words will not be accepted.');
		$f->addOption('loose', $this->_('Ban just common passwords (recommended)'));
		$f->addOption('strict', $this->_('Ban all passwords containing any common words (strict)'));
		$f->attr('value', $this->complexifyBanMode);
		$f->columnWidth = 50;
		$inputfields->add($f);
		
		$f = $this->wire('modules')->get('InputfieldFloat');
		$f->attr('name', 'complexifyFactor');
		$f->label = $this->_('Complexify factor');
		$f->description = $this->_('Lower numbers allow weaker passwords, higher numbers require stronger passwords.');
		$f->notes = $this->_('We recommend something between 0.5 and 1.0');
		$f->attr('value', $this->complexifyFactor);
		$f->columnWidth = 50;
		$inputfields->add($f);

		$f = $this->wire('modules')->get('InputfieldInteger');
		$f->attr('name', 'minlength'); 
		$f->label = $this->_('Minimum password length'); 
		$f->attr('value', $this->attr('minlength'));
		$f->columnWidth = 50;
		$inputfields->add($f);
		
		if(!$this->getSetting('hasFieldtype')) {
			$f = $this->wire('modules')->get('InputfieldCheckbox');
			$f->attr('name', 'showPass');
			$f->label = $this->_('Allow existing passwords to be shown and/or rendered in form?');
			if($this->getSetting("showPass")) $f->attr('checked', 'checked');
			$inputfields->add($f);
		}
		
		return $inputfields; 
	}
}
