| 1 |
<?php |
| 2 |
|
| 3 |
namespace passster; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
class PS_Ajax { |
| 7 |
/** |
| 8 |
* Contains instance or null |
| 9 |
* |
| 10 |
* @var object|null |
| 11 |
*/ |
| 12 |
private static $instance = null; |
| 13 |
|
| 14 |
/** |
| 15 |
* Constructor for PS_Public |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
add_action( 'wp_enqueue_scripts', array($this, 'add_public_scripts') ); |
| 19 |
add_action( 'wp_ajax_validate_input', array($this, 'validate_input') ); |
| 20 |
add_action( 'wp_ajax_nopriv_validate_input', array($this, 'validate_input') ); |
| 21 |
add_action( 'wp_ajax_hash_password', array($this, 'hash_password') ); |
| 22 |
add_action( 'wp_ajax_nopriv_hash_password', array($this, 'hash_password') ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Returns instance of PS_Public. |
| 27 |
* |
| 28 |
* @return object |
| 29 |
*/ |
| 30 |
public static function get_instance() { |
| 31 |
if ( null === self::$instance ) { |
| 32 |
self::$instance = new self(); |
| 33 |
} |
| 34 |
return self::$instance; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Validate ajax given input. |
| 39 |
* |
| 40 |
* @return void |
| 41 |
* @throws Exception |
| 42 |
*/ |
| 43 |
public function validate_input() { |
| 44 |
$options = get_option( 'passster' ); |
| 45 |
// check nonce. |
| 46 |
if ( !wp_verify_nonce( $_POST['nonce'], 'ps-password-nonce' ) ) { |
| 47 |
$response = array( |
| 48 |
'error' => 'Security check failed.', |
| 49 |
); |
| 50 |
print wp_json_encode( $response ); |
| 51 |
exit; |
| 52 |
} |
| 53 |
// Default response. |
| 54 |
$response = array( |
| 55 |
'error' => $options['error'], |
| 56 |
'content' => '', |
| 57 |
); |
| 58 |
// Check if input exists. |
| 59 |
if ( !empty( $_POST['input'] ) ) { |
| 60 |
$input = sanitize_text_field( $_POST['input'] ); |
| 61 |
} else { |
| 62 |
$input = ''; |
| 63 |
} |
| 64 |
// prepare validation. |
| 65 |
$remove_spaces = apply_filters( 'passster_remove_spaces_from_list', true ); |
| 66 |
$type = sanitize_text_field( $_POST['type'] ); |
| 67 |
$post_id = sanitize_text_field( $_POST['post_id'] ); |
| 68 |
$protection = sanitize_text_field( $_POST['protection'] ); |
| 69 |
// check protection. |
| 70 |
if ( empty( $protection ) ) { |
| 71 |
$protection = false; |
| 72 |
} |
| 73 |
// prepare content. |
| 74 |
$post = get_post( $post_id ); |
| 75 |
$content = apply_filters( 'passster_compatibility_actions', $post->post_content, $post_id ); |
| 76 |
// if it's an ACF Field. |
| 77 |
if ( !empty( $_POST['acf'] ) ) { |
| 78 |
$acf = esc_html( $_POST['acf'] ); |
| 79 |
$content = \get_field( $acf, $post_id ); |
| 80 |
} |
| 81 |
// Check if redirection. |
| 82 |
$redirect = ''; |
| 83 |
if ( !empty( $_POST['redirect'] ) ) { |
| 84 |
$redirect = esc_url( $_POST['redirect'] ); |
| 85 |
} |
| 86 |
switch ( $type ) { |
| 87 |
case 'password': |
| 88 |
// Get password. |
| 89 |
$password = get_post_meta( $post_id, 'passster_password', true ); |
| 90 |
// Check protection type. |
| 91 |
switch ( $protection ) { |
| 92 |
case 'full': |
| 93 |
if ( !empty( $password ) && $input === $password ) { |
| 94 |
$validation = $this->validate_full_protection( |
| 95 |
$input, |
| 96 |
$post, |
| 97 |
$content, |
| 98 |
$redirect |
| 99 |
); |
| 100 |
if ( false !== $validation ) { |
| 101 |
$response = $validation; |
| 102 |
} |
| 103 |
} |
| 104 |
break; |
| 105 |
case 'area': |
| 106 |
$area_id = esc_html( $_POST['area'] ); |
| 107 |
$password = get_post_meta( $area_id, 'passster_password', true ); |
| 108 |
if ( !empty( $area_id ) && !empty( $password ) && $input === $password ) { |
| 109 |
$area = get_post( $area_id ); |
| 110 |
$validation = $this->validate_area_protection( $input, $area, $redirect ); |
| 111 |
if ( false !== $validation ) { |
| 112 |
$response = $validation; |
| 113 |
} |
| 114 |
} |
| 115 |
break; |
| 116 |
default: |
| 117 |
$content = apply_filters( 'passster_compatibility_actions', PS_Helper::get_shortcode_content( $content, $input ) ); |
| 118 |
$validation = $this->validate_shortcode_protection( |
| 119 |
$input, |
| 120 |
$content, |
| 121 |
$redirect, |
| 122 |
$options |
| 123 |
); |
| 124 |
if ( false !== $validation ) { |
| 125 |
$response = $validation; |
| 126 |
} |
| 127 |
} |
| 128 |
print wp_json_encode( $response ); |
| 129 |
exit; |
| 130 |
} |
| 131 |
print wp_json_encode( $response ); |
| 132 |
exit; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @param $input string given input password. |
| 137 |
* @param $post object current post object. |
| 138 |
* @param $content string given content. |
| 139 |
* @param $redirect bool redirect or not. |
| 140 |
* |
| 141 |
* @return array|false |
| 142 |
*/ |
| 143 |
public function validate_full_protection( |
| 144 |
string $input, |
| 145 |
object $post, |
| 146 |
string $content, |
| 147 |
bool $redirect |
| 148 |
) { |
| 149 |
// Check that its published. |
| 150 |
if ( 'publish' !== $post->post_status ) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
$response = array( |
| 154 |
'success' => true, |
| 155 |
'content' => $content, |
| 156 |
); |
| 157 |
do_action( 'passster_validation_success', $input ); |
| 158 |
if ( $redirect ) { |
| 159 |
$response = array( |
| 160 |
'success' => true, |
| 161 |
'redirect' => true, |
| 162 |
); |
| 163 |
} |
| 164 |
return $response; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* @param $input string given password. |
| 169 |
* @param $area object given area. |
| 170 |
* @param $redirect bool redirect or not. |
| 171 |
* |
| 172 |
* @return array|false |
| 173 |
*/ |
| 174 |
public function validate_area_protection( string $input, object $area, bool $redirect ) { |
| 175 |
// Check that it is an area. |
| 176 |
if ( 'protected_areas' !== $area->post_type ) { |
| 177 |
return false; |
| 178 |
} |
| 179 |
// Check that its published. |
| 180 |
if ( 'publish' !== $area->post_status ) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
$content = apply_filters( 'passster_compatibility_actions', $area->post_content, $area->ID ); |
| 184 |
$response = array( |
| 185 |
'success' => true, |
| 186 |
'content' => $content, |
| 187 |
); |
| 188 |
do_action( 'passster_validation_success', $input ); |
| 189 |
if ( $redirect ) { |
| 190 |
$response = array( |
| 191 |
'success' => true, |
| 192 |
'redirect' => true, |
| 193 |
); |
| 194 |
} |
| 195 |
return $response; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* @param $input string given password. |
| 200 |
* @param $content string current content. |
| 201 |
* @param $redirect bool redirect or not. |
| 202 |
* @param $options array given options. |
| 203 |
* |
| 204 |
* @return array|bool[]|false |
| 205 |
*/ |
| 206 |
public function validate_shortcode_protection( |
| 207 |
string $input, |
| 208 |
string $content, |
| 209 |
bool $redirect, |
| 210 |
array $options |
| 211 |
) { |
| 212 |
if ( !empty( $content ) ) { |
| 213 |
$response = array( |
| 214 |
'success' => true, |
| 215 |
'content' => $content, |
| 216 |
); |
| 217 |
do_action( 'passster_validation_success', $input ); |
| 218 |
return $response; |
| 219 |
} elseif ( 'on' === $options['toggle_ajax'] ) { |
| 220 |
$response = array( |
| 221 |
'success' => true, |
| 222 |
); |
| 223 |
do_action( 'passster_validation_success', $input ); |
| 224 |
if ( $redirect ) { |
| 225 |
$response = array( |
| 226 |
'success' => true, |
| 227 |
'redirect' => true, |
| 228 |
); |
| 229 |
} |
| 230 |
return $response; |
| 231 |
} |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Hashing the password to store in a cookie. |
| 237 |
* @return void |
| 238 |
*/ |
| 239 |
public function hash_password() { |
| 240 |
// check nonce. |
| 241 |
if ( !wp_verify_nonce( $_POST['hash_nonce'], 'ps-hash-nonce' ) ) { |
| 242 |
print wp_json_encode( array( |
| 243 |
'success' => false, |
| 244 |
'error' => 'Security check failed.', |
| 245 |
) ); |
| 246 |
exit; |
| 247 |
} |
| 248 |
// Check if input exists. |
| 249 |
if ( empty( $_POST['password'] ) ) { |
| 250 |
print wp_json_encode( array( |
| 251 |
'success' => false, |
| 252 |
'error' => 'No password provided.', |
| 253 |
) ); |
| 254 |
exit; |
| 255 |
} |
| 256 |
$response = array( |
| 257 |
'success' => true, |
| 258 |
'password' => hash_hmac( 'sha256', esc_html( $_POST['password'] ), get_option( 'passster_secure_key' ) ), |
| 259 |
); |
| 260 |
print wp_json_encode( $response ); |
| 261 |
exit; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Enqueue scripts for shortcode |
| 266 |
* |
| 267 |
* @return void |
| 268 |
*/ |
| 269 |
public function add_public_scripts() { |
| 270 |
$suffix = ( defined( SCRIPT_DEBUG ) && SCRIPT_DEBUG ? '' : '.min' ); |
| 271 |
$options = get_option( 'passster' ); |
| 272 |
wp_enqueue_style( |
| 273 |
'passster-public', |
| 274 |
PASSSTER_URL . '/assets/public/passster-public' . $suffix . '.css', |
| 275 |
array(), |
| 276 |
PASSSTER_VERSION, |
| 277 |
'all' |
| 278 |
); |
| 279 |
wp_enqueue_script( |
| 280 |
'passster-cookie', |
| 281 |
PASSSTER_URL . '/assets/public/cookie.js', |
| 282 |
array('jquery'), |
| 283 |
false, |
| 284 |
false |
| 285 |
); |
| 286 |
wp_enqueue_script( |
| 287 |
'passster-public', |
| 288 |
PASSSTER_URL . '/assets/public/passster-public' . $suffix . '.js', |
| 289 |
array('jquery', 'passster-cookie'), |
| 290 |
PASSSTER_VERSION, |
| 291 |
false |
| 292 |
); |
| 293 |
$shortcodes = array(); |
| 294 |
if ( isset( $options['third_party_shortcodes'] ) && !empty( $options['third_party_shortcodes'] ) ) { |
| 295 |
$shortcodes_in_options = explode( ',', $options['third_party_shortcodes'] ); |
| 296 |
if ( is_array( $shortcodes_in_options ) ) { |
| 297 |
foreach ( $shortcodes_in_options as $shortcode ) { |
| 298 |
$shortcodes[$shortcode] = do_shortcode( str_replace( '{post-id}', get_the_id(), $shortcode ) ); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
$args = array( |
| 303 |
'ajax_url' => admin_url() . 'admin-ajax.php', |
| 304 |
'nonce' => wp_create_nonce( 'ps-password-nonce' ), |
| 305 |
'hash_nonce' => wp_create_nonce( 'ps-hash-nonce' ), |
| 306 |
'logout_nonce' => wp_create_nonce( 'ps-logout-nonce' ), |
| 307 |
'post_id' => get_the_id(), |
| 308 |
'shortcodes' => $shortcodes, |
| 309 |
'permalink' => get_permalink( get_the_id() ), |
| 310 |
); |
| 311 |
if ( isset( $options['cookie_duration_unit'] ) ) { |
| 312 |
$args['cookie_duration_unit'] = esc_html( $options['cookie_duration_unit'] ); |
| 313 |
} else { |
| 314 |
$args['cookie_duration_unit'] = 'days'; |
| 315 |
} |
| 316 |
if ( isset( $options['cookie_duration'] ) ) { |
| 317 |
$args['cookie_duration'] = esc_html( $options['cookie_duration'] ); |
| 318 |
} else { |
| 319 |
$args['cookie_duration'] = 1; |
| 320 |
} |
| 321 |
if ( isset( $options['disable_cookie'] ) ) { |
| 322 |
$args['disable_cookie'] = esc_html( $options['disable_cookie'] ); |
| 323 |
} else { |
| 324 |
$args['disable_cookie'] = false; |
| 325 |
} |
| 326 |
if ( isset( $options['unlock_mode'] ) ) { |
| 327 |
$args['unlock_mode'] = esc_html( $options['unlock_mode'] ); |
| 328 |
} else { |
| 329 |
$args['unlock_mode'] = false; |
| 330 |
} |
| 331 |
wp_localize_script( 'passster-public', 'ps_ajax', $args ); |
| 332 |
// if password type hint used. |
| 333 |
$password_typing = $options['show_password']; |
| 334 |
if ( $password_typing ) { |
| 335 |
wp_enqueue_script( |
| 336 |
'password-typing', |
| 337 |
PASSSTER_URL . '/assets/public/password-typing.js', |
| 338 |
array('jquery'), |
| 339 |
PASSSTER_VERSION, |
| 340 |
false |
| 341 |
); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
} |
| 346 |
|