m1k1oblog/app/log.class.php

40 lines
1,017 B
PHP
Raw Permalink Normal View History

2016-12-28 09:48:30 +00:00
<?php
defined('PROJECT_PATH') OR exit('No direct script access allowed');
2016-12-28 09:48:30 +00:00
class Log
{
private static $_files = [
"ajax_access",
"ajax_errors",
"login_fails",
"visitors"
];
2019-12-20 17:38:48 +00:00
2016-12-28 09:48:30 +00:00
public static function put($_file, $_text = null){
if(!Config::get_safe("logs", false) || !in_array($_file, static::$_files)){
return ;
}
2019-09-02 15:49:17 +00:00
$_logs_path = Config::get('logs_path');
if(!is_dir($_logs_path) && !mkdir($_logs_path, 755, true)){
die("Logs directory could not be created.");
}
if(false === file_put_contents($_logs_path.$_file.".log", self::line($_text), FILE_APPEND) && Config::get_safe('debug', false)){
2019-09-02 15:49:17 +00:00
die(sprintf("Can't write to %s.log file.", $_file));
2016-12-28 09:48:30 +00:00
}
}
2019-12-20 17:38:48 +00:00
2019-12-22 21:14:09 +00:00
private static function escape($_text = null){
return preg_replace("/[\n\r\t]/", "-", $_text);
}
2016-12-28 09:48:30 +00:00
private static function line($_text = null){
2019-12-22 21:14:09 +00:00
return trim(
date('Y-m-d H:i:s')."\t".
self::escape($_SERVER["REMOTE_ADDR"])."\t".
self::escape($_SERVER["HTTP_USER_AGENT"])."\t".
self::escape($_text)
).PHP_EOL;
2016-12-28 09:48:30 +00:00
}
}