PluginProbe
Passster – Password Protect Pages and Content / 4.3.7
Passster – Password Protect Pages and Content v4.3.7
4.3.15 4.3.14 4.3.12 4.3.13 4.3.11 4.3.10 4.3.9 4.3.8 4.3.7 4.3.6 4.3.5 trunk 3.5.4 3.5.5.2 3.5.5.8 3.5.5.9 4.0 4.1.4 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 4.2.15 4.2.16 All 47 releases
content-protector / inc / class-ps-rest-handler.php

class-ps-rest-handler.php in Passster – Password Protect Pages and Content 4.3.7, at inc/class-ps-rest-handler.php

177 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace passster;
4
5 class PS_Rest_Handler {
6
7 /**
8 * Contains instance or null
9 *
10 * @var object|null
11 */
12 private static $instance = null;
13
14 /**
15 * Returns instance of PS_Rest_Handler.
16 *
17 * @return object
18 */
19 public static function get_instance() {
20
21 if ( null === self::$instance ) {
22 self::$instance = new self();
23 }
24
25 return self::$instance;
26 }
27
28 /**
29 * Constructor for PS_Rest_Handler
30 */
31 public function __construct() {
32 add_filter( 'rest_authentication_errors', array( $this, 'restrict_rest_access' ) );
33 add_filter( 'rest_prepare_post', array( $this, 'filter_rest_response' ), 10, 3 );
34
35 add_action( 'rest_api_init', array( $this, 'register_nonce_routes' ) );
36 }
37
38 public function restrict_rest_access( $result ) {
39
40 // If a previous authentication check was applied,
41 // pass that result along without modification.
42 if ( true === $result || is_wp_error( $result ) ) {
43 return $result;
44 }
45
46 // Allow Passster public endpoints (unlock flow must work for guests).
47 $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
48 $public_passster_paths = array(
49 '/passster/v1/nonces',
50 '/passster/v1/unlock',
51 '/passster/v1/hash',
52 '/passster/v1/captcha',
53 '/passster/v1/logout',
54 );
55 foreach ( $public_passster_paths as $path ) {
56 if ( strpos( $request_uri, $path ) !== false ) {
57 return $result;
58 }
59 }
60
61 // Check if request is coming from a frontend page builder.
62 if ( current_user_can( 'manage_options' ) && ( is_plugin_active( 'elementor/elementor.php' ) || is_plugin_active( 'livecanvas/livecanvas-plugin-index.php' ) || is_plugin_active( 'divi-builder/divi-builder.php' ) || is_plugin_active( 'oxygen/functions.php' ) || is_plugin_active( 'pagelayer/pagelayer.php' ) ) ) {
63 return $result;
64 }
65
66 // Global protection activated?
67 $settings = get_option( 'passster' );
68 $protection_enabled = $settings['activate_global_protection'] ?? false;
69
70 // Check if access is allowed.
71 $valid = false;
72
73 if ( ! empty( $settings['global_protection_id'] ) ) {
74 $page_id = esc_attr( $settings['global_protection_id'] );
75 $atts = array( 'password' => get_post_meta( $page_id, 'passster_password', true ) );
76 $valid = PS_Conditional::is_valid( $atts );
77 }
78
79 if ( $protection_enabled && ! $valid && ! current_user_can( 'manage_options' ) ) {
80 return new \WP_Error(
81 'rest_not_logged_in',
82 __( 'You are not allowed to access this content. Please authenticate with a password first.', 'content-protector' ),
83 array( 'status' => 401 )
84 );
85 }
86
87 return $result;
88 }
89
90 /**
91 * Filter REST API response to hide sensitive password data from unauthenticated users
92 *
93 * @param \WP_REST_Response $response The response object.
94 * @param \WP_Post $post The post object.
95 * @param \WP_REST_Request $request The request object.
96 * @return \WP_REST_Response
97 */
98 public function filter_rest_response( $response, $post, $request ) {
99 // Only filter for unauthenticated users or users without proper permissions
100 if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) {
101 return $response;
102 }
103
104 // Get the response data
105 $data = $response->get_data();
106
107 // List of sensitive meta fields that should be hidden
108 $sensitive_fields = array(
109 'passster_password',
110 'passster_passwords',
111 'passster_password_list',
112 'passster_password_lists',
113 );
114
115 // Remove sensitive fields from meta if they exist
116 if ( isset( $data['meta'] ) && is_array( $data['meta'] ) ) {
117 foreach ( $sensitive_fields as $field ) {
118 if ( isset( $data['meta'][ $field ] ) ) {
119 unset( $data['meta'][ $field ] );
120 }
121 }
122 }
123
124 if ( $post && class_exists( 'passster\PS_Category_Lock' ) && PS_Category_Lock::get_instance()->get_active_category_lock( $post->ID ) ) {
125 if ( isset( $data['content']['rendered'] ) ) {
126 $data['content']['rendered'] = '';
127 }
128 if ( isset( $data['excerpt']['rendered'] ) ) {
129 $data['excerpt']['rendered'] = '';
130 }
131 }
132
133 // Update the response data
134 $response->set_data( $data );
135
136 return $response;
137 }
138
139 public function register_nonce_routes() {
140
141 register_rest_route(
142 'passster/v1',
143 '/nonces',
144 array(
145 'methods' => 'POST',
146 'callback' => array( $this, 'get_all_nonces' ),
147 'permission_callback' => '__return_true',
148 )
149 );
150 }
151 public function get_all_nonces() {
152 $user_id = wp_validate_auth_cookie( '', 'logged_in' );
153
154 if ( $user_id ) {
155 wp_set_current_user( $user_id );
156 }
157
158 $response = new \WP_REST_Response(
159 array(
160 'nonce' => wp_create_nonce( 'ps-password-nonce' ),
161 'hash_nonce' => wp_create_nonce( 'ps-hash-nonce' ),
162 'logout_nonce' => wp_create_nonce( 'ps-logout-nonce' ),
163 )
164 );
165
166 // Prevent caching of nonces - they are user-session specific.
167 $response->header( 'Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0, private' );
168 $response->header( 'Pragma', 'no-cache' );
169 $response->header( 'Expires', '0' );
170 $response->header( 'X-LiteSpeed-Cache-Control', 'no-cache' );
171 $response->header( 'X-Accel-Expires', '0' ); // Nginx
172 $response->header( 'Surrogate-Control', 'no-store' ); // Varnish/CDN
173
174 return $response;
175 }
176 }
177