Security: verwijder hardcoded wachtwoord, voeg random-wachtwoord-generator toe bij eerste installatie
This commit is contained in:
@@ -7,13 +7,46 @@
|
||||
* - ZIP backup of the entire content directory
|
||||
* - Restore from an uploaded ZIP file
|
||||
* - Optional git-based versioning (init / commit / log / restore)
|
||||
*
|
||||
* @since 2.6.4
|
||||
*/
|
||||
class ContentBackup
|
||||
{
|
||||
/**
|
||||
* Pad naar de content-map.
|
||||
*
|
||||
* @since 2.6.4
|
||||
* @var string Pad naar de content-map.
|
||||
*/
|
||||
private string $contentDir;
|
||||
|
||||
/**
|
||||
* Pad naar de project-root.
|
||||
*
|
||||
* @since 2.6.4
|
||||
* @var string Pad naar de project-root.
|
||||
*/
|
||||
private string $projectRoot;
|
||||
|
||||
/**
|
||||
* Of git beschikbaar is in de content-map.
|
||||
*
|
||||
* @since 2.6.4
|
||||
* @var bool Of git beschikbaar is.
|
||||
*/
|
||||
private bool $gitAvailable;
|
||||
|
||||
/**
|
||||
* Initialiseer de ContentBackup-instantie.
|
||||
*
|
||||
* Stelt de content-map en project-root in en detecteert of git
|
||||
* beschikbaar is op het systeem.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param string $contentDir Pad naar de content-map.
|
||||
* @param string $projectRoot Optioneel. Pad naar de project-root. Default de map drie niveaus boven deze class.
|
||||
*/
|
||||
public function __construct(string $contentDir, string $projectRoot = '')
|
||||
{
|
||||
$this->contentDir = rtrim($contentDir, '/');
|
||||
@@ -22,7 +55,11 @@ class ContentBackup
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether git is available and the content dir is inside a git repo.
|
||||
* Controleer of git beschikbaar is en de content-map in een git-repo zit.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return bool True indien git beschikbaar is.
|
||||
*/
|
||||
public function isGitAvailable(): bool
|
||||
{
|
||||
@@ -30,10 +67,15 @@ class ContentBackup
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a ZIP backup of the content directory.
|
||||
* Maak een ZIP-backup van de content-map.
|
||||
*
|
||||
* @param string $outputPath Where to write the ZIP file
|
||||
* @return bool True on success
|
||||
* Controleert of ZipArchive beschikbaar is en de content-map bestaat,
|
||||
* en voegt recursief alle bestanden en mappen toe aan het ZIP-archief.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param string $outputPath Pad waar het ZIP-bestand geschreven moet worden.
|
||||
* @return bool True bij succes, false bij falen.
|
||||
*/
|
||||
public function createZipBackup(string $outputPath): bool
|
||||
{
|
||||
@@ -54,10 +96,15 @@ class ContentBackup
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore content from a ZIP file.
|
||||
* Herstel content vanuit een ZIP-bestand.
|
||||
*
|
||||
* @param string $zipPath Path to the uploaded ZIP file
|
||||
* @return array ['success' => bool, 'message' => string]
|
||||
* Pakt het ZIP-bestand uit in een tijdelijke map, maakt een backup van de
|
||||
* huidige content-map, kopieert de nieuwe content en ruimt oude backups op.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param string $zipPath Pad naar het geüploade ZIP-bestand.
|
||||
* @return array{success:bool,message:string} Resultaat-array met success en message.
|
||||
*/
|
||||
public function restoreFromZip(string $zipPath): array
|
||||
{
|
||||
@@ -112,9 +159,14 @@ class ContentBackup
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize git in the content directory.
|
||||
* Initialiseer een git-repository in de content-map.
|
||||
*
|
||||
* @return array ['success' => bool, 'message' => string]
|
||||
* Voert `git init` uit en configureert een default identiteit (user.email
|
||||
* en user.name) zodat commits werken zonder globale git-config.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return array{success:bool,message:string} Resultaat-array met success en message.
|
||||
*/
|
||||
public function gitInit(): array
|
||||
{
|
||||
@@ -135,10 +187,16 @@ class ContentBackup
|
||||
}
|
||||
|
||||
/**
|
||||
* Commit all changes in the content directory.
|
||||
* Commit alle wijzigingen in de content-map.
|
||||
*
|
||||
* @param string $message Commit message
|
||||
* @return array ['success' => bool, 'message' => string]
|
||||
* Voegt alle bestanden toe met `git add -A`, controleert of er wijzigingen
|
||||
* zijn via `git status --porcelain`, en commit met de opgegeven message.
|
||||
* Bij een lege message wordt een standaardmessage gegenereerd.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param string $message Commit message.
|
||||
* @return array{success:bool,message:string} Resultaat-array met success en message.
|
||||
*/
|
||||
public function gitCommit(string $message): array
|
||||
{
|
||||
@@ -173,10 +231,15 @@ class ContentBackup
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the git log for the content directory.
|
||||
* Haal de git-log op voor de content-map.
|
||||
*
|
||||
* @param int $limit Maximum number of entries
|
||||
* @return array ['success' => bool, 'commits' => array, 'message' => string]
|
||||
* Leest commits uit met `git log --pretty=format:%H|%h|%ai|%s` en parseert
|
||||
* deze naar een array met hash, short, date en message per commit.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param int $limit Maximum aantal entries. Default 20.
|
||||
* @return array{success:bool,commits:array<int,array{hash:string,short:string,date:string,message:string}>,message:string} Resultaat-array met success, commits en message.
|
||||
*/
|
||||
public function gitLog(int $limit = 20): array
|
||||
{
|
||||
@@ -221,10 +284,16 @@ class ContentBackup
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore content to a specific git commit.
|
||||
* Herstel content naar een specifieke git-commit.
|
||||
*
|
||||
* @param string $commitHash Commit hash to restore to
|
||||
* @return array ['success' => bool, 'message' => string]
|
||||
* Controleert de commit-hash, saniteert deze en voert
|
||||
* `git checkout <hash> -- .` uit om de bestanden van die commit
|
||||
* in de working tree te herstellen.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param string $commitHash Commit-hash om naar te herstellen.
|
||||
* @return array{success:bool,message:string} Resultaat-array met success en message.
|
||||
*/
|
||||
public function gitRestore(string $commitHash): array
|
||||
{
|
||||
@@ -251,7 +320,11 @@ class ContentBackup
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the content directory has a git repository.
|
||||
* Controleer of de content-map een git-repository bevat.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return bool True indien een .git-map aanwezig is.
|
||||
*/
|
||||
public function hasGitRepo(): bool
|
||||
{
|
||||
@@ -262,6 +335,15 @@ class ContentBackup
|
||||
// Private helpers
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Detecteer of git beschikbaar is op het systeem.
|
||||
*
|
||||
* Voert `git --version` uit en controleert de exit-code.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return bool True indien git uitvoerbaar is.
|
||||
*/
|
||||
private function detectGit(): bool
|
||||
{
|
||||
$result = $this->execGitCapture(['--version']);
|
||||
@@ -269,9 +351,16 @@ class ContentBackup
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a git command and capture output, error, and exit code separately.
|
||||
* Voer een git-commando uit en capture output, error en exit-code apart.
|
||||
*
|
||||
* @return array ['output' => string, 'error' => string, 'exit' => int]
|
||||
* Gebruikt proc_open met aparte pipes voor stdout, stderr en stdin.
|
||||
* Alle argumenten worden via escapeshellarg ge-escaped.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param array<int,string> $args Git-commando-argumenten.
|
||||
* @param string|null $cwd Optioneel. Werkdirectory voor het git-proces.
|
||||
* @return array{output:string,error:string,exit:int} Array met output, error en exit-code.
|
||||
*/
|
||||
private function execGitCapture(array $args, ?string $cwd = null): array
|
||||
{
|
||||
@@ -305,6 +394,19 @@ class ContentBackup
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Voeg een map recursief toe aan een ZipArchive.
|
||||
*
|
||||
* Itereert over alle entries in $dir en voegt mappen (als lege dir) en
|
||||
* bestanden toe onder de opgegeven $prefix in het ZIP-archief.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param ZipArchive $zip ZIP-archief om aan toe te voegen.
|
||||
* @param string $dir Pad naar de bronmap.
|
||||
* @param string $prefix Prefix (pad binnen het ZIP) voor de entries.
|
||||
* @return void
|
||||
*/
|
||||
private function addDirToZip(ZipArchive $zip, string $dir, string $prefix): void
|
||||
{
|
||||
$entries = scandir($dir);
|
||||
@@ -325,6 +427,18 @@ class ContentBackup
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Kopieer een map recursief naar een doelmap.
|
||||
*
|
||||
* Maakt de doelmap aan indien nodig en kopieert alle bestanden en
|
||||
* submappen vanuit de bronmap.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param string $src Pad naar de bronmap.
|
||||
* @param string $dst Pad naar de doelmap.
|
||||
* @return void
|
||||
*/
|
||||
private function copyDir(string $src, string $dst): void
|
||||
{
|
||||
if (!is_dir($src)) {
|
||||
@@ -352,6 +466,16 @@ class ContentBackup
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verwijder een map en alle inhoud recursief.
|
||||
*
|
||||
* Verwijdert eerst alle bestanden en submappen, daarna de map zelf.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param string $dir Pad naar de te verwijderen map.
|
||||
* @return void
|
||||
*/
|
||||
private function removeDir(string $dir): void
|
||||
{
|
||||
if (!is_dir($dir)) {
|
||||
@@ -376,6 +500,17 @@ class ContentBackup
|
||||
rmdir($dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ruim oude content-backups op, waarbij alleen de laatste bewaard blijft.
|
||||
*
|
||||
* Zoekt backups via een glob-patroon op de content-mapnaam met .bak.*
|
||||
* suffix, sorteert op wijzigingstijd (oudste eerst) en verwijdert alle
|
||||
* backups behalve de meest recente.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function cleanupOldBackups(): void
|
||||
{
|
||||
$parentDir = dirname($this->contentDir);
|
||||
|
||||
Reference in New Issue
Block a user