public function get_location($ip, $args = array())
 {
     if (!extension_loaded('bcmath')) {
         require_once 'bcmath.php';
     }
     if (!class_exists('IP2Location')) {
         require_once 'IP2Location.php';
     }
     // setup database file and function
     if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
         $file = apply_filters(IP_Geo_Block::PLUGIN_SLUG . 'ip2location-path', $this->get_db_dir() . IP_GEO_BLOCK_IP2LOC_IPV4_DAT);
         $type = IP_GEO_BLOCK_API_TYPE_IPV4;
     } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
         $file = $this->get_db_dir() . IP_GEO_BLOCK_IP2LOC_IPV6_DAT;
         $type = IP_GEO_BLOCK_API_TYPE_IPV6;
     } else {
         return array('errorMessage' => 'illegal format');
     }
     try {
         $geo = new IP2Location($file);
         if ($geo && $geo->get_database_type() & $type) {
             $res = array();
             $data = $geo->lookup($ip);
             foreach ($this->transform_table as $key => $val) {
                 if (isset($data->{$val}) && IP2Location::FIELD_NOT_SUPPORTED !== $data->{$val}) {
                     $res[$key] = $data->{$val};
                 }
             }
             if (isset($res['countryCode']) && strlen($res['countryCode']) === 2) {
                 return $res;
             }
         }
     } catch (Exception $e) {
         return array('errorMessage' => $e->getMessage());
     }
     return array('errorMessage' => 'Not supported');
 }
Ejemplo n.º 2
0
 private function checkLocation($id)
 {
     $model = Video::model()->findByPk($id);
     $ip = $_SERVER['REMOTE_ADDR'];
     $loc = new IP2Location(Yii::getPathOfAlias('webroot') . '/IP2LOCATION-LITE-DB11.BIN');
     $record = $loc->lookup($ip, IP2Location::ALL);
     // echo 'Latitude: ' . $record->latitude . '<br />';
     //echo 'Longitude: ' . $record->longitude . '<br />';
     // print_r($record);
     //die();
     //$request_url = 'http://freegeoip.net/json/' . $ip;
     // $curl_connection = curl_init($request_url);
     // curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
     // //curl_setopt($curl_connection, CURLOPT_USERAGENT,
     // //    'Googlebot/2.1 (+http://www.google.com/bot.html)'
     // //);
     // curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
     // curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
     // $response = curl_exec($curl_connection);
     // curl_close($curl_connection);
     // $coords = json_decode($response);
     //$ip_addr = $_SERVER['REMOTE_ADDR'];
     // $geoplugin = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip_addr) );
     // if ( is_numeric($geoplugin['geoplugin_latitude']) && is_numeric($geoplugin['geoplugin_longitude']) ) {
     // $lat = $geoplugin['geoplugin_latitude'];
     // $long = $geoplugin['geoplugin_longitude'];
     // }
     // echo $ip_addr.';'.$lat.';'.$long;
     $return_value = false;
     if (!empty($model->geolocations)) {
         if ($model->pal_all_location == 0) {
             foreach ($model->geolocations as $geo) {
                 $distance_between = $this->vincentyGreatCircleDistance($record->latitude, $record->longitude, $geo->latitude, $geo->longitude);
                 if ($distance_between <= $geo->distance) {
                     $return_value = true;
                 }
             }
         } else {
             foreach ($model->geolocations as $geo) {
                 $distance_between = $this->vincentyGreatCircleDistance($record->latitude, $record->longitude, $geo->latitude, $geo->longitude);
                 if ($distance_between > $geo->distance) {
                     $return_value = true;
                 }
             }
         }
     } else {
         $return_value = true;
     }
     return $return_value;
 }