Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions lib/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public static function getMailer() {
// Settings
$mailer->IsSMTP();
$mailer->CharSet = 'UTF-8';
$mailer->Host = MAILER['host'];
$mailer->SMTPDebug = 0;
$mailer->Port = MAILER['port'];
$mailer->Host = MAILER['host'];
$mailer->SMTPDebug = 0;
$mailer->Port = MAILER['port'];
if (isset(MAILER['user'])) {
$mailer->SMTPAuth = true;
$mailer->Username = MAILER['user'];
$mailer->Password = MAILER['password'];
$mailer->SMTPAuth = true;
$mailer->Username = MAILER['user'];
$mailer->Password = MAILER['password'];
}
$mailer->isHTML(true);
$mailer->setFrom(MAILER['from']);
Expand Down Expand Up @@ -50,10 +50,10 @@ public static function sendAccountCreated($data) {
$mailer->addAddress($mailTo);

$mailer->Subject = $mailSubject;
$mailer->Body = $mailHtmlBody;
$mailer->Body = $mailHtmlBody;
$mailer->AltBody = $mailPlainBody;

$mailer->send();
return $mailer->send();
}

public static function sendVerify($data) {
Expand All @@ -79,10 +79,10 @@ public static function sendVerify($data) {
$mailer->addAddress($mailTo);

$mailer->Subject = $mailSubject;
$mailer->Body = $mailHtmlBody;
$mailer->Body = $mailHtmlBody;
$mailer->AltBody = $mailPlainBody;

$mailer->send();
return $mailer->send();
}

public static function sendResetPassword($data) {
Expand All @@ -107,10 +107,10 @@ public static function sendResetPassword($data) {
$mailer->addAddress($mailTo);

$mailer->Subject = $mailSubject;
$mailer->Body = $mailHtmlBody;
$mailer->Body = $mailHtmlBody;
$mailer->AltBody = $mailPlainBody;

$mailer->send();
return $mailer->send();
}

public static function sendDeleteAccount($data) {
Expand All @@ -135,9 +135,9 @@ public static function sendDeleteAccount($data) {
$mailer->addAddress($mailTo);

$mailer->Subject = $mailSubject;
$mailer->Body = $mailHtmlBody;
$mailer->Body = $mailHtmlBody;
$mailer->AltBody = $mailPlainBody;

$mailer->send();
return $mailer->send();
}
}
192 changes: 96 additions & 96 deletions lib/PasswordValidator.php
Original file line number Diff line number Diff line change
@@ -1,114 +1,114 @@
<?php
/*
Code modified from https://gitlab.com/garybell/password-validation/ (MIT licensed)
Code modified from https://gitlab.com/garybell/password-validation/ (MIT licensed)
*/
namespace Pdsinterop\PhpSolid;

class PasswordValidator
{
private static string $specialCharacters = ' !"#$%&\'()*+,-./:;<=>?@[\]^_{|}~';
private static string $lowercaseCharacters = 'abcdefghijklmnopqrstuvwxyz';
private static string $uppercaseCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
private static string $numbers = '0123456789';
private static string $specialCharacters = ' !"#$%&\'()*+,-./:;<=>?@[\]^_{|}~';
private static string $lowercaseCharacters = 'abcdefghijklmnopqrstuvwxyz';
private static string $uppercaseCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
private static string $numbers = '0123456789';

/**
* The maximum number of times the same character can appear in the password
* @var int
*/
private static int $maxOccurrences = 2;
/**
* The maximum number of times the same character can appear in the password
* @var int
*/
private static int $maxOccurrences = 2;

/**
* Get the base amount of characters from the characters used in the password.
* This is the number of possible characters to pick from in the used character sets
* i.e. 26 for only lower case passwords
* @param $password
* @return int
*/
public static function getBase(string $password): int
{
$characters = str_split($password);
$base = 0;
$hasSpecial = false;
$hasLower = false;
$hasUpper = false;
$hasDigits = false;
/**
* Get the base amount of characters from the characters used in the password.
* This is the number of possible characters to pick from in the used character sets
* i.e. 26 for only lower case passwords
* @param $password
* @return int
*/
public static function getBase(string $password): int
{
$characters = str_split($password);
$base = 0;
$hasSpecial = false;
$hasLower = false;
$hasUpper = false;
$hasDigits = false;

foreach ($characters as $character) {
if (!$hasLower && strpos(self::$lowercaseCharacters, $character) !== false) {
$hasLower = true;
$base += strlen(self::$lowercaseCharacters);
}
if (!$hasUpper && strpos(self::$uppercaseCharacters, $character) !== false) {
$hasUpper = true;
$base += strlen(self::$uppercaseCharacters);
}
if (!$hasSpecial && strpos(self::$specialCharacters, $character) !== false) {
$hasSpecial = true;
$base += strlen(self::$specialCharacters);
}
if (!$hasDigits && strpos(self::$numbers, $character) !== false) {
$hasDigits = true;
$base += strlen(self::$numbers);
}
foreach ($characters as $character) {
if (!$hasLower && strpos(self::$lowercaseCharacters, $character) !== false) {
$hasLower = true;
$base += strlen(self::$lowercaseCharacters);
}
if (!$hasUpper && strpos(self::$uppercaseCharacters, $character) !== false) {
$hasUpper = true;
$base += strlen(self::$uppercaseCharacters);
}
if (!$hasSpecial && strpos(self::$specialCharacters, $character) !== false) {
$hasSpecial = true;
$base += strlen(self::$specialCharacters);
}
if (!$hasDigits && strpos(self::$numbers, $character) !== false) {
$hasDigits = true;
$base += strlen(self::$numbers);
}

if (
strpos(self::$lowercaseCharacters, $character) === false
&& strpos(self::$uppercaseCharacters, $character) === false
&& strpos(self::$specialCharacters, $character) === false
&& strpos(self::$numbers, $character) === false
) {
$base++;
}
}
if (
strpos(self::$lowercaseCharacters, $character) === false
&& strpos(self::$uppercaseCharacters, $character) === false
&& strpos(self::$specialCharacters, $character) === false
&& strpos(self::$numbers, $character) === false
) {
$base++;
}
}

return $base;
}
return $base;
}

/**
* get the calculated entropy of the password based on the rules for excluding duplicate characters
* If a password is in the banned list, entropy will be 0.
* @see bannedPassords()
* @param string $password
* @param array $bannedPasswords a custom list of passwords to disallow
* @return float
*/
public static function getEntropy(string $password, array $bannedPasswords = []): float
{
if (in_array(strtolower($password), $bannedPasswords)) {
// these are so weak, we just want to outright ban them. Entropy will be 0 for anything in this list.
return 0;
}
$base = self::getBase($password);
$length = self::getLength($password);
/**
* get the calculated entropy of the password based on the rules for excluding duplicate characters
* If a password is in the banned list, entropy will be 0.
* @see bannedPassords()
* @param string $password
* @param array $bannedPasswords a custom list of passwords to disallow
* @return float
*/
public static function getEntropy(string $password, array $bannedPasswords = []): float
{
if (in_array(strtolower($password), $bannedPasswords)) {
// these are so weak, we just want to outright ban them. Entropy will be 0 for anything in this list.
return 0;
}
$base = self::getBase($password);
$length = self::getLength($password);

$decimalPlaces = 2;
return number_format(log($base ** $length), $decimalPlaces);
}
$decimalPlaces = 2;
return number_format(log($base ** $length), $decimalPlaces);
}

/**
* Check the length of the password based on known rules
* Characters will only be counted a maximum of 2 times e.g. aaa has length 2
* @param $password
* @return int
*/
public static function getLength(string $password): int
{
$usedCharacters = [];
$characters = str_split($password);
$length = 0;
/**
* Check the length of the password based on known rules
* Characters will only be counted a maximum of 2 times e.g. aaa has length 2
* @param $password
* @return int
*/
public static function getLength(string $password): int
{
$usedCharacters = [];
$characters = str_split($password);
$length = 0;

foreach ($characters as $character)
{
if (array_key_exists($character, $usedCharacters) && $usedCharacters[$character] < self::$maxOccurrences) {
$length++;
$usedCharacters[$character]++;
}
if (!array_key_exists($character, $usedCharacters)) {
$usedCharacters[$character] = 1;
$length++;
}
}
foreach ($characters as $character)
{
if (array_key_exists($character, $usedCharacters) && $usedCharacters[$character] < self::$maxOccurrences) {
$length++;
$usedCharacters[$character]++;
}
if (!array_key_exists($character, $usedCharacters)) {
$usedCharacters[$character] = 1;
$length++;
}
}

return $length;
}
return $length;
}
}
Loading