|
38 | 38 | 'type' => $type,
|
39 | 39 | 'id' => $this->getFieldId(),
|
40 | 40 | 'value' => $value,
|
41 |
| - 'autocomplete' => 'new-password', |
42 | 41 | ];
|
43 | 42 |
|
| 43 | +if (rex::isFrontend()) { |
| 44 | + $attributes['autocomplete'] = 'new-password'; |
| 45 | +} |
| 46 | + |
44 | 47 | $attributes = $this->getAttributeElements($attributes, ['autocomplete', 'pattern', 'required', 'disabled', 'readonly']);
|
45 | 48 |
|
46 | 49 | $span = '';
|
|
50 | 53 | if ($script) {
|
51 | 54 | $funcName = uniqid('rex_ycom_password_create' . $this->getId());
|
52 | 55 | $span = '<span class="input-group-btn">
|
53 |
| - <button type="button" class="btn btn-default getNewPass" onclick="' . $funcName . 'refresh(' . $this->getId() . ')"><span class="fa fa-refresh"></span></button> |
| 56 | + <button type="button" class="btn btn-default getNewPass rex-ycom-password-refresh-button" data-myRules=\'' . json_encode($rules) . '\' data-myField="' . rex_escape($this->getFieldName()) . '"><span class="fa fa-refresh"></span></button> |
54 | 57 | </span>';
|
55 | 58 |
|
56 | 59 | $nonce = '';
|
57 | 60 | $nonce = ' nonce="' . rex_response::getNonce() . '"';
|
58 | 61 |
|
59 | 62 | ?><script type="text/javascript"<?= $nonce ?>>
|
60 | 63 |
|
| 64 | + $(document).on('rex:ready', function () { |
| 65 | + $(".rex-ycom-password-refresh-button").each(function() { |
| 66 | + $(this).off("click"); |
| 67 | + $(this).on("click", function() { |
| 68 | + rex_ycom_password_refresh(this); |
| 69 | + }); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
61 | 73 | // Credit to @Blender https://stackoverflow.com/users/464744/blender
|
62 | 74 | String.prototype.pick = function(min, max) {
|
63 | 75 | var n, chars = '';
|
|
92 | 104 | return array.join('');
|
93 | 105 | };
|
94 | 106 |
|
95 |
| - function <?= $funcName . 'refresh' ?>(input) { |
96 |
| - |
97 |
| - var rules = { |
98 |
| - letter:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", |
99 |
| - uppercase:"ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
100 |
| - lowercase: "abcdefghijklmnopqrstuvwxyz", |
101 |
| - digit: "0123456789", |
102 |
| - symbol: "!@#$%^&*()_+{}:\"<>?\|[];',./`~", |
103 |
| - all: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+{}:\"<>?|[];',./`~", |
104 |
| - }; |
105 |
| - rules.letter = rules.uppercase + rules.lowercase; |
106 |
| - rules.all = rules.uppercase + rules.lowercase + rules.digit + rules.symbol; |
107 |
| - |
108 |
| - var ruleset = ''; |
109 |
| - var myRules = <?= json_encode($rules) ?>; |
110 |
| - var myPassword = ''; |
111 |
| - |
112 |
| - if (typeof myRules.uppercase === "object") { |
113 |
| - min = myRules.uppercase.min; |
114 |
| - if (typeof myRules.uppercase.min === "undefined") { |
115 |
| - min = 1; |
116 |
| - } |
117 |
| - max = myRules.uppercase.max; |
118 |
| - if (typeof myRules.uppercase.max === "undefined") { |
119 |
| - max = min; |
120 |
| - } |
121 |
| - generate = myRules.uppercase.generate; |
122 |
| - if (typeof myRules.uppercase.generate !== "undefined") { |
123 |
| - min = generate; |
124 |
| - max = generate; |
125 |
| - } |
126 |
| - ruleset += rules.uppercase; |
127 |
| - myPassword += rules.uppercase.pick(min,max); |
128 |
| - } |
129 |
| - if (typeof myRules.lowercase === "object") { |
130 |
| - min = myRules.lowercase.min; |
131 |
| - if (typeof myRules.lowercase.min === "undefined") { |
132 |
| - min = 1; |
133 |
| - } |
134 |
| - max = myRules.lowercase.max; |
135 |
| - if (typeof myRules.lowercase.max === "undefined") { |
136 |
| - max = min; |
137 |
| - } |
138 |
| - generate = myRules.lowercase.generate; |
139 |
| - if (typeof myRules.lowercase.generate !== "undefined") { |
140 |
| - min = generate; |
141 |
| - max = generate; |
142 |
| - } |
143 |
| - ruleset += rules.lowercase; |
144 |
| - myPassword += rules.lowercase.pick(min,max); |
145 |
| - } |
146 |
| - if (typeof myRules.letter === "object") { |
147 |
| - min = myRules.letter.min; |
148 |
| - if (typeof myRules.letter.min === "undefined") { |
149 |
| - min = 1; |
150 |
| - } |
151 |
| - max = myRules.letter.max; |
152 |
| - if (typeof myRules.letter.max === "undefined") { |
153 |
| - max = min; |
154 |
| - } |
155 |
| - if (min > myPassword.length) { |
156 |
| - min = min - myPassword.length; |
157 |
| - } else { |
158 |
| - min = 0; |
159 |
| - } |
160 |
| - if (max > myPassword.length) { |
161 |
| - max = max - myPassword.length; |
162 |
| - } else { |
163 |
| - min = 0; |
| 107 | + if (typeof rex_ycom_password_refresh !== 'function') { |
| 108 | + function rex_ycom_password_refresh(button) { |
| 109 | + |
| 110 | + var rules = { |
| 111 | + letter: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", |
| 112 | + uppercase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
| 113 | + lowercase: "abcdefghijklmnopqrstuvwxyz", |
| 114 | + digit: "0123456789", |
| 115 | + symbol: "!@#$%^&*()_+{}:\"<>?\|[];',./`~", |
| 116 | + all: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+{}:\"<>?|[];',./`~", |
| 117 | + }; |
| 118 | + rules.letter = rules.uppercase + rules.lowercase; |
| 119 | + rules.all = rules.uppercase + rules.lowercase + rules.digit + rules.symbol; |
| 120 | + |
| 121 | + var ruleset = ''; |
| 122 | + var myRules = JSON.parse(decodeURIComponent(button.getAttribute('data-myRules'))); |
| 123 | + var myPassword = ''; |
| 124 | + |
| 125 | + if (typeof myRules.uppercase === "object") { |
| 126 | + min = myRules.uppercase.min; |
| 127 | + if (typeof myRules.uppercase.min === "undefined") { |
| 128 | + min = 1; |
| 129 | + } |
| 130 | + max = myRules.uppercase.max; |
| 131 | + if (typeof myRules.uppercase.max === "undefined") { |
| 132 | + max = min; |
| 133 | + } |
| 134 | + generate = myRules.uppercase.generate; |
| 135 | + if (typeof myRules.uppercase.generate !== "undefined") { |
| 136 | + min = generate; |
| 137 | + max = generate; |
| 138 | + } |
| 139 | + ruleset += rules.uppercase; |
| 140 | + myPassword += rules.uppercase.pick(min, max); |
164 | 141 | }
|
165 |
| - generate = myRules.letter.generate; |
166 |
| - if (typeof myRules.letter.max !== "undefined") { |
167 |
| - min = generate; |
168 |
| - max = generate; |
| 142 | + if (typeof myRules.lowercase === "object") { |
| 143 | + min = myRules.lowercase.min; |
| 144 | + if (typeof myRules.lowercase.min === "undefined") { |
| 145 | + min = 1; |
| 146 | + } |
| 147 | + max = myRules.lowercase.max; |
| 148 | + if (typeof myRules.lowercase.max === "undefined") { |
| 149 | + max = min; |
| 150 | + } |
| 151 | + generate = myRules.lowercase.generate; |
| 152 | + if (typeof myRules.lowercase.generate !== "undefined") { |
| 153 | + min = generate; |
| 154 | + max = generate; |
| 155 | + } |
| 156 | + ruleset += rules.lowercase; |
| 157 | + myPassword += rules.lowercase.pick(min, max); |
169 | 158 | }
|
170 |
| - myPassword += ruleset.pick(min,max); |
| 159 | + if (typeof myRules.letter === "object") { |
| 160 | + min = myRules.letter.min; |
| 161 | + if (typeof myRules.letter.min === "undefined") { |
| 162 | + min = 1; |
| 163 | + } |
| 164 | + max = myRules.letter.max; |
| 165 | + if (typeof myRules.letter.max === "undefined") { |
| 166 | + max = min; |
| 167 | + } |
| 168 | + if (min > myPassword.length) { |
| 169 | + min = min - myPassword.length; |
| 170 | + } else { |
| 171 | + min = 0; |
| 172 | + } |
| 173 | + if (max > myPassword.length) { |
| 174 | + max = max - myPassword.length; |
| 175 | + } else { |
| 176 | + min = 0; |
| 177 | + } |
| 178 | + generate = myRules.letter.generate; |
| 179 | + if (typeof myRules.letter.max !== "undefined") { |
| 180 | + min = generate; |
| 181 | + max = generate; |
| 182 | + } |
| 183 | + myPassword += ruleset.pick(min, max); |
171 | 184 |
|
172 |
| - } |
173 |
| - if (typeof myRules.digit === "object") { |
174 |
| - min = myRules.digit.min; |
175 |
| - if (typeof myRules.digit.min === "undefined") { |
176 |
| - min = 1; |
177 |
| - } |
178 |
| - max = myRules.digit.max; |
179 |
| - if (typeof myRules.digit.max === "undefined") { |
180 |
| - max = min; |
181 | 185 | }
|
182 |
| - generate = myRules.digit.generate; |
183 |
| - if (typeof myRules.digit.max !== "undefined") { |
184 |
| - min = generate; |
185 |
| - max = generate; |
186 |
| - } |
187 |
| - ruleset += rules.digit; |
188 |
| - myPassword += rules.digit.pick(min,max); |
189 |
| - } |
190 |
| - if (typeof myRules.symbol === "object") { |
191 |
| - min = myRules.symbol.min; |
192 |
| - if (typeof myRules.symbol.min === "undefined") { |
193 |
| - min = 1; |
| 186 | + if (typeof myRules.digit === "object") { |
| 187 | + min = myRules.digit.min; |
| 188 | + if (typeof myRules.digit.min === "undefined") { |
| 189 | + min = 1; |
| 190 | + } |
| 191 | + max = myRules.digit.max; |
| 192 | + if (typeof myRules.digit.max === "undefined") { |
| 193 | + max = min; |
| 194 | + } |
| 195 | + generate = myRules.digit.generate; |
| 196 | + if (typeof myRules.digit.max !== "undefined") { |
| 197 | + min = generate; |
| 198 | + max = generate; |
| 199 | + } |
| 200 | + ruleset += rules.digit; |
| 201 | + myPassword += rules.digit.pick(min, max); |
194 | 202 | }
|
195 |
| - max = myRules.symbol.max; |
196 |
| - if (typeof myRules.symbol.max === "undefined") { |
197 |
| - max = min; |
| 203 | + if (typeof myRules.symbol === "object") { |
| 204 | + min = myRules.symbol.min; |
| 205 | + if (typeof myRules.symbol.min === "undefined") { |
| 206 | + min = 1; |
| 207 | + } |
| 208 | + max = myRules.symbol.max; |
| 209 | + if (typeof myRules.symbol.max === "undefined") { |
| 210 | + max = min; |
| 211 | + } |
| 212 | + generate = myRules.symbol.generate; |
| 213 | + if (typeof myRules.symbol.max !== "undefined") { |
| 214 | + min = generate; |
| 215 | + max = generate; |
| 216 | + } |
| 217 | + ruleset += rules.symbol; |
| 218 | + myPassword += rules.symbol.pick(min, max); |
198 | 219 | }
|
199 |
| - generate = myRules.symbol.generate; |
200 |
| - if (typeof myRules.symbol.max !== "undefined") { |
201 |
| - min = generate; |
202 |
| - max = generate; |
203 |
| - } |
204 |
| - ruleset += rules.symbol; |
205 |
| - myPassword += rules.symbol.pick(min,max); |
206 |
| - } |
207 | 220 |
|
208 |
| - if (typeof myRules.length === "object") { |
209 |
| - min = myRules.length.min; |
210 |
| - if (typeof myRules.length.min === "undefined") { |
211 |
| - min = 1; |
212 |
| - } |
213 |
| - max = myRules.length.max; |
214 |
| - if (typeof myRules.length.max === "undefined") { |
215 |
| - max = min; |
216 |
| - } |
217 |
| - if (min > myPassword.length) { |
218 |
| - min = min - myPassword.length; |
219 |
| - } else { |
220 |
| - min = 0; |
221 |
| - } |
222 |
| - if (max > myPassword.length) { |
223 |
| - max = max - myPassword.length; |
224 |
| - } else { |
225 |
| - min = 0; |
226 |
| - } |
227 |
| - generate = myRules.length.generate; |
228 |
| - if (typeof myRules.length.max !== "undefined") { |
229 |
| - min = generate; |
230 |
| - max = generate; |
231 |
| - } |
232 |
| - myPassword += ruleset.pick(min,max); |
| 221 | + if (typeof myRules.length === "object") { |
| 222 | + min = myRules.length.min; |
| 223 | + if (typeof myRules.length.min === "undefined") { |
| 224 | + min = 1; |
| 225 | + } |
| 226 | + max = myRules.length.max; |
| 227 | + if (typeof myRules.length.max === "undefined") { |
| 228 | + max = min; |
| 229 | + } |
| 230 | + if (min > myPassword.length) { |
| 231 | + min = min - myPassword.length; |
| 232 | + } else { |
| 233 | + min = 0; |
| 234 | + } |
| 235 | + if (max > myPassword.length) { |
| 236 | + max = max - myPassword.length; |
| 237 | + } else { |
| 238 | + min = 0; |
| 239 | + } |
| 240 | + generate = myRules.length.generate; |
| 241 | + if (typeof myRules.length.max !== "undefined") { |
| 242 | + min = generate; |
| 243 | + max = generate; |
| 244 | + } |
| 245 | + myPassword += ruleset.pick(min, max); |
233 | 246 |
|
234 |
| - } |
| 247 | + } |
235 | 248 |
|
236 |
| - var item = document.getElementsByName('<?= $this->getFieldName() ?>').item(0); |
237 |
| - var name = item.getAttribute('name'); |
238 |
| - var type = item.getAttribute('value'); |
| 249 | + var item = document.getElementsByName(button.getAttribute('data-myField')).item(0); |
| 250 | + var name = item.getAttribute('name'); |
| 251 | + var type = item.getAttribute('value'); |
239 | 252 |
|
240 |
| - item.value = myPassword; |
| 253 | + item.value = myPassword; |
241 | 254 |
|
| 255 | + } |
242 | 256 | }
|
243 | 257 | </script><?php
|
244 | 258 |
|
|
0 commit comments