Skip to content

Commit

Permalink
backend: fix indentation and format
Browse files Browse the repository at this point in the history
  • Loading branch information
mbolli committed Mar 8, 2019
1 parent 7902602 commit 0edd0e6
Show file tree
Hide file tree
Showing 11 changed files with 618 additions and 475 deletions.
165 changes: 101 additions & 64 deletions backend/api/api.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace nfsen_ng\api;

use nfsen_ng\common\{Debug, Config, NfDump};
Expand All @@ -8,48 +9,50 @@ class API {
private $request;
private $input;
private $d;

public function __construct() {
$this->d = Debug::getInstance();

header('Content-Type: application/json');
header('X-Content-Type-Options: nosniff');
header('X-Frame-Options: deny');

// try to read config
try {
Config::initialize();
} catch (\Exception $e) {
$this->error(503, $e->getMessage());
}

// get the HTTP method, path and body of the request
$this->method = $_SERVER['REQUEST_METHOD'];
$this->request = explode('/', trim($_GET['request'],'/'));
$this->input = json_decode(file_get_contents('php://input'),true);

$this->request = explode('/', trim($_GET['request'], '/'));
$this->input = json_decode(file_get_contents('php://input'), true);
// only allow GET requests
// if at some time POST requests are enabled, check the request's content type (or return 406)
if ($this->method !== "GET") $this->error(501);

// call correct method
if (!method_exists($this, $this->request[0])) $this->error(404);

// remove method name from $_REQUEST
$_REQUEST = array_filter($_REQUEST, function($x) { return $x !== $this->request[0]; });

$_REQUEST = array_filter($_REQUEST, function ($x) {
return $x !== $this->request[0];
});

$method = new \ReflectionMethod($this, $this->request[0]);

// check number of parameters
if ($method->getNumberOfRequiredParameters() > count($_REQUEST)) $this->error(400, 'Not enough parameters');

$args = array();
// iterate over each parameter
foreach($method->getParameters() as $arg) {
if(!isset($_REQUEST[$arg->name])) $this->error(400, 'Expected parameter ' . $arg->name);

foreach ($method->getParameters() as $arg) {
if (!isset($_REQUEST[$arg->name])) $this->error(400, 'Expected parameter ' . $arg->name);
// make sure the data types are correct
switch($arg->getType()) {
switch ($arg->getType()) {
case 'int':
if (!is_numeric($_REQUEST[$arg->name])) $this->error(400, 'Expected type int for ' . $arg->name);
$args[$arg->name] = intval($_REQUEST[$arg->name]);
Expand All @@ -66,13 +69,13 @@ public function __construct() {
$args[$arg->name] = $_REQUEST[$arg->name];
}
}

// get output
$output = $this->{$this->request[0]}(...array_values($args));

// return output
if (array_key_exists('csv', $_REQUEST)) {

// output CSV
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=export.csv');
Expand All @@ -81,28 +84,29 @@ public function __construct() {
if ($i === 0) continue; // skip first line
fputcsv($return, $line);
}

fclose($return);

} else {

// output JSON
echo json_encode($output);
}

}

/**
* Helper function, returns the http status and exits the application.
* @param int $code
*
* @param int $code
* @param string $msg
*/
public function error(int $code, $msg = '') {
http_response_code($code);
$debug = Debug::getInstance();

$response = array('code' => $code, 'error' => '');
switch($code) {
switch ($code) {
case 400:
$response['error'] = '400 - Bad Request. ' . (empty($msg) ? 'Probably wrong or not enough arguments.' : $msg);
$debug->log($response['error'], LOG_INFO);
Expand Down Expand Up @@ -131,22 +135,33 @@ public function error(int $code, $msg = '') {
echo json_encode($response);
exit();
}

/**
* Execute the nfdump command to get statistics
* @param int $datestart
* @param int $dateend
* @param array $sources
*
* @param int $datestart
* @param int $dateend
* @param array $sources
* @param string $filter
* @param int $top
* @param int $top
* @param string $for
* @param string $limit
* @param array $output
* @param array $output
*
* @return array
*/
public function stats(int $datestart, int $dateend, array $sources, string $filter, int $top, string $for, string $limit, array $output) {
public function stats(
int $datestart,
int $dateend,
array $sources,
string $filter,
int $top,
string $for,
string $limit,
array $output
) {
$sources = implode(':', $sources);

$nfdump = new NfDump();
$nfdump->setOption('-M', $sources); // multiple sources
$nfdump->setOption('-R', array($datestart, $dateend)); // date range
Expand All @@ -155,75 +170,97 @@ public function stats(int $datestart, int $dateend, array $sources, string $filt
if (!empty($limit)) $nfdump->setOption('-l', $limit); // todo -L for traffic, -l for packets
if (isset($output['IPv6'])) $nfdump->setOption('-6', null);
$nfdump->setFilter($filter);

try {
$return = $nfdump->execute();
} catch (\Exception $e) {
$this->error(503, $e->getMessage());
}

return $return;
}

/**
* Execute the nfdump command to get flows
* @param int $datestart
* @param int $dateend
* @param array $sources
*
* @param int $datestart
* @param int $dateend
* @param array $sources
* @param string $filter
* @param int $limit
* @param int $limit
* @param string $aggregate
* @param string $sort
* @param array $output
* @param array $output
*
* @return array
*/
public function flows(int $datestart, int $dateend, array $sources, string $filter, int $limit, string $aggregate, string $sort, array $output) {
public function flows(
int $datestart,
int $dateend,
array $sources,
string $filter,
int $limit,
string $aggregate,
string $sort,
array $output
) {
$aggregate_command = "";
// nfdump -M /srv/nfsen/profiles-data/live/tiber:n048:gate:swibi:n055:swi6 -T -r 2017/04/10/nfcapd.201704101150 -c 20
$sources = implode(':', $sources);
if (!empty($aggregate))
$aggregate_command = ($aggregate === 'bidirectional') ? '-B' : '-A' . $aggregate; // no space inbetween


$nfdump = new NfDump();
$nfdump->setOption('-M', $sources); // multiple sources
$nfdump->setOption('-R', array($datestart, $dateend)); // date range
$nfdump->setOption('-c', $limit); // limit
$nfdump->setOption('-o', $output['format']);

if (!empty($sort)) $nfdump->setOption('-O', 'tstart');
if (array_key_exists('IPv6', $output)) $nfdump->setOption('-6', $output['IPv6']);
if (!empty($aggregate_command)) $nfdump->setOption('-a', $aggregate_command);
$nfdump->setFilter($filter);

try {
$return = $nfdump->execute();
} catch (\Exception $e) {
$this->error(503, $e->getMessage());
}

return $return;
}

/**
* Get data to build a graph
* @param int $datestart
* @param int $dateend
* @param array $sources
* @param array $protocols
* @param array $ports
*
* @param int $datestart
* @param int $dateend
* @param array $sources
* @param array $protocols
* @param array $ports
* @param string $type
* @param string $display
*
* @return array|string
*/
public function graph(int $datestart, int $dateend, array $sources, array $protocols, array $ports, string $type, string $display) {
public function graph(
int $datestart,
int $dateend,
array $sources,
array $protocols,
array $ports,
string $type,
string $display
) {
$graph = Config::$db->get_graph_data($datestart, $dateend, $sources, $protocols, $ports, $type, $display);
if (!is_array($graph)) $this->error(400, $graph);
return $graph;
}

public function graph_stats() {}


public function graph_stats() {
}

/**
* Get config info
* @return array
Expand All @@ -232,21 +269,21 @@ public function config() {
$sources = Config::$cfg['general']['sources'];
$ports = Config::$cfg['general']['ports'];
$frontend = Config::$cfg['frontend'];

$stored_output_formats = array(); // todo implement
$stored_filters = array(); // todo implement

$folder = dirname(__FILE__, 2);
$pidfile = $folder . '/nfsen-ng.pid';
$daemon_running = file_exists($pidfile);

return array(
'sources' => $sources,
'ports' => $ports,
'stored_output_formats' => $stored_output_formats,
'stored_filters' => $stored_filters,
'daemon_running' => $daemon_running,
'frontend' => $frontend,
'frontend' => $frontend,
);
}
}
Loading

0 comments on commit 0edd0e6

Please sign in to comment.