日志升级与缓存修复最终版
This commit is contained in:
+111
-26
@@ -4,7 +4,7 @@ require_once(dirname(__DIR__) . "/cong.php");
|
||||
require_once(dirname(__DIR__) . "/lib/DbHelper.php");
|
||||
require_once(dirname(__DIR__) . "/lib/Cloak/PipelineTimer.php");
|
||||
require_once(dirname(__DIR__) . "/lib/Cloak/VisitorLogSchema.php");
|
||||
require_once(dirname(__DIR__) . "/lib/Cloak/VisitorLogListHelper.php");
|
||||
require_once(dirname(__DIR__) . "/lib/Cloak/VisitorApiAudit.php");
|
||||
|
||||
$reasons = array(
|
||||
"blank_referrer" => "过滤无来源的访问",
|
||||
@@ -44,9 +44,15 @@ $reasons = array(
|
||||
"time_of_day" => "不可访问的日期",
|
||||
);
|
||||
|
||||
$jsonResult = array();
|
||||
$conn = new mysqli('localhost', DB_USERNAME, DB_PASSWORD, DB_NAME);
|
||||
$servername = "localhost";
|
||||
$username = DB_USERNAME;
|
||||
$password = DB_PASSWORD;
|
||||
$dbname = DB_NAME;
|
||||
|
||||
// Create connection
|
||||
$conn = new mysqli($servername, $username, $password, $dbname);
|
||||
$jsonResult = array();
|
||||
// Check connection
|
||||
if ($conn->connect_error) {
|
||||
$error = "Connection failed: " . $conn->connect_error;
|
||||
$handle = fopen(dirname(__FILE__) . "/err.txt", "a+");
|
||||
@@ -59,40 +65,119 @@ if ($conn->connect_error) {
|
||||
$jsonResult["data"] = array();
|
||||
} else {
|
||||
VisitorLogSchema::ensureColumns($conn);
|
||||
VisitorLogSchema::ensureIndexes($conn);
|
||||
|
||||
if(isset($_GET['clear']) && $_GET['clear'] == "true") {
|
||||
$conn->query("TRUNCATE `visitor_logs`");
|
||||
if(isset($_GET['clear']) && $_GET['clear'] == "true") { //清除日志数据表
|
||||
$sql = "TRUNCATE `visitor_logs`";
|
||||
|
||||
// 执行查询并输出结果
|
||||
$result = mysqli_query($conn, $sql);
|
||||
$jsonResult["code"] = 0;
|
||||
$jsonResult["msg"] = "success";
|
||||
$jsonResult["count"] = 0;
|
||||
$jsonResult["data"] = array();
|
||||
} else {
|
||||
$pageNumber = isset($_GET['page']) ? max(1, (int) $_GET['page']) : 1;
|
||||
$recordsPerPage = isset($_GET['limit']) ? max(1, min(200, (int) $_GET['limit'])) : 50;
|
||||
} else { // 默认分页数据
|
||||
// 获取当前页码(默认为第1页)
|
||||
$pageNumber = isset($_GET['page']) ? (int) $_GET['page'] : 1;
|
||||
|
||||
// 设置每页显示的记录条数
|
||||
$recordsPerPage = isset($_GET['limit']) ? (int) $_GET['limit'] : 50;
|
||||
|
||||
// 计算起始位置
|
||||
$startPosition = ($pageNumber - 1) * $recordsPerPage;
|
||||
$where = VisitorLogListHelper::buildWhere($conn, $_GET);
|
||||
|
||||
$sql = VisitorLogListHelper::listSelectSql($where, $startPosition, $recordsPerPage, false);
|
||||
$result = $conn->query($sql);
|
||||
$jsonData = array();
|
||||
|
||||
if ($result) {
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$jsonData[] = VisitorLogListHelper::formatListRow($row, $countries, $reasons);
|
||||
}
|
||||
$result->free();
|
||||
$map = array();
|
||||
$where = 1;
|
||||
|
||||
if(!empty($_GET['visit_date'])) {
|
||||
$map[] = "DATE_FORMAT(`visit_date`, '%Y-%m-%d') LIKE '" . cloak_db_escape($conn, $_GET['visit_date']) . "'";
|
||||
}
|
||||
|
||||
$where .= cloak_visitor_log_apply_result_filter($conn, $_GET['result'] ?? '');
|
||||
|
||||
if(!empty($_GET['country'])) {
|
||||
$map[] = "country LIKE '" . cloak_db_escape($conn, $_GET['country']) . "'";
|
||||
}
|
||||
|
||||
if(!empty($_GET['IP'])) {
|
||||
$map[] = "IP LIKE '" . cloak_db_escape($conn, $_GET['IP']) . "'";
|
||||
}
|
||||
|
||||
if(!empty($_GET['domain'])) {
|
||||
$map[] = "domain LIKE '%" . cloak_db_escape($conn, $_GET['domain']) . "%'";
|
||||
}
|
||||
|
||||
if(!empty($_GET['spage'])) {
|
||||
$map[] = "page LIKE '%" . cloak_db_escape($conn, $_GET['spage']) . "%'";
|
||||
}
|
||||
|
||||
if(!empty($map)) {
|
||||
$where .= " AND " . join(" AND ", $map);
|
||||
}
|
||||
// SQL语句
|
||||
$sql = "SELECT `id`, `campagin_id`, `visit_md5_code`, `domain`, `visit_date`, `IP`, `country`, `result`, `reason`, `fp_url`, `referer`, `vtimes`, `client`, `browser`, `page`, `language`, `user_agent`, `http_referer`, `accept_language_raw`, `judge_timing`, `fp_hdata`, `api_by_request`, `api_by_response`, `api_risk_request`, `api_risk_response`, `device`, `add_time` FROM `visitor_logs` WHERE {$where} ORDER BY `id` DESC LIMIT {$startPosition}, {$recordsPerPage}";
|
||||
|
||||
// 执行查询并输出结果
|
||||
$result = mysqli_query($conn, $sql);
|
||||
$jsonData = array();
|
||||
|
||||
while ($row = mysqli_fetch_assoc($result)) {
|
||||
//echo "<p>{$row["columnName"]}</p>"; // 根据需要修改列名
|
||||
if($row['vtimes'] == 0) {
|
||||
$row['vtimes'] = 1; // 还未统计访问次数
|
||||
$sqlVtimes = mysqli_query($conn, "SELECT count(*) as `total` FROM `visitor_logs` WHERE `IP` = '" . cloak_db_escape($conn, $row["IP"]) . "' AND `domain` = '" . cloak_db_escape($conn, $row["domain"]) . "' AND id < " . (int)$row['id']);
|
||||
$vtimesRow = mysqli_fetch_assoc($sqlVtimes);
|
||||
$vtimes = $vtimesRow["total"]; // 如果之前此IP有访问记录
|
||||
if($vtimes > 0) {
|
||||
$row['vtimes'] += $vtimes; // 访问次数叠加
|
||||
}
|
||||
|
||||
// 更新访问次数
|
||||
$updateSql = "UPDATE `visitor_logs` SET `vtimes`=" . (int)$row['vtimes'] . " WHERE id = " . $row['id'];
|
||||
mysqli_query($conn, $updateSql);
|
||||
}
|
||||
|
||||
$row['country'] = $countries[$row['country']];
|
||||
if(isset($reasons[$row['reason']])) {
|
||||
$row['reason'] = $reasons[$row['reason']];
|
||||
}
|
||||
if ($row['result'] === 'wait') {
|
||||
$row['result'] = '检测中';
|
||||
}
|
||||
$row['judge_timing_text'] = cloak_visitor_log_format_timing($row['judge_timing'] ?? '');
|
||||
$row['fp_hdata_text'] = cloak_visitor_log_format_fp_hdata($row['fp_hdata'] ?? '');
|
||||
if (!empty($row['fp_hdata'])) {
|
||||
$explain = cloak_visitor_log_fp_risk_explain(
|
||||
$row['fp_hdata'],
|
||||
$row['reason'] ?? '',
|
||||
$row['result'] ?? '',
|
||||
$row['domain'] ?? ''
|
||||
);
|
||||
$row['fp_hdata_explain_summary'] = $explain['summary'];
|
||||
$row['fp_hdata_explain_html'] = $explain['html'];
|
||||
$row['fp_hdata_explain'] = $explain['data'];
|
||||
} else {
|
||||
$row['fp_hdata_explain_summary'] = '';
|
||||
$row['fp_hdata_explain_html'] = '';
|
||||
$row['fp_hdata_explain'] = null;
|
||||
}
|
||||
$row['api_by_request_text'] = VisitorApiAudit::displayText($row['api_by_request'] ?? '');
|
||||
$row['api_by_response_text'] = VisitorApiAudit::displayText($row['api_by_response'] ?? '');
|
||||
$row['api_risk_request_text'] = VisitorApiAudit::displayText($row['api_risk_request'] ?? '');
|
||||
$row['api_risk_response_text']= VisitorApiAudit::displayText($row['api_risk_response'] ?? '');
|
||||
$jsonData[] = $row;
|
||||
}
|
||||
|
||||
$sqlCount = mysqli_query($conn, "SELECT count(*) as `total` FROM `visitor_logs` WHERE {$where}");
|
||||
$countRow = mysqli_fetch_assoc($sqlCount);
|
||||
|
||||
$jsonResult["code"] = 0;
|
||||
$jsonResult["msg"] = "";
|
||||
$jsonResult["count"] = VisitorLogListHelper::countWhere($conn, $where);
|
||||
$jsonResult["count"] = $countRow['total'];
|
||||
$jsonResult["data"] = $jsonData;
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭数据库连接
|
||||
mysqli_close($conn);
|
||||
|
||||
$conn->close();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode($jsonResult);
|
||||
exit;
|
||||
echo json_encode($jsonResult);exit;
|
||||
|
||||
Reference in New Issue
Block a user