| 1 |
<?php |
| 2 |
|
| 3 |
namespace passster; |
| 4 |
|
| 5 |
use Exception ; |
| 6 |
class PS_Conditional |
| 7 |
{ |
| 8 |
/** |
| 9 |
* Check if valid authentication exists. |
| 10 |
* |
| 11 |
* @param array $atts array of attributes. |
| 12 |
* |
| 13 |
* @return boolean |
| 14 |
* @throws Exception |
| 15 |
*/ |
| 16 |
public static function is_valid( array $atts ) : bool |
| 17 |
{ |
| 18 |
$input = ''; |
| 19 |
// is Cookie set? |
| 20 |
if ( !empty($_COOKIE['passster']) ) { |
| 21 |
$input = $_COOKIE['passster']; |
| 22 |
} |
| 23 |
// Valid password. |
| 24 |
if ( self::is_valid_password( $input, $atts ) ) { |
| 25 |
return true; |
| 26 |
} |
| 27 |
// captcha. |
| 28 |
if ( isset( $atts['captcha'] ) ) { |
| 29 |
if ( 'captcha' == $input ) { |
| 30 |
return true; |
| 31 |
} |
| 32 |
} |
| 33 |
// if nothing was correct. |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Validate the password. |
| 39 |
* |
| 40 |
* @param string $input given password to validate. |
| 41 |
* @param array $atts given arguments to check. |
| 42 |
* |
| 43 |
* @return bool |
| 44 |
* @throws Exception |
| 45 |
*/ |
| 46 |
public static function is_valid_password( string $input, array $atts ) : bool |
| 47 |
{ |
| 48 |
// password. |
| 49 |
|
| 50 |
if ( !empty($atts['password']) ) { |
| 51 |
$hash = hash_hmac( 'sha256', $atts['password'], get_option( 'passster_secure_key' ) ); |
| 52 |
if ( hash_equals( $hash, $input ) ) { |
| 53 |
return true; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
} |