· 5 years ago · Aug 19, 2020, 01:24 AM
1<?php
2
3
4// For now file storage requires that the file specified is created already and chmodded to allow writing.
5
6
7/**
8
9* Connection info logging script created by Xeru
10
11*
12
13* Website: https://xeru.me
14
15* Twitter: https://twitter.com/Xeru_
16
17* GitHub: https://github.com/exec
18
19*
20
21*/
22
23
24
25// Message user sees. Set to "" if you don't want to display a message, ideal for including in external scripts.
26
27$message = "<h1>404 Not Found</h1>";
28
29
30// Set variables
31
32$date = new DateTime(); // Date+time variable
33
34$rdate = $date->format('Y-m-d H:i:s'); // Normalizing it
35
36$protocol = $_SERVER['SERVER_PROTOCOL']; // what protocol the client is connecting from
37
38$ip = $_SERVER['REMOTE_ADDR']; // the IP address of the connecting client.
39
40
41
42// If connecting through Cloudflare, rely on Cloudflare's connecting-IP header to get IP, otherwise check server header.
43
44if(!isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
45
46 $ip = $_SERVER['REMOTE_ADDR'];
47
48} else {
49
50 $ip = '[cloudflare reports] '.$_SERVER['HTTP_CF_CONNECTING_IP'];
51
52}
53
54
55
56$port = $_SERVER['REMOTE_PORT']; // connecting port of the client. Not useful usually but can be interesting.
57
58$agent = $_SERVER['HTTP_USER_AGENT']; // Unreliable to find a connecting client's browser, but can be useful sometimes.
59
60$href = $_SERVER['HTTP_REFERER']; // If you're linking this to someone directly, it will usually be nothing.
61
62$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']); // Attempt to resolve hostname from IP address.
63
64
65$type = "mysql"; // "file" or "mysql"
66
67
68// MySQL storage config
69
70$db_host = 'localhost';
71
72$db_username = 'records';
73
74$db_password = 'records';
75
76$db_database = 'records';
77
78
79// file storage config
80
81$file = "log.txt"; // Should rename this in case someone looks for the log file. Or protect with htaccess.
82
83
84
85// If you don't know what you're doing leave this alone pls
86
87if(!empty($type) && $type == "file") {
88
89 if(is_writable($file)) {
90
91 $fh = fopen($file, 'a');
92
93 if(filesize($file) < 32) {
94
95 fwrite($fh, " _____________________\n");
96
97 fwrite($fh, "| |\n");
98
99 fwrite($fh, "| PHP LOGGER BY XERU |\n");
100
101 fwrite($fh, "|_____________________|\n\n");
102
103 fwrite($fh, " BEGIN LOGFILE ".$file."\n");
104
105 fwrite($fh, " _____________________\n");
106
107 fwrite($fh, "|\n");
108
109 }
110
111 fwrite($fh, '| Connection from '.$ip.' at '.$date->format('Y-m-d H:i:s')."\n");
112
113 fwrite($fh, '| Hostname: '."".$hostname ."\n");
114
115 fwrite($fh, '| Port: '."".$port ."\n");
116
117 fwrite($fh, '| User Agent: '."".$agent ."\n");
118
119 fwrite($fh, '| HTTP Referer: '."".$href ."\n");
120
121 fwrite($fh, "|_____________________\n");
122
123 fclose($fh);
124
125 echo $message;
126
127 } else {
128
129 chmod($file, 0777); // attempt to chmod the file to make it writable.
130
131 die("<pre>Content could not load. Please try again in a few seconds.\n");
132
133 die("If you are the owner of this site, please adjust file permissions to allow writing to the file specified in config.</pre>");
134
135 }
136
137} elseif(!empty($type) && $type == "mysql") {
138
139 $sqlconn = mysqli_connect($db_host, $db_username, $db_password, $db_database);
140
141 if (mysqli_connect_errno()) {
142
143 printf("Could not connect to MySQL database: %s\n", mysqli_connect_error());
144
145 exit();
146
147 }
148
149 mysqli_query($sqlconn, "CREATE TABLE IF NOT EXISTS records.`logs` (
150
151IP TEXT( 16 ) NOT NULL,
152
153DATE TEXT( 30 ) NOT NULL,
154
155HOSTNAME TEXT( 255 ) NOT NULL,
156
157PORT INT( 6 ) NOT NULL,
158
159USERAGENT TEXT( 255 ) NOT NULL,
160
161HTTPREFERER TEXT( 255 ) NOT NULL
162
163)");
164
165 mysqli_query($sqlconn, "INSERT INTO records.`logs` VALUES (
166
167'$ip',
168
169'$rdate',
170
171'$hostname',
172
173'$port',
174
175'$agent',
176
177'$href'
178
179)");
180
181 mysqli_close($sqlconn);
182
183 echo $message;
184
185}
186
187