diff --git a/ajax/bandwidth/get_bandwidth.php b/ajax/bandwidth/get_bandwidth.php new file mode 100644 index 00000000..48cd5156 --- /dev/null +++ b/ajax/bandwidth/get_bandwidth.php @@ -0,0 +1,96 @@ + 0) { + $interface = $interfacesWlo[0]; + } else { + exit('No network interfaces found.'); + } +} + +define('IFNAMSIZ', 16); +if (strlen($interface) > IFNAMSIZ) { + exit('Interface name too long.'); +} elseif(!preg_match('/^[a-zA-Z0-9]+$/', $interface)) { + exit('Invalid interface name.'); +} + +exec(sprintf('vnstat -i %s --json ', escapeshellarg($interface)), $jsonstdoutvnstat, + $exitcodedaily); +if ($exitcodedaily !== 0) { + exit('vnstat error'); +} + +$jsonobj = json_decode($jsonstdoutvnstat[0], true); +$timeunits = filter_input(INPUT_GET, 'tu'); +if ($timeunits === 'm') { + // months + $jsonData = $jsonobj['interfaces'][0]['traffic']['months']; +//} elseif ($timeunits === 'h') { +// $jsonData = $jsonobj['interfaces'][0]['traffic']['hours']; +} else { + // default: days + $jsonData = $jsonobj['interfaces'][0]['traffic']['days']; +} + +$datasizeunits = filter_input(INPUT_GET, 'dsu'); + +header('X-Content-Type-Options: nosniff'); +header('Content-Type: application/json'); +echo '[ '; +$firstelm = true; +for ($i = count($jsonData) - 1; $i >= 0; --$i) { + if ($timeunits === 'm') { + $dt = DateTime::createFromFormat('Y n', $jsonData[$i]['date']['year'].' '. + $jsonData[$i]['date']['month']); +// } elseif ($timeunits === 'h') { +// $dt = DateTime::createFromFormat('Y n j G i', $jsonData[$i]['date']['year'].' '. +// $jsonData[$i]['date']['month'].' '. +// $jsonData[$i]['date']['day'].' '. +// $i.' 00'); + } else { + $dt = DateTime::createFromFormat('Y n j', $jsonData[$i]['date']['year'].' '. + $jsonData[$i]['date']['month'].' '. + $jsonData[$i]['date']['day']); + } + + if ($firstelm) { + $firstelm = false; + } else { + echo ','; + } + + if ($datasizeunits == 'mb') { + $datasend = round($jsonData[$i]['tx'] / 1024, 0); + $datareceived = round($jsonData[$i]['rx'] / 1024, 0); + } else { + $datasend = $jsonData[$i]['rx']; + $datareceived = $jsonData[$i]['rx']; + } + + if ($timeunits === 'm') { + echo '{ "date": "' , $dt->format('Y-m') , '", "rx": "' , $datareceived , + '", "tx": "' , $datasend , '" }'; +// } elseif ($timeunits === 'h') { +// echo '{ "date": "' , $dt->format('Y-m-d H:i') , '", "rx": ' , $datareceived , +// ', "tx": ' , $datasend , ' }'; + } else { + echo '{ "date": "' , $dt->format('Y-m-d') , '", "rx": "' , $datareceived , + '", "tx": "' , $datasend , '" }'; + } +} + +echo ' ]'; + + diff --git a/includes/authenticate.php b/includes/authenticate.php index 79151a24..d6cd2c6e 100755 --- a/includes/authenticate.php +++ b/includes/authenticate.php @@ -6,7 +6,13 @@ $validated = ($user == $config['admin_user']) && password_verify($pass, $config[ if (!$validated) { header('WWW-Authenticate: Basic realm="RaspAP"'); - header('HTTP/1.0 401 Unauthorized'); - die ("Not authorized"); + if (function_exists('http_response_code')) { + // http_response_code will respond with proper HTTP version back. + http_response_code(401); + } else { + header('HTTP/1.0 401 Unauthorized'); + } + + exit('Not authorized'.PHP_EOL); } diff --git a/includes/vnstat.php b/includes/vnstat.php index d9735f25..a0035b9f 100644 --- a/includes/vnstat.php +++ b/includes/vnstat.php @@ -5,116 +5,106 @@ include_once( 'includes/status_messages.php' ); /** * Generate html output for tab with vnstat traffic amount information. */ -function DisplayVnstat() +function DisplayVnstat(&$extraFooterScripts) { - $status = new StatusMessages(); - exec('vnstat -m ', $stdoutvnstatmonthly, $exitcodemonthly); - if ($exitcodemonthly !== 0) { - $status->addMessage(sprinf(_('Getting vnstat %s information failed.'), _('daily')), 'error'); - } - - exec('vnstat -w ', $stdoutvnstatweekly, $exitcodeweekly); - if ($exitcodeweekly !== 0) { - $status->addMessage(sprinf(_('Getting vnstat %s information failed.'), _('weekly')), 'error'); - } - - exec('vnstat -d ', $stdoutvnstatdaily, $exitcodedaily); - if ($exitcodedaily !== 0) { - $status->addMessage(sprinf(_('Getting vnstat %s information failed.'), _('monthly')), - 'error'); - } ?>
showMessages(); ?>
- - - -