HEX
Server: Apache
System: Linux vps.teamads.com 4.18.0-553.126.1.el8_10.x86_64 #1 SMP Thu May 28 06:44:09 EDT 2026 x86_64
User: teamadsc (1024)
PHP: 8.1.34
Disabled: NONE
Upload Files
File: /home/teamadsc/public_html/wp-content/plugins/wp-defender/src/extra/class-geoip.php
<?php
/**
 * GeoIP functionality for WP Defender.
 *
 * @package WP_Defender\Extra
 */

namespace WP_Defender\Extra;

use MaxMind\Db\Reader;

/**
 * GeoIP class for IP geolocation functionality.
 */
class GeoIp {
	/**
	 * MaxMind database reader instance.
	 *
	 * @var Reader
	 */
	protected $provider;

	/**
	 * Constructor.
	 *
	 * @param string $db_path Path to the MaxMind database file.
	 */
	public function __construct( $db_path ) {
		$this->provider = new Reader( $db_path );
	}

	/**
	 * Convert IP address to country information.
	 *
	 * @param string $ip IP address to lookup.
	 *
	 * @return array<string, string>|false Country data array with 'iso' and 'name' keys, or false on failure.
	 * @throws Reader\InvalidDatabaseException When database format is invalid.
	 */
	public function ip_to_country( $ip ) {
		$info = $this->provider->get( $ip );

		// Try country first, then fall back to registered_country.
		$country_data = null;
		if ( isset( $info['country'] ) && is_array( $info['country'] ) ) {
			$country_data = $info['country'];
		} elseif ( isset( $info['registered_country'] ) && is_array( $info['registered_country'] ) ) {
			$country_data = $info['registered_country'];
		}

		if ( null === $country_data ) {
			return false;
		}

		return array(
			'iso'  => $country_data['iso_code'] ?? '',
			'name' => $country_data['names']['en'] ?? '',
		);
	}
}