Stack: Nginx + PHP 8.2 + MariaDB + Postfix + Dovecot + Samba - install.sh: script installation automatisé pour RPi OS Bookworm - Interface web: visionneuse mails + admin (comptes, domaine, dashboard) - Chaque compte mail génère automatiquement un share Samba - Processeur cron: extraction pièces jointes via zbateson/mail-mime-parser - Zéro injection SQL (PDO + prepared statements) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
199 lines
9.4 KiB
PHP
199 lines
9.4 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../includes/db.php';
|
|
require_once __DIR__ . '/../includes/auth.php';
|
|
require_once __DIR__ . '/../includes/layout.php';
|
|
|
|
auth_check();
|
|
|
|
$error = '';
|
|
|
|
// Création d'un compte
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
|
|
|
|
if ($_POST['action'] === 'create') {
|
|
$username = preg_replace('/[^a-z0-9._-]/', '', strtolower(trim($_POST['username'] ?? '')));
|
|
$password = trim($_POST['password'] ?? '');
|
|
$display = trim($_POST['display_name'] ?? '');
|
|
$domain = cfg('domain');
|
|
|
|
if (strlen($username) < 2) {
|
|
$error = 'Nom d\'utilisateur invalide (min 2 caractères, a-z0-9._-)';
|
|
} elseif (strlen($password) < 6) {
|
|
$error = 'Mot de passe trop court (min 6 caractères).';
|
|
} else {
|
|
$email = "{$username}@{$domain}";
|
|
$smbPath = "/srv/copymail/{$username}";
|
|
|
|
$st = db()->prepare('SELECT id FROM accounts WHERE username = ? OR email = ?');
|
|
$st->execute([$username, $email]);
|
|
if ($st->fetch()) {
|
|
$error = "Le compte « {$username} » existe déjà.";
|
|
} else {
|
|
db()->prepare(
|
|
'INSERT INTO accounts (username, email, display_name, smb_path) VALUES (?, ?, ?, ?)'
|
|
)->execute([$username, $email, $display, $smbPath]);
|
|
|
|
// Crée le dossier, l'utilisateur Dovecot et le share Samba
|
|
$cmd = sprintf(
|
|
'/opt/copymail/scripts/create_account.sh %s %s %s 2>&1',
|
|
escapeshellarg($username),
|
|
escapeshellarg($password),
|
|
escapeshellarg($domain)
|
|
);
|
|
shell_exec($cmd);
|
|
|
|
flash('success', "Compte <strong>{$email}</strong> créé. Share SMB : <code>\\\\serveur\\{$username}</code>");
|
|
header('Location: /admin/accounts.php');
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($_POST['action'] === 'delete') {
|
|
$id = (int)$_POST['account_id'];
|
|
$st = db()->prepare('SELECT username FROM accounts WHERE id = ?');
|
|
$st->execute([$id]);
|
|
$row = $st->fetch();
|
|
if ($row) {
|
|
$cmd = sprintf('/opt/copymail/scripts/delete_account.sh %s 2>&1', escapeshellarg($row['username']));
|
|
shell_exec($cmd);
|
|
db()->prepare('DELETE FROM accounts WHERE id = ?')->execute([$id]);
|
|
flash('success', "Compte « {$row['username']} » supprimé.");
|
|
}
|
|
header('Location: /admin/accounts.php');
|
|
exit;
|
|
}
|
|
|
|
if ($_POST['action'] === 'toggle') {
|
|
$id = (int)$_POST['account_id'];
|
|
db()->prepare('UPDATE accounts SET active = 1 - active WHERE id = ?')->execute([$id]);
|
|
header('Location: /admin/accounts.php');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$accounts = db()->query(
|
|
'SELECT a.*, COUNT(sf.id) as mail_count FROM accounts a
|
|
LEFT JOIN source_file sf ON sf.account_id = a.id
|
|
GROUP BY a.id ORDER BY a.created_at DESC'
|
|
)->fetchAll();
|
|
|
|
$domain = cfg('domain');
|
|
|
|
html_head('Comptes');
|
|
html_navbar('accounts');
|
|
?>
|
|
<div class="container-fluid">
|
|
<?php flash_render(); ?>
|
|
|
|
<div class="row g-4">
|
|
|
|
<!-- Formulaire création -->
|
|
<div class="col-lg-4">
|
|
<div class="card shadow-sm">
|
|
<div class="card-header fw-semibold"><i class="bi bi-person-plus-fill me-2"></i>Nouveau compte</div>
|
|
<div class="card-body">
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger py-2 small"><?= htmlspecialchars($error) ?></div>
|
|
<?php endif; ?>
|
|
<form method="post">
|
|
<input type="hidden" name="action" value="create">
|
|
<div class="mb-3">
|
|
<label class="form-label">Nom d'utilisateur</label>
|
|
<div class="input-group">
|
|
<input type="text" name="username" class="form-control" placeholder="copycaisse" pattern="[a-zA-Z0-9._-]+" required>
|
|
<span class="input-group-text text-muted small">@<?= htmlspecialchars($domain) ?></span>
|
|
</div>
|
|
<div class="form-text">Lettres, chiffres, . _ -</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Mot de passe IMAP/SMB</label>
|
|
<input type="password" name="password" class="form-control" required minlength="6">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Nom affiché <span class="text-muted">(optionnel)</span></label>
|
|
<input type="text" name="display_name" class="form-control" placeholder="Copy Caisse">
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100">
|
|
<i class="bi bi-plus-circle me-1"></i>Créer le compte
|
|
</button>
|
|
</form>
|
|
<hr class="my-3">
|
|
<div class="small text-muted">
|
|
<i class="bi bi-info-circle me-1"></i>La création génère automatiquement :
|
|
<ul class="mt-1 mb-0">
|
|
<li>Boîte mail <code>username@<?= htmlspecialchars($domain) ?></code></li>
|
|
<li>Dossier <code>/srv/copymail/username/</code></li>
|
|
<li>Share SMB <code>\\serveur\username</code></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Liste des comptes -->
|
|
<div class="col-lg-8">
|
|
<div class="card shadow-sm">
|
|
<div class="card-header d-flex justify-content-between align-items-center">
|
|
<span><i class="bi bi-people-fill me-2"></i>Comptes existants</span>
|
|
<span class="badge bg-secondary"><?= count($accounts) ?></span>
|
|
</div>
|
|
<?php if (empty($accounts)): ?>
|
|
<div class="card-body text-muted">Aucun compte créé.</div>
|
|
<?php else: ?>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>Compte</th>
|
|
<th>Email</th>
|
|
<th>Share SMB</th>
|
|
<th class="text-center">Mails</th>
|
|
<th class="text-center">Statut</th>
|
|
<th class="text-center">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($accounts as $acc): ?>
|
|
<tr>
|
|
<td>
|
|
<strong><?= htmlspecialchars($acc['username']) ?></strong>
|
|
<?php if ($acc['display_name']): ?>
|
|
<br><small class="text-muted"><?= htmlspecialchars($acc['display_name']) ?></small>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td class="small"><?= htmlspecialchars($acc['email']) ?></td>
|
|
<td class="small font-monospace">\\serveur\<?= htmlspecialchars($acc['username']) ?></td>
|
|
<td class="text-center">
|
|
<span class="badge bg-light text-dark"><?= $acc['mail_count'] ?></span>
|
|
</td>
|
|
<td class="text-center">
|
|
<form method="post" class="d-inline">
|
|
<input type="hidden" name="action" value="toggle">
|
|
<input type="hidden" name="account_id" value="<?= $acc['id'] ?>">
|
|
<button class="badge border-0 <?= $acc['active'] ? 'bg-success' : 'bg-secondary' ?>" title="Cliquer pour basculer">
|
|
<?= $acc['active'] ? 'actif' : 'inactif' ?>
|
|
</button>
|
|
</form>
|
|
</td>
|
|
<td class="text-center">
|
|
<form method="post" onsubmit="return confirm('Supprimer le compte <?= htmlspecialchars($acc['username']) ?> et tous ses fichiers ?')">
|
|
<input type="hidden" name="action" value="delete">
|
|
<input type="hidden" name="account_id" value="<?= $acc['id'] ?>">
|
|
<button class="btn btn-sm btn-outline-danger" title="Supprimer">
|
|
<i class="bi bi-trash"></i>
|
|
</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php html_foot(); ?>
|