<?php

// security check
if( ! defined( 'WP_PLUGIN_DIR' ) )
{
	die( 'There is nothing to see here.' );
}

$counterize_plugin_dir = WP_PLUGIN_DIR . '/' . plugin_basename( dirname( __FILE__ ) );
if( counterize_is_ssl() )
{
	$counterize_plugin_url = str_replace( 'http://', 'https://', WP_PLUGIN_URL ) . '/' . plugin_basename( dirname( __FILE__ ) );
}
else
{
	$counterize_plugin_url = WP_PLUGIN_URL . '/' . plugin_basename( dirname( __FILE__ ) );
}
$countries = Array();

require_once( "{$counterize_plugin_dir}/ip_files/countries.php" );

$counterize_countries = $countries;
unset( $countries );

//returns the 2 characters country code for the specified IP address
function counterize_iptocountrycode( $ip )
{
	global $counterize_plugin_dir;

	$numbers = explode( '.', $ip );

	if( count( $numbers ) != 4 )
	{
		return '00';
	}

	$ranges = Array();
	$country = 'unknown';

	include( $counterize_plugin_dir . '/ip_files/' . $numbers[0] . '.php' );

	$code = ( $numbers[0] * 0x1000000 ) + ( $numbers[1] * 0x10000 ) + ( $numbers[2] * 0x100 ) + $numbers[3];

	foreach( $ranges as $key => $value )
	{
		if( $key <= $code && $value[0] >= $code )
		{
			$country = $value[1];
			break;
		}
	}
	return $country;
}

//returns the 3 characters country code from the 2 characters country code in parameter
function counterize_get_countrycode3( $code2 )
{
	global $counterize_countries;
	if( $code2 != '00' )
	{
		return $counterize_countries[$code2][0];
	}
	else
	{
		return '???';
	}
}

//returns the complete country name from the 2 characters country code in parameter
function counterize_get_countryname( $code2 )
{
	global $counterize_countries;
	if( $code2 != '00' )
	{
		return $counterize_countries[$code2][1];
	}
	else
	{
		return __( 'Unknown', COUNTERIZE_TD );
	}
}

//return a img tag of the country
function counterize_get_flag_tag( $code, $alt )
{
	global $counterize_plugin_dir;
	global $counterize_plugin_url;
	global $counterize_width_height;

	$alt = htmlspecialchars( $alt );
	$code = htmlspecialchars( $code );
	$src = counterize_get_flag_url( $code );
	$res = '';

	if( $src != '' )
	{
		$res .= "
								<img
									src='{$src}'
									alt='" . sprintf( __( 'National flag of %s', COUNTERIZE_TD ), $alt ) . "'
									title='" . counterize_get_countryname( $code ) . " ({$code})'
									height='{$counterize_width_height}'
									class='countryflag'
								 />
									";
	}

	return $res;
}

//return the URL of the country flag
function counterize_get_flag_url( $code )
{
	global $counterize_plugin_dir;
	global $counterize_plugin_url;
	if( file_exists( "{$counterize_plugin_dir}/ip_files/flags/{$code}.gif" ) )
	{
		return "{$counterize_plugin_url}/ip_files/flags/{$code}.gif";
	}
	elseif( file_exists( "{$counterize_plugin_dir}/ip_files/flags/" . strtolower( $code ) . ".gif" ) )
	{
		return "{$counterize_plugin_url}/ip_files/flags/" . strtolower( $code ) . ".gif";
	}
	else
	{
		return '';
	}
}

?>