Files
xamxam/app/src/AppLogger.php
Pontoporeia a2cba6d3c0 feat: prevent duplicate TFE submissions with logging and user feedback
- Add DuplicateThesisException (typed, carries existing thesis metadata)
- Add Database::findDuplicateThesis(): matches on year + author + normalised
  title (exact, prefix, Levenshtein ≤10% of longer string)
- ThesisCreateController::submit() runs duplicate check before any DB write
  and throws DuplicateThesisException on match
- AppLogger::logDuplicate() writes status=duplicate entries to the JSON-lines
  log for audit purposes
- App::flash/consumeFlash extended to support 'warning' flash type
- admin/actions/formulaire.php: catches DuplicateThesisException, logs it,
  flashes an HTML warning toast with a clickable link to the existing thesis,
  and repopulates the form fields
- partage/index.php: same catch block; surfaces a plain-text flash-warning
  banner on the student form with identifier, title, and year of the match;
  form is repopulated via session
- toast.php: renders toast--warning variant
- admin.css: .toast--warning + link colour rules
- form.css: .flash-warning style for the partage form
2026-05-05 11:04:52 +02:00

103 lines
3.3 KiB
PHP

<?php
/**
* Structured application logger for form submissions.
*
* Writes JSON-lines to a log file in storage/logs/.
* Each entry contains: timestamp, source (admin|partage), action,
* status (success|error), context (IP, UA, thesis ID, error message, etc.).
*/
class AppLogger
{
private string $logDir;
private string $logFile;
public function __construct(?string $logDir = null)
{
$this->logDir = $logDir ?? (defined('STORAGE_ROOT') ? STORAGE_ROOT . '/logs' : __DIR__ . '/../storage/logs');
if (!is_dir($this->logDir)) {
mkdir($this->logDir, 0755, true);
}
$this->logFile = $this->logDir . '/form-submissions.log';
}
/**
* Log a successful thesis submission.
*
* @param string $source 'admin' or 'partage'
* @param int $thesisId
* @param string $identifier e.g. "2025-003"
* @param string $authorName
* @param array $extras Additional context (e.g. share link slug)
*/
public function logSubmission(string $source, int $thesisId, string $identifier, string $authorName, array $extras = []): void
{
$this->write(array_merge([
'source' => $source,
'action' => 'submit',
'status' => 'success',
'thesis_id' => $thesisId,
'identifier' => $identifier,
'author' => $authorName,
], $extras));
}
/**
* Log a duplicate-submission attempt.
*
* @param string $source 'admin' or 'partage'
* @param string $authorName Author name from the incoming form
* @param int $existingThesisId ID of the matched existing thesis
* @param string $existingIdentifier Identifier of the matched thesis (e.g. "2025-003")
* @param array $extras Additional context (e.g. share_slug)
*/
public function logDuplicate(
string $source,
string $authorName,
int $existingThesisId,
string $existingIdentifier,
array $extras = []
): void {
$this->write(array_merge([
'source' => $source,
'action' => 'submit',
'status' => 'duplicate',
'author' => $authorName,
'existing_thesis_id' => $existingThesisId,
'existing_identifier' => $existingIdentifier,
], $extras));
}
/**
* Log a failed thesis submission.
*
* @param string $source
* @param string $errorMessage
* @param array $extras
*/
public function logError(string $source, string $errorMessage, array $extras = []): void
{
$this->write(array_merge([
'source' => $source,
'action' => 'submit',
'status' => 'error',
'error' => $errorMessage,
], $extras));
}
/**
* Write a structured log line.
*/
private function write(array $entry): void
{
$entry['timestamp'] = date('c');
$entry['ip'] = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
$entry['user_agent'] = $_SERVER['HTTP_USER_AGENT'] ?? '';
$line = json_encode($entry, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n";
error_log($line, 3, $this->logFile);
}
}