PluginProbe
Booking Calendar / 10.11
Booking Calendar v10.11
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 / _functions / sanitizing.php

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

772 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 ) { // FixIn: 6.2.1.4.
478
479 if ( $value === '' ) return $value;
480
481
482 if ( is_array( $value ) ) {
483 foreach ( $value as $key => $check_value ) {
484 $value[ $key ] = wpbc_clean_digit_or_csd( $check_value );
485 }
486 return $value;
487 }
488
489 $value = str_replace( ';', ',', $value );
490
491 $array_of_nums = explode(',', $value);
492
493 $result = array();
494 foreach ($array_of_nums as $check_element) {
495
496 $result[] = intval( $check_element ); // FixIn: 8.0.2.10.
497 }
498 $result = implode(',', $result );
499 return $result;
500 }
501
502
503 /**
504 * Cehck about Valid date, like 2016-07-20 or digit
505 *
506 * @param string $value
507 * @return string or int
508 */
509 function wpbc_clean_digit_or_date( $value ) { // FixIn: 6.2.1.4.
510
511 if ( $value === '' ) return $value;
512
513 if ( preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $value ) ) {
514
515 return $value; // Date is valid in format: 2016-07-20
516 } else {
517 return intval( $value );
518 }
519
520 }
521
522
523 /**
524 * Check about Valid dat in format '2024-05-08' otherwise return ''
525 *
526 * @param string $value
527 *
528 * @return string date or '' if date was not valie
529 */
530 function wpbc_clean_date( $value ) {
531
532 if (
533 ( ! empty( $value ) )
534 && ( preg_match( "/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $value ) )
535 ){
536 return $value; // Date is valid in format: 2024-05-08
537 }
538
539 return ''; // Date not Valid
540 }
541
542
543 /**
544 * Escape any XSS injection from values in booking form
545 *
546 * @param array $structured_booking_data_arr [...]
547 *
548 * @return array [...]
549 */
550 function wpbc_escape_any_xss_in_arr( $structured_booking_data_arr ) {
551
552 foreach ( $structured_booking_data_arr as $field_name => $field_value ) {
553
554 if ( is_array( $field_value ) ) {
555
556 $structured_booking_data_arr[ $field_name ] = wpbc_escape_any_xss_in_arr( $field_value );
557
558 } else {
559 $is_escape_sql = false; // Do not replace %
560 $field_value_cleaned = wpbc_escape_any_xss_in_string( $field_value, $is_escape_sql );
561 $structured_booking_data_arr[ $field_name ] = $field_value_cleaned;
562 }
563 }
564
565 return $structured_booking_data_arr;
566 }
567
568
569 /**
570 * Escape any XSS injection from string values
571 *
572 * @param string $field_value
573 *
574 * @return string
575 */
576 function wpbc_escape_any_xss_in_string( $field_value, $is_escape_sql = true ) {
577
578 $field_value_cleaned = wpbc_clean_parameter( $field_value, $is_escape_sql );
579 $field_value_cleaned = str_replace( '%', '&#37;', $field_value_cleaned ); // clean % in form, because can be problems with SQL prepare function
580
581 return $field_value_cleaned;
582 }
583
584
585 function wpbc_esc_like( $value_trimmed ) {
586
587 global $wpdb;
588 if ( method_exists( $wpdb ,'esc_like' ) )
589 return $wpdb->esc_like( $value_trimmed ); // Its require minimum WP 4.0.0
590 else
591 return addcslashes( $value_trimmed, '_%\\' ); // Direct implementation from $wpdb->esc_like(
592 }
593
594 /**
595 * Escape single quote from ' to &#039;
596 *
597 * @param string $value - String to escape.
598 *
599 * @return array|string|string[]
600 */
601 function wpbc_esc_single_quote( $value ) {
602
603 $safe_text = wp_check_invalid_utf8( $value );
604 $escaped_value = str_replace( "'", '&#039;', $safe_text );
605
606 return $escaped_value;
607 }
608
609 /**
610 * Sanitize term to Slug format (no spaces, lowercase).
611 * urldecode - reverse munging of UTF8 characters.
612 *
613 * @param mixed $value
614 * @return string
615 */
616 function wpbc_get_slug_format( $value ) {
617 return urldecode( sanitize_title( $value ) );
618 }
619
620
621 /**
622 * Clean user string for using in SQL LIKE statement - append to LIKE sql
623 *
624 * @param string $value - to clean
625 * @return string - escaped
626 * Exmaple:
627 * $search_escaped_like_title = wpbc_clean_like_string_for_append_in_sql_for_db( $input_var );
628 *
629 * $where_sql = " WHERE title LIKE ". $search_escaped_like_title ." ";
630 */
631 function wpbc_clean_like_string_for_append_in_sql_for_db( $value ) {
632 global $wpdb;
633
634 $value_trimmed = trim( stripslashes( $value ) );
635 $wild = '%';
636 $like = $wild . wpbc_esc_like( $value_trimmed ) . $wild;
637 /* phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder */
638 $sql = $wpdb->prepare( "'%s'", $like );
639
640 return $sql;
641
642
643 /* Help:
644 * First half of escaping for LIKE special characters % and _ before preparing for MySQL.
645 * Use this only before wpdb::prepare() or esc_sql(). Reversing the order is very bad for security.
646 *
647 * Example Prepared Statement:
648 *
649 * $wild = '%';
650 * $find = 'only 43% of planets';
651 * $like = $wild . wpbc_esc_like( $find ) . $wild;
652 * $sql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_content LIKE '%s'", $like );
653 *
654 * Example Escape Chain:
655 *
656 * $sql = esc_sql( wpbc_esc_like( $input ) );
657 */
658
659 }
660
661
662 /**
663 * Clean string for using in SQL LIKE requests inside single quotes: WHERE title LIKE '%". $escaped_search_title ."%'
664 * Replaced _ to \_ % to \% \ to \\
665 * @param string $value - to clean
666 * @return string - escaped
667 * Exmaple:
668 * $search_escaped_like_title = wpbc_clean_like_string_for_db( $input_var );
669 *
670 * $where_sql = " WHERE title LIKE '%". $search_escaped_like_title ."%' ";
671 *
672 * Important! Use SINGLE quotes after in SQL query: LIKE '%".$data."%'
673 */
674 function wpbc_clean_like_string_for_db( $value ){
675
676 global $wpdb;
677
678 $value_trimmed = trim( stripslashes( $value ) );
679
680 $value_trimmed = wpbc_esc_like( $value_trimmed );
681
682 /* phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder */
683 $value = trim( $wpdb->prepare( "'%s'", $value_trimmed ), "'" );
684
685 return $value;
686
687 /* Help:
688 * First half of escaping for LIKE special characters % and _ before preparing for MySQL.
689 * Use this only before wpdb::prepare() or esc_sql(). Reversing the order is very bad for security.
690 *
691 * Example Prepared Statement:
692 *
693 * $wild = '%';
694 * $find = 'only 43% of planets';
695 * $like = $wild . wpbc_esc_like( $find ) . $wild;
696 * $sql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_content LIKE '%s'", $like );
697 *
698 * Example Escape Chain:
699 *
700 * $sql = esc_sql( wpbc_esc_like( $input ) );
701 */
702 }
703
704
705 /**
706 * Escape string from SQL for the HTML form field
707 *
708 * @param string $value
709 * @return string
710 *
711 * Used: esc_sql function.
712 *
713 * https://codex.wordpress.org/Function_Reference/esc_sql
714 * Note: Be careful to use this function correctly. It will only escape values to be used in strings in the query.
715 * That is, it only provides escaping for values that will be within quotes in the SQL (as in field = '{$escaped_value}').
716 * If your value is not going to be within quotes, your code will still be vulnerable to SQL injection.
717 * For example, this is vulnerable, because the escaped value is not surrounded by quotes in the SQL query:
718 * ORDER BY {$escaped_value}. As such, this function does not escape unquoted numeric values, field names, or SQL keywords.
719 *
720 */
721 function wpbc_clean_string_for_form( $value ){
722
723 global $wpdb;
724
725 $value_trimmed = trim( stripslashes( $value ) );
726
727 //FixIn: 8.0.2.10 //Fix for update of WP 4.8.3
728 if ( method_exists( $wpdb, 'remove_placeholder_escape' ) )
729 $esc_sql_value = $wpdb->remove_placeholder_escape( esc_sql( $value_trimmed ) );
730 else
731 $esc_sql_value = esc_sql( $value_trimmed );
732
733 //$value = trim( $wpdb->prepare( "'%s'", $esc_sql_value ) , "'" );
734
735 $esc_sql_value = trim( stripslashes( $esc_sql_value ) );
736
737 return $esc_sql_value;
738
739 }
740
741
742 /**
743 * Escape shortcode parameters
744 *
745 * @param array $attr
746 *
747 * @return array
748 */
749 function wpbc_escape_shortcode_params( $attr ) { //FixIn: 9.7.3.6.1
750
751 if ( is_array( $attr ) ) {
752
753 $scaped_attr = array();
754
755 foreach ( $attr as $attr_key => $attr_val ) {
756 $attr_key = esc_attr( $attr_key );
757 $attr_val = esc_attr( $attr_val );
758 $scaped_attr[ $attr_key ] = $attr_val;
759 }
760 return $scaped_attr;
761 }
762
763 if ( is_string( $attr ) ) { //FixIn: 9.7.3.6.2
764
765 $scaped_attr = esc_attr( $attr );
766
767 return $scaped_attr;
768 }
769
770 return $attr;
771 }
772