-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.php
More file actions
126 lines (102 loc) · 3.08 KB
/
logging.php
File metadata and controls
126 lines (102 loc) · 3.08 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
if ($GLOBALS['is_my_file_browser_YES'] == true) {
function getClientIP() {
if (array_key_exists('HTTP_CF_CONNECTING_IP', $_SERVER)) {
return $_SERVER["HTTP_CF_CONNECTING_IP"];
}else if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
return $_SERVER["HTTP_X_FORWARDED_FOR"];
}else if (array_key_exists('REMOTE_ADDR', $_SERVER)) {
return $_SERVER['REMOTE_ADDR'];
}else if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) {
return $_SERVER['HTTP_CLIENT_IP'];
}
return '';
};
function is_whitelisted($clientIp) {
return in_array($clientIp, $GLOBALS['ip_whitelist'])==true;
}
function log_ip() {
$ip_whitelist = $GLOBALS['ip_whitelist'];
$clientIp = getClientIP();
$ipline = '';
if (!is_writable('$BASIC_IP_LOG_FILE$')) {
echo 'hell';
}
$ipf = fopen('$BASIC_IP_LOG_FILE$', 'a+');
$cursor = -1;
fseek($ipf, $cursor, SEEK_END);
$char = fgetc($ipf);
/**
* Trim trailing newline chars of the file
*/
while ($char === "\n" || $char === "\r") {
fseek($ipf, $cursor--, SEEK_END);
$char = fgetc($ipf);
}
/**
* Read until the start of file or first newline char
*/
while ($char !== false && $char !== "\n" && $char !== "\r") {
/**
* Prepend the new char
*/
$ipline = $char . $ipline;
fseek($ipf, $cursor--, SEEK_END);
$char = fgetc($ipf);
}
if (!is_whitelisted(getClientIP()) && !is_whitelisted($ipline) && $ipline !== $clientIp) {
fwrite($ipf, $clientIp . "\n");
} else if (in_array($ipline, $ip_whitelist)==true) {
$ipline = 'Whitelisted';
}
fclose($ipf);
return $ipline;
}
function log_activity($activity,$user,$files,$extra) {
echo("LOGGED");
if (is_writable('$EVENT_LOG_FILE$')) {
$log_fd = fopen('$EVENT_LOG_FILE$', 'a+');
if (!is_whitelisted(getClientIP())) {
fwrite($log_fd, $activity.'|'.$user.'|'.getClientIP() .'|'. date("Y-m-d H:i:s").'|'.implode(',',$files).'|'.$extra."\n");
}
fclose($log_fd);
} else {
echo 'Cannot write to logfile.';
}
}
// Allows the website to show recently uploaded files
// This is much easier than running a database
function appendEntryToRecents($text) {
$n = 6-1;
$file = '$RECENTS_FILE$';
$lines = file($file);
$keepLines = array_slice($lines,0,$n);
if (!in_array($file,$keepLines)) {
$fd = fopen($file,'w');
fwrite($fd,$text."\n");
foreach ($keepLines as $line) {
fwrite($fd,$line);
}
fclose($fd);
}
}
function getRecents() {
$file = '$RECENTS_FILE$';
return array_map(function ($x) {return trim($x);}, file($file));
}
function removeEntryFromRecents($text) {
$file = '$RECENTS_FILE$';
$lines = file($file);
$fd = fopen($file,'w');
foreach ($keepLines as $line) {
if (trim($line)!=$text) {
fwrite($fd,$line);
}
}
fclose($fd);
}
}
else {
?><h1>Access forbidden.</h1> <?php
}
?>