diff --git a/Access_Core.php b/Access_Core.php index 18e35a1..5d27b13 100644 --- a/Access_Core.php +++ b/Access_Core.php @@ -8,7 +8,7 @@ class Access_Core protected $db; protected $request; protected $response; - + public $ua; public $config; public $action; @@ -28,13 +28,13 @@ class Access_Core # Load language pack if (Typecho_I18n::getLang() != 'zh_CN') { $file = __TYPECHO_ROOT_DIR__ . __TYPECHO_PLUGIN_DIR__ . - '/Access/lang/' . Typecho_I18n::getLang() . '.mo'; + '/Access/lang/' . Typecho_I18n::getLang() . '.mo'; file_exists($file) && Typecho_I18n::addLang($file); } # Init variables - $this->db = Typecho_Db::get(); - $this->config = Typecho_Widget::widget('Widget_Options')->plugin('Access'); - $this->request = Typecho_Request::getInstance(); + $this->db = Typecho_Db::get(); + $this->config = Typecho_Widget::widget('Widget_Options')->plugin('Access'); + $this->request = Typecho_Request::getInstance(); $this->response = Typecho_Response::getInstance(); if ($this->config->pageSize == null || $this->config->isDrop == null) { throw new Typecho_Plugin_Exception(_t('请先设置插件!')); @@ -69,8 +69,8 @@ class Access_Core $pagenum = $this->request->get('page', 1); $offset = (max(intval($pagenum), 1) - 1) * $this->config->pageSize; $query = $this->db->select()->from('table.access_log') - ->order('time', Typecho_Db::SORT_DESC) - ->offset($offset)->limit($this->config->pageSize); + ->order('time', Typecho_Db::SORT_DESC) + ->offset($offset)->limit($this->config->pageSize); $qcount = $this->db->select('count(1) AS count')->from('table.access_log'); switch ($type) { case 1: @@ -123,7 +123,7 @@ class Access_Core $this->logs['list'] = $this->htmlEncode($this->urlDecode($list)); $this->logs['rows'] = $this->db->fetchAll($qcount)[0]['count']; - + $page = new Access_Page($this->config->pageSize, $this->logs['rows'], $pagenum, 10, array( 'panel' => Access_Plugin::$panel, 'action' => 'logs', @@ -148,11 +148,11 @@ class Access_Core protected function parseReferer() { $this->referer['url'] = $this->db->fetchAll($this->db->select('DISTINCT entrypoint AS value, COUNT(1) as count') - ->from('table.access_log')->where("entrypoint <> ''")->group('entrypoint') - ->order('count', Typecho_Db::SORT_DESC)->limit($this->config->pageSize)); + ->from('table.access_log')->where("entrypoint <> ''")->group('entrypoint') + ->order('count', Typecho_Db::SORT_DESC)->limit($this->config->pageSize)); $this->referer['domain'] = $this->db->fetchAll($this->db->select('DISTINCT entrypoint_domain AS value, COUNT(1) as count') - ->from('table.access_log')->where("entrypoint_domain <> ''")->group('entrypoint_domain') - ->order('count', Typecho_Db::SORT_DESC)->limit($this->config->pageSize)); + ->from('table.access_log')->where("entrypoint_domain <> ''")->group('entrypoint_domain') + ->order('count', Typecho_Db::SORT_DESC)->limit($this->config->pageSize)); $this->referer = $this->htmlEncode($this->urlDecode($this->referer)); } @@ -165,53 +165,72 @@ class Access_Core protected function parseOverview() { # 初始化统计数组 - foreach (['ip', 'uv', 'pv'] as $type) { - foreach (['today', 'yesterday'] as $day) { + foreach (array('ip', 'uv', 'pv') as $type) { + foreach (array('today', 'yesterday') as $day) { $this->overview[$type][$day]['total'] = 0; } } - + # 分类分时段统计数据 - foreach (['today' => date("Y-m-d"), 'yesterday'=> date("Y-m-d", strtotime('-1 day'))] as $day => $time) { + foreach (array('today' => date("Y-m-d"), 'yesterday' => date("Y-m-d", strtotime('-1 day'))) as $day => $time) { for ($i = 0; $i < 24; $i++) { $start = strtotime(date("{$time} {$i}:00:00")); - $end = strtotime(date("{$time} {$i}:59:59")); + $end = strtotime(date("{$time} {$i}:59:59")); // "SELECT DISTINCT ip FROM {$this->table} {$where} AND `time` BETWEEN {$start} AND {$end}")); $subQuery = $this->db->select('DISTINCT ip')->from('table.access_log') - ->where("time >= ? AND time <= ?", $start, $end); + ->where("time >= ? AND time <= ?", $start, $end); if (method_exists($subQuery, 'prepare')) { $subQuery = $subQuery->prepare($subQuery); } $this->overview['ip'][$day]['hours'][$i] = intval($this->db->fetchAll($this->db->select('COUNT(1) AS count') - ->from('(' . $subQuery . ') AS tmp'))[0]['count']); - $this->overview['ip'][$day]['total'] += $this->overview['ip'][$day]['hours'][$i]; + ->from('(' . $subQuery . ') AS tmp'))[0]['count']); + // "SELECT DISTINCT ip,ua FROM {$this->table} {$where} AND `time` BETWEEN {$start} AND {$end}")); $this->overview['uv'][$day]['hours'][$i] = intval($this->db->fetchAll($this->db->select('COUNT(1) AS count') - ->from('(' . $subQuery . ') AS tmp'))[0]['count']); - $this->overview['uv'][$day]['total'] += $this->overview['uv'][$day]['hours'][$i]; + ->from('(' . $subQuery . ') AS tmp'))[0]['count']); // "SELECT ip FROM {$this->table} {$where} AND `time` BETWEEN {$start} AND {$end}")); $this->overview['pv'][$day]['hours'][$i] = intval($this->db->fetchAll($this->db->select('COUNT(1) AS count') - ->from('table.access_log')->where('time >= ? AND time <= ?', $start, $end))[0]['count']); - $this->overview['pv'][$day]['total'] += $this->overview['pv'][$day]['hours'][$i]; + ->from('table.access_log')->where('time >= ? AND time <= ?', $start, $end))[0]['count']); + } + + $start = strtotime(date("{$time} 00:00:00")); + $end = strtotime(date("{$time} 23:59:59")); + + $subQuery = $this->db->select('DISTINCT ip')->from('table.access_log')->where("time >= ? AND time <= ?", $start, $end); + if (method_exists($subQuery, 'prepare')) { + $subQuery = $subQuery->prepare($subQuery); + } + $this->overview['ip'][$day]['total'] = $this->db->fetchAll($this->db->select('COUNT(1) AS count')->from('(' . $subQuery . ') AS tmp'))[0]['count']; + + $subQuery = $this->db->select('DISTINCT ip,ua')->from('table.access_log')->where("time >= ? AND time <= ?", $start, $end); + if (method_exists($subQuery, 'prepare')) { + $subQuery = $subQuery->prepare($subQuery); + } + $this->overview['uv'][$day]['total'] = $this->db->fetchAll($this->db->select('COUNT(1) AS count')->from('(' . $subQuery . ') AS tmp'))[0]['count']; + + $this->overview['pv'][$day]['total'] = $this->db->fetchAll($this->db->select('COUNT(1) AS count') + ->from('table.access_log') + ->where("time >= ? AND time <= ?", $start, $end) + )[0]['count']; } # 总统计数据 // "SELECT DISTINCT ip FROM {$this->table} {$where}")); $this->overview['ip']['all']['total'] = $this->db->fetchAll($this->db->select('COUNT(1) AS count') - ->from('(' . $this->db->select('DISTINCT ip')->from('table.access_log') . ') AS tmp'))[0]['count']; + ->from('(' . $this->db->select('DISTINCT ip')->from('table.access_log') . ') AS tmp'))[0]['count']; // "SELECT DISTINCT ip,ua FROM {$this->table} {$where}")); $this->overview['uv']['all']['total'] = $this->db->fetchAll($this->db->select('COUNT(1) AS count') - ->from('(' . $this->db->select('DISTINCT ip,ua')->from('table.access_log') . ') AS tmp'))[0]['count']; + ->from('(' . $this->db->select('DISTINCT ip,ua')->from('table.access_log') . ') AS tmp'))[0]['count']; // "SELECT ip FROM {$this->table} {$where}")); $this->overview['pv']['all']['total'] = $this->db->fetchAll($this->db->select('COUNT(1) AS count') - ->from('table.access_log'))[0]['count']; + ->from('table.access_log'))[0]['count']; # 分类型绘制24小时访问图 - $this->overview['chart']['xAxis']['categories'] = json_encode([ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 - ]); - foreach (['ip', 'uv', 'pv'] as $type) { + $this->overview['chart']['xAxis']['categories'] = json_encode(array( + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + )); + foreach (array('ip', 'uv', 'pv') as $type) { $this->overview['chart']['series'][$type] = json_encode($this->overview[$type]['today']['hours']); } $this->overview['chart']['title']['text'] = _t('%s 统计', date("Y-m-d")); @@ -231,7 +250,7 @@ class Access_Core protected function htmlEncode($data, $valuesOnly = true, $charset = 'UTF-8') { if (is_array($data)) { - $d = []; + $d = array(); foreach ($data as $key => $value) { if (!$valuesOnly) { $key = $this->htmlEncode($key, $valuesOnly, $charset); @@ -258,7 +277,7 @@ class Access_Core protected function urlDecode($data, $valuesOnly = true) { if (is_array($data)) { - $d = []; + $d = array(); foreach ($data as $key => $value) { if (!$valuesOnly) { $key = $this->urlDecode($key, $valuesOnly); @@ -298,7 +317,7 @@ class Access_Core { foreach ($ids as $id) { $this->db->query($this->db->delete('table.access_log') - ->where('id = ?', $id) + ->where('id = ?', $id) ); } } @@ -343,10 +362,10 @@ class Access_Core $ip = '0.0.0.0'; } $ip = bindec(decbin(ip2long($ip))); - + $entrypoint = $this->getEntryPoint(); - $referer = $this->request->getReferer(); - $time = Helper::options()->gmtTime + (Helper::options()->timezone - Helper::options()->serverTimezone); + $referer = $this->request->getReferer(); + $time = Helper::options()->gmtTime + (Helper::options()->timezone - Helper::options()->serverTimezone); if ($archive != null) { $parsedArchive = $this->parseArchive($archive); @@ -358,25 +377,25 @@ class Access_Core } $rows = array( - 'ua' => $this->ua->getUA(), - 'browser_id' => $this->ua->getBrowserID(), - 'browser_version' => $this->ua->getBrowserVersion(), - 'os_id' => $this->ua->getOSID(), - 'os_version' => $this->ua->getOSVersion(), - 'url' => $url, - 'path' => parse_url($url, PHP_URL_PATH), - 'query_string' => parse_url($url, PHP_URL_QUERY), - 'ip' => $ip, - 'referer' => $referer, - 'referer_domain' => parse_url($referer, PHP_URL_HOST), - 'entrypoint' => $entrypoint, + 'ua' => $this->ua->getUA(), + 'browser_id' => $this->ua->getBrowserID(), + 'browser_version' => $this->ua->getBrowserVersion(), + 'os_id' => $this->ua->getOSID(), + 'os_version' => $this->ua->getOSVersion(), + 'url' => $url, + 'path' => parse_url($url, PHP_URL_PATH), + 'query_string' => parse_url($url, PHP_URL_QUERY), + 'ip' => $ip, + 'referer' => $referer, + 'referer_domain' => parse_url($referer, PHP_URL_HOST), + 'entrypoint' => $entrypoint, 'entrypoint_domain' => parse_url($entrypoint, PHP_URL_HOST), - 'time' => $time, - 'content_id' => $content_id, - 'meta_id' => $meta_id, - 'robot' => $this->ua->isRobot() ? 1 : 0, - 'robot_id' => $this->ua->getRobotID(), - 'robot_version' => $this->ua->getRobotVersion(), + 'time' => $time, + 'content_id' => $content_id, + 'meta_id' => $meta_id, + 'robot' => $this->ua->isRobot() ? 1 : 0, + 'robot_id' => $this->ua->getRobotID(), + 'robot_version' => $this->ua->getRobotVersion(), ); try { @@ -391,19 +410,19 @@ class Access_Core * @return void * @throws Typecho_Plugin_Exception */ - public static function rewriteLogs() + public static function rewriteLogs() { $db = Typecho_Db::get(); $rows = $db->fetchAll($db->select()->from('table.access_log')); foreach ($rows as $row) { $ua = new Access_UA($row['ua']); - $row['browser_id' ] = $ua->getBrowserID(); - $row['browser_version' ] = $ua->getBrowserVersion(); - $row['os_id' ] = $ua->getOSID(); - $row['os_version' ] = $ua->getOSVersion(); - $row['robot' ] = $ua->isRobot() ? 1 : 0; - $row['robot_id' ] = $ua->getRobotID(); - $row['robot_version' ] = $ua->getRobotVersion(); + $row['browser_id'] = $ua->getBrowserID(); + $row['browser_version'] = $ua->getBrowserVersion(); + $row['os_id'] = $ua->getOSID(); + $row['os_version'] = $ua->getOSVersion(); + $row['robot'] = $ua->isRobot() ? 1 : 0; + $row['robot_id'] = $ua->getRobotID(); + $row['robot_version'] = $ua->getRobotVersion(); try { $db->query($db->update('table.access_log')->rows($row)->where('id = ?', $row['id'])); } catch (Typecho_Db_Exception $e) { @@ -414,11 +433,11 @@ class Access_Core /** * 解析archive对象 - * + * * @access public * @return array */ - public function parseArchive($archive) + public function parseArchive($archive) { // 暂定首页的meta_id为0 $content_id = null; @@ -434,8 +453,8 @@ class Access_Core } elseif ($archive->is('archive', 404)) {} return array( - 'content_id' => $content_id, - 'meta_id' => $meta_id, + 'content_id' => $content_id, + 'meta_id' => $meta_id, ); } diff --git a/Action.php b/Action.php index 59b5989..56c84ea 100644 --- a/Action.php +++ b/Action.php @@ -16,12 +16,10 @@ class Access_Action implements Widget_Interface_Do } public function execute() - { - } + {} public function action() - { - } + {} public function writeLogs() { @@ -71,7 +69,6 @@ class Access_Action implements Widget_Interface_Do public function deleteLogs() { - $this->response->setContentType('application/json'); try { $this->checkAuth(); $data = @file_get_contents('php://input'); @@ -91,7 +88,7 @@ class Access_Action implements Widget_Interface_Do ); } - exit(Json::encode($response)); + $this->response->throwJson($response); } protected function checkAuth() diff --git a/Plugin.php b/Plugin.php index a078656..95a5a3f 100644 --- a/Plugin.php +++ b/Plugin.php @@ -5,13 +5,13 @@ require_once __DIR__ . '/Access_Bootstrap.php'; * * @package Access * @author Kokororin - * @version 2.0.1 + * @version 2.0.2 * @link https://kotori.love */ class Access_Plugin implements Typecho_Plugin_Interface { public static $panel = 'Access/page/console.php'; - + /** * 激活插件方法,如果激活失败,直接抛出异常 * @@ -93,7 +93,8 @@ class Access_Plugin implements Typecho_Plugin_Interface * @param Typecho_Widget_Helper_Form $form * @return void */ - public static function personalConfig(Typecho_Widget_Helper_Form $form) { } + public static function personalConfig(Typecho_Widget_Helper_Form $form) + {} /** * 初始化以及升级插件数据库,如初始化失败,直接抛出异常 @@ -112,8 +113,8 @@ class Access_Plugin implements Typecho_Plugin_Interface if (false === strpos($adapterName, 'Mysql')) { throw new Typecho_Plugin_Exception(_t('你的适配器为%s,目前只支持Mysql', $adapterName)); } - - $prefix = $db->getPrefix(); + + $prefix = $db->getPrefix(); $scripts = file_get_contents('usr/plugins/Access/sql/Mysql.sql'); $scripts = str_replace('typecho_', $prefix, $scripts); $scripts = str_replace('%charset%', 'utf8', $scripts); @@ -136,25 +137,27 @@ class Access_Plugin implements Typecho_Plugin_Interface foreach ($rows as $row) { $ua = new Access_UA($row['ua']); $time = Helper::options()->gmtTime + (Helper::options()->timezone - Helper::options()->serverTimezone); - $row['browser_id' ] = $ua->getBrowserID(); - $row['browser_version' ] = $ua->getBrowserVersion(); - $row['os_id' ] = $ua->getOSID(); - $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['entrypoint' ] = $row['referer']; + $row['browser_id'] = $ua->getBrowserID(); + $row['browser_version'] = $ua->getBrowserVersion(); + $row['os_id'] = $ua->getOSID(); + $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['entrypoint'] = $row['referer']; $row['entrypoint_domain'] = $row['referer_domain']; - $row['time' ] = $row['date']; - $row['robot' ] = $ua->isRobot() ? 1 : 0; - $row['robot_id' ] = $ua->getRobotID(); - $row['robot_version' ] = $ua->getRobotVersion(); + $row['time'] = $row['date']; + $row['robot'] = $ua->isRobot() ? 1 : 0; + $row['robot_id'] = $ua->getRobotID(); + $row['robot_version'] = $ua->getRobotVersion(); unset($row['date']); try { $db->query($db->insert('table.access_log')->rows($row)); } catch (Typecho_Db_Exception $e) { - if ($e->getCode() != 23000) + if ($e->getCode() != 23000) { throw new Typecho_Plugin_Exception(_t('导入旧版数据失败,插件启用失败,错误信息:%s。', $e->getMessage())); + } + } } $db->query("DROP TABLE `{$prefix}access`;", Typecho_Db::WRITE); @@ -210,7 +213,7 @@ class Access_Plugin implements Typecho_Plugin_Interface $(document).ready(function() { $("#start-link").append("