login/edit: filter to replace newlines in input values
[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         ],
22         'email' => [
23                 'label' => 'e-mailadres',
24                 'type' => 'email',
25                 'explain' => "Voor contact van of met deze site. Wij zullen dit nooit vrij- of doorgeven.",
26         ],
27         'avatar' => [
28                 'label' => 'portretfoto',
29                 'type' => 'file',
30         ],
31 ];
32
33 foreach ($cols as $col => &$colconf) {
34         $filetype = @$colconf['type'] == 'file' ? 'jpg' : 'txt';
35         $colpath = "{$user['dir']}/$col.$filetype";
36         if (file_exists($colpath)) {
37                 $colconf['value'] = $filetype != 'txt' ? '' :
38                         file_get_contents($colpath);
39         }
40         if (file_exists($user['dir']) and !is_writable($user['dir'])) {
41                 continue;  # locked parent directory
42         }
43         if (isset($colconf['value']) and !is_writable($colpath)) {
44                 continue;  # locked column file
45         }
46         $colconf['target'] = $colpath;  # editing allowed
47 }
48
49 $cols = [
50         'username' => [
51                 'label' => 'login',
52                 'value' => $user['name'],
53                 'target' => NULL,
54                 'pattern' => "[a-z0-9-]+",
55         ],
56 ] + $cols;
57
58 $tagdir = 'profile/.tags';
59 if (file_exists($tagdir)) {
60         $tags = [];
61         foreach (glob("$tagdir/*") as $tag) {
62                 $tagname = pathinfo($tag, PATHINFO_BASENAME);
63                 $target = "$tag/{$user['name']}";
64                 $val = file_exists($target);
65                 $tags[$tagname] = ['value' => $val];
66                 if (empty($User['admin'])) {
67                         continue;  # forbidden
68                 }
69                 if (!is_writable($tag)) {
70                         continue;  # locked tag directory
71                 }
72                 if ($val and !is_writable($target)) {
73                         continue;  # existing file locked
74                 }
75                 $tags[$tagname]['target'] = $target;
76         }
77
78         if ($tags) {
79                 $cols['tags'] = [
80                         'label' => 'groepen',
81                         'values' => $tags,
82                 ];
83         }
84 }
85
86 if (isset($user['pass'])) {
87         $cols['newpass'] = [
88                 'label' => 'wachtwoord',
89                 'input' => <<<'EOT'
90                         <input type="password" name="oldpass" value="" placeholder="Huidig wachtwoord" />
91                         <input type="password" id="newpass" name="newpass" value="" placeholder="Nieuw wachtwoord" />
92                         <input type="password" name="passconf" value="" placeholder="Nogmaals" />
93 EOT
94                 ,
95                 'hide'  => 'pass',
96         ];
97 }
98
99 $colwarn = [];
100 if ($_POST) {
101         if (!file_exists($user['dir']) and !@mkdir($user['dir'])) {
102                 print "<p class=warn>Fout bij het aanmaken van gebruikersprofiel voor <em>{$user['name']}</em>.</p>\n\n";
103                 return;
104         }
105
106         foreach ($_POST as $col => $val) {
107                 if (!isset($cols[$col])) {
108                         continue; # unknown
109                 }
110                 if (isset($cols[$col]['values'])) {
111                         $optwarn = [];
112                         foreach ($val as $optcol => $optval) {
113                                 $option = &$cols[$col]['values'][$optcol];
114                                 if (!isset($option['target'])) {
115                                         $optok = FALSE;  # forbidden
116                                 }
117                                 if ($option['value'] === !empty($optval)) {
118                                         continue;  # unaltered
119                                 }
120                                 elseif (empty($optval)) {
121                                         $optok = @unlink($option['target']);
122                                 }
123                                 else {
124                                         # link option target to current user dir
125                                         $optok = @symlink("../../{$user['name']}", $option['target']);
126                                 }
127                                 $option['value'] = $optval;  # update form value
128                                 if (!$optok) {
129                                         $optwarn[$optcol] = TRUE;
130                                 }
131                         }
132                         if ($optwarn) {
133                                 $colwarn[$col] = "Wijziging niet opgeslagen voor "
134                                         . implode(', ', array_keys($optwarn));
135                         }
136                         continue;
137                 }
138
139                 if (isset($cols[$col]['filter'])) {
140                         list ($targetstr, $inputstr) = $cols[$col]['filter'];
141                         $val = str_replace($inputstr, $targetstr, $val);
142                 }
143                 if (isset($cols[$col]['value']) and $cols[$col]['value'] === $val) {
144                         continue; # unaltered
145                 }
146                 $cols[$col]['value'] = $val;  # update form value
147                 if (empty($cols[$col]['target'])) {
148                         if (empty($cols[$col]['input'])) {
149                                 $colwarn[$col] = "Kan niet worden aangepast.";
150                         }
151                         continue;
152                 }
153                 if (file_put_contents($cols[$col]['target'], $val) === FALSE) {
154                         $colwarn[$col] = "Fout bij opslaan.";
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         elseif (isset($colconf['values'])) {
225                 foreach ($colconf['values'] as $tag => $val) {
226                         printf(
227                                 "\n\t\t" .
228                                 '<input type="hidden" name="%1$s" value="" />' .
229                                 '<input type="checkbox" name="%s" value="1" id="%s"%s%s />' .
230                                 '<label for="%2$s"> %s</label>',
231                                 "tags[$tag]", "tag-$tag",
232                                 $val['value'] ? ' checked' : '',
233                                 isset($val['target']) ? '' : ' readonly',
234                                 ucfirst($tag)
235                         );
236                 }
237         }
238         else {
239                 if (isset($cols[$col]['filter'])) {
240                         list ($targetstr, $inputstr) = $cols[$col]['filter'];
241                         $colconf['value'] = str_replace($targetstr, $inputstr, $colconf['value']);
242                 }
243
244                 $attrs = [
245                         'type'        => @$colconf['type'] ?: 'text',
246                         'name'        => $col,
247                         'id'          => $col,
248                         'value'       => htmlspecialchars(@$colconf['value']),
249                         'placeholder' => "Niet ingesteld",
250                         'readonly'    => empty($colconf['target']),
251                         'pattern'     => @$colconf['pattern'] ?: FALSE,
252                 ];
253                 if (@$colconf['type'] == 'file') {
254                         $attrs['accept'] = "image/jpeg";
255                 }
256
257                 print '<input';
258                 foreach ($attrs as $attr => $attrval) {
259                         if ($attrval === FALSE) {
260                                 continue;
261                         }
262                         print ' ' . $attr;
263                         if ($attrval !== TRUE) {
264                                 printf('="%s"', $attrval);
265                         }
266                 }
267                 print ' />';
268         }
269
270         if (!empty($colconf['explain'])) {
271                 printf(' <span>(%s)</span>', $colconf['explain']);
272         }
273
274         if ($hide) {
275                 print '</span>';
276         }
277
278         if ($error = @$colwarn[$col]) {
279                 print " <span class=warn>$error</span>\n";
280         }
281         print "</li>\n";
282 }
283 ?>
284         </ul>
285         <p><input type="submit" value="Opslaan" /></p>
286 </form>