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