login/edit: apply tags input to target links
[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                 $options = '';
78                 foreach ($tags as $tag => $val) {
79                         $options .= sprintf(
80                                 "\n\t\t" .
81                                 '<input type="hidden" name="tags[%1$s]" value="" />' .
82                                 '<input type="checkbox" name="tags[%s]" value="1"%s%s /> %s',
83                                 $tag,
84                                 $val['value'] ? ' checked' : '',
85                                 isset($val['target']) ? '' : ' readonly',
86                                 ucfirst($tag)
87                         );
88                 }
89
90                 $cols['tags'] = [
91                         'label' => 'groepen',
92                         'input' => $options,
93                         'values' => $tags,
94                 ];
95         }
96 }
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]['value']) and $cols[$col]['value'] === $val) {
123                         continue; # unaltered
124                 }
125                 $cols[$col]['value'] = $val;  # update form value
126                 if (empty($cols[$col]['target'])) {
127                         $colwarn[$col] = "Kan niet worden aangepast.";
128                         continue;
129                 }
130                 if (file_put_contents($cols[$col]['target'], $val) === FALSE) {
131                         $colwarn[$col] = "Fout bij opslaan.";
132                 }
133         }
134
135         if (isset($cols['tags']) and !empty($_POST['tags'])) {
136                 $tagok = [];
137                 foreach ($_POST['tags'] as $col => $val) {
138                         $tag = $cols['tags']['values'][$col];
139                         if (!isset($tag['target'])) {
140                                 $tagok[$col] = 'forbidden';
141                         }
142                         if ($tag['value'] === !empty($val)) {
143                                 $tagok[$col] = NULL;  # unaltered
144                         }
145                         elseif (empty($val)) {
146                                 $tagok[$col] = !@unlink($tag['target']);
147                         }
148                         else {
149                                 $tagok[$col] = !@symlink("../../{$user['name']}", $tag['target']);
150                         }
151                 }
152                 if ($tagok = array_filter($tagok)) {
153                         $colwarn['tags'] = "Wijziging niet opgeslagen voor "
154                                 . implode(', ', array_keys($tagok));
155                 }
156         }
157
158         foreach ($_FILES as $col => $val) {
159                 if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
160                         continue; # unknown
161                 }
162                 switch ($val['error']) {
163                 case UPLOAD_ERR_OK:
164                         break;
165                 case UPLOAD_ERR_NO_FILE:
166                         continue 2; # current
167                 default:
168                         $colwarn[$col] = "Afbeelding niet goed ontvangen.";
169                         continue 2;
170                 }
171                 if (empty($cols[$col]['target'])) {
172                         $colwarn[$col] = "Kan niet worden aangepast.";
173                         continue;
174                 }
175                 if (!@move_uploaded_file($val['tmp_name'], $cols[$col]['target'])) {
176                         $colwarn[$col] = "Fout bij opslaan.";
177                 }
178                 foreach (@glob('thumb/*/') as $thumbres) {
179                         # attempt to remove old derivations
180                         @unlink($thumbres.'/'.$cols[$col]['target']);
181                 }
182                 $cols[$col]['value'] = '';
183         }
184
185         if (!empty($_POST['newpass'])) {
186                 require_once('login/pass.inc.php');
187                 if ($error = passform($user, $_POST)) {
188                         $colwarn['newpass'] = $error;
189                 }
190         }
191
192         if ($colwarn) {
193                 print "<p class=warn>Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.</p>\n\n";
194         }
195         else {
196                 print "<p>Alle instellingen zijn opgeslagen.</p>\n\n";
197         }
198 }
199
200 ?>
201 <form method="post" enctype="multipart/form-data">
202         <ul class="grid">
203 <?php
204 foreach ($cols as $col => &$colconf) {
205         print "\t";
206         printf('<li><label for="%s">%s:</label>', $col, ucfirst($colconf['label']));
207         if (@$colconf['type'] == 'file' and isset($colconf['value'])) {
208                 printf('<a href="/%s"><img src="/thumb/%s/%s?%s" /></a><br />',
209                         $colconf['target'],
210                         200, $colconf['target'], filemtime($colconf['target'])
211                 );
212         }
213
214         if ($hide = @$colconf['hide'] and empty($_POST[$col])) {
215                 printf('<a onclick="%s">Wijzigen</a><span id="%s" hidden>',
216                         "document.getElementById('$hide').removeAttribute('hidden'); this.remove()",
217                         $hide
218                 );
219         }
220
221         if (isset($colconf['input'])) {
222                 print $colconf['input'];
223         }
224         else {
225                 $attrs = [
226                         'type'        => @$colconf['type'] ?: 'text',
227                         'name'        => $col,
228                         'id'          => $col,
229                         'value'       => htmlspecialchars(@$colconf['value']),
230                         'placeholder' => "Niet ingesteld",
231                         'readonly'    => empty($colconf['target']),
232                         'pattern'     => @$colconf['pattern'] ?: FALSE,
233                 ];
234                 if (@$colconf['type'] == 'file') {
235                         $attrs['accept'] = "image/jpeg";
236                 }
237
238                 print '<input';
239                 foreach ($attrs as $attr => $attrval) {
240                         if ($attrval === FALSE) {
241                                 continue;
242                         }
243                         print ' ' . $attr;
244                         if ($attrval !== TRUE) {
245                                 printf('="%s"', $attrval);
246                         }
247                 }
248                 print ' />';
249         }
250         if (!empty($colconf['explain'])) {
251                 printf(' <span>(%s)</span>', $colconf['explain']);
252         }
253
254         if ($hide) {
255                 print '</span>';
256         }
257
258         if ($error = @$colwarn[$col]) {
259                 print " <span class=warn>$error</span>\n";
260         }
261         print "</li>\n";
262 }
263 ?>
264         </ul>
265         <p><input type="submit" value="Opslaan" /></p>
266 </form>