/
InputfieldPassword.module
123 lines (101 loc) · 3.79 KB
/
InputfieldPassword.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* An Inputfield for handling a password
*
*/
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,
);
}
protected $_page = null;
public function __construct() {
parent::__construct();
$this->attr('type', 'password');
$this->attr('size', 50);
$this->attr('maxlength', 256);
$this->set('minlength', 6);
}
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;
}
public function renderReady(Inputfield $parent = null, $renderValueMode = false) {
if($this->label == 'Set Password') $this->label = $this->defaultLabel;
return parent::renderReady($parent, $renderValueMode);
}
public function ___render() {
if(!$this->description) $this->set('description', sprintf($this->_("Password must be at least %d characters and have at least 1 letter and 1 digit. Password may not have whitespace."), $this->minlength)); // Description of valid password
$value = $this->attr('value');
$trackChanges = $this->trackChanges();
if($trackChanges) $this->setTrackChanges(false);
$this->attr('value', '');
$out = "\n<p><input " . $this->getAttributesString() . " /></p>" .
"\n<p><input type='password' size='{$this->size}' name='_{$this->name}' value='' /> <span class='detail'>" . $this->_('(confirm)') . "</span></p>";
$this->attr('value', $value);
if($trackChanges) $this->setTrackChanges(true);
return $out;
}
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);
}
public function ___renderValue() {
return strlen($this->attr('value')) ? '******' : '';
}
public function ___processInput(WireInputData $input) {
parent::___processInput($input);
$key = $this->attr('name');
$value = $this->attr('value');
if(isset($input->$key)) {
// form was submitted
if($input->$key) {
// password was submitted
$confirmKey = "_" . $key;
if(preg_match('/[\s\t\r\n]/', $value)) $this->error($this->_("Password contained whitespace."));
if($input->$confirmKey != $input->$key) $this->error($this->_("Passwords do not match."));
if(strlen($value) < $this->minlength) $this->error($this->_("Password is less than required number of characters."));
if(!preg_match('/[a-zA-Z]/', $value)) $this->error($this->_("Password does not contain at least one letter (a-z A-Z)."));
if(!preg_match('/\d/', $value)) $this->error($this->_("Password does not contain at least one digit (0-9)."));
} 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 the fields required to configure an instance of InputfieldPassword
*
*/
public function ___getConfigInputfields() {
$inputfields = parent::___getConfigInputfields();
$skips = array('collapsed', 'placeholder', 'stripTags', 'pattern');
foreach($skips as $name) {
$f = $inputfields->get($name);
if($f) $inputfields->remove($f);
}
return $inputfields;
}
}