| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @version 1.0 |
| 4 | 4 | * @package Booking Calendar |
| @@ -223,8 +223,39 @@ | ||
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | |
| 226 | 226 | /** |
| 227 | + * Validate a locale received from a request. | |
| 228 | + * | |
| 229 | + * Locale values are used request-wide and can later be rendered inside JavaScript and HTML attributes. Accept only | |
| 230 | + * ASCII locale identifiers, such as "en", "en_US", "de_DE_formal", or "en-US". Invalid values must be rejected | |
| 231 | + * instead of sanitized into a different value. // FixIn: 11.4.3.2. | |
| 232 | + * | |
| 233 | + * @param mixed $locale Locale value from the request. | |
| 234 | + * | |
| 235 | + * @return string|false Valid locale, or false when the value is invalid. | |
| 236 | + */ | |
| 237 | +function wpbc_validate_request_locale( $locale ) { | |
| 238 | + | |
| 239 | + if ( ! is_string( $locale ) ) { | |
| 240 | + return false; | |
| 241 | + } | |
| 242 | + | |
| 243 | + $locale = wp_unslash( $locale ); | |
| 244 | + | |
| 245 | + if ( | |
| 246 | + ( '' === $locale ) | |
| 247 | + || ( strlen( $locale ) > 32 ) | |
| 248 | + || ( 1 !== preg_match( '/\A[A-Za-z0-9]+(?:[_-][A-Za-z0-9]+)*\z/D', $locale ) ) | |
| 249 | + ) { | |
| 250 | + return false; | |
| 251 | + } | |
| 252 | + | |
| 253 | + return $locale; | |
| 254 | +} | |
| 255 | + | |
| 256 | + | |
| 257 | +/** | |
| 227 | 258 | * Get maybe reloaded 'booking' locale ( WPBC_LOCALE_RELOAD ) and if not defined WPBC_LOCALE_RELOAD define it. |
| 228 | 259 | * |
| 229 | 260 | * @return string |
| 230 | 261 | */ |
| @@ -260,15 +291,15 @@ | ||
| 260 | 291 | |
| 261 | 292 | $wpbc_ajx_locale = false; |
| 262 | 293 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing |
| 263 | 294 | if ( isset( $_REQUEST['wpdev_active_locale'] ) ) { |
| 264 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.NonceVerification.Recommended | |
| 265 | - $wpbc_ajx_locale = sanitize_text_field( $_REQUEST['wpdev_active_locale'] ); | |
| 295 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 296 | + $wpbc_ajx_locale = wpbc_validate_request_locale( $_REQUEST['wpdev_active_locale'] ); | |
| 266 | 297 | } |
| 267 | 298 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing |
| 268 | 299 | if ( isset( $_REQUEST['wpbc_ajx_locale'] ) ) { |
| 269 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.NonceVerification.Recommended | |
| 270 | - $wpbc_ajx_locale = sanitize_text_field( $_REQUEST['wpbc_ajx_locale'] ); | |
| 300 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 301 | + $wpbc_ajx_locale = wpbc_validate_request_locale( $_REQUEST['wpbc_ajx_locale'] ); | |
| 271 | 302 | } |
| 272 | 303 | |
| 273 | 304 | // Reload locale ONLY in AJAX, and if `isset $_REQUEST['wpdev_active_locale'] |
| 274 | 305 | if ( |
| @@ -378,11 +409,11 @@ | ||
| 378 | 409 | |
| 379 | 410 | /** |
| 380 | 411 | * Translate content. Check for language sections -- [lang=xx_XX] shortcode. // FixIn: 10.0.0.46. |
| 381 | 412 | * |
| 382 | - * @param $content_orig | |
| 383 | - * | |
| 384 | - * @return string | |
| 413 | + * @param mixed $content_orig Content containing optional language sections. | |
| 414 | + * | |
| 415 | + * @return string Translated content, or an empty string for unsupported values. | |
| 385 | 416 | */ |
| 386 | 417 | function wpbc_lang( $content_orig ) { |
| 387 | 418 | return wpdev_check_for_active_language( $content_orig ); |
| 388 | 419 | } |
| @@ -390,24 +421,32 @@ | ||
| 390 | 421 | |
| 391 | 422 | /** |
| 392 | 423 | * Check plugin text for active language section -- [lang=xx_XX] shortcode |
| 393 | 424 | * |
| 394 | - * @param string $content_orig | |
| 395 | - * @return string | |
| 425 | + * @param mixed $content_orig Content containing optional language sections. | |
| 426 | + * @return string Translated content, or an empty string for unsupported values. | |
| 396 | 427 | * Usage: |
| 397 | 428 | * $text = wpbc_lang( $text ); |
| 398 | 429 | */ |
| 399 | -function wpdev_check_for_active_language( $content_orig ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound | |
| 430 | +function wpdev_check_for_active_language( $content_orig ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound | |
| 431 | + | |
| 432 | + if ( is_string( $content_orig ) ) { | |
| 433 | + $content = $content_orig; | |
| 434 | + } elseif ( is_scalar( $content_orig ) ) { | |
| 435 | + $content = (string) $content_orig; | |
| 436 | + } elseif ( is_object( $content_orig ) && method_exists( $content_orig, '__toString' ) ) { | |
| 437 | + $content = (string) $content_orig; | |
| 438 | + } else { | |
| 439 | + return ''; | |
| 440 | + } | |
| 441 | + | |
| 442 | + $languages = array(); | |
| 443 | + $content_ex = explode( '[lang', $content ); | |
| 444 | + | |
| 445 | + foreach ( $content_ex as $value ) { | |
| 446 | + | |
| 447 | + if ( '=' === substr( $value, 0, 1 ) ) { | |
| 400 | 448 | |
| 401 | - $content = $content_orig; | |
| 402 | - | |
| 403 | - $languages = array(); | |
| 404 | - $content_ex = explode('[lang',$content); | |
| 405 | - | |
| 406 | - foreach ( $content_ex as $value ) { | |
| 407 | - | |
| 408 | - if ( '=' == substr( $value, 0, 1 ) ) { | |
| 409 | - | |
| 410 | 449 | $pos_s = strpos( $value, '=' ); |
| 411 | 450 | $pos_f = strpos( $value, ']' ); |
| 412 | 451 | $key = trim( substr( $value, ( $pos_s + 1 ), ( $pos_f - $pos_s - 1 ) ) ); |
| 413 | 452 | $value_l = trim( substr( $value, $pos_f + 1 ) ); |
| @@ -417,9 +456,9 @@ | ||
| 417 | 456 | $languages['default'] = $value; |
| 418 | 457 | } |
| 419 | 458 | } |
| 420 | 459 | |
| 421 | - $locale = wpbc_get_maybe_reloaded_booking_locale(); // $locale = 'fr_FR'; | |
| 460 | + $locale = wpbc_get_maybe_reloaded_booking_locale(); // $locale = 'fr_FR'. | |
| 422 | 461 | |
| 423 | 462 | if ( isset( $languages[ $locale ] ) ) { |
| 424 | 463 | $return_text = $languages[ $locale ]; |
| 425 | 464 | } else { |