This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
/
FormPassword.php
208 lines (175 loc) · 4.25 KB
/
FormPassword.php
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
/**
* Class FormPassword
*
* @property boolean $mandatory
* @property integer $maxlength
* @property string $placeholder
* @property string $confirmLabel
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class FormPassword extends \Widget
{
/**
* Submit user input
*
* @var boolean
*/
protected $blnSubmitInput = true;
/**
* Add a for attribute
*
* @var boolean
*/
protected $blnForAttribute = true;
/**
* Template
*
* @var string
*/
protected $strTemplate = 'form_password';
/**
* The CSS class prefix
*
* @var string
*/
protected $strPrefix = 'widget widget-password';
/**
* Always decode entities
*
* @param array $arrAttributes An optional attributes array
*/
public function __construct($arrAttributes=null)
{
parent::__construct($arrAttributes);
$this->decodeEntities = true;
}
/**
* Add specific attributes
*
* @param string $strKey The attribute name
* @param mixed $varValue The attribute value
*/
public function __set($strKey, $varValue)
{
switch ($strKey)
{
case 'maxlength':
if ($varValue > 0)
{
$this->arrAttributes['maxlength'] = $varValue;
}
break;
case 'mandatory':
if ($varValue)
{
$this->arrAttributes['required'] = 'required';
}
else
{
unset($this->arrAttributes['required']);
}
parent::__set($strKey, $varValue);
break;
case 'placeholder':
$this->arrAttributes['placeholder'] = $varValue;
break;
default:
parent::__set($strKey, $varValue);
break;
}
}
/**
* Validate input and set value
*
* @param mixed $varInput The user input
*
* @return mixed The validated user input
*/
protected function validator($varInput)
{
$this->blnSubmitInput = false;
if (!strlen($varInput) && (strlen($this->varValue) || !$this->mandatory))
{
return '';
}
if (utf8_strlen($varInput) < \Config::get('minPasswordLength'))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['passwordLength'], \Config::get('minPasswordLength')));
}
if ($varInput != $this->getPost($this->strName . '_confirm'))
{
$this->addError($GLOBALS['TL_LANG']['ERR']['passwordMatch']);
}
$varInput = parent::validator($varInput);
if (!$this->hasErrors())
{
$this->blnSubmitInput = true;
return password_hash($varInput, PASSWORD_DEFAULT);
}
return '';
}
/**
* Parse the template file and return it as string
*
* @param array $arrAttributes An optional attributes array
*
* @return string The template markup
*/
public function parse($arrAttributes=null)
{
$this->confirmLabel = sprintf($GLOBALS['TL_LANG']['MSC']['confirmation'], $this->strLabel);
return parent::parse($arrAttributes);
}
/**
* Generate the widget and return it as string
*
* @return string The widget markup
*/
public function generate()
{
return sprintf('<input type="password" name="%s" id="ctrl_%s" class="text password%s" value=""%s%s',
$this->strName,
$this->strId,
(($this->strClass != '') ? ' ' . $this->strClass : ''),
$this->getAttributes(),
$this->strTagEnding) . $this->addSubmit();
}
/**
* Generate the label of the confirmation field and return it as string
*
* @return string The confirmation label markup
*/
public function generateConfirmationLabel()
{
return sprintf('<label for="ctrl_%s_confirm" class="confirm%s">%s%s%s</label>',
$this->strId,
(($this->strClass != '') ? ' ' . $this->strClass : ''),
($this->mandatory ? '<span class="invisible">'.$GLOBALS['TL_LANG']['MSC']['mandatory'].' </span>' : ''),
sprintf($GLOBALS['TL_LANG']['MSC']['confirmation'], $this->strLabel),
($this->mandatory ? '<span class="mandatory">*</span>' : ''));
}
/**
* Generate the widget and return it as string
*
* @return string The confirmation field markup
*/
public function generateConfirmation()
{
return sprintf('<input type="password" name="%s_confirm" id="ctrl_%s_confirm" class="text password confirm%s" value=""%s%s',
$this->strName,
$this->strId,
(($this->strClass != '') ? ' ' . $this->strClass : ''),
$this->getAttributes(),
$this->strTagEnding);
}
}