├── .gitattributes ├── .gitignore ├── COPYING ├── HISTORY ├── README.txt ├── cidr_optim.txt ├── cities.txt ├── example-geoip.php ├── example.php ├── geoipregionvars.ru.php └── ipgeobase.php /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /HISTORY: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rossvs/ipgeobase.php/f984106477f69dd3e6759c53e544eca8106fd3e4/HISTORY -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | **UPDATE ipgeobase.ru закрыт в 2021 году, репозиторий не актуален!** 2 | 3 | НАЗНАЧЕНИЕ 4 | 5 | Этот скрипт предназначен для использования текстовых баз сервиса гео-локации 6 | ipgeobase.ru на PHP. Ipgeobase.ru предоставляет подробную информацию 7 | по IP-адресу: город, регион, федеральный округ, координаты - по городам России 8 | и Украины. 9 | По этим странам сервис работает точнее MaxMind GeoIP: на тестовой выборке в 10 | 30000 IP Ipgeobase смог определить регион в 98,6% случаев, а GeoIP - только 11 | в 78% (подробнее тут: http://ross.vc/?p=204). 12 | 13 | 14 | ИСПОЛЬЗОВАНИЕ 15 | 16 | 1. Скачайте архив http://ipgeobase.ru/cgi-bin/Archive.cgi 17 | (хорошая идея настроить переодическое скачивание с помощью wget). 18 | 2. Распакуйте cidr_optim.txt и cities.txt. 19 | 2. Подключите ipgeobase.php. 20 | 3. Используйте класс IPGeoBase (см. example.php). 21 | 22 | Скрипт работает в кодировке windows-1251, т.к. в этой кодировке поставляются 23 | файлы cities.txt и cidr_optim.txt. 24 | Если предполагается высокая частота обращений к скрипту, возможно, хорошей 25 | идеей будет разместить файлы cidr_optim.txt и cities.txt на RAM-диске или 26 | вообще отказаться от этой библиотеки и разместить базу в SQL РСУБД. 27 | Если не требуется определение зарубежных стран, можно удалить из базы 28 | диапазоны, не относящиеся к России, например, командой sed: 29 | sed -e '/RU/!d' cidr_optim.txt > cidr_optim_RU.txt 30 | 31 | 32 | ИСПОЛЬЗОВАНИЕ СОВМЕСТНО С MAXMIND GEOIP 33 | 34 | Чтобы получать информацию о городе и регионе по всем странам можно 35 | дополнительно использовать базу GeoLite сервиса MaxMind GeoIP. 36 | 37 | 1. Скачайте базу GeoLite http://dev.maxmind.com/geoip/legacy/geolite/ 38 | 2. Скачайте библитеку PHP http://dev.maxmind.com/geoip/legacy/downloadable/ 39 | 3. Пример функции для одновременной работы Ipgeobase и GeoIP дан 40 | в example-geoip.php. Пример предполагает следующую структуру 41 | папок: 42 | 43 | |_geoip папка API GeoIP 44 | |_cidr_optim.txt база диапазонов IP Ipgeobase 45 | |_cities.txt база городов Ipgeobase 46 | |_example-geoip.php файл примера 47 | |_geoipregionvars.ru.php перевод регионов на русский для GeoIP 48 | |_GeoLiteCity.dat база GeoIP 49 | |_ipgeobase.php класс IPGeoBase 50 | 51 | 52 | КОНТАКТЫ 53 | 54 | Владислав Росс vladislav.ross@gmail.com 55 | -------------------------------------------------------------------------------- /cities.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rossvs/ipgeobase.php/f984106477f69dd3e6759c53e544eca8106fd3e4/cities.txt -------------------------------------------------------------------------------- /example-geoip.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rossvs/ipgeobase.php/f984106477f69dd3e6759c53e544eca8106fd3e4/example-geoip.php -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rossvs/ipgeobase.php/f984106477f69dd3e6759c53e544eca8106fd3e4/example.php -------------------------------------------------------------------------------- /geoipregionvars.ru.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rossvs/ipgeobase.php/f984106477f69dd3e6759c53e544eca8106fd3e4/geoipregionvars.ru.php -------------------------------------------------------------------------------- /ipgeobase.php: -------------------------------------------------------------------------------- 1 | fhandleCIDR = fopen($CIDRFile, 'r') or die("Cannot open $CIDRFile"); 53 | $this->fhandleCities = fopen($CitiesFile, 'r') or die("Cannot open $CitiesFile"); 54 | $this->fSizeCIDR = filesize($CIDRFile); 55 | $this->fsizeCities = filesize($CitiesFile); 56 | } 57 | 58 | /* 59 | * @brief Получение информации о городе по индексу 60 | * @param idx индекс города 61 | * @return массив или false, если не найдено 62 | */ 63 | private function getCityByIdx($idx) 64 | { 65 | rewind($this->fhandleCities); 66 | while(!feof($this->fhandleCities)) 67 | { 68 | $str = fgets($this->fhandleCities); 69 | $arRecord = explode("\t", trim($str)); 70 | if($arRecord[0] == $idx) 71 | { 72 | return array( 'city' => $arRecord[1], 73 | 'region' => $arRecord[2], 74 | 'district' => $arRecord[3], 75 | 'lat' => $arRecord[4], 76 | 'lng' => $arRecord[5]); 77 | } 78 | } 79 | return false; 80 | } 81 | 82 | /* 83 | * @brief Получение гео-информации по IP 84 | * @param ip IPv4-адрес 85 | * @return массив или false, если не найдено 86 | */ 87 | function getRecord($ip) 88 | { 89 | $ip = sprintf('%u', ip2long($ip)); 90 | 91 | rewind($this->fhandleCIDR); 92 | $rad = floor($this->fSizeCIDR / 2); 93 | $pos = $rad; 94 | while(fseek($this->fhandleCIDR, $pos, SEEK_SET) != -1) 95 | { 96 | if($rad) 97 | { 98 | $str = fgets($this->fhandleCIDR); 99 | } 100 | else 101 | { 102 | rewind($this->fhandleCIDR); 103 | } 104 | 105 | $str = fgets($this->fhandleCIDR); 106 | 107 | if(!$str) 108 | { 109 | return false; 110 | } 111 | 112 | $arRecord = explode("\t", trim($str)); 113 | 114 | $rad = floor($rad / 2); 115 | if(!$rad && ($ip < $arRecord[0] || $ip > $arRecord[1])) 116 | { 117 | return false; 118 | } 119 | 120 | if($ip < $arRecord[0]) 121 | { 122 | $pos -= $rad; 123 | } 124 | elseif($ip > $arRecord[1]) 125 | { 126 | $pos += $rad; 127 | } 128 | else 129 | { 130 | $result = array('range' => $arRecord[2], 'cc' => $arRecord[3]); 131 | 132 | if($arRecord[4] != '-' && $cityResult = $this->getCityByIdx($arRecord[4])) 133 | { 134 | $result += $cityResult; 135 | } 136 | 137 | return $result; 138 | } 139 | } 140 | return false; 141 | } 142 | } 143 | 144 | --------------------------------------------------------------------------------