Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ip_csv2dat($input_file, $output_file) {
- $lines = file($input_file);
- $fh = fopen($output_file, 'wb+');
- fwrite($fh, 'TKdb');
- fwrite($fh, pack('l', 0));
- $min = NULL;
- $countries = array();
- $cnt = 0;
- $topidx = array();
- $minip = 0;
- $maxip = 0;
- foreach ($lines as $line) {
- $cnt++;
- $line = str_replace('"', '', $line);
- list($start, $end, $two, , ) = explode(',', $line, 4);
- $start = "$start" + 0;
- $end = "$end" + 0;
- if (!in_array($two, $countries)) {
- $countries[] = $two;
- }
- $countryidx = array_search($two, $countries);
- fwrite($fh, pack('VVv', $start, $end, $countryidx));
- list($aclassStart, , , ) = explode('.', long2ip($start));
- list($aclassEnd, , , ) = explode('.', long2ip($end));
- for ($i = $aclassStart; $i <= $aclassEnd; $i++) {
- if (!isset($topidx[$i])) {
- $topidx[$i] = $cnt;
- }
- }
- if (is_null($min)) {
- $min = $start;
- $minip = $aclassStart;
- }
- $max = $end;
- $maxip = $aclassEnd;
- }
- $idx = ftell($fh);
- fwrite($fh, pack('VVVCv', $cnt, $min, $max, strlen(pack('VVv', 'P', 'H', 'P')), count($countries)));
- foreach ($countries as $country) {
- fwrite($fh, $country);
- }
- fwrite($fh, pack('CC', $minip, $maxip));
- $lastpos = 0;
- $pos = 0;
- for ($i = $minip; $i <= $maxip; $i++) {
- if (isset($topidx[$i])) {
- $pos = $topidx[$i];
- $lastpos = $pos;
- } else {
- $pos = -$lastpos;
- }
- fwrite($fh, pack('i', $pos));
- }
- fseek($fh, 4);
- fwrite($fh, pack('l', $idx));
- fclose($fh);
- return array(0 => $cnt, 1 => count($countries));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement