Speichern und löschen von Themen und Fächern eingebaut

This commit is contained in:
Matthias Grief
2024-12-06 22:12:47 +01:00
committed by Eric Blommel
parent 7a146d8982
commit e09438c6ea
3 changed files with 292 additions and 1 deletions

View File

@@ -15,15 +15,25 @@ class Util
return preg_replace("/[^a-zA-Z0-9_-]/", "", $string);
}
/**
* Prüft, ob Zeichen außer A-Z, a-z, 0-9 sowie - und _ enthalten sind
* @param string $string Zu prüfender Text
* @return bool true, wenn ungültige Zeichen enthalten sind, sonst false
*/
static function containsIllegalCharacters(string $string): bool
{
if(preg_match("/[^a-zA-Z0-9_-]/", $string)) {
if (preg_match("/[^a-zA-Z0-9_-]/", $string)) {
return true;
}
return false;
}
/**
* Liest den gesamten Text aus einer Datei aus
* @param string $filename Dateipfad
* @return string|null Der enthaltene Text oder null bei einem Fehler
*/
static function readFileContent(string $filename): string|null
{
if (!file_exists($filename)) {
@@ -42,6 +52,54 @@ class Util
return $fileContent;
}
/**
* Schreibt Inhalt in eine Datei. Überschreibt vorhandene Inhalte. Erstellt eine neue Datei, falls nötig.
* @param string $filename Dateipfad
* @param string $content Zu schreibender Text
* @return bool true, wenn erfolgreich, false, wenn ein Fehler aufgetreten ist
*/
static function writeFileContent(string $filename, string $content): bool
{
$file = fopen($filename, "w");
if (!$file) {
return false;
}
if (!fwrite($file, $content)) {
return false;
}
fclose($file);
return true;
}
/**
* Löscht eine Datei oder einen Ordner inklusive aller Inhalte
* @param string $path Pfad zur Datei oder Verzeichnis
* @return bool true, wenn gelöscht, sonst false
*/
static function delete(string $path): bool
{
if(is_file($path)) {
if(!unlink($path)) {
return false;
}
} else if(is_dir($path)) {
$entries = scandir($path);
foreach ($entries as $entry) {
if($entry == "." || $entry == "..") {
continue;
}
self::delete($path . "/" . $entry);
}
}
return true;
}
/**
* Öffnet eine Datei und gibt JSON-Inhalte als Array zurück
* @param string $filename Dateipfad