From 56252c8d2e764a9c6dacb35e61a730b54ac97723 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Sat, 9 Jun 2018 04:15:01 +0200 Subject: [PATCH] login/edit: generic column configuration Support other fields besides email. --- login/edit.php | 57 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/login/edit.php b/login/edit.php index 116abb3..f99c999 100644 --- a/login/edit.php +++ b/login/edit.php @@ -5,32 +5,52 @@ if (empty($User)) { } $userdir = $User['dir']; -$setfile = "$userdir/email.txt"; -$error = NULL; +$cols = [ + 'email' => ['label' => 'e-mailadres', 'type' => 'email'], +]; + +foreach ($cols as $col => &$colconf) { + $colpath = "$userdir/$col.txt"; + if (file_exists($colpath)) { + $colconf['value'] = file_get_contents($colpath); + } + if (!is_writable($userdir)) { + continue; # locked parent directory + } + if (isset($colconf['value']) and !is_writable($colpath)) { + continue; # locked column file + } + $colconf['target'] = $colpath; # editing allowed +} + +$colwarn = []; if ($_POST) { - foreach (@$_POST['email'] as $val) { - if (!isset($val)) { - continue; + foreach ($_POST as $col => $val) { + if (!isset($cols[$col])) { + continue; # unknown } - if (!is_writable(file_exists($setfile) ? $setfile : $userdir)) { - $error = "Kan niet worden aangepast."; + if (isset($cols[$col]['value']) and $cols[$col]['value'] === $val) { + continue; # unaltered + } + $cols[$col]['value'] = $val; # update form value + if (empty($cols[$col]['target'])) { + $colwarn[$col] = "Kan niet worden aangepast."; continue; } - if (!file_put_contents($setfile, $val)) { - $error = "Fout bij opslaan."; + if (!file_put_contents($cols[$col]['target'], $val)) { + $colwarn[$col] = "Fout bij opslaan."; } } - if ($error) { + if ($colwarn) { print "

Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.

\n\n"; } else { - print "

Het e-mailadres is ingesteld.

\n\n"; + print "

Alle instellingen zijn opgeslagen.

\n\n"; } } -$usermail = @file_get_contents($setfile); ?>

@@ -39,18 +59,21 @@ $usermail = @file_get_contents($setfile);

&$colconf) { print "\t\n"; - if ($error) { + if ($error = @$colwarn[$col]) { print "$error\n"; } +} ?>

-- 2.30.0