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 / _functions / sanitizing.php

sanitizing.php in Booking Calendar 11.7, at includes/_functions/sanitizing.php

775 lines 25.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version 1.0
4 * @package Booking Calendar
5 * @subpackage Security: Escaping & Sanitizing Functions
6 * @category Functions
7 *
8 * @author wpdevelop
9 * @link https://wpbookingcalendar.com/
10 * @email info@wpbookingcalendar.com
11 *
12 * @modified 2024-09-03
13 */
14
15 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
16
17
18 // =====================================================================================================================
19 // == Security: Escaping & Sanitizing ==
20 // =====================================================================================================================
21
22 /**
23 * Check specific parameters in ARRAY and return cleaned params or default values
24 *
25 * @param array $request_params_values_arr = / think like in $_REQUEST parameter /
26 * array(
27 * 'page_num' => 1,
28 * 'page_items_count' => 10,
29 * 'sort' => 'rule_id',
30 * 'sort_type' => 'DESC',
31 * 'status' => '',
32 * 'keyword' => '',
33 * 'create_date' => ''
34 * )
35 * @param array $params_rules = array(
36 * 'page_num' => array( 'validate' => 'd', 'default' => 1 )
37 * , 'page_items_count' => array( 'validate' => 'd', 'default' => 10 )
38 * , 'sort' => array( 'validate' => array( 'rule_id' ), 'default' => 'rule_id' )
39 * , 'sort_type' => array( 'validate' => array( 'ASC', 'DESC'),'default' => 'DESC' )
40 * , 'status' => array( 'validate' => 's', 'default' => '' )
41 * , 'keyword' => array( 'validate' => 's', 'default' => '' )
42 * , 'create_date' => array( 'validate' => 'date', 'default' => '' )
43 * )
44 *
45 *
46 * 'd'; // '1' | ''
47 * 's'; // string !!! Clean 'LIKE' string for DB !!!
48 * 'digit_or_csd'; // '0' | '1,2,3' | ''
49 * 'digit_or_date'; // number | date 2016-07-20
50 *
51 * 'checked_skip_it' // Skip checking
52 * array( '0', 'trash', 'any'); // Elements only listed in array
53 *
54 *@return array $clean_params = Array (
55 * [page_num] => 3
56 * [page_items_count] => 20
57 * [sort] => booking_id
58 * [sort_type] => DESC
59 * [keyword] =>
60 * [source] =>
61 * [create_date] =>
62 * )
63 *
64 *
65 Example of Direct Clean Params:
66
67 $request_params_ajx_booking = array(
68 'page_num' => array( 'validate' => 'd', 'default' => 1 )
69 , 'page_items_count' => array( 'validate' => 'd', 'default' => 10 )
70 , 'sort' => array( 'validate' => array( 'ajx_booking_id' ), 'default' => 'ajx_booking_id' )
71 , 'sort_type' => array( 'validate' => array( 'ASC', 'DESC'),'default' => 'DESC' )
72 , 'status' => array( 'validate' => 's', 'default' => '' )
73 , 'keyword' => array( 'validate' => 's', 'default' => '' )
74 , 'ru_create_date' => array( 'validate' => 'date', 'default' => '' )
75 );
76 $request_params_values = array( // Usually $request_params_values is $_REQUEST
77 'page_num' => 1,
78 'page_items_count' => 3,
79 'sort' => 'ajx_booking_id',
80 'sort_type' => 'DESC',
81 'status' => '',
82 'keyword' => '',
83 'ru_create_date' => ''
84 );
85 $request_params = wpbc_sanitize_params_in_arr( $request_params_values, $request_params_ajx_booking );
86 */
87 function wpbc_sanitize_params_in_arr( $request_params_values_arr, $params_rules ){
88
89 $clean_params = array();
90
91 foreach ( $params_rules as $request_key_name => $clean_type ) {
92
93 if ( isset( $request_params_values_arr[ $request_key_name ] ) ) {
94 $request_value_check = $request_params_values_arr[ $request_key_name ];
95 } else {
96 $request_value_check = false;
97 }
98
99 // If not defined in VALUES (think like in $_REQUEST parameter), then get default value
100 if ( false === $request_value_check ) {
101
102 // D E F A U L T
103 $clean_params[ $request_key_name ] = $params_rules[ $request_key_name ]['default'];
104
105 } else {
106
107 // C L E A N I N G
108 $clean_type = $params_rules[ $request_key_name ]['validate'];
109
110 // Check only values from this Array
111 if ( is_array( $clean_type ) ) {
112
113 $clean_type = array_map( 'strtolower', $clean_type );
114
115 if ( ( isset( $request_value_check ) ) && ( ! in_array( strtolower( $request_value_check ), $clean_type ) ) ) {
116 $clean_type = 'checked_skip_it';
117 $request_value_check = $params_rules[ $request_key_name ]['default']; // Reset it, if value not in array And get default value
118 } else {
119 $clean_type = 'checked_skip_it';
120 }
121 }
122
123 switch ( $clean_type ) {
124
125 case 'checked_skip_it':
126 $clean_params[ $request_key_name ] = $request_value_check;
127 break;
128
129 case 'date': // Date
130 $clean_params[ $request_key_name ] = wpbc_sanitize_date( $request_value_check );
131 break;
132
133 case 'csv_dates': // CSV Dates: '11.11.2025, 12.11.2025, 13.11.2025' or '2024-02-06, 2024-02-10'
134 $clean_params[ $request_key_name ] = wpbc_sanitize_csv_dates( $request_value_check ); // FixIn: 9.9.1.1.
135 break;
136
137 case 'digit_or_date': // digit or Date
138 $clean_params[ $request_key_name ] = wpbc_sanitize_digit_or_date( $request_value_check );
139 break;
140
141 case 'digit_or_csd': // digit or comma separated digit
142 $clean_params[ $request_key_name ] = wpbc_sanitize_digit_or_csd( $request_value_check );
143 break;
144
145 case 's': // string
146 $clean_params[ $request_key_name ] = wpbc_sanitize_text( $request_value_check );
147 break;
148
149 case 'strong': // string
150 $clean_params[ $request_key_name ] = wpbc_sanitize_text_strong( $request_value_check );
151 break;
152
153 case 'array':
154 if ( is_array( $request_value_check ) ) {
155 foreach ( $request_value_check as $check_arr_index => $check_arr_value ) {
156 $request_value_check[ $check_arr_index ] = wpbc_sanitize_text( $check_arr_value ); // Check each option as string
157 }
158 $clean_params[ $request_key_name ] = $request_value_check;
159
160 } else {
161 $clean_params[ $request_key_name ] = $params_rules[ $request_key_name ]['default'];
162 }
163 break;
164
165 case 'digit_or_empty': // digit or ''
166 if ( '' === $request_value_check) {
167 $clean_params[ $request_key_name ] = '';
168 } else {
169 $clean_params[ $request_key_name ] = intval( $request_value_check );
170 }
171 break;
172
173 case 'float_or_empty': // digit or ''
174 if ( '' === $request_value_check) {
175 $clean_params[ $request_key_name ] = '';
176 } else {
177
178 // In case if was entered 10,99 instead of 10.99
179 $request_value_check = str_replace( ',', '.', $request_value_check );
180
181 $clean_params[ $request_key_name ] = floatval( $request_value_check );
182 }
183 break;
184
185 case 'f': // float
186 $clean_params[ $request_key_name ] = floatval( $request_value_check );
187 break;
188
189 case 'd': // digit
190 $clean_params[ $request_key_name ] = intval( $request_value_check );
191 break;
192
193 default:
194 $clean_params[ $request_key_name ] = intval( $request_value_check );
195 break;
196 }
197 }
198 }
199 return $clean_params;
200 }
201
202 /**
203 * Check parameter if it number or comma separated list of numbers
204 *
205 * @param string | array $value
206 *
207 * @return string | array
208 *
209 * Example:
210 * wpbc_sanitize_digit_or_csd( '12,a,45,9' ) => '12,0,45,9'
211 * or
212 * wpbc_sanitize_digit_or_csd( '10a' ) => '10
213 * or
214 * wpbc_sanitize_digit_or_csd( array( '12,a,45,9', '10a' ) ) => array ( '12,0,45,9', '10' )
215 */
216 function wpbc_sanitize_digit_or_csd( $value ) { // FixIn: 6.2.1.4.
217
218 if ( $value === '' ) {
219 return $value;
220 }
221
222 if ( is_array( $value ) ) {
223 foreach ( $value as $key => $check_value ) {
224 $value[ $key ] = wpbc_sanitize_digit_or_csd( $check_value );
225 }
226
227 return $value;
228 }
229
230 $value = str_replace( ';', ',', $value );
231 $array_of_nums = explode( ',', $value );
232
233
234 $result = array();
235 foreach ( $array_of_nums as $check_element ) {
236 $result[] = intval( $check_element );
237 }
238 $result = implode( ',', $result );
239
240 return $result;
241 }
242
243 /**
244 * Check about Valid date or number, like 2016-07-20 and return this date or number
245 *
246 * @param string $value
247 *
248 * @return string | int '2022-05-31' or 5 or ''
249 */
250 function wpbc_sanitize_digit_or_date( $value ) { // FixIn: 6.2.1.4.
251
252 if ( $value === '' ) return $value;
253
254 if ( preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $value ) ) {
255
256 return $value; // Date is valid in format: 2016-07-20
257 } else {
258 return intval( $value );
259 }
260
261 }
262
263 /**
264 * Check about Valid date, like 2016-07-20 and return this date or ''
265 *
266 * @param string $value
267 *
268 * @return string '2022-05-31' or ''
269 */
270 function wpbc_sanitize_date( $value ) { // FixIn: 6.2.1.4.
271
272 if ( $value === '' ) return $value;
273
274 if ( preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $value ) ) {
275
276 return $value; // Date is valid in format: 2016-07-20
277 } else {
278 return '';
279 }
280
281 }
282
283 /**
284 * Check about Valid date, like '31.05.2022 and return this date or ''
285 *
286 * @param string $value
287 *
288 * @return string '31.05.2022' or ''
289 */
290 function wpbc_sanitize_date_dmy( $value ) { // FixIn: 9.9.1.1.
291
292 if ( $value === '' ) return $value;
293
294 if ( preg_match("/^(0[1-9]|[1-2][0-9]|3[0-1]).(0[1-9]|1[0-2]).[0-9]{4}$/", $value ) ) {
295
296 return $value; // Date is valid in format: 31.05.2022
297 } else {
298 return '';
299 }
300
301 }
302
303 /**
304 * Check about Valid date(s), like CSV Dates: such as: '11.11.2025, 12.11.2025, 13.11.2025' or '2024-02-06, 2024-02-10' and then return them sanitized dates or ''
305 *
306 * @param string $value
307 *
308 * @return string '2022-05-31' or ''
309 */
310 function wpbc_sanitize_csv_dates( $value ) { // FixIn: 9.9.1.1.
311
312 if ( '' === $value ) { return $value; }
313
314 $value = str_replace( ';', ',', $value );
315 $array_of_nums = explode( ',', $value );
316
317 $result = array();
318
319 foreach ( $array_of_nums as $single_date ) {
320
321 $single_date = trim( $single_date );
322
323 // Check for date '2024-02-06
324 $date_ymd = wpbc_sanitize_date( $single_date );
325
326 if ( '' !== $date_ymd ) {
327 $result[] = $date_ymd;
328 } else {
329
330 // Otherwise check for date: '06.02.2024'
331 $date_dmy = wpbc_sanitize_date_dmy( $single_date );
332 if ( '' !== $date_dmy ) {
333 $result[] = $date_dmy;
334 }
335 }
336 }
337
338 $result = implode( ',', $result );
339
340 return $result;
341 }
342
343 /**
344 * Escape string from SQL for the HTML form field
345 *
346 * @param string $value
347 *
348 * @return string
349 *
350 * Used: esc_sql function.
351 *
352 * https://codex.wordpress.org/Function_Reference/esc_sql
353 * Note: Be careful to use this function correctly. It will only escape values to be used in strings in the query.
354 * That is, it only provides escaping for values that will be within quotes in the SQL (as in field = '{$escaped_value}').
355 * If your value is not going to be within quotes, your code will still be vulnerable to SQL injection.
356 * For example, this is vulnerable, because the escaped value is not surrounded by quotes in the SQL query:
357 * ORDER BY {$escaped_value}. As such, this function does not escape unquoted numeric values, field names, or SQL keywords.
358 *
359 */
360 function wpbc_sanitize_text( $value ){
361
362 $value_trimmed = trim( stripslashes( $value ) ); // \' becomes ' and so on
363
364 $esc_sql_value = sanitize_textarea_field( $value_trimmed ); // preserves new lines (\n) and other whitespace
365 //$esc_sql_value = sanitize_text_field( $value_trimmed ); // remove new lines (\n) and other whitespace
366
367 //global $wpdb;
368 //$value = trim( $wpdb->prepare( "'%s'", $esc_sql_value ) , "'" );
369 //$esc_sql_value = trim( stripslashes( $esc_sql_value ) );
370
371 return $esc_sql_value;
372 }
373
374 /**
375 * Escape string from SQL for the HTML form field
376 *
377 * @param string $value
378 *
379 * @return string
380 *
381 * Used: esc_sql function.
382 *
383 * https://codex.wordpress.org/Function_Reference/esc_sql
384 * Note: Be careful to use this function correctly. It will only escape values to be used in strings in the query.
385 * That is, it only provides escaping for values that will be within quotes in the SQL (as in field = '{$escaped_value}').
386 * If your value is not going to be within quotes, your code will still be vulnerable to SQL injection.
387 * For example, this is vulnerable, because the escaped value is not surrounded by quotes in the SQL query:
388 * ORDER BY {$escaped_value}. As such, this function does not escape unquoted numeric values, field names, or SQL keywords.
389 *
390 */
391 function wpbc_sanitize_text_strong( $value ){
392
393 $value_trimmed = trim( stripslashes( $value ) ); // \' becomes ' and so on
394
395 //$esc_sql_value = sanitize_text_field( $value_trimmed ); // remove new lines (\n) and other whitespace
396 $esc_sql_value = sanitize_textarea_field( $value_trimmed ); // preserves new lines (\n) and other whitespace
397
398 // clean any tags
399 $esc_sql_value = preg_replace( '/<[^>]*>/', '', $esc_sql_value );
400 $esc_sql_value = str_replace( '<', ' ', $esc_sql_value );
401 $esc_sql_value = str_replace( '>', ' ', $esc_sql_value );
402 $esc_sql_value = wp_strip_all_tags( $esc_sql_value );
403
404 //FixIn: 9.7.4.1 - escape coded html/xss // Escape any XSS injection
405 // If we have field converted to 'Unicode Hex Character Code', then we make HTML decode firstly (html_entity_decode) and then make sanitizing
406 $esc_sql_value = sanitize_textarea_field( html_entity_decode( $esc_sql_value ) );
407
408 // $esc_sql_value = str_replace('%', '&#37;', $esc_sql_value ); // clean any % from the form, because otherwise, there is problems with SQL prepare function
409 // $esc_sql_value = str_replace('_', '&#95;', $esc_sql_value ); // clean any _
410
411 // $esc_sql_value = str_replace('^', '&#94;', $esc_sql_value ); // clean any ^ caret symbols
412 // $esc_sql_value = str_replace('~', '&#126;', $esc_sql_value ); // clean any ~ equivalency sign - tilde
413
414 return $esc_sql_value;
415 }
416
417
418 // ---------------------------------------------------------------------------------------------------------------------
419 // Other Sanitize functions
420 // ---------------------------------------------------------------------------------------------------------------------
421
422 /**
423 * Sanitize $_GET, $_POST, $_REQUEST text parameters // FixIn: 10.0.0.12.
424 *
425 * @param $value
426 * @param $keep_newlines bool
427 *
428 * @return string
429 */
430 function wpbc_clean_text_value( $value , $keep_newlines = false ){
431
432 if ( $keep_newlines ) {
433 $value_cleaned = sanitize_textarea_field( $value );
434 } else {
435 $value_cleaned = sanitize_text_field( $value );
436 }
437
438 return $value_cleaned;
439 }
440
441
442 // check $value for injection here
443 function wpbc_clean_parameter( $value, $is_escape_sql = true ) {
444
445 $value = preg_replace( '/<[^>]*>/', '', $value ); // clean any tags
446 $value = str_replace( '<', ' ', $value );
447 $value = str_replace( '>', ' ', $value );
448 $value = wp_strip_all_tags( $value );
449
450 //FixIn: 9.7.4.1 - escape coded html/xss // Escape any XSS injection
451 $value = sanitize_textarea_field( $value );
452 $value = sanitize_textarea_field( html_entity_decode( $value ) ); // If we have field converted to 'Unicode Hex Character Code', then we make HTML decode firstly (html_entity_decode) and then make sanitizing
453
454 if ( $is_escape_sql ) {
455 $value = esc_sql( $value ); // Clean SQL injection // FixIn: 9.7.4.2.
456 }
457
458 $value = esc_textarea( $value ); // FixIn: 7.1.1.2.
459
460 return $value;
461 }
462
463
464 /**
465 * Check parameter if it number or comma separated list of numbers
466 *
467 * @param $value
468 * @return array|string
469 *
470 * Example:
471 * wpbc_clean_digit_or_csd( '12,a,45,9' ) => '12,0,45,9'
472 * or
473 * wpbc_clean_digit_or_csd( '10a' ) => '10
474 * or
475 * wpbc_clean_digit_or_csd( array( '12,a,45,9', '10a' ) ) => array ( '12,0,45,9', '10' )
476 */
477 function wpbc_clean_digit_or_csd( $value ) {
478
479 if ( '' === $value ) {
480 return $value;
481 }
482
483 if ( is_array( $value ) ) {
484 foreach ( $value as $key => $check_value ) {
485 $value[ $key ] = wpbc_clean_digit_or_csd( $check_value );
486 }
487 return $value;
488 }
489
490 $value = str_replace( ';', ',', $value );
491
492 $array_of_nums = explode( ',', $value );
493
494 $result = array();
495 foreach ( $array_of_nums as $check_element ) {
496 $result[] = intval( $check_element ); // FixIn: 8.0.2.10.
497 }
498 $result = implode( ',', $result );
499
500 return $result;
501 }
502
503
504 /**
505 * Cehck about Valid date, like 2016-07-20 or digit
506 *
507 * @param string $value
508 * @return string or int
509 */
510 function wpbc_clean_digit_or_date( $value ) { // FixIn: 6.2.1.4.
511
512 if ( $value === '' ) return $value;
513
514 if ( preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $value ) ) {
515
516 return $value; // Date is valid in format: 2016-07-20
517 } else {
518 return intval( $value );
519 }
520
521 }
522
523
524 /**
525 * Check about Valid dat in format '2024-05-08' otherwise return ''
526 *
527 * @param string $value
528 *
529 * @return string date or '' if date was not valie
530 */
531 function wpbc_clean_date( $value ) {
532
533 if (
534 ( ! empty( $value ) )
535 && ( preg_match( "/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $value ) )
536 ){
537 return $value; // Date is valid in format: 2024-05-08
538 }
539
540 return ''; // Date not Valid
541 }
542
543
544 /**
545 * Escape any XSS injection from values in booking form
546 *
547 * @param array $structured_booking_data_arr [...]
548 *
549 * @return array [...]
550 */
551 function wpbc_escape_any_xss_in_arr( $structured_booking_data_arr ) {
552
553 foreach ( $structured_booking_data_arr as $field_name => $field_value ) {
554
555 if ( is_array( $field_value ) ) {
556
557 $structured_booking_data_arr[ $field_name ] = wpbc_escape_any_xss_in_arr( $field_value );
558
559 } else {
560 $is_escape_sql = false; // Do not replace %
561 $field_value_cleaned = wpbc_escape_any_xss_in_string( $field_value, $is_escape_sql );
562 $structured_booking_data_arr[ $field_name ] = $field_value_cleaned;
563 }
564 }
565
566 return $structured_booking_data_arr;
567 }
568
569
570 /**
571 * Escape any XSS injection from string values
572 *
573 * @param string $field_value
574 *
575 * @return string
576 */
577 function wpbc_escape_any_xss_in_string( $field_value, $is_escape_sql = true ) {
578
579 $field_value_cleaned = wpbc_clean_parameter( $field_value, $is_escape_sql );
580 $field_value_cleaned = str_replace( '%', '&#37;', $field_value_cleaned ); // clean % in form, because can be problems with SQL prepare function
581
582 return $field_value_cleaned;
583 }
584
585
586 function wpbc_esc_like( $value_trimmed ) {
587
588 global $wpdb;
589 if ( method_exists( $wpdb ,'esc_like' ) )
590 return $wpdb->esc_like( $value_trimmed ); // Its require minimum WP 4.0.0
591 else
592 return addcslashes( $value_trimmed, '_%\\' ); // Direct implementation from $wpdb->esc_like(
593 }
594
595 /**
596 * Escape single quote from ' to &#039;
597 *
598 * @param string $value - String to escape.
599 *
600 * @return array|string|string[]
601 */
602 function wpbc_esc_single_quote( $value ) {
603
604 $safe_text = wp_check_invalid_utf8( $value );
605 $escaped_value = str_replace( "'", '&#039;', $safe_text );
606
607 return $escaped_value;
608 }
609
610 /**
611 * Sanitize term to Slug format (no spaces, lowercase).
612 * urldecode - reverse munging of UTF8 characters.
613 *
614 * @param mixed $value
615 * @return string
616 */
617 function wpbc_get_slug_format( $value ) {
618 return urldecode( sanitize_title( $value ) );
619 }
620
621
622 /**
623 * Clean user string for using in SQL LIKE statement - append to LIKE sql
624 *
625 * @param string $value - to clean
626 * @return string - escaped
627 * Exmaple:
628 * $search_escaped_like_title = wpbc_clean_like_string_for_append_in_sql_for_db( $input_var );
629 *
630 * $where_sql = " WHERE title LIKE ". $search_escaped_like_title ." ";
631 */
632 function wpbc_clean_like_string_for_append_in_sql_for_db( $value ) {
633 global $wpdb;
634
635 $value_trimmed = trim( stripslashes( $value ) );
636 $wild = '%';
637 $like = $wild . wpbc_esc_like( $value_trimmed ) . $wild;
638 /* phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder */
639 $sql = $wpdb->prepare( "'%s'", $like );
640
641 return $sql;
642
643
644 /* Help:
645 * First half of escaping for LIKE special characters % and _ before preparing for MySQL.
646 * Use this only before wpdb::prepare() or esc_sql(). Reversing the order is very bad for security.
647 *
648 * Example Prepared Statement:
649 *
650 * $wild = '%';
651 * $find = 'only 43% of planets';
652 * $like = $wild . wpbc_esc_like( $find ) . $wild;
653 * $sql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_content LIKE '%s'", $like );
654 *
655 * Example Escape Chain:
656 *
657 * $sql = esc_sql( wpbc_esc_like( $input ) );
658 */
659
660 }
661
662
663 /**
664 * Clean string for using in SQL LIKE requests inside single quotes: WHERE title LIKE '%". $escaped_search_title ."%'
665 * Replaced _ to \_ % to \% \ to \\
666 * @param string $value - to clean
667 * @return string - escaped
668 * Exmaple:
669 * $search_escaped_like_title = wpbc_clean_like_string_for_db( $input_var );
670 *
671 * $where_sql = " WHERE title LIKE '%". $search_escaped_like_title ."%' ";
672 *
673 * Important! Use SINGLE quotes after in SQL query: LIKE '%".$data."%'
674 */
675 function wpbc_clean_like_string_for_db( $value ){
676
677 global $wpdb;
678
679 $value_trimmed = trim( stripslashes( $value ) );
680
681 $value_trimmed = wpbc_esc_like( $value_trimmed );
682
683 /* phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder */
684 $value = trim( $wpdb->prepare( "'%s'", $value_trimmed ), "'" );
685
686 return $value;
687
688 /* Help:
689 * First half of escaping for LIKE special characters % and _ before preparing for MySQL.
690 * Use this only before wpdb::prepare() or esc_sql(). Reversing the order is very bad for security.
691 *
692 * Example Prepared Statement:
693 *
694 * $wild = '%';
695 * $find = 'only 43% of planets';
696 * $like = $wild . wpbc_esc_like( $find ) . $wild;
697 * $sql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_content LIKE '%s'", $like );
698 *
699 * Example Escape Chain:
700 *
701 * $sql = esc_sql( wpbc_esc_like( $input ) );
702 */
703 }
704
705
706 /**
707 * Escape string from SQL for the HTML form field
708 *
709 * @param string $value
710 * @return string
711 *
712 * Used: esc_sql function.
713 *
714 * https://codex.wordpress.org/Function_Reference/esc_sql
715 * Note: Be careful to use this function correctly. It will only escape values to be used in strings in the query.
716 * That is, it only provides escaping for values that will be within quotes in the SQL (as in field = '{$escaped_value}').
717 * If your value is not going to be within quotes, your code will still be vulnerable to SQL injection.
718 * For example, this is vulnerable, because the escaped value is not surrounded by quotes in the SQL query:
719 * ORDER BY {$escaped_value}. As such, this function does not escape unquoted numeric values, field names, or SQL keywords.
720 *
721 */
722 function wpbc_clean_string_for_form( $value ){
723
724 global $wpdb;
725
726 $value_trimmed = trim( stripslashes( $value ) );
727
728 //FixIn: 8.0.2.10 //Fix for update of WP 4.8.3
729 if ( method_exists( $wpdb, 'remove_placeholder_escape' ) )
730 $esc_sql_value = $wpdb->remove_placeholder_escape( esc_sql( $value_trimmed ) );
731 else
732 $esc_sql_value = esc_sql( $value_trimmed );
733
734 //$value = trim( $wpdb->prepare( "'%s'", $esc_sql_value ) , "'" );
735
736 $esc_sql_value = trim( stripslashes( $esc_sql_value ) );
737
738 return $esc_sql_value;
739
740 }
741
742
743 /**
744 * Escape shortcode parameters
745 *
746 * @param array $attr
747 *
748 * @return array
749 */
750 function wpbc_escape_shortcode_params( $attr ) {
751
752 if ( is_array( $attr ) ) {
753
754 $scaped_attr = array();
755
756 foreach ( $attr as $attr_key => $attr_val ) {
757 $attr_key = sanitize_text_field( $attr_key ); // FixIn: 10.11.2.1.
758 $attr_val = sanitize_text_field( $attr_val ); // FixIn: 10.11.2.1.
759
760 $scaped_attr[ $attr_key ] = $attr_val;
761 }
762
763 return $scaped_attr;
764 }
765
766 if ( is_string( $attr ) ) {
767
768 $scaped_attr = sanitize_text_field( $attr ); // FixIn: 10.11.2.1.
769
770 return $scaped_attr;
771 }
772
773 return $attr;
774 }
775