View source
<?php
namespace Drupal\password_strength;
use Drupal\Component\Plugin\PluginBase;
class MatchBase extends PluginBase {
protected $zxcvbn_matcher;
public static function findAll($string, $regex) {
$count = preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
if (!$count) {
return array();
}
$pos = 0;
$groups = array();
foreach ($matches as $group) {
$captureBegin = 0;
$match = array_shift($group);
$matchBegin = strpos($string, $match, $pos);
$captures = array(
array(
'begin' => $matchBegin,
'end' => $matchBegin + strlen($match) - 1,
'token' => $match,
),
);
foreach ($group as $capture) {
$captureBegin = strpos($match, $capture, $captureBegin);
$captures[] = array(
'begin' => $matchBegin + $captureBegin,
'end' => $matchBegin + $captureBegin + strlen($capture) - 1,
'token' => $capture,
);
}
$groups[] = $captures;
$pos += strlen($match) - 1;
}
return $groups;
}
public function getCardinality() {
if (!is_null($this->cardinality)) {
return $this->cardinality;
}
$lower = $upper = $digits = $symbols = $unicode = 0;
$chars = str_split($this->token);
foreach ($chars as $char) {
$ord = ord($char);
if ($this
->isDigit($ord)) {
$digits = 10;
}
elseif ($this
->isUpper($ord)) {
$upper = 26;
}
elseif ($this
->isLower($ord)) {
$lower = 26;
}
elseif ($this
->isSymbol($ord)) {
$symbols = 33;
}
else {
$unicode = 100;
}
}
$this->cardinality = $lower + $digits + $upper + $symbols + $unicode;
return $this->cardinality;
}
protected function isDigit($ord) {
return $ord >= 0x30 && $ord <= 0x39;
}
protected function isUpper($ord) {
return $ord >= 0x41 && $ord <= 0x5a;
}
protected function isLower($ord) {
return $ord >= 0x61 && $ord <= 0x7a;
}
protected function isSymbol($ord) {
return $ord <= 0x7f;
}
protected function log($number) {
return log($number, 2);
}
protected function binom($n, $k) {
$j = $res = 1;
if ($k < 0 || $k > $n) {
return 0;
}
if ($n - $k < $k) {
$k = $n - $k;
}
while ($j <= $k) {
$res *= $n--;
$res /= $j++;
}
return $res;
}
public function getEntropy() {
return $this->zxcvbn_matcher
->getEntropy();
}
}