Added host view option

This commit is contained in:
Pinga 2023-11-12 16:07:59 +02:00
parent de6c67eebb
commit 2d6c2b98eb
3 changed files with 158 additions and 4 deletions

View file

@ -247,4 +247,68 @@ class HostsController extends Controller
'registrars' => $registrars,
]);
}
public function viewHost(Request $request, Response $response, $args)
{
$db = $this->container->get('db');
function isValidHostname($hostname) {
// Check for IDN and convert to ASCII if necessary
if (mb_detect_encoding($hostname, 'ASCII', true) === false) {
$hostname = idn_to_ascii($hostname, 0, INTL_IDNA_VARIANT_UTS46);
}
// Regular expression for validating a hostname (simplified version)
$pattern = '/^([a-zA-Z0-9-]{1,63}\.){1,}[a-zA-Z]{2,63}$/';
return preg_match($pattern, $hostname);
}
if ($args && isValidHostname($args)) {
$host = $db->selectRow('SELECT id, name, clid, crdate FROM host WHERE name = ?',
[ $args ]);
if ($host) {
$registrars = $db->selectRow('SELECT id, clid, name FROM registrar WHERE id = ?', [$host['clid']]);
// Check if the user is not an admin (assuming role 0 is admin)
if ($_SESSION["auth_roles"] != 0) {
$userRegistrars = $db->select('SELECT registrar_id FROM registrar_users WHERE user_id = ?', [$_SESSION['auth_user_id']]);
// Assuming $userRegistrars returns an array of arrays, each containing 'registrar_id'
$userRegistrarIds = array_column($userRegistrars, 'registrar_id');
// Check if the registrar's ID is in the user's list of registrar IDs
if (!in_array($registrars['id'], $userRegistrarIds)) {
// Redirect to the hosts view if the user is not authorized for this host
return $response->withHeader('Location', '/hosts')->withStatus(302);
}
}
$hostStatus = $db->selectRow('SELECT status FROM host_status WHERE host_id = ?',
[ $host['id'] ]);
$hostIPv4 = $db->select("SELECT addr FROM host_addr WHERE host_id = ? AND ip = 'v4'",
[ $host['id'] ]);
$hostIPv6 = $db->select("SELECT addr FROM host_addr WHERE host_id = ? AND ip = 'v6'",
[ $host['id'] ]);
return view($response,'admin/hosts/viewHost.twig', [
'host' => $host,
'hostStatus' => $hostStatus,
'hostIPv4' => $hostIPv4,
'hostIPv6' => $hostIPv6,
'registrars' => $registrars,
]);
} else {
// Host does not exist, redirect to the hosts view
return $response->withHeader('Location', '/hosts')->withStatus(302);
}
} else {
// Redirect to the hosts view
return $response->withHeader('Location', '/hosts')->withStatus(302);
}
}
}

View file

@ -0,0 +1,87 @@
{% extends "layouts/app.twig" %}
{% block title %}{{ __('Host Details') }}{% 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">
{{ __('Host Details') }}
</h2>
</div>
<!-- Page title actions -->
<div class="col-auto ms-auto d-print-none">
<div class="btn-list">
<a href="{{route('hostcreate')}}" class="btn btn-primary d-none d-sm-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"/><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></svg>
{{ __('Create Host') }}
</a>
<a href="{{route('hostcreate')}}" class="btn btn-primary d-sm-none btn-icon" aria-label="{{ __('Create Host') }}">
<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-header">
<h3 class="card-title">{{ host.name }}&nbsp;<span class="status status-green">{{ hostStatus.status }}</span></h3>
</div>
<div class="card-body">
<div class="datagrid">
<div class="datagrid-item">
<div class="datagrid-title">IPv4</div>
<div class="datagrid-content">{% for ipv4 in hostIPv4 %}
{{ ipv4.addr }}{% if not loop.last %}, {% endif %}
{% endfor %}
</div>
</div>
<div class="datagrid-item">
<div class="datagrid-title">IPv6</div>
<div class="datagrid-content">{% for ipv6 in hostIPv6 %}
{{ ipv6.addr }}{% if not loop.last %}, {% endif %}
{% endfor %}
</div>
</div>
<div class="datagrid-item">
<div class="datagrid-title">Creation Date</div>
<div class="datagrid-content">{{ host.crdate }}</div>
</div>
<div class="datagrid-item">
<div class="datagrid-title">Registrar</div>
<div class="datagrid-content">{{ registrars.name }}</div>
</div>
</div>
</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

@ -52,7 +52,8 @@ $app->group('', function ($route) {
$route->get('/hosts', HostsController::class .':view')->setName('hosts');
$route->map(['GET', 'POST'], '/host/create', HostsController::class . ':create')->setName('hostcreate');
$route->get('/host/{domain}', HostsController::class . ':viewHost')->setName('viewHost');
$route->get('/registrars', RegistrarsController::class .':view')->setName('registrars');
$route->get('/users', UsersController::class .':view')->setName('users');
@ -199,9 +200,11 @@ $app->add(function (Psr\Http\Message\ServerRequestInterface $request, Psr\Http\S
try {
return $handler->handle($request);
} catch (HttpNotFoundException $e) {
$response = new Response();
$response->getBody()->write('404 Not Found');
return $response->withStatus(404);
$responseFactory = new \Nyholm\Psr7\Factory\Psr17Factory();
$response = $responseFactory->createResponse();
return $response
->withHeader('Location', '/')
->withStatus(302);
}
});