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

Commit cf24ff3

Browse files
vyskocilpavelPavel Vyskočil
authored andcommitted
Initial commit.
1 parent 628e3dd commit cf24ff3

File tree

14 files changed

+592
-0
lines changed

14 files changed

+592
-0
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "cesnet/simplesamlphp-module-proxystatistics",
3+
"description": "A SimpleSAMLPHP module for statistics",
4+
"type": "simplesamlphp-module",
5+
"keywords": ["statistics","simplesamlphp"],
6+
"license": "BSD-2",
7+
"authors": [
8+
{
9+
"name": "Pavel Vyskocil",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"php": ">=5.4.0"
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* This is example configuration of SimpleSAMLphp Perun interface and additional features.
4+
* Copy this file to default config directory and edit the properties.
5+
*
6+
* @author Pavel Vyskočil <[email protected]>
7+
*/
8+
9+
$config = array(
10+
11+
'serverName' => 'localhost',
12+
13+
'userName' => 'stats',
14+
15+
'password' => 'stats',
16+
17+
'databaseName' => 'STATS',
18+
19+
'proxyTableName' => 'statistics_proxy',
20+
21+
'servicesProxyName' => 'statistics_services',
22+
);

default-disable

Whitespace-only changes.

enable

Whitespace-only changes.
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
include ("DatabaseConnector.php");
3+
/**
4+
* @author Pavel Vyskočil <[email protected]>
5+
*/
6+
7+
class DatabaseCommand
8+
{
9+
10+
public static function insertLogin(&$request, &$date)
11+
{
12+
$databaseConnector = new DatabaseConnector();
13+
$conn = $databaseConnector->getConnection();
14+
assert($conn != NULL);
15+
$proxyTableName = $databaseConnector->getProxyTableName();
16+
$servicesTableName = $databaseConnector->getServicesTableName();
17+
$sourceIdp = $request['saml:sp:IdP'];
18+
$service = $request['Destination']['name']['en'];
19+
20+
$sql = "INSERT INTO ".$proxyTableName."(year, month, day, sourceIdp, count) VALUES ('".$date->format('Y')."','".$date->format('m') ."','".$date->format('d')."','".$sourceIdp."','1') ON DUPLICATE KEY UPDATE count = count + 1";
21+
SimpleSAML_Logger::info($sql);
22+
if ($conn->query($sql) === FALSE) {
23+
SimpleSAML_Logger::error("The login log wasn't inserted into the database.");
24+
}
25+
26+
$sql = "INSERT INTO ".$servicesTableName."(year, month, day, service, count) VALUES ('".$date->format('Y')."','".$date->format('m') ."','".$date->format('d')."','".$service."','1') ON DUPLICATE KEY UPDATE count = count + 1";
27+
SimpleSAML_Logger::info($sql);
28+
if ($conn->query($sql) === FALSE) {
29+
SimpleSAML_Logger::error("The login log wasn't inserted into the database.");
30+
}
31+
32+
$conn->close();
33+
}
34+
35+
public static function getLoginCountPerDay()
36+
{
37+
$databaseConnector = new DatabaseConnector();
38+
$conn = $databaseConnector->getConnection();
39+
assert($conn != NULL);
40+
$table_name = $databaseConnector->getProxyTableName();
41+
$sql = "SELECT year, month, day, SUM(count) AS count FROM ".$table_name." GROUP BY year,month,day";
42+
$result = $conn->query($sql);
43+
while($row = $result->fetch_assoc()) {
44+
echo "[new Date(".$row["year"].",". ($row["month"] - 1 ). ", ".$row["day"]."), {v:".$row["count"]."}],";
45+
}
46+
$conn->close();
47+
}
48+
49+
50+
public static function getLoginCountPerDeyPerService()
51+
{
52+
$databaseConnector = new DatabaseConnector();
53+
$conn = $databaseConnector->getConnection();
54+
assert($conn != NULL);
55+
$table_name = $databaseConnector->getProxyTableName();
56+
$sql = "SELECT year, month, sourceIdp, SUM(count) AS count FROM ".$table_name. " GROUP BY year, month, sourceIdp HAVING sourceIdp != ''";
57+
$result = $conn->query($sql);
58+
while($row = $result->fetch_assoc()) {
59+
echo "[new Date(".$row["year"].",".($row["month"] - 1 )."),'".$row["sourceIdp"]."', {v:".$row["count"]."}],";
60+
}
61+
$conn->close();
62+
}
63+
64+
public static function getAccessToServicesPerMonth()
65+
{
66+
$databaseConnector = new DatabaseConnector();
67+
$conn = $databaseConnector->getConnection();
68+
assert($conn != NULL);
69+
$table_name = $databaseConnector->getServicesTableName();
70+
$sql = "SELECT year, month, service, SUM(count) AS count FROM ".$table_name." GROUP BY year, month, service HAVING service != ''";
71+
$result = $conn->query($sql);
72+
while($row = $result->fetch_assoc()) {
73+
echo "[new Date(".$row["year"].",".($row["month"] - 1 )."),'".$row["service"]."', {v:".$row["count"]."}],"; }
74+
$conn->close();
75+
}
76+
77+
public static function getCountOfAllLogins()
78+
{
79+
$databaseConnector = new DatabaseConnector();
80+
$conn = $databaseConnector->getConnection();
81+
assert($conn != NULL);
82+
$table_name = $databaseConnector->getProxyTableName();
83+
$sql = "SELECT SUM(count) AS count FROM " . $table_name;
84+
$result = $conn->query($sql);
85+
while ($row = $result->fetch_assoc()) {
86+
$count = $row["count"];
87+
}
88+
$conn->close();
89+
if ($count === null)
90+
{
91+
$count = 0;
92+
}
93+
echo $count;
94+
}
95+
96+
public static function getCountOfAllLoginsForToday()
97+
{
98+
$count = 0;
99+
$dateTime = new DateTime();
100+
$databaseConnector = new DatabaseConnector();
101+
$conn = $databaseConnector->getConnection();
102+
assert($conn != NULL);
103+
$table_name = $databaseConnector->getProxyTableName();
104+
$sql = "SELECT SUM(count) AS count FROM " . $table_name." WHERE year = ".$dateTime->format('Y')." AND month=".$dateTime->format('m')." AND day = ".$dateTime->format('d');
105+
$result = $conn->query($sql);
106+
while ($row = $result->fetch_assoc()) {
107+
$count = $row["count"];
108+
}
109+
$conn->close();
110+
if ($count === null)
111+
{
112+
$count = 0;
113+
}
114+
echo $count;
115+
}
116+
117+
118+
public static function getAccessCountPerService()
119+
{
120+
$databaseConnector = new DatabaseConnector();
121+
$conn = $databaseConnector->getConnection();
122+
assert($conn != NULL);
123+
$table_name = $databaseConnector->getServicesTableName();
124+
$sql = "SELECT service, SUM(count) AS count FROM ".$table_name." GROUP BY service HAVING service != ''";
125+
$result = $conn->query($sql);
126+
while($row = $result->fetch_assoc()) {
127+
echo "['".$row["service"]."', ".$row["count"]."],";
128+
}
129+
$conn->close();
130+
}
131+
132+
public static function getLoginCountPerIdp()
133+
{
134+
$databaseConnector = new DatabaseConnector();
135+
$conn = $databaseConnector->getConnection();
136+
assert($conn != NULL);
137+
$table_name = $databaseConnector->getProxyTableName();
138+
$sql = "SELECT sourceIdp, SUM(count) AS count FROM ".$table_name." GROUP BY sourceIdp HAVING sourceIdp != ''";
139+
$result = $conn->query($sql);
140+
while($row = $result->fetch_assoc()) {
141+
echo "['".$row["sourceIdp"]."', ".$row["count"]."],";
142+
}
143+
$conn->close();
144+
}
145+
146+
public static function getCountOfUsedIdp()
147+
{
148+
$databaseConnector = new DatabaseConnector();
149+
$conn = $databaseConnector->getConnection();
150+
assert($conn != NULL);
151+
$table_name = $databaseConnector->getProxyTableName();
152+
$sql = "SELECT COUNT(*) AS count FROM (SELECT DISTINCT sourceIdp FROM ".$table_name." ) AS idps WHERE sourceIdp != ''";
153+
$result = $conn->query($sql);
154+
while($row = $result->fetch_assoc()) {
155+
$count = $row["count"];
156+
}
157+
$conn->close();
158+
if ($count === null)
159+
{
160+
$count = 0;
161+
}
162+
echo $count;
163+
}
164+
165+
public static function getCountOfAccesedServices()
166+
{
167+
$databaseConnector = new DatabaseConnector();
168+
$conn = $databaseConnector->getConnection();
169+
assert($conn != NULL);
170+
$table_name = $databaseConnector->getServicesTableName();
171+
$sql = "SELECT COUNT(*) AS count FROM (SELECT DISTINCT service FROM ".$table_name." ) AS services WHERE service != ''";
172+
$result = $conn->query($sql);
173+
while($row = $result->fetch_assoc()) {
174+
$count = $row["count"];
175+
}
176+
$conn->close();
177+
if ($count === null)
178+
{
179+
$count = 0;
180+
}
181+
echo $count;
182+
}
183+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* @author Pavel Vyskočil <[email protected]>
4+
*/
5+
6+
class databaseConnector
7+
{
8+
private $serverName;
9+
private $username;
10+
private $password;
11+
private $databaseName;
12+
private $proxyTableName;
13+
private $servicesTableName;
14+
15+
const CONFIG_FILE_NAME = 'module_statisticsproxy.php';
16+
const SERVER = 'serverName';
17+
const USER = 'userName';
18+
const PASSWORD = 'password';
19+
const DATABASE = 'databaseName';
20+
const PROXY_TABLE_NAME = 'proxyTableName';
21+
const SERVICES_TABLE_NAME = 'servicesProxyName' ;
22+
23+
24+
25+
public function __construct ()
26+
{
27+
$conf = SimpleSAML_Configuration::getConfig(self::CONFIG_FILE_NAME);
28+
$this->serverName = $conf->getString(self::SERVER);
29+
$this->username = $conf->getString(self::USER);
30+
$this->password = $conf->getString(self::PASSWORD);
31+
$this->databaseName = $conf->getString(self::DATABASE);
32+
$this->proxyTableName = $conf->getString(self::PROXY_TABLE_NAME);
33+
$this->servicesTableName = $conf->getString(self::SERVICES_TABLE_NAME);
34+
}
35+
36+
public function getConnection()
37+
{
38+
$conn = NULL;
39+
$conn = new mysqli($this->serverName, $this->username, $this->password, $this->databaseName);
40+
return $conn;
41+
}
42+
43+
public function getProxyTableName()
44+
{
45+
return $this->proxyTableName;
46+
47+
}
48+
49+
public function getServicesTableName()
50+
{
51+
return $this->servicesTableName;
52+
53+
}
54+
55+
56+
}

lib/Auth/Process/statistics.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
include ("DatabaseCommand.php");
3+
/**
4+
*
5+
* @author Pavel Vyskočil <[email protected]>
6+
*/
7+
8+
class sspmod_statisticsperun_Auth_Process_statistics extends SimpleSAML_Auth_ProcessingFilter
9+
{
10+
private $config;
11+
private $reserved;
12+
13+
public function __construct($config, $reserved)
14+
{
15+
parent::__construct($config, $reserved);
16+
17+
if (!isset($config['config'])) {
18+
throw new SimpleSAML_Error_Exception("missing mandatory configuration option 'config'");
19+
}
20+
$this->config = (array) $config['config'];
21+
$this->reserved = (array) $reserved;
22+
}
23+
24+
public function process(&$request)
25+
{
26+
$dateTime = new DateTime();
27+
DatabaseCommand::insertLogin($request, $dateTime);
28+
}
29+
30+
}

templates/statistics-tpl.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
include dirname(__DIR__)."/lib/Auth/Process/DatabaseCommand.php";
3+
/**
4+
* @author Pavel Vyskočil <[email protected]>
5+
*/
6+
$this->data['header'] = 'SimpleSAMLphp Statistics';
7+
8+
$this->data['jquery'] = array('core' => TRUE, 'ui' => TRUE, 'css' => TRUE);
9+
$this->data['head'] = '<link rel="stylesheet" media="screen" type="text/css" href="' . SimpleSAML_Module::getModuleUrl('statisticsperun/statisticsproxy.css') . '" />';
10+
$this->data['head'] .='';
11+
$this->data['head'] .= '<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>';
12+
$this->data['head'] .= '<script type="text/javascript">
13+
$(document).ready(function() {
14+
$("#tabdiv").tabs();
15+
});
16+
</script>';
17+
$this->includeAtTemplateBase('includes/header.php');
18+
?>
19+
<div id="tabdiv">
20+
<ul class="tabset_tabs" width="100px">
21+
<li><a href='summary.php'>Summary</a></li>
22+
<li><a href='graphs.php'>Graphs</a></li>
23+
<li><a href='tables.php'>Tables</a></li>
24+
</ul>
25+
26+
</div>
27+
28+
<?php
29+
$this->includeAtTemplateBase('includes/footer.php');
30+
31+
?>

0 commit comments

Comments
 (0)