PluginProbe
Booking Calendar / 10.10
Booking Calendar v10.10
11.8.2 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 All 202 releases
booking / includes / _capacity / captcha_simple_text.php

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

175 lines 6.4 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 (
21 ( 'On' === get_bk_option( 'booking_is_use_captcha' ) )
22 && ( ! $is_from_admin_panel )
23 && ( ( isset( $original_ajx_search_params['captcha_user_input'] ) ) && ( isset( $original_ajx_search_params['captcha_chalange'] ) ) )
24 ) {
25
26 if ( ! wpbc_captcha__simple__is_ansfer_correct( $request_params['captcha_user_input'], $request_params['captcha_chalange'] ) ) {
27
28 $captcha_arr = wpbc_captcha__simple__generate_new();
29
30 $ajx_data_arr = array();
31 $ajx_data_arr['status'] = 'error';
32 $ajx_data_arr['status_error'] = 'captcha_simple_wrong';
33 $ajx_data_arr['captcha__simple'] = $captcha_arr;
34 $ajx_data_arr['ajx_after_action_message'] = __( 'The code you entered is incorrect', 'booking' );
35 $ajx_data_arr['ajx_after_action_message_status'] = 'warning';
36
37 wp_send_json( array(
38 'ajx_data' => $ajx_data_arr,
39 'ajx_search_params' => $original_ajx_search_params,
40 'ajx_cleaned_params' => $request_params,
41 'resource_id' => $request_params['resource_id']
42 ) );
43 // After this page will die;
44 }
45 }
46 }
47
48
49
50 // ---------------------------------------------------------------------------------------------------------------------
51 // CAPTCHA Support
52 // ---------------------------------------------------------------------------------------------------------------------
53
54 /**
55 * Is entered CAPTCHA correct ?
56 *
57 * @param string $captcha_user_input user entrance
58 * @param string $captcha_chalange chalange
59 *
60 * @return bool
61 */
62 function wpbc_captcha__simple__is_ansfer_correct( $captcha_user_input, $captcha_chalange ) {
63
64 if ( ( empty( $captcha_user_input ) ) || ( empty( $captcha_chalange ) ) ) {
65 return false;
66 }
67
68 $captcha_instance = new wpdevReallySimpleCaptcha();
69 $correct = $captcha_instance->check( $captcha_chalange, $captcha_user_input );
70
71 return $correct;
72 }
73
74
75 /**
76 * Generate new CAPTCHA image and return URL to this image and challenge code
77 *
78 * @return array [
79 'url' => $captcha_url,
80 'challenge' => $captcha_challenge
81 ]
82 */
83 function wpbc_captcha__simple__generate_new() {
84
85 $captcha_instance = new wpdevReallySimpleCaptcha();
86
87 // Clean up dead files older than 2 minutes
88 $captcha_instance->cleanup( 2 ); // FixIn: 7.0.1.67.
89
90 //$captcha_instance->img_size = array( 72, 24 );
91 /* Background color of CAPTCHA image. RGB color 0-255 */
92 //$captcha_instance->bg = array( 0, 0, 0 );//array( 255, 255, 255 );
93 /* Foreground (character) color of CAPTCHA image. RGB color 0-255 */
94 //$captcha_instance->fg = array( 255, 255, 255 );//array( 0, 0, 0 );
95 /* Coordinates for a text in an image. I don't know the meaning. Just adjust. */
96 //$captcha_instance->base = array( 6, 18 );
97 /* Font size */
98 //$captcha_instance->font_size = 14;
99 /* Width of a character */
100 //$captcha_instance->font_char_width = 15;
101 /* Image type. 'png', 'gif' or 'jpeg' */
102 //$captcha_instance->img_type = 'png';
103
104
105 $word = $captcha_instance->generate_random_word();
106 $prefix = wp_rand();
107
108 $captcha_instance->generate_image( $prefix, $word );
109
110 $filename = $prefix . '.png';
111
112 $captcha_url = WPBC_PLUGIN_URL . '/js/captcha/tmp/' . $filename;
113 $captcha_challenge = substr( $filename, 0, strrpos( $filename, '.' ) );
114
115 return array(
116 'url' => $captcha_url,
117 'challenge' => $captcha_challenge
118 );
119 }
120
121
122 /**
123 * Generate initial HTML content for CAPTCHA in booking form
124 *
125 * @param $resource_id
126 *
127 * @return string|true
128 */
129 function wpbc_captcha__simple__generate_html_content( $resource_id ) {
130
131 if ( true !== wpbc_captcha__simple__is_installed() ) {
132 return wpbc_captcha__simple__is_installed();
133 }
134
135 $captcha_arr = wpbc_captcha__simple__generate_new();
136
137 $captcha_url = $captcha_arr['url'];
138 $captcha_challenge = $captcha_arr['challenge'];
139 $html = '<span class="wpbc_text_captcha_container wpdev-form-control-wrap ">';
140 $html .= '<input autocomplete="off" type="hidden" name="wpdev_captcha_challenge_' . $resource_id . '" id="wpdev_captcha_challenge_' . $resource_id . '" value="' . $captcha_challenge . '" />';
141 $html .= '<input autocomplete="off" type="text" class="captachinput" value="" name="captcha_input' . $resource_id . '" id="captcha_input' . $resource_id . '" />';
142 // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
143 $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 . '" />';
144 $html .= '</span>';
145
146 return $html;
147 }
148
149
150 /**
151 * Check if captcha can work here
152 *
153 * @return string|true if true then can work, Otherwise return error message
154 */
155 function wpbc_captcha__simple__is_installed() {
156
157 // FixIn: 8.8.3.5.
158 if ( function_exists( 'gd_info' ) ) {
159
160 return true;
161 /*
162 $gd_info = gd_info();
163 if ( isset( $gd_info['GD Version'] ) ) {
164 $gd_info = $gd_info['GD Version'];
165 } else {
166 $gd_info = wp_json_encode( $gd_info );
167 }
168 */
169 } else {
170 return '<strong>Error!</strong> CAPTCHA requires the GD library activated in your PHP configuration.'
171 .'Please check more <a href="https://wpbookingcalendar.com/faq/captcha-showing-problems/">here</a>.';
172 }
173
174
175 }