| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Auth; |
|
|
| use Piwik\Piwik; |
|
|
| |
| |
| |
| |
| |
| |
| class PasswordStrength |
| { |
| |
| private $enabled; |
|
|
| public function __construct(bool $featureEnabled) |
| { |
| $this->enabled = $featureEnabled; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function getRules(): array |
| { |
| if (!$this->enabled) { |
| return []; |
| } |
|
|
| return [ |
| [ |
| 'validationRegex' => '/^.{12,}$/', |
| 'ruleText' => Piwik::translate('General_PasswordStrengthValidationLength'), |
| ], |
| [ |
| 'validationRegex' => '/^.*[a-z].*$/', |
| 'ruleText' => Piwik::translate('General_PasswordStrengthValidationLowercase'), |
| ], |
| [ |
| 'validationRegex' => '/^.*[A-Z].*$/', |
| 'ruleText' => Piwik::translate('General_PasswordStrengthValidationUppercase'), |
| ], |
| [ |
| 'validationRegex' => '/^.*[0-9].*$/', |
| 'ruleText' => Piwik::translate('General_PasswordStrengthValidationNumber'), |
| ], |
| [ |
| 'validationRegex' => '/^.*[!\"#$%&\\\'(\\\\)*+,\-.\/:;<=>?@[\\]^_\`{\|}\~].*$/', |
| 'ruleText' => Piwik::translate('General_PasswordStrengthValidationSpecialChar'), |
| ], |
| ]; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function validatePasswordStrength(string $candidate): array |
| { |
| if (!$this->enabled) { |
| return []; |
| } |
|
|
| $brokenRules = []; |
| foreach ($this->getRules() as $rule) { |
| if (!preg_match($rule['validationRegex'], $candidate)) { |
| $brokenRules[] = $rule['ruleText']; |
| } |
| } |
|
|
| return $brokenRules; |
| } |
|
|
| public function formatValidationFailedMessage(array $brokenRules): string |
| { |
| if (!$this->enabled || empty($brokenRules)) { |
| return ''; |
| } |
|
|
| $concatenatedRules = implode(', ', array_map('lcfirst', $brokenRules)); |
|
|
| return Piwik::translate('General_PasswordStrengthValidationFailed', $concatenatedRules); |
| } |
|
|
| public function getRulesAsHtmlList(): string |
| { |
| $list = ''; |
| $rules = $this->getRules(); |
| foreach ($rules as $rule) { |
| $ruleText = $rule['ruleText']; |
| $list .= "<li>$ruleText</li>"; |
| } |
|
|
| return "<ul class='browser-default'>$list</ul>"; |
| } |
| } |
|
|