';
$html[] = '
';
$services = $this->get_services();
$services_used_forip = isset($options['services_used_forip'])
? (string) $options['services_used_forip'] : 'db-ip.com';
$services_saved = get_option('WooZone_services_saved', array());
$checked_ip = isset($services_saved["ip"]) ? $services_saved["ip"] : Utils::getInstance()->get_client_ip();
//var_dump('
', $checked_ip, $services_used_forip, $services , '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
$allowed_countries = WooZone()->get_all_country2mainaffid( array(
'uk2gb' => true,
));
//var_dump('
', $allowed_countries , '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
ob_start();
?>
This option allows you to use an external server api for detecting client country per IP address or you can try our local IP detection.
Client country (per IP address) is used on website frontend, when we redirect to the amazon store for that country.
IP =
!!! if country = NOT-FOUND or if country it's not one of the following countries () then the selected Main Affiliate ID (from this amazon config module / Amazon Setup tab / General & Amazon) will be used instead.
get_box_list_services( array(
'services' => $services,
'services_used_forip' => $services_used_forip,
'services_saved' => $services_saved,
));
?>
'; //end main box
return implode( "\n", $html );
}
public function get_box_list_services( $pms=array() ) {
// parameters
$pms = array_merge(array(
'services' => null,
'services_used_forip' => null,
'services_saved' => null,
), $pms);
if ( is_null($pms['services']) ) {
$pms['services'] = $this->get_services();
}
if ( is_null($pms['services_used_forip']) ) {
$pms['services_used_forip'] = isset($options['services_used_forip'])
? (string) $options['services_used_forip'] : 'db-ip.com';
}
if ( is_null($pms['services_saved']) ) {
$pms['services_saved'] = get_option('WooZone_services_saved', array());
}
//var_dump('
', $services_used_forip, $services_saved, $services, '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
extract($pms);
//var_dump('
', $pms, '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
$services_saved_list = isset($services_saved['response'])
? $services_saved['response'] : array();
$html = array();
ob_start();
?>
Service API
Service Keys
Last check
Last status
$sinfo) {
$is_selected = $services_used_forip === $sname ? true : false;
$css_is_selected = $is_selected ? 'wzg-item-selected' : '';
$url_website = isset($sinfo['website']) ? $sinfo['website'] : null;
//$url_api = GeoLocation::getInstance()->get_service_apiurl( $sname );
$url_api = isset($sinfo['api_url']) ? $sinfo['api_url'] : null;
$api_keys = isset($sinfo['keys']) ? $sinfo['keys'] : array();
$checked_serv = isset($services_saved_list["$sname"]) ? $services_saved_list["$sname"] : null;
$checked_data = isset($checked_serv["checked_date"]) ? $checked_serv["checked_date"] : null;
$checked_data = ! is_null($checked_data) ? date('Y-m-d h:i', $checked_data) : '';
$checked_resp = isset($checked_serv["response"]) ? $checked_serv["response"] : array();
//var_dump('
',$checked_data, $checked_resp ,'
'); continue 1; //DEBUG
$css_resp_status = '';
if ( 'invalid' === $checked_resp['status'] ) {
$css_resp_status = 'wzg-item-status-invalid';
} else if ( 'valid' === $checked_resp['status'] ) {
$css_resp_status = 'wzg-item-status-valid';
}
$css_item = $css_is_selected;
$css_item2 = implode(' ', array($css_is_selected, $css_resp_status));
$checked_resp_show = array();
if ( isset($checked_resp['country']) ) {
$checked_resp_show[] = "
COUNTRY = {$checked_resp['country']}";
}
if ( isset($checked_resp['status']) && 'invalid' === $checked_resp['status'] ) {
$checked_resp_show[] = " | {$checked_resp['msg']}";
}
$checked_resp_show = implode('', $checked_resp_show);
?>
$skey_info) {
$elem_name = "services_saved[$sname][keys][$skey_name]";
$elem_value = isset($skey_info['value']) ? $skey_info['value'] : null;
$elem_placeh = sprintf( __('enter %s here', WooZone()->localizationName), $skey_name );
?>
'NOT-FOUND',
'status' => 'invalid',
'msg' => '',
);
$service_url = $this->get_service_apiurl( $service );
if ( empty($service_url) ) {
$msg = 'api service not found!';
$ret = array_replace_recursive($ret, array(
//'country' => $country,
'status' => 'invalid',
'msg' => $msg,
));
return $ret;
}
$get_user_location = wp_remote_get( $service_url );
if ( is_wp_error( $get_user_location ) ) {
//$msg = is_array($get_user_location->errors) ? maybe_serialize($get_user_location->errors) : $get_user_location->errors;
$msg = maybe_serialize( $get_user_location->get_error_message() );
$ret = array_replace_recursive($ret, array(
//'country' => $country,
'status' => 'invalid',
'msg' => $msg,
));
//var_dump('
', $service, $service_url, $ret , '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
return $ret;
}
//if ( wp_remote_retrieve_response_code( $get_user_location ) !== 200 ) ;
//$content = isset($get_user_location['body']) ? $get_user_location['body'] : '';
$content = wp_remote_retrieve_body( $get_user_location );
//var_dump('
', $service, $service_url, $content , '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
$content = $this->_parse_service_external( $service, $content );
$ret = array_replace_recursive($ret, $content);
//var_dump('
', $service, $service_url, $ret , '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
return $ret;
}
private function _parse_service_external( $service, $content ) {
$country = 'NOT-FOUND';
$msg = '';
$ret = array(
'country' => 'NOT-FOUND',
'status' => 'invalid',
'msg' => '',
);
if ( is_null($content) ) {
$msg = 'api service json response: content is empty!';
$ret = array_replace_recursive($ret, array(
'msg' => $msg,
));
return $ret;
}
$content = json_decode($content);
//var_dump('
', $service, $service_url, $content , '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
if ( ! is_object($content) ) {
$msg = 'api service json response: jsone decoded is invalid!';
$ret = array_replace_recursive($ret, array(
'msg' => $msg,
));
return $ret;
}
$country = $content;
switch ($service) {
// case 'www.geoplugin.net':
// if ( isset($country->geoplugin_countryCode) ) {
// $country = (string) $country->geoplugin_countryCode;
// $country = strtoupper( $country );
// } else {
// $country = 'NOT-FOUND';
// $msg = 'api service json response: missing field geoplugin_countryCode';
// }
// break;
case 'ipinfo.io':
if ( isset($country->country) ) {
$country = (string) $country->country;
$country = strtoupper( $country );
}
else if ( isset($country->error->message) ) {
$msg = 'api service json response: ' . ((string) $country->error->message);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'api.ipstack.com':
if ( isset($country->country_code) ) {
$country = (string) $country->country_code;
$country = strtoupper( $country );
}
else if ( isset($country->error->info) ) {
$msg = 'api service json response: ' . ((string) $country->error->info);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'ipapi.com':
if ( isset($country->country_code) ) {
$country = (string) $country->country_code;
$country = strtoupper( $country );
}
else if ( isset($country->error->info) ) {
$msg = 'api service json response: ' . ((string) $country->error->info);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'whoisxmlapi.com':
if ( isset($country->location->country) ) {
$country = (string) $country->location->country;
$country = strtoupper( $country );
}
else if ( isset($country->error) ) {
$msg = 'api service json response: ' . ((string) $country->error);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'geo.ipify.org':
if ( isset($country->location->country) ) {
$country = (string) $country->location->country;
$country = strtoupper( $country );
}
else if ( isset($country->messages) ) {
$msg = 'api service json response: ' . ((string) $country->messages);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'ipinfodb.com':
if ( isset($country->countryCode) && ! empty($country->countryCode) ) {
$country = (string) $country->countryCode;
$country = strtoupper( $country );
}
else if ( isset($country->statusMessage) ) {
$msg = 'api service json response: ' . ((string) $country->statusMessage);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'db-ip.com':
if ( isset($country->countryCode) && ! empty($country->countryCode) ) {
$country = (string) $country->countryCode;
$country = strtoupper( $country );
}
else if ( isset($country->error) ) {
$msg = 'api service json response: ' . ((string) $country->error);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'ipdata.co':
if ( isset($country->country_code) && ! empty($country->country_code) ) {
$country = (string) $country->country_code;
$country = strtoupper( $country );
}
else if ( isset($country->message) ) {
$msg = 'api service json response: ' . ((string) $country->message);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'ipgeolocation.io':
if ( isset($country->country_code2) && ! empty($country->country_code2) ) {
$country = (string) $country->country_code2;
$country = strtoupper( $country );
}
else if ( isset($country->message) ) {
$msg = 'api service json response: ' . ((string) $country->message);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'ip2location.com':
if ( isset($country->country_code) && ! empty($country->country_code) ) {
$country = (string) $country->country_code;
$country = strtoupper( $country );
}
else if ( isset($country->response) ) {
$msg = 'api service json response: ' . ((string) $country->response);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'ip-api.com':
if ( isset($country->countryCode) && ! empty($country->countryCode) ) {
$country = (string) $country->countryCode;
$country = strtoupper( $country );
}
else if ( isset($country->message) ) {
$msg = 'api service json response: ' . ((string) $country->message);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'ipapi.co':
if ( isset($country->country_code) && ! empty($country->country_code) ) {
$country = (string) $country->country_code;
$country = strtoupper( $country );
}
else if ( isset($country->reason) ) {
$msg = 'api service json response: ' . ((string) $country->reason);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'api.ipgeolocationapi.com':
if ( isset($country->alpha2) && ! empty($country->alpha2) ) {
$country = (string) $country->alpha2;
$country = strtoupper( $country );
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'freegeoip.live':
if ( isset($country->country_code) && ! empty($country->country_code) ) {
$country = (string) $country->country_code;
$country = strtoupper( $country );
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'api.snoopi.io':
if ( isset($country->CountryCode) && ! empty($country->CountryCode) ) {
$country = (string) $country->CountryCode;
$country = strtoupper( $country );
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'iplocate.io':
if ( isset($country->country_code) && ! empty($country->country_code) ) {
$country = (string) $country->country_code;
$country = strtoupper( $country );
}
else if ( isset($country->error) ) {
$msg = 'api service json response: ' . ((string) $country->error);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'ipwhois.io':
if ( isset($country->country_code) && ! empty($country->country_code) ) {
$country = (string) $country->country_code;
$country = strtoupper( $country );
}
else if ( isset($country->message) ) {
$msg = 'api service json response: ' . ((string) $country->message);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
case 'extreme-ip-lookup.com':
if ( isset($country->countryCode) && ! empty($country->countryCode) ) {
$country = (string) $country->countryCode;
$country = strtoupper( $country );
}
else if ( isset($country->message) ) {
$msg = 'api service json response: ' . ((string) $country->message);
$country = 'NOT-FOUND';
}
else {
$msg = 'api service json response: missing field country';
$country = 'NOT-FOUND';
}
break;
default:
$msg = 'api service not found!';
$country = 'NOT-FOUND';
break;
}
$ret = array_replace_recursive($ret, array(
'country' => $country,
'status' => 'NOT-FOUND' === $country ? 'invalid' : 'valid',
'msg' => $msg,
));
//var_dump('
', $service, $service_url, $ret , '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
return $ret;
}
private function _get_service_local_csv() {
$country = 'NOT-FOUND';
$msg = '';
$ret = array(
'country' => 'NOT-FOUND',
'status' => 'invalid',
'msg' => '',
);
$ip = Utils::getInstance()->get_client_ip();
$ip2number = Utils::getInstance()->ip2number( $ip );
//var_dump('
', "ip = $ip, ipnumber = $ip2number", '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
// read csv hash (string with ip from list)
$file = WooZone()->cfg['paths']['plugin_dir_path'] . 'assets/geolocation/GeoIPCountryWhois-hash.csv';
$csv_hash = file_get_contents($file);
//var_dump('
', $csv_hash, '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
$csv_hash = explode(',', $csv_hash);
//var_dump('
', $csv_hash, '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
// read csv full (ip from, ip to, country)
$file = WooZone()->cfg['paths']['plugin_dir_path'] . 'assets/geolocation/GeoIPCountryWhois-full.csv';
$csv_full = file_get_contents($file);
//var_dump('
', $csv_full, '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
$csv_full = explode(PHP_EOL, $csv_full);
//var_dump('
', $csv_full, '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
$ipHashIndex = $this->_binary_search( $ip2number, $csv_hash, array($this, '_binary_search_cmp') );
//var_dump('
', $ipHashIndex , '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
// verify if is between (ip_from, ip_to) of csv row
if ( $ipHashIndex < 0 ) {
$ipHashIndex = abs( $ipHashIndex );
$ipFullRow = $csv_full["$ipHashIndex"];
$csv_row = explode(',', $ipFullRow);
if ( $ip2number >= $csv_row[0] && $ip2number <= $csv_row[1] ) {
$country = $csv_row[2];
} else {
$msg = 'local_csv: country not found!';
}
}
// exact match in the list as ip_from of csv row
else {
$ipFullRow = $csv_full["$ipHashIndex"];
$country = explode(',', $ipFullRow);
$country = end($country);
}
$country = strtoupper( $country );
//var_dump('
', $ipHashIndex, $ipFullRow, $country , '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
$ret = array_replace_recursive($ret, array(
'country' => $country,
'status' => 'NOT-FOUND' === $country ? 'invalid' : 'valid',
'msg' => $msg,
));
return $ret;
}
//================================================
//== MISC
private function _get_service_apiurl( $curservice_info ) {
$ip = Utils::getInstance()->get_client_ip();
$ip2number = null; //Utils::getInstance()->ip2number( $ip );
//var_dump('
', "ip = $ip, ipnumber = $ip2number", '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
$service_keys = isset($curservice_info['keys']) ? $curservice_info['keys'] : array();
$service_url = isset($curservice_info['api']) ? $curservice_info['api'] : null;
if ( empty($service_url) ) {
return null;
}
// api url
$service_url = str_replace('{ipaddress}', $ip, $service_url);
// api keys
foreach ($service_keys as $skey_name => $skey_info) {
$service_url = str_replace('{'.$skey_name.'}', $skey_info['value'], $service_url);
}
//var_dump('
', $service, $service_url, '
'); echo __FILE__ . ":" . __LINE__;die . PHP_EOL;
return $service_url;
}
/**
* Parameters:
* $key - The key to be searched for.
* $list - The sorted array.
* $compare_func - A user defined function for comparison. Same definition as the one in usort
* $low - First index of the array to be searched (local parameters).
* $high - Last index of the array to be searched (local parameters).
*
* Return:
* index of the search key if found, otherwise return -(insert_index + 1).
* insert_index is the index of greatest element that is smaller than $key or count($list) if $key
* is larger than all elements in the array.
*
* License: Feel free to use the code if you need it.
*/
private function _binary_search( $key, array $list, $compare_func ) {
$low = 0;
$high = count($list) - 1;
while ($low <= $high) {
$mid = (int) (($high - $low) / 2) + $low; // could use php ceil function
$cmp = call_user_func($compare_func, $list[$mid], $key);
if ($cmp < 0) {
$low = $mid + 1;
}
else if ($cmp > 0) {
$high = $mid - 1;
}
else {
return $mid;
}
}
return -($low - 1);
}
private function _binary_search_cmp( $a, $b ) {
return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
}
} } // end class