login/edit: option to override tag labels
[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                         rtrim(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 (@$cols[$col]['type'] != 'file') {
126                         $val .= "\n"; # eol in text files
127                 }
128                 if (file_put_contents($cols[$col]['target'], $val) === FALSE) {
129                         $colwarn[$col] = "Fout bij opslaan.";
130                 }
131         }
132
133         foreach ($_FILES as $col => $val) {
134                 if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
135                         continue; # unknown
136                 }
137                 if (empty($cols[$col]['target'])) {
138                         $colwarn[$col] = "Kan niet worden aangepast.";
139                         continue;
140                 }
141                 try {
142                         require_once('upload.inc.php');
143                         $target = userupload($val, NULL, $cols[$col]['target']);
144                         if (!$target) continue;
145                         $cols[$col]['value'] = '';
146                 }
147                 catch (Exception $e) {
148                         $colwarn[$col] = ucfirst($e->getMessage()).'.';
149                 }
150         }
151
152         if (!empty($_POST['newpass'])) {
153                 require_once('login/pass.inc.php');
154                 if ($error = passform($user, $_POST)) {
155                         $colwarn['newpass'] = $error;
156                 }
157         }
158
159         if ($colwarn) {
160                 print "<p class=warn>Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.</p>\n\n";
161         }
162         else {
163                 print "<p>Alle instellingen zijn opgeslagen.</p>\n\n";
164         }
165 }
166
167 ?>
168 <form method="post" enctype="multipart/form-data">
169         <ul class="grid">
170 <?php
171 foreach ($cols as $col => &$colconf) {
172         if (!$colconf['visible']) {
173                 continue;
174         }
175
176         print "\t";
177         printf('<li><label for="%s">%s:</label>', $col, ucfirst($colconf['label']));
178         if (@$colconf['type'] == 'file' and isset($colconf['value'])) {
179                 $target = $user['dir'] . '/' . $colconf['filename'];
180                 printf('<a href="/%s"><img src="/thumb/%s/%s?%s" /></a><br />',
181                         $target,
182                         200, $target, filemtime($target)
183                 );
184         }
185
186         if ($hide = @$colconf['hide'] and empty($_POST[$col])) {
187                 printf('<a onclick="%s">Wijzigen</a><span id="%s" hidden>',
188                         "document.getElementById('$hide').removeAttribute('hidden'); this.remove()",
189                         $hide
190                 );
191         }
192
193         if (isset($colconf['input'])) {
194                 print $colconf['input'];
195         }
196         elseif (isset($colconf['values'])) {
197                 foreach ($colconf['values'] as $tag => $val) {
198                         printf(
199                                 "\n\t\t" .
200                                 '<input type="hidden" name="%1$s" value="" />' .
201                                 '<input type="checkbox" name="%s" value="1" id="%s"%s%s />' .
202                                 '<label for="%2$s"> %s</label>',
203                                 "tags[$tag]", "tag-$tag",
204                                 !empty($val['value']) ? ' checked' : '',
205                                 isset($val['target']) ? '' : ' readonly',
206                                 @$val['label'] ?: ucfirst($tag)
207                         );
208                 }
209         }
210         elseif (@$colconf['type'] !== 'file' or isset($colconf['target'])) {
211                 if (isset($cols[$col]['filter'])) {
212                         list ($targetstr, $inputstr) = $cols[$col]['filter'];
213                         $colconf['value'] = str_replace($targetstr, $inputstr, @$colconf['value']);
214                 }
215
216                 $attrs = [
217                         'type'        => @$colconf['type'] ?: 'text',
218                         'name'        => $col,
219                         'id'          => $col,
220                         'value'       => htmlspecialchars(@$colconf['value']),
221                         'placeholder' => "Niet ingesteld",
222                         'readonly'    => empty($colconf['target']),
223                 ] + (@$colconf['attr'] ?: []);
224
225                 print '<input';
226                 foreach ($attrs as $attr => $attrval) {
227                         if ($attrval === FALSE) {
228                                 continue;
229                         }
230                         print ' ' . $attr;
231                         if ($attrval !== TRUE) {
232                                 printf('="%s"', $attrval);
233                         }
234                 }
235                 print ' />';
236         }
237
238         if (!empty($colconf['explain'])) {
239                 printf(' <span>(%s)</span>', $colconf['explain']);
240         }
241
242         if ($hide) {
243                 print '</span>';
244         }
245
246         if ($error = @$colwarn[$col]) {
247                 print " <span class=warn>$error</span>\n";
248         }
249         print "</li>\n";
250 }
251 ?>
252         </ul>
253         <p><input type="submit" value="Opslaan" /></p>
254 </form>