| 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 |
* @throws Exception |
| 39 |
*/ |
| 40 |
public static function is_valid_password( $input, $atts ) : bool |
| 41 |
{ |
| 42 |
// password. |
| 43 |
|
| 44 |
if ( !empty($atts['password']) ) { |
| 45 |
$hash = hash_hmac( 'sha256', $atts['password'], get_option( 'passster_secure_key' ) ); |
| 46 |
if ( hash_equals( $hash, $input ) ) { |
| 47 |
return true; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// passwords. |
| 52 |
|
| 53 |
if ( !empty($atts['passwords']) ) { |
| 54 |
$passwords = explode( ',', str_replace( ' ', '', $atts['passwords'] ) ); |
| 55 |
// Convert passwords to hashes. |
| 56 |
foreach ( $passwords as $password ) { |
| 57 |
$hash = hash_hmac( 'sha256', $password, get_option( 'passster_secure_key' ) ); |
| 58 |
if ( hash_equals( $hash, $input ) ) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// password lists. |
| 65 |
|
| 66 |
if ( !empty($atts['password_list']) ) { |
| 67 |
$passwords_in_list = get_post_meta( $atts['password_list'], 'passster_passwords', true ); |
| 68 |
$passwords_in_list = explode( ',', $passwords_in_list ); |
| 69 |
// Convert passwords to hashes. |
| 70 |
foreach ( $passwords_in_list as $password ) { |
| 71 |
$hash = hash_hmac( 'sha256', $password, get_option( 'passster_secure_key' ) ); |
| 72 |
|
| 73 |
if ( hash_equals( $hash, $input ) ) { |
| 74 |
self::maybe_expire_password_from_list__premium_only( $input, $passwords_in_list, $atts['password_list'] ); |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
} |