9b339a22dedf1cd8121bbda10777931190137194
[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                         $tags[$tagname] = ['value' => $val];
45                         if (!is_writable($tag)) {
46                                 continue;  # locked tag directory
47                         }
48                         if ($val and !is_writable($target)) {
49                                 continue;  # existing file locked
50                         }
51                         $tags[$tagname]['target'] = $target;
52                 }
53         }
54
55         $filetype = @$colconf['type'] == 'file' ? 'jpg' : 'txt';
56         $colpath = $user['dir'] . '/' . $colconf['filename'];
57         if (file_exists($colpath)) {
58                 $colconf['value'] = $filetype != 'txt' ? '' :
59                         file_get_contents($colpath);
60         }
61         if (file_exists($user['dir']) and !is_writable($user['dir'])) {
62                 continue;  # locked parent directory
63         }
64         if (isset($colconf['value']) and !is_writable($colpath)) {
65                 continue;  # locked column file
66         }
67         $colconf['target'] = $colpath;  # editing allowed
68 }
69
70 $colwarn = [];
71 if ($_POST) {
72         if (!file_exists($user['dir']) and !@mkdir($user['dir'])) {
73                 print "<p class=warn>Fout bij het aanmaken van gebruikersprofiel voor <em>{$user['name']}</em>.</p>\n\n";
74                 return;
75         }
76
77         foreach ($_POST as $col => $val) {
78                 if (!isset($cols[$col])) {
79                         continue; # unknown
80                 }
81                 if (isset($cols[$col]['values'])) {
82                         $optwarn = [];
83                         foreach ($val as $optcol => $optval) {
84                                 $option = &$cols[$col]['values'][$optcol];
85                                 if (!isset($option['target'])) {
86                                         $optok = FALSE;  # forbidden
87                                 }
88                                 if ($option['value'] === !empty($optval)) {
89                                         continue;  # unaltered
90                                 }
91                                 elseif (empty($optval)) {
92                                         $optok = @unlink($option['target']);
93                                 }
94                                 else {
95                                         # link option target to current user dir
96                                         $optok = @symlink("../../{$user['name']}", $option['target']);
97                                 }
98                                 $option['value'] = $optval;  # update form value
99                                 if (!$optok) {
100                                         $optwarn[$optcol] = TRUE;
101                                 }
102                         }
103                         if ($optwarn) {
104                                 $colwarn[$col] = "Wijziging niet opgeslagen voor "
105                                         . implode(', ', array_keys($optwarn));
106                         }
107                         continue;
108                 }
109
110                 if (isset($cols[$col]['filter'])) {
111                         list ($targetstr, $inputstr) = $cols[$col]['filter'];
112                         $val = str_replace($inputstr, $targetstr, $val);
113                 }
114                 if (isset($cols[$col]['value']) and $cols[$col]['value'] === $val) {
115                         continue; # unaltered
116                 }
117                 $cols[$col]['value'] = $val;  # update form value
118                 if (empty($cols[$col]['target'])) {
119                         if (empty($cols[$col]['input'])) {
120                                 $colwarn[$col] = "Kan niet worden aangepast.";
121                         }
122                         continue;
123                 }
124                 if (file_put_contents($cols[$col]['target'], $val) === FALSE) {
125                         $colwarn[$col] = "Fout bij opslaan.";
126                 }
127         }
128
129         foreach ($_FILES as $col => $val) {
130                 if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
131                         continue; # unknown
132                 }
133                 if (empty($cols[$col]['target'])) {
134                         $colwarn[$col] = "Kan niet worden aangepast.";
135                         continue;
136                 }
137                 try {
138                         require_once('upload.inc.php');
139                         $target = userupload($val, NULL, $cols[$col]['target']);
140                         if (!$target) continue;
141                         $cols[$col]['value'] = '';
142                 }
143                 catch (Exception $e) {
144                         $colwarn[$col] = ucfirst($e->getMessage()).'.';
145                 }
146         }
147
148         if (!empty($_POST['newpass'])) {
149                 require_once('login/pass.inc.php');
150                 if ($error = passform($user, $_POST)) {
151                         $colwarn['newpass'] = $error;
152                 }
153         }
154
155         if ($colwarn) {
156                 print "<p class=warn>Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.</p>\n\n";
157         }
158         else {
159                 print "<p>Alle instellingen zijn opgeslagen.</p>\n\n";
160         }
161 }
162
163 ?>
164 <form method="post" enctype="multipart/form-data">
165         <ul class="grid">
166 <?php
167 foreach ($cols as $col => &$colconf) {
168         if (!$colconf['visible']) {
169                 continue;
170         }
171
172         print "\t";
173         printf('<li><label for="%s">%s:</label>', $col, ucfirst($colconf['label']));
174         if (@$colconf['type'] == 'file' and isset($colconf['value'])) {
175                 printf('<a href="/%s"><img src="/thumb/%s/%s?%s" /></a><br />',
176                         $colconf['target'],
177                         200, $colconf['target'], filemtime($colconf['target'])
178                 );
179         }
180
181         if ($hide = @$colconf['hide'] and empty($_POST[$col])) {
182                 printf('<a onclick="%s">Wijzigen</a><span id="%s" hidden>',
183                         "document.getElementById('$hide').removeAttribute('hidden'); this.remove()",
184                         $hide
185                 );
186         }
187
188         if (isset($colconf['input'])) {
189                 print $colconf['input'];
190         }
191         elseif (isset($colconf['values'])) {
192                 foreach ($colconf['values'] as $tag => $val) {
193                         printf(
194                                 "\n\t\t" .
195                                 '<input type="hidden" name="%1$s" value="" />' .
196                                 '<input type="checkbox" name="%s" value="1" id="%s"%s%s />' .
197                                 '<label for="%2$s"> %s</label>',
198                                 "tags[$tag]", "tag-$tag",
199                                 $val['value'] ? ' checked' : '',
200                                 isset($val['target']) ? '' : ' readonly',
201                                 ucfirst($tag)
202                         );
203                 }
204         }
205         else {
206                 if (isset($cols[$col]['filter'])) {
207                         list ($targetstr, $inputstr) = $cols[$col]['filter'];
208                         $colconf['value'] = str_replace($targetstr, $inputstr, @$colconf['value']);
209                 }
210
211                 $attrs = [
212                         'type'        => @$colconf['type'] ?: 'text',
213                         'name'        => $col,
214                         'id'          => $col,
215                         'value'       => htmlspecialchars(@$colconf['value']),
216                         'placeholder' => "Niet ingesteld",
217                         'readonly'    => empty($colconf['target']),
218                         'pattern'     => @$colconf['pattern'] ?: FALSE,
219                         'size'        => @$colconf['size'] ?: FALSE,
220                 ];
221                 if (@$colconf['type'] == 'file') {
222                         $attrs['accept'] = "image/jpeg";
223                 }
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>