Subversion Repositories ALCASAR

Rev

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