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