2007-09-11 02:26:55 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-06-26 15:03:11 +02:00
|
|
|
* Two masked input fields, checks for matching passwords.
|
2007-12-19 02:45:22 +01:00
|
|
|
* Optionally hides the fields by default and shows
|
|
|
|
* a link to toggle their visibility.
|
2008-04-05 01:15:42 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
2007-09-11 02:26:55 +02:00
|
|
|
*/
|
|
|
|
class ConfirmedPasswordField extends FormField {
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Minimum character length of the password.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2008-04-26 08:35:50 +02:00
|
|
|
public $minLength = null;
|
2007-09-11 02:26:55 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Maximum character length of the password.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2008-04-26 08:35:50 +02:00
|
|
|
public $maxLength = null;
|
2007-09-11 02:26:55 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Enforces at least one digit and one alphanumeric
|
|
|
|
* character (in addition to {$minLength} and {$maxLength}
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2007-09-11 02:26:55 +02:00
|
|
|
public $requireStrongPassword = false;
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Allow empty fields in serverside validation
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2007-09-11 02:26:55 +02:00
|
|
|
public $canBeEmpty = false;
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
2008-11-22 04:33:00 +01:00
|
|
|
* If set to TRUE, the "password" and "confirm password"
|
|
|
|
* formfields will be hidden via CSS and JavaScript by default,
|
|
|
|
* and triggered by a link. An additional hidden field
|
|
|
|
* determines if showing the fields has been triggered,
|
|
|
|
* and just validates/saves the input in this case.
|
|
|
|
* This behaviour works unobtrusively, without JavaScript enabled
|
|
|
|
* the fields show, validate and save by default.
|
|
|
|
*
|
|
|
|
* @param boolean $showOnClick
|
2007-12-19 02:45:22 +01:00
|
|
|
*/
|
|
|
|
protected $showOnClick = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Title for the link that triggers
|
|
|
|
* the visibility of password fields.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2008-11-22 04:33:00 +01:00
|
|
|
public $showOnClickTitle;
|
2007-12-19 02:45:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param string $title
|
|
|
|
* @param mixed $value
|
|
|
|
* @param Form $form
|
|
|
|
* @param boolean $showOnClick
|
2008-04-09 13:29:29 +02:00
|
|
|
* @param string $titleConfirmField Alternate title (not localizeable)
|
2007-12-19 02:45:22 +01:00
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
public function __construct($name, $title = null, $value = "", $form = null, $showOnClick = false,
|
|
|
|
$titleConfirmField = null) {
|
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
// naming with underscores to prevent values from actually being saved somewhere
|
2011-05-11 09:51:54 +02:00
|
|
|
$this->children = new FieldList(
|
2008-04-09 13:29:29 +02:00
|
|
|
new PasswordField(
|
|
|
|
"{$name}[_Password]",
|
2010-12-08 23:07:37 +01:00
|
|
|
(isset($title)) ? $title : _t('Member.PASSWORD', 'Password')
|
|
|
|
),
|
2008-04-09 13:29:29 +02:00
|
|
|
new PasswordField(
|
|
|
|
"{$name}[_ConfirmPassword]",
|
|
|
|
(isset($titleConfirmField)) ? $titleConfirmField : _t('Member.CONFIRMPASSWORD', 'Confirm Password')
|
|
|
|
)
|
2007-09-11 02:26:55 +02:00
|
|
|
);
|
2008-11-22 04:33:00 +01:00
|
|
|
|
|
|
|
// has to be called in constructor because Field() isn't triggered upon saving the instance
|
|
|
|
if($showOnClick) {
|
2007-12-19 02:45:22 +01:00
|
|
|
$this->children->push(new HiddenField("{$name}[_PasswordFieldVisible]"));
|
|
|
|
}
|
2008-11-22 04:33:00 +01:00
|
|
|
$this->showOnClick = $showOnClick;
|
2007-12-19 02:45:22 +01:00
|
|
|
|
|
|
|
// we have labels for the subfields
|
|
|
|
$title = false;
|
2007-09-11 02:26:55 +02:00
|
|
|
|
2009-06-27 06:00:10 +02:00
|
|
|
parent::__construct($name, $title, null, $form);
|
|
|
|
$this->setValue($value);
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/javascript/ConfirmedPasswordField.js');
|
|
|
|
Requirements::css(FRAMEWORK_DIR . '/css/ConfirmedPasswordField.css');
|
2007-12-19 02:45:22 +01:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
$content = '';
|
2007-12-19 02:45:22 +01:00
|
|
|
|
|
|
|
if($this->showOnClick) {
|
2008-11-22 04:33:00 +01:00
|
|
|
if($this->showOnClickTitle) {
|
|
|
|
$title = $this->showOnClickTitle;
|
|
|
|
} else {
|
|
|
|
$title = _t(
|
|
|
|
'ConfirmedPasswordField.SHOWONCLICKTITLE',
|
|
|
|
'Change Password',
|
2012-04-14 00:16:22 +02:00
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
'Label of the link which triggers display of the "change password" formfields'
|
|
|
|
);
|
|
|
|
}
|
2007-12-19 02:45:22 +01:00
|
|
|
|
|
|
|
$content .= "<div class=\"showOnClick\">\n";
|
2012-03-07 17:45:14 +01:00
|
|
|
$content .= "<a href=\"#\">{$title}</a>\n";
|
2007-12-19 02:45:22 +01:00
|
|
|
$content .= "<div class=\"showOnClickContainer\">";
|
|
|
|
}
|
2008-11-22 04:33:00 +01:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
foreach($this->children as $field) {
|
2009-04-29 01:55:53 +02:00
|
|
|
$field->setDisabled($this->isDisabled());
|
|
|
|
$field->setReadonly($this->isReadonly());
|
2012-12-11 22:57:45 +01:00
|
|
|
if(count($this->attributes)) {
|
|
|
|
foreach($this->attributes as $name => $value) {
|
|
|
|
$field->setAttribute($name, $value);
|
|
|
|
}
|
|
|
|
}
|
2007-12-19 02:45:22 +01:00
|
|
|
$content .= $field->FieldHolder();
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->showOnClick) {
|
|
|
|
$content .= "</div>\n";
|
|
|
|
$content .= "</div>\n";
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Can be empty is a flag that turns on/off empty field checking.
|
|
|
|
* For example, set this to false (the default) when creating a user account,
|
|
|
|
* and true
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setCanBeEmpty($value) {
|
2007-09-11 02:26:55 +02:00
|
|
|
$this->canBeEmpty = (bool)$value;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
/**
|
|
|
|
* The title on the link which triggers display of the
|
|
|
|
* "password" and "confirm password" formfields.
|
|
|
|
* Only used if {@link setShowOnClick()} is set to TRUE.
|
|
|
|
*
|
|
|
|
* @param $title
|
|
|
|
*/
|
|
|
|
public function setShowOnClickTitle($title) {
|
|
|
|
$this->showOnClickTitle = $title;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-11-22 04:33:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getShowOnClickTitle() {
|
|
|
|
return $this->showOnClickTitle;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setRightTitle($title) {
|
2007-09-11 02:26:55 +02:00
|
|
|
foreach($this->children as $field) {
|
|
|
|
$field->setRightTitle($title);
|
|
|
|
}
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
|
|
|
|
2009-06-23 09:47:45 +02:00
|
|
|
/**
|
|
|
|
* @param array: 2 entrie array with the customised title for each of the 2 children.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setChildrenTitles($titles) {
|
2009-06-23 09:47:45 +02:00
|
|
|
if(is_array($titles)&&count($titles)==2){
|
2009-06-23 09:46:12 +02:00
|
|
|
foreach($this->children as $field) {
|
|
|
|
if(isset($titles[0])){
|
|
|
|
$field->setTitle($titles[0]);
|
|
|
|
array_shift($titles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2009-06-23 09:46:12 +02:00
|
|
|
}
|
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
/**
|
|
|
|
* Value is sometimes an array, and sometimes a single value, so we need to handle both cases
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setValue($value) {
|
2007-09-11 02:26:55 +02:00
|
|
|
if(is_array($value)) {
|
|
|
|
if($value['_Password'] || (!$value['_Password'] && !$this->canBeEmpty)) {
|
|
|
|
$this->value = $value['_Password'];
|
|
|
|
}
|
2011-04-25 12:44:54 +02:00
|
|
|
if($this->showOnClick && isset($value['_PasswordFieldVisible'])){
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->children->fieldByName($this->getName() . '[_PasswordFieldVisible]')
|
|
|
|
->setValue($value['_PasswordFieldVisible']);
|
2008-05-24 03:22:27 +02:00
|
|
|
}
|
2007-09-11 02:26:55 +02:00
|
|
|
} else {
|
2011-03-18 23:10:34 +01:00
|
|
|
if($value || (!$value && $this->canBeEmpty)) {
|
2007-09-11 02:26:55 +02:00
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
}
|
2011-10-29 06:01:52 +02:00
|
|
|
$this->children->fieldByName($this->getName() . '[_Password]')->setValue($this->value);
|
|
|
|
$this->children->fieldByName($this->getName() . '[_ConfirmPassword]')->setValue($this->value);
|
2012-02-17 13:35:26 +01:00
|
|
|
|
|
|
|
return $this;
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Determines if the field was actually
|
|
|
|
* shown on the clientside - if not,
|
|
|
|
* we don't validate or save it.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isSaveable() {
|
2011-10-29 06:01:52 +02:00
|
|
|
$isVisible = $this->children->fieldByName($this->getName() . '[_PasswordFieldVisible]');
|
2007-12-19 02:45:22 +01:00
|
|
|
return (!$this->showOnClick || ($this->showOnClick && $isVisible && $isVisible->Value()));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function validate($validator) {
|
2007-09-11 02:26:55 +02:00
|
|
|
$name = $this->name;
|
2007-12-19 02:45:22 +01:00
|
|
|
|
|
|
|
// if field isn't visible, don't validate
|
|
|
|
if(!$this->isSaveable()) return true;
|
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
$passwordField = $this->children->fieldByName($name.'[_Password]');
|
|
|
|
$passwordConfirmField = $this->children->fieldByName($name.'[_ConfirmPassword]');
|
|
|
|
$passwordField->setValue($_POST[$name]['_Password']);
|
|
|
|
$passwordConfirmField->setValue($_POST[$name]['_ConfirmPassword']);
|
2007-12-19 02:45:22 +01:00
|
|
|
|
|
|
|
$value = $passwordField->Value();
|
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
// both password-fields should be the same
|
2007-12-19 02:45:22 +01:00
|
|
|
if($value != $passwordConfirmField->Value()) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$validator->validationError($name, _t('Form.VALIDATIONPASSWORDSDONTMATCH',"Passwords don't match"),
|
|
|
|
"validation", false);
|
2007-09-11 02:26:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$this->canBeEmpty) {
|
|
|
|
// both password-fields shouldn't be empty
|
2007-12-19 02:52:11 +01:00
|
|
|
if(!$value || !$passwordConfirmField->Value()) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$validator->validationError($name, _t('Form.VALIDATIONPASSWORDSNOTEMPTY', "Passwords can't be empty"),
|
|
|
|
"validation", false);
|
2007-09-11 02:26:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lengths
|
2007-12-19 02:45:22 +01:00
|
|
|
if(($this->minLength || $this->maxLength)) {
|
2007-09-11 02:26:55 +02:00
|
|
|
if($this->minLength && $this->maxLength) {
|
2008-09-12 07:40:55 +02:00
|
|
|
$limit = "{{$this->minLength},{$this->maxLength}}";
|
2012-05-01 21:44:54 +02:00
|
|
|
$errorMsg = _t(
|
|
|
|
'ConfirmedPasswordField.BETWEEN',
|
|
|
|
'Passwords must be {min} to {max} characters long.',
|
|
|
|
array('min' => $this->minLength, 'max' => $this->maxLength)
|
|
|
|
);
|
2007-09-11 02:26:55 +02:00
|
|
|
} elseif($this->minLength) {
|
2008-09-12 07:40:55 +02:00
|
|
|
$limit = "{{$this->minLength}}.*";
|
2012-05-01 21:44:54 +02:00
|
|
|
$errorMsg = _t(
|
|
|
|
'ConfirmedPasswordField.ATLEAST',
|
|
|
|
'Passwords must be at least {min} characters long.',
|
|
|
|
array('min' => $this->minLength)
|
|
|
|
);
|
2007-09-11 02:26:55 +02:00
|
|
|
} elseif($this->maxLength) {
|
2008-09-12 07:40:55 +02:00
|
|
|
$limit = "{0,{$this->maxLength}}";
|
2012-05-01 21:44:54 +02:00
|
|
|
$errorMsg = _t(
|
|
|
|
'ConfirmedPasswordField.MAXIMUM',
|
|
|
|
'Passwords must be at most {max} characters long.',
|
|
|
|
array('max' => $this->maxLength)
|
|
|
|
);
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
2008-09-12 07:40:55 +02:00
|
|
|
$limitRegex = '/^.' . $limit . '$/';
|
2007-12-19 02:45:22 +01:00
|
|
|
if(!empty($value) && !preg_match($limitRegex,$value)) {
|
2007-09-11 02:26:55 +02:00
|
|
|
$validator->validationError('Password', $errorMsg,
|
|
|
|
"validation",
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->requireStrongPassword) {
|
2007-12-19 02:45:22 +01:00
|
|
|
if(!preg_match('/^(([a-zA-Z]+\d+)|(\d+[a-zA-Z]+))[a-zA-Z0-9]*$/',$value)) {
|
2007-09-11 02:26:55 +02:00
|
|
|
$validator->validationError(
|
|
|
|
'Password',
|
2012-09-26 23:34:00 +02:00
|
|
|
_t('Form.VALIDATIONSTRONGPASSWORD',
|
|
|
|
"Passwords must have at least one digit and one alphanumeric character"),
|
2007-09-11 02:26:55 +02:00
|
|
|
"validation",
|
|
|
|
false
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-11-12 03:58:27 +01:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Only save if field was shown on the client,
|
|
|
|
* and is not empty.
|
|
|
|
*
|
|
|
|
* @param DataObject $record
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function saveInto(DataObjectInterface $record) {
|
2007-12-19 02:45:22 +01:00
|
|
|
if(!$this->isSaveable()) return false;
|
|
|
|
|
2007-11-12 03:58:27 +01:00
|
|
|
if(!($this->canBeEmpty && !$this->value)) {
|
|
|
|
parent::saveInto($record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a pretty readonly field with some stars in it
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2007-11-12 03:58:27 +01:00
|
|
|
$stars = '*****';
|
|
|
|
|
|
|
|
$field = new ReadonlyField($this->name, $this->title ? $this->title : _t('Member.PASSWORD'), $stars);
|
|
|
|
$field->setForm($this->form);
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
}
|