first commit

This commit is contained in:
Adrien MALINGREY 2021-09-25 22:45:02 +02:00
parent a4b1e5bdea
commit 1a9410c7b0
13 changed files with 304 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
connect.php

27
addNetwork.php Normal file
View File

@ -0,0 +1,27 @@
<?php
if (!(isset($_POST['name'])
&& isset($_POST['gateway'])
&& isset($_POST['mask'])
&& isset($_POST['siteId'])
&& isset($_POST['siteName']))) {
die("Erreur : données manquantes !<br/><a href='javascript:history.back(1);'>Revenir en arrière</a>");
}
$siteId = (int) $_POST['siteId'];
$gateway = ip2long($_POST['gateway']);
$mask = ip2long($_POST['mask']);
$networkAddress = $gateway & $mask;
include "connect.php";
try {
$insert = $db->prepare("INSERT INTO Networks(Name, Address, Mask, SiteId) VALUES(:name, $networkAddress, $mask, $siteId)");
$insert->execute(['name' => $_POST['name']]);
$networkId = $db->lastInsertId();
$insert = $db->exec("INSERT INTO Hosts(IPAddress, NetworkId, Comments) VALUES($gateway, $networkId, 'Passerelle')");
header("Location: .?site=${_POST['siteName']}");
} catch(Exception $e) {
echo($e->getMessage() . "<br/><a href='javascript:history.back(1);'>Revenir en arrière</a>");
}
exit;
?>

14
addSite.php Normal file
View File

@ -0,0 +1,14 @@
<?php
if (!isset($_POST['siteName'])) {
die("Nom du site manquant !<br/><a href='javascript:history.back(1);'>Revenir en arrière</a>");
}
include "connect.php";
try {
$insert = $db->prepare("INSERT INTO Sites(Name) VALUES(:name)");
$insert->execute(['name' => $_POST['siteName']]);
header("Location: .?site=${_POST['siteName']}");
exit;
} catch(Exception $e) {
echo($e->getMessage() . "<br/><a href='javascript:history.back(1);'>Revenir en arrière</a>");
}
?>

15
connect.inc.php Normal file
View File

@ -0,0 +1,15 @@
<?php
// Fill with your database login informations and rename file to connect.php
$DB_HOST = "localhost";
$DB_NAME = "TableIP";
$DB_USER = "user";
$DB_PASSWORD = "password";
try {
$db = new PDO("mysql:host=$DB_HOST;dbname=$DB_NAME", $DB_USER, $DB_PASSWORD);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(Exception $e) {
die($e->getMessage() . "<br/><a href='javascript:history.back(1);'>Revenir en arrière</a>");
}
?>

7
index.php Normal file
View File

@ -0,0 +1,7 @@
<?php
if (isset($_GET["site"])) {
include "siteNetworks.php";
} else {
include "sitesList.php";
}
?>

BIN
nok.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
ok.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

24
script.js Normal file
View File

@ -0,0 +1,24 @@
function updateHost(input) {
input.style.backgroundImage = 'url(wait.gif)'
input.style.fontStyle = "italic"
fetch(new Request("updateHost.php", {
method:"POST",
body:new FormData(input.form),
mode:"cors"
})).then(response => {
if (response.ok) {
input.style.backgroundImage = ''
input.style.fontStyle = ""
} else {
input.style.backgroundImage = 'url(nok.png)'
}
})
}
function checkMask(input) {
if (input.checkValidity()) {
} else {
input.setCustomValidity("Masque incorrect")
}
}

103
siteNetworks.php Normal file
View File

@ -0,0 +1,103 @@
<?php
$MAX_LINES = 2500;
include "connect.php";
$siteName = $_GET["site"];
// Check if site is known
$site = $db->prepare('SELECT id from Sites WHERE Name=:siteName');
$site->execute(['siteName' => $siteName]);
$siteId = $site->fetch()["id"];
if (!$siteId) {
header("HTTP/1.1 404 Not Found");
die("Erreur ! Site inconnu : ${_GET["site"]}<br/><a href=".">Accueil</a>");
}
?>
<html>
<head>
<title>TablIP - <?=$siteName?></title>
<link rel="stylesheet" href="style.css"/>
<script src="script.js"></script>
</head>
<body>
<header>
<h1>TablIP</h1>
<h2><?=$siteName?></h2>
</header>
<?php
$networks = $db->query("SELECT * FROM `Networks` WHERE `SiteId` = $siteId");
while ($network = $networks->fetch())
{
$networkId = $network["id"];
$networkAddress = (int) $network["Address"];
$networkMask = (int) $network["Mask"];
?>
<table style="width:100%">
<caption><?=$network['Name']." : ".long2ip($networkAddress)." / ".long2ip($networkMask)?><caption>
<thead>
<tr>
<th>Adresse IP</th>
<th>Nom d'hôte</th>
<th>FQDN</th>
<th>Adresse MAC</th>
<th>Commentaires</th>
<tr>
<td><em><?= long2ip($networkAddress)?></em></td>
<td colspan="4"><em>Adresse réseau</em></td>
</tr>
</tr>
</thead>
<tbody>
<?php
for ($ip = $networkAddress + 1; ($ip+1 & $networkMask) == $networkAddress && $ip < $networkAddress + $MAX_LINES; $ip++ ) {
$hosts = $db->query("SELECT * from `Hosts` WHERE IPAddress=$ip AND NetworkId=$networkId");
$host = $hosts->fetch();
?>
<tr>
<form>
<input type="hidden" name="Ip" value=<?=$ip?>/>
<input type="hidden" name="NetworkId" value=<?=$networkId?>/>
<td><?=long2ip($ip)?></td>
<td><input type="text" onchange="updateHost(this)" name='Hostname' value="<?=$host["Hostname"]?>"/></td>
<td><input type="text" onchange="updateHost(this)" name='FQDN' value="<?=$host["FQDN"]?>"/></td>
<td><input type="text" onchange="updateHost(this)" name='MacAddress' value="<?=$host["MacAddress"]?>"/></td>
<td><input type="text" onchange="updateHost(this)" name='Comments' value="<?=$host["Comments"]?>"/></td>
</form>
</tr>
<?php
}
?>
</tbody>
<tfoot>
<tr>
<td><em><?= long2ip($ip)?></em></td>
<td colspan="4"><em>Adresse de diffusion</em></td>
</tr>
</tfoot>
</table>
<?php
}
$networks->closeCursor();
?>
<form name="addNetwork" id="addNetwork" action="addNetwork.php" method="post">
<fieldset class="add">
<legend>Ajouter un réseau</legend>
<label for="nameInput">Nom</label>
<input type="text" id="nameInput" name="name" required/>
<label for="gatewayInput">Passerelle</label>
<input type="text" id="gatewayInput" name="gateway" pattern="^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"/>
<label for="maskInput">Masque</label>
<input type="text" id="maskInput" name="mask" pattern="^((0|128|192|224|240|248|252|255)\.0\.0.0|255\.(0|128|192|224|240|248|252|255)\.0\.0|255\.255\.(0|128|192|224|240|248|252|255)\.0|255\.255\.255\.(0|128|192|224|240|248|252|255))$"'/>
<input type="hidden" name="siteId" value="<?=$siteId?>"/>
<input type="hidden" name="siteName" value="<?=$siteName?>"/>
<button id="addButton" type="submit">Ajouter</button>
</fieldset>
</form>
<footer>
<a href=".">Accueil</a>
</footer>
</body>
</html>

32
sitesList.php Normal file
View File

@ -0,0 +1,32 @@
<html>
<head>
<title>TablIP</title>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<header>
<h1>TablIP</h1>
</header>
<ul>
<?php
include "connect.php";
$sites = $db->query('SELECT Name FROM Site ORDER BY Name');
while ($site = $sites->fetch())
{
echo " <li><a href='.?site=${site['Name']}'>${site['Name']}</a></li>\n";
}
$sites->closeCursor();
?>
</ul>
<form name="addSite" id="addSite" action="addSite.php" method="post">
<fieldset class="add">
<legend>Ajouter un site</legend>
<label for="siteName">Nom</label>
<input type="text" id="siteName" name="siteName" required/>
<button type="submit">Ajouter</button>
</fieldset>
</form>
</body>
</html>

45
style.css Normal file
View File

@ -0,0 +1,45 @@
body {
margin: auto 1em;
}
header {
text-align: center;
}
li {
list-style: none;
}
table {
border-collapse: collapse;
}
th, td {
border: solid 1px;
}
td {
padding: 0;
}
td input {
border: 0;
background-repeat: no-repeat;
background-position: right center;
width: 100%;
}
.add {
display: grid;
grid-template-columns: 1fr 2fr;
grid-gap: 1em;
}
.add button {
width: 100%;
margin: auto;
grid-column: 1/3;
}
footer {
text-align: center;
}

36
updateHost.php Normal file
View File

@ -0,0 +1,36 @@
<?php
if (!(isset($_POST["Ip"])
&& isset($_POST["NetworkId"])
&& isset($_POST["Hostname"])
&& isset($_POST["FQDN"])
&& isset($_POST["MacAddress"])
&& isset($_POST["Comments"]))) {
header("HTTP/1.x 400 Bad Request");
die();
}
$ip = (int) $_POST["Ip"];
$networkId = (int) $_POST["NetworkId"];
try {
include "connect.php";
$update = $db->prepare("
INSERT INTO Hosts(IpAddress, NetworkId, Hostname, FQDN, MacAddress, Comments)
VALUES($ip, $networkId, :i_hostname, :i_fqdn, :i_macAddress, :i_comments)
ON DUPLICATE KEY UPDATE Hostname = :u_hostname, FQDN = :u_fqdn, MacAddress = :u_macAddress, Comments = :u_comments
");
$update->execute([
'i_hostname' => $_POST["Hostname"],
'i_fqdn' => $_POST["FQDN"],
'i_macAddress' => $_POST["MacAddress"],
'i_comments' => $_POST["Comments"],
'u_hostname' => $_POST["Hostname"],
'u_fqdn' => $_POST["FQDN"],
'u_macAddress' => $_POST["MacAddress"],
'u_comments' => $_POST["Comments"]
]);
} catch(Exception $e) {
header("HTTP/1.x 500 " . $e->getMessage());
die($e->getMessage());
}
?>

BIN
wait.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB