login/edit: silence alter error for custom input
[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 $tagdir = 'profile/.tags';
57 if (file_exists($tagdir)) {
58         $tags = [];
59         foreach (glob("$tagdir/*") as $tag) {
60                 $tagname = pathinfo($tag, PATHINFO_BASENAME);
61                 $target = "$tag/{$user['name']}";
62                 $val = file_exists($target);
63                 $tags[$tagname] = ['value' => $val];
64                 if (empty($User['admin'])) {
65                         continue;  # forbidden
66                 }
67                 if (!is_writable($tag)) {
68                         continue;  # locked tag directory
69                 }
70                 if ($val and !is_writable($target)) {
71                         continue;  # existing file locked
72                 }
73                 $tags[$tagname]['target'] = $target;
74         }
75
76         if ($tags) {
77                 $cols['tags'] = [
78                         'label' => 'groepen',
79                         'values' => $tags,
80                 ];
81         }
82 }
83
84 if (isset($user['pass'])) {
85         $cols['newpass'] = [
86                 'label' => 'wachtwoord',
87                 'input' => <<<'EOT'
88                         <input type="password" name="oldpass" value="" placeholder="Huidig wachtwoord" />
89                         <input type="password" id="newpass" name="newpass" value="" placeholder="Nieuw wachtwoord" />
90                         <input type="password" name="passconf" value="" placeholder="Nogmaals" />
91 EOT
92                 ,
93                 'hide'  => 'pass',
94         ];
95 }
96
97 $colwarn = [];
98 if ($_POST) {
99         if (!file_exists($user['dir']) and !@mkdir($user['dir'])) {
100                 print "<p class=warn>Fout bij het aanmaken van gebruikersprofiel voor <em>{$user['name']}</em>.</p>\n\n";
101                 return;
102         }
103
104         foreach ($_POST as $col => $val) {
105                 if (!isset($cols[$col])) {
106                         continue; # unknown
107                 }
108                 if (isset($cols[$col]['values'])) {
109                         $optwarn = [];
110                         foreach ($val as $optcol => $optval) {
111                                 $option = &$cols[$col]['values'][$optcol];
112                                 if (!isset($option['target'])) {
113                                         $optok = FALSE;  # forbidden
114                                 }
115                                 if ($option['value'] === !empty($optval)) {
116                                         continue;  # unaltered
117                                 }
118                                 elseif (empty($optval)) {
119                                         $optok = @unlink($option['target']);
120                                 }
121                                 else {
122                                         # link option target to current user dir
123                                         $optok = @symlink("../../{$user['name']}", $option['target']);
124                                 }
125                                 $option['value'] = $optval;  # update form value
126                                 if (!$optok) {
127                                         $optwarn[$optcol] = TRUE;
128                                 }
129                         }
130                         if ($optwarn) {
131                                 $colwarn[$col] = "Wijziging niet opgeslagen voor "
132                                         . implode(', ', array_keys($optwarn));
133                         }
134                         continue;
135                 }
136                 if (isset($cols[$col]['value']) and $cols[$col]['value'] === $val) {
137                         continue; # unaltered
138                 }
139                 $cols[$col]['value'] = $val;  # update form value
140                 if (empty($cols[$col]['target'])) {
141                         if (empty($cols[$col]['input'])) {
142                                 $colwarn[$col] = "Kan niet worden aangepast.";
143                         }
144                         continue;
145                 }
146                 if (file_put_contents($cols[$col]['target'], $val) === FALSE) {
147                         $colwarn[$col] = "Fout bij opslaan.";
148                 }
149         }
150
151         foreach ($_FILES as $col => $val) {
152                 if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
153                         continue; # unknown
154                 }
155                 switch ($val['error']) {
156                 case UPLOAD_ERR_OK:
157                         break;
158                 case UPLOAD_ERR_NO_FILE:
159                         continue 2; # current
160                 default:
161                         $colwarn[$col] = "Afbeelding niet goed ontvangen.";
162                         continue 2;
163                 }
164                 if (empty($cols[$col]['target'])) {
165                         $colwarn[$col] = "Kan niet worden aangepast.";
166                         continue;
167                 }
168                 if (!@move_uploaded_file($val['tmp_name'], $cols[$col]['target'])) {
169                         $colwarn[$col] = "Fout bij opslaan.";
170                 }
171                 foreach (@glob('thumb/*/') as $thumbres) {
172                         # attempt to remove old derivations
173                         @unlink($thumbres.'/'.$cols[$col]['target']);
174                 }
175                 $cols[$col]['value'] = '';
176         }
177
178         if (!empty($_POST['newpass'])) {
179                 require_once('login/pass.inc.php');
180                 if ($error = passform($user, $_POST)) {
181                         $colwarn['newpass'] = $error;
182                 }
183         }
184
185         if ($colwarn) {
186                 print "<p class=warn>Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.</p>\n\n";
187         }
188         else {
189                 print "<p>Alle instellingen zijn opgeslagen.</p>\n\n";
190         }
191 }
192
193 ?>
194 <form method="post" enctype="multipart/form-data">
195         <ul class="grid">
196 <?php
197 foreach ($cols as $col => &$colconf) {
198         print "\t";
199         printf('<li><label for="%s">%s:</label>', $col, ucfirst($colconf['label']));
200         if (@$colconf['type'] == 'file' and isset($colconf['value'])) {
201                 printf('<a href="/%s"><img src="/thumb/%s/%s?%s" /></a><br />',
202                         $colconf['target'],
203                         200, $colconf['target'], filemtime($colconf['target'])
204                 );
205         }
206
207         if ($hide = @$colconf['hide'] and empty($_POST[$col])) {
208                 printf('<a onclick="%s">Wijzigen</a><span id="%s" hidden>',
209                         "document.getElementById('$hide').removeAttribute('hidden'); this.remove()",
210                         $hide
211                 );
212         }
213
214         if (isset($colconf['input'])) {
215                 print $colconf['input'];
216         }
217         elseif (isset($colconf['values'])) {
218                 foreach ($colconf['values'] as $tag => $val) {
219                         printf(
220                                 "\n\t\t" .
221                                 '<input type="hidden" name="%1$s" value="" />' .
222                                 '<input type="checkbox" name="%s" value="1" id="%s"%s%s />' .
223                                 '<label for="%2$s"> %s</label>',
224                                 "tags[$tag]", "tag-$tag",
225                                 $val['value'] ? ' checked' : '',
226                                 isset($val['target']) ? '' : ' readonly',
227                                 ucfirst($tag)
228                         );
229                 }
230         }
231         else {
232                 $attrs = [
233                         'type'        => @$colconf['type'] ?: 'text',
234                         'name'        => $col,
235                         'id'          => $col,
236                         'value'       => htmlspecialchars(@$colconf['value']),
237                         'placeholder' => "Niet ingesteld",
238                         'readonly'    => empty($colconf['target']),
239                         'pattern'     => @$colconf['pattern'] ?: FALSE,
240                 ];
241                 if (@$colconf['type'] == 'file') {
242                         $attrs['accept'] = "image/jpeg";
243                 }
244
245                 print '<input';
246                 foreach ($attrs as $attr => $attrval) {
247                         if ($attrval === FALSE) {
248                                 continue;
249                         }
250                         print ' ' . $attr;
251                         if ($attrval !== TRUE) {
252                                 printf('="%s"', $attrval);
253                         }
254                 }
255                 print ' />';
256         }
257
258         if (!empty($colconf['explain'])) {
259                 printf(' <span>(%s)</span>', $colconf['explain']);
260         }
261
262         if ($hide) {
263                 print '</span>';
264         }
265
266         if ($error = @$colwarn[$col]) {
267                 print " <span class=warn>$error</span>\n";
268         }
269         print "</li>\n";
270 }
271 ?>
272         </ul>
273         <p><input type="submit" value="Opslaan" /></p>
274 </form>