Files
WP-Security-Pack/includes/class-wpsp-helper.php
T
2026-01-25 23:14:49 +01:00

556 lines
18 KiB
PHP

<?php
/**
* Helper functions for WP Security Pack.
*
* @package WP_Security_Pack
*/
// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Helper class with utility functions.
*/
class WPSP_Helper {
/**
* Get the real client IP address.
*
* Handles proxies and CDNs like Cloudflare.
*
* @return string|null
*/
public static function get_client_ip() {
$ip = null;
// Priority order for IP detection.
$headers = array(
'HTTP_CF_CONNECTING_IP', // Cloudflare.
'HTTP_X_REAL_IP', // Nginx reverse proxy.
'HTTP_X_FORWARDED_FOR', // Generic proxy.
'REMOTE_ADDR', // Direct connection.
);
foreach ( $headers as $header ) {
if ( ! empty( $_SERVER[ $header ] ) ) {
// X-Forwarded-For can contain multiple IPs, get the first one.
$ip = sanitize_text_field( wp_unslash( $_SERVER[ $header ] ) );
if ( 'HTTP_X_FORWARDED_FOR' === $header && strpos( $ip, ',' ) !== false ) {
$ips = explode( ',', $ip );
$ip = trim( $ips[0] );
}
break;
}
}
// Validate IP.
if ( $ip && filter_var( $ip, FILTER_VALIDATE_IP ) ) {
return $ip;
}
return null;
}
/**
* Check if an IP is in a CIDR range.
*
* @param string $ip IP address to check.
* @param string $cidr CIDR notation (e.g., 192.168.1.0/24).
* @return bool
*/
public static function ip_in_cidr( $ip, $cidr ) {
// Handle exact IP match (no CIDR notation).
if ( strpos( $cidr, '/' ) === false ) {
return $ip === $cidr;
}
list( $network, $mask ) = explode( '/', $cidr );
// Detect IP version.
$ip_is_v6 = filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 );
$network_is_v6 = filter_var( $network, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 );
// Both must be same version.
if ( $ip_is_v6 !== $network_is_v6 ) {
return false;
}
if ( $ip_is_v6 ) {
return self::ipv6_in_cidr( $ip, $network, (int) $mask );
}
return self::ipv4_in_cidr( $ip, $network, (int) $mask );
}
/**
* Check if IPv4 is in CIDR range.
*
* @param string $ip IPv4 address.
* @param string $network Network address.
* @param int $mask CIDR mask.
* @return bool
*/
private static function ipv4_in_cidr( $ip, $network, $mask ) {
$ip_long = ip2long( $ip );
$network_long = ip2long( $network );
if ( false === $ip_long || false === $network_long ) {
return false;
}
// Calculate network mask.
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
$network_mask = ~( ( 1 << ( 32 - $mask ) ) - 1 );
return ( $ip_long & $network_mask ) === ( $network_long & $network_mask );
}
/**
* Check if IPv6 is in CIDR range.
*
* @param string $ip IPv6 address.
* @param string $network Network address.
* @param int $mask CIDR mask.
* @return bool
*/
private static function ipv6_in_cidr( $ip, $network, $mask ) {
$ip_bin = inet_pton( $ip );
$network_bin = inet_pton( $network );
if ( false === $ip_bin || false === $network_bin ) {
return false;
}
// Compare full bytes.
$full_bytes = (int) floor( $mask / 8 );
for ( $i = 0; $i < $full_bytes; $i++ ) {
if ( $ip_bin[ $i ] !== $network_bin[ $i ] ) {
return false;
}
}
// Compare remaining bits.
$remaining_bits = $mask % 8;
if ( $remaining_bits > 0 && $full_bytes < 16 ) {
$bit_mask = ( ( 1 << $remaining_bits ) - 1 ) << ( 8 - $remaining_bits );
if ( ( ord( $ip_bin[ $full_bytes ] ) & $bit_mask ) !== ( ord( $network_bin[ $full_bytes ] ) & $bit_mask ) ) {
return false;
}
}
return true;
}
/**
* Check if an IP matches any rule in a list.
*
* @param string $ip IP address to check.
* @param array $rules Array of IP addresses or CIDR ranges.
* @return bool
*/
public static function ip_matches_rules( $ip, $rules ) {
if ( empty( $rules ) || ! is_array( $rules ) ) {
return false;
}
foreach ( $rules as $rule ) {
$rule = trim( $rule );
if ( empty( $rule ) || strpos( $rule, '#' ) === 0 ) {
continue; // Skip empty lines and comments.
}
if ( self::ip_in_cidr( $ip, $rule ) ) {
return true;
}
}
return false;
}
/**
* Parse IP list from textarea.
*
* @param string $text Textarea content with IPs/CIDRs.
* @return array
*/
public static function parse_ip_list( $text ) {
if ( empty( $text ) ) {
return array();
}
$lines = explode( "\n", $text );
$ips = array();
foreach ( $lines as $line ) {
$line = trim( $line );
// Skip empty lines and comments.
if ( empty( $line ) || strpos( $line, '#' ) === 0 ) {
continue;
}
// Remove inline comments.
if ( strpos( $line, '#' ) !== false ) {
$line = trim( substr( $line, 0, strpos( $line, '#' ) ) );
}
// Validate IP or CIDR.
if ( self::is_valid_ip_or_cidr( $line ) ) {
$ips[] = $line;
}
}
return $ips;
}
/**
* Check if a string is a valid IP or CIDR.
*
* @param string $value Value to check.
* @return bool
*/
public static function is_valid_ip_or_cidr( $value ) {
// Plain IP.
if ( filter_var( $value, FILTER_VALIDATE_IP ) ) {
return true;
}
// CIDR notation.
if ( strpos( $value, '/' ) !== false ) {
list( $ip, $mask ) = explode( '/', $value );
if ( ! filter_var( $ip, FILTER_VALIDATE_IP ) ) {
return false;
}
$mask = (int) $mask;
$is_ipv6 = filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 );
$max_mask = $is_ipv6 ? 128 : 32;
return $mask >= 0 && $mask <= $max_mask;
}
return false;
}
/**
* Get country list for geo-blocking UI.
*
* @return array
*/
public static function get_countries() {
return array(
'AF' => __( 'Afghanistan', 'wp-security-pack' ),
'AL' => __( 'Albania', 'wp-security-pack' ),
'DZ' => __( 'Algeria', 'wp-security-pack' ),
'AS' => __( 'American Samoa', 'wp-security-pack' ),
'AD' => __( 'Andorra', 'wp-security-pack' ),
'AO' => __( 'Angola', 'wp-security-pack' ),
'AI' => __( 'Anguilla', 'wp-security-pack' ),
'AQ' => __( 'Antarctica', 'wp-security-pack' ),
'AG' => __( 'Antigua and Barbuda', 'wp-security-pack' ),
'AR' => __( 'Argentina', 'wp-security-pack' ),
'AM' => __( 'Armenia', 'wp-security-pack' ),
'AW' => __( 'Aruba', 'wp-security-pack' ),
'AU' => __( 'Australia', 'wp-security-pack' ),
'AT' => __( 'Austria', 'wp-security-pack' ),
'AZ' => __( 'Azerbaijan', 'wp-security-pack' ),
'BS' => __( 'Bahamas', 'wp-security-pack' ),
'BH' => __( 'Bahrain', 'wp-security-pack' ),
'BD' => __( 'Bangladesh', 'wp-security-pack' ),
'BB' => __( 'Barbados', 'wp-security-pack' ),
'BY' => __( 'Belarus', 'wp-security-pack' ),
'BE' => __( 'Belgium', 'wp-security-pack' ),
'BZ' => __( 'Belize', 'wp-security-pack' ),
'BJ' => __( 'Benin', 'wp-security-pack' ),
'BM' => __( 'Bermuda', 'wp-security-pack' ),
'BT' => __( 'Bhutan', 'wp-security-pack' ),
'BO' => __( 'Bolivia', 'wp-security-pack' ),
'BA' => __( 'Bosnia and Herzegovina', 'wp-security-pack' ),
'BW' => __( 'Botswana', 'wp-security-pack' ),
'BR' => __( 'Brazil', 'wp-security-pack' ),
'BN' => __( 'Brunei', 'wp-security-pack' ),
'BG' => __( 'Bulgaria', 'wp-security-pack' ),
'BF' => __( 'Burkina Faso', 'wp-security-pack' ),
'BI' => __( 'Burundi', 'wp-security-pack' ),
'KH' => __( 'Cambodia', 'wp-security-pack' ),
'CM' => __( 'Cameroon', 'wp-security-pack' ),
'CA' => __( 'Canada', 'wp-security-pack' ),
'CV' => __( 'Cape Verde', 'wp-security-pack' ),
'KY' => __( 'Cayman Islands', 'wp-security-pack' ),
'CF' => __( 'Central African Republic', 'wp-security-pack' ),
'TD' => __( 'Chad', 'wp-security-pack' ),
'CL' => __( 'Chile', 'wp-security-pack' ),
'CN' => __( 'China', 'wp-security-pack' ),
'CO' => __( 'Colombia', 'wp-security-pack' ),
'KM' => __( 'Comoros', 'wp-security-pack' ),
'CG' => __( 'Congo', 'wp-security-pack' ),
'CD' => __( 'Congo (DRC)', 'wp-security-pack' ),
'CR' => __( 'Costa Rica', 'wp-security-pack' ),
'CI' => __( 'Ivory Coast', 'wp-security-pack' ),
'HR' => __( 'Croatia', 'wp-security-pack' ),
'CU' => __( 'Cuba', 'wp-security-pack' ),
'CY' => __( 'Cyprus', 'wp-security-pack' ),
'CZ' => __( 'Czech Republic', 'wp-security-pack' ),
'DK' => __( 'Denmark', 'wp-security-pack' ),
'DJ' => __( 'Djibouti', 'wp-security-pack' ),
'DM' => __( 'Dominica', 'wp-security-pack' ),
'DO' => __( 'Dominican Republic', 'wp-security-pack' ),
'EC' => __( 'Ecuador', 'wp-security-pack' ),
'EG' => __( 'Egypt', 'wp-security-pack' ),
'SV' => __( 'El Salvador', 'wp-security-pack' ),
'GQ' => __( 'Equatorial Guinea', 'wp-security-pack' ),
'ER' => __( 'Eritrea', 'wp-security-pack' ),
'EE' => __( 'Estonia', 'wp-security-pack' ),
'ET' => __( 'Ethiopia', 'wp-security-pack' ),
'FJ' => __( 'Fiji', 'wp-security-pack' ),
'FI' => __( 'Finland', 'wp-security-pack' ),
'FR' => __( 'France', 'wp-security-pack' ),
'GA' => __( 'Gabon', 'wp-security-pack' ),
'GM' => __( 'Gambia', 'wp-security-pack' ),
'GE' => __( 'Georgia', 'wp-security-pack' ),
'DE' => __( 'Germany', 'wp-security-pack' ),
'GH' => __( 'Ghana', 'wp-security-pack' ),
'GR' => __( 'Greece', 'wp-security-pack' ),
'GL' => __( 'Greenland', 'wp-security-pack' ),
'GD' => __( 'Grenada', 'wp-security-pack' ),
'GU' => __( 'Guam', 'wp-security-pack' ),
'GT' => __( 'Guatemala', 'wp-security-pack' ),
'GN' => __( 'Guinea', 'wp-security-pack' ),
'GW' => __( 'Guinea-Bissau', 'wp-security-pack' ),
'GY' => __( 'Guyana', 'wp-security-pack' ),
'HT' => __( 'Haiti', 'wp-security-pack' ),
'HN' => __( 'Honduras', 'wp-security-pack' ),
'HK' => __( 'Hong Kong', 'wp-security-pack' ),
'HU' => __( 'Hungary', 'wp-security-pack' ),
'IS' => __( 'Iceland', 'wp-security-pack' ),
'IN' => __( 'India', 'wp-security-pack' ),
'ID' => __( 'Indonesia', 'wp-security-pack' ),
'IR' => __( 'Iran', 'wp-security-pack' ),
'IQ' => __( 'Iraq', 'wp-security-pack' ),
'IE' => __( 'Ireland', 'wp-security-pack' ),
'IL' => __( 'Israel', 'wp-security-pack' ),
'IT' => __( 'Italy', 'wp-security-pack' ),
'JM' => __( 'Jamaica', 'wp-security-pack' ),
'JP' => __( 'Japan', 'wp-security-pack' ),
'JO' => __( 'Jordan', 'wp-security-pack' ),
'KZ' => __( 'Kazakhstan', 'wp-security-pack' ),
'KE' => __( 'Kenya', 'wp-security-pack' ),
'KI' => __( 'Kiribati', 'wp-security-pack' ),
'KP' => __( 'North Korea', 'wp-security-pack' ),
'KR' => __( 'South Korea', 'wp-security-pack' ),
'KW' => __( 'Kuwait', 'wp-security-pack' ),
'KG' => __( 'Kyrgyzstan', 'wp-security-pack' ),
'LA' => __( 'Laos', 'wp-security-pack' ),
'LV' => __( 'Latvia', 'wp-security-pack' ),
'LB' => __( 'Lebanon', 'wp-security-pack' ),
'LS' => __( 'Lesotho', 'wp-security-pack' ),
'LR' => __( 'Liberia', 'wp-security-pack' ),
'LY' => __( 'Libya', 'wp-security-pack' ),
'LI' => __( 'Liechtenstein', 'wp-security-pack' ),
'LT' => __( 'Lithuania', 'wp-security-pack' ),
'LU' => __( 'Luxembourg', 'wp-security-pack' ),
'MO' => __( 'Macao', 'wp-security-pack' ),
'MK' => __( 'North Macedonia', 'wp-security-pack' ),
'MG' => __( 'Madagascar', 'wp-security-pack' ),
'MW' => __( 'Malawi', 'wp-security-pack' ),
'MY' => __( 'Malaysia', 'wp-security-pack' ),
'MV' => __( 'Maldives', 'wp-security-pack' ),
'ML' => __( 'Mali', 'wp-security-pack' ),
'MT' => __( 'Malta', 'wp-security-pack' ),
'MH' => __( 'Marshall Islands', 'wp-security-pack' ),
'MR' => __( 'Mauritania', 'wp-security-pack' ),
'MU' => __( 'Mauritius', 'wp-security-pack' ),
'MX' => __( 'Mexico', 'wp-security-pack' ),
'FM' => __( 'Micronesia', 'wp-security-pack' ),
'MD' => __( 'Moldova', 'wp-security-pack' ),
'MC' => __( 'Monaco', 'wp-security-pack' ),
'MN' => __( 'Mongolia', 'wp-security-pack' ),
'ME' => __( 'Montenegro', 'wp-security-pack' ),
'MA' => __( 'Morocco', 'wp-security-pack' ),
'MZ' => __( 'Mozambique', 'wp-security-pack' ),
'MM' => __( 'Myanmar', 'wp-security-pack' ),
'NA' => __( 'Namibia', 'wp-security-pack' ),
'NR' => __( 'Nauru', 'wp-security-pack' ),
'NP' => __( 'Nepal', 'wp-security-pack' ),
'NL' => __( 'Netherlands', 'wp-security-pack' ),
'NZ' => __( 'New Zealand', 'wp-security-pack' ),
'NI' => __( 'Nicaragua', 'wp-security-pack' ),
'NE' => __( 'Niger', 'wp-security-pack' ),
'NG' => __( 'Nigeria', 'wp-security-pack' ),
'NO' => __( 'Norway', 'wp-security-pack' ),
'OM' => __( 'Oman', 'wp-security-pack' ),
'PK' => __( 'Pakistan', 'wp-security-pack' ),
'PW' => __( 'Palau', 'wp-security-pack' ),
'PS' => __( 'Palestine', 'wp-security-pack' ),
'PA' => __( 'Panama', 'wp-security-pack' ),
'PG' => __( 'Papua New Guinea', 'wp-security-pack' ),
'PY' => __( 'Paraguay', 'wp-security-pack' ),
'PE' => __( 'Peru', 'wp-security-pack' ),
'PH' => __( 'Philippines', 'wp-security-pack' ),
'PL' => __( 'Poland', 'wp-security-pack' ),
'PT' => __( 'Portugal', 'wp-security-pack' ),
'PR' => __( 'Puerto Rico', 'wp-security-pack' ),
'QA' => __( 'Qatar', 'wp-security-pack' ),
'RO' => __( 'Romania', 'wp-security-pack' ),
'RU' => __( 'Russia', 'wp-security-pack' ),
'RW' => __( 'Rwanda', 'wp-security-pack' ),
'SA' => __( 'Saudi Arabia', 'wp-security-pack' ),
'SN' => __( 'Senegal', 'wp-security-pack' ),
'RS' => __( 'Serbia', 'wp-security-pack' ),
'SC' => __( 'Seychelles', 'wp-security-pack' ),
'SL' => __( 'Sierra Leone', 'wp-security-pack' ),
'SG' => __( 'Singapore', 'wp-security-pack' ),
'SK' => __( 'Slovakia', 'wp-security-pack' ),
'SI' => __( 'Slovenia', 'wp-security-pack' ),
'SB' => __( 'Solomon Islands', 'wp-security-pack' ),
'SO' => __( 'Somalia', 'wp-security-pack' ),
'ZA' => __( 'South Africa', 'wp-security-pack' ),
'SS' => __( 'South Sudan', 'wp-security-pack' ),
'ES' => __( 'Spain', 'wp-security-pack' ),
'LK' => __( 'Sri Lanka', 'wp-security-pack' ),
'SD' => __( 'Sudan', 'wp-security-pack' ),
'SR' => __( 'Suriname', 'wp-security-pack' ),
'SZ' => __( 'Eswatini', 'wp-security-pack' ),
'SE' => __( 'Sweden', 'wp-security-pack' ),
'CH' => __( 'Switzerland', 'wp-security-pack' ),
'SY' => __( 'Syria', 'wp-security-pack' ),
'TW' => __( 'Taiwan', 'wp-security-pack' ),
'TJ' => __( 'Tajikistan', 'wp-security-pack' ),
'TZ' => __( 'Tanzania', 'wp-security-pack' ),
'TH' => __( 'Thailand', 'wp-security-pack' ),
'TL' => __( 'Timor-Leste', 'wp-security-pack' ),
'TG' => __( 'Togo', 'wp-security-pack' ),
'TO' => __( 'Tonga', 'wp-security-pack' ),
'TT' => __( 'Trinidad and Tobago', 'wp-security-pack' ),
'TN' => __( 'Tunisia', 'wp-security-pack' ),
'TR' => __( 'Turkey', 'wp-security-pack' ),
'TM' => __( 'Turkmenistan', 'wp-security-pack' ),
'TV' => __( 'Tuvalu', 'wp-security-pack' ),
'UG' => __( 'Uganda', 'wp-security-pack' ),
'UA' => __( 'Ukraine', 'wp-security-pack' ),
'AE' => __( 'United Arab Emirates', 'wp-security-pack' ),
'GB' => __( 'United Kingdom', 'wp-security-pack' ),
'US' => __( 'United States', 'wp-security-pack' ),
'UY' => __( 'Uruguay', 'wp-security-pack' ),
'UZ' => __( 'Uzbekistan', 'wp-security-pack' ),
'VU' => __( 'Vanuatu', 'wp-security-pack' ),
'VE' => __( 'Venezuela', 'wp-security-pack' ),
'VN' => __( 'Vietnam', 'wp-security-pack' ),
'YE' => __( 'Yemen', 'wp-security-pack' ),
'ZM' => __( 'Zambia', 'wp-security-pack' ),
'ZW' => __( 'Zimbabwe', 'wp-security-pack' ),
);
}
/**
* Sanitize a custom login URL slug.
*
* @param string $slug The slug to sanitize.
* @return string
*/
public static function sanitize_login_slug( $slug ) {
$slug = sanitize_title( $slug );
$slug = preg_replace( '/[^a-z0-9\-]/', '', $slug );
// Prevent common reserved slugs.
$reserved = array( 'wp-admin', 'wp-login', 'admin', 'login', 'wp-content', 'wp-includes' );
if ( in_array( $slug, $reserved, true ) ) {
return '';
}
return $slug;
}
/**
* Get the server's public IP address.
*
* Uses external service to determine the server's outbound IP.
* Result is cached for 1 hour to avoid excessive external requests.
*
* @return string|null Server IP or null on failure.
*/
public static function get_server_ip() {
$cached = get_transient( 'wpsp_server_ip' );
if ( false !== $cached ) {
return $cached;
}
// Try multiple services for reliability.
$services = array(
'https://api.ipify.org',
'https://ifconfig.me/ip',
'https://icanhazip.com',
);
$ip = null;
foreach ( $services as $service ) {
$response = wp_remote_get(
$service,
array(
'timeout' => 5,
'sslverify' => true,
)
);
if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) {
$body = trim( wp_remote_retrieve_body( $response ) );
if ( filter_var( $body, FILTER_VALIDATE_IP ) ) {
$ip = $body;
break;
}
}
}
if ( $ip ) {
// Cache for 1 hour.
set_transient( 'wpsp_server_ip', $ip, HOUR_IN_SECONDS );
}
return $ip;
}
/**
* Get country flag emoji from country code.
*
* Converts ISO 3166-1 alpha-2 country codes to Unicode flag emojis.
*
* @param string $country_code Two-letter country code (e.g., "US", "GB").
* @return string Flag emoji or globe emoji for invalid codes.
*/
public static function get_country_flag( $country_code ) {
if ( empty( $country_code ) || strlen( $country_code ) !== 2 ) {
return '🌐'; // Globe emoji for invalid codes.
}
$country_code = strtoupper( $country_code );
// Convert country code to Unicode regional indicator symbols.
// Regional indicators are U+1F1E6 (A) through U+1F1FF (Z).
$first_letter = mb_chr( ord( $country_code[0] ) - ord( 'A' ) + 0x1F1E6, 'UTF-8' );
$second_letter = mb_chr( ord( $country_code[1] ) - ord( 'A' ) + 0x1F1E6, 'UTF-8' );
return $first_letter . $second_letter;
}
/**
* Get countries array with flags.
*
* @return array Country code => "Flag Name" format.
*/
public static function get_countries_with_flags() {
$countries = self::get_countries();
$countries_flags = array();
foreach ( $countries as $code => $name ) {
$flag = self::get_country_flag( $code );
$countries_flags[ $code ] = $flag . ' ' . $name;
}
return $countries_flags;
}
}