login/pass: require new password value to be confirmed
[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                 'name' => $username,
12         ];
13         if (!is_writable($user['dir'])) {
14                 print "<p class=warn>Het is niet mogelijk om de gebruiker <em>{$user['name']}</em> aan te passen.</p>\n\n";
15                 return;
16         }
17 }
18
19 $cols = [
20         'name'  => ['label' => 'volledige naam'],
21         'email' => ['label' => 'e-mailadres', 'type' => 'email'],
22 ];
23
24 foreach ($cols as $col => &$colconf) {
25         $colpath = "{$user['dir']}/$col.txt";
26         if (file_exists($colpath)) {
27                 $colconf['value'] = file_get_contents($colpath);
28         }
29         if (!is_writable($user['dir'])) {
30                 continue;  # locked parent directory
31         }
32         if (isset($colconf['value']) and !is_writable($colpath)) {
33                 continue;  # locked column file
34         }
35         $colconf['target'] = $colpath;  # editing allowed
36 }
37
38 $cols = [
39         'login' => ['label' => 'login', 'value' => $user['name'], 'target' => NULL],
40 ] + $cols;
41
42 $colwarn = [];
43 if ($_POST) {
44         foreach ($_POST as $col => $val) {
45                 if (!isset($cols[$col])) {
46                         continue; # unknown
47                 }
48                 if (isset($cols[$col]['value']) and $cols[$col]['value'] === $val) {
49                         continue; # unaltered
50                 }
51                 $cols[$col]['value'] = $val;  # update form value
52                 if (empty($cols[$col]['target'])) {
53                         $colwarn[$col] = "Kan niet worden aangepast.";
54                         continue;
55                 }
56                 if (!file_put_contents($cols[$col]['target'], $val)) {
57                         $colwarn[$col] = "Fout bij opslaan.";
58                 }
59         }
60
61         if (!empty($_POST['newpass'])) {
62                 require_once('login/pass.inc.php');
63                 if ($error = passform($user, $_POST)) {
64                         $colwarn['pass'] = $error;
65                 }
66         }
67
68         if ($colwarn) {
69                 print "<p class=warn>Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.</p>\n\n";
70         }
71         else {
72                 print "<p>Alle instellingen zijn opgeslagen.</p>\n\n";
73         }
74 }
75
76 ?>
77 <form method="post">
78         <p>
79         Geef een e-mailadres op waarmee we u kunnen bereiken indien nodig.
80         Wij zullen dit adres nooit vrij- of doorgeven.
81         </p>
82 <?php
83 foreach ($cols as $col => &$colconf) {
84         print "\t";
85         printf('<label for="%s">%s:</label> ', $col, ucfirst($colconf['label']));
86         print "<input";
87         if (empty($colconf['target'])) print ' readonly';
88         printf(' type="%s" name="%s" id="%1$s" value="%s"',
89                 @$colconf['type'] ?: 'text',
90                 $col,
91                 htmlspecialchars(@$colconf['value'])
92         );
93         print ' placeholder="Niet ingesteld"';
94         print " />";
95
96         if ($error = @$colwarn[$col]) {
97                 print " <span class=warn>$error</span>\n";
98         }
99         print "<br />\n";
100 }
101
102 if (isset($user['pass'])) {
103         if ($hide = empty($_POST['newpass'])) {
104 ?>
105         <p><a onclick="document.getElementById('pass').removeAttribute('hidden'); this.remove()">Wachtwoord wijzigen</a></p>
106 <?php
107         }
108 ?>
109         <div id="pass"<?php if ($hide) print ' hidden'; ?>>
110                 <label for="newpass">Wachtwoord:</label>
111                 <input type="password" name="oldpass" value="" placeholder="Huidig wachtwoord" />
112                 <input type="password" name="newpass" value="" placeholder="Nieuw wachtwoord" />
113                 <input type="password" name="passconf" value="" placeholder="Nogmaals" />
114 <?php
115         if ($error = @$colwarn['pass']) {
116                 print " <span class=warn>$error</span>\n";
117         }
118 ?>
119         </div>
120 <?php
121 }
122 ?>
123         <input type="submit" value="Opslaan" />
124 </form>