Subversion Repositories ALCASAR

Rev

Rev 3089 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
324 richard 1
<?php
2191 tom.houday 2
# $Id: stats.php 3105 2023-01-04 15:01:47Z 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_uploads		= "Données sortantes";
2861 rexy 22
	$l_downloads		= "Données entrantes";
2858 rexy 23
	$l_from			= "Du";
24
	$l_to			= "au";
25
	$l_user			= "utilisateur";
26
	$l_onserver		= "sur le serveur";
2861 rexy 27
	$l_fields_to_display	= "Champs à afficher";
28
	$l_number_of_sessions	= "Nombre de sessions";
29
	$l_total_usage_time     = "Temps d'usage total";
30
	$l_date			= "date";
31
	$l_daily_summary	= "Récapitulatif journalier";
32
	$l_refresh		= "Rafraîchir";
33
	$l_stat_for_all		= "Statistique pour <b><font color=\"darkblue\">tous</font></b> les utilisateurs";
34
	$l_stat_for_user	= "Statistique pour l'utilisateur :";
35
	$l_maximum		= "Maximum";
36
	$l_average		= "Moyenne";
37
	$l_summary		= "Récapitulatif";
2856 rexy 38
} else if($Language == 'es') {
2858 rexy 39
	$l_Pagename		= "Uso diario";
2856 rexy 40
	$l_title		= "SAnálisis diario";
41
	$l_uploads		= "Datos de salida";
2861 rexy 42
	$l_downloads		= "Datos entrantes";
2858 rexy 43
	$l_from			= "Del";
44
	$l_to			= "al";
45
	$l_user			= "usuario";
46
	$l_onserver		= "en el servidor";
2861 rexy 47
	$l_fields_to_display	= "Campos a mostrar";
48
	$l_number_of_sessions	= "Número de sesiones";
49
        $l_total_usage_time     = "Tiempo total de uso";
50
	$l_date			= "Fecha";
51
	$l_daily_summary	= "Resumen diaro";
52
	$l_refresh		= "Actualizar";
53
	$l_stat_for_all		= "Estadísticas para <b><font color=\"darkblue\">todos</font></b> los usuarios";
54
	$l_stat_for_user	= "Etadísticas para el usuario :";
55
	$l_maximum		= "Máximo";
56
	$l_average		= "Promedio";
57
	$l_summary		= "Résumen";
2856 rexy 58
} else {
2858 rexy 59
	$l_Pagename		= "Daily use";
2856 rexy 60
	$l_title		= "Daily analysis";
61
	$l_uploads		= "Uploads";
2861 rexy 62
	$l_downloads		= "Downloads";
2858 rexy 63
	$l_from			= "From";
64
	$l_to			= "to";
65
	$l_user			= "user";
66
	$l_onserver		= "on the server";
2861 rexy 67
	$l_fields_to_display	= "Fields to display";
68
	$l_number_of_sessions	= "Number of sesions";
69
	$l_total_usage_time	= "Total usage time";
70
	$l_date			= "date";
71
	$l_daily_summary	= "Daily summary";
72
	$l_refresh		= "Refresh";
73
	$l_stat_for_all		= "Statistic for <b><font color=\"darkblue\">all</font></b> users";
74
	$l_stat_for_user	= "Statistic for user :";
75
	$l_maximum		= "Maximum";
76
	$l_average		= "Average";
77
	$l_summary		= "Summary";
2856 rexy 78
}
324 richard 79
if (is_file("../lib/sql/drivers/$config[sql_type]/functions.php"))
80
	include_once("../lib/sql/drivers/$config[sql_type]/functions.php");
81
else{
82
	echo <<<EOM
2856 rexy 83
<title>$l_title</title>
84
<meta http-equiv="Content-Type" content="text/html; charset=$config[general_charset]">
85
<link rel="stylesheet" href="/css/acc.css">
86
</head>
87
<body>
88
<center><b>Could not include SQL library functions. Aborting</b></center>
324 richard 89
</body>
90
</html>
91
EOM;
92
	exit();
93
}
2856 rexy 94
?>
95
<title><?= $l_title ?></title>
96
<meta http-equiv="Content-Type" content="text/html; charset=<?= $config['general_charset'] ?>">
97
<link rel="stylesheet" href="/css/acc.css" type="text/css">
98
</head>
99
<body>
100
<?php
324 richard 101
$stats_num = array();
102
$date = strftime('%A, %e %B %Y, %T %Z');
103
$now = time();
1831 raphael.pi 104
if (!isset($before))
105
{
106
	$before = date($config['sql_date_format'], $now + 86400);
107
}
108
if (!isset($after))
109
{
110
	$after = date($config['sql_date_format'], $now - 604800 );
111
}
112
$after_time = strtotime($after);
113
$before_time = strtotime($before);
324 richard 114
$days[0] = $after;
115
$counter = $after_time + 86400;
116
$i = 1;
117
while($counter < $before_time){
1831 raphael.pi 118
	$days[$i++] = date($config['sql_date_format'],$counter);	
324 richard 119
	$counter += 86400;
120
}
121
$days[$i] = $before;
122
$num_days = $i;
123
 
1831 raphael.pi 124
$column1 = (isset($column1)) ? "$column1" : 'sessions';
324 richard 125
$column[1] = "$column1";
1831 raphael.pi 126
$selected1["$column1"] = 'selected';
127
 
128
$column2 = (isset($column2)) ? "$column2" : 'usage';
324 richard 129
$column[2] = "$column2";
1831 raphael.pi 130
$selected2["$column2"] = 'selected';
131
 
132
$column3 = (isset($column3)) ? "$column3" : 'download';
324 richard 133
$column[3] = "$column3";
134
$selected3["$column3"] = 'selected';
135
 
2861 rexy 136
$message['sessions'] = $l_number_of_sessions;
137
$message['usage'] = $l_total_usage_time;
2856 rexy 138
$message['upload'] = $l_uploads;
139
$message['download'] = $l_downloads;
2861 rexy 140
 
1831 raphael.pi 141
if ($config['general_stats_use_totacct'] == 'yes'){
324 richard 142
	$sql_val['sessions'] = 'connnum';
143
	$sql_val['usage'] = 'conntotduration';
2229 richard 144
	$sql_val['upload'] = 'outputoctets'; # invert with 'inputoctets' in order to display it more logically
2266 tom.houday 145
	$sql_val['download'] = 'inputoctets';
324 richard 146
}
147
else{
148
	$sql_val['usage'] = 'acctsessiontime';
149
	$sql_val['upload'] = 'acctinputoctets';
150
	$sql_val['download'] = 'acctoutputoctets';
151
}
1831 raphael.pi 152
$fun['sessions'] = 'nothing';
153
$fun['usage'] = 'time2strclock';
154
$fun['upload'] = 'bytes2str';
155
$fun['download'] = 'bytes2str';
156
$sql_val['user'] = (!isset($login) || $login == '' ) ? "WHERE username LIKE '%'" : "WHERE username = '$login'";
324 richard 157
for ($j = 1; $j <= 3; $j++){
158
	$tmp = "{$sql_val[$column[$j]]}";
1831 raphael.pi 159
	$res[$j] = (!isset($tmp)) ? "COUNT(radacctid) AS res_$j" : "sum($tmp) AS res_$j";
324 richard 160
}
161
$i = 1;
1831 raphael.pi 162
$servers['all'] = 'all';
324 richard 163
foreach ($nas_list as $nas){
1831 raphael.pi 164
	$name = $nas['name'];
165
	if ($nas['ip'] == '')
324 richard 166
		continue;
1831 raphael.pi 167
	$servers[$name] = $nas['ip'];
324 richard 168
	$i++;
169
}
170
ksort($servers);
1831 raphael.pi 171
if(isset($server))
172
{
173
	if ($server != 'all' && $server != ''){
174
		$server = da_sql_escape_string($link,$server);
175
		$s = "AND nasipaddress = '$server'";
176
	}
324 richard 177
}
178
$sql_extra_query = '';
1831 raphael.pi 179
if (isset($config['sql_accounting_extra_query']))
180
{
181
	$sql_extra_query = xlat($config['sql_accounting_extra_query'],$login,$config);
182
}
183
else
184
{
185
	$sql_extra_query = "";
186
}
324 richard 187
 
188
$link = @da_sql_pconnect($config);
1831 raphael.pi 189
if ($link)
190
{
191
	$data['sum'] = array();
192
	$data['sum'][1] = "";
193
	$data['sum'][2] = "";
194
	$data['sum'][3] = "";
195
 
196
	$stats_num[1] = "";
197
	$stats_num[2] = "";
198
	$stats_num[3] = "";
199
 
324 richard 200
	for ($i = $num_days;$i > -1; $i--){
201
		$day = "$days[$i]";
1831 raphael.pi 202
		if ($config['general_stats_use_totacct'] == 'yes')
324 richard 203
			$search = @da_sql_query($link,$config,
204
			"SELECT $res[1],$res[2],$res[3] FROM $config[sql_total_accounting_table]
205
			$sql_val[user] AND acctdate = '$day' $s $sql_extra_query;");
206
		else
207
			$search = @da_sql_query($link,$config,
208
			"SELECT $res[1],$res[2],$res[3] FROM $config[sql_accounting_table]
209
			$sql_val[user] AND acctstoptime >= '$day 00:00:00' 
210
			AND acctstoptime <= '$day 23:59:59' $s $sql_extra_query;");
211
		if ($search){
212
			$row = @da_sql_fetch_array($search,$config);
1831 raphael.pi 213
			$data[$day][1] = $row['res_1'];
3089 rexy 214
			$data_tmp = (int)$data['sum'][1] ;
215
			$data['sum'][1] = $data_tmp + $row['res_1'];
3105 rexy 216
			$stats_num[1] = ($data[$day][1]) ? (int)$stats_num[1] + 1 : $stats_num[1];
1831 raphael.pi 217
			$data[$day][2] = $row['res_2'];
3089 rexy 218
			$data_tmp = (int)$data['sum'][2];
219
			$data['sum'][2] = $data_tmp + $row['res_2'];
3105 rexy 220
			$stats_num[2] = ($data[$day][2]) ? (int)$stats_num[2] + 1 : $stats_num[2];
1831 raphael.pi 221
			$data[$day][3] = $row['res_3'];
3089 rexy 222
			$data_tmp = (int)$data['sum'][3];
223
			$data['sum'][3] = $data_tmp + $row['res_3'];
3105 rexy 224
			$stats_num[3] = ($data[$day][3]) ? (int)$stats_num[3] + 1 : $stats_num[3];
324 richard 225
		}
226
		else
227
			echo "<b>Database query failed: " . da_sql_error($link,$config) . "</b><br>\n";
228
	}
229
}
230
else
231
	echo "<b>Could not connect to SQL database</b><br>\n";
232
 
233
$stats_num[1] = ($stats_num[1]) ? $stats_num[1] : 1;
234
$stats_num[2] = ($stats_num[2]) ? $stats_num[2] : 1;
235
$stats_num[3] = ($stats_num[3]) ? $stats_num[3] : 1;
236
 
237
$data['avg'][1] = ceil($data['sum'][1] / $stats_num[1]);
238
$data['avg'][2] = ceil($data['sum'][2] / $stats_num[2]);
239
$data['avg'][3] = ceil($data['sum'][3] / $stats_num[3]);
240
 
241
$data['avg'][1] = $fun[$column[1]]($data['avg'][1]);
242
$data['avg'][2] = $fun[$column[2]]($data['avg'][2]);
243
$data['avg'][3] = $fun[$column[3]]($data['avg'][3]);
244
 
245
$data['sum'][1] = $fun[$column[1]]($data['sum'][1]);
246
$data['sum'][2] = $fun[$column[2]]($data['sum'][2]);
247
$data['sum'][3] = $fun[$column[3]]($data['sum'][3]);
248
 
1831 raphael.pi 249
$max[1] = 0;
250
$max[2] = 0;
251
$max[3] = 0;
252
 
324 richard 253
for ($i = 0; $i <= $num_days; $i++){
254
	$day = "$days[$i]";
255
	$max[1] = ($max[1] > $data[$day][1] ) ? $max[1] : $data[$day][1];
256
	$max[2] = ($max[2] > $data[$day][2] ) ? $max[2] : $data[$day][2];
257
	$max[3] = ($max[3] > $data[$day][3] ) ? $max[3] : $data[$day][3];
258
 
259
}
260
for ($i = 0; $i <= $num_days; $i++){
261
	$day = "$days[$i]";
262
	for ($j = 1; $j <= 3; $j++){
263
		$tmp = $data[$day][$j];
264
		if (!$max[$j])
265
			$p = $w = $c = 0;
266
		else{
267
			$p = floor(100 * ($tmp / $max[$j]));
268
			$w = floor(70 * ($tmp / $max[$j]));
269
			$c = hexdec('f0e9e2') - (258 * $p);
270
			$c = dechex($c);
271
		}
272
		if (!$w)
273
			$w++;
274
		$perc[$day][$j] = $p . "%";
275
		$width[$day][$j] = $w;
276
		$color[$day][$j] = $c;
277
	}
278
 
279
	$data[$day][1] = $fun[$column[1]]($data[$day][1]);
280
	$data[$day][2] = $fun[$column[2]]($data[$day][2]);
281
	$data[$day][3] = $fun[$column[3]]($data[$day][3]);
282
}
283
 
1831 raphael.pi 284
$data['max'][1] = $fun[$column[1]]($max[1]);
285
$data['max'][2] = $fun[$column[2]]($max[2]);
286
$data['max'][3] = $fun[$column[3]]($max[3]);
324 richard 287
 
288
require('../html/stats.html.php');
289
?>