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