::mb_strtoupper((string) $string, $encoding); } } if (!function_exists('mb_substitute_character')) { function mb_substitute_character(string|int|null $substitute_character = null): string|int|bool { return p\Mbstring::mb_substitute_character($substitute_character); } } if (!function_exists('mb_substr')) { function mb_substr(?string $string, ?int $start, ?int $length = null, ?string $encoding = null): string { return p\Mbstring::mb_substr((string) $string, (int) $start, $length, $encoding); } } if (!function_exists('mb_stripos')) { function mb_stripos(?string $haystack, ?string $needle, ?int $offset = 0, ?string $encoding = null): int|false { return p\Mbstring::mb_stripos((string) $haystack, (string) $needle, (int) $offset, $encoding); } } if (!function_exists('mb_stristr')) { function mb_stristr(?string $haystack, ?string $needle, ?bool $before_needle = false, ?string $encoding = null): string|false { return p\Mbstring::mb_stristr((string) $haystack, (string) $needle, (bool) $before_needle, $encoding); } } if (!function_exists('mb_strrchr')) { function mb_strrchr(?string $haystack, ?string $needle, ?bool $before_needle = false, ?string $encoding = null): string|false { return p\Mbstring::mb_strrchr((string) $haystack, (string) $needle, (bool) $before_needle, $encoding); } } if (!function_exists('mb_strrichr')) { function mb_strrichr(?string $haystack, ?string $needle, ?bool $before_needle = false, ?string $encoding = null): string|false { return p\Mbstring::mb_strrichr((string) $haystack, (string) $needle, (bool) $before_needle, $encoding); } } if (!function_exists('mb_strripos')) { function mb_strripos(?string $haystack, ?string $needle, ?int $offset = 0, ?string $encoding = null): int|false { return p\Mbstring::mb_strripos((string) $haystack, (string) $needle, (int) $offset, $encoding); } } if (!function_exists('mb_strrpos')) { function mb_strrpos(?string $haystack, ?string $needle, ?int $offset = 0, ?string $encoding = null): int|false { return p\Mbstring::mb_strrpos((string) $haystack, (string) $needle, (int) $offset, $encoding); } } if (!function_exists('mb_strstr')) { function mb_strstr(?string $haystack, ?string $needle, ?bool $before_needle = false, ?string $encoding = null): string|false { return p\Mbstring::mb_strstr((string) $haystack, (string) $needle, (bool) $before_needle, $encoding); } } if (!function_exists('mb_get_info')) { function mb_get_info(?string $type = 'all'): array|string|int|false { return p\Mbstring::mb_get_info((string) $type); } } if (!function_exists('mb_http_output')) { function mb_http_output(?string $encoding = null): string|bool { return p\Mbstring::mb_http_output($encoding); } } if (!function_exists('mb_strwidth')) { function mb_strwidth(?string $string, ?string $encoding = null): int { return p\Mbstring::mb_strwidth((string) $string, $encoding); } } if (!function_exists('mb_substr_count')) { function mb_substr_count(?string $haystack, ?string $needle, ?string $encoding = null): int { return p\Mbstring::mb_substr_count((string) $haystack, (string) $needle, $encoding); } } if (!function_exists('mb_output_handler')) { function mb_output_handler(?string $string, ?int $status): string { return p\Mbstring::mb_output_handler((string) $string, (int) $status); } } if (!function_exists('mb_http_input')) { function mb_http_input(?string $type = null): array|string|false { return p\Mbstring::mb_http_input($type); } } if (!function_exists('mb_convert_variables')) { function mb_convert_variables(?string $to_encoding, array|string|null $from_encoding, mixed &$var, mixed &...$vars): string|false { return p\Mbstring::mb_convert_variables((string) $to_encoding, $from_encoding ?? '', $var, ...$vars); } } if (!function_exists('mb_ord')) { function mb_ord(?string $string, ?string $encoding = null): int|false { return p\Mbstring::mb_ord((string) $string, $encoding); } } if (!function_exists('mb_chr')) { function mb_chr(?int $codepoint, ?string $encoding = null): string|false { return p\Mbstring::mb_chr((int) $codepoint, $encoding); } } if (!function_exists('mb_scrub')) { function mb_scrub(?string $string, ?string $encoding = null): string { $encoding ??= mb_internal_encoding(); return mb_convert_encoding((string) $string, $encoding, $encoding); } } if (!function_exists('mb_str_split')) { function mb_str_split(?string $string, ?int $length = 1, ?string $encoding = null): array { return p\Mbstring::mb_str_split((string) $string, (int) $length, $encoding); } } if (extension_loaded('mbstring')) { return; } if (!defined('MB_CASE_UPPER')) { define('MB_CASE_UPPER', 0); } if (!defined('MB_CASE_LOWER')) { define('MB_CASE_LOWER', 1); } if (!defined('MB_CASE_TITLE')) { define('MB_CASE_TITLE', 2); } s_message; } public static function get_show_default_success_message() { $self = self::get_instance(); return $self->show_default_success_message; } public static function set_show_default_error_message( $show_default_error_message ) { $self = self::get_instance(); $old_show_default_error_message = $self->show_default_error_message; $self->show_default_error_message = $show_default_error_message; return $old_show_default_error_message; } public static function get_show_default_error_message() { $self = self::get_instance(); return $self->show_default_error_message; } public static function prevent_modal_close() { $self = self::get_instance(); $self->close_modal = false; } public static function reload_module( $module ) { $self = self::get_instance(); $self->add_js_function_call( 'reloadModule', $module ); } public static function reload_all_modules() { self::get_instance()->add_js_function_call( 'reloadAllModules' ); } public static function refresh_page() { self::get_instance()->add_js_function_call( 'refreshPage' ); } public static function regenerate_wp_config() { $self = self::get_instance(); $self->regenerate_wp_config = true; self::reload_module( 'wp-config-rules' ); } public static function regenerate_server_config() { $self = self::get_instance(); $self->regenerate_server_config = true; self::reload_module( 'server-config-rules' ); } public static function force_logout() { $self = self::get_instance(); if ( $self->force_logout ) { return; } $self->force_logout = true; self::redirect( add_query_arg( 'loggedout', 'true', ITSEC_Lib::get_login_url() ) ); } public static function redirect( $redirect ) { $self = self::get_instance(); $self->redirect = $redirect; } public static function maybe_regenerate_wp_config() { $self = self::get_instance(); if ( $self->regenerate_wp_config ) { ITSEC_Files::regenerate_wp_config(); $self->regenerate_wp_config = false; } } public static function maybe_regenerate_server_config() { $self = self::get_instance(); if ( $self->regenerate_server_config ) { ITSEC_Files::regenerate_server_config(); $self->regenerate_server_config = false; } } public static function maybe_do_force_logout() { $self = self::get_instance(); if ( $self->force_logout ) { @wp_clear_auth_cookie(); $self->force_logout = false; } } public static function maybe_do_redirect() { $self = self::get_instance(); if ( $self->redirect ) { wp_safe_redirect( $self->redirect ); exit(); } } public static function maybe_flag_new_notifications_available() { $nc = ITSEC_Core::get_notification_center(); $current = array_keys( $nc->get_notifications() ); $nc->clear_notifications_cache(); $new = array_keys( $nc->get_notifications() ); $added = array_diff( $new, $current ); if ( $added ) { self::flag_new_notifications_available(); } } public static function flag_new_notifications_available() { static $run_count = 0; if ( $run_count ++ > 0 ) { return; } self::reload_module( 'notification-center' ); self::get_instance()->has_new_notifications = true; self::get_instance()->add_info( sprintf( esc_html__( 'New notifications available in the %1$sNotification Center%2$s.', 'better-wp-security' ), '', '' ) ); } public static function get_raw_data() { $self = self::get_instance(); self::maybe_regenerate_wp_config(); self::maybe_regenerate_server_config(); self::maybe_do_force_logout(); if ( is_wp_error( $self->response ) ) { $self->add_error( $self->response ); $self->set_response( null ); } $data = array( 'source' => 'ITSEC_Response', 'success' => $self->success, 'response' => $self->response, 'errors' => self::get_error_strings( $self->errors ), 'warnings' => self::get_error_strings( $self->warnings ), 'messages' => $self->messages, 'infos' => $self->infos, 'functionCalls' => self::parse_js_function_calls_for_module_reloads(), 'storeDispatches' => $self->store_dispatches, 'redirect' => $self->redirect, 'closeModal' => $self->close_modal, 'newNotifications' => $self->has_new_notifications, ); return $data; } public static function send_json() { $data = self::get_raw_data(); wp_send_json( $data ); } public static function get_instance() { if ( ! self::$instance ) { self::$instance = new self; } return self::$instance; } public function reset_to_defaults() { $this->response = null; $this->errors = array(); $this->warnings = array(); $this->messages = array(); $this->infos = array(); $this->success = true; $this->js_function_calls = array(); $this->store_dispatches = array(); $this->show_default_success_message = true; $this->show_default_error_message = true; $this->force_logout = false; $this->redirect = false; $this->close_modal = true; $this->regenerate_wp_config = false; $this->regenerate_server_config = false; } public static function get_error_strings( $error ) { if ( is_string( $error ) ) { return array( $error ); } elseif ( is_a( $error, 'WP_Error' ) ) { /* translators: 1: error message, 2: error code */ $format = __( '%1$s (%2$s)', 'better-wp-security' ); $errors = array(); foreach ( $error->get_error_codes() as $code ) { $message = implode( ' ', (array) $error->get_error_messages( $code ) ); $errors[] = sprintf( $format, $message, $code ) . ' '; } return $errors; } elseif ( is_array( $error ) ) { $errors = array(); foreach ( $error as $error_item ) { $new_errors = self::get_error_strings( $error_item ); $errors = array_merge( $errors, $new_errors ); } return $errors; } /* translators: 1: variable type */ return array( sprintf( __( 'Unknown error type received: %1$s.', 'better-wp-security' ), gettype( $error ) ) ); } public static function as_wp_error(): WP_Error { return self::get_wp_error( self::get_errors() ); } private static function get_wp_error( $errors ): WP_Error { if ( is_wp_error( $errors ) ) { return $errors; } $wp_error = new \WP_Error(); foreach ( $errors as $error ) { if ( is_string( $error ) ) { $wp_error->add( 'unknown-error', $error ); } elseif ( is_wp_error( $error ) ) { $wp_error->merge_from( $error ); } elseif ( is_array( $error ) ) { $wp_error->merge_from( self::get_wp_error( $errors ) ); } } return $wp_error; } private static function parse_js_function_calls_for_module_reloads() { $has_reload_all = false; $function_calls = self::get_instance()->js_function_calls; foreach ( $function_calls as $function_call ) { if ( $function_call[0] === 'reloadAllModules' ) { $has_reload_all = true; break; } } if ( ! $has_reload_all ) { return $function_calls; } foreach ( $function_calls as $i => $function_call ) { if ( $function_call[0] === 'reloadModule' ) { unset( $function_calls[ $i ] ); } } return array_values( $function_calls ); } public function shutdown() { self::maybe_regenerate_wp_config(); self::maybe_regenerate_server_config(); self::maybe_do_force_logout(); } }