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