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