login/edit: password input from common form option
[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 = strtolower(ltrim($Args, '/'));
9         $user = [
10                 'dir' => "profile/$username",
11                 'name' => $username,
12         ];
13 }
14
15 $cols = [
16         'name'  => ['label' => 'volledige naam'],
17         'email' => ['label' => 'e-mailadres', 'type' => 'email'],
18         'avatar' => [
19                 'label' => 'portretfoto',
20                 'type' => 'file',
21         ],
22 ];
23
24 foreach ($cols as $col => &$colconf) {
25         $filetype = @$colconf['type'] == 'file' ? 'jpg' : 'txt';
26         $colpath = "{$user['dir']}/$col.$filetype";
27         if (file_exists($colpath)) {
28                 $colconf['value'] = $filetype != 'txt' ? '' :
29                         file_get_contents($colpath);
30         }
31         if (file_exists($user['dir']) and !is_writable($user['dir'])) {
32                 continue;  # locked parent directory
33         }
34         if (isset($colconf['value']) and !is_writable($colpath)) {
35                 continue;  # locked column file
36         }
37         $colconf['target'] = $colpath;  # editing allowed
38 }
39
40 $cols = [
41         'login' => ['label' => 'login', 'value' => $user['name'], 'target' => NULL],
42 ] + $cols;
43
44 if (isset($user['pass'])) {
45         $cols['newpass'] = [
46                 'label' => 'wachtwoord',
47                 'input' => <<<'EOT'
48                         <input type="password" name="oldpass" value="" placeholder="Huidig wachtwoord" />
49                         <input type="password" id="newpass" name="newpass" value="" placeholder="Nieuw wachtwoord" />
50                         <input type="password" name="passconf" value="" placeholder="Nogmaals" />
51 EOT
52                 ,
53                 'hide'  => 'pass',
54         ];
55 }
56
57 $colwarn = [];
58 if ($_POST) {
59         if (!file_exists($user['dir']) and !@mkdir($user['dir'])) {
60                 print "<p class=warn>Fout bij het aanmaken van gebruikersprofiel voor <em>{$user['name']}</em>.</p>\n\n";
61                 return;
62         }
63
64         foreach ($_POST as $col => $val) {
65                 if (!isset($cols[$col])) {
66                         continue; # unknown
67                 }
68                 if (isset($cols[$col]['value']) and $cols[$col]['value'] === $val) {
69                         continue; # unaltered
70                 }
71                 $cols[$col]['value'] = $val;  # update form value
72                 if (empty($cols[$col]['target'])) {
73                         $colwarn[$col] = "Kan niet worden aangepast.";
74                         continue;
75                 }
76                 if (file_put_contents($cols[$col]['target'], $val) === FALSE) {
77                         $colwarn[$col] = "Fout bij opslaan.";
78                 }
79         }
80
81         foreach ($_FILES as $col => $val) {
82                 if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
83                         continue; # unknown
84                 }
85                 switch ($val['error']) {
86                 case UPLOAD_ERR_OK:
87                         break;
88                 case UPLOAD_ERR_NO_FILE:
89                         continue 2; # current
90                 default:
91                         $colwarn[$col] = "Afbeelding niet goed ontvangen.";
92                         continue 2;
93                 }
94                 if (empty($cols[$col]['target'])) {
95                         $colwarn[$col] = "Kan niet worden aangepast.";
96                         continue;
97                 }
98                 if (!@move_uploaded_file($val['tmp_name'], $cols[$col]['target'])) {
99                         $colwarn[$col] = "Fout bij opslaan.";
100                 }
101                 foreach (@glob('thumb/*/') as $thumbres) {
102                         # attempt to remove old derivations
103                         @unlink($thumbres.'/'.$cols[$col]['target']);
104                 }
105                 $cols[$col]['value'] = '';
106         }
107
108         if (!empty($_POST['newpass'])) {
109                 require_once('login/pass.inc.php');
110                 if ($error = passform($user, $_POST)) {
111                         $colwarn['newpass'] = $error;
112                 }
113         }
114
115         if ($colwarn) {
116                 print "<p class=warn>Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.</p>\n\n";
117         }
118         else {
119                 print "<p>Alle instellingen zijn opgeslagen.</p>\n\n";
120         }
121 }
122
123 ?>
124 <form method="post" enctype="multipart/form-data">
125         <p>
126         Geef een e-mailadres op waarmee we u kunnen bereiken indien nodig.
127         Wij zullen dit adres nooit vrij- of doorgeven.
128         </p>
129         <ul class="grid">
130 <?php
131 foreach ($cols as $col => &$colconf) {
132         print "\t";
133         printf('<li><label for="%s">%s:</label>', $col, ucfirst($colconf['label']));
134         if (@$colconf['type'] == 'file' and isset($colconf['value'])) {
135                 printf('<a href="/%s"><img src="/thumb/%s/%s?%s" /></a><br />',
136                         $colconf['target'],
137                         200, $colconf['target'], filemtime($colconf['target'])
138                 );
139         }
140
141         if ($hide = @$colconf['hide'] and empty($_POST[$col])) {
142                 printf('<a onclick="%s">Wijzigen</a><span id="%s" hidden>',
143                         "document.getElementById('$hide').removeAttribute('hidden'); this.remove()",
144                         $hide
145                 );
146         }
147
148         if (isset($colconf['input'])) {
149                 print $colconf['input'];
150         }
151         else {
152                 $attrs = [
153                         'type'        => @$colconf['type'] ?: 'text',
154                         'name'        => $col,
155                         'id'          => $col,
156                         'value'       => htmlspecialchars(@$colconf['value']),
157                         'placeholder' => "Niet ingesteld",
158                         'readonly'    => empty($colconf['target']),
159                 ];
160                 if (@$colconf['type'] == 'file') {
161                         $attrs['accept'] = "image/jpeg";
162                 }
163
164                 print '<input';
165                 foreach ($attrs as $attr => $attrval) {
166                         if ($attrval === FALSE) {
167                                 continue;
168                         }
169                         print ' ' . $attr;
170                         if ($attrval !== TRUE) {
171                                 printf('="%s"', $attrval);
172                         }
173                 }
174                 print ' />';
175         }
176
177         if ($hide) {
178                 print '</span>';
179         }
180
181         if ($error = @$colwarn[$col]) {
182                 print " <span class=warn>$error</span>\n";
183         }
184         print "</li>\n";
185 }
186 ?>
187         </ul>
188         <p><input type="submit" value="Opslaan" /></p>
189 </form>