login/edit: generic setup of input attributes
[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['visible'])) {
21                 if ($colconf['visible'] == 'admin' and empty($User['admin'])) {
22                         $colconf['visible'] = FALSE;
23                         continue;
24                 }
25         }
26         else {
27                 $colconf['visible'] = TRUE;
28         }
29
30         if (!isset($colconf['filename'])) {
31                 continue;  # exceptional storage
32         }
33
34         if (isset($colconf['values'])) {
35                 if (!file_exists($colconf['filename'])) {
36                         $colconf['visible'] = FALSE;
37                         continue;
38                 }
39                 $tags = [];
40                 foreach (glob($colconf['filename'] . '/*') as $tag) {
41                         $tagname = pathinfo($tag, PATHINFO_BASENAME);
42                         $target = "$tag/{$user['name']}";
43                         $val = file_exists($target);
44                         $tagopt = &$colconf['values'][$tagname];
45                         $tagopt = ['value' => $val];
46                         if (!is_writable($tag)) {
47                                 continue;  # locked tag directory
48                         }
49                         if ($val and !is_writable($target)) {
50                                 continue;  # existing file locked
51                         }
52                         $tagopt['target'] = $target;
53                 }
54         }
55
56         $filetype = @$colconf['type'] == 'file' ? 'jpg' : 'txt';
57         $colpath = $user['dir'] . '/' . $colconf['filename'];
58         if (file_exists($colpath)) {
59                 $colconf['value'] = $filetype != 'txt' ? '' :
60                         file_get_contents($colpath);
61         }
62         if (file_exists($user['dir']) and !is_writable($user['dir'])) {
63                 continue;  # locked parent directory
64         }
65         if (isset($colconf['value']) and !is_writable($colpath)) {
66                 continue;  # locked column file
67         }
68         $colconf['target'] = $colpath;  # editing allowed
69 }
70
71 $colwarn = [];
72 if ($_POST) {
73         if (!file_exists($user['dir']) and !@mkdir($user['dir'])) {
74                 print "<p class=warn>Fout bij het aanmaken van gebruikersprofiel voor <em>{$user['name']}</em>.</p>\n\n";
75                 return;
76         }
77
78         foreach ($_POST as $col => $val) {
79                 if (!isset($cols[$col])) {
80                         continue; # unknown
81                 }
82                 if (isset($cols[$col]['values'])) {
83                         $optwarn = [];
84                         foreach ($val as $optcol => $optval) {
85                                 $option = &$cols[$col]['values'][$optcol];
86                                 if (!isset($option['target'])) {
87                                         $optok = FALSE;  # forbidden
88                                 }
89                                 if ($option['value'] === !empty($optval)) {
90                                         continue;  # unaltered
91                                 }
92                                 elseif (empty($optval)) {
93                                         $optok = @unlink($option['target']);
94                                 }
95                                 else {
96                                         # link option target to current user dir
97                                         $optok = @symlink("../../{$user['name']}", $option['target']);
98                                 }
99                                 $option['value'] = $optval;  # update form value
100                                 if (!$optok) {
101                                         $optwarn[$optcol] = TRUE;
102                                 }
103                         }
104                         if ($optwarn) {
105                                 $colwarn[$col] = "Wijziging niet opgeslagen voor "
106                                         . implode(', ', array_keys($optwarn));
107                         }
108                         continue;
109                 }
110
111                 if (isset($cols[$col]['filter'])) {
112                         list ($targetstr, $inputstr) = $cols[$col]['filter'];
113                         $val = str_replace($inputstr, $targetstr, $val);
114                 }
115                 if (isset($cols[$col]['value']) and $cols[$col]['value'] === $val) {
116                         continue; # unaltered
117                 }
118                 $cols[$col]['value'] = $val;  # update form value
119                 if (empty($cols[$col]['target'])) {
120                         if (empty($cols[$col]['input'])) {
121                                 $colwarn[$col] = "Kan niet worden aangepast.";
122                         }
123                         continue;
124                 }
125                 if (file_put_contents($cols[$col]['target'], $val) === FALSE) {
126                         $colwarn[$col] = "Fout bij opslaan.";
127                 }
128         }
129
130         foreach ($_FILES as $col => $val) {
131                 if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
132                         continue; # unknown
133                 }
134                 if (empty($cols[$col]['target'])) {
135                         $colwarn[$col] = "Kan niet worden aangepast.";
136                         continue;
137                 }
138                 try {
139                         require_once('upload.inc.php');
140                         $target = userupload($val, NULL, $cols[$col]['target']);
141                         if (!$target) continue;
142                         $cols[$col]['value'] = '';
143                 }
144                 catch (Exception $e) {
145                         $colwarn[$col] = ucfirst($e->getMessage()).'.';
146                 }
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         if (!$colconf['visible']) {
170                 continue;
171         }
172
173         print "\t";
174         printf('<li><label for="%s">%s:</label>', $col, ucfirst($colconf['label']));
175         if (@$colconf['type'] == 'file' and isset($colconf['value'])) {
176                 $target = $user['dir'] . '/' . $colconf['filename'];
177                 printf('<a href="/%s"><img src="/thumb/%s/%s?%s" /></a><br />',
178                         $target,
179                         200, $target, filemtime($target)
180                 );
181         }
182
183         if ($hide = @$colconf['hide'] and empty($_POST[$col])) {
184                 printf('<a onclick="%s">Wijzigen</a><span id="%s" hidden>',
185                         "document.getElementById('$hide').removeAttribute('hidden'); this.remove()",
186                         $hide
187                 );
188         }
189
190         if (isset($colconf['input'])) {
191                 print $colconf['input'];
192         }
193         elseif (isset($colconf['values'])) {
194                 foreach ($colconf['values'] as $tag => $val) {
195                         printf(
196                                 "\n\t\t" .
197                                 '<input type="hidden" name="%1$s" value="" />' .
198                                 '<input type="checkbox" name="%s" value="1" id="%s"%s%s />' .
199                                 '<label for="%2$s"> %s</label>',
200                                 "tags[$tag]", "tag-$tag",
201                                 $val['value'] ? ' checked' : '',
202                                 isset($val['target']) ? '' : ' readonly',
203                                 ucfirst($tag)
204                         );
205                 }
206         }
207         elseif (@$colconf['type'] !== 'file' or isset($colconf['target'])) {
208                 if (isset($cols[$col]['filter'])) {
209                         list ($targetstr, $inputstr) = $cols[$col]['filter'];
210                         $colconf['value'] = str_replace($targetstr, $inputstr, @$colconf['value']);
211                 }
212
213                 $attrs = [
214                         'type'        => @$colconf['type'] ?: 'text',
215                         'name'        => $col,
216                         'id'          => $col,
217                         'value'       => htmlspecialchars(@$colconf['value']),
218                         'placeholder' => "Niet ingesteld",
219                         'readonly'    => empty($colconf['target']),
220                 ] + (@$colconf['attr'] ?: []);
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>