20 $step = intval($step);
29 $bExtTotal = is_array($total_reindex);
32 $my_total_reindex = $total_reindex[0];
33 $fseek = $total_reindex[1];
37 $fseek = $total_reindex;
38 $my_total_reindex = $total_reindex;
41 if ($fp=fopen(
$_SERVER[
"DOCUMENT_ROOT"].$file_name,
"rb"))
43 if ($fseek<=0) $mode =
"wb";
else $mode =
"ab";
44 $f_db = fopen(
$_SERVER[
"DOCUMENT_ROOT"].$file_db,$mode);
45 $f_idx = fopen(
$_SERVER[
"DOCUMENT_ROOT"].$file_idx,$mode);
50 fwrite($f_idx, $d.
"\n0,0\n");
59 while ($fp>0 && !feof($fp))
61 $arr=fgetcsv($fp,1000,
",");
62 if (is_array(
$arr) && $file_type==
"maxmind.com" && !isset($beginIpNum))
66 $value = trim($value);
67 if ($value==
"beginIpNum" || $value==
"endIpNum" || $value==
"countryCode") ${$value} =
$key;
73 if($fseek > 0 && $src_db_lines <= $fseek)
77 if ($file_type==
"maxmind.com")
79 $ix_beginIpNum = (!isset($beginIpNum)) ? 2 : intval($beginIpNum);
80 $ip_from = trim(trim(
$arr[$ix_beginIpNum]),
"\"");
82 $ix_endIpNum = (!isset($endIpNum)) ? 3 : intval($endIpNum);
83 $ip_to = trim(trim(
$arr[$ix_endIpNum]),
"\"");
85 $ip_to = (float) $ip_to;
86 if ($ip_to<=0)
continue;
88 $ix_countryCode = (!isset($countryCode)) ? 4 : intval($countryCode);
89 $country_id = trim(trim(
$arr[$ix_countryCode]),
"\"");
93 $ip_from = trim(trim(
$arr[0]),
"\"");
94 $ip_to = trim(trim(
$arr[1]),
"\"");
95 $country_id = trim(trim(
$arr[2]),
"\"");
97 if ($country_id ==
'' && mb_strlen($country_id) != 2)
continue;
99 $ip_from_p = str_pad($ip_from, 10,
"0", STR_PAD_LEFT);
100 $ip_to_p = str_pad($ip_to, 10,
"0", STR_PAD_LEFT);
101 fwrite($f_db, $ip_from_p.$ip_to_p.$country_id.
"\n");
104 $int = floor($ip_from/$d);
105 if ($int != $int_prev)
106 fwrite($f_idx, $int.
",".($my_total_reindex + $step_reindex).
"\n");
109 if($step > 0 && (microtime(
true) -
$start) > $step)
111 $reindex_success =
"N";
116 if ($reindex_success!=
"N")
117 $reindex_success =
"Y";
121 $total_reindex[0] += $step_reindex;
122 $total_reindex[1] = ftell($fp);
126 $total_reindex += $step_reindex;
140 $DB = CDatabase::GetModuleConnection(
'statistic');
145 if ($fp=fopen(
$_SERVER[
"DOCUMENT_ROOT"].$file_name,
"rb"))
148 ignore_user_abort(
true);
151 "SHORT_NAME" =>
"'N00'"
154 if (intval(
$rows)<=0)
156 $strSql =
"INSERT INTO b_stat_country (ID, SHORT_NAME, NAME) VALUES ('N0','N00','NA')";
159 $arrUpdated =
array();
162 $bMaxMind = $file_type ==
"maxmind.com";
176 $arr = fgetcsv($fp, 4096);
179 if($bMaxMind && $bHeader)
183 $value = trim($value);
184 if ($value ==
"countryCode")
187 $ix_countryCode = intval(
$key);
189 elseif($value ==
"countryName")
192 $ix_countryName = intval(
$key);
198 if(array_key_exists(
$arr[$ix_countryCode], $arrUpdated))
201 $country_id = trim(
$arr[$ix_countryCode],
"\"");
203 if(!array_key_exists($country_id, $arrUpdated))
206 $country_name = trim(
$arr[$ix_countryName],
"\"");
209 if(mb_strlen($country_id) != 2)
211 $country_short_name =
"";
215 $country_short_name = trim(
$arr[3],
"\"");
218 $arFields[
"NAME"] =
"'".$DB->ForSql($country_name, 50).
"'";
219 $arFields[
"SHORT_NAME"] =
"'".$DB->ForSql($country_short_name, 3).
"'";
221 $rows =
$DB->Update(
"b_stat_country",
$arFields,
"WHERE ID='".
$DB->ForSql(mb_strtoupper($country_id), 2).
"'");
222 if(intval(
$rows)<=0 && $country_id <>
'' && $country_name <>
'')
225 INSERT INTO b_stat_country (ID, SHORT_NAME, NAME) VALUES (
226 '".$DB->ForSql($country_id, 2).
"',
232 $arrUpdated[$country_id] =
true;
269 if(file_exists(
$_SERVER[
'DOCUMENT_ROOT'].$idx_name))
271 $dbidx = fopen(
$_SERVER[
'DOCUMENT_ROOT'].$idx_name,
"rb");
274 $granularity = intval(fgets($dbidx, 64));
275 $ip_chunk = intval($ip / $granularity);
282 $data = fgetcsv($dbidx, 100);
285 if($ip_chunk >= $idxpart && $ip_chunk < intval(
$data[0]))
288 $prev_recnum > 0? $prev_recnum: $recnum,
293 $prev_recnum = $recnum;
294 $idxpart = intval(
$data[0]);
295 $recnum = intval(
$data[1]);
300 $prev_recnum > 0? $prev_recnum : $recnum,
313 $ipdb = fopen(
$_SERVER[
'DOCUMENT_ROOT'].$db_name,
"rb");
321 $record_size = $i1_size + $i2_size + $cn_size + 1;
322 $seek = ($idx[0]*$record_size)-$record_size;
325 while (!feof($ipdb) && !($range_start <= $ip && $range_end >= $ip))
327 if ($idx[1] != -1 && $idx[0] > $idx[1])
332 $record = fread($ipdb,$record_size);
333 if (mb_strlen($record) != $record_size)
338 $range_start = (float)mb_substr($record, 0, $i1_size);
339 $range_end = (float)mb_substr($record, $i1_size, $i2_size);
340 $country = mb_substr($record, $i1_size + $i2_size, $cn_size);
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)