PluginProbe
Booking Calendar / 11.8.3
Booking Calendar v11.8.3
11.8.3 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 All 203 releases
booking / includes / _booking_hash / booking_hash.php

booking_hash.php in Booking Calendar 11.8.3, at includes/_booking_hash/booking_hash.php

265 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php /**
2 * @version 1.0
3 * @description Booking Hash Functions
4 * @category Booking Hash
5 * @author wpdevelop
6 *
7 * @web-site http://oplugins.com/
8 * @email info@oplugins.com
9 *
10 * @modified 2022-08-05
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
14
15 // H A S H // FixIn: 9.2.3.3.
16
17 /**
18 * Generate an opaque booking hash without relying on database hashing functions.
19 *
20 * Booking hashes are used in customer-facing edit, cancellation, and payment
21 * links. Keep the historical 32-character hexadecimal shape so existing
22 * consumers continue to work, while deriving new values from WordPress random
23 * data and a PHP hashing algorithm supported by the plugin's PHP requirement.
24 *
25 * @return string A 32-character lowercase hexadecimal booking hash.
26 */
27 function wpbc_hash__generate_booking_hash() {
28 $random_source = wp_generate_password( 64, true, true ) . '|' . microtime( true ) . '|' . wp_rand();
29
30 return substr( hash( 'sha256', $random_source ), 0, 32 );
31 }
32
33 /**
34 * Get booking ID and resource ID by booking HASH
35 *
36 * @param $booking_hash
37 *
38 * @return array|false - array( $booking_id, $resource_id ) | false if not found
39 */
40 function wpbc_hash__get_booking_id__resource_id( $booking_hash ) {
41
42 if ( '' === $booking_hash ) {
43 return false;
44 }
45 global $wpdb;
46
47 if ( class_exists( 'wpdev_bk_personal' ) ) {
48
49 $sql = $wpdb->prepare( "SELECT booking_id as id, booking_type as type FROM {$wpdb->prefix}booking as bk WHERE bk.hash = %s", $booking_hash );
50 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
51 $res = $wpdb->get_results( $sql );
52
53 if ( ( ! empty( $res ) ) && ( is_array( $res ) ) && ( isset( $res[0]->id ) ) && ( isset( $res[0]->type ) ) ) { // FixIn: 8.1.2.13.
54 return array( $res[0]->id, $res[0]->type );
55 }
56 } else {
57
58 $sql = $wpdb->prepare( "SELECT booking_id as id FROM {$wpdb->prefix}booking as bk WHERE bk.hash = %s", $booking_hash );
59 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
60 $res = $wpdb->get_results( $sql );
61
62 if ( ( ! empty( $res ) ) && ( is_array( $res ) ) && ( isset( $res[0]->id ) ) ) { // FixIn: 8.1.2.13.
63 return array( $res[0]->id, 1 );
64 }
65 }
66
67 return false;
68 }
69
70
71 /**
72 * Get booking HASH and resource ID
73 * by booking ID
74 *
75 * @param $booking_id
76 *
77 * @return array|false - array( $hash, $resource_id ) | false if not found
78 */
79 function wpbc_hash__get_booking_hash__resource_id( $booking_id ) {
80
81 if ( '' == $booking_id ) {
82 return false;
83 }
84 global $wpdb;
85
86 if ( class_exists( 'wpdev_bk_personal' ) ) {
87
88 $sql = $wpdb->prepare( "SELECT hash, booking_type as type FROM {$wpdb->prefix}booking as bk WHERE bk.booking_id = %d", $booking_id );
89 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
90 $res = $wpdb->get_results( $sql );
91
92 if ( ( ! empty( $res ) ) && ( is_array( $res ) ) && ( isset( $res[0]->hash ) ) && ( isset( $res[0]->type ) ) ) {
93 return array( $res[0]->hash, $res[0]->type );
94 }
95 } else {
96
97 $sql = $wpdb->prepare( "SELECT hash FROM {$wpdb->prefix}booking as bk WHERE bk.booking_id = %d", $booking_id );
98 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
99 $res = $wpdb->get_results( $sql );
100
101 if ( ( ! empty( $res ) ) && ( is_array( $res ) ) && ( isset( $res[0]->hash ) ) ) {
102 return array( $res[0]->hash, 1 );
103 }
104 }
105
106 return false;
107 }
108
109
110 /**
111 * Update booking hash to newly generated. Run after creation/modification of booking in post request
112 *
113 * @param $booking_id
114 * @param $resource_id
115 *
116 * @return void
117 */
118 function wpbc_hash__update_booking_hash( $booking_id, $resource_id = '1' ) {
119 global $wpdb;
120 // FixIn: 10.12.1.5.
121 $update_sql = $wpdb->prepare(
122 "UPDATE {$wpdb->prefix}booking SET hash = %s WHERE booking_id = %d"
123 , wpbc_hash__generate_booking_hash()
124 , $booking_id
125 );
126 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
127 if ( false === $wpdb->query( $update_sql ) ) {
128 ?>
129 <script type="text/javascript"> document.getElementById('submiting<?php echo esc_attr( $resource_id ); ?>').innerHTML = '<div style=&quot;height:20px;width:100%;text-align:center;margin:15px auto;&quot;><?php debuge_error( 'Error during updating hash in BD', __FILE__, __LINE__ ); ?></div>'; </script> <?php
130 die();
131 }
132 }
133
134
135 /**
136 * Get JavaScript for start dates selection in calendar after 1.5 second
137 *
138 * @param array $to_select__dates_sql_arr
139 * @param int $resource_id
140 *
141 * @return string - JavaScript code as text
142 */
143 function wpbc_get_dates_selection_js_code( $to_select__dates_sql_arr, $resource_id ){
144
145 $dates_selection_js_code = '<script type="text/javascript"> ' . wpbc_jq_ready_start(); // FixIn: 10.1.3.7.
146
147 // FixIn: 10.0.0.50.
148 $dates_selection_js_code .= ' var select_dates_in_calendar_id = ' . intval( $resource_id ) . ';';
149 $dates_selection_js_code .= " jQuery( 'body' ).on( 'wpbc_calendar_ajx__loaded_data', function ( event, loaded_resource_id ){ "; // Fire on all booking dates loaded
150 $dates_selection_js_code .= " if ( loaded_resource_id == select_dates_in_calendar_id ){ ";
151
152 $string__dates_sql_arr = array_map( function ( $value ) {
153 $value = explode( ' ', $value );
154 $new_value = "'" . $value[0] . "'";
155 return $new_value;
156 }, $to_select__dates_sql_arr );
157 $string__dates_sql_arr = array_unique($string__dates_sql_arr);
158 $string__dates_sql_str = implode( ',', $string__dates_sql_arr );
159
160 $dates_selection_js_code .= " setTimeout( function (){ wpbc_auto_select_dates_in_calendar( select_dates_in_calendar_id, [" . $string__dates_sql_str . "] ); }, 500 );";
161 $dates_selection_js_code .= " } ";
162 $dates_selection_js_code .= " } ); ";
163
164 $dates_selection_js_code .= wpbc_jq_ready_end() . '</script>'; // FixIn: 10.1.3.7.
165
166 return $dates_selection_js_code;
167 }
168
169
170 /**
171 * Get booking ID, from the $_GET['booking_hash'] in url
172 *
173 * @param $booking_hash if default false, then get hash from $_GET['booking_hash'] Otherwise get it from this parameter
174 *
175 * @return int|string ID of booking or '', if not found
176 */
177 function wpbc_get_booking_id__from_hash_in_url( $booking_hash = false ){
178
179 $result_arr = wpbc_get_booking_arr__from_hash_in_url( $booking_hash );
180
181 return $result_arr['booking_id'];
182 }
183
184
185 /**
186 * Get booking ID, from the $_GET['booking_hash'] in url
187 *
188 * @param $booking_hash if default false, then get hash from $_GET['booking_hash'] Otherwise get it from this parameter
189 *
190 * @return int|string ID of ooking resource or '', if not found
191 */
192 function wpbc_get_resource_id__from_hash_in_url( $booking_hash = false ){
193
194 $result_arr = wpbc_get_booking_arr__from_hash_in_url( $booking_hash );
195
196 return $result_arr['resource_id'];
197 }
198
199
200
201 /**
202 * Get booking data ['booking_id' => 2453, 'resource_id' => 4 ], from the URL -- $_GET['booking_hash']
203 *
204 * @param $booking_hash if default false, then get hash from $_REQUEST['booking_hash'] Otherwise get it from this parameter
205 *
206 * @return array if not found then ['booking_id' => '', 'resource_id' => '']
207 * otherwise ['booking_id' => 2453, 'resource_id' => 4 ]
208 */
209 function wpbc_get_booking_arr__from_hash_in_url( $booking_hash = false ){
210
211 if ( empty( $booking_hash ) ) {
212 $booking_hash = ( isset( $_REQUEST['booking_hash'] ) ) ? sanitize_text_field( wp_unslash( $_REQUEST['booking_hash'] ) ) : ''; /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */
213 }
214
215 $booking_id = '';
216 $resource_id = '';
217
218 if ( ! empty( $booking_hash ) ) {
219 $my_booking_id_type = wpbc_hash__get_booking_id__resource_id( $booking_hash );
220
221 if (false !== $my_booking_id_type ){
222 list( $booking_id, $resource_id ) = $my_booking_id_type;
223 }
224 }
225
226 $result = array(
227 'booking_id' => $booking_id,
228 'resource_id' => $resource_id
229 );
230
231 return $result;
232
233 }
234
235 // FixIn: 10.10.1.1.
236 /**
237 * Change hash of booking after approval / pending / trash / restore booking(s)
238 *
239 * @param integer|string $booking_id_csd - ID(s) of booking(s): integer or comma seperated integer string.
240 * @param bool $is_approve_or_pending - Status of the action: approved or pending | trashed or restored.
241 *
242 * @return void
243 */
244 function wpbc_hook__change_hash__afteraction( $booking_id_csd, $is_approve_or_pending ) {
245
246 $is_change_hash_after_approvement = get_bk_option( 'booking_is_change_hash_after_approvement' );
247
248 if ( 'Off' !== $is_change_hash_after_approvement ) {
249
250 if ( is_numeric( $booking_id_csd ) ) {
251 wpbc_hash__update_booking_hash( intval( $booking_id_csd ) );
252 } else {
253 $booking_id_csd = wpbc_clean_digit_or_csd( $booking_id_csd );
254 $booking_id_arr = explode( ',', $booking_id_csd );
255 foreach ( $booking_id_arr as $booking_id ) {
256 wpbc_hash__update_booking_hash( (int) $booking_id );
257 }
258 }
259 }
260 }
261 add_action( 'wpbc_booking_approved', 'wpbc_hook__change_hash__afteraction', 10, 2 );
262 add_action( 'wpbc_booking_action__approved', 'wpbc_hook__change_hash__afteraction', 10, 2 );
263 add_action( 'wpbc_booking_trash', 'wpbc_hook__change_hash__afteraction', 10, 2 );
264 add_action( 'wpbc_booking_action__trash', 'wpbc_hook__change_hash__afteraction', 10, 2 );
265