From a0ffc19ee17d6fcc6ff8ec6951f7d420495de6b6 Mon Sep 17 00:00:00 2001 From: Kokororin Date: Tue, 8 Nov 2016 13:27:19 +0800 Subject: [PATCH] =?UTF-8?q?1.4=20=E4=BF=AE=E5=A4=8Dreferer=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=88=A0=E9=99=A4=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Access.php | 20 +++++++++++- Action.php | 51 +++++++++++++++++++++++++++++-- Plugin.php | 54 +++++++++++++++++---------------- README.md | 16 +++++++++- page/console.php | 79 +++++++++++++++++++++++++++++++++++++++++++----- 5 files changed, 181 insertions(+), 39 deletions(-) diff --git a/Access.php b/Access.php index c901827..2b0933e 100644 --- a/Access.php +++ b/Access.php @@ -8,7 +8,6 @@ class Access_Extend protected $request; protected $pageSize; protected $isDrop; - private static $_instance = null; public $action; public $title; public $logs = array(); @@ -283,4 +282,23 @@ class Access_Extend } } + public function isAdmin() + { + $hasLogin = Typecho_Widget::widget('Widget_User')->hasLogin(); + if (!$hasLogin) { + return false; + } + $isAdmin = Typecho_Widget::widget('Widget_User')->pass('administrator', true); + return $isAdmin; + } + + public function deleteLogs($ids) + { + foreach ($ids as $id) { + $this->db->query($this->db->delete($this->table) + ->where('id = ?', $id) + ); + } + } + } diff --git a/Action.php b/Action.php index b6d2195..e43a69c 100644 --- a/Action.php +++ b/Action.php @@ -4,11 +4,14 @@ class Access_Action implements Widget_Interface_Do private $response; private $request; + private $extend; public function __construct() { $this->response = Typecho_Response::getInstance(); $this->request = Typecho_Request::getInstance(); + require_once __DIR__ . '/Access.php'; + $this->extend = new Access_Extend(); } public function execute() @@ -21,9 +24,51 @@ class Access_Action implements Widget_Interface_Do public function ip() { - $ip = $this->request->get('ip'); - $response = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip); - exit($response); + $this->response->setContentType('application/json'); + try { + $this->checkAuth(); + $ip = $this->request->get('ip'); + $response = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip); + if (!$response) { + throw new Exception('HTTP request failed'); + } + exit($response); + } catch (Exception $e) { + exit(Json::encode(array( + 'code' => 100, + 'message' => $e->getMessage(), + ))); + } + } + + public function deleteLogs() + { + $this->response->setContentType('application/json'); + try { + $this->checkAuth(); + $data = @file_get_contents('php://input'); + $data = Json::decode($data, true); + if (!is_array($data)) { + throw new Exception('params invalid'); + } + $this->extend->deleteLogs($data); + exit(Json::encode(array( + 'code' => 0, + ))); + + } catch (Exception $e) { + exit(Json::encode(array( + 'code' => 100, + 'message' => $e->getMessage(), + ))); + } + } + + protected function checkAuth() + { + if (!$this->extend->isAdmin()) { + throw new Exception('Access Denied'); + } } } diff --git a/Plugin.php b/Plugin.php index c72a40c..78b0335 100644 --- a/Plugin.php +++ b/Plugin.php @@ -4,7 +4,7 @@ * * @package Access * @author Kokororin - * @version 1.3 + * @version 1.4 * @link https://kotori.love */ class Access_Plugin implements Typecho_Plugin_Interface @@ -15,6 +15,7 @@ class Access_Plugin implements Typecho_Plugin_Interface $msg = Access_Plugin::install(); Helper::addPanel(1, self::$panel, 'Access控制台', 'Access插件控制台', 'subscriber'); Helper::addRoute("access_ip", "/access/ip.json", "Access_Action", 'ip'); + Helper::addRoute("access_delete_logs", "/access/log/delete", "Access_Action", 'deleteLogs'); Typecho_Plugin::factory('Widget_Archive')->header = array('Access_Plugin', 'start'); Typecho_Plugin::factory('admin/footer.php')->end = array('Access_Plugin', 'adminFooter'); return _t($msg); @@ -31,20 +32,27 @@ class Access_Plugin implements Typecho_Plugin_Interface } Helper::removePanel(1, self::$panel); Helper::removeRoute("access_ip"); + Helper::removeRoute("access_delete_logs"); } public static function config(Typecho_Widget_Helper_Form $form) { $pageSize = new Typecho_Widget_Helper_Form_Element_Text( - 'pageSize', null, '', + 'pageSize', null, '10', '分页数量', '每页显示的日志数量'); $isDrop = new Typecho_Widget_Helper_Form_Element_Radio( 'isDrop', array( '0' => '删除', '1' => '不删除', - ), '', '删除数据表:', '请选择是否在禁用插件时,删除日志数据表'); + ), '1', '删除数据表:', '请选择是否在禁用插件时,删除日志数据表'); + $canAnalytize = new Typecho_Widget_Helper_Form_Element_Radio( + 'canAnalytize', array( + '0' => '不允许', + '1' => '允许', + ), '1', '允许统计使用情况:', '请选择是否允许插件作者统计使用情况'); $form->addInput($pageSize); $form->addInput($isDrop); + $form->addInput($canAnalytize); } public static function personalConfig(Typecho_Widget_Helper_Form $form) @@ -94,48 +102,42 @@ class Access_Plugin implements Typecho_Plugin_Interface } } - public static function hasLogin() - { - $cookieUid = Typecho_Cookie::get('__typecho_uid'); - if (null !== $cookieUid) { - $db = Typecho_Db::get(); - $user = $db->fetchRow($db->select()->from('table.users') - ->where('uid = ?', intval($cookieUid)) - ->limit(1)); - - $cookieAuthCode = Typecho_Cookie::get('__typecho_authCode'); - if ($user && Typecho_Common::hashValidate($user['authCode'], $cookieAuthCode)) { - return true; - } - Typecho_Cookie::delete('__typecho_uid'); - Typecho_Cookie::delete('__typecho_authCode'); - } - return false; - } - public static function start() { - if (self::hasLogin()) { + require_once __DIR__ . '/Access.php'; + $extend = new Access_Extend(); + if ($extend->isAdmin()) { return; } $config = Typecho_Widget::widget('Widget_Options')->plugin('Access'); $request = Typecho_Request::getInstance(); $ip = $request->getIp(); - $url = $_SERVER['REQUEST_URI']; + $url = $request->getServer('REQUEST_URI'); if ($ip == null) { - $ip = 'UnKnow'; + $ip = 'UnKnown'; } $options = Typecho_Widget::widget('Widget_Options'); $timeStamp = $options->gmtTime; $offset = $options->timezone - $options->serverTimezone; $gtime = $timeStamp + $offset; $db = Typecho_Db::get(); + $referer = Typecho_Cookie::get('__typecho_access_referer'); + if ($referer == null) { + $referer = $request->getReferer(); + if (strpos($referer, rtrim(Helper::options()->siteUrl, '/')) !== false) { + $referer = null; + } + if ($referer != null) { + Typecho_Cookie::set('__typecho_access_referer', $referer); + } + } + $rows = array( 'ua' => $request->getAgent(), 'url' => $url, 'ip' => $ip, - 'referer' => $request->getReferer(), + 'referer' => $referer, 'referer_domain' => parse_url($request->getReferer(), PHP_URL_HOST), 'date' => $gtime, ); diff --git a/README.md b/README.md index a3a1c00..dba2c60 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,21 @@ * ip归属地使用淘宝API * 管理员登录时不记录日志 * 查看来源页和来源域名排名 +* 修复Referer记录错误的bug +* 添加删除日志的功能 ### 使用须知 -插件目录名请修改为Access \ No newline at end of file +* 插件更新升级时,请先禁用插件后再上传 +* 插件目录名请修改为Access + +### 图示 +![](https://kotori.love/usr/uploads/2015/12/4187563925.jpg) + +![A75B8F39-C8B6-4CD2-AFFC-784B3E27B8A4.png](https://kotori.love/usr/uploads/2015/12/2019049143.png) + +![](https://kotori.love/usr/uploads/2016/08/1564663056.png) + +![](https://kotori.love/usr/uploads/2016/08/1121750290.png) + +![BDEF004E-157E-4ADF-99C0-5EE65BDA61A6.png](https://kotori.love/usr/uploads/2016/11/3973345673.png) \ No newline at end of file diff --git a/page/console.php b/page/console.php index 8451f3e..a75d112 100644 --- a/page/console.php +++ b/page/console.php @@ -31,7 +31,7 @@ $extend = new Access_Extend();
@@ -75,8 +75,8 @@ $extend = new Access_Extend(); logs['list'])): ?> logs['list'] as $log): ?> - - + + "> parseUA($log['ua']); ?> @@ -102,7 +102,7 @@ $extend = new Access_Extend();
@@ -265,18 +265,18 @@ $(document).ready(function() { dataType: 'json', data: {ip: $(this).data('ip')}, success: function(data) { - if (data.code == 0){ + if (data.code == 0) { swal({ title: "IP查询成功", text: data.data.country + data.data.area + data.data.city + data.data.country + data.data.isp, - type: "info", + type: "success", confirmButtonText: "OK" }); } else { swal({ title: "IP查询失败", text: '接口返回状态码错误', - type: "info", + type: "warning", confirmButtonText: "OK" }); } @@ -285,13 +285,61 @@ $(document).ready(function() { swal({ title: "IP查询失败", text: '网络异常或PHP环境配置异常', - type: "info", + type: "warning", confirmButtonText: "OK" }); } }); return false; }); + + $('.dropdown-menu a[data-action="delete"]').click(function() { + swal({ + title: "你确定?", + text: "你确认要删除这些记录吗?", + type: "warning", + showCancelButton: true, + confirmButtonColor: "#DD6B55", + confirmButtonText: "是的", + cancelButtonText: "算啦", + closeOnConfirm: false + }, function() { + var ids = []; + $('.typecho-list-table input[type="checkbox"]').each(function(index, elem) { + if (elem.checked) { + ids.push($(elem).data('id')); + } + }); + + if (ids.length == 0) { + return swal("错误", "你并没有勾选任何内容", "warning"); + } + $.ajax({ + url: 'index, '/').'/access/log/delete';?>', + method: 'post', + dataType: 'json', + contentType: 'application/json', + data: JSON.stringify(ids), + success: function(data) { + if (data.code == 0) { + swal("删除成功", "所选记录已删除", "success"); + $.each(ids, function(index, elem) { + $('.typecho-list-table tbody tr[data-id="' + elem + '"]').fadeOut(500).remove(); + }); + } else { + swal({ + title: "错误", + text: '发生错误了', + type: "warning", + confirmButtonText: "OK" + }); + } + } + }); + }); + var t = $(this); + t.parents('.dropdown-menu').hide().prev().removeClass('active'); + }); }); @@ -346,6 +394,21 @@ $(document).ready(function() { +plugin('Access') == 1):?> + + + \ No newline at end of file