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 + +### 图示 + + + + + + + + + \ 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();