-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusernamecheck.php
303 lines (259 loc) · 14.2 KB
/
usernamecheck.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
* @package Framontb.Plugin
* @subpackage User.usernamecheck
*
* @copyright Copyright (C) 2005 - 2016 framontb. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
use Joomla\String\StringHelper;
/**
* Check if field 'username' (in com_user register form), meets some requirements:
* - Minimum number of characters (if no specified or 0, no limit apply).
* - Maximum number of characters (if no specified or 0, no limit apply).
* - username spelling compliant with defined character presets
*/
class PlgUserUsernameCheck extends JPlugin
{
/**
* Application object
*
* @var JApplicationCms
* @since 3.2
*/
protected $app;
/**
* Load the language file on instantiation.
*
* @var boolean
* @since 3.1
*/
protected $autoloadLanguage = true;
/**
* Method to handle the "onUserBeforeSave" event
*
* @param array $user Holds the old user data.
* @param boolean $isnew True if a new user is stored.
* @param array $data Holds the new user data.
*
* @return boolean $isNew True to allow the save process to continue,
* false to stop it
*
* @since 3.6
*/
public function onUserBeforeSave($user, $isNew, $data)
{
// Result defaults to true
$result = true;
$value = $data['username'];
// CHECK MINIMUM CHARACTER'S NUMBER
// Get the number of characters in $username
$usernameLenght = StringHelper::strlen($value);
// Get the minimum number of characters
$minNumChars = $this->params->get('minimum_length_username');
// If is set minNumChars and $usernameLenght does't achieve minimum lenght
if (($minNumChars) && ($usernameLenght < $minNumChars))
{
$this->app->enqueueMessage(JText::sprintf('PLG_USER_USERNAMECHECK_FIELD_USERNAME_MINNUMCHARS_REQUIRED', $minNumChars, $usernameLenght), 'warning');
$result = false;
}
// CHECK MAXIMUM CHARACTER'S NUMBER
// Get the maximum number of characters
$maxNumChars = $this->params->get('maximum_length_username');
// If is set maxNumChars and $usernameLenght surpass maximum lenght
if (($maxNumChars) && ($usernameLenght > $maxNumChars))
{
$this->app->enqueueMessage(JText::sprintf('PLG_USER_USERNAMECHECK_FIELD_USERNAME_MAXNUMCHARS_REQUIRED', $maxNumChars, $usernameLenght), 'warning');
$result = false;
}
// CHECK IF USERNAME SPELLING IN ALLOWED CHARACTER SET
// Get preset option
$allowed_preset = $this->params->get('allowed_chars_username_preset');
if ($allowed_preset)
{
switch ($allowed_preset)
{
case 1: // CUSTOM ALLOWED
case 2: // CUSTOM FORBIDDEN
$customCharsUsername = array_unique(StringHelper::str_split($this->params->get('custom_chars_username')));
// Get the username
$uname = array_unique(StringHelper::str_split($value));
if ($allowed_preset == 1)
{
// Get the invalid chars for CUSTOM ALLOWED
$invalid_chars = array_diff($uname, $customCharsUsername);
}
else
{
// Get the invalid chars for CUSTOM FORBIDDEN
$invalid_chars = array_intersect($uname, $customCharsUsername);
}
// Check if all the $uname chars are valid chars
if (!empty($invalid_chars))
{
$this->app->enqueueMessage(JText::sprintf('PLG_USER_USERNAMECHECK_FIELD_USERNAME_CHARSET_REQUIRED', implode(' ', $invalid_chars)), 'warning');
$result = false;
}
break;
case 3:
// CUSTOM IS REGEXP
// All that match is rejected
$regExp = (string) $this->params->get('custom_chars_username');
if (preg_match_all($regExp, $value, $nonRegExpChars))
{
$nonRegExpString = implode(' ', array_unique($nonRegExpChars[0]));
// Enqueue error message and return false
$this->app->enqueueMessage(JText::sprintf('PLG_USER_USERNAMECHECK_FIELD_USERNAME_CHARSET_REQUIRED', $nonRegExpString), 'warning');
$result = false;
}
break;
case 4:
// ALPHANUMERIC ONLY
// @TODO: with regExp
if (!ctype_alnum($value))
{
// Enqueue error message and return false
$this->app->enqueueMessage(JText::sprintf('PLG_USER_USERNAMECHECK_FIELD_USERNAME_ALPHANUMERIC_REQUIRED'), 'warning');
$result = false;
}
break;
case 5:
// LATIN ONLY
if (preg_match_all('/[^\\p{Common}\\p{Latin}]/u', $value, $nonLatinChars))
{
$nonLatinString = implode(' ', array_unique($nonLatinChars[0]));
// Enqueue error message and return false
$this->app->enqueueMessage(JText::sprintf('PLG_USER_USERNAMECHECK_FIELD_USERNAME_LATIN_REQUIRED', $nonLatinString), 'warning');
$result = false;
}
break;
case 6:
// UNICODE Security MECHANISMS IMPLEMENTATION of Highly Restrictive level
// http://www.unicode.org/reports/tr39/#General_Security_Profile
// Whenever **scripts** are tested for in the following definitions,
// characters with Script_Extension=Common and Script_Extension=Inherited are ignored.
$commonORinherited = '/[\p{Common}]+|[\p{Inherited}]+/u';
$valueWithoutCommonInherited = preg_replace($commonORinherited, '', $value);
// UAX31 Only some scripts are recommended for Highly Restrictive level
// http://www.unicode.org/reports/tr31/#Table_Recommended_Scripts
$regexRecommendedScripts = '/^(?:'
. '[\p{Latin}\p{Han}\p{Hiragana}\p{Katakana}]+'
. '|[\p{Latin}\p{Han}\p{Bopomofo}]+'
. '|[\p{Latin}\p{Han}\p{Hangul}]+'
. '|[\p{Arabic}]+|[\p{Armenian}]+|[\p{Bengali}]+|[\p{Cyrillic}]+|[\p{Devanagari}]+|[\p{Ethiopic}]+|[\p{Georgian}]+|[\p{Greek}]+'
. '|[\p{Gujarati}]+|[\p{Gurmukhi}]+|[\p{Hebrew}]+|[\p{Kannada}]+|[\p{Khmer}]+|[\p{Lao}]+|[\p{Malayalam}]+|[\p{Myanmar}]+'
. '|[\p{Oriya}]+|[\p{Sinhala}]+|[\p{Tamil}]+|[\p{Telugu}]+|[\p{Thaana}]+|[\p{Thai}]+|[\p{Tibetan}]+'
. ')$/u';
// Si el username proviene de más de un script recomendado (o combinación permitida) lanza error
if (!preg_match_all($regexRecommendedScripts, $valueWithoutCommonInherited))
{
// Enqueue error message and return false
$this->app->enqueueMessage(JText::sprintf('PLG_USER_USERNAMECHECK_FIELD_USERNAME_ONESCRIPT_REQUIRED'), 'warning');
$result = false;
}
/* No characters in the identifier (username) can be outside of the Identifier Profile
* Identifier profile defined here: http://www.unicode.org/Public/security/latest/xidmodifications.txt
* Security Profile for General Identifiers :: Version: 8.0.0
*/
$regexAllowedChars = "/[^\x{0030}-\x{0039}\x{0041}-\x{005A}\x{005F}\x{0061}-\x{007A}\x{00C0}-\x{00D6}\x{00D8}-\x{00F6}\x{00F8}-\x{0131}\\
\x{0134}-\x{013E}\x{0141}-\x{0148}\x{014A}-\x{017E}\x{018F}\x{01A0}-\x{01A1}\x{01AF}-\x{01B0}\x{01CD}-\x{01DC}\x{01DE}-\x{01E3}\\
\x{01E6}-\x{01F0}\x{01F4}-\x{01F5}\x{01F8}-\x{021B}\x{021E}-\x{021F}\x{0226}-\x{0233}\x{0259}\x{02BB}-\x{02BC}\x{02EC}\\
\x{0300}-\x{0304}\x{0306}-\x{030C}\x{030F}-\x{0311}\x{0313}-\x{0314}\x{031B}\x{0323}-\x{0328}\x{032D}-\x{032E}\\
\x{0330}-\x{0331}\x{0335}\x{0338}-\x{0339}\x{0342}\x{0345}\x{037B}-\x{037D}\x{0386}\x{0388}-\x{038A}\x{038C}\x{038E}-\x{03A1}\\
\x{03A3}-\x{03CE}\x{03FC}-\x{045F}\x{048A}-\x{0529}\x{052E}-\x{052F}\x{0531}-\x{0556}\x{0559}\x{0561}-\x{0586}\x{05B4}\\
\x{05D0}-\x{05EA}\x{05F0}-\x{05F2}\x{0620}-\x{063F}\x{0641}-\x{0655}\x{0660}-\x{0669}\x{0670}-\x{0672}\x{0674}\x{0679}-\x{068D}\\
\x{068F}-\x{06D3}\x{06D5}\x{06E5}-\x{06E6}\x{06EE}-\x{06FC}\x{06FF}\x{0750}-\x{07B1}\x{08A0}-\x{08AC}\x{08B2}\x{0901}-\x{094D}\\
\x{094F}-\x{0950}\x{0956}-\x{0957}\x{0960}-\x{0963}\x{0966}-\x{096F}\x{0971}-\x{0977}\x{0979}-\x{097F}\x{0981}-\x{0983}\\
\x{0985}-\x{098C}\x{098F}-\x{0990}\x{0993}-\x{09A8}\x{09AA}-\x{09B0}\x{09B2}\x{09B6}-\x{09B9}\x{09BC}-\x{09C4}\x{09C7}-\x{09C8}\\
\x{09CB}-\x{09CE}\x{09D7}\x{09E0}-\x{09E3}\x{09E6}-\x{09F1}\x{0A01}-\x{0A03}\x{0A05}-\x{0A0A}\x{0A0F}-\x{0A10}\x{0A13}-\x{0A28}\\
\x{0A2A}-\x{0A30}\x{0A32}\x{0A35}\x{0A38}-\x{0A39}\x{0A3C}\x{0A3E}-\x{0A42}\x{0A47}-\x{0A48}\x{0A4B}-\x{0A4D}\x{0A5C}\\
\x{0A66}-\x{0A74}\x{0A81}-\x{0A83}\x{0A85}-\x{0A8D}\x{0A8F}-\x{0A91}\x{0A93}-\x{0AA8}\x{0AAA}-\x{0AB0}\x{0AB2}-\x{0AB3}\\
\x{0AB5}-\x{0AB9}\x{0ABC}-\x{0AC5}\x{0AC7}-\x{0AC9}\x{0ACB}-\x{0ACD}\x{0AD0}\x{0AE0}-\x{0AE3}\x{0AE6}-\x{0AEF}\x{0B01}-\x{0B03}\\
\x{0B05}-\x{0B0C}\x{0B0F}-\x{0B10}\x{0B13}-\x{0B28}\x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\x{A71F}\x{A788}\\
\x{A78D}-\x{A78E}\x{A790}-\x{A793}\x{A7A0}-\x{A7AA}\x{A7FA}\x{A9E7}-\x{A9FE}\x{AA60}-\x{AA76}\x{AA7A}-\x{AA7F}\x{AB01}-\x{AB06}\\
\x{AB09}-\x{AB0E}\x{AB11}-\x{AB16}\x{AB20}-\x{AB26}\x{AB28}-\x{AB2E}\x{AC00}-\x{D7A3}\x{FA0E}-\x{FA0F}\x{FA11}\x{FA13}-\x{FA14}\\
\x{FA1F}\x{FA21}\x{FA23}-\x{FA24}\x{FA27}-\x{FA29}\x{20000}-\x{2A6D6}\x{2A700}-\x{2B734}\x{2B740}-\x{2B81D}\x{2B820}-\x{2CEA1}\\
\x{0027}\x{002D}-\x{002E}\x{003A}\x{00B7}\x{0375}\x{058A}\x{05F3}-\x{05F4}\x{06FD}-\x{06FE}\x{0F0B}\x{200C}-\x{200D}\x{2010}\\
\x{2019}\x{2027}\x{30A0}\x{30FB}]/u";
// No characters in the identifier (username) can be outside of the Identifier Profile
if (preg_match_all($regexAllowedChars, $value, $nonIdentifierProfile))
{
$nonIdentifierProfileString = implode(',', array_unique($nonIdentifierProfile[0]));
if (!preg_match('/[\p{Mc}]/u', $nonIdentifierProfileString))
{
$nonIdentifierProfileString .= ' whitespace';
}
// Enqueue error message and return false
$this->app->enqueueMessage(JText::sprintf('PLG_USER_USERNAMECHECK_FIELD_USERNAME_IDPROFILE_REQUIRED', $nonIdentifierProfileString), 'warning');
$result = false;
}
/*
* // UAX31-D1. Default Identifier Syntax: <ID_Start>
* // <identifier> := <ID_Start> <ID_Continue>*
* if (!preg_match('/^[\p{Lu}|\p{Ll}|\p{Lt}|\p{Lm}|\p{Lo}|\p{Nl}]/u', $value))
* {
* // Enqueue error message and return false
* $this->app->enqueueMessage(JText::sprintf('PLG_USER_USERNAMECHECK_FIELD_USERNAME_IDSTART_REQUIRED'), 'warning');
*
* $result = false;
* }
*
* // UAX31-D1. Default Identifier Syntax: <ID_Continue>
* // http://www.unicode.org/reports/tr31/#Default_Identifier_Syntax
* if (preg_match_all('/[^\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}]/u', $value, $nonIdContinue))
* {
* $nonIdContinueString = implode(' ', array_unique($nonIdContinue[0]));
*
* // Enqueue error message and return false
* $this->app->enqueueMessage(JText::sprintf('PLG_USER_USERNAMECHECK_FIELD_USERNAME_IDCONTINUE_REQUIRED', $nonIdContinueString), 'warning');
*
* $result = false;
* }
*/
break;
case 7:
// EMAIL
jimport('joomla.mail.helper');
if (!JMailHelper::isEmailAddress($value) )
{
// Enqueue error message and return false
$this->app->enqueueMessage(JText::sprintf('PLG_USER_USERNAMECHECK_FIELD_USERNAME_EMAIL_REQUIRED', implode(' ', $invalid_chars)), 'warning');
$result = false;
}
break;
default:
// NO OPTION
$this->app->enqueueMessage(JText::sprintf('PLG_USER_USERNAMECHECK_FIELD_USERNAME_NOOPTION'), 'warning');
return false;
}
}
return $result;
}
}