Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit a75a1a9

Browse files
Pavel Vyskočilvyskocilpavel
authored andcommitted
Eliminate inline javascript
* All JS code was moved to 'index.js' * Using 'fetch_all' instead of 'fetch_asoc' to get data from DB * Added configuration file for ESLint
1 parent 2886957 commit a75a1a9

File tree

10 files changed

+315
-519
lines changed

10 files changed

+315
-519
lines changed

.eslintrc.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
env:
2+
browser: true
3+
jquery: true
4+
extends:
5+
- airbnb-base
6+
parserOptions:
7+
ecmaVersion: 5
8+
sourceType: script
9+
impliedStrict: true
10+
jsx: false
11+
rules:
12+
comma-dangle:
13+
- error
14+
- "never"
15+
no-plusplus:
16+
- error
17+
- {allowForLoopAfterthoughts: true}
18+
no-var: off
19+
object-shorthand: off
20+
prefer-arrow-callback: off
21+
prefer-const: off
22+
prefer-destructuring: off
23+
prefer-numeric-literals: off
24+
prefer-rest-params: off
25+
prefer-spread: off
26+
prefer-template: off
27+
vars-on-top: off

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
All notable changes to this project will be documented in this file.
33

44
## [Unreleased]
5+
#### Added
6+
- Added configuration file for ESLint
7+
58
#### Changed
69
- Using of short array syntax (from array() to [])
710
- Specify engine and default charset in tables.sql
811
- Removed unused include from 'templates/spDetail-tpl.php'
912
- Deleted useless code
1013
- Deleted 'head' and 'body' tag in tab templates
1114
- Use 'filter_input' to GET and VALIDATE value send as GET/POST param
15+
- Eliminate inline javascript
16+
- All JS code was moved to 'index.js'
17+
- Using 'fetch_all' instead of 'fetch_asoc' to get data from DB
1218

1319
#### Fixed
1420
- Fixed the syntax of CHANGELOG

lib/Auth/Process/DatabaseCommand.php

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,9 @@ public static function getLoginCountPerDay($days)
127127
}
128128
$stmt->execute();
129129
$result = $stmt->get_result();
130-
while ($row = $result->fetch_assoc()) {
131-
echo "[new Date(" .
132-
$row["year"] . "," .
133-
($row["month"] - 1) .", " .
134-
$row["day"] . "), {v:" .
135-
$row["count"] .
136-
"}],";
137-
}
130+
$r = $result->fetch_all(MYSQLI_ASSOC);
138131
$conn->close();
132+
return $r;
139133
}
140134

141135
public static function getLoginCountPerDayForService($days, $spIdentifier)
@@ -165,15 +159,9 @@ public static function getLoginCountPerDayForService($days, $spIdentifier)
165159
}
166160
$stmt->execute();
167161
$result = $stmt->get_result();
168-
while ($row = $result->fetch_assoc()) {
169-
echo "[new Date(" .
170-
$row["year"] . "," .
171-
($row["month"] - 1) . ", " .
172-
$row["day"] . "), {v:" .
173-
$row["count"] .
174-
"}],";
175-
}
162+
$r = $result->fetch_all(MYSQLI_ASSOC);
176163
$conn->close();
164+
return $r;
177165
}
178166

179167
public static function getLoginCountPerDayForIdp($days, $idpIdentifier)
@@ -203,15 +191,9 @@ public static function getLoginCountPerDayForIdp($days, $idpIdentifier)
203191
}
204192
$stmt->execute();
205193
$result = $stmt->get_result();
206-
while ($row = $result->fetch_assoc()) {
207-
echo "[new Date(" .
208-
$row["year"] . "," .
209-
($row["month"] - 1) . ", " .
210-
$row["day"] . "), {v:" .
211-
$row["count"] .
212-
"}],";
213-
}
194+
$r = $result->fetch_all(MYSQLI_ASSOC);
214195
$conn->close();
196+
return $r;
215197
}
216198

217199
public static function getAccessCountPerService($days)
@@ -223,15 +205,15 @@ public static function getAccessCountPerService($days)
223205
$serviceProvidersMapTableName = $databaseConnector->getServiceProvidersMapTableName();
224206
if ($days == 0) { // 0 = all time
225207
$stmt = $conn->prepare(
226-
"SELECT service, IFNULL(name,service) AS spName, SUM(count) AS count " .
208+
"SELECT IFNULL(name,service) AS spName, service, SUM(count) AS count " .
227209
"FROM " . $table_name . " " .
228210
"LEFT OUTER JOIN " . $serviceProvidersMapTableName . " ON service = identifier " .
229211
"GROUP BY service HAVING service != '' " .
230212
"ORDER BY count DESC"
231213
);
232214
} else {
233215
$stmt = $conn->prepare(
234-
"SELECT year, month, day, service, IFNULL(name,service) AS spName, SUM(count) AS count " .
216+
"SELECT IFNULL(name,service) AS spName, service, SUM(count) AS count " .
235217
"FROM " . $table_name . " " .
236218
"LEFT OUTER JOIN " . $serviceProvidersMapTableName . " ON service = identifier " .
237219
"WHERE CONCAT(year,'-',LPAD(month,2,'00'),'-',LPAD(day,2,'00')) " .
@@ -243,12 +225,9 @@ public static function getAccessCountPerService($days)
243225
}
244226
$stmt->execute();
245227
$result = $stmt->get_result();
246-
while ($row = $result->fetch_assoc()) {
247-
echo "['" .
248-
str_replace("'", "\'", $row["spName"]) .
249-
"', '" . $row ["service"] . "', " . $row["count"] . "],";
250-
}
228+
$r = $result->fetch_all(MYSQLI_NUM);
251229
$conn->close();
230+
return $r;
252231
}
253232

254233
public static function getAccessCountForServicePerIdentityProviders($days, $spIdentifier)
@@ -260,7 +239,7 @@ public static function getAccessCountForServicePerIdentityProviders($days, $spId
260239
$identityProvidersMapTableName = $databaseConnector->getIdentityProvidersMapTableName();
261240
if ($days == 0) { // 0 = all time
262241
$stmt = $conn->prepare(
263-
"SELECT sourceIdp, service, IFNULL(name,sourceIdp) AS idpName, SUM(count) AS count " .
242+
"SELECT IFNULL(name,sourceIdp) AS idpName, SUM(count) AS count " .
264243
"FROM " . $table_name . " " .
265244
"LEFT OUTER JOIN " . $identityProvidersMapTableName . " ON sourceIdp = entityId " .
266245
"GROUP BY sourceIdp, service HAVING sourceIdp != '' AND service=? " .
@@ -269,7 +248,7 @@ public static function getAccessCountForServicePerIdentityProviders($days, $spId
269248
$stmt->bind_param('s', $spIdentifier);
270249
} else {
271250
$stmt = $conn->prepare(
272-
"SELECT year, month, day, sourceIdp, service, IFNULL(name,sourceIdp) AS idpName, SUM(count) AS count " .
251+
"SELECT IFNULL(name,sourceIdp) AS idpName, SUM(count) AS count " .
273252
"FROM " . $table_name . " " .
274253
"LEFT OUTER JOIN " . $identityProvidersMapTableName . " ON sourceIdp = entityId " .
275254
"WHERE CONCAT(year,'-',LPAD(month,2,'00'),'-',LPAD(day,2,'00')) " .
@@ -281,10 +260,9 @@ public static function getAccessCountForServicePerIdentityProviders($days, $spId
281260
}
282261
$stmt->execute();
283262
$result = $stmt->get_result();
284-
while ($row = $result->fetch_assoc()) {
285-
echo "['" . str_replace("'", "\'", $row["idpName"]) . "', " . $row["count"] . "],";
286-
}
263+
$r = $result->fetch_all(MYSQLI_NUM);
287264
$conn->close();
265+
return $r;
288266
}
289267

290268
public static function getAccessCountForIdentityProviderPerServiceProviders($days, $idpEntityId)
@@ -296,7 +274,7 @@ public static function getAccessCountForIdentityProviderPerServiceProviders($day
296274
$serviceProvidersMapTableName = $databaseConnector->getServiceProvidersMapTableName();
297275
if ($days == 0) { // 0 = all time
298276
$stmt = $conn->prepare(
299-
"SELECT sourceIdp, service, IFNULL(name,service) AS spName, SUM(count) AS count " .
277+
"SELECT IFNULL(name,service) AS spName, SUM(count) AS count " .
300278
"FROM " . $table_name . " " .
301279
"LEFT OUTER JOIN " . $serviceProvidersMapTableName . " ON service = identifier " .
302280
"GROUP BY sourceIdp, service HAVING service != '' AND sourceIdp=? " .
@@ -305,7 +283,7 @@ public static function getAccessCountForIdentityProviderPerServiceProviders($day
305283
$stmt->bind_param('s', $idpEntityId);
306284
} else {
307285
$stmt = $conn->prepare(
308-
"SELECT year, month, day, sourceIdp, service, IFNULL(name,service) AS spName, SUM(count) AS count " .
286+
"SELECT IFNULL(name,service) AS spName, SUM(count) AS count " .
309287
"FROM " . $table_name . " " .
310288
"LEFT OUTER JOIN " . $serviceProvidersMapTableName . " ON service = identifier " .
311289
"WHERE CONCAT(year,'-',LPAD(month,2,'00'),'-',LPAD(day,2,'00')) " .
@@ -317,10 +295,9 @@ public static function getAccessCountForIdentityProviderPerServiceProviders($day
317295
}
318296
$stmt->execute();
319297
$result = $stmt->get_result();
320-
while ($row = $result->fetch_assoc()) {
321-
echo "['" . str_replace("'", "\'", $row["spName"]) . "', " . $row["count"] . "],";
322-
}
298+
$r = $result->fetch_all(MYSQLI_NUM);
323299
$conn->close();
300+
return $r;
324301
}
325302

326303
public static function getLoginCountPerIdp($days)
@@ -332,15 +309,15 @@ public static function getLoginCountPerIdp($days)
332309
$identityProvidersMapTableName = $databaseConnector->getIdentityProvidersMapTableName();
333310
if ($days == 0) { // 0 = all time
334311
$stmt = $conn->prepare(
335-
"SELECT sourceIdp, IFNULL(name,sourceIdp) AS idpName, SUM(count) AS count " .
312+
"SELECT IFNULL(name,sourceIdp) AS idpName, sourceIdp, SUM(count) AS count " .
336313
"FROM " . $tableName . " " .
337314
"LEFT OUTER JOIN " . $identityProvidersMapTableName . " ON sourceIdp = entityId " .
338315
"GROUP BY sourceIdp HAVING sourceIdp != '' " .
339316
"ORDER BY count DESC"
340317
);
341318
} else {
342319
$stmt = $conn->prepare(
343-
"SELECT year, month, day, sourceIdp, IFNULL(name,sourceIdp) AS idpName, SUM(count) AS count " .
320+
"SELECT IFNULL(name,sourceIdp) AS idpName, sourceIdp, SUM(count) AS count " .
344321
"FROM " . $tableName . " " .
345322
"LEFT OUTER JOIN " . $identityProvidersMapTableName . " ON sourceIdp = entityId " .
346323
"WHERE CONCAT(year,'-',LPAD(month,2,'00'),'-',LPAD(day,2,'00')) " .
@@ -352,11 +329,8 @@ public static function getLoginCountPerIdp($days)
352329
}
353330
$stmt->execute();
354331
$result = $stmt->get_result();
355-
while ($row = $result->fetch_assoc()) {
356-
echo "['" .
357-
str_replace("'", "\'", $row["idpName"]) .
358-
"', '" . $row['sourceIdp'] . "', " . $row["count"] . "],";
359-
}
332+
$r = $result->fetch_all(MYSQLI_NUM);
360333
$conn->close();
334+
return $r;
361335
}
362336
}

templates/identityProviders-tpl.php

Lines changed: 6 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
use SimpleSAML\Module;
4-
use SimpleSAML\Module\proxystatistics\Auth\Process\DatabaseCommand;
54

65
/**
76
* @author Pavel Vyskočil <[email protected]>
@@ -11,103 +10,28 @@
1110
$lastDays = $this->data['lastDays'];
1211

1312
?>
14-
15-
<link rel="stylesheet" media="screen" type="text/css"
16-
href="<?php Module::getModuleUrl('proxystatistics/statisticsproxy.css') ?>"/>
17-
<script type="text/javascript">
18-
google.charts.load('current', {'packages': ['corechart', 'table']});
19-
google.charts.setOnLoadCallback(drawIdpsChart);
20-
google.charts.setOnLoadCallback(drawIdpsTable);
21-
22-
function drawIdpsChart() {
23-
var data = google.visualization.arrayToDataTable([
24-
['sourceIdp', 'sourceIdPEntityId', 'Count'],
25-
<?php DatabaseCommand::getLoginCountPerIdp($lastDays)?>
26-
]);
27-
28-
data.sort([{column: 2, desc: true}]);
29-
30-
var view = new google.visualization.DataView(data);
31-
32-
view.setColumns([0, 2]);
33-
34-
var options = {
35-
pieSliceText: 'value',
36-
chartArea: {left: 20, top: 0, width: '100%', height: '100%'},
37-
};
38-
39-
var chart = new google.visualization.PieChart(document.getElementById('idpsChartDetail'));
40-
chart.draw(view, options);
41-
42-
google.visualization.events.addListener(chart, 'select', selectHandler);
43-
44-
function selectHandler() {
45-
var selection = chart.getSelection();
46-
if (selection.length) {
47-
var entityId = data.getValue(selection[0].row, 1);
48-
window.location.href = 'idpDetail.php?entityId=' + entityId;
49-
}
50-
}
51-
}
52-
53-
function drawIdpsTable() {
54-
var data = new google.visualization.DataTable();
55-
56-
data.addColumn(
57-
'string', '<?php echo $this->t('{proxystatistics:Proxystatistics:templates/tables_identity_provider}'); ?>'
58-
);
59-
data.addColumn(
60-
'string', '<?php echo $this->t('{proxystatistics:Proxystatistics:templates/tables_identity_provider}'); ?>'
61-
);
62-
data.addColumn(
63-
'number', '<?php echo $this->t('{proxystatistics:Proxystatistics:templates/count}'); ?>'
64-
);
65-
data.addRows([<?php DatabaseCommand::getLoginCountPerIdp($lastDays)?>]);
66-
67-
data.sort([{column: 2, desc: true}]);
68-
69-
var view = new google.visualization.DataView(data);
70-
71-
view.setColumns([0, 2]);
72-
73-
var table = new google.visualization.Table(document.getElementById('idpsTable'));
74-
75-
table.draw(view);
76-
77-
google.visualization.events.addListener(table, 'select', selectHandler);
78-
79-
function selectHandler() {
80-
var selection = table.getSelection();
81-
if (selection.length) {
82-
var entityId = data.getValue(selection[0].row, 1);
83-
window.location.href = 'idpDetail.php?entityId=' + entityId;
84-
}
85-
}
86-
}
87-
88-
</script>
8913
<div class="timeRange">
9014
<h4><?php echo $this->t('{proxystatistics:Proxystatistics:templates_time_range}'); ?></h4>
9115
<form id="dateSelector" method="post">
92-
<input name="tab" value="2" hidden>
16+
<input name="tab" value="1" type="hidden">
9317
<label>
9418
<input id="1" type="radio" name="lastDays" value=0
95-
onclick="this.form.submit()"<?php echo ($lastDays == 0) ? "checked=true" : "" ?>>
19+
<?php echo ($lastDays == 0) ? "checked=true" : "" ?>>
9620
<?php echo $this->t('{proxystatistics:Proxystatistics:templates/statistics-tpl_all}'); ?>
9721
</label>
9822
<label>
9923
<input id="2" type="radio" name="lastDays" value=7
100-
onclick="this.form.submit()" <?php echo ($lastDays == 7) ? "checked=true" : "" ?>>
24+
<?php echo ($lastDays == 7) ? "checked=true" : "" ?>>
10125
<?php echo $this->t('{proxystatistics:Proxystatistics:templates/statistics-tpl_week}'); ?>
10226
</label>
10327
<label>
10428
<input id="3" type="radio" name="lastDays" value=30
105-
onclick="this.form.submit()" <?php echo ($lastDays == 30) ? "checked=true" : "" ?>>
29+
<?php echo ($lastDays == 30) ? "checked=true" : "" ?>>
10630
<?php echo $this->t('{proxystatistics:Proxystatistics:templates/statistics-tpl_month}'); ?>
10731
</label>
10832
<label>
10933
<input id="4" type="radio" name="lastDays" value=365
110-
onclick="this.form.submit()" <?php echo ($lastDays == 365) ? "checked=true" : "" ?>>
34+
<?php echo ($lastDays == 365) ? "checked=true" : "" ?>>
11135
<?php echo $this->t('{proxystatistics:Proxystatistics:templates/statistics-tpl_year}'); ?>
11236
</label>
11337
</form>
@@ -119,7 +43,7 @@ function selectHandler() {
11943
</div>
12044
<div class="row">
12145
<div class="col-md-8">
122-
<div id="idpsChartDetail" class="pieChart"></div>
46+
<div id="idpsChartDetail" class="pieChart chart-idpsChart"></div>
12347
</div>
12448
<div class="col-md-4">
12549
<div id="idpsTable" class="table"></div>

0 commit comments

Comments
 (0)