Files

94 lines
3.0 KiB
PHP

<?php
/**
* Prépare le contexte attendu par header.php (pushCfg, alerte_abonnement, avatar/initiales)
* SANS appeler init_praticien.php => pas de redirection abonnement.
*/
/* =========================
Helpers
========================= */
function is_header_logged_in(): bool
{
$role = (string) ($_SESSION['role'] ?? ($_SESSION['user_type'] ?? ''));
if (! empty($_SESSION['reference_id']) && $role !== '') {
return true;
}
if (! empty($_SESSION['user_id'])) {
return true;
}
if (! empty($_SESSION['client_id'])) {
return true;
}
if (! empty($_SESSION['praticien_id'])) {
return true;
}
return false;
}
/**
* Inclut /public/includes/header.php sans DOCTYPE/HTML/HEAD/BODY et sans recharger bootstrap/css.
*/
function include_header_clean(string $absPath): void
{
global $translations, $lang;
require_once APP_HTDOCS . '/public/includes/securite.php';
ob_start();
include $absPath; // /public/includes/header.php
$buf = ob_get_clean() ?: '';
// 0) DOCTYPE + <html>…</html>
$buf = preg_replace('~<!doctype\s+html[^>]*>~i', '', $buf);
$buf = preg_replace('~</?html\b[^>]*>~i', '', $buf);
// 1) retirer <head>…</head> et <body> ouverts/fermés
$buf = preg_replace('~<head\b[^>]*>.*?</head>~is', '', $buf);
$buf = preg_replace('~</?body\b[^>]*>~i', '', $buf);
// 2) retirer les <link> CSS “globaux” pour éviter d’écraser la home
$buf = preg_replace('~<link[^>]+href="[^"]*(bootstrap[^"]*\.css|mobil_style\.css|site_style\.css|fonts\.googleapis)[^"]*"[^>]*>~i', '', $buf);
// 3) retirer le <script> Bootstrap CDN (on le charge déjà en bas de la page)
$buf = preg_replace('~<script[^>]+src="[^"]*bootstrap[^"]*bundle[^"]*\.js[^"]*"[^>]*>\s*</script>~i', '', $buf);
echo $buf; // garde <header> + tes scripts inline utiles
}
function prepare_secure_header_context(): void
{
$pdo = $GLOBALS['pdo'] ?? null;
if (! ($pdo instanceof PDO)) {
return;
}
$role = strtolower((string) ($_SESSION['role'] ?? ''));
$refId = (int) ($_SESSION['reference_id'] ?? 0);
// Initiales client/praticien
if ($refId > 0 && ($role === 'client' || $role === 'praticien')) {
try {
$table = ($role === 'client') ? 'client' : 'praticien';
$st = $pdo->prepare("SELECT nom, prenom FROM {$table} WHERE id = ? LIMIT 1");
$st->execute([$refId]);
$u = $st->fetch(PDO::FETCH_ASSOC) ?: [];
$nom = trim((string) ($u['nom'] ?? ''));
$prenom = trim((string) ($u['prenom'] ?? ''));
$ini = mb_strtoupper(
mb_substr($prenom, 0, 1, 'UTF-8') . mb_substr($nom, 0, 1, 'UTF-8'),
'UTF-8'
);
$GLOBALS['initiales'] = $ini !== '' ? $ini : 'U';
} catch (Throwable $e) {
$GLOBALS['initiales'] = 'U';
}
}
}
// Si connecté -> préparer le contexte header sécurisé (sans redirection)
$logged = is_header_logged_in();
if ($logged) {
prepare_secure_header_context();
}