| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Base_Challenge_Options |
| 4 |
* |
| 5 |
* @package WP_Defender\Component\Altcha |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component\Altcha; |
| 9 |
|
| 10 |
/** |
| 11 |
* Base class for challenge options. |
| 12 |
* |
| 13 |
* @phpstan-type ChallengeParams array<string, null|scalar> |
| 14 |
*/ |
| 15 |
class Base_Challenge_Options { |
| 16 |
public const DEFAULT_MAX_NUMBER = 1000000; |
| 17 |
|
| 18 |
/** |
| 19 |
* The salt used in hashing. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
public string $salt; |
| 24 |
|
| 25 |
/** |
| 26 |
* Options for creation of a new challenge. |
| 27 |
* |
| 28 |
* @see Challenge_Options for options with sane defaults. |
| 29 |
* |
| 30 |
* @param string $algorithm The hashing algorithm. |
| 31 |
* @param int $max_number The maximum number associated with the challenge. |
| 32 |
* @param null|\DateTimeInterface $expires Optional expiration time for the challenge. |
| 33 |
* @param string $salt The salt used in hashing. |
| 34 |
* @param int $number The number associated with the challenge. |
| 35 |
* @param array $params Optional URL-encoded query parameters. |
| 36 |
*/ |
| 37 |
public function __construct( |
| 38 |
public string $algorithm, |
| 39 |
public int $max_number, |
| 40 |
public ?\DateTimeInterface $expires, |
| 41 |
string $salt, |
| 42 |
public int $number, |
| 43 |
public array $params |
| 44 |
) { |
| 45 |
if ( $expires ) { |
| 46 |
$params['expires'] = $expires->getTimestamp(); |
| 47 |
} |
| 48 |
|
| 49 |
if ( is_array( $params ) && array() !== $params ) { |
| 50 |
$salt .= '?' . http_build_query( $params ); |
| 51 |
} |
| 52 |
|
| 53 |
// Add a delimiter to prevent parameter splicing. |
| 54 |
if ( ! str_ends_with( $salt, '&' ) ) { |
| 55 |
$salt .= '&'; |
| 56 |
} |
| 57 |
|
| 58 |
$this->salt = $salt; |
| 59 |
} |
| 60 |
} |
| 61 |
|