public function run($request)
 {
     $strCSVPath = CONTINENTAL_CONTENT_PATH . '/code/ThirdParty/dbip-city.csv';
     if (!file_exists($strCSVPath)) {
         echo "<p>I cant find the dbip-city.csv file to import any data.<br>\n\t\t\t\tPlease download any database from <a href='https://db-ip.com/db/'>https://db-ip.com/db/</a>.<br>\n\t\t\t\tNOTE: It's adviced to edit the DB to only include the countries you want to handle, it contains 2 million records!!!<br>\n\t\t\t\tOr make a CSV contain these columns<br>\n\t\t\t\t`IPFrom`,`IPTo`,`Country`,`Region`,`City`\n\t\t\t\t</p>";
         //"0.0.0.0","0.255.255.255","US","California","Los Angeles"
     } else {
         if (!isset($_REQUEST['confirm'])) {
             $strLink = Director::baseURL() . 'dev/tasks/ImportDBIPcom?confirm=1';
             echo "<p>CAUTION!!!<br>\n\t\t\t\t\tPlease confirm your action<br>\n\t\t\t\t\t<a href='{$strLink}'>I confirm the action</a><br>\n\t\t\t\t\t<a href='{$strLink}&emptydb=1'>I confirm the action, please empty the DB before you import</a>\n\t\t\t\t\t</p>";
         } else {
             increase_time_limit_to();
             if (isset($_REQUEST['emptydb'])) {
                 DB::query('TRUNCATE `IpToLocation`;');
             }
             $arrFields = array_keys(Config::inst()->get('IpToLocation', 'db'));
             $handle = fopen($strCSVPath, "r");
             if ($handle) {
                 while (($line = fgets($handle)) !== false) {
                     $line = str_replace('","', '___', $line);
                     $line = str_replace('"', '', $line);
                     $arrParts = Convert::raw2sql(explode("___", $line));
                     $arrParts[0] = ContinentalContentUtils::IPAddressToIPNumber($arrParts[0]);
                     $arrParts[1] = ContinentalContentUtils::IPAddressToIPNumber($arrParts[1]);
                     DB::query('INSERT INTO `IpToLocation` (`' . implode('`,`', $arrFields) . '`) VALUES (\'' . implode('\',\'', $arrParts) . '\')');
                 }
                 fclose($handle);
             } else {
                 echo 'Error opening file';
             }
         }
     }
 }
 public function getLocation($ip, $ipNumber)
 {
     $api = Config::inst()->get('DBIP_IpParser', 'api');
     $url = "http://api.db-ip.com/v2/{$api}/{$ip}";
     try {
         $json = file_get_contents($url);
         $data = Convert::json2array($json);
         $location = new ArrayData(array('Country' => $data['countryCode'], 'Region' => $data['stateProv'], 'City' => $data['city'], 'Type' => ContinentalContentUtils::IPType($ip)));
         $this->debugLocation($location);
         return $location;
     } catch (Exception $e) {
     }
 }
 public function run($request)
 {
     $strCSVPath = CONTINENTAL_CONTENT_PATH . '/code/ThirdParty/dbip-city.csv';
     if (!file_exists($strCSVPath)) {
         echo "<p>I cant find the dbip-city.csv file to import any data.<br>\n\t\t\t\tPlease download any database from <a href='https://db-ip.com/db/'>https://db-ip.com/db/</a>.<br>\n\t\t\t\tNOTE: It's adviced to edit the DB to only include the countries you want to handle, it contains 2 million records!!!<br>\n\t\t\t\tOr make a CSV contain these columns<br>\n\t\t\t\t`IPFrom`,`IPTo`,`Country`,`Region`,`City`\n\t\t\t\t</p>";
         //"0.0.0.0","0.255.255.255","US","California","Los Angeles"
     } else {
         if (!isset($_REQUEST['confirm'])) {
             $strLink = Director::baseURL() . 'dev/tasks/ImportDBIPcom?confirm=1';
             echo "<p>CAUTION!!!<br>\n\t\t\t\t\tPlease confirm your action<br>\n\t\t\t\t\t<a href='{$strLink}'>I confirm the action</a><br>\n\t\t\t\t\t<a href='{$strLink}&emptydb=1'>I confirm the action, please empty the DB before you import</a>\n\t\t\t\t\t</p>";
         } else {
             increase_time_limit_to();
             //
             // this needs varbinary fields so create the table here
             //
             $arr = DB::table_list();
             if (!in_array('dbip_lookup', $arr)) {
                 $strSQL = "CREATE TABLE `dbip_lookup` (\n\t\t\t\t\t  `addr_type` enum('ipv4','ipv6') NOT NULL,\n\t\t\t\t\t  `ip_start` varbinary(16) NOT NULL,\n\t\t\t\t\t  `ip_end` varbinary(16) NOT NULL,\n\t\t\t\t\t  `country` char(2) NOT NULL,\n\t\t\t\t\t  `stateprov` varchar(80) NOT NULL,\n\t\t\t\t\t  `city` varchar(80) NOT NULL,\n\t\t\t\t\t  PRIMARY KEY (`ip_start`)\n\t\t\t\t\t);";
                 DB::query($strSQL);
             }
             if (isset($_REQUEST['emptydb'])) {
                 DB::query('TRUNCATE `dbip_lookup`;');
             }
             $conn = DB::get_conn();
             if ($conn->supportsTransactions()) {
                 $conn->transactionStart();
             }
             $handle = fopen($strCSVPath, "r");
             if ($handle) {
                 while (($line = fgets($handle)) !== false) {
                     $line = str_replace('","', '___', $line);
                     $line = str_replace('"', '', $line);
                     $arrParts = Convert::raw2sql(explode("___", $line));
                     $vals = array('addr_type' => "'" . IpToLocation::addr_type($arrParts[0]) . "'", 'ip_start' => "'" . $conn->escapeString(ContinentalContentUtils::IPAddressToIPNumber($arrParts[0])) . "'", 'ip_end' => "'" . $conn->escapeString(ContinentalContentUtils::IPAddressToIPNumber($arrParts[1])) . "'", 'country' => "'" . $arrParts[2] . "'", 'stateprov' => "'" . $arrParts[3] . "'", 'city' => "'" . $arrParts[4] . "'");
                     $fields = array_keys($vals);
                     DB::query('INSERT INTO `dbip_lookup` (`' . implode('`,`', $fields) . '`) VALUES (' . implode(',', $vals) . ')');
                 }
                 fclose($handle);
             } else {
                 echo 'Error opening file';
             }
             if ($conn->supportsTransactions()) {
                 $conn->transactionEnd();
             }
         }
     }
 }
 /**
  * @return int|string
  */
 public static function CurrentContinent()
 {
     if (self::$current_continent) {
         return self::$current_continent;
     }
     self::$current_continent = CONTINENTAL_DEFAULT;
     if (!self::IsViewingThroughProxy() && Session::get('SESSION_MAP_LOCATION')) {
         self::$current_continent = strtolower(trim(Session::get('SESSION_MAP_LOCATION')));
     } else {
         if ($location = ContinentalContentUtils::GetLocation()) {
             foreach (self::GetContinents() as $strContinent => $strCode) {
                 if (strtolower(trim($location->Country)) == strtolower(trim($strContinent)) || strtolower(trim($location->Region)) == strtolower(trim($strContinent)) || strtolower(trim($location->City)) == strtolower(trim($strContinent)) || strtolower(trim($location->Country)) == strtolower(trim($strCode)) || strtolower(trim($location->Region)) == strtolower(trim($strCode)) || strtolower(trim($location->City)) == strtolower(trim($strCode))) {
                     self::$current_continent = $strCode;
                     break;
                 }
             }
         }
     }
     return self::$current_continent;
 }
 /**
  * @return int|string
  */
 public static function CurrentContinent()
 {
     if (self::$current_continent) {
         return self::$current_continent;
     }
     self::$current_continent = CONTINENTAL_DEFAULT;
     if (!self::IsViewingThroughProxy() && Session::get('SESSION_MAP_LOCATION')) {
         self::$current_continent = strtolower(trim(Session::get('SESSION_MAP_LOCATION')));
     } else {
         if ($forwarder = ForwardedIP::get()->filter('IP', ContinentalContentUtils::IPAddress())->first()) {
             $countryToCode = array_flip(self::$country_codes);
             foreach (self::GetContinents() as $strCode => $arrContinents) {
                 foreach ($arrContinents as $strContinent) {
                     $strCountryCode = isset($countryToCode[$strContinent]) ? $countryToCode[$strContinent] : null;
                     if ($strCountryCode == $forwarder->Continent) {
                         self::$current_continent = $strCode;
                         if (isset($_REQUEST['debug_location'])) {
                             echo "<p style='display: block; padding: 10px 40px; background: white; color: black; position: absolute; top: 43px; left: 0; z-index: 999999;'>{$forwarder->Continent}</p>";
                         }
                         break 2;
                     }
                 }
             }
         } else {
             if ($location = ContinentalContentUtils::GetLocation()) {
                 $countryToCode = array_flip(self::$country_codes);
                 foreach (self::GetContinents() as $strCode => $arrContinents) {
                     foreach ($arrContinents as $strContinent) {
                         $strCountryCode = isset($countryToCode[$strContinent]) ? $countryToCode[$strContinent] : null;
                         if (strtolower(trim($location->Country)) == strtolower(trim($strContinent)) || strtolower(trim($location->Region)) == strtolower(trim($strContinent)) || strtolower(trim($location->City)) == strtolower(trim($strContinent)) || strtolower(trim($location->Country)) == strtolower(trim($strCode)) || strtolower(trim($location->Region)) == strtolower(trim($strCode)) || strtolower(trim($location->City)) == strtolower(trim($strCode)) || !is_null($strCountryCode) && strtolower(trim($location->Country)) == strtolower(trim($strCountryCode))) {
                             if (isset($_REQUEST['debug_location'])) {
                                 echo "<p style='display: block; padding: 10px 40px; background: white; color: black; position: absolute; top: 43px; left: 0; z-index: 999999;'>{$strCode}</p>";
                             }
                             self::$current_continent = $strCode;
                             break 2;
                         }
                     }
                 }
             }
         }
     }
     return self::$current_continent;
 }
 public function getLocation($ip, $ipNumber)
 {
     $location = IpToLocation::get()->filter(array('IPFrom:LessThanOrEqual' => $ipNumber, 'IPTo:GreaterThanOrEqual' => $ipNumber, 'Type' => ContinentalContentUtils::IPType($ip) == 'ipv4' ? 'IpV4' : 'IpV6'))->first();
     $this->debugLocation($location);
     return $location;
 }