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
|
<?php
class Page_Statistics_Reporting extends Page
{
private $action;
private $type;
// "Constants"
/**
* @var int
*/
private $from;
/**
* @var int
*/
private $to;
/**
* @var int
*/
private $lower;
/**
* @var int
*/
private $upper;
/**
* @var array Names of columns that are being used by the various tables
*/
private $COLUMNS = array('locationname', 'totalTime', 'medianSessionLength', 'sessions', 'longSessions', 'shortSessions',
'totalOffTime', 'totalStandbyTime', 'totalSessionTime', 'totalIdleTime', 'lastLogout', 'lastStart');
/**
* @var array Names of the tables we can display
*/
private $TABLES = array('total', 'location', 'client', 'user', 'vm');
/**
* Called before any page rendering happens - early hook to check parameters etc.
*/
protected function doPreprocess()
{
User::load();
if (!User::isLoggedIn()) {
Message::addError('main.no-permission');
Util::redirect('?do=Main'); // does not return
}
$this->action = Request::any('action', 'show', 'string');
$this->type = Request::get('type', 'total', 'string');
// Format: yyyy-mm-dd
$fromString = Request::get('from', '-6 days', 'string');
$toString = Request::get('to', 'today', 'string');
$this->lower = Request::get('lower', 8, 'int');
$this->upper = Request::get('upper', 20, 'int');
if (!in_array($this->type, $this->TABLES)) {
Message::addError('invalid-table-type', $this->type);
$this->type = 'total';
}
// convert from/to dates to unixtime
$this->from = min(strtotime($fromString.' 00:00:00'), time());
$this->to = min(strtotime($toString.' 23:59:59'), time());
// timespan you want to see. default = last 7 days
GetData::$from = $this->from;
GetData::$to = $this->to;
GetData::$lowerTimeBound = $this->lower;
GetData::$upperTimeBound = $this->upper;
/*
GetData::$from = strtotime("2017-12-01 00:00:00");
GetData::$to = strtotime("2017-12-15 00:00:00");
GetData::$lowerTimeBound = 1;
GetData::$upperTimeBound = 2;
*/
// Export - handle in doPreprocess so we don't render the menu etc.
if ($this->action === 'export') {
if (User::hasPermission("table.export") && User::hasPermission("table.view.$this->type")) {
$this->doExport();
// Does not return
}
}
// Get report - fetch data exactly the way it would automatically be reported
// so the user can know what is going on
if ($this->action === 'getreport') {
if(User::hasPermission("reporting.download")) {
$report = RemoteReport::generateReport(time());
Header('Content-Disposition: attachment; filename=remote-report.json');
Header('Content-Type: application/json; charset=utf-8');
die(json_encode($report));
}
}
User::assertPermission('*');
}
/**
* Menu etc. has already been generated, now it's time to generate page content.
*/
protected function doRender()
{
if ($this->action === 'show') {
/*
* Leave these here for the translate module
* Dictionary::translate('table_total');
* Dictionary::translate('table_location');
* Dictionary::translate('table_client');
* Dictionary::translate('table_user');
* Dictionary::translate('table_vm');
*/
$data = array(
'columns' => array(),
'tables' => array(),
'days' => array()
);
$forceOn = (Request::get('type') === false);
foreach ($this->COLUMNS as $column) {
$data['columns'][] = array(
'id' => 'col_' . $column,
'name' => Dictionary::translateFile('template-tags', 'lang_' . $column),
'checked' => ($forceOn || Request::get('col_' . $column, 'off', 'string') !== 'off') ? 'checked' : '',
);
}
foreach ($this->TABLES as $table) {
$data['tables'][] = array(
'name' => Dictionary::translate('table_' . $table),
'value' => $table,
'allowed' => User::hasPermission("table.view.$table"),
'selected' => ($this->type === $table) ? 'selected' : '',
);
}
$data['from'] = date_format(date_timestamp_set(new DateTime(), $this->from), 'Y-m-d');
$data['to'] = date_format(date_timestamp_set(new DateTime(), $this->to), 'Y-m-d');
$data['lower'] = $this->lower;
$data['upper'] = $this->upper;
if (RemoteReport::isReportingEnabled()) {
$data['settingsButtonClass'] = 'default';
$data['reportChecked'] = 'checked';
} else {
$data['settingsButtonClass'] = 'danger';
}
$data['allowedExport'] = User::hasPermission("table.export") && User::hasPermission("table.view.$this->type");
$data['allowedDownload'] = User::hasPermission("reporting.download");
$data['allowedReportChange'] = User::hasPermission("reporting.change");
Render::addTemplate('columnChooser', $data);
$data['data'] = $this->fetchData(GETDATA_PRINTABLE);
if (User::hasPermission("table.view.$this->type")) {
Render::addTemplate('table-' . $this->type, $data);
}
}
}
protected function doAjax()
{
$this->action = Request::any('action', false, 'string');
if ($this->action === 'setReporting') {
if (User::hasPermission("reporting.change")) {
$state = Request::post('reporting', false, 'string');
if ($state === false) {
die('Missing setting value.');
}
RemoteReport::setReportingEnabled($state);
$data = array();
if (RemoteReport::isReportingEnabled()) {
$data['class'] = 'default';
$data['checked'] = true;
} else {
$data['class'] = 'danger';
}
Header('Content-Type: application/json; charset=utf-8');
die(json_encode($data));
}
die('No permission.');
}
echo 'Invalid action.';
}
private function doExport()
{
$format = Request::get('format', 'json', 'string');
$printable = (bool)Request::get('printable', 0, 'int');
$flags = 0;
if ($printable) {
$flags |= GETDATA_PRINTABLE;
}
$res = $this->fetchData($flags);
// Filter unwanted columns
if (isset($res[0])) {
foreach ($this->COLUMNS as $column) {
if (Request::get('col_' . $column, 'delete', 'string') === 'delete') {
foreach ($res as &$row) {
unset($row[$column], $row[$column . '_s']);
if ($column === 'locationname') {
unset($row['locationid']);
}
}
} elseif ($printable && isset($row[0][$column . '_s'])) {
foreach ($res as &$row) {
unset($row[$column]);
}
} elseif ($column === 'locationname' && (isset($res[0]['locationname']) || isset($res[0]['locationid']))) {
foreach ($res as &$row) {
if ($printable) {
unset($row['locationid']);
} else {
unset($row['locationname']);
}
}
}
}
unset($row);
}
Header('Content-Disposition: attachment; filename=' . 'statistics-' . date('Y.m.d-H.i.s') . '.' . $format);
switch ($format) {
case 'json':
Header('Content-Type: application/json; charset=utf-8');
$output = json_encode(array('data' => $res));
break;
case 'csv':
if (!is_array($res)) {
die('Error fetching data.');
}
Header('Content-Type: text/csv; charset=utf-8');
$fh = fopen('php://output', 'w');
// Output UTF-8 BOM - Excel needs this to automatically decode as UTF-8
// (and since Excel is the only sane reason to export as csv, just always do it)
fputs($fh, chr(239) . chr(187) . chr(191));
// Output
if (isset($res[0]) && is_array($res[0])) {
// List of rows
fputcsv($fh, array_keys($res[0]), ';');
foreach ($res as $row) {
fputcsv($fh, $row, ';');
}
} else {
// Single assoc array
fputcsv($fh, array_keys($res), ';');
fputcsv($fh, $res, ';');
}
fclose($fh);
exit();
case 'xml':
$xml_data = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?><data></data>');
$this->array_to_xml($res, $xml_data, 'row');
$output = $xml_data->asXML();
Header('Content-Type: text/xml; charset=utf-8');
break;
default:
Header('Content-Type: text/plain');
$output = 'Invalid format: ' . $format;
}
die($output);
}
/**
* @param $data array Data to encode
* @param $xml_data SimpleXMLElement XML Object to append to
*/
private function array_to_xml(array $data, SimpleXMLElement $xml_data, string $parentName = 'row'): void
{
foreach ($data as $key => $value) {
if (is_numeric($key)) {
$key = $parentName;
}
if (is_array($value)) {
$subnode = $xml_data->addChild($key);
$this->array_to_xml($value, $subnode, $key);
} else {
$xml_data->addChild($key, htmlspecialchars($value));
}
}
}
private function fetchData(int $flags): array
{
// TODO: Make all modes location-aware, filter while querying, not after
switch ($this->type) {
case 'total':
return GetData::total($flags);
case 'location':
$data = GetData::perLocation($flags);
$highlight = Request::get('location', false, 'int');
if ($highlight !== false) {
foreach ($data as &$row) {
if ($row['locationid'] == $highlight) {
$row['highlight'] = true;
}
}
}
// only show locations which you have permission for
$filterLocs = User::getAllowedLocations("table.view.location");
foreach ($data as $key => $row) {
if (!in_array($row['locationid'], $filterLocs)) {
unset($data[$key]);
}
}
// correct indexing of array after deletions
return array_values($data);
case 'client':
$data = GetData::perClient($flags);
// only show clients from locations which you have permission for
$filterLocs = User::getAllowedLocations("table.view.client");
foreach ($data as $key => $row) {
if (!in_array($row['locationid'], $filterLocs)) {
unset($data[$key]);
}
}
// correct indexing of array after deletions
return array_values($data);
case 'user':
return GetData::perUser($flags);
case 'vm':
return GetData::perVM($flags);
default:
ErrorHandler::traceError('Unexpected value');
}
}
}
|