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