Subversion Repositories ALCASAR

Rev

Rev 2266 | Rev 2858 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
324 richard 1
<?php
2191 tom.houday 2
# $Id: stats.php 2856 2020-07-21 22:11:44Z rexy $
3
 
324 richard 4
require('/etc/freeradius-web/config.php');
2856 rexy 5
require('../lib/functions.php');
324 richard 6
require('../lib/sql/nas_list.php');
7
require_once('../lib/xlat.php');
8
?>
2856 rexy 9
<!DOCTYPE html>
324 richard 10
<html>
11
<head>
12
<?php
2856 rexy 13
# Choice of language
14
$Language = 'en';
15
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
16
	$Langue		= explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
17
	$Language	= strtolower(substr(chop($Langue[0]),0,2)); }
18
if($Language == 'fr'){
19
	$l_title		= "Analyse journalière";
20
	$l_sessions		= "Sessions";
21
	$l_usage		= "temps d'utilisation total";
22
	$l_uploads		= "Données sortantes";
23
	$l_downloads	= "Données entrantes";
24
} else if($Language == 'es') {
25
	$l_title		= "SAnálisis diario";
26
	$l_sessions		= "sesiones";
27
	$l_usage		= "total usage time";
28
	$l_uploads		= "Datos de salida";
29
	$l_downloads	= "Datos entrantes";
30
} else {
31
	$l_title		= "Daily analysis";
32
	$l_sessions		= "Sessions";
33
	$l_usage		= "tiempo total de uso";
34
	$l_uploads		= "Uploads";
35
	$l_downloads	= "Downloads";
36
}
324 richard 37
if (is_file("../lib/sql/drivers/$config[sql_type]/functions.php"))
38
	include_once("../lib/sql/drivers/$config[sql_type]/functions.php");
39
else{
40
	echo <<<EOM
2856 rexy 41
<title>$l_title</title>
42
<meta http-equiv="Content-Type" content="text/html; charset=$config[general_charset]">
43
<link rel="stylesheet" href="/css/acc.css">
44
</head>
45
<body>
46
<center><b>Could not include SQL library functions. Aborting</b></center>
324 richard 47
</body>
48
</html>
49
EOM;
50
	exit();
51
}
2856 rexy 52
?>
53
<title><?= $l_title ?></title>
54
<meta http-equiv="Content-Type" content="text/html; charset=<?= $config['general_charset'] ?>">
55
<link rel="stylesheet" href="/css/acc.css" type="text/css">
56
</head>
57
<body>
58
<?php
324 richard 59
$stats_num = array();
60
$date = strftime('%A, %e %B %Y, %T %Z');
61
$now = time();
1831 raphael.pi 62
if (!isset($before))
63
{
64
	$before = date($config['sql_date_format'], $now + 86400);
65
}
66
if (!isset($after))
67
{
68
	$after = date($config['sql_date_format'], $now - 604800 );
69
}
70
$after_time = strtotime($after);
71
$before_time = strtotime($before);
324 richard 72
$days[0] = $after;
73
$counter = $after_time + 86400;
74
$i = 1;
75
while($counter < $before_time){
1831 raphael.pi 76
	$days[$i++] = date($config['sql_date_format'],$counter);	
324 richard 77
	$counter += 86400;
78
}
79
$days[$i] = $before;
80
$num_days = $i;
81
 
1831 raphael.pi 82
$column1 = (isset($column1)) ? "$column1" : 'sessions';
324 richard 83
$column[1] = "$column1";
1831 raphael.pi 84
$selected1["$column1"] = 'selected';
85
 
86
$column2 = (isset($column2)) ? "$column2" : 'usage';
324 richard 87
$column[2] = "$column2";
1831 raphael.pi 88
$selected2["$column2"] = 'selected';
89
 
90
$column3 = (isset($column3)) ? "$column3" : 'download';
324 richard 91
$column[3] = "$column3";
92
$selected3["$column3"] = 'selected';
93
 
2856 rexy 94
$message['sessions'] = $l_sessions;
95
$message['usage'] = $l_usage;
96
$message['upload'] = $l_uploads;
97
$message['download'] = $l_downloads;
1831 raphael.pi 98
if ($config['general_stats_use_totacct'] == 'yes'){
324 richard 99
	$sql_val['sessions'] = 'connnum';
100
	$sql_val['usage'] = 'conntotduration';
2229 richard 101
	$sql_val['upload'] = 'outputoctets'; # invert with 'inputoctets' in order to display it more logically
2266 tom.houday 102
	$sql_val['download'] = 'inputoctets';
324 richard 103
}
104
else{
105
	$sql_val['usage'] = 'acctsessiontime';
106
	$sql_val['upload'] = 'acctinputoctets';
107
	$sql_val['download'] = 'acctoutputoctets';
108
}
1831 raphael.pi 109
$fun['sessions'] = 'nothing';
110
$fun['usage'] = 'time2strclock';
111
$fun['upload'] = 'bytes2str';
112
$fun['download'] = 'bytes2str';
113
$sql_val['user'] = (!isset($login) || $login == '' ) ? "WHERE username LIKE '%'" : "WHERE username = '$login'";
324 richard 114
for ($j = 1; $j <= 3; $j++){
115
	$tmp = "{$sql_val[$column[$j]]}";
1831 raphael.pi 116
	$res[$j] = (!isset($tmp)) ? "COUNT(radacctid) AS res_$j" : "sum($tmp) AS res_$j";
324 richard 117
}
118
$i = 1;
1831 raphael.pi 119
$servers['all'] = 'all';
324 richard 120
foreach ($nas_list as $nas){
1831 raphael.pi 121
	$name = $nas['name'];
122
	if ($nas['ip'] == '')
324 richard 123
		continue;
1831 raphael.pi 124
	$servers[$name] = $nas['ip'];
324 richard 125
	$i++;
126
}
127
ksort($servers);
1831 raphael.pi 128
if(isset($server))
129
{
130
	if ($server != 'all' && $server != ''){
131
		$server = da_sql_escape_string($link,$server);
132
		$s = "AND nasipaddress = '$server'";
133
	}
324 richard 134
}
135
$sql_extra_query = '';
1831 raphael.pi 136
if (isset($config['sql_accounting_extra_query']))
137
{
138
	$sql_extra_query = xlat($config['sql_accounting_extra_query'],$login,$config);
139
}
140
else
141
{
142
	$sql_extra_query = "";
143
}
324 richard 144
 
145
$link = @da_sql_pconnect($config);
1831 raphael.pi 146
if ($link)
147
{
148
	$data['sum'] = array();
149
	$data['sum'][1] = "";
150
	$data['sum'][2] = "";
151
	$data['sum'][3] = "";
152
 
153
	$stats_num[1] = "";
154
	$stats_num[2] = "";
155
	$stats_num[3] = "";
156
 
324 richard 157
	for ($i = $num_days;$i > -1; $i--){
158
		$day = "$days[$i]";
1831 raphael.pi 159
		if ($config['general_stats_use_totacct'] == 'yes')
324 richard 160
			$search = @da_sql_query($link,$config,
161
			"SELECT $res[1],$res[2],$res[3] FROM $config[sql_total_accounting_table]
162
			$sql_val[user] AND acctdate = '$day' $s $sql_extra_query;");
163
		else
164
			$search = @da_sql_query($link,$config,
165
			"SELECT $res[1],$res[2],$res[3] FROM $config[sql_accounting_table]
166
			$sql_val[user] AND acctstoptime >= '$day 00:00:00' 
167
			AND acctstoptime <= '$day 23:59:59' $s $sql_extra_query;");
168
		if ($search){
169
			$row = @da_sql_fetch_array($search,$config);
1831 raphael.pi 170
			$data[$day][1] = $row['res_1'];
171
			$data['sum'][1] += $row['res_1'];
324 richard 172
			$stats_num[1] = ($data[$day][1]) ? $stats_num[1] + 1 : $stats_num[1];
1831 raphael.pi 173
			$data[$day][2] = $row['res_2'];
174
			$data['sum'][2] += $row['res_2'];
324 richard 175
			$stats_num[2] = ($data[$day][2]) ? $stats_num[2] + 1 : $stats_num[2];
1831 raphael.pi 176
			$data[$day][3] = $row['res_3'];
177
			$data['sum'][3] += $row['res_3'];
324 richard 178
			$stats_num[3] = ($data[$day][3]) ? $stats_num[3] + 1 : $stats_num[3];
179
		}
180
		else
181
			echo "<b>Database query failed: " . da_sql_error($link,$config) . "</b><br>\n";
182
	}
183
}
184
else
185
	echo "<b>Could not connect to SQL database</b><br>\n";
186
 
187
$stats_num[1] = ($stats_num[1]) ? $stats_num[1] : 1;
188
$stats_num[2] = ($stats_num[2]) ? $stats_num[2] : 1;
189
$stats_num[3] = ($stats_num[3]) ? $stats_num[3] : 1;
190
 
191
$data['avg'][1] = ceil($data['sum'][1] / $stats_num[1]);
192
$data['avg'][2] = ceil($data['sum'][2] / $stats_num[2]);
193
$data['avg'][3] = ceil($data['sum'][3] / $stats_num[3]);
194
 
195
$data['avg'][1] = $fun[$column[1]]($data['avg'][1]);
196
$data['avg'][2] = $fun[$column[2]]($data['avg'][2]);
197
$data['avg'][3] = $fun[$column[3]]($data['avg'][3]);
198
 
199
$data['sum'][1] = $fun[$column[1]]($data['sum'][1]);
200
$data['sum'][2] = $fun[$column[2]]($data['sum'][2]);
201
$data['sum'][3] = $fun[$column[3]]($data['sum'][3]);
202
 
1831 raphael.pi 203
$max[1] = 0;
204
$max[2] = 0;
205
$max[3] = 0;
206
 
324 richard 207
for ($i = 0; $i <= $num_days; $i++){
208
	$day = "$days[$i]";
209
	$max[1] = ($max[1] > $data[$day][1] ) ? $max[1] : $data[$day][1];
210
	$max[2] = ($max[2] > $data[$day][2] ) ? $max[2] : $data[$day][2];
211
	$max[3] = ($max[3] > $data[$day][3] ) ? $max[3] : $data[$day][3];
212
 
213
}
214
for ($i = 0; $i <= $num_days; $i++){
215
	$day = "$days[$i]";
216
	for ($j = 1; $j <= 3; $j++){
217
		$tmp = $data[$day][$j];
218
		if (!$max[$j])
219
			$p = $w = $c = 0;
220
		else{
221
			$p = floor(100 * ($tmp / $max[$j]));
222
			$w = floor(70 * ($tmp / $max[$j]));
223
			$c = hexdec('f0e9e2') - (258 * $p);
224
			$c = dechex($c);
225
		}
226
		if (!$w)
227
			$w++;
228
		$perc[$day][$j] = $p . "%";
229
		$width[$day][$j] = $w;
230
		$color[$day][$j] = $c;
231
	}
232
 
233
	$data[$day][1] = $fun[$column[1]]($data[$day][1]);
234
	$data[$day][2] = $fun[$column[2]]($data[$day][2]);
235
	$data[$day][3] = $fun[$column[3]]($data[$day][3]);
236
}
237
 
1831 raphael.pi 238
$data['max'][1] = $fun[$column[1]]($max[1]);
239
$data['max'][2] = $fun[$column[2]]($max[2]);
240
$data['max'][3] = $fun[$column[3]]($max[3]);
324 richard 241
 
242
require('../html/stats.html.php');
243
?>