-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogger.php
More file actions
93 lines (73 loc) · 3.4 KB
/
logger.php
File metadata and controls
93 lines (73 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?
Logger::init();
class Logger {
private static $db = null;
private static $init = false;
private static $destructor = null;
private static $start_time = null;
private static $cache = array();
private static $sessid = null;
public static function init() {
self::$start_time = microtime(true);
if (self::$destructor == null) self::$destructor = new LoggerDestructor();
}
public static function log($type, $data) {
self::$cache[] = array("data" => $data, "time" => microtime(true), "type" => $type);
}
private static function query() {
$args = func_get_args();
$sql = array_shift($args);
foreach ($args as $key => $value) $args[$key] = self::clean($value);
$res = self::$db->query(vsprintf($sql, $args));
if (!$res) {
if (strstr(self::$db->error, "MySQL server has gone away")) {
echo "MySQL server timeout, reconnecting\n";
self::$db = null;
self::connect();
$res = self::$db->query(vsprintf($sql, $args));
} else die("Logger: MySQLi Error: " . self::$db->error);
}
return $res;
}
private static function clean($string) {
return self::$db->real_escape_string(trim($string));
}
private static function connect() {
if (self::$db == null) {
include(dirname(__FILE__) . '/config.php');
self::$db = new mysqli($config["database"]["host"], $config["database"]["user"], $config["database"]["pass"], $config["database"]["db"]);
if (mysqli_connect_errno()) die("Unable to connect to Logging SQL Database: " . mysqli_connect_error());
}
}
public static function destruct() {
self::store();
$user = LanWebsite_Main::getUserManager()->getActiveUser();
if (self::$sessid != null) {
self::query("UPDATE logger_sessions SET user_id = '%s', end_time = '%s' WHERE logger_session_id = '%s'", $user->getUserId(), microtime(true), self::$sessid);
}
}
public static function store() {
if (count(self::$cache) > 0) {
//Connect
self::connect();
if (self::$sessid == null) {
if (isset($_SERVER["REQUEST_URI"])) {
$url = $_SERVER["REQUEST_URI"];
} else $url = "";
self::query("INSERT INTO `logger_sessions` (url, lan_number, start_time) VALUES ('%s', '%s', '%s')", $url, LanWebsite_Main::getSettings()->getSetting("lan_number"), self::$start_time);
self::$sessid = self::$db->insert_id;
}
foreach (self::$cache as $log) {
self::query("INSERT INTO logger_entries (logger_session_id, type, time, data) VALUES ('%s', '%s', '%s', '%s')", self::$sessid, $log["type"], $log["time"], $log["data"]);
}
self::$cache = array();
self::init();
}
}
}
class LoggerDestructor {
public function __destruct() {
Logger::destruct();
}
}
?>