<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class smpush_geoloc extends smpush_controller{
  private static $ip;
  private static $apiurl;

  public function __construct(){}

  public static function curl($headers=false) {
    if(function_exists('curl_init')){
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, self::$apiurl);
      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.6 (KHTML, like Gecko) Chrome/16.0.897.0 Safari/535.6');
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
      curl_setopt($ch, CURLOPT_TIMEOUT, 4);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      if($headers !== false){
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      }
      else{
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
      }
      if(defined('WP_PROXY_HOST')){
        curl_setopt($ch, CURLOPT_PROXY, WP_PROXY_HOST);
        curl_setopt($ch, CURLOPT_PROXYPORT, WP_PROXY_PORT);
        curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
        if(defined('WP_PROXY_USERNAME')){
          curl_setopt($ch, CURLOPT_PROXYUSERPWD, WP_PROXY_USERNAME.':'.WP_PROXY_PASSWORD);
          curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
        }
      }
      $result = curl_exec($ch);
      curl_close($ch);
    }
    else {
      $result = smpush_helper::readFile(self::$apiurl);
    }
    return json_decode($result);
  }

  public static function db_ip_service(){
    self::$apiurl = 'http://api.db-ip.com/addrinfo?addr='.self::$ip.'&api_key='.self::$apisetting['db_ip_apikey'];
    $info = self::curl();
    if(empty($info->latitude)){
      return false;
    }
    $data['country'] = $info->country;
    $data['city'] = $info->city;
    $data['latitude'] = $info->latitude;
    $data['longitude'] = $info->longitude;
    $data['ip'] = self::$ip;
    return $data;
  }

  public static function telize_service(){
    self::$apiurl = 'https://telize-v1.p.mashape.com/geoip/'.self::$ip;
    $info = self::curl(array('X-Mashape-Key: '.self::$apisetting['db_ip_apikey'], 'Accept: application/json'));
    if(empty($info->latitude)){
      return false;
    }
    $data['country'] = $info->country;
    $data['city'] = $info->city;
    $data['ip'] = self::$ip;
    $data['latitude'] = $info->latitude;
    $data['longitude'] = $info->longitude;
    return $data;
  }

  public static function ip_api_service(){
    self::$apiurl = 'http://ip-api.com/json/'.self::$ip;
    $info = self::curl();
    if(empty($info->lat)){
      return false;
    }
    $data['country'] = $info->country;
    $data['city'] = $info->city;
    $data['ip'] = self::$ip;
    $data['latitude'] = $info->lat;
    $data['longitude'] = $info->lon;
    return $data;
  }

  public static function get_location_info(){
    self::$ip = $_SERVER['REMOTE_ADDR'];
    if(self::$apisetting['auto_geo'] == 0){
      return false;
    }
    if(self::$apisetting['geo_provider'] == 'db-ip.com'){
      if(empty(self::$apisetting['db_ip_apikey'])){
        return false;
      }
      return self::db_ip_service();
    }
    elseif(self::$apisetting['geo_provider'] == 'telize.com'){
      return self::telize_service();
    }
    elseif(self::$apisetting['geo_provider'] == 'ip-api.com'){
      return self::ip_api_service();
    }
    else{
      return false;
    }
  }

}
