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