-
Notifications
You must be signed in to change notification settings - Fork 22
/
minecraft.php
392 lines (327 loc) · 11.5 KB
/
minecraft.php
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?PHP
/**
* Minecraft server script
*
* This class can take a Minecraft server log and parse it for data such as
* online users, chat logs, total time logged in and more!
*
* @category Minecraft
* @package Minecraft_Server_Script
* @subpackage Minecraft_Class
* @copyright Copyright (c) Jaryth Frenette 2012, hfuller 2011, caseypugh, 2011
* @license Open Source - Anyone can use, modify and redistribute as wanted
* @version Release: 1.0
* @link http://jaryth.net
*/
//Global settings:
//Set your time zone to make sure calculations are correct!
//Time zone should match whatever time your Minecraft server runs on.
//List of Timezones can be found at: http://php.net/manual/en/timezones.php
date_default_timezone_set('America/Winnipeg');
class minecraft{
//User Settings:
//Change the following options to reflect how you want the class to work
//Enable or disable log caching. true = enabled, false = disabled
//Note: Disabling does not delete an existing cache if one exists
//Default: true
var $cacheEnable = true;
//Enable or disable avatar caching. true = enabled, false = disabled
//Note: Disabling does not delete an existing cache if one exists
//Default: true
var $cacheAvatar = true;
//Set the name of the cache folder. Ignored if caching is disabled above.
//Note: Changing the cache folder does not delete the old one if it exists
//Note: You will need to also change this setting in the avatar.php file
//Default 'cache'
var $cacheFolder = 'cache';
//Set the timeout limit for cache data
//Default '60' (60 seconds = 1 minute, 300 = 5 minutes, 3600 = 1 hour)
var $cacheTime = '60';
//Set up initial variables
var $users = array();
var $chat = array();
var $log = '';
var $cache = '';
//<-- Class Constructor :: Saves log location, checks for cache and generation //Constructor
function minecraft($logLocation){
//Verify the log file exists
if(is_file($logLocation)){
//Save the location and continue
$this->log = $logLocation;
}else{
//Rerun false and cancel the rest of the class if it does not.
return false;
}
//Check if the Cache is enabled
if($this->cacheEnable){
//Check cache status (load and generate call are in this function)
$this->cache();
}else{
//Or generate content
$this->parseLog();
}
}
//--> End of construct()
//<-- parseLog :: Parses though the server log. This is the primary function
function parseLog(){
//set up the log
$file = file_get_contents($this->log);
$logs = explode("\n", $file);
//Parse though the log for all of the information we need
foreach ($logs as $l){
//Check for users chatting, set them online, log their chat
if (preg_match("/([0-9-]+ [0-9:]+) \[INFO\] \<([a-zA-Z0-9-_]+)\> (.*)/i", $l, $m))
$this->online($m[2], $m[1], 0, $m[3]);
//check for users entering the server, set them online
else if (preg_match("/([0-9-]+ [0-9:]+) \[INFO\] ([a-zA-Z0-9-_]+) ?\[.*logged in with entity/i", $l, $m))
$this->online($m[2], $m[1], 1);
//Check for users leaving, set them as offline
else if (preg_match("/([0-9-]+ [0-9:]+) \[INFO\] ([a-zA-Z0-9-_]+) lost connection/i", $l, $m))
$this->offline($m[2], $m[1]);
//Check if server shut down, log off all users
else if (preg_match("/([0-9-]+ [0-9:]+) \[INFO\] Stopping server/i", $l, $m))
$this->server_quit($m[1]);
}
//Finally we sort the users
$this->sortUsers();
//Save the cache data if its enabled
if($this->cacheEnable){
$this->saveCache();
}
}
//--> End of parseLog
// -- User Stuff :: Functions dedicated to user related tasks -- //User Stuff
//<-- add_user :: Adds a user to the array, sets default settings
function add_user($name, $state, $time){
//If Avatar Caching is enabled, set cache name
if($this->cacheAvatar){
$avatar = "/avatar.php?name={$name}&size=40&cache=1";
}else{
$avatar = "/avatar.php?name={$name}&size=40";
}
//Enter user data into array
$this->users[$name] = array(
'name' => $name,
'online' => $state,
'logcount' => 1,
'avatar' => $avatar,
'time' => $time,
'lastonline' => $time,
'totaltime' => 0
);
}
//--> End of add_user()
//<-- online :: Sets a user to 'online' and saves the time. and their chat log
function online($name, $time, $log=0, $chat=false){
//This creates a chat log, just adds the string into the array
if($chat){
$this->chat[] = $name . " said: " . $chat . "<br>\n";
}
//Check to see if the user exists yet, and changes their status if they do
if(array_key_exists($name, $this->users)){
if($log == 1){
//Increase total logon count, and set last log time.
$this->users[$name]['logcount']++;
$this->users[$name]['lastonline'] = $time;
}
//set user to online and set the time they where last seen
$this->users[$name]['online'] = true;
$this->users[$name]['time'] = $time;
return true;
}
//if a user does not exist, add them to the users
$this->add_user($name, true, $time);
}
//--> End of online()
//<-- offline :: Sets a user to 'offline' and calculates session time
function offline($name, $time = false, $shutDownTime = false){
//Check to see if the user exists yet, and changes their status if they do
if(array_key_exists($name, $this->users)){
//Set user to 'offline'
$this->users[$name]['online'] = false;
//If the time flag was set:
if($time){
//set the time they went offline
$this->users[$name]['time'] = $time;
//calculate session time and add it to their total.
if($this->users[$name]['lastonline'] > 0){
$this->users[$name]['totaltime'] += strtotime($time) - strtotime($this->users[$name]['lastonline']);
$this->users[$name]['lastonline'] = 0;
}
}
//calculate session time and add it to their total.
if($shutDownTime){
if($this->users[$name]['lastonline'] > 0){
$this->users[$name]['totaltime'] += strtotime($shutDownTime) - strtotime($this->users[$name]['lastonline']);
$this->users[$name]['lastonline'] = 0;
}
}
return true;
}
//if a user does not exist, add them to the users
$this->add_user($name, false, $time);
}
//--> End of offline()
//<-- server_quit :: Logs off all users when the server shuts down.
function server_quit($time){
//Loop though all users and change them all to offline
foreach($this->users as $user){
$this->offline($user['name'], false, $time);
}
}
//--> End of server_quit()
//<-- sortUsers :: Gets the user list and sorts it
function sortUsers(){
uasort($this->users, array($this,"cmp"));
//if 'total' is set, sort by total time spent on server instead of default
if(isset($_GET['total'])){
uasort($this->users, array($this,"cmpTime"));
}
}
//--> End of sortUsers()
// -- Cache Manipulation :: Functions dedicated to cache related tasks -- //Cache Manipulation
//<-- cache :: Checks if cache exists, and how old it is
function cache(){
//Create the cache folder if need
if(!is_dir($this->cacheFolder)){
mkdir($this->cacheFolder);
}
//This sets the cache location
$this->cache = $this->cacheFolder . DIRECTORY_SEPARATOR . md5($this->log) . '.cache';
//Verify the cache file exists
if(is_file($this->cache)){
//If it does exist, read the file off disk
$cache = file($this->cache);
//Set the time difference to see how old the cache is
$timeDiffrence = mktime() - $cache[0];
//Check to see how old the cache is
if($timeDiffrence < $this->cacheTime){
//If its less than cacheTime then load the data into the users array
$this->users = unserialize($cache[1]);
}else{
//If its too old, generate fresh one
$this->parseLog();
}
//If file does not exist, create it
}else{
$this->parseLog();
}
}
//--> End of cache
//<-- saveCache :: This function serializes the cache data and saves it to disk
function saveCache(){
//Set cache generation time for tracking later
$cache = mktime() . "\n" . serialize($this->users);
//Create the file and write the data
$cacheFile = fopen($this->cache, 'w');
fwrite($cacheFile, $cache);
fclose($cacheFile);
}
//--> End of saveCache
// -- Time Manipulation :: Functions dedicated to time related tasks -- //Time Manipulation
//<-- getTimeAgo :: Calculates how much time has passed
function getTimeAgo($datetime, $skip = 0){
//make sure time is not empty
if(trim($datetime) == ""){
return false;
}
//Sets the time difference. Make sure your time zone is correct!
$datediff = strtotime('now') - strtotime($datetime);
//if Skip is set, will calculate time without timezone.
if($skip == 1){
$datediff = $datetime;
}
//Break down the different times
$min = round($datediff / 60);
$hours = round($datediff / (60 * 60));
$days = round($datediff / (60 * 60 * 24));
$months = round($datediff / (60 * 60 * 24 * 31));
$years = round($datediff / (60 * 60 * 24 * 365));
//we don't want to say "ago" so we can use this for online also
if($datediff < 60){ // seconds
if($datediff == 0) return "just now";
return "$datediff second".$this->pluralizer($datediff > 1);// . " ago";
}
else if($min < 60){
return "$min minute".$this->pluralizer($min>1);//." ago";
}
else if($hours < 24){
return "$hours hour".$this->pluralizer($hours>1);//." ago";
}
else if($days < 31){
return "$days day".$this->pluralizer($days>1);//." ago";
}
else if($months < 12){
return "$months month".$this->pluralizer($months>1);//." ago";
}
else {
return "$years year".$this->pluralizer($years>1);//." ago";
}
return false;
}
//--> End of gatTimeAgo()
//<-- pluralizer :: Will add 's to the ends of numbers not ending in 1.
function pluralizer($bln, $suffix='s'){
return $bln ? $suffix : '';
}
//--> End of pluralizer()
//<-- Sec2Time :: Turns Seconds to Year, Day, Hours, Minutes, Seconds format.
function Sec2Time($time){
if(is_numeric($time)){
$value = array(
"years" => 0, "days" => 0, "hours" => 0,
"minutes" => 0, "seconds" => 0,
);
$string = "";
if($time >= 31556926){
$value["years"] = floor($time/31556926);
$string .= $value["years"] . " Years, ";
$time = ($time%31556926);
}
if($time >= 86400){
$value["days"] = floor($time/86400);
$string .= $value["days"] . " days, ";
$time = ($time%86400);
}
if($time >= 3600){
$value["hours"] = floor($time/3600);
$string .= $value["hours"] . " hours, ";
$time = ($time%3600);
}
if($time >= 60){
$value["minutes"] = floor($time/60);
$string .= $value["minutes"] . " minutes, ";
$time = ($time%60);
}
$value["seconds"] = floor($time);
$string .= $value["seconds"] . " seconds ";
return $string;
}else{
return FALSE;
}
}
//--> End of Sec2Time()
// -- Misc Functions :: Functions for doing random other tasks -- //Misc Functions
//<-- cmp :: Primary sorting function, orders by time and name
function cmp($a, $b){
if ( $a['online'] ) {
if ( $b['online'] ) { //both online - alphabetically
return strtotime($a['time']) - strtotime($b['time']);
} else { // only a is online - it comes first
return -1;
}
} else if ( $b['online'] ) { //only b is online - comes first
return 1;
} else {// both offline - the one that was most recently on comes first
return strtotime($b['time']) - strtotime($a['time']);
}
}
//--> End of cmp()
//<-- cmpTime :: Secondary sorting function, only sorts by total time online
function cmpTime($a, $b){
return $b['totaltime'] - $a['totaltime'];
}
//->> End of cmpTime()
}
//--> End minecraft class
?>