113 lines
3.9 KiB
PHP
113 lines
3.9 KiB
PHP
<?php
|
||
/*
|
||
Plugin Name: 在线统计
|
||
Version: 1.7.1
|
||
Plugin URL: http://xiaosong.org/tech/new-version-online-statistics-plugin-released
|
||
Description: 显示当前在线人数及最高纪录,后台显示当前在线列表并可自定义过滤的蜘蛛UA标记
|
||
ForEmlog:4.2.1+
|
||
Author: 小松
|
||
Author Email: sahala_2007@126.com
|
||
Author URL: http://xiaosong.org/
|
||
*/
|
||
!defined('EMLOG_ROOT') && exit('access deined!');
|
||
|
||
require_once('online2_config.php');
|
||
|
||
function is_bot(){
|
||
global $ua_block;
|
||
if (empty($ua_block)) {
|
||
return false; // Not block bot
|
||
}
|
||
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
|
||
$ua_blocks = strtolower($ua_block);
|
||
$ua_block_array = explode("|", $ua_blocks);
|
||
$botlist = $ua_block_array;
|
||
foreach($botlist as $bot){
|
||
if(strpos($ua, $bot) !== false)
|
||
return true; // Is a bot
|
||
}
|
||
return false; // Not a bot
|
||
}
|
||
|
||
function online2(){
|
||
$DB = MySql::getInstance();
|
||
$stringIp = getIp();
|
||
$useragent = addslashes($_SERVER['HTTP_USER_AGENT']);
|
||
if (!empty($stringIp) && !is_bot()) {
|
||
$inDB = $DB->query("SELECT 1 FROM ".DB_PREFIX."online2 WHERE ip='".$stringIp."'");
|
||
if (!$DB->num_rows($inDB)) {
|
||
$DB->query("INSERT INTO ".DB_PREFIX."online2 (ip, useragent) VALUES('".$stringIp."', '".$useragent."')");
|
||
} else {
|
||
$DB->query("UPDATE ".DB_PREFIX."online2 SET dt=NOW(), useragent = '".$useragent."' WHERE ip='".$stringIp."'");
|
||
}
|
||
if (ROLE == 'admin') {
|
||
$DB->query("DELETE FROM ".DB_PREFIX."online2 WHERE dt<SUBTIME(NOW(),'0 0:10:0')");
|
||
}
|
||
$nowOnline = nowOnline();
|
||
$res_max = maxOnline();
|
||
$maxOnline = $res_max['maximum'];
|
||
if ($nowOnline > $maxOnline) {
|
||
$cacheData = serialize(array('maximum' => $nowOnline, 'maxDate' => time()));
|
||
Option::updateOption('maxOnline', $cacheData);
|
||
$CACHE = Cache::getInstance();
|
||
$CACHE->updateCache('options');
|
||
}
|
||
}
|
||
}
|
||
addAction('index_head', 'online2');
|
||
addAction('adm_head', 'online2');
|
||
|
||
function maxOnline(){
|
||
$data = unserialize(Option::get('maxOnline'));
|
||
return $data;
|
||
}
|
||
|
||
function nowOnline(){
|
||
$DB = MySql::getInstance();
|
||
$res_now = $DB->once_fetch_array("SELECT count(1) AS total FROM ".DB_PREFIX."online2 WHERE dt>SUBTIME(NOW(),'0 0:10:0') ");
|
||
$nowOnline = $res_now['total'];
|
||
return $nowOnline;
|
||
}
|
||
|
||
function onlineOutput(){
|
||
global $is_querycount, $query_template;
|
||
$DB = MySql::getInstance();
|
||
$maxOnline = maxOnline();
|
||
$nowOnline = nowOnline();
|
||
$maxOnlineTime = smartDate($maxOnline["maxDate"], 'Y-m-d');
|
||
$queryCount = $DB->getQueryCount();
|
||
$queryCountTemplate = str_replace('{queryCount}', $queryCount, $query_template);
|
||
$queryCountOutput = (isset($is_querycount) && $is_querycount == 'true') ? $queryCountTemplate : '';
|
||
echo '总计 '.$nowOnline.' 人在线 - 最高纪录是 '.$maxOnline["maximum"].' 于 '.$maxOnlineTime.$queryCountOutput.'<!-- Powered online2 plugin by xiaosong.org -->';
|
||
}
|
||
|
||
if ($is_footer == 'true') {
|
||
addAction('index_footer', 'onlineOutput');
|
||
}
|
||
|
||
//仅供后台使用
|
||
function displayOnlineList(){
|
||
$DB = MySql::getInstance();
|
||
$res_nowonline = $DB->query("SELECT ip, dt, useragent FROM ".DB_PREFIX."online2 order by dt DESC");
|
||
$output_online = '';
|
||
while($row = $DB->fetch_array($res_nowonline)){
|
||
$iplocation = function_exists('convertip') ? '[- '.convertip($row['ip']).']' : '';
|
||
$output_online .= empty($row) ? '<tr><td colspan="3">暂无在线访客</td></tr>' : '<tr><td>'.$row['ip'].$iplocation.'</td><td>'.$row['dt'].'</td><td>'.$row['useragent'].'</td></tr>';
|
||
}
|
||
return $output_online;
|
||
}
|
||
|
||
function online_backup(){
|
||
$DB = MySql::getInstance();
|
||
global $tables;
|
||
$is_exist_online2_query = $DB->query('show tables like "'.DB_PREFIX.'online2"');
|
||
if($DB->num_rows($is_exist_online2_query) != 0) array_push($tables, 'online2');
|
||
}
|
||
addAction('data_prebakup', 'online_backup');
|
||
|
||
function online_menu()
|
||
{
|
||
echo '<div class="sidebarsubmenu" id="online2"><a href="./plugin.php?plugin=online2">在线统计</a></div>';
|
||
}
|
||
addAction('adm_sidebar_ext', 'online_menu');
|
||
?>
|