Added host create page

This commit is contained in:
Pinga 2023-09-01 13:38:11 +03:00
parent c5a3f7641e
commit b961dc4e76
6 changed files with 361 additions and 9 deletions

View file

@ -11,8 +11,236 @@ class HostsController extends Controller
{
public function view(Request $request, Response $response)
{
$hostsModel = new Host($this->container->get('db'));
$hosts = $hostsModel->getAllHost();
return view($response,'admin/hosts/index.twig', compact('hosts'));
return view($response,'admin/hosts/view.twig');
}
public function create(Request $request, Response $response)
{
if ($request->getMethod() === 'POST') {
// Retrieve POST data
$data = $request->getParsedBody();
$db = $this->container->get('db');
$hostName = $data['hostname'] ?? null;
$ipv4 = $data['ipv4'] ?? null;
$ipv6 = $data['ipv6'] ?? null;
$registrar_id = $data['registrar'] ?? null;
$registrars = $db->select("SELECT id, clid, name FROM registrar");
if ($hostName) {
$hostModel = new Host($this->container->get('db'));
if (preg_match('/^([A-Z0-9]([A-Z0-9-]{0,61}[A-Z0-9]){0,1}\.){1,125}[A-Z0-9]([A-Z0-9-]{0,61}[A-Z0-9])$/i', $hostName) && strlen($hostName) < 254) {
$host_id_already_exist = $hostModel->getHostByNom($hostName);
if ($host_id_already_exist) {
return view($response, 'admin/hosts/create.twig', [
'hostName' => $hostName,
'error' => 'host name already exists',
'registrars' => $registrars,
]);
}
} else {
return view($response, 'admin/hosts/create.twig', [
'hostName' => $hostName,
'error' => 'Invalid host name',
'registrars' => $registrars,
]);
}
$result = $db->select('SELECT registrar_id FROM registrar_users WHERE user_id = ?', [$_SESSION['auth_user_id']]);
if (is_array($result)) {
$clid = $result['registrar_id'];
} else if (is_object($result) && method_exists($result, 'fetch')) {
$clid = $result->fetch();
} else {
$clid = $registrar_id;
}
if ($ipv4) {
$ipv4 = normalize_v4_address($ipv4);
if (!filter_var($ipv4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return view($response, 'admin/hosts/create.twig', [
'hostName' => $hostName,
'error' => 'Invalid host addr v4',
'registrars' => $registrars,
]);
}
}
if ($ipv6) {
$ipv6 = normalize_v6_address($ipv6);
if (!filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return view($response, 'admin/hosts/create.twig', [
'hostName' => $hostName,
'error' => 'Invalid host addr v6',
'registrars' => $registrars,
]);
}
}
$internal_host = false;
$query = "SELECT tld FROM domain_tld";
$result = $db->select($query);
foreach ($result as $row) {
if (preg_match("/" . preg_quote(strtoupper($row['tld']), '/') . "$/i", $hostName)) {
$internal_host = true;
break;
}
}
if ($internal_host) {
$domain_exist = false;
$clid_domain = 0;
$superordinate_dom = 0;
$result = $db->select("SELECT id, clid, name FROM domain");
foreach ($result as $row) {
if (strpos($hostName, $row['name']) !== false) {
$domain_exist = true;
$clid_domain = $row['clid'];
$superordinate_dom = $row['id'];
break;
}
}
if (!$domain_exist) {
return view($response, 'admin/hosts/create.twig', [
'hostName' => $hostName,
'error' => 'A host name object can NOT be created in a repository for which no superordinate domain name object exists',
'registrars' => $registrars,
]);
}
if ($_SESSION['auth_roles'] !== 0) {
if ($clid != $clid_domain) {
return view($response, 'admin/hosts/create.twig', [
'hostName' => $hostName,
'error' => 'The domain name belongs to another registrar, you are not allowed to create hosts for it',
'registrars' => $registrars,
]);
}
}
$db->beginTransaction();
try {
$db->insert(
'host',
[
'name' => $hostName,
'domain_id' => $superordinate_dom,
'clid' => $clid,
'crid' => $clid,
'crdate' => date('Y-m-d H:i:s')
]
);
$host_id = $db->getLastInsertId();
if (!$ipv4 && !$ipv6) {
return view($response, 'admin/hosts/create.twig', [
'hostName' => $hostName,
'error' => 'At least one of IPv4 or IPv6 must be provided',
'registrars' => $registrars,
]);
}
if ($ipv4) {
$ipv4 = normalize_v4_address($ipv4);
$db->insert(
'host_addr',
[
'host_id' => $host_id,
'addr' => $ipv4,
'ip' => 'v4'
]
);
}
if ($ipv6) {
$ipv6 = normalize_v6_address($ipv6);
$db->insert(
'host_addr',
[
'host_id' => $host_id,
'addr' => $ipv6,
'ip' => 'v6'
]
);
}
$host_status = 'ok';
$db->insert(
'host_status',
[
'host_id' => $host_id,
'status' => $host_status
]
);
$db->commit();
} catch (Exception $e) {
$db->rollBack();
return view($response, 'admin/hosts/create.twig', [
'hostName' => $hostName,
'error' => $e->getMessage(),
'registrars' => $registrars,
]);
}
$crdate = $db->selectValue(
"SELECT crdate FROM host WHERE name = ? LIMIT 1",
[$hostName]
);
return view($response, 'admin/hosts/create.twig', [
'hostName' => $hostName,
'crdate' => $crdate,
'registrars' => $registrars,
]);
} else {
$db->insert(
'host',
[
'name' => $hostName,
'clid' => $clid,
'crid' => $clid,
'crdate' => date('Y-m-d H:i:s')
]
);
$host_id = $db->getLastInsertId();
$host_status = 'ok';
$db->insert(
'host_status',
[
'host_id' => $host_id,
'status' => $host_status
]
);
$crdate = $db->selectValue(
"SELECT crdate FROM host WHERE name = ? LIMIT 1",
[$hostName]
);
return view($response, 'admin/hosts/create.twig', [
'hostName' => $hostName,
'crdate' => $crdate,
'registrars' => $registrars,
]);
}
}
}
$db = $this->container->get('db');
$registrars = $db->select("SELECT id, clid, name FROM registrar");
// Default view for GET requests or if POST data is not set
return view($response,'admin/hosts/create.twig', [
'registrars' => $registrars,
]);
}
}

View file

@ -51,6 +51,19 @@ class Host
return $this->db->select($sql, [$id])->fetch();
}
public function getHostByNom($name)
{
$result = $this->db->select('SELECT id FROM host WHERE name = ?', [$name]);
if (is_array($result)) {
return $result;
} else if (is_object($result) && method_exists($result, 'fetch')) {
return $result->fetch();
}
return null;
}
public function deleteHost($id)
{
$this->db->delete('DELETE FROM host WHERE id = ?', [$id]);

View file

@ -0,0 +1,108 @@
{% extends "layouts/app.twig" %}
{% block title %}{{ __('Create Host') }}{% endblock %}
{% block content %}
<div class="page-wrapper">
<!-- Page header -->
<div class="page-header d-print-none">
<div class="container-xl">
<div class="row g-2 align-items-center">
<div class="col">
<!-- Page pre-title -->
<div class="page-pretitle">
Overview
</div>
<h2 class="page-title">
{{ __('Create Host') }}
</h2>
</div>
<!-- Page title actions -->
<div class="col-auto ms-auto d-print-none">
<div class="btn-list">
<span class="d-none d-sm-inline">
<a href="#" class="btn">
New view
</a>
</span>
<a href="#" class="btn btn-primary d-none d-sm-inline-block" data-bs-toggle="modal" data-bs-target="#modal-report">
<!-- Download SVG icon from http://tabler-icons.io/i/plus -->
<svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></svg>
Create new report
</a>
<a href="#" class="btn btn-primary d-sm-none btn-icon" data-bs-toggle="modal" data-bs-target="#modal-report" aria-label="Create new report">
<!-- Download SVG icon from http://tabler-icons.io/i/plus -->
<svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></svg>
</a>
</div>
</div>
</div>
</div>
</div>
<!-- Page body -->
<div class="page-body">
<div class="container-xl">
<div class="col-12">
<div class="card">
<div class="card-body border-bottom py-3">
<form action="/host/create" method="post">
{{ csrf.field | raw }}
<div class="form-group">
<label for="hostname" class="form-label required">Host Name:</label>
<input type="text" class="form-control" id="hostname" name="hostname" placeholder="ns1.example.com" required>
</div>
{% if registrars %}
<div class="form-group mt-3">
<label for="registrarDropdown" class="form-label required">Select Registrar:</label>
<select id="registrarDropdown" name="registrar" class="form-control">
{% for registrar in registrars %}
<option value="{{ registrar.id }}">{{ registrar.name }}</option>
{% endfor %}
</select>
</div>
{% endif %}
<div class="form-group mt-3">
<label for="ipv4" class="form-label">IPv4 Address (Optional):</label>
<input type="text" class="form-control" id="ipv4" name="ipv4" placeholder="192.168.1.1" pattern="^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$">
<small class="form-text text-muted">Please enter a valid IPv4 address.</small>
</div>
<div class="form-group mt-3">
<label for="ipv6" class="form-label">IPv6 Address (Optional):</label>
<input type="text" class="form-control" id="ipv6" name="ipv6" placeholder="2001:0db8:85a3:0000:0000:8a2e:0370:7334">
<small class="form-text text-muted">Please enter a valid IPv6 address.</small>
</div>
<button type="submit" class="btn btn-primary mt-3">Create</button>
</form>
{% if hostName is defined and crdate is defined %}
<div class="alert alert-success mt-3" role="alert">
Host <strong>{{ hostName }}</strong> has been created successfully on <strong>{{ crdate|date("Y-m-d H:i:s") }}!</strong>
</div>
{% elseif error is defined %}
<div class="alert alert-danger mt-3" role="alert">
<strong>{{ hostName }}</strong> is not available: <strong>{{ error }}</strong>
</div>
{% endif %}
</div>
</div>
</div>
</div>
</div>
<footer class="footer footer-transparent d-print-none">
<div class="container-xl">
<div class="col-12 col-lg-auto mt-3 mt-lg-0">
<ul class="list-inline list-inline-dots mb-0">
<li class="list-inline-item">
Copyright &copy; 2023
<a href="https://namingo.org" target="_blank" class="link-secondary">Namingo</a>.
</li>
</ul>
</div>
</div>
</div>
</footer>
</div>
{% endblock %}

View file

@ -59,9 +59,9 @@
</div>
</div>
</div>
</div>
<div class="table-responsive">
<div id="hostTable"></div>
<div class="table-responsive mt-3">
<div id="hostTable"></div>
</div>
</div>
</div>
</div>

View file

@ -222,7 +222,7 @@
</a>
</div>
</li>
<li {{ is_current_url('hosts') ? 'class="nav-item dropdown active"' : 'class="nav-item dropdown"' }}>
<li {{ is_current_url('hosts') or is_current_url('hostcreate') ? 'class="nav-item dropdown active"' : 'class="nav-item dropdown"' }}>
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown" data-bs-auto-close="outside" role="button" aria-expanded="false">
<span class="nav-link-icon d-md-none d-lg-inline-block"><svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M12 9m-6 0a6 6 0 1 0 12 0a6 6 0 1 0 -12 0"></path><path d="M12 3c1.333 .333 2 2.333 2 6s-.667 5.667 -2 6"></path><path d="M12 3c-1.333 .333 -2 2.333 -2 6s.667 5.667 2 6"></path><path d="M6 9h12"></path><path d="M3 19h7"></path><path d="M14 19h7"></path><path d="M12 19m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0"></path><path d="M12 15v2"></path></svg>
</span>
@ -234,7 +234,7 @@
<a class="dropdown-item" href="{{route('hosts')}}">
{{ __('List Hosts') }}
</a>
<a class="dropdown-item" href="#">
<a class="dropdown-item" href="{{route('hostcreate')}}">
{{ __('Create Host') }}
</a>
</div>

View file

@ -43,7 +43,10 @@ $app->group('', function ($route) {
$route->map(['GET', 'POST'], '/domain/check', DomainsController::class . ':check')->setName('domaincheck');
$route->get('/contacts', ContactsController::class .':view')->setName('contacts');
$route->get('/hosts', HostsController::class .':view')->setName('hosts');
$route->map(['GET', 'POST'], '/host/create', HostsController::class . ':create')->setName('hostcreate');
$route->get('/registrars', RegistrarsController::class .':view')->setName('registrars');
$route->get('/logs', LogsController::class .':view')->setName('logs');
$route->get('/reports', ReportsController::class .':view')->setName('reports');