Beispiel #1
0
 /**
  * Load region code table
  *
  *
  * @link http://www.ip2location.com/free/iso3166-2
  * @throws Mzax_GeoIp_Exception
  * @return array
  */
 public static function tz()
 {
     $handle = self::_openDataFile('timezones.csv');
     $data = array();
     $backup = array();
     while (($row = fgetcsv($handle, 1000, ",")) !== false) {
         if (count($row) != 4) {
             continue;
         }
         list($countryCode, $regionCode, $city, $timeZone) = $row;
         if (!$regionCode || !$countryCode) {
             continue;
         }
         if (!isset($backup[$countryCode . '/' . $regionCode])) {
             $backup[$countryCode . '/' . $regionCode] = array();
         }
         $backup[$countryCode . '/' . $regionCode][] = $row;
         if (!isset($data[$countryCode])) {
             $data[$countryCode] = array('time_zone' => $timeZone, 'regions' => array());
         } else {
             if ($data[$countryCode]['time_zone'] == $timeZone) {
                 continue;
             }
         }
         if (!isset($data[$countryCode]['regions'][$regionCode])) {
             $data[$countryCode]['regions'][$regionCode] = array('time_zone' => $timeZone, 'cities' => array());
         } else {
             if ($data[$countryCode]['regions'][$regionCode]['time_zone'] == $timeZone) {
                 continue;
             }
         }
         $data[$countryCode]['regions'][$regionCode]['cities'][] = $row;
     }
     fclose($handle);
     ksort($data);
     header("Content-Type: text/plain; charset=utf-8");
     foreach ($data as $countryCode => $country) {
         if (!$countryCode) {
             continue;
         }
         $buffer = array();
         $regions = $country['regions'];
         ksort($regions);
         $single = count($regions) === 1;
         $ast = count($regions);
         foreach ($regions as $regionCode => $region) {
             $cities = $region['cities'];
             if (count($cities) > 1) {
                 if (isset($backup[$countryCode . '/' . $regionCode])) {
                     $cities = $backup[$countryCode . '/' . $regionCode];
                 }
             }
             $tz = reset($cities);
             $tz = $tz[3];
             $same = true;
             foreach ($cities as $row) {
                 if ($row[3] != $tz) {
                     $same = false;
                     break;
                 }
             }
             if ($same) {
                 $cities = array(reset($cities));
             }
             $defaultTz = null;
             // $single = $single && count($region['cities']) === 1;
             if (true) {
                 self::$tzCount = array();
                 foreach ($cities as $row) {
                     if (!isset(self::$tzCount[$row[3]])) {
                         self::$tzCount[$row[3]] = 0;
                     }
                     self::$tzCount[$row[3]] = self::$tzCount[$row[3]] + 1;
                 }
                 // print_r(self::$tzCount);
                 arsort(self::$tzCount);
                 $defaultTz = array_keys(self::$tzCount);
                 $defaultTz = $defaultTz[0];
                 //print_r(self::$tzCount);
                 if (self::$tzCount[$defaultTz] <= 1) {
                     $defaultTz = null;
                 } else {
                     $buffer[] = "{$countryCode},{$regionCode},,\"{$defaultTz}\"\n";
                 }
                 usort($cities, array('Mzax_GeoIp_Region', 'sort'));
             }
             $finalCities = array();
             foreach ($cities as $row) {
                 list($countryCode, $regionCode, $city, $timeZone) = $row;
                 if (!$timeZone) {
                     continue;
                 }
                 if ($timeZone === $defaultTz) {
                     continue;
                 }
                 $finalCities[] = $row;
             }
             $cities = $finalCities;
             $singleA = $single && count($cities) === 1;
             foreach ($cities as $row) {
                 list($countryCode, $regionCode, $city, $timeZone) = $row;
                 /*
                                     if(!$timeZone) {
                                         continue;
                                     }
                                     
                                     if($timeZone === $defaultTz) {
                                         continue;
                                     }*/
                 if ($city) {
                     $city = trim($city);
                     $city = "\"{$city}\"";
                 }
                 if ($singleA) {
                     $city = null;
                     $regionCode = null;
                 } else {
                     if (!$defaultTz && count($cities) == 1) {
                         $city = null;
                     }
                 }
                 $timeZone = "\"{$timeZone}\"";
                 $buffer[] = "{$countryCode},{$regionCode},{$city},{$timeZone}\n";
                 /*
                 if($countryCode == 'AU') {
                     var_dump($regions);
                 }
                 */
             }
         }
         if (count($buffer) >= 1) {
             $first = preg_replace('/([A-Z]{2}),.*?,.*?,(.*?)/', '$1,,,$2', $buffer[0]);
             if ($buffer[0] != $first) {
                 echo $first;
             }
         }
         echo implode("", $buffer);
     }
     //var_dump($data);
     exit;
     return $data;
 }
Beispiel #2
0
 /**
  * Refine data
  * 
  * Not all data can be retrieved but some data can be guessed or assumed.
  * Try to fill in the blanks with the data that we already have.
  * 
  * @return Mzax_GeoIp_Request
  */
 public function refine()
 {
     // try to get region id from string
     if (!$this->regionId && $this->region && $this->countryId) {
         $this->regionId = Mzax_GeoIp_Region::getRegionCode($this->countryId, $this->region);
     }
     if (!$this->regionId && $this->countryId && preg_match('/^[A-Z0-9]{2,3}$/', $this->region)) {
         $this->regionId = $this->region;
         $this->region = Mzax_GeoIp_Region::getRegionCode($this->countryId, $this->regionId);
     }
     if (!$this->timeZone) {
         // try to get a time zone from the location, not perfect but will give us a rough idea
         $this->timeZone = Mzax_GeoIp_Region::getTimeZone($this->countryId, $this->regionId, $this->city);
     }
     if ($this->timeZone && $this->timeOffset === null) {
         try {
             $time = new DateTime("now", new DateTimeZone($this->timeZone));
             $this->timeOffset = $time->getOffset() / -60;
         } catch (Exception $e) {
         }
     }
     return $this;
 }