login/edit: generic column configuration
[minimedit.git] / login / edit.php
1 <?php
2 global $User;
3 if (empty($User)) {
4         return;
5 }
6
7 $userdir = $User['dir'];
8
9 $cols = [
10         'email' => ['label' => 'e-mailadres', 'type' => 'email'],
11 ];
12
13 foreach ($cols as $col => &$colconf) {
14         $colpath = "$userdir/$col.txt";
15         if (file_exists($colpath)) {
16                 $colconf['value'] = file_get_contents($colpath);
17         }
18         if (!is_writable($userdir)) {
19                 continue;  # locked parent directory
20         }
21         if (isset($colconf['value']) and !is_writable($colpath)) {
22                 continue;  # locked column file
23         }
24         $colconf['target'] = $colpath;  # editing allowed
25 }
26
27 $colwarn = [];
28 if ($_POST) {
29         foreach ($_POST as $col => $val) {
30                 if (!isset($cols[$col])) {
31                         continue; # unknown
32                 }
33                 if (isset($cols[$col]['value']) and $cols[$col]['value'] === $val) {
34                         continue; # unaltered
35                 }
36                 $cols[$col]['value'] = $val;  # update form value
37                 if (empty($cols[$col]['target'])) {
38                         $colwarn[$col] = "Kan niet worden aangepast.";
39                         continue;
40                 }
41                 if (!file_put_contents($cols[$col]['target'], $val)) {
42                         $colwarn[$col] = "Fout bij opslaan.";
43                 }
44         }
45
46         if ($colwarn) {
47                 print "<p class=warn>Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.</p>\n\n";
48         }
49         else {
50                 print "<p>Alle instellingen zijn opgeslagen.</p>\n\n";
51         }
52 }
53
54 ?>
55 <form method="post" class="inline">
56         <p>
57         Geef een e-mailadres op waarmee we u kunnen bereiken indien nodig.
58         Wij zullen dit adres nooit vrij- of doorgeven.
59         </p>
60         <p>
61 <?php
62 foreach ($cols as $col => &$colconf) {
63         print "\t<input";
64         if (empty($colconf['target'])) print ' readonly';
65         printf(' type="%s" name="%s" id="%1$s" value="%s"',
66                 @$colconf['type'] ?: 'text',
67                 $col,
68                 htmlspecialchars(@$colconf['value'])
69         );
70         print ' placeholder="Geen '.$colconf['label'].' ingesteld"';
71         print " />\n";
72
73         if ($error = @$colwarn[$col]) {
74                 print "<span class=warn>$error</span>\n";
75         }
76 }
77 ?>
78         <input type="submit" value="Opslaan" />
79         </p>
80 </form>