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