| @@ -1,10 +1,45 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace passster; |
| 4 | 4 | |
| 5 | -class PS_Helper | |
| 6 | -{ | |
| 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 | + | |
| 7 | 42 | /** |
| 8 | 43 | * Get string between two strings. |
| 9 | 44 | * |
| 10 | 45 | * @param string $string string to find. |
| @@ -12,10 +47,9 @@ | ||
| 12 | 47 | * @param string $end end string. |
| 13 | 48 | * |
| 14 | 49 | * @return string |
| 15 | 50 | */ |
| 16 | - public static function get_string_between( string $string, string $start, string $end ) : string | |
| 17 | - { | |
| 51 | + public static function get_string_between( string $string, string $start, string $end ) : string { | |
| 18 | 52 | $string = ' ' . $string; |
| 19 | 53 | $ini = strpos( $string, $start ); |
| 20 | 54 | if ( $ini == 0 ) { |
| 21 | 55 | return ''; |
| @@ -23,9 +57,9 @@ | ||
| 23 | 57 | $ini += strlen( $start ); |
| 24 | 58 | $len = strpos( $string, $end, $ini ) - $ini; |
| 25 | 59 | return substr( $string, $ini, $len ); |
| 26 | 60 | } |
| 27 | - | |
| 61 | + | |
| 28 | 62 | /** |
| 29 | 63 | * Preg matches shortcode and return cleaned output. |
| 30 | 64 | * |
| 31 | 65 | * @param string $content given content. |
| @@ -31,17 +65,26 @@ | ||
| 31 | 65 | * @param string $content given content. |
| 32 | 66 | * |
| 33 | 67 | * @return string |
| 34 | 68 | */ |
| 35 | - public static function get_shortcode_content( string $content, $password ) : string | |
| 36 | - { | |
| 37 | - preg_match( '/\\[passster.*password="' . $password . '".*?\\]/', $content, $matches ); | |
| 69 | + public static function get_shortcode_content( string $content, $password, string $mode = 'password' ) { | |
| 38 | 70 | $string = ''; |
| 39 | - if ( isset( $matches ) && !empty($matches) ) { | |
| 40 | - foreach ( $matches as $match ) { | |
| 41 | - $string = self::get_string_between( $content, $match, '[/passster]' ); | |
| 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]' ); | |
| 42 | 79 | } |
| 80 | + return $string; | |
| 43 | 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 | + } | |
| 44 | 87 | return $string; |
| 45 | 88 | } |
| 46 | 89 | |
| 47 | -} | |
| 90 | +} | |