login/edit: generic column configuration
[minimedit.git] / login / edit.php
index 7ebe7b907ad2875a7653de76b6984347e99123ae..f99c999c12396cd1b47b4071af6806e8c55789f4 100644 (file)
@@ -1,29 +1,56 @@
 <?php
 global $User;
+if (empty($User)) {
+       return;
+}
+
+$userdir = $User['dir'];
+
+$cols = [
+       'email' => ['label' => 'e-mailadres', 'type' => 'email'],
+];
 
-if ($_POST and isset($_POST['email'])) {
-       if ($error = setmailform($_POST)) {
-               print "<p class=warn>$error</p>\n\n";
+foreach ($cols as $col => &$colconf) {
+       $colpath = "$userdir/$col.txt";
+       if (file_exists($colpath)) {
+               $colconf['value'] = file_get_contents($colpath);
        }
-       else {
-               print "<p>Het e-mailadres is ingesteld.</p>\n\n";
+       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
 }
 
-function setmailform($input = [])
-{
-       global $User;
-       $setfile = "{$User['dir']}/email.txt";
-       if (!is_writable(file_exists($setfile) ? $setfile : dirname($setfile))) {
-               return "Het e-mailadres kan niet worden aangepast voor deze gebruiker.";
+$colwarn = [];
+if ($_POST) {
+       foreach ($_POST as $col => $val) {
+               if (!isset($cols[$col])) {
+                       continue; # unknown
+               }
+               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($cols[$col]['target'], $val)) {
+                       $colwarn[$col] = "Fout bij opslaan.";
+               }
        }
-       if (!file_put_contents($setfile, @$_POST['email'])) {
-               return "Het e-mailadres kon niet worden opgeslagen. Probeer het later nog eens.";
+
+       if ($colwarn) {
+               print "<p class=warn>Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.</p>\n\n";
+       }
+       else {
+               print "<p>Alle instellingen zijn opgeslagen.</p>\n\n";
        }
-       return;
 }
 
-$usermail = @file_get_contents("{$User['dir']}/email.txt");
 ?>
 <form method="post" class="inline">
        <p>
@@ -31,7 +58,23 @@ $usermail = @file_get_contents("{$User['dir']}/email.txt");
        Wij zullen dit adres nooit vrij- of doorgeven.
        </p>
        <p>
-       <input type="email" name="email" id="email" value="<?= htmlspecialchars($usermail) ?>" placeholder="Geen e-mailadres ingesteld" />
+<?php
+foreach ($cols as $col => &$colconf) {
+       print "\t<input";
+       if (empty($colconf['target'])) print ' readonly';
+       printf(' type="%s" name="%s" id="%1$s" value="%s"',
+               @$colconf['type'] ?: 'text',
+               $col,
+               htmlspecialchars(@$colconf['value'])
+       );
+       print ' placeholder="Geen '.$colconf['label'].' ingesteld"';
+       print " />\n";
+
+       if ($error = @$colwarn[$col]) {
+               print "<span class=warn>$error</span>\n";
+       }
+}
+?>
        <input type="submit" value="Opslaan" />
        </p>
 </form>