PluginProbe
Booking Calendar / 11.7
Booking Calendar v11.7
11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 10.11.2 10.11.3 10.11.4 All 201 releases
booking / includes / _capacity / captcha_simple_text.php

captcha_simple_text.php in Booking Calendar 11.7, at includes/_capacity/captcha_simple_text.php

289 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly // FixIn: 9.8.0.4.
4
5 // ---------------------------------------------------------------------------------------------------------------------
6 // Main function for ajax_ WPBC_AJX_BOOKING__CREATE
7 // ---------------------------------------------------------------------------------------------------------------------
8
9 /**
10 * Check CAPTCHA during Ajax request from booking form and if it's incorrect, then STOP execution and send request to show warning
11 *
12 * @param $request_params [ 'captcha_user_input'=> '...', 'captcha_chalange'=> '...']
13 * @param $is_from_admin_panel true | false
14 * @param $original_ajx_search_params usually $_REQUEST[ $request_prefix ]
15 *
16 * @return void wp_send_json() TO front-end or skip and continue
17 */
18 function wpbc_captcha__in_ajx__check( $request_params, $is_from_admin_panel , $original_ajx_search_params ) {
19
20 if ( ( ! $is_from_admin_panel ) && ( ( isset( $original_ajx_search_params['captcha_user_input'] ) ) && ( isset( $original_ajx_search_params['captcha_chalange'] ) ) ) ) {
21
22 if ( ! wpbc_captcha__simple__is_ansfer_correct( $request_params['captcha_user_input'], $request_params['captcha_chalange'] ) ) {
23
24 $captcha_arr = wpbc_captcha__simple__generate_new();
25
26 $ajx_data_arr = array();
27 $ajx_data_arr['status'] = 'error';
28 $ajx_data_arr['status_error'] = 'captcha_simple_wrong';
29 $ajx_data_arr['captcha__simple'] = $captcha_arr;
30 $ajx_data_arr['ajx_after_action_message'] = wpbc_frontend_messages__get( 'message_captcha_incorrect', array(), $request_params['resource_id'] );
31 $ajx_data_arr['ajx_after_action_message_status'] = 'warning';
32
33 wp_send_json(
34 array(
35 'ajx_data' => $ajx_data_arr,
36 'ajx_search_params' => $original_ajx_search_params,
37 'ajx_cleaned_params' => $request_params,
38 'resource_id' => $request_params['resource_id'],
39 )
40 );
41 // After this page will die;.
42 }
43 }
44 }
45
46
47
48 // ---------------------------------------------------------------------------------------------------------------------
49 // CAPTCHA Support
50 // ---------------------------------------------------------------------------------------------------------------------
51
52 /**
53 * Is entered CAPTCHA correct ?
54 *
55 * @param string $captcha_user_input user entrance
56 * @param string $captcha_chalange chalange
57 *
58 * @return bool
59 */
60 function wpbc_captcha__simple__is_ansfer_correct( $captcha_user_input, $captcha_chalange ) {
61
62 if ( ( empty( $captcha_user_input ) ) || ( empty( $captcha_chalange ) ) ) {
63 return false;
64 }
65
66 $captcha_instance = new wpdevReallySimpleCaptcha();
67 $correct = $captcha_instance->check( $captcha_chalange, $captcha_user_input );
68
69 return $correct;
70 }
71
72
73 /**
74 * Generate new CAPTCHA image and return URL to this image and challenge code
75 *
76 * @return array [
77 'url' => $captcha_url,
78 'challenge' => $captcha_challenge
79 ]
80 */
81 function wpbc_captcha__simple__generate_new() {
82
83 $captcha_instance = new wpdevReallySimpleCaptcha();
84
85 // Clean up dead files older than 2 minutes
86 $captcha_instance->cleanup( 2 ); // FixIn: 7.0.1.67.
87
88 //$captcha_instance->img_size = array( 72, 24 );
89 /* Background color of CAPTCHA image. RGB color 0-255 */
90 //$captcha_instance->bg = array( 0, 0, 0 );//array( 255, 255, 255 );
91 /* Foreground (character) color of CAPTCHA image. RGB color 0-255 */
92 //$captcha_instance->fg = array( 255, 255, 255 );//array( 0, 0, 0 );
93 /* Coordinates for a text in an image. I don't know the meaning. Just adjust. */
94 //$captcha_instance->base = array( 6, 18 );
95 /* Font size */
96 //$captcha_instance->font_size = 14;
97 /* Width of a character */
98 //$captcha_instance->font_char_width = 15;
99 /* Image type. 'png', 'gif' or 'jpeg' */
100 //$captcha_instance->img_type = 'png';
101
102
103 $word = $captcha_instance->generate_random_word();
104 $prefix = wp_rand();
105
106 $captcha_instance->generate_image( $prefix, $word );
107
108 $filename = $prefix . '.png';
109
110 $captcha_url = WPBC_PLUGIN_URL . '/js/captcha/tmp/' . $filename;
111 $captcha_challenge = substr( $filename, 0, strrpos( $filename, '.' ) );
112
113 return array(
114 'url' => $captcha_url,
115 'challenge' => $captcha_challenge
116 );
117 }
118
119
120 /**
121 * Generate initial HTML content for CAPTCHA in booking form
122 *
123 * @param $resource_id
124 *
125 * @return string|true
126 */
127 function wpbc_captcha__simple__generate_html_content( $resource_id ) {
128
129 if ( true !== wpbc_captcha__simple__is_installed() ) {
130 return wpbc_captcha__simple__is_installed();
131 }
132
133 $captcha_arr = wpbc_captcha__simple__generate_new();
134
135 $captcha_url = $captcha_arr['url'];
136 $captcha_challenge = $captcha_arr['challenge'];
137 $html = '<span class="wpbc_text_captcha_container wpdev-form-control-wrap ">';
138 $html .= '<input autocomplete="off" type="hidden" name="wpdev_captcha_challenge_' . $resource_id . '" id="wpdev_captcha_challenge_' . $resource_id . '" value="' . $captcha_challenge . '" />';
139 $html .= '<input autocomplete="off" type="text" class="captachinput" value="" name="captcha_input' . $resource_id . '" id="captcha_input' . $resource_id . '" />';
140 // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
141 $html .= '<img class="captcha_img" id="captcha_img' . $resource_id . '" alt="To show CAPTCHA, please deactivate cache plugin or exclude this page from caching or disable CAPTCHA at WP Booking Calendar - Settings General page in Form Options section." src="' . $captcha_url . '" />';
142 $html .= '</span>';
143
144 return $html;
145 }
146
147
148 /**
149 * Check if captcha can work here
150 *
151 * @return string|true if true then can work, Otherwise return error message
152 */
153 function wpbc_captcha__simple__is_installed() {
154
155 // FixIn: 8.8.3.5.
156 if ( function_exists( 'gd_info' ) ) {
157
158 return true;
159 /*
160 $gd_info = gd_info();
161 if ( isset( $gd_info['GD Version'] ) ) {
162 $gd_info = $gd_info['GD Version'];
163 } else {
164 $gd_info = wp_json_encode( $gd_info );
165 }
166 */
167 } else {
168 return '<strong>Error!</strong> CAPTCHA requires the GD library activated in your PHP configuration.'
169 .'Please check more <a href="https://wpbookingcalendar.com/faq/captcha-showing-problems/">here</a>.';
170 }
171
172
173 }
174
175
176 /**
177 * Decide if CAPTCHA can be shown for current request/context.
178 *
179 * Rules:
180 * - Must be enabled in settings (booking_is_use_captcha = On)
181 * - Must not be a WP Admin page, except plugin settings form page
182 *
183 * @return bool
184 */
185 function wpbc_booking_form_is_captcha_allowed_here() {
186
187 // In admin area we usually do not show CAPTCHA, except booking form builder preview pages.
188 // if ( is_admin() && ( ! wpbc_is_settings_form_page() ) ) { return false; } //.
189
190 // old URI-based logic for edge cases, keep it as extra guard:.
191 $server_request_uri = ( isset( $_SERVER['REQUEST_URI'] ) ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
192
193 // Optional legacy-style admin URI check.
194 $admin_uri = ltrim( str_replace( get_site_url( null, '', 'admin' ), '', admin_url( 'admin.php?' ) ), '/' );
195
196 if (
197 ( '' !== $server_request_uri )
198 && ( false !== strpos( $server_request_uri, $admin_uri ) )
199 && ( ! wpbc_is_settings_form_page() )
200 && ( ! wpbc_is_builder_booking_form_page() )
201 ) {
202 return false;
203 }
204
205 return true;
206 }
207
208
209 /**
210 * Default CAPTCHA placeholder replacement for booking form HTML.
211 *
212 * Replaces [captcha] token with generated CAPTCHA markup (if enabled),
213 * or removes the token when CAPTCHA is disabled/not allowed in current context.
214 *
215 * Add-ons can:
216 * - Replace HTML entirely by filtering 'wpbc_booking_form_captcha_html'
217 * - Or override this whole replacement by using a lower priority on
218 * 'wpbc_booking_form_html__create_captha' / '__create_captcha'
219 *
220 * @param string $form_html Booking form HTML.
221 * @param int $resource_id Booking resource ID.
222 *
223 * @return string
224 */
225 function wpbc_booking_form_html__create_captcha_default( $form_html, $resource_id ) {
226
227 if ( false === strpos( $form_html, '[captcha]' ) ) {
228 return $form_html;
229 }
230
231 $resource_id = (int) $resource_id;
232
233 // If CAPTCHA is not allowed here, remove placeholder.
234 if ( ! wpbc_booking_form_is_captcha_allowed_here() ) {
235 return str_replace( '[captcha]', '', $form_html );
236 }
237
238 /**
239 * Filter: Provide CAPTCHA HTML for booking form.
240 *
241 * Return non-empty string to supply your own CAPTCHA markup (addons).
242 * Return empty string to let WPBC fallback to the built-in simple CAPTCHA.
243 *
244 * @param string $captcha_html CAPTCHA HTML (empty by default).
245 * @param int $resource_id Booking resource ID.
246 * @param array $args Context args.
247 */
248 $captcha_html = apply_filters( 'wpbc_booking_form_captcha_html', '', $resource_id,
249 array(
250 'context' => 'booking_form',
251 'placeholder' => '[captcha]',
252 )
253 );
254
255 if ( '' === $captcha_html ) {
256 // Core fallback (your existing generator).
257 $captcha_html = wpbc_captcha__simple__generate_html_content( $resource_id );
258 }
259
260 return str_replace( '[captcha]', $captcha_html, $form_html );
261 }
262 add_filter( 'wpbc_booking_form_html__create_captcha', 'wpbc_booking_form_html__create_captcha_default', 10, 2 );
263
264
265 /**
266 * How an add-on supplies a different CAPTCHA (example).
267 * add_filter( 'wpbc_booking_form_captcha_html', 'my_addon_wpbc_captcha_html', 20, 3 );
268 *
269 *
270 * Provide custom CAPTCHA HTML (e.g., reCAPTCHA/hCaptcha).
271 *
272 * @param string $captcha_html Current HTML (empty by default).
273 * @param int $resource_id Booking resource ID.
274 * @param array $args Context args.
275 *
276 * @return string
277 *
278 * function my_addon_wpbc_captcha_html( $captcha_html, $resource_id, $args ) {
279 *
280 * // Only replace on booking form rendering context.
281 * if ( empty( $args['context'] ) || ( 'booking_form' !== $args['context'] ) ) {
282 * return $captcha_html;
283 * }
284 *
285 * // Return your custom markup here.
286 * return '<div class="my-addon-captcha">...</div>';
287 * }
288 */
289