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