mirror of
https://github.com/Oxbian/SIDPS.git
synced 2025-07-08 04:43:47 +02:00
arborescence clean
This commit is contained in:
7
web/php/constants.php
Normal file
7
web/php/constants.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
// Database constants.
|
||||
define('DB_USER', 'sidps');
|
||||
define('DB_PASSWORD', 'sidps');
|
||||
define('DB_NAME', 'sidps');
|
||||
define('DB_SERVER', 'localhost');
|
||||
?>
|
79
web/php/database.php
Normal file
79
web/php/database.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
require_once('constants.php');
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
//--- dbConnect --------------------------------------------------------------
|
||||
//----------------------------------------------------------------------------
|
||||
// Create the connection to the database.
|
||||
// \return False on error and the database otherwise.
|
||||
function dbConnect()
|
||||
{
|
||||
try {
|
||||
$db = new PDO(
|
||||
'mysql:host=' . DB_SERVER . ';dbname=' . DB_NAME . ';charset=utf8',
|
||||
DB_USER,
|
||||
DB_PASSWORD
|
||||
);
|
||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
} catch (PDOException $exception) {
|
||||
error_log('Connection error: ' . $exception->getMessage());
|
||||
return false;
|
||||
}
|
||||
return $db;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
//--- dbRequestAlertes --------------------------------------------------------
|
||||
//----------------------------------------------------------------------------
|
||||
// Function to get all alertes
|
||||
// \param db The connected database.
|
||||
// \return The list of alertes.
|
||||
function dbRequestAlerts($db, $filtres = null, $orderby, $order)
|
||||
{
|
||||
try {
|
||||
$request = 'SELECT * FROM alertes';
|
||||
$params = [];
|
||||
|
||||
if (isset($filtres)) {
|
||||
$conditions = [];
|
||||
foreach ($filtres as $colonne => $valeur) {
|
||||
$conditions[] = "$colonne = :$colonne";
|
||||
$params[":$colonne"] = $valeur;
|
||||
}
|
||||
$request .= ' WHERE ' . implode(' AND ', $conditions);
|
||||
}
|
||||
|
||||
$request .= " ORDER BY $orderby $order";
|
||||
|
||||
$statement = $db->prepare($request);
|
||||
$statement->execute($params);
|
||||
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $exception) {
|
||||
error_log('Request error: ' . $exception->getMessage());
|
||||
return false;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
//--- dbRequestDevices --------------------------------------------------------
|
||||
//----------------------------------------------------------------------------
|
||||
// Function to get all Devices
|
||||
// \param db The connected database.
|
||||
// \return The list of Devices.
|
||||
function dbRequestDevices($db)
|
||||
{
|
||||
try {
|
||||
$request = 'SELECT device_product FROM alertes GROUP BY device_product;';
|
||||
$statement = $db->prepare($request);
|
||||
$statement->execute();
|
||||
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $exception) {
|
||||
error_log('Request error: ' . $exception->getMessage());
|
||||
return false;
|
||||
}
|
||||
return $result;
|
||||
}
|
53
web/php/request.php
Normal file
53
web/php/request.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
require_once('database.php');
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
|
||||
// Database connexion.
|
||||
$db = dbConnect();
|
||||
if (!$db) {
|
||||
header('HTTP/1.1 503 Service Unavailable');
|
||||
exit;
|
||||
}
|
||||
|
||||
// Check the request.
|
||||
$requestMethod = $_SERVER['REQUEST_METHOD'];
|
||||
$request = $_SERVER['PATH_INFO'];
|
||||
$request = explode('/', $request);
|
||||
|
||||
if ($request[1] == 'alertes') {
|
||||
if ($requestMethod == 'GET') {
|
||||
|
||||
$orderby = isset($_GET['orderby']) ? $_GET['orderby'] : 'date_alerte';
|
||||
$order = isset($_GET['order']) && ($_GET['order'] == 'desc') ? 'DESC' : 'ASC';
|
||||
|
||||
$filtresArray = [];
|
||||
if (isset($_GET['device_product']))
|
||||
$filtresArray['device_product'] = $_GET['device_product'];
|
||||
if (isset($_GET['agent_severity']))
|
||||
$filtresArray['agent_severity'] = $_GET['agent_severity'];
|
||||
|
||||
if (!empty($filtresArray)) {
|
||||
$data = dbRequestAlerts($db, $filtresArray, $orderby, $order);
|
||||
} else {
|
||||
$data = dbRequestAlerts($db, null, $orderby, $order);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($request[1] == 'devices') {
|
||||
$data = dbRequestDevices($db);
|
||||
}
|
||||
|
||||
// Send data to the client.
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-control: no-store, no-cache, must-revalidate');
|
||||
header('Pragma: no-cache');
|
||||
if ($requestMethod == 'POST')
|
||||
header('HTTP/1.1 201 Created');
|
||||
else
|
||||
header('HTTP/1.1 200 OK');
|
||||
echo json_encode($data);
|
||||
exit;
|
Reference in New Issue
Block a user