| 1 |
<?php |
| 2 |
|
| 3 |
namespace passster; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
class PS_Helper { |
| 7 |
public static function remember_unlock( string $proof ) { |
| 8 |
$options = get_option( 'passster', array() ); |
| 9 |
if ( !empty( $options['disable_cookie'] ) ) { |
| 10 |
return; |
| 11 |
} |
| 12 |
$existing = array(); |
| 13 |
if ( !empty( $_COOKIE['passster'] ) ) { |
| 14 |
$existing = array_filter( explode( '|', esc_html( wp_unslash( $_COOKIE['passster'] ) ) ) ); |
| 15 |
} |
| 16 |
if ( in_array( $proof, $existing, true ) ) { |
| 17 |
return; |
| 18 |
} |
| 19 |
$existing[] = $proof; |
| 20 |
$unit = $options['cookie_duration_unit'] ?? 'days'; |
| 21 |
$duration = (int) ($options['cookie_duration'] ?? 1); |
| 22 |
switch ( $unit ) { |
| 23 |
case 'hours': |
| 24 |
$expires = time() + $duration * HOUR_IN_SECONDS; |
| 25 |
break; |
| 26 |
case 'minutes': |
| 27 |
$expires = time() + $duration * MINUTE_IN_SECONDS; |
| 28 |
break; |
| 29 |
default: |
| 30 |
$expires = time() + $duration * DAY_IN_SECONDS; |
| 31 |
break; |
| 32 |
} |
| 33 |
setcookie( 'passster', implode( '|', $existing ), array( |
| 34 |
'expires' => $expires, |
| 35 |
'path' => '/', |
| 36 |
'secure' => is_ssl(), |
| 37 |
'httponly' => false, |
| 38 |
'samesite' => 'Strict', |
| 39 |
) ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get string between two strings. |
| 44 |
* |
| 45 |
* @param string $string string to find. |
| 46 |
* @param string $start start string. |
| 47 |
* @param string $end end string. |
| 48 |
* |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public static function get_string_between( string $string, string $start, string $end ) : string { |
| 52 |
$string = ' ' . $string; |
| 53 |
$ini = strpos( $string, $start ); |
| 54 |
if ( $ini == 0 ) { |
| 55 |
return ''; |
| 56 |
} |
| 57 |
$ini += strlen( $start ); |
| 58 |
$len = strpos( $string, $end, $ini ) - $ini; |
| 59 |
return substr( $string, $ini, $len ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Preg matches shortcode and return cleaned output. |
| 64 |
* |
| 65 |
* @param string $content given content. |
| 66 |
* |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
public static function get_shortcode_content( string $content, $password, string $mode = 'password' ) { |
| 70 |
$string = ''; |
| 71 |
if ( 'captcha' === $mode ) { |
| 72 |
return $string; |
| 73 |
$recaptcha_re = '/\\[passster[^\\]]*recaptcha="true"[^\\]]*\\]/'; |
| 74 |
$turnstile_re = '/\\[passster[^\\]]*turnstile="true"[^\\]]*\\]/'; |
| 75 |
if ( preg_match( $recaptcha_re, $content, $m ) ) { |
| 76 |
$string = self::get_string_between( $content, $m[0], '[/passster]' ); |
| 77 |
} elseif ( preg_match( $turnstile_re, $content, $m ) ) { |
| 78 |
$string = self::get_string_between( $content, $m[0], '[/passster]' ); |
| 79 |
} |
| 80 |
return $string; |
| 81 |
} |
| 82 |
$password = preg_quote( (string) $password, '/' ); |
| 83 |
$pass_re = '/\\[passster[^\\]]*password="' . $password . '"[^\\]]*\\]/'; |
| 84 |
if ( preg_match( $pass_re, $content, $m ) ) { |
| 85 |
$string = self::get_string_between( $content, $m[0], '[/passster]' ); |
| 86 |
} |
| 87 |
return $string; |
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
|