1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 15 16 17 18
19 class JFormRulePassword extends JFormRule
20 {
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
40 public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, JForm $form = null)
41 {
42 $meter = isset($element['strengthmeter']) ? ' meter="0"' : '1';
43 $threshold = isset($element['threshold']) ? (int) $element['threshold'] : 66;
44 $minimumLength = isset($element['minimum_length']) ? (int) $element['minimum_length'] : 4;
45 $minimumIntegers = isset($element['minimum_integers']) ? (int) $element['minimum_integers'] : 0;
46 $minimumSymbols = isset($element['minimum_symbols']) ? (int) $element['minimum_symbols'] : 0;
47 $minimumUppercase = isset($element['minimum_uppercase']) ? (int) $element['minimum_uppercase'] : 0;
48
49
50
51 $params = JComponentHelper::getParams('com_users');
52
53 if (!empty($params))
54 {
55 $minimumLengthp = $params->get('minimum_length');
56 $minimumIntegersp = $params->get('minimum_integers');
57 $minimumSymbolsp = $params->get('minimum_symbols');
58 $minimumUppercasep = $params->get('minimum_uppercase');
59 $meterp = $params->get('meter');
60 $thresholdp = $params->get('threshold');
61
62 empty($minimumLengthp) ? : $minimumLength = (int) $minimumLengthp;
63 empty($minimumIntegersp) ? : $minimumIntegers = (int) $minimumIntegersp;
64 empty($minimumSymbolsp) ? : $minimumSymbols = (int) $minimumSymbolsp;
65 empty($minimumUppercasep) ? : $minimumUppercase = (int) $minimumUppercasep;
66 empty($meterp) ? : $meter = $meterp;
67 empty($thresholdp) ? : $threshold = $thresholdp;
68 }
69
70
71 $required = ((string) $element['required'] === 'true' || (string) $element['required'] === 'required');
72
73 if (!$required && empty($value))
74 {
75 return true;
76 }
77
78 $valueLength = strlen($value);
79
80
81 JFactory::getLanguage()->load('com_users');
82
83
84 if ($valueLength > 4096)
85 {
86 JFactory::getApplication()->enqueueMessage(JText::_('COM_USERS_MSG_PASSWORD_TOO_LONG'), 'warning');
87 }
88
89
90 $valueTrim = trim($value);
91
92
93 $validPassword = true;
94
95 if (strlen($valueTrim) !== $valueLength)
96 {
97 JFactory::getApplication()->enqueueMessage(
98 JText::_('COM_USERS_MSG_SPACES_IN_PASSWORD'),
99 'warning'
100 );
101
102 $validPassword = false;
103 }
104
105
106 if (!empty($minimumIntegers))
107 {
108 $nInts = preg_match_all('/[0-9]/', $value, $imatch);
109
110 if ($nInts < $minimumIntegers)
111 {
112 JFactory::getApplication()->enqueueMessage(
113 JText::plural('COM_USERS_MSG_NOT_ENOUGH_INTEGERS_N', $minimumIntegers),
114 'warning'
115 );
116
117 $validPassword = false;
118 }
119 }
120
121
122 if (!empty($minimumSymbols))
123 {
124 $nsymbols = preg_match_all('[\W]', $value, $smatch);
125
126 if ($nsymbols < $minimumSymbols)
127 {
128 JFactory::getApplication()->enqueueMessage(
129 JText::plural('COM_USERS_MSG_NOT_ENOUGH_SYMBOLS_N', $minimumSymbols),
130 'warning'
131 );
132
133 $validPassword = false;
134 }
135 }
136
137
138 if (!empty($minimumUppercase))
139 {
140 $nUppercase = preg_match_all('/[A-Z]/', $value, $umatch);
141
142 if ($nUppercase < $minimumUppercase)
143 {
144 JFactory::getApplication()->enqueueMessage(
145 JText::plural('COM_USERS_MSG_NOT_ENOUGH_UPPERCASE_LETTERS_N', $minimumUppercase),
146 'warning'
147 );
148
149 $validPassword = false;
150 }
151 }
152
153
154 if (!empty($minimumLength))
155 {
156 if (strlen((string) $value) < $minimumLength)
157 {
158 JFactory::getApplication()->enqueueMessage(
159 JText::plural('COM_USERS_MSG_PASSWORD_TOO_SHORT_N', $minimumLength),
160 'warning'
161 );
162
163 $validPassword = false;
164 }
165 }
166
167
168 if (!$validPassword)
169 {
170 return false;
171 }
172
173 return true;
174 }
175 }
176