forked from Spudley/plg_user_restrictusername
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestrictusername.php
122 lines (102 loc) · 4.64 KB
/
restrictusername.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
<?php
/**
* @package Restrictusername
* @type Plugin (User)
* @version 1.0.0
* @author Simon Champion
* @copyright (C) 2016 Simon Champion
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
**/
defined('_JEXEC') or die('Restricted access');
class plgUserRestrictusername extends JPlugin
{
const MIN_MIN = 2; //lowest allowed value for minLength.
protected $autoloadLanguage = true;
/**
* @throws RuntimeException if the username is not valid according to the regex.
*/
public function onUserBeforeSave($user, $isnew, $data)
{
if (!$data['username']) {
return true;
}
if ($this->params->get('allowEmail')) {
if (JFactory::getMailer()->ValidateAddress($data['username'])) {
return true;
}
}
$regex = $this->buildAllowedCharsRegex();
if($regex && !preg_match($regex, $data['username'])) {
throw new RuntimeException($this->allowedCharsErrorMessage());
}
return true;
}
protected function buildAllowedCharsRegex()
{
if($this->params->get('useOwnRegex')) {
return $this->params->get('userDefinedRegex');
}
$characterExpression = implode('', $this->regexesFromParamData());
if (!$characterExpression) { return ''; }
$max = (int)$this->params->get('maxLength', 0);
$min = (int)$this->params->get('minLength', 0);
if ($min < self::MIN_MIN) { $min = self::MIN_MIN;}
if ($max < $min) { $max = 0; }
$lengthRange = "{".$min.",".($max?:'')."}";
return "/^[{$characterExpression}]{$lengthRange}$/";
}
protected function allowedCharsErrorMessage()
{
if($this->params->get('userDefinedErrorMessage')) {
return $this->params->get('userDefinedErrorMessage');
}
if($this->params->get('useRegex')) {
return JText::_('PLG_USER_RESTRICTUSERNAME_NOT_MATCH_REGEX'); //"Invalid Username"
}
$errorMessage = JText::_('PLG_USER_RESTRICTUSERNAME_NOT_MEET_CRITERIA'); //"Username does not meet the following criteria:"
$errorBullets = $this->messagesFromParamData();
return $errorMessage."<ul><li>".implode('</li><li>', $errorBullets)."</li></ul>";
}
protected function messagesFromParamData()
{
$output = [];
foreach($this->paramData() as $paramName=>$paramData) {
list($rex, $message) = $paramData;
if($this->params->get($paramName)) {
$output[] = $message;
}
}
return $output;
}
protected function regexesFromParamData()
{
$output = [];
foreach($this->paramData() as $paramName=>$paramData) {
list($rex, $message) = $paramData;
if($this->params->get($paramName)) {
$output[] = $rex;
}
}
return $output;
}
protected function paramData() {
$sym = $this->params->get('userDefinedSymbols');
return [
'allowSpaces' => ['\s', JText::_('PLG_USER_RESTRICTUSERNAME_MAY_CONTAIN_SPACES')],
'allowUpper' => ['A-Z', JText::_('PLG_USER_RESTRICTUSERNAME_MAY_CONTAIN_UPPER')],
'allowLower' => ['a-z', JText::_('PLG_USER_RESTRICTUSERNAME_MAY_CONTAIN_LOWER')],
'allowNumbers' => ['\d', JText::_('PLG_USER_RESTRICTUSERNAME_MAY_CONTAIN_NUMBERS')],
'allowDots' => [preg_quote('.'), JText::_('PLG_USER_RESTRICTUSERNAME_MAY_CONTAIN_DOTS')],
'allowDashes' => [preg_quote('-'), JText::_('PLG_USER_RESTRICTUSERNAME_MAY_CONTAIN_DASHES')],
'allowSymbols' => [preg_quote($sym), JText::sprintf('PLG_USER_RESTRICTUSERNAME_MAY_CONTAIN_SYMBOLS', htmlentities($sym))],
'allowCyrillic' => ['\p{Cyrillic}', JText::_('PLG_USER_RESTRICTUSERNAME_MAY_CONTAIN_CYRILLIC')],
'allowArabic' => ['\p{Arabic}', JText::_('PLG_USER_RESTRICTUSERNAME_MAY_CONTAIN_ARABIC')],
'allowHebrew' => ['\p{Hebrew}', JText::_('PLG_USER_RESTRICTUSERNAME_MAY_CONTAIN_HEBREW')],
'allowLatin' => ['\p{Latin}', JText::_('PLG_USER_RESTRICTUSERNAME_MAY_CONTAIN_LATIN')],
//no regex for these as they're validated separately, but still here so we can include them in the message.
'allowEmail' => ['', JText::_('PLG_USER_RESTRICTUSERNAME_MAY_BE_EMAIL')],
'minLength' => ['', JText::sprintf('PLG_USER_RESTRICTUSERNAME_MIN_LENGTH', (int)$this->params->get('minLength', 0))],
'maxLength' => ['', JText::sprintf('PLG_USER_RESTRICTUSERNAME_MAX_LENGTH', (int)$this->params->get('maxLength', 0))],
];
}
}