上传日志

This commit is contained in:
icret
2021-11-14 23:25:21 +08:00
parent 6169e582a7
commit 7b519752d0
9 changed files with 166 additions and 21 deletions

View File

@@ -31,7 +31,7 @@ function getLatelyTime($type = '')
$total_contents = APP_ROOT . $config['path']; // 获取用户自定义的上传目录
$chart_total_file_md5 = strval(md5_file(APP_ROOT . '/config/config.php')); // 以config.php文件的md5命名
$chart_total_file = $total_contents . "cache/chart-$chart_total_file_md5.php"; // 文件绝对目录
$chart_total_file = APP_ROOT . "/admin/logs/counts/chart-$chart_total_file_md5.php"; // 文件绝对目录
function write_chart_total()
{
@@ -54,7 +54,7 @@ function write_chart_total()
// 统计每日占用空间
$count_contents['chart_disk'][] = [$count_day[$i] => getDirectorySize($total_contents . $count_day[$i])];
}
$count_contents = json_encode($count_contents, true); // serialize存储文件
file_put_contents($chart_total_file, $count_contents); // 存储文件
}

View File

@@ -16,9 +16,9 @@ class getVerson
public function readJson()
{
if (file_exists(__DIR__ . '/../i/cache/verson.json')) {
$file = fopen(__DIR__ . '/../i/cache/verson.json', 'r');
$test = fread($file, filesize(__DIR__ . '/../i/cache/verson.json'));
if (file_exists(__DIR__ . '/../admin/logs/verson/verson.json')) {
$file = fopen(__DIR__ . '/../admin/logs/verson/verson.json', 'r');
$test = fread($file, filesize(__DIR__ . '/../admin/logs/verson/verson.json'));
$verson = json_decode($test, true);
return $verson['tag_name'];
fclose($file);
@@ -32,7 +32,7 @@ class getVerson
$verson = $this->geturl($this->url);
$verson = json_decode($verson, true);
$file = fopen(__DIR__ . '/../i/cache/verson.json', 'w+');
$file = fopen(__DIR__ . '/../admin/logs/verson/verson.json', 'w+');
fwrite($file, $verson);
fclose($file);
}

View File

@@ -34,8 +34,7 @@ function total_files($file)
$total_file_path = APP_ROOT . $config['path']; // 获取用户自定义的上传目录
$totalJsonMD5 = strval(md5_file(APP_ROOT . '/config/config.php')); // 以config.php文件的md5命名
$totalJsonName = $total_file_path . "cache/total-files-$totalJsonMD5.php"; // 文件绝对目录
$totalJsonName = APP_ROOT . "/admin/logs/counts/total-files-$totalJsonMD5.php"; // 文件绝对目录
function creat_json() // 创建json文件
{

85
application/write-log.php Executable file
View File

@@ -0,0 +1,85 @@
<?php
require_once __DIR__ . '/function.php';
/**
* 获得用户的真实IP地址
* <br />来源ecshop
* <br />$_SERVER和getenv的区别getenv不支持IIS的isapi方式运行的php
* @access public
* @return string
*/
function real_ip()
{
static $realip = NULL;
if ($realip !== NULL) {
return $realip;
}
if (isset($_SERVER)) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
/* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
foreach ($arr as $ip) {
$ip = trim($ip);
if ($ip != 'unknown') {
$realip = $ip;
break;
}
}
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$realip = $_SERVER['HTTP_CLIENT_IP'];
} else {
if (isset($_SERVER['REMOTE_ADDR'])) {
$realip = $_SERVER['REMOTE_ADDR'];
} else {
$realip = '0.0.0.0';
}
}
} else {
if (getenv('HTTP_X_FORWARDED_FOR')) {
$realip = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('HTTP_CLIENT_IP')) {
$realip = getenv('HTTP_CLIENT_IP');
} else {
$realip = getenv('REMOTE_ADDR');
}
}
// 使用正则验证IP地址的有效性防止伪造IP地址进行SQL注入攻击
preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
$realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
return $realip;
}
/**
* 写日志
* 日志格式:图片名称->上传时间(北京时间)->IP地址->浏览器信息->文件相对路径->cache文件相对路径
*/
function write_log($file, $cacheFile = null)
{
global $config;
$name = trim(basename($file), " \t\n\r\0\x0B"); // 图片名称
$log = array($name => array(
'date' => date('Y-m-d H:i:s'), // 上传日期
'ip' => real_ip(), // 上传ip
'user_agent' => $_SERVER['HTTP_USER_AGENT'], //浏览器信息
'path' => $file, // 文件相对路径
'cache' => $cacheFile, // 文件缓存相对位置
));
$logFileName = APP_ROOT . '/admin/logs/upload/' . date('Y-m') . '.php';
// 写入禁止浏览器直接访问
if (is_file($logFileName) == false) {
$php_code = '<?php exit;?>';
file_put_contents($logFileName, $php_code);
}
$log = json_encode($log, true);
file_put_contents($logFileName, PHP_EOL . $log, FILE_APPEND | LOCK_EX);
}
/*
for ($i = 0; $i < 100000; $i++) {
write_log('/i/2021/11/13/12der8s.jpg', '/i/cache/2021_11_13_12der8s.jpg');
}
*/