forked from J0hnHawk/FS19_WebStats_Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cronUserHistory.php
145 lines (134 loc) · 4.72 KB
/
cronUserHistory.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
<?php
/**
* This file is part of the "FS19 Web Stats" package.
* Copyright (C) 2019 John Hawk <john.hawk@gmx.net> and JanSe <admin@fs19.cz>
*
* "FS19 Web Stats" is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "FS19 Web Stats" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
ini_set ( 'error_reporting', E_ALL );
ini_set ( 'display_errors', 0 );
ini_set ( 'log_errors', 1 );
ini_set ( 'error_log', 'error.log' );
// -----------------------------------------------------------------------------
// Settings
$logFile = './onlineHistory.xml';
$configFile = '../config/webStatsConfig.xml';
$logLevel = 2; // 0 = quiet; 1 = default; 2 = extra
// -----------------------------------------------------------------------------
// Variables
$changeFound = false;
$onlineUser = array ();
function logEntry($level, $entry) {
global $logLevel;
$nl = (PHP_SAPI == 'cli') ? "\n" : "<br>\n";
if ($logLevel >= $level) {
echo (date ( 'Y-m-d H:i:s - ' ) . $entry . $nl);
}
}
// Read FS19 Web Stats config file
logEntry ( 1, "Start user logging." );
if (file_exists ( $configFile )) {
$webStatsConfig = simplexml_load_file ( $configFile );
if (! isset ( $webStatsConfig->webStatsVersion )) {
logEntry ( 1, "Configuration file too old." );
exit ( 1 );
}
if ($webStatsConfig->savegame_type == 'local') {
logEntry ( 1, "No user logging for local savegames." );
exit ( 1 );
}
} else {
logEntry ( 1, "Configuration file does not exists." );
exit ( 1 );
}
// Get online user from dedicated server
$ch = curl_init ();
curl_setopt_array ( $ch, array (
CURLOPT_URL => $webStatsConfig->link_xml,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 10
) );
$xmlStr = curl_exec ( $ch );
$http_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
curl_close ( $ch );
if ($http_code != 200) {
logEntry ( 1, "Dedicated Server unreachable or code incorrect." );
exit ( 1 );
}
logEntry ( 1, "Online status downloaded from server." );
$stats = simplexml_load_string ( $xmlStr );
foreach ( $stats->Slots->Player as $player ) {
if ($player ["isUsed"] == "true") {
$minutesOnline = floor ( $player ["uptime"] );
$onlineUser [strval ( $player )] = $minutesOnline;
}
}
// Read log file or create a new one
if (file_exists ( $logFile )) {
$logFileXML = simplexml_load_file ( $logFile );
$storedOnlineUser = $logFileXML->onlineUser;
$logEnties = $logFileXML->logEntries;
logEntry ( 2, "Log file loaded." );
} else {
$logFileXML = new SimpleXMLElement ( '<?xml version="1.0" encoding="UTF-8"?><log></log>' );
$storedOnlineUser = $logFileXML->addChild ( 'onlineUser' );
$logEnties = $logFileXML->addChild ( 'logEntries' );
logEntry ( 2, "New log file created." );
$changeFound = true;
}
// User still online?
foreach ( $storedOnlineUser->user as $user ) {
$name = strval ( $user ['name'] );
if (! isset ( $onlineUser [$name] )) {
$logEntry = $logEnties->addChild ( 'logEntry' );
$logEntry->addAttribute ( 'name', $name );
$logEntry->addAttribute ( 'type', 'offline' );
$logEntry->addAttribute ( 'since', date ( "Y-m-d H:i", time () ) );
$dom = dom_import_simplexml ( $user );
$dom->parentNode->removeChild ( $dom );
logEntry ( 2, "The user '$name' is no longer online." );
$changeFound = true;
} else {
$lastRunOnlineUser [$name] = strval ( $user ['onlineSince'] );
}
}
// New user online?
foreach ( $onlineUser as $name => $minutesOnline ) {
if (! isset ( $lastRunOnlineUser [$name] )) {
$onlineSince = date ( "Y-m-d H:i", time () - $minutesOnline * 60 );
$logEntry = $logEnties->addChild ( 'logEntry' );
$logEntry->addAttribute ( 'name', $name );
$logEntry->addAttribute ( 'type', 'online' );
$logEntry->addAttribute ( 'since', $onlineSince );
$user = $storedOnlineUser->addChild ( 'user' );
$user->addAttribute ( 'name', $name );
$user->addAttribute ( 'onlineSince', $onlineSince );
logEntry ( 2, "User '$name' is online since: " . $onlineSince );
$changeFound = true;
}
}
// Save if changes found
if ($changeFound) {
$dom = new DOMDocument ( '1.0' );
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML ( $logFileXML->asXML () );
if ($dom->save ( $logFile )) {
logEntry ( 1, "Log file saved." );
} else {
logEntry ( 1, "Log file could not saved." );
}
} else {
logEntry ( 1, "No change found." );
}