| 1 |
<?php |
| 2 |
|
| 3 |
namespace passster; |
| 4 |
|
| 5 |
class PS_Helper |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Set AMP headers |
| 9 |
* |
| 10 |
* @param string $auth the current $_POST argument. |
| 11 |
* @param string $password the current password. |
| 12 |
* @return void |
| 13 |
*/ |
| 14 |
public static function set_amp_headers( $auth, $password ) |
| 15 |
{ |
| 16 |
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) { |
| 17 |
|
| 18 |
if ( !empty($_POST) ) { |
| 19 |
header( 'Content-type: application/json' ); |
| 20 |
header( 'Access-Control-Allow-Credentials: true' ); |
| 21 |
header( 'Access-Control-Allow-Origin: ' . \get_bloginfo( 'url' ) . '.cdn.ampproject.org' ); |
| 22 |
header( 'AMP-Access-Control-Allow-Source-Origin: ' . \get_bloginfo( 'url' ) ); |
| 23 |
header( 'Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin' ); |
| 24 |
header( 'AMP-Redirect-To: ' . \get_the_permalink() ); |
| 25 |
header( 'Access-Control-Expose-Headers: AMP-Redirect-To, AMP-Access-Control-Allow-Source-Origin' ); |
| 26 |
if ( $password === $_POST['passster_password'] ) { |
| 27 |
setcookie( |
| 28 |
'passster', |
| 29 |
base64_encode( $password ), |
| 30 |
time() + 2592000, |
| 31 |
'/' |
| 32 |
); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get string between two strings. |
| 41 |
* |
| 42 |
* @param string $string string to find. |
| 43 |
* @param string $start start string. |
| 44 |
* @param string $end end string. |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
public static function get_string_between( $string, $start, $end ) |
| 48 |
{ |
| 49 |
$string = ' ' . $string; |
| 50 |
$ini = strpos( $string, $start ); |
| 51 |
if ( $ini == 0 ) { |
| 52 |
return ''; |
| 53 |
} |
| 54 |
$ini += strlen( $start ); |
| 55 |
$len = strpos( $string, $end, $ini ) - $ini; |
| 56 |
return substr( $string, $ini, $len ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Preg matches shortcode and return cleaned output. |
| 61 |
* |
| 62 |
* @param string $content given content. |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
public static function get_shortcode_content( $content, $password ) |
| 66 |
{ |
| 67 |
preg_match( '/\\[passster+.*.".*' . $password . '.*".*?[\\]]/', $content, $matches ); |
| 68 |
preg_match( '/\\[passster+.*."true".*?[\\]]/', $content, $captcha_matches ); |
| 69 |
$string = ''; |
| 70 |
|
| 71 |
if ( isset( $matches ) && !empty($matches) ) { |
| 72 |
foreach ( $matches as $match ) { |
| 73 |
$string = self::get_string_between( $content, $match, '[/passster]' ); |
| 74 |
} |
| 75 |
} elseif ( isset( $captcha_matches ) && !empty($captcha_matches) ) { |
| 76 |
foreach ( $captcha_matches as $ca_match ) { |
| 77 |
$string = self::get_string_between( $content, $ca_match, '[/passster]' ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return $string; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get short URL from bitly. |
| 86 |
* |
| 87 |
* @param string $unlock_link unlock URL from Passster. |
| 88 |
* @return string |
| 89 |
*/ |
| 90 |
public static function get_bitly_url( $unlock_link ) |
| 91 |
{ |
| 92 |
$advanced_options = wp_parse_args( get_option( 'passster_advanced_settings' ), PS_ADMIN::get_defaults( 'passster_advanced_settings' ) ); |
| 93 |
$url = 'https://api-ssl.bitly.com/v3/shorten?longUrl=' . $unlock_link . '&access_token=' . $advanced_options['passster_bitly_access_key']; |
| 94 |
$ch = curl_init( $url ); |
| 95 |
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET' ); |
| 96 |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); |
| 97 |
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); |
| 98 |
$result = curl_exec( $ch ); |
| 99 |
$response = json_decode( $result ); |
| 100 |
if ( is_object( $response ) ) { |
| 101 |
return $response->data->url; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
} |