Skip to content

Commit 10c90df

Browse files
author
zhao.binyan
committed
新增数据库文件的更新逻辑
1 parent 24c203c commit 10c90df

3 files changed

Lines changed: 98 additions & 2 deletions

File tree

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
## 数据库文件更新日期
1414

15-
2018年3月25日更新,数据库为2018年3月20日版本
15+
2019年7月25日更新,数据库为2018年7月20日版本
1616

1717
## 使用说明
1818

@@ -65,7 +65,17 @@ echo json_encode(IpLocation::getLocation($ip), JSON_UNESCAPED_UNICODE) . "\n";
6565
{"ip":"60.195.153.98","country":"中国","province":"北京","city":"顺义区","county":"","isp":"","area":"中国北京顺义区后沙峪金龙网吧"}
6666
```
6767

68-
## 自己手动更新数据库
68+
## 更新数据库
69+
70+
### 在线直接更新
71+
72+
更新到源码目录
73+
`php ~/bin/update-ip.php`
74+
75+
更新到指定目录
76+
`php ~/bin/update-ip.php -d=/tmp`
77+
78+
### 【或者】自己手动更新数据库
6979

7080
1,http://www.cz88.net/fox/ipdat.shtml
7181
下载数据库程序(Windows 环境),执行完毕后,即可在程序安装目录找到数据库文件 qqwry.dat
@@ -84,6 +94,8 @@ echo json_encode(IpLocation::getLocation($ip), JSON_UNESCAPED_UNICODE) . "\n";
8494
```
8595
IP 地理位置查询类
8696
97+
2019-07-25 赵彬言 1,增加自动更新功能,参考 https://blog.shuax.com/archives/QQWryUpdate.html 感谢 https://github.com/itbdw/ip-database/issues/10
98+
8799
2017-09-12 赵彬言 1,缩减返回数据,去掉字段 remark smallarea beginip endip
88100
2,将调用改为单例模式,保证只读取一次文件
89101
3,修复 bug,直接将返回 gbk 编码内容转为 utf-8,移除编码隐患

bin/update-ip.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: zhao.binyan
5+
* Date: 2019/7/25
6+
* Time: 下午2:24
7+
*/
8+
9+
/*
10+
纯真数据库自动更新原理实现
11+
www.shuax.com 2014.03.27
12+
*/
13+
14+
/**
15+
* 示例
16+
* `php ~/bin/update-ip.php`
17+
*
18+
* 更新到指定目录
19+
* `php ~/bin/update-ip.php -d=/tmp`
20+
*/
21+
22+
date_default_timezone_set("PRC");
23+
24+
//可设置为服务器特定目录,单独,避免组件升级互相影响
25+
$dir = dirname(__DIR__) . "/src";
26+
$option = getopt("d::");
27+
if (isset($option['d'])) {
28+
if (!is_readable($option['d'])) {
29+
die("bad param, dir not readable " . $option['d']);
30+
}
31+
$dir = $option['d'];
32+
}
33+
34+
$stime = microtime(true);
35+
36+
echo "开始准备更新数据库" . date("Y-m-d H:i:s");
37+
echo "\n";
38+
39+
$copywrite = file_get_contents("http://update.cz88.net/ip/copywrite.rar");
40+
41+
if (!$copywrite) {
42+
$download_spend = $qqwry_time - $stime;
43+
die("copywrite.rar 下载失败 " . sprintf("下载耗时%s", $download_spend));
44+
}
45+
46+
$qqwry = file_get_contents("http://update.cz88.net/ip/qqwry.rar");
47+
$qqwry_time = microtime(true);
48+
49+
if (!$qqwry) {
50+
$download_spend = $qqwry_time - $stime;
51+
die("qqwry.rar 下载失败 " . sprintf("下载耗时%s", $download_spend));
52+
}
53+
54+
$key = unpack("V6", $copywrite)[6];
55+
for ($i = 0; $i < 0x200; $i++) {
56+
$key *= 0x805;
57+
$key++;
58+
$key = $key & 0xFF;
59+
$qqwry[$i] = chr(ord($qqwry[$i]) ^ $key);
60+
}
61+
$qqwry = gzuncompress($qqwry);
62+
$unzip_time = microtime(true);
63+
64+
$download_spend = $qqwry_time - $stime;
65+
$unzip_spend = $unzip_time - $qqwry_time;
66+
67+
if (!$qqwry) {
68+
die("gzip 解压缩失败 " . sprintf("下载耗时%s,解压耗时%s", $download_spend, $unzip_spend));
69+
}
70+
71+
$tmp_file = $dir . '/' . 'qqwry.dat.bak';
72+
$online_file = $dir . '/' . 'qqwry.dat';
73+
74+
if (file_put_contents($tmp_file, $qqwry)) {
75+
$put_time = microtime(true);
76+
$put_spend = $put_time - $unzip_time;
77+
copy($online_file, $online_file.'.online.bak');
78+
copy($tmp_file, $online_file);
79+
80+
$copy_spend = microtime(true) - $put_time;
81+
die("更新成功 " . sprintf("下载耗时%s,解压耗时%s,写入耗时%s,复制耗时%s", $download_spend, $unzip_spend, $put_spend, $copy_spend));
82+
} else {
83+
die("更新失败 " . sprintf("下载耗时%s,解压耗时%s", $download_spend, $unzip_spend));
84+
}

src/qqwry.dat

100755100644
1.01 MB
Binary file not shown.

0 commit comments

Comments
 (0)