login/edit: generic column configuration
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 9 Jun 2018 02:15:01 +0000 (04:15 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 9 Jun 2018 12:43:21 +0000 (14:43 +0200)
Support other fields besides email.

login/edit.php

index 116abb302e7df0806346a55f91af3034c29d7fb9..f99c999c12396cd1b47b4071af6806e8c55789f4 100644 (file)
@@ -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 "<p class=warn>Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.</p>\n\n";
        }
        else {
-               print "<p>Het e-mailadres is ingesteld.</p>\n\n";
+               print "<p>Alle instellingen zijn opgeslagen.</p>\n\n";
        }
 }
 
-$usermail = @file_get_contents($setfile);
 ?>
 <form method="post" class="inline">
        <p>
@@ -39,18 +59,21 @@ $usermail = @file_get_contents($setfile);
        </p>
        <p>
 <?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"',
-               'email',
-               'email',
-               htmlspecialchars($usermail)
+               @$colconf['type'] ?: 'text',
+               $col,
+               htmlspecialchars(@$colconf['value'])
        );
-       print ' placeholder="Geen e-mailadres ingesteld"';
+       print ' placeholder="Geen '.$colconf['label'].' ingesteld"';
        print " />\n";
 
-       if ($error) {
+       if ($error = @$colwarn[$col]) {
                print "<span class=warn>$error</span>\n";
        }
+}
 ?>
        <input type="submit" value="Opslaan" />
        </p>