License
4 years ago
Notices
3 years ago
pQuery
4 years ago
APIPermissionHelper.php
4 years ago
CdnAssetUrl.php
4 years ago
ConflictResolver.php
4 years ago
Cookies.php
4 years ago
DBCollationChecker.php
4 years ago
DOM.php
4 years ago
DateConverter.php
4 years ago
FreeDomains.php
4 years ago
Helpers.php
4 years ago
Installation.php
4 years ago
ProgressBar.php
4 years ago
SecondLevelDomainNames.php
4 years ago
Security.php
4 years ago
Url.php
4 years ago
index.php
4 years ago
Security.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | namespace MailPoet\Util; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use Exception; |
| 9 | use MailPoet\Entities\NewsletterEntity; |
| 10 | use MailPoet\Entities\SubscriberEntity; |
| 11 | use MailPoet\Newsletter\NewslettersRepository; |
| 12 | use MailPoet\Subscribers\SubscribersRepository; |
| 13 | |
| 14 | class Security { |
| 15 | const HASH_LENGTH = 12; |
| 16 | const UNSUBSCRIBE_TOKEN_LENGTH = 15; |
| 17 | |
| 18 | /** @var NewslettersRepository */ |
| 19 | private $newslettersRepository; |
| 20 | |
| 21 | /** @var SubscribersRepository */ |
| 22 | private $subscribersRepository; |
| 23 | |
| 24 | public function __construct( |
| 25 | NewslettersRepository $newslettersRepository, |
| 26 | SubscribersRepository $subscribersRepository |
| 27 | ) { |
| 28 | $this->newslettersRepository = $newslettersRepository; |
| 29 | $this->subscribersRepository = $subscribersRepository; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Generate random lowercase alphanumeric string. |
| 34 | * 1 lowercase alphanumeric character = 6 bits (because log2(36) = 5.17) |
| 35 | * So 3 bytes = 4 characters |
| 36 | * @param int $length Minimal lenght is 5 |
| 37 | * @return string |
| 38 | */ |
| 39 | public static function generateRandomString($length = 5): string { |
| 40 | $length = max(5, (int)$length); |
| 41 | $string = base_convert( |
| 42 | bin2hex( |
| 43 | random_bytes( // phpcs:ignore |
| 44 | (int)ceil(3 * $length / 4) |
| 45 | ) |
| 46 | ), |
| 47 | 16, |
| 48 | 36 |
| 49 | ); |
| 50 | $result = substr($string, 0, $length); |
| 51 | if (strlen($result) === $length) return $result; |
| 52 | // in very rare occasions we generate a shorter string when random_bytes generates something starting with 0 let's try again |
| 53 | return self::generateRandomString($length); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param int $length Maximal length is 32 |
| 58 | * @return string |
| 59 | */ |
| 60 | public static function generateHash($length = null) { |
| 61 | $length = ($length) ? $length : self::HASH_LENGTH; |
| 62 | $authKey = self::generateRandomString(64); |
| 63 | if (defined('AUTH_KEY')) { |
| 64 | $authKey = AUTH_KEY; |
| 65 | } |
| 66 | return substr( |
| 67 | hash_hmac('sha512', self::generateRandomString(64), $authKey), |
| 68 | 0, |
| 69 | $length |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | static public function generateUnsubscribeToken($model) { |
| 74 | do { |
| 75 | $token = self::generateRandomString(self::UNSUBSCRIBE_TOKEN_LENGTH); |
| 76 | $found = $model::whereEqual('unsubscribe_token', $token)->count(); |
| 77 | } while ($found > 0); |
| 78 | return $token; |
| 79 | } |
| 80 | |
| 81 | public function generateUnsubscribeTokenByEntity($entity): string { |
| 82 | $repository = null; |
| 83 | if ($entity instanceof NewsletterEntity) { |
| 84 | $repository = $this->newslettersRepository; |
| 85 | } elseif ($entity instanceof SubscriberEntity) { |
| 86 | $repository = $this->subscribersRepository; |
| 87 | } else { |
| 88 | throw new Exception('Unsupported Entity type'); |
| 89 | } |
| 90 | |
| 91 | do { |
| 92 | $token = self::generateRandomString(self::UNSUBSCRIBE_TOKEN_LENGTH); |
| 93 | $found = count($repository->findBy(['unsubscribeToken' => $token])); |
| 94 | } while ($found > 0); |
| 95 | return $token; |
| 96 | } |
| 97 | } |
| 98 |