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', 'arkhost-security-pack' ), 'AL' => __( 'Albania', 'arkhost-security-pack' ), 'DZ' => __( 'Algeria', 'arkhost-security-pack' ), 'AS' => __( 'American Samoa', 'arkhost-security-pack' ), 'AD' => __( 'Andorra', 'arkhost-security-pack' ), 'AO' => __( 'Angola', 'arkhost-security-pack' ), 'AI' => __( 'Anguilla', 'arkhost-security-pack' ), 'AQ' => __( 'Antarctica', 'arkhost-security-pack' ), 'AG' => __( 'Antigua and Barbuda', 'arkhost-security-pack' ), 'AR' => __( 'Argentina', 'arkhost-security-pack' ), 'AM' => __( 'Armenia', 'arkhost-security-pack' ), 'AW' => __( 'Aruba', 'arkhost-security-pack' ), 'AU' => __( 'Australia', 'arkhost-security-pack' ), 'AT' => __( 'Austria', 'arkhost-security-pack' ), 'AZ' => __( 'Azerbaijan', 'arkhost-security-pack' ), 'BS' => __( 'Bahamas', 'arkhost-security-pack' ), 'BH' => __( 'Bahrain', 'arkhost-security-pack' ), 'BD' => __( 'Bangladesh', 'arkhost-security-pack' ), 'BB' => __( 'Barbados', 'arkhost-security-pack' ), 'BY' => __( 'Belarus', 'arkhost-security-pack' ), 'BE' => __( 'Belgium', 'arkhost-security-pack' ), 'BZ' => __( 'Belize', 'arkhost-security-pack' ), 'BJ' => __( 'Benin', 'arkhost-security-pack' ), 'BM' => __( 'Bermuda', 'arkhost-security-pack' ), 'BT' => __( 'Bhutan', 'arkhost-security-pack' ), 'BO' => __( 'Bolivia', 'arkhost-security-pack' ), 'BA' => __( 'Bosnia and Herzegovina', 'arkhost-security-pack' ), 'BW' => __( 'Botswana', 'arkhost-security-pack' ), 'BR' => __( 'Brazil', 'arkhost-security-pack' ), 'BN' => __( 'Brunei', 'arkhost-security-pack' ), 'BG' => __( 'Bulgaria', 'arkhost-security-pack' ), 'BF' => __( 'Burkina Faso', 'arkhost-security-pack' ), 'BI' => __( 'Burundi', 'arkhost-security-pack' ), 'KH' => __( 'Cambodia', 'arkhost-security-pack' ), 'CM' => __( 'Cameroon', 'arkhost-security-pack' ), 'CA' => __( 'Canada', 'arkhost-security-pack' ), 'CV' => __( 'Cape Verde', 'arkhost-security-pack' ), 'KY' => __( 'Cayman Islands', 'arkhost-security-pack' ), 'CF' => __( 'Central African Republic', 'arkhost-security-pack' ), 'TD' => __( 'Chad', 'arkhost-security-pack' ), 'CL' => __( 'Chile', 'arkhost-security-pack' ), 'CN' => __( 'China', 'arkhost-security-pack' ), 'CO' => __( 'Colombia', 'arkhost-security-pack' ), 'KM' => __( 'Comoros', 'arkhost-security-pack' ), 'CG' => __( 'Congo', 'arkhost-security-pack' ), 'CD' => __( 'Congo (DRC)', 'arkhost-security-pack' ), 'CR' => __( 'Costa Rica', 'arkhost-security-pack' ), 'CI' => __( 'Ivory Coast', 'arkhost-security-pack' ), 'HR' => __( 'Croatia', 'arkhost-security-pack' ), 'CU' => __( 'Cuba', 'arkhost-security-pack' ), 'CY' => __( 'Cyprus', 'arkhost-security-pack' ), 'CZ' => __( 'Czech Republic', 'arkhost-security-pack' ), 'DK' => __( 'Denmark', 'arkhost-security-pack' ), 'DJ' => __( 'Djibouti', 'arkhost-security-pack' ), 'DM' => __( 'Dominica', 'arkhost-security-pack' ), 'DO' => __( 'Dominican Republic', 'arkhost-security-pack' ), 'EC' => __( 'Ecuador', 'arkhost-security-pack' ), 'EG' => __( 'Egypt', 'arkhost-security-pack' ), 'SV' => __( 'El Salvador', 'arkhost-security-pack' ), 'GQ' => __( 'Equatorial Guinea', 'arkhost-security-pack' ), 'ER' => __( 'Eritrea', 'arkhost-security-pack' ), 'EE' => __( 'Estonia', 'arkhost-security-pack' ), 'ET' => __( 'Ethiopia', 'arkhost-security-pack' ), 'FJ' => __( 'Fiji', 'arkhost-security-pack' ), 'FI' => __( 'Finland', 'arkhost-security-pack' ), 'FR' => __( 'France', 'arkhost-security-pack' ), 'GA' => __( 'Gabon', 'arkhost-security-pack' ), 'GM' => __( 'Gambia', 'arkhost-security-pack' ), 'GE' => __( 'Georgia', 'arkhost-security-pack' ), 'DE' => __( 'Germany', 'arkhost-security-pack' ), 'GH' => __( 'Ghana', 'arkhost-security-pack' ), 'GR' => __( 'Greece', 'arkhost-security-pack' ), 'GL' => __( 'Greenland', 'arkhost-security-pack' ), 'GD' => __( 'Grenada', 'arkhost-security-pack' ), 'GU' => __( 'Guam', 'arkhost-security-pack' ), 'GT' => __( 'Guatemala', 'arkhost-security-pack' ), 'GN' => __( 'Guinea', 'arkhost-security-pack' ), 'GW' => __( 'Guinea-Bissau', 'arkhost-security-pack' ), 'GY' => __( 'Guyana', 'arkhost-security-pack' ), 'HT' => __( 'Haiti', 'arkhost-security-pack' ), 'HN' => __( 'Honduras', 'arkhost-security-pack' ), 'HK' => __( 'Hong Kong', 'arkhost-security-pack' ), 'HU' => __( 'Hungary', 'arkhost-security-pack' ), 'IS' => __( 'Iceland', 'arkhost-security-pack' ), 'IN' => __( 'India', 'arkhost-security-pack' ), 'ID' => __( 'Indonesia', 'arkhost-security-pack' ), 'IR' => __( 'Iran', 'arkhost-security-pack' ), 'IQ' => __( 'Iraq', 'arkhost-security-pack' ), 'IE' => __( 'Ireland', 'arkhost-security-pack' ), 'IL' => __( 'Israel', 'arkhost-security-pack' ), 'IT' => __( 'Italy', 'arkhost-security-pack' ), 'JM' => __( 'Jamaica', 'arkhost-security-pack' ), 'JP' => __( 'Japan', 'arkhost-security-pack' ), 'JO' => __( 'Jordan', 'arkhost-security-pack' ), 'KZ' => __( 'Kazakhstan', 'arkhost-security-pack' ), 'KE' => __( 'Kenya', 'arkhost-security-pack' ), 'KI' => __( 'Kiribati', 'arkhost-security-pack' ), 'KP' => __( 'North Korea', 'arkhost-security-pack' ), 'KR' => __( 'South Korea', 'arkhost-security-pack' ), 'KW' => __( 'Kuwait', 'arkhost-security-pack' ), 'KG' => __( 'Kyrgyzstan', 'arkhost-security-pack' ), 'LA' => __( 'Laos', 'arkhost-security-pack' ), 'LV' => __( 'Latvia', 'arkhost-security-pack' ), 'LB' => __( 'Lebanon', 'arkhost-security-pack' ), 'LS' => __( 'Lesotho', 'arkhost-security-pack' ), 'LR' => __( 'Liberia', 'arkhost-security-pack' ), 'LY' => __( 'Libya', 'arkhost-security-pack' ), 'LI' => __( 'Liechtenstein', 'arkhost-security-pack' ), 'LT' => __( 'Lithuania', 'arkhost-security-pack' ), 'LU' => __( 'Luxembourg', 'arkhost-security-pack' ), 'MO' => __( 'Macao', 'arkhost-security-pack' ), 'MK' => __( 'North Macedonia', 'arkhost-security-pack' ), 'MG' => __( 'Madagascar', 'arkhost-security-pack' ), 'MW' => __( 'Malawi', 'arkhost-security-pack' ), 'MY' => __( 'Malaysia', 'arkhost-security-pack' ), 'MV' => __( 'Maldives', 'arkhost-security-pack' ), 'ML' => __( 'Mali', 'arkhost-security-pack' ), 'MT' => __( 'Malta', 'arkhost-security-pack' ), 'MH' => __( 'Marshall Islands', 'arkhost-security-pack' ), 'MR' => __( 'Mauritania', 'arkhost-security-pack' ), 'MU' => __( 'Mauritius', 'arkhost-security-pack' ), 'MX' => __( 'Mexico', 'arkhost-security-pack' ), 'FM' => __( 'Micronesia', 'arkhost-security-pack' ), 'MD' => __( 'Moldova', 'arkhost-security-pack' ), 'MC' => __( 'Monaco', 'arkhost-security-pack' ), 'MN' => __( 'Mongolia', 'arkhost-security-pack' ), 'ME' => __( 'Montenegro', 'arkhost-security-pack' ), 'MA' => __( 'Morocco', 'arkhost-security-pack' ), 'MZ' => __( 'Mozambique', 'arkhost-security-pack' ), 'MM' => __( 'Myanmar', 'arkhost-security-pack' ), 'NA' => __( 'Namibia', 'arkhost-security-pack' ), 'NR' => __( 'Nauru', 'arkhost-security-pack' ), 'NP' => __( 'Nepal', 'arkhost-security-pack' ), 'NL' => __( 'Netherlands', 'arkhost-security-pack' ), 'NZ' => __( 'New Zealand', 'arkhost-security-pack' ), 'NI' => __( 'Nicaragua', 'arkhost-security-pack' ), 'NE' => __( 'Niger', 'arkhost-security-pack' ), 'NG' => __( 'Nigeria', 'arkhost-security-pack' ), 'NO' => __( 'Norway', 'arkhost-security-pack' ), 'OM' => __( 'Oman', 'arkhost-security-pack' ), 'PK' => __( 'Pakistan', 'arkhost-security-pack' ), 'PW' => __( 'Palau', 'arkhost-security-pack' ), 'PS' => __( 'Palestine', 'arkhost-security-pack' ), 'PA' => __( 'Panama', 'arkhost-security-pack' ), 'PG' => __( 'Papua New Guinea', 'arkhost-security-pack' ), 'PY' => __( 'Paraguay', 'arkhost-security-pack' ), 'PE' => __( 'Peru', 'arkhost-security-pack' ), 'PH' => __( 'Philippines', 'arkhost-security-pack' ), 'PL' => __( 'Poland', 'arkhost-security-pack' ), 'PT' => __( 'Portugal', 'arkhost-security-pack' ), 'PR' => __( 'Puerto Rico', 'arkhost-security-pack' ), 'QA' => __( 'Qatar', 'arkhost-security-pack' ), 'RO' => __( 'Romania', 'arkhost-security-pack' ), 'RU' => __( 'Russia', 'arkhost-security-pack' ), 'RW' => __( 'Rwanda', 'arkhost-security-pack' ), 'SA' => __( 'Saudi Arabia', 'arkhost-security-pack' ), 'SN' => __( 'Senegal', 'arkhost-security-pack' ), 'RS' => __( 'Serbia', 'arkhost-security-pack' ), 'SC' => __( 'Seychelles', 'arkhost-security-pack' ), 'SL' => __( 'Sierra Leone', 'arkhost-security-pack' ), 'SG' => __( 'Singapore', 'arkhost-security-pack' ), 'SK' => __( 'Slovakia', 'arkhost-security-pack' ), 'SI' => __( 'Slovenia', 'arkhost-security-pack' ), 'SB' => __( 'Solomon Islands', 'arkhost-security-pack' ), 'SO' => __( 'Somalia', 'arkhost-security-pack' ), 'ZA' => __( 'South Africa', 'arkhost-security-pack' ), 'SS' => __( 'South Sudan', 'arkhost-security-pack' ), 'ES' => __( 'Spain', 'arkhost-security-pack' ), 'LK' => __( 'Sri Lanka', 'arkhost-security-pack' ), 'SD' => __( 'Sudan', 'arkhost-security-pack' ), 'SR' => __( 'Suriname', 'arkhost-security-pack' ), 'SZ' => __( 'Eswatini', 'arkhost-security-pack' ), 'SE' => __( 'Sweden', 'arkhost-security-pack' ), 'CH' => __( 'Switzerland', 'arkhost-security-pack' ), 'SY' => __( 'Syria', 'arkhost-security-pack' ), 'TW' => __( 'Taiwan', 'arkhost-security-pack' ), 'TJ' => __( 'Tajikistan', 'arkhost-security-pack' ), 'TZ' => __( 'Tanzania', 'arkhost-security-pack' ), 'TH' => __( 'Thailand', 'arkhost-security-pack' ), 'TL' => __( 'Timor-Leste', 'arkhost-security-pack' ), 'TG' => __( 'Togo', 'arkhost-security-pack' ), 'TO' => __( 'Tonga', 'arkhost-security-pack' ), 'TT' => __( 'Trinidad and Tobago', 'arkhost-security-pack' ), 'TN' => __( 'Tunisia', 'arkhost-security-pack' ), 'TR' => __( 'Turkey', 'arkhost-security-pack' ), 'TM' => __( 'Turkmenistan', 'arkhost-security-pack' ), 'TV' => __( 'Tuvalu', 'arkhost-security-pack' ), 'UG' => __( 'Uganda', 'arkhost-security-pack' ), 'UA' => __( 'Ukraine', 'arkhost-security-pack' ), 'AE' => __( 'United Arab Emirates', 'arkhost-security-pack' ), 'GB' => __( 'United Kingdom', 'arkhost-security-pack' ), 'US' => __( 'United States', 'arkhost-security-pack' ), 'UY' => __( 'Uruguay', 'arkhost-security-pack' ), 'UZ' => __( 'Uzbekistan', 'arkhost-security-pack' ), 'VU' => __( 'Vanuatu', 'arkhost-security-pack' ), 'VE' => __( 'Venezuela', 'arkhost-security-pack' ), 'VN' => __( 'Vietnam', 'arkhost-security-pack' ), 'YE' => __( 'Yemen', 'arkhost-security-pack' ), 'ZM' => __( 'Zambia', 'arkhost-security-pack' ), 'ZW' => __( 'Zimbabwe', 'arkhost-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( 'arksp_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( 'arksp_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; } }