Log class created and implemented

This commit is contained in:
sedivy.miro 2016-12-28 04:48:30 -05:00
parent 8208f506aa
commit 6d2f56a39f
4 changed files with 31 additions and 9 deletions

View file

@ -2,8 +2,7 @@
include 'common.php';
function error($msg){
if(Config::get_safe("logs", false))
file_put_contents('logs/ajax_errors.log', date('Y-m-d H:i:s')."\t".$_SERVER["REMOTE_ADDR"]."\t".$_SERVER["HTTP_USER_AGENT"]."\t".$msg.PHP_EOL, FILE_APPEND);
Log::put("ajax_errors", $msg);
header('Content-Type: application/json');
echo json_encode(["error" => true, "msg" => $msg]);
exit;
@ -31,8 +30,7 @@ $f = ['Post', @$r["action"]];
// If method exists
if(is_callable($f)){
$c = call_user_func($f, $r);
if(Config::get_safe("logs", false))
file_put_contents('logs/ajax_access.log', date('Y-m-d H:i:s')."\t".$_SERVER["REMOTE_ADDR"]."\t".$_SERVER["HTTP_USER_AGENT"]."\t".@$r["action"].PHP_EOL, FILE_APPEND);
Log::put("ajax_access", @$r["action"]);
} else {
error("Method was not found.");
}

View file

@ -12,8 +12,7 @@ if(empty($_SESSION['token'])){
//$.ajaxSetup({headers:{'Csrf-Token':'token'}});
if(Config::get_safe("logs", false))
file_put_contents('logs/visitors.log', date('Y-m-d H:i:s')."\t".$_SERVER["REMOTE_ADDR"]."\t".$_SERVER["HTTP_USER_AGENT"].PHP_EOL, FILE_APPEND);
Log::put("visitors");
?><!DOCTYPE html>
<html>
<head>

27
lib/log.class.php Normal file
View file

@ -0,0 +1,27 @@
<?php
class Log
{
private static $_files = [
"ajax_access",
"ajax_errors",
"login_fails",
"visitors"
];
private static $_path = 'logs/';
public static function put($_file, $_text = null){
if(!Config::get_safe("logs", false) || !in_array($_file, static::$_files)){
return ;
}
if(false === file_put_contents(PROJECT_PATH.DIRECTORY_SEPARATOR.static::$_path.$_file.".log", self::line($_text), FILE_APPEND)){
trigger_error("Can't write to {$_file}.log file.", E_USER_NOTICE);
}
}
private static function line($_text = null){
return date('Y-m-d H:i:s')."\t".$_SERVER["REMOTE_ADDR"]."\t".$_SERVER["HTTP_USER_AGENT"].($_text ? "\t".$_text : "").PHP_EOL;
}
}

View file

@ -378,9 +378,7 @@ class Post
return ["error" => false];
}
if(Config::get_safe("logs", false))
file_put_contents('logs/login_fails.log', date('Y-m-d H:i:s')."\t".$_SERVER["REMOTE_ADDR"]."\t".$_SERVER["HTTP_USER_AGENT"]."\t".$r["nick"].PHP_EOL, FILE_APPEND);
Log::put("login_fails", $r["nick"]);
return ["error" => true, "msg" => "The nick or password is incorrect."];
}