login/edit: generic multi-values declaration
[minimedit.git] / login / edit.php
1 <?php
2 global $User;
3 if (empty($user = &$User)) {
4         return;
5 }
6
7 if (!empty($User['admin'])
8 and !empty($Place['user']) and $Place['user'] !== $User['name']) {
9         $username = strtolower($Place['user']);
10         unset($user);
11         $user = [
12                 'dir' => "profile/$username",
13                 'name' => $username,
14         ];
15 }
16
17 $cols = [
18         'name'  => [
19                 'label' => 'volledige naam',
20                 'explain' => "Alleen zichtbaar voor andere leden.",
21                 'filter' => ["\n", '; '],
22                 'size' => 30,
23                 'filename' => 'name.txt',
24         ],
25         'email' => [
26                 'label' => 'e-mailadres',
27                 'type' => 'email',
28                 'explain' => "Voor contact van of met deze site. Wij zullen dit nooit vrij- of doorgeven.",
29                 'size' => 30,
30                 'filename' => 'email.txt',
31         ],
32         'avatar' => [
33                 'label' => 'portretfoto',
34                 'type' => 'file',
35                 'filename' => 'avatar.jpg',
36         ],
37         'tags' => [
38                 'label' => 'groepen',
39                 'values' => [],
40                 'filename' => 'profile/.tags',
41         ],
42 ];
43
44 foreach ($cols as $col => &$colconf) {
45         if (!isset($colconf['filename'])) {
46                 continue;  # exceptional storage
47         }
48
49         if (isset($colconf['values'])) {
50                 if (!file_exists($colconf['filename'])) {
51                         continue;
52                         #TODO: drop key
53                 }
54                 $tags = [];
55                 foreach (glob($colconf['filename'] . '/*') as $tag) {
56                         $tagname = pathinfo($tag, PATHINFO_BASENAME);
57                         $target = "$tag/{$user['name']}";
58                         $val = file_exists($target);
59                         $tags[$tagname] = ['value' => $val];
60                         if (empty($User['admin'])) {
61                                 continue;  # forbidden
62                         }
63                         if (!is_writable($tag)) {
64                                 continue;  # locked tag directory
65                         }
66                         if ($val and !is_writable($target)) {
67                                 continue;  # existing file locked
68                         }
69                         $tags[$tagname]['target'] = $target;
70                 }
71         }
72
73         $filetype = @$colconf['type'] == 'file' ? 'jpg' : 'txt';
74         $colpath = $user['dir'] . '/' . $colconf['filename'];
75         if (file_exists($colpath)) {
76                 $colconf['value'] = $filetype != 'txt' ? '' :
77                         file_get_contents($colpath);
78         }
79         if (file_exists($user['dir']) and !is_writable($user['dir'])) {
80                 continue;  # locked parent directory
81         }
82         if (isset($colconf['value']) and !is_writable($colpath)) {
83                 continue;  # locked column file
84         }
85         $colconf['target'] = $colpath;  # editing allowed
86 }
87
88 $cols = [
89         'username' => [
90                 'label' => 'login',
91                 'value' => $user['name'],
92                 'target' => NULL,
93                 'pattern' => "[a-z0-9-]+",
94                 'size' => 10,
95         ],
96 ] + $cols;
97
98 if (isset($user['pass'])) {
99         $cols['newpass'] = [
100                 'label' => 'wachtwoord',
101                 'input' => <<<'EOT'
102                         <input type="password" name="oldpass" value="" placeholder="Huidig wachtwoord" />
103                         <input type="password" id="newpass" name="newpass" value="" placeholder="Nieuw wachtwoord" />
104                         <input type="password" name="passconf" value="" placeholder="Nogmaals" />
105 EOT
106                 ,
107                 'hide'  => 'pass',
108         ];
109 }
110
111 $colwarn = [];
112 if ($_POST) {
113         if (!file_exists($user['dir']) and !@mkdir($user['dir'])) {
114                 print "<p class=warn>Fout bij het aanmaken van gebruikersprofiel voor <em>{$user['name']}</em>.</p>\n\n";
115                 return;
116         }
117
118         foreach ($_POST as $col => $val) {
119                 if (!isset($cols[$col])) {
120                         continue; # unknown
121                 }
122                 if (isset($cols[$col]['values'])) {
123                         $optwarn = [];
124                         foreach ($val as $optcol => $optval) {
125                                 $option = &$cols[$col]['values'][$optcol];
126                                 if (!isset($option['target'])) {
127                                         $optok = FALSE;  # forbidden
128                                 }
129                                 if ($option['value'] === !empty($optval)) {
130                                         continue;  # unaltered
131                                 }
132                                 elseif (empty($optval)) {
133                                         $optok = @unlink($option['target']);
134                                 }
135                                 else {
136                                         # link option target to current user dir
137                                         $optok = @symlink("../../{$user['name']}", $option['target']);
138                                 }
139                                 $option['value'] = $optval;  # update form value
140                                 if (!$optok) {
141                                         $optwarn[$optcol] = TRUE;
142                                 }
143                         }
144                         if ($optwarn) {
145                                 $colwarn[$col] = "Wijziging niet opgeslagen voor "
146                                         . implode(', ', array_keys($optwarn));
147                         }
148                         continue;
149                 }
150
151                 if (isset($cols[$col]['filter'])) {
152                         list ($targetstr, $inputstr) = $cols[$col]['filter'];
153                         $val = str_replace($inputstr, $targetstr, $val);
154                 }
155                 if (isset($cols[$col]['value']) and $cols[$col]['value'] === $val) {
156                         continue; # unaltered
157                 }
158                 $cols[$col]['value'] = $val;  # update form value
159                 if (empty($cols[$col]['target'])) {
160                         if (empty($cols[$col]['input'])) {
161                                 $colwarn[$col] = "Kan niet worden aangepast.";
162                         }
163                         continue;
164                 }
165                 if (file_put_contents($cols[$col]['target'], $val) === FALSE) {
166                         $colwarn[$col] = "Fout bij opslaan.";
167                 }
168         }
169
170         foreach ($_FILES as $col => $val) {
171                 if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
172                         continue; # unknown
173                 }
174                 switch ($val['error']) {
175                 case UPLOAD_ERR_OK:
176                         break;
177                 case UPLOAD_ERR_NO_FILE:
178                         continue 2; # current
179                 default:
180                         $colwarn[$col] = "Afbeelding niet goed ontvangen.";
181                         continue 2;
182                 }
183                 if (empty($cols[$col]['target'])) {
184                         $colwarn[$col] = "Kan niet worden aangepast.";
185                         continue;
186                 }
187                 if (!@move_uploaded_file($val['tmp_name'], $cols[$col]['target'])) {
188                         $colwarn[$col] = "Fout bij opslaan.";
189                 }
190                 foreach (@glob('thumb/*/') as $thumbres) {
191                         # attempt to remove old derivations
192                         @unlink($thumbres.'/'.$cols[$col]['target']);
193                 }
194                 $cols[$col]['value'] = '';
195         }
196
197         if (!empty($_POST['newpass'])) {
198                 require_once('login/pass.inc.php');
199                 if ($error = passform($user, $_POST)) {
200                         $colwarn['newpass'] = $error;
201                 }
202         }
203
204         if ($colwarn) {
205                 print "<p class=warn>Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.</p>\n\n";
206         }
207         else {
208                 print "<p>Alle instellingen zijn opgeslagen.</p>\n\n";
209         }
210 }
211
212 ?>
213 <form method="post" enctype="multipart/form-data">
214         <ul class="grid">
215 <?php
216 foreach ($cols as $col => &$colconf) {
217         print "\t";
218         printf('<li><label for="%s">%s:</label>', $col, ucfirst($colconf['label']));
219         if (@$colconf['type'] == 'file' and isset($colconf['value'])) {
220                 printf('<a href="/%s"><img src="/thumb/%s/%s?%s" /></a><br />',
221                         $colconf['target'],
222                         200, $colconf['target'], filemtime($colconf['target'])
223                 );
224         }
225
226         if ($hide = @$colconf['hide'] and empty($_POST[$col])) {
227                 printf('<a onclick="%s">Wijzigen</a><span id="%s" hidden>',
228                         "document.getElementById('$hide').removeAttribute('hidden'); this.remove()",
229                         $hide
230                 );
231         }
232
233         if (isset($colconf['input'])) {
234                 print $colconf['input'];
235         }
236         elseif (isset($colconf['values'])) {
237                 foreach ($colconf['values'] as $tag => $val) {
238                         printf(
239                                 "\n\t\t" .
240                                 '<input type="hidden" name="%1$s" value="" />' .
241                                 '<input type="checkbox" name="%s" value="1" id="%s"%s%s />' .
242                                 '<label for="%2$s"> %s</label>',
243                                 "tags[$tag]", "tag-$tag",
244                                 $val['value'] ? ' checked' : '',
245                                 isset($val['target']) ? '' : ' readonly',
246                                 ucfirst($tag)
247                         );
248                 }
249         }
250         else {
251                 if (isset($cols[$col]['filter'])) {
252                         list ($targetstr, $inputstr) = $cols[$col]['filter'];
253                         $colconf['value'] = str_replace($targetstr, $inputstr, $colconf['value']);
254                 }
255
256                 $attrs = [
257                         'type'        => @$colconf['type'] ?: 'text',
258                         'name'        => $col,
259                         'id'          => $col,
260                         'value'       => htmlspecialchars(@$colconf['value']),
261                         'placeholder' => "Niet ingesteld",
262                         'readonly'    => empty($colconf['target']),
263                         'pattern'     => @$colconf['pattern'] ?: FALSE,
264                         'size'        => @$colconf['size'] ?: FALSE,
265                 ];
266                 if (@$colconf['type'] == 'file') {
267                         $attrs['accept'] = "image/jpeg";
268                 }
269
270                 print '<input';
271                 foreach ($attrs as $attr => $attrval) {
272                         if ($attrval === FALSE) {
273                                 continue;
274                         }
275                         print ' ' . $attr;
276                         if ($attrval !== TRUE) {
277                                 printf('="%s"', $attrval);
278                         }
279                 }
280                 print ' />';
281         }
282
283         if (!empty($colconf['explain'])) {
284                 printf(' <span>(%s)</span>', $colconf['explain']);
285         }
286
287         if ($hide) {
288                 print '</span>';
289         }
290
291         if ($error = @$colwarn[$col]) {
292                 print " <span class=warn>$error</span>\n";
293         }
294         print "</li>\n";
295 }
296 ?>
297         </ul>
298         <p><input type="submit" value="Opslaan" /></p>
299 </form>