Skip to content

Commit ae8b2bb

Browse files
authored
Merge pull request #10 from pdsinterop/cleanup/routes
Cleanup to remove logic from the routes and to their own class.
2 parents 0a33933 + 8a6762e commit ae8b2bb

File tree

15 files changed

+717
-603
lines changed

15 files changed

+717
-603
lines changed

lib/Mailer.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ public static function getMailer() {
1414
// Settings
1515
$mailer->IsSMTP();
1616
$mailer->CharSet = 'UTF-8';
17-
$mailer->Host = MAILER['host'];
18-
$mailer->SMTPDebug = 0;
19-
$mailer->Port = MAILER['port'];
17+
$mailer->Host = MAILER['host'];
18+
$mailer->SMTPDebug = 0;
19+
$mailer->Port = MAILER['port'];
2020
if (isset(MAILER['user'])) {
21-
$mailer->SMTPAuth = true;
22-
$mailer->Username = MAILER['user'];
23-
$mailer->Password = MAILER['password'];
21+
$mailer->SMTPAuth = true;
22+
$mailer->Username = MAILER['user'];
23+
$mailer->Password = MAILER['password'];
2424
}
2525
$mailer->isHTML(true);
2626
$mailer->setFrom(MAILER['from']);
@@ -50,10 +50,10 @@ public static function sendAccountCreated($data) {
5050
$mailer->addAddress($mailTo);
5151

5252
$mailer->Subject = $mailSubject;
53-
$mailer->Body = $mailHtmlBody;
53+
$mailer->Body = $mailHtmlBody;
5454
$mailer->AltBody = $mailPlainBody;
5555

56-
$mailer->send();
56+
return $mailer->send();
5757
}
5858

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

8181
$mailer->Subject = $mailSubject;
82-
$mailer->Body = $mailHtmlBody;
82+
$mailer->Body = $mailHtmlBody;
8383
$mailer->AltBody = $mailPlainBody;
8484

85-
$mailer->send();
85+
return $mailer->send();
8686
}
8787

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

109109
$mailer->Subject = $mailSubject;
110-
$mailer->Body = $mailHtmlBody;
110+
$mailer->Body = $mailHtmlBody;
111111
$mailer->AltBody = $mailPlainBody;
112112

113-
$mailer->send();
113+
return $mailer->send();
114114
}
115115

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

137137
$mailer->Subject = $mailSubject;
138-
$mailer->Body = $mailHtmlBody;
138+
$mailer->Body = $mailHtmlBody;
139139
$mailer->AltBody = $mailPlainBody;
140140

141-
$mailer->send();
141+
return $mailer->send();
142142
}
143143
}

lib/PasswordValidator.php

Lines changed: 96 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,114 @@
11
<?php
22
/*
3-
Code modified from https://gitlab.com/garybell/password-validation/ (MIT licensed)
3+
Code modified from https://gitlab.com/garybell/password-validation/ (MIT licensed)
44
*/
55
namespace Pdsinterop\PhpSolid;
66

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

14-
/**
15-
* The maximum number of times the same character can appear in the password
16-
* @var int
17-
*/
18-
private static int $maxOccurrences = 2;
14+
/**
15+
* The maximum number of times the same character can appear in the password
16+
* @var int
17+
*/
18+
private static int $maxOccurrences = 2;
1919

20-
/**
21-
* Get the base amount of characters from the characters used in the password.
22-
* This is the number of possible characters to pick from in the used character sets
23-
* i.e. 26 for only lower case passwords
24-
* @param $password
25-
* @return int
26-
*/
27-
public static function getBase(string $password): int
28-
{
29-
$characters = str_split($password);
30-
$base = 0;
31-
$hasSpecial = false;
32-
$hasLower = false;
33-
$hasUpper = false;
34-
$hasDigits = false;
20+
/**
21+
* Get the base amount of characters from the characters used in the password.
22+
* This is the number of possible characters to pick from in the used character sets
23+
* i.e. 26 for only lower case passwords
24+
* @param $password
25+
* @return int
26+
*/
27+
public static function getBase(string $password): int
28+
{
29+
$characters = str_split($password);
30+
$base = 0;
31+
$hasSpecial = false;
32+
$hasLower = false;
33+
$hasUpper = false;
34+
$hasDigits = false;
3535

36-
foreach ($characters as $character) {
37-
if (!$hasLower && strpos(self::$lowercaseCharacters, $character) !== false) {
38-
$hasLower = true;
39-
$base += strlen(self::$lowercaseCharacters);
40-
}
41-
if (!$hasUpper && strpos(self::$uppercaseCharacters, $character) !== false) {
42-
$hasUpper = true;
43-
$base += strlen(self::$uppercaseCharacters);
44-
}
45-
if (!$hasSpecial && strpos(self::$specialCharacters, $character) !== false) {
46-
$hasSpecial = true;
47-
$base += strlen(self::$specialCharacters);
48-
}
49-
if (!$hasDigits && strpos(self::$numbers, $character) !== false) {
50-
$hasDigits = true;
51-
$base += strlen(self::$numbers);
52-
}
36+
foreach ($characters as $character) {
37+
if (!$hasLower && strpos(self::$lowercaseCharacters, $character) !== false) {
38+
$hasLower = true;
39+
$base += strlen(self::$lowercaseCharacters);
40+
}
41+
if (!$hasUpper && strpos(self::$uppercaseCharacters, $character) !== false) {
42+
$hasUpper = true;
43+
$base += strlen(self::$uppercaseCharacters);
44+
}
45+
if (!$hasSpecial && strpos(self::$specialCharacters, $character) !== false) {
46+
$hasSpecial = true;
47+
$base += strlen(self::$specialCharacters);
48+
}
49+
if (!$hasDigits && strpos(self::$numbers, $character) !== false) {
50+
$hasDigits = true;
51+
$base += strlen(self::$numbers);
52+
}
5353

54-
if (
55-
strpos(self::$lowercaseCharacters, $character) === false
56-
&& strpos(self::$uppercaseCharacters, $character) === false
57-
&& strpos(self::$specialCharacters, $character) === false
58-
&& strpos(self::$numbers, $character) === false
59-
) {
60-
$base++;
61-
}
62-
}
54+
if (
55+
strpos(self::$lowercaseCharacters, $character) === false
56+
&& strpos(self::$uppercaseCharacters, $character) === false
57+
&& strpos(self::$specialCharacters, $character) === false
58+
&& strpos(self::$numbers, $character) === false
59+
) {
60+
$base++;
61+
}
62+
}
6363

64-
return $base;
65-
}
64+
return $base;
65+
}
6666

67-
/**
68-
* get the calculated entropy of the password based on the rules for excluding duplicate characters
69-
* If a password is in the banned list, entropy will be 0.
70-
* @see bannedPassords()
71-
* @param string $password
72-
* @param array $bannedPasswords a custom list of passwords to disallow
73-
* @return float
74-
*/
75-
public static function getEntropy(string $password, array $bannedPasswords = []): float
76-
{
77-
if (in_array(strtolower($password), $bannedPasswords)) {
78-
// these are so weak, we just want to outright ban them. Entropy will be 0 for anything in this list.
79-
return 0;
80-
}
81-
$base = self::getBase($password);
82-
$length = self::getLength($password);
67+
/**
68+
* get the calculated entropy of the password based on the rules for excluding duplicate characters
69+
* If a password is in the banned list, entropy will be 0.
70+
* @see bannedPassords()
71+
* @param string $password
72+
* @param array $bannedPasswords a custom list of passwords to disallow
73+
* @return float
74+
*/
75+
public static function getEntropy(string $password, array $bannedPasswords = []): float
76+
{
77+
if (in_array(strtolower($password), $bannedPasswords)) {
78+
// these are so weak, we just want to outright ban them. Entropy will be 0 for anything in this list.
79+
return 0;
80+
}
81+
$base = self::getBase($password);
82+
$length = self::getLength($password);
8383

84-
$decimalPlaces = 2;
85-
return number_format(log($base ** $length), $decimalPlaces);
86-
}
84+
$decimalPlaces = 2;
85+
return number_format(log($base ** $length), $decimalPlaces);
86+
}
8787

88-
/**
89-
* Check the length of the password based on known rules
90-
* Characters will only be counted a maximum of 2 times e.g. aaa has length 2
91-
* @param $password
92-
* @return int
93-
*/
94-
public static function getLength(string $password): int
95-
{
96-
$usedCharacters = [];
97-
$characters = str_split($password);
98-
$length = 0;
88+
/**
89+
* Check the length of the password based on known rules
90+
* Characters will only be counted a maximum of 2 times e.g. aaa has length 2
91+
* @param $password
92+
* @return int
93+
*/
94+
public static function getLength(string $password): int
95+
{
96+
$usedCharacters = [];
97+
$characters = str_split($password);
98+
$length = 0;
9999

100-
foreach ($characters as $character)
101-
{
102-
if (array_key_exists($character, $usedCharacters) && $usedCharacters[$character] < self::$maxOccurrences) {
103-
$length++;
104-
$usedCharacters[$character]++;
105-
}
106-
if (!array_key_exists($character, $usedCharacters)) {
107-
$usedCharacters[$character] = 1;
108-
$length++;
109-
}
110-
}
100+
foreach ($characters as $character)
101+
{
102+
if (array_key_exists($character, $usedCharacters) && $usedCharacters[$character] < self::$maxOccurrences) {
103+
$length++;
104+
$usedCharacters[$character]++;
105+
}
106+
if (!array_key_exists($character, $usedCharacters)) {
107+
$usedCharacters[$character] = 1;
108+
$length++;
109+
}
110+
}
111111

112-
return $length;
113-
}
112+
return $length;
113+
}
114114
}

0 commit comments

Comments
 (0)