fix: database use plain ip address for ipv6 compatibility

This commit is contained in:
Emil Zhai 2022-12-02 08:43:13 +00:00
parent b45bb0d7d5
commit 45c1d9fd1c
No known key found for this signature in database
GPG Key ID: 780B385DB72F1EBD
6 changed files with 49 additions and 49 deletions

View File

@ -68,10 +68,10 @@ class Access_Core
$filter = $this->request->get('filter', 'all');
$pagenum = $this->request->get('page', 1);
$offset = (max(intval($pagenum), 1) - 1) * $this->config->pageSize;
$query = $this->db->select()->from('table.access_log')
$query = $this->db->select()->from('table.access_logs')
->order('time', Typecho_Db::SORT_DESC)
->offset($offset)->limit($this->config->pageSize);
$qcount = $this->db->select('count(1) AS count')->from('table.access_log');
$qcount = $this->db->select('count(1) AS count')->from('table.access_logs');
switch ($type) {
case 1:
$query->where('robot = ?', 0);
@ -87,7 +87,6 @@ class Access_Core
switch ($filter) {
case 'ip':
$ip = $this->request->get('ip', '');
$ip = bindec(decbin(ip2long($ip)));
$query->where('ip = ?', $ip);
$qcount->where('ip = ?', $ip);
break;
@ -128,7 +127,7 @@ class Access_Core
$this->logs['list'] = $this->htmlEncode($this->urlDecode($list));
$this->logs['rows'] = $this->db->fetchAll($qcount)[0]['count'];
$filter = $this->request->get('filter', 'all');
$filterOptions = $this->request->get($filter);
@ -145,10 +144,10 @@ class Access_Core
$this->logs['page'] = $page->show();
$this->logs['cidList'] = $this->db->fetchAll($this->db->select('DISTINCT content_id as cid, COUNT(1) as count, table.contents.title')
->from('table.access_log')
->join('table.contents', 'table.access_log.content_id = table.contents.cid')
->where('table.access_log.content_id <> ?', null)
->group('table.access_log.content_id')
->from('table.access_logs')
->join('table.contents', 'table.access_logs.content_id = table.contents.cid')
->where('table.access_logs.content_id <> ?', null)
->group('table.access_logs.content_id')
->order('count', Typecho_Db::SORT_DESC));
}
@ -232,7 +231,7 @@ class Access_Core
public function deleteLogs($ids)
{
foreach ($ids as $id) {
$this->db->query($this->db->delete('table.access_log')
$this->db->query($this->db->delete('table.access_logs')
->where('id = ?', $id)
);
}
@ -288,11 +287,8 @@ class Access_Core
} catch(Excpetion $e) {
$ip_country = $ip_province = $ip_city = '未知';
}
# ip转int
$ip = bindec(decbin(ip2long($ip)));
} else {
$ip = 0;
$ip = '';
}
$entrypoint = $this->getEntryPoint();
@ -337,7 +333,7 @@ class Access_Core
);
try {
$this->db->query($this->db->insert('table.access_log')->rows($rows));
$this->db->query($this->db->insert('table.access_logs')->rows($rows));
} catch (Exception $e) {} catch (Typecho_Db_Query_Exception $e) {}
}
@ -351,7 +347,7 @@ class Access_Core
public static function rewriteLogs()
{
$db = Typecho_Db::get();
$rows = $db->fetchAll($db->select()->from('table.access_log'));
$rows = $db->fetchAll($db->select()->from('table.access_logs'));
foreach ($rows as $row) {
$ua = new Access_UA($row['ua']);
$row['browser_id'] = $ua->getBrowserID();
@ -362,7 +358,7 @@ class Access_Core
$row['robot_id'] = $ua->getRobotID();
$row['robot_version'] = $ua->getRobotVersion();
try {
$db->query($db->update('table.access_log')->rows($row)->where('id = ?', $row['id']));
$db->query($db->update('table.access_logs')->rows($row)->where('id = ?', $row['id']));
} catch (Typecho_Db_Exception $e) {
throw new Typecho_Plugin_Exception(_t('刷新数据库失败:%s。', $e->getMessage()));
}
@ -395,7 +391,7 @@ class Access_Core
'meta_id' => $meta_id,
);
}
public function long2ip($long) {
if ($long < 0 || $long > 4294967295) return false;
$ip = "";

View File

@ -53,7 +53,7 @@ class Access_Statistic {
# ip数
$subQuery = $this->db
->select('DISTINCT ip')
->from('table.access_log');
->from('table.access_logs');
if ($endTime > 0) {
$subQuery->where("time >= ? AND time <= ?", $startTime, $endTime);
}
@ -67,7 +67,7 @@ class Access_Statistic {
# 访客数
$subQuery = $this->db
->select('DISTINCT ip, ua')
->from('table.access_log');
->from('table.access_logs');
if ($endTime > 0) {
$subQuery->where("time >= ? AND time <= ?", $startTime, $endTime);
}
@ -81,7 +81,7 @@ class Access_Statistic {
# 浏览数
$subQuery = $this->db
->select('COUNT(1) AS cnt')
->from('table.access_log');
->from('table.access_logs');
if ($endTime > 0) {
$subQuery->where("time >= ? AND time <= ?", $startTime, $endTime);
}
@ -104,7 +104,7 @@ class Access_Statistic {
$this->db->fetchAll(
$this->db
->select('content_id AS cid, table.contents.title AS title, COUNT(1) AS cnt')
->from('table.access_log')
->from('table.access_logs')
->join('table.contents', 'content_id = table.contents.cid', Typecho_Db::INNER_JOIN)
->where('IFNULL(content_id, 0)')
->group('content_id')
@ -138,7 +138,7 @@ class Access_Statistic {
$fetchData = $this->db->fetchAll(
$this->db
->select("IF(ip_province = '中国', '国内未明确', ip_province) AS area, COUNT(1) AS cnt")
->from('table.access_log')
->from('table.access_logs')
->where("ip_country = '中国'")
->group('area')
->order('cnt', Typecho_Db::SORT_DESC)
@ -150,7 +150,7 @@ class Access_Statistic {
$fetchData = $this->db->fetchAll(
$this->db
->select("ip_country AS area, COUNT(1) AS cnt")
->from('table.access_log')
->from('table.access_logs')
->group('area')
->order('cnt', Typecho_Db::SORT_DESC)
->limit(15)
@ -217,7 +217,7 @@ class Access_Statistic {
# ip数
$subQuery = $this->db
->select('DISTINCT ip')
->from('table.access_log')
->from('table.access_logs')
->where('time >= ? AND time <= ?', $startTime, $endTime);
if(method_exists($subQuery, 'prepare'))
$subQuery = $subQuery->prepare($subQuery);
@ -229,7 +229,7 @@ class Access_Statistic {
# 访客数
$subQuery = $this->db
->select('DISTINCT ip,ua')
->from('table.access_log')
->from('table.access_logs')
->where('time >= ? AND time <= ?', $startTime, $endTime);
if(method_exists($subQuery, 'prepare'))
$subQuery = $subQuery->prepare($subQuery);
@ -242,7 +242,7 @@ class Access_Statistic {
$chart['pv'] = intval($this->db->fetchRow(
$this->db
->select('COUNT(1) AS count')
->from('table.access_log')
->from('table.access_logs')
->where('time >= ? AND time <= ?', $startTime, $endTime)
)['count']);
$resp['chart'][] = $chart;
@ -282,7 +282,7 @@ class Access_Statistic {
$fetchData = $this->db->fetchAll(
$this->db
->select('DISTINCT entrypoint AS value, COUNT(1) as cnt')
->from('table.access_log')
->from('table.access_logs')
->where("entrypoint != ''")
->group('entrypoint')
->order('cnt', Typecho_Db::SORT_DESC)
@ -292,7 +292,7 @@ class Access_Statistic {
case 'domain':
$fetchData = $this->db->fetchAll($this->db
->select('DISTINCT entrypoint_domain AS value, COUNT(1) as cnt')
->from('table.access_log')
->from('table.access_logs')
->where("entrypoint_domain != ''")
->group('entrypoint_domain')
->order('cnt', Typecho_Db::SORT_DESC)

View File

@ -45,7 +45,7 @@ class Access_Plugin implements Typecho_Plugin_Interface
$config = Typecho_Widget::widget('Widget_Options')->plugin('Access');
if ($config->isDrop == 0) {
$db = Typecho_Db::get();
$db->query("DROP TABLE `{$db->getPrefix()}access_log`", Typecho_Db::WRITE);
$db->query("DROP TABLE `{$db->getPrefix()}access_logs`", Typecho_Db::WRITE);
}
Helper::removePanel(1, self::$panel);
Helper::removeRoute("access_track_gif");
@ -104,7 +104,7 @@ class Access_Plugin implements Typecho_Plugin_Interface
}
$db = Typecho_Db::get();
$adapterName = $db->getAdapterName();
if (strpos($adapterName, 'Mysql') !== false) {
$prefix = $db->getPrefix();
$scripts = file_get_contents('usr/plugins/Access/sql/Mysql.sql');
@ -114,7 +114,7 @@ class Access_Plugin implements Typecho_Plugin_Interface
try {
$configLink = '<a href="' . Helper::options()->adminUrl . 'options-plugin.php?config=Access">' . _t('前往设置') . '</a>';
# 初始化数据库如果不存在
if (!$db->fetchRow($db->query("SHOW TABLES LIKE '{$prefix}access_log';", Typecho_Db::READ))) {
if (!$db->fetchRow($db->query("SHOW TABLES LIKE '{$prefix}access_logs';", Typecho_Db::READ))) {
foreach ($scripts as $script) {
$script = trim($script);
if ($script) {
@ -135,7 +135,7 @@ class Access_Plugin implements Typecho_Plugin_Interface
$row['os_version' ] = $ua->getOSVersion();
$row['path' ] = parse_url($row['url'], PHP_URL_PATH);
$row['query_string' ] = parse_url($row['url'], PHP_URL_QUERY);
$row['ip' ] = bindec(decbin(ip2long($row['ip'])));
$row['ip' ] = $row['ip'];
$row['entrypoint' ] = $row['referer'];
$row['entrypoint_domain'] = $row['referer_domain'];
$row['time' ] = $row['date'];
@ -144,7 +144,7 @@ class Access_Plugin implements Typecho_Plugin_Interface
$row['robot_version' ] = $ua->getRobotVersion();
unset($row['date']);
try {
$db->query($db->insert('table.access_log')->rows($row));
$db->query($db->insert('table.access_logs')->rows($row));
} catch (Typecho_Db_Exception $e) {
if ($e->getCode() != 23000)
throw new Typecho_Plugin_Exception(_t('导入旧版数据失败,插件启用失败,错误信息:%s。', $e->getMessage()));
@ -153,6 +153,20 @@ class Access_Plugin implements Typecho_Plugin_Interface
$db->query("DROP TABLE `{$prefix}access`;", Typecho_Db::WRITE);
$msg = _t('成功创建数据表并更新数据,插件启用成功,') . $configLink;
}
if ($db->fetchRow($db->query("SHOW TABLES LIKE '{$prefix}access_log';", Typecho_Db::READ))) {
$rows = $db->fetchAll($db->select()->from('table.access_log'));
foreach ($rows as $row) {
$row['ip'] = long2ip($row['ip']);
try {
$db->query($db->insert('table.access_logs')->rows($row));
} catch (Typecho_Db_Exception $e) {
if ($e->getCode() != 23000)
throw new Typecho_Plugin_Exception(_t('导入旧版数据失败,插件启用失败,错误信息:%s。', $e->getMessage()));
}
}
$db->query("DROP TABLE `{$prefix}access_log`;", Typecho_Db::WRITE);
$msg = _t('成功创建数据表并更新数据,插件启用成功,') . $configLink;
}
return $msg;
} catch (Typecho_Db_Exception $e) {
throw new Typecho_Plugin_Exception(_t('数据表建立失败,插件启用失败,错误信息:%s。', $e->getMessage()));
@ -167,7 +181,7 @@ class Access_Plugin implements Typecho_Plugin_Interface
try {
$configLink = '<a href="' . Helper::options()->adminUrl . 'options-plugin.php?config=Access">' . _t('前往设置') . '</a>';
# 初始化数据库如果不存在
if (!$db->fetchRow($db->query("SELECT name FROM sqlite_master WHERE TYPE='table' AND name='{$prefix}access_log';", Typecho_Db::READ))) {
if (!$db->fetchRow($db->query("SELECT name FROM sqlite_master WHERE TYPE='table' AND name='{$prefix}access_logs';", Typecho_Db::READ))) {
foreach ($scripts as $script) {
$script = trim($script);
if ($script) {

View File

@ -97,7 +97,7 @@ $access = new Access_Core();
<td><input type="checkbox" data-id="<?= $log['id']; ?>" value="<?= $log['id']; ?>" name="id[]"/></td>
<td><a target="_self" href="<?php $options->adminUrl('extending.php?panel=' . Access_Plugin::$panel . '&filter=path&path=' . $log['path'] . '&type='. $request->type); ?>"><?= urldecode(str_replace("%23", "#", $log['url'])); ?></a></td>
<td><a data-action="ua" href="#" title="<?= $log['ua'];?>"><?= $log['display_name']; ?></a></td>
<td><a data-action="ip" data-ip="<?= $access->long2ip($log['ip']); ?>" href="<?php $options->adminUrl('extending.php?panel=' . Access_Plugin::$panel . '&filter=ip&ip=' . $access->long2ip($log['ip']) . '&type='. $request->type); ?>"><?= $access->long2ip($log['ip']); ?></td>
<td><a data-action="ip" data-ip="<?= $log['ip'] ?>" href="<?php $options->adminUrl('extending.php?panel=' . Access_Plugin::$panel . '&filter=ip&ip=' . $log['ip'] . '&type='. $request->type); ?>"><?= $log['ip']; ?></td>
<td><?= $log['ip_loc'] ?></td>
<td><a target="_blank" data-action="referer" href="<?= $log['referer']; ?>"><?= $log['referer']; ?></a></td>
<td><?= date('Y-m-d H:i:s', $log['time']); ?></td>
@ -383,16 +383,6 @@ $(document).ready(function() {
});
$form.find('button[type="button"]').on('click', function() {
var ipRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
if ($filterSelect.val() == 'ip' && !ipRegex.test($ipInput.val())) {
return swal({
icon: 'error',
title: '筛选条件错误',
text: 'IP地址不合法'
});
}
$form.submit();
});
});

View File

@ -1,4 +1,4 @@
CREATE TABLE `typecho_access_log` (
CREATE TABLE `typecho_access_logs` (
`id` int(10) unsigned NOT NULL auto_increment,
`ua` varchar(512) default '' ,
`browser_id` varchar(32) default '' ,
@ -8,7 +8,7 @@ CREATE TABLE `typecho_access_log` (
`url` varchar(255) default '' ,
`path` varchar(255) default '' ,
`query_string` varchar(255) default '' ,
`ip` int(32) unsigned default '0' ,
`ip` varchar(64) default '' ,
`ip_country` varchar(255) default '' ,
`ip_province` varchar(255) default '' ,
`ip_city` varchar(255) default '' ,

View File

@ -1,4 +1,4 @@
CREATE TABLE `typecho_access_log` (
CREATE TABLE `typecho_access_logs` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`ua` varchar(512) default '' ,
`browser_id` varchar(32) default '' ,
@ -8,7 +8,7 @@ CREATE TABLE `typecho_access_log` (
`url` varchar(255) default '' ,
`path` varchar(255) default '' ,
`query_string` varchar(255) default '' ,
`ip` int(32) default '0' ,
`ip` varchar(64) default '' ,
`ip_country` varchar(255) default '' ,
`ip_province` varchar(255) default '' ,
`ip_city` varchar(255) default '' ,