servnest/top.inc.php
2021-05-22 14:07:25 +02:00

139 lines
4.4 KiB
PHP

<?php
if (strpos($_SERVER['PHP_SELF'], "inc.php") !== false)
exit("This file is meant to be included.");
// Définition de constantes
define("USERNAME_REGEX", "^[a-z]{4,32}$");
define("PASSWORD_REGEX", "^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]{8,1024}|.{10,1024}$");
define("SUBDOMAIN_REGEX", "^[a-z]{4,63}$");
define("PREFIX", "/pmkdel"); // Prefix in the URL, if any
define("ROOT_PATH", "/srv/http/niver" . PREFIX); // Niver directory
define("DB_PATH", ROOT_PATH . "/db/niver.db"); // Niver SQLite database
define("KNOTC_PATH", "/usr/sbin/knotc"); // Binary file
define("KEYMGR_PATH", "/usr/sbin/keymgr"); // Binary file
define("NGINX_CONFIG_PATH", "/etc/nginx/hyper"); // Config directory
define("TOR_CONFIG_PATH", "/etc/tor/torrc"); // Config file
define("KNOT_ZONES_PATH", "/var/lib/knot/zones"); // Zones directory
define("TOR_KEYS_PATH", "/var/lib/tor/niver"); // Keys directory
define("SUDO_PATH", "/usr/bin/sudo"); // Binary file
define("LS_PATH", "/usr/bin/ls"); // Binary file
define("NIVER_TEMPLATE_PATH", "/usr/local/share/niver"); // Templates directory (skel, nginx, knot...)
define("MANIVER_PATH", "/usr/local/bin/maniver"); // Binary file
// The mountpoint of the hypertext storage partition (that will be accessed over SFTP)
define("HT_PATH", "/srv/ht");
define("SERVICE", substr(dirname($_SERVER['PHP_SELF']), strlen(PREFIX) + 1));
define("PAGE", basename($_SERVER['PHP_SELF'], '.php'));
define("THEME", array(
'htColor' => "#FF0000",
'regColor' => "#DA03E5",
'authColor' => "#00FF00",
'nsColor' => "#00c4c4",
'lightColor' => '#FFFFFF',
'darkColor' => '#2a2a2a',
));
// Initialisation de la session (avec des cookies)
session_start([
'name' => 'niver',
'sid_length' => 64,
'cookie_secure' => true,
'cookie_httponly' => true,
'cookie_samesite' => 'Strict',
'cookie_path' => PREFIX . '/',
'cookie_lifetime' => 432000, // = 60*60*24*5 = 5 days
'gc_maxlifetime' => 10800,
'use_strict_mode' => true,
'use_cookies' => true,
'use_only_cookies' => true,
]);
// Redirige vers la page de connexion si pas connecté·e
if (SERVICE != "auth" AND !isset($_SESSION['username'])) {
header('Location: ' . PREFIX . '/auth/login?redir=' . SERVICE . "/" . PAGE, true, 302);
exit;
}
// Enlève .php de l'URL (si jamais)
if (substr($_SERVER['REQUEST_URI'], -4) == ".php") {
header("Location: " . PREFIX . "/" . SERVICE . "/" . PAGE, true, 301); // 301 Moved Permanently
exit;
}
// Fonctions utiles partout
require "inc/all.inc.php";
require "inc/format.inc.php";
// Fonctions specifiques à un service
require "inc/ht.inc.php";
require "inc/ns.inc.php";
require "inc/reg.inc.php";
// Définitions des titres de pages
require "inc/pages.inc.php";
// Compilation du Less vers le CSS
require_once 'lessphp/lib/Less/Autoloader.php';
Less_Autoloader::register();
// Liste les fichiers dans le dossier less/
$relativeLessFiles = array_diff(scandir(ROOT_PATH . "/less"), array('..', '.'));
// Remplace les clés par les valeurs, et les valeurs par les clés
$relativeLessFiles = array_flip($relativeLessFiles);
// Transforme les chemins relatifs en chemins absolus
foreach ($relativeLessFiles as $relativeLessFile => $nothing) {
$absoluteLessFiles[ROOT_PATH . "/less/" . $relativeLessFile] = "";
}
// Génère un seul fichier CSS minimifié dans css/ à partir des sources dans less/
$options = array(
'cache_dir' => ROOT_PATH . '/css/',
'compress' => true
);
$cssFileName = Less_Cache::Get($absoluteLessFiles, $options, THEME);
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title><?php
if (isset($page['title']) AND $page['title'] != "Accueil")
echo $page['title'] . " · ";
if (isset($page['service'])) {
echo $page['service'] . " · ";
} ?>Niver</title>
<link type="text/css" rel="stylesheet" href="<?= PREFIX ?>/css/<?= $cssFileName ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<header>
<nav>
<a href="<?= PREFIX ?>">Niver</a><?php
$homepage = (PAGE != "index");
if (isset($page['service'])) {
echo ' > ';
if ($homepage)
echo '<a href="./">';
echo $page['service'];
if ($homepage)
echo '</a>';
}
if ($homepage)
echo " > " . $page['title'];
?>
</nav>
<?php if (isset($page['title'])) { ?>
<h1><?= $page['title'] ?></h1>
<?php } ?>
</header>
<main>