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