login/edit: list user existence in tags
[minimedit.git] / login / edit.php
1 <?php
2 global $User;
3 if (empty($user = &$User)) {
4         return;
5 }
6
7 if (!empty($User['admin']) and $Page == 'login/edit' and $Args) {
8         $username = strtolower(ltrim($Args, '/'));
9         $user = [
10                 'dir' => "profile/$username",
11                 'name' => $username,
12         ];
13 }
14
15 $cols = [
16         'name'  => [
17                 'label' => 'volledige naam',
18                 'explain' => "Alleen zichtbaar voor andere leden.",
19         ],
20         'email' => [
21                 'label' => 'e-mailadres',
22                 'type' => 'email',
23                 'explain' => "Voor contact van of met deze site. Wij zullen dit nooit vrij- of doorgeven.",
24         ],
25         'avatar' => [
26                 'label' => 'portretfoto',
27                 'type' => 'file',
28         ],
29 ];
30
31 foreach ($cols as $col => &$colconf) {
32         $filetype = @$colconf['type'] == 'file' ? 'jpg' : 'txt';
33         $colpath = "{$user['dir']}/$col.$filetype";
34         if (file_exists($colpath)) {
35                 $colconf['value'] = $filetype != 'txt' ? '' :
36                         file_get_contents($colpath);
37         }
38         if (file_exists($user['dir']) and !is_writable($user['dir'])) {
39                 continue;  # locked parent directory
40         }
41         if (isset($colconf['value']) and !is_writable($colpath)) {
42                 continue;  # locked column file
43         }
44         $colconf['target'] = $colpath;  # editing allowed
45 }
46
47 $cols = [
48         'login' => [
49                 'label' => 'login',
50                 'value' => $user['name'],
51                 'target' => NULL,
52                 'pattern' => "[a-z0-9-]+",
53         ],
54 ] + $cols;
55
56 $tagdir = 'profile/.tags';
57 if (file_exists($tagdir)) {
58         $tags = [];
59         foreach (glob("$tagdir/*") as $tag) {
60                 $tagname = pathinfo($tag, PATHINFO_BASENAME);
61                 $target = "$tag/{$user['name']}";
62                 $tags[$tagname] = file_exists($tagname);
63         }
64
65         if ($tags) {
66                 $options = '';
67                 foreach ($tags as $tag => $val) {
68                         $options .= sprintf(
69                                 "\n\t\t" .
70                                 '<input type="hidden" name="tags[%1$s]" value="" />' .
71                                 '<input type="checkbox" name="tags[%s]" value="1"%s%s /> %s',
72                                 $tag,
73                                 $val ? ' checked' : '',
74                                 ' readonly',
75                                 ucfirst($tag)
76                         );
77                 }
78
79                 $cols['tags'] = [
80                         'label' => 'groepen',
81                         'input' => $options,
82                 ];
83         }
84 }
85
86 if (isset($user['pass'])) {
87         $cols['newpass'] = [
88                 'label' => 'wachtwoord',
89                 'input' => <<<'EOT'
90                         <input type="password" name="oldpass" value="" placeholder="Huidig wachtwoord" />
91                         <input type="password" id="newpass" name="newpass" value="" placeholder="Nieuw wachtwoord" />
92                         <input type="password" name="passconf" value="" placeholder="Nogmaals" />
93 EOT
94                 ,
95                 'hide'  => 'pass',
96         ];
97 }
98
99 $colwarn = [];
100 if ($_POST) {
101         if (!file_exists($user['dir']) and !@mkdir($user['dir'])) {
102                 print "<p class=warn>Fout bij het aanmaken van gebruikersprofiel voor <em>{$user['name']}</em>.</p>\n\n";
103                 return;
104         }
105
106         foreach ($_POST as $col => $val) {
107                 if (!isset($cols[$col])) {
108                         continue; # unknown
109                 }
110                 if (isset($cols[$col]['value']) and $cols[$col]['value'] === $val) {
111                         continue; # unaltered
112                 }
113                 $cols[$col]['value'] = $val;  # update form value
114                 if (empty($cols[$col]['target'])) {
115                         $colwarn[$col] = "Kan niet worden aangepast.";
116                         continue;
117                 }
118                 if (file_put_contents($cols[$col]['target'], $val) === FALSE) {
119                         $colwarn[$col] = "Fout bij opslaan.";
120                 }
121         }
122
123         foreach ($_FILES as $col => $val) {
124                 if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
125                         continue; # unknown
126                 }
127                 switch ($val['error']) {
128                 case UPLOAD_ERR_OK:
129                         break;
130                 case UPLOAD_ERR_NO_FILE:
131                         continue 2; # current
132                 default:
133                         $colwarn[$col] = "Afbeelding niet goed ontvangen.";
134                         continue 2;
135                 }
136                 if (empty($cols[$col]['target'])) {
137                         $colwarn[$col] = "Kan niet worden aangepast.";
138                         continue;
139                 }
140                 if (!@move_uploaded_file($val['tmp_name'], $cols[$col]['target'])) {
141                         $colwarn[$col] = "Fout bij opslaan.";
142                 }
143                 foreach (@glob('thumb/*/') as $thumbres) {
144                         # attempt to remove old derivations
145                         @unlink($thumbres.'/'.$cols[$col]['target']);
146                 }
147                 $cols[$col]['value'] = '';
148         }
149
150         if (!empty($_POST['newpass'])) {
151                 require_once('login/pass.inc.php');
152                 if ($error = passform($user, $_POST)) {
153                         $colwarn['newpass'] = $error;
154                 }
155         }
156
157         if ($colwarn) {
158                 print "<p class=warn>Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.</p>\n\n";
159         }
160         else {
161                 print "<p>Alle instellingen zijn opgeslagen.</p>\n\n";
162         }
163 }
164
165 ?>
166 <form method="post" enctype="multipart/form-data">
167         <ul class="grid">
168 <?php
169 foreach ($cols as $col => &$colconf) {
170         print "\t";
171         printf('<li><label for="%s">%s:</label>', $col, ucfirst($colconf['label']));
172         if (@$colconf['type'] == 'file' and isset($colconf['value'])) {
173                 printf('<a href="/%s"><img src="/thumb/%s/%s?%s" /></a><br />',
174                         $colconf['target'],
175                         200, $colconf['target'], filemtime($colconf['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         else {
190                 $attrs = [
191                         'type'        => @$colconf['type'] ?: 'text',
192                         'name'        => $col,
193                         'id'          => $col,
194                         'value'       => htmlspecialchars(@$colconf['value']),
195                         'placeholder' => "Niet ingesteld",
196                         'readonly'    => empty($colconf['target']),
197                         'pattern'     => @$colconf['pattern'] ?: FALSE,
198                 ];
199                 if (@$colconf['type'] == 'file') {
200                         $attrs['accept'] = "image/jpeg";
201                 }
202
203                 print '<input';
204                 foreach ($attrs as $attr => $attrval) {
205                         if ($attrval === FALSE) {
206                                 continue;
207                         }
208                         print ' ' . $attr;
209                         if ($attrval !== TRUE) {
210                                 printf('="%s"', $attrval);
211                         }
212                 }
213                 print ' />';
214         }
215         if (!empty($colconf['explain'])) {
216                 printf(' <span>(%s)</span>', $colconf['explain']);
217         }
218
219         if ($hide) {
220                 print '</span>';
221         }
222
223         if ($error = @$colwarn[$col]) {
224                 print " <span class=warn>$error</span>\n";
225         }
226         print "</li>\n";
227 }
228 ?>
229         </ul>
230         <p><input type="submit" value="Opslaan" /></p>
231 </form>