1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ip_tools.php
См. документацию.
1<?
2
3define("IP_DEFAULT_SOURCE_FILENAME", "/bitrix/modules/statistic/ip2country/ip-to-country.csv");
4define("IP_DEFAULT_SOURCE_TYPE", "ip-to-country.com");
5define("IP_DB_FILENAME", "/bitrix/modules/statistic/ip2country/ip-to-country.db");
6define("IP_IDX_FILENAME", "/bitrix/modules/statistic/ip2country/ip-to-country.idx");
7
8function i2c_create_db(
9 &$total_reindex,
10 &$reindex_success,
11 &$step_reindex,
12 &$int_prev,
13 $step=0,
14 $file_name = false,
15 $file_type = false,
16 $file_db = false,
17 $file_idx = false
18 )
19{
20 $step = intval($step);
21
22 if ($file_name===false) $file_name = IP_DEFAULT_SOURCE_FILENAME;
23 if ($file_type===false) $file_type = IP_DEFAULT_SOURCE_TYPE;
24 if ($file_db===false) $file_db = IP_DB_FILENAME;
25 if ($file_idx===false) $file_idx = IP_IDX_FILENAME;
26
27 $start = microtime(true);
28
29 $bExtTotal = is_array($total_reindex);
30 if($bExtTotal)
31 {
32 $my_total_reindex = $total_reindex[0];
33 $fseek = $total_reindex[1];
34 }
35 else
36 {
37 $fseek = $total_reindex;
38 $my_total_reindex = $total_reindex;
39 }
40
41 if ($fp=fopen($_SERVER["DOCUMENT_ROOT"].$file_name,"rb"))
42 {
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);
46
47 $d = 1000000;
48 if ($fseek<=0)
49 {
50 fwrite($f_idx, $d."\n0,0\n");
51 }
52
53 $src_db_lines = 0;
54 $step_reindex = 0;
55
56 if($bExtTotal)
57 fseek($fp, $fseek);
58
59 while ($fp>0 && !feof($fp))
60 {
61 $arr=fgetcsv($fp,1000,",");
62 if (is_array($arr) && $file_type=="maxmind.com" && !isset($beginIpNum))
63 {
64 foreach ($arr as $key => $value)
65 {
66 $value = trim($value);
67 if ($value=="beginIpNum" || $value=="endIpNum" || $value=="countryCode") ${$value} = $key;
68 }
69 }
70 $src_db_lines++;
71 if(!$bExtTotal)
72 {
73 if($fseek > 0 && $src_db_lines <= $fseek)
74 continue;
75 }
76
77 if ($file_type=="maxmind.com")
78 {
79 $ix_beginIpNum = (!isset($beginIpNum)) ? 2 : intval($beginIpNum);
80 $ip_from = trim(trim($arr[$ix_beginIpNum]), "\"");
81
82 $ix_endIpNum = (!isset($endIpNum)) ? 3 : intval($endIpNum);
83 $ip_to = trim(trim($arr[$ix_endIpNum]), "\"");
84
85 $ip_to = (float) $ip_to;
86 if ($ip_to<=0) continue;
87
88 $ix_countryCode = (!isset($countryCode)) ? 4 : intval($countryCode);
89 $country_id = trim(trim($arr[$ix_countryCode]), "\"");
90 }
91 else
92 {
93 $ip_from = trim(trim($arr[0]), "\"");
94 $ip_to = trim(trim($arr[1]), "\"");
95 $country_id = trim(trim($arr[2]), "\"");
96 }
97 if ($country_id == '' && mb_strlen($country_id) != 2) continue;
98
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");
102 $step_reindex++;
103
104 $int = floor($ip_from/$d);
105 if ($int != $int_prev)
106 fwrite($f_idx, $int.",".($my_total_reindex + $step_reindex)."\n");
107 $int_prev = $int;
108
109 if($step > 0 && (microtime(true) - $start) > $step)
110 {
111 $reindex_success = "N";
112 break;
113 }
114 }
115
116 if ($reindex_success!="N")
117 $reindex_success = "Y";
118
119 if($bExtTotal)
120 {
121 $total_reindex[0] += $step_reindex;
122 $total_reindex[1] = ftell($fp);
123 }
124 else
125 {
126 $total_reindex += $step_reindex;
127 }
128
129 fclose($fp);
130 fclose($f_db);
131 fclose($f_idx);
132 }
133}
134
136 $file_name = false,
137 $file_type = false
138 )
139{
140 $DB = CDatabase::GetModuleConnection('statistic');
141
142 if ($file_name===false) $file_name = IP_DEFAULT_SOURCE_FILENAME;
143 if ($file_type===false) $file_type = IP_DEFAULT_SOURCE_TYPE;
144
145 if ($fp=fopen($_SERVER["DOCUMENT_ROOT"].$file_name,"rb"))
146 {
147 set_time_limit(0);
148 ignore_user_abort(true);
149 $arFields = Array(
150 "NAME" => "'NA'",
151 "SHORT_NAME" => "'N00'"
152 );
153 $rows = $DB->Update("b_stat_country",$arFields,"WHERE ID='N0'");
154 if (intval($rows)<=0)
155 {
156 $strSql = "INSERT INTO b_stat_country (ID, SHORT_NAME, NAME) VALUES ('N0','N00','NA')";
157 $DB->Query($strSql);
158 }
159 $arrUpdated = array();
160
161 $bHeader = true;
162 $bMaxMind = $file_type == "maxmind.com";
163 if($bMaxMind)
164 {
165 $ix_countryCode = 5;
166 $ix_countryName = 6;
167 }
168 else
169 {
170 $ix_countryCode = 2;
171 $ix_countryName = 4;
172 }
173
174 while (!feof($fp))
175 {
176 $arr = fgetcsv($fp, 4096);
177 if(is_array($arr) && (count($arr) > 0))
178 {
179 if($bMaxMind && $bHeader)
180 {
181 foreach($arr as $key => $value)
182 {
183 $value = trim($value);
184 if ($value == "countryCode")
185 {
186 $bHeader = false;
187 $ix_countryCode = intval($key);
188 }
189 elseif($value == "countryName")
190 {
191 $bHeader = false;
192 $ix_countryName = intval($key);
193 }
194 }
195 }
196
197 //Check if was hanled with "dirty" $country_id
198 if(array_key_exists($arr[$ix_countryCode], $arrUpdated))
199 continue;
200
201 $country_id = trim($arr[$ix_countryCode], "\"");
202
203 if(!array_key_exists($country_id, $arrUpdated))
204 {
205
206 $country_name = trim($arr[$ix_countryName], "\"");
207 if($bMaxMind)
208 {
209 if(mb_strlen($country_id) != 2)
210 continue;
211 $country_short_name = "";
212 }
213 else
214 {
215 $country_short_name = trim($arr[3], "\"");
216 }
217
218 $arFields["NAME"] = "'".$DB->ForSql($country_name, 50)."'";
219 $arFields["SHORT_NAME"] = "'".$DB->ForSql($country_short_name, 3)."'";
220
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 <> '')
223 {
224 $strSql = "
225 INSERT INTO b_stat_country (ID, SHORT_NAME, NAME) VALUES (
226 '".$DB->ForSql($country_id, 2)."',
227 ".$arFields["SHORT_NAME"].",
228 ".$arFields["NAME"]."
229 )";
230 $DB->Query($strSql);
231 }
232 $arrUpdated[$country_id] = true;
233 }
234 }
235 }
236 fclose($fp);
237 }
238}
239
240function ip2number($dotted)
241{
242 return sprintf("%u", ip2long($dotted));
243}
244
245function ip2address($ip_number)
246{
247 $ip_number = (float) $ip_number;
248 return long2ip($ip_number);
249}
250
251function i2c_get_country($ip=false)
252{
253 if($ip === false)
254 $ip = $_SERVER['REMOTE_ADDR'];
255
256 $ipn = (float) sprintf("%u", ip2long($ip));
257 $idx = i2c_search_in_index($ipn);
258
259 if($idx !== false)
260 $country = i2c_search_in_db($ipn, $idx);
261 else
262 $country = 'N0';
263
264 return $country;
265}
266
267function i2c_search_in_index($ip, $idx_name = IP_IDX_FILENAME)
268{
269 if(file_exists($_SERVER['DOCUMENT_ROOT'].$idx_name))
270 {
271 $dbidx = fopen($_SERVER['DOCUMENT_ROOT'].$idx_name,"rb");
272 if($dbidx)
273 {
274 $granularity = intval(fgets($dbidx, 64));
275 $ip_chunk = intval($ip / $granularity);
276 $idxpart = 0;
277 $recnum = 0;
278 $prev_recnum = 0;
279
280 while(!feof($dbidx))
281 {
282 $data = fgetcsv($dbidx, 100);
283 if(is_array($data) && count($data))
284 {
285 if($ip_chunk >= $idxpart && $ip_chunk < intval($data[0]))
286 {
287 return array(
288 $prev_recnum > 0? $prev_recnum: $recnum,
289 intval($data[1])
290 );
291 }
292
293 $prev_recnum = $recnum;
294 $idxpart = intval($data[0]);
295 $recnum = intval($data[1]);
296 }
297 }
298
299 return array(
300 $prev_recnum > 0? $prev_recnum : $recnum,
301 -1
302 );
303 }
304 }
305 return false;
306}
307
308function i2c_search_in_db($ip, $idx, $db_name=IP_DB_FILENAME)
309{
310 $range_start = 0;
311 $range_end = 0;
312 $country = "N0";
313 $ipdb = fopen($_SERVER['DOCUMENT_ROOT'].$db_name,"rb");
314
315 if (!$ipdb)
316 return $country;
317
318 $i1_size = 10;
319 $i2_size = 10;
320 $cn_size = 2;
321 $record_size = $i1_size + $i2_size + $cn_size + 1;
322 $seek = ($idx[0]*$record_size)-$record_size;
323 fseek($ipdb, $seek);
324 $ip = (float) $ip;
325 while (!feof($ipdb) && !($range_start <= $ip && $range_end >= $ip))
326 {
327 if ($idx[1] != -1 && $idx[0] > $idx[1])
328 {
329 $country = "N0";
330 break;
331 }
332 $record = fread($ipdb,$record_size);
333 if (mb_strlen($record) != $record_size)
334 {
335 $country = "N0";
336 break;
337 }
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);
341 $idx[0] += 1;
342 }
343 fclose($ipdb);
344 return $country;
345}
346
347function get_realip()
348{
349 $result = false;
350 if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
351 {
352 $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
353 foreach ($ips as $ip)
354 {
355 $ip = trim($ip);
356 if ($ip && !preg_match('/^(10|172\\.16|192\\.168)\\./', $ip))
357 {
358 $result = $ip;
359 break;
360 }
361 }
362 }
363
364 return ($result ?: $_SERVER['REMOTE_ADDR']);
365}
$arFields
Определения dblapprove.php:5
$data['IS_AVAILABLE']
Определения .description.php:13
$arr
Определения file_new.php:624
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$start
Определения get_search.php:9
ip2address($ip_number)
Определения ip_tools.php:245
i2c_load_countries( $file_name=false, $file_type=false)
Определения ip_tools.php:135
ip2number($dotted)
Определения ip_tools.php:240
i2c_get_country($ip=false)
Определения ip_tools.php:251
const IP_DEFAULT_SOURCE_FILENAME
Определения ip_tools.php:3
get_realip()
Определения ip_tools.php:347
i2c_create_db(&$total_reindex, &$reindex_success, &$step_reindex, &$int_prev, $step=0, $file_name=false, $file_type=false, $file_db=false, $file_idx=false)
Определения ip_tools.php:8
const IP_DEFAULT_SOURCE_TYPE
Определения ip_tools.php:4
i2c_search_in_index($ip, $idx_name=IP_IDX_FILENAME)
Определения ip_tools.php:267
const IP_IDX_FILENAME
Определения ip_tools.php:6
const IP_DB_FILENAME
Определения ip_tools.php:5
i2c_search_in_db($ip, $idx, $db_name=IP_DB_FILENAME)
Определения ip_tools.php:308
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
global $DB
Определения cron_frame.php:29
$country
Определения payment.php:59
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$rows
Определения options.php:264