login/edit: column descriptions; explain name visibility
[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' => ['label' => 'login', 'value' => $user['name'], 'target' => NULL],
49 ] + $cols;
50
51 if (isset($user['pass'])) {
52         $cols['newpass'] = [
53                 'label' => 'wachtwoord',
54                 'input' => <<<'EOT'
55                         <input type="password" name="oldpass" value="" placeholder="Huidig wachtwoord" />
56                         <input type="password" id="newpass" name="newpass" value="" placeholder="Nieuw wachtwoord" />
57                         <input type="password" name="passconf" value="" placeholder="Nogmaals" />
58 EOT
59                 ,
60                 'hide'  => 'pass',
61         ];
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['name']}</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]['value']) and $cols[$col]['value'] === $val) {
76                         continue; # unaltered
77                 }
78                 $cols[$col]['value'] = $val;  # update form value
79                 if (empty($cols[$col]['target'])) {
80                         $colwarn[$col] = "Kan niet worden aangepast.";
81                         continue;
82                 }
83                 if (file_put_contents($cols[$col]['target'], $val) === FALSE) {
84                         $colwarn[$col] = "Fout bij opslaan.";
85                 }
86         }
87
88         foreach ($_FILES as $col => $val) {
89                 if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
90                         continue; # unknown
91                 }
92                 switch ($val['error']) {
93                 case UPLOAD_ERR_OK:
94                         break;
95                 case UPLOAD_ERR_NO_FILE:
96                         continue 2; # current
97                 default:
98                         $colwarn[$col] = "Afbeelding niet goed ontvangen.";
99                         continue 2;
100                 }
101                 if (empty($cols[$col]['target'])) {
102                         $colwarn[$col] = "Kan niet worden aangepast.";
103                         continue;
104                 }
105                 if (!@move_uploaded_file($val['tmp_name'], $cols[$col]['target'])) {
106                         $colwarn[$col] = "Fout bij opslaan.";
107                 }
108                 foreach (@glob('thumb/*/') as $thumbres) {
109                         # attempt to remove old derivations
110                         @unlink($thumbres.'/'.$cols[$col]['target']);
111                 }
112                 $cols[$col]['value'] = '';
113         }
114
115         if (!empty($_POST['newpass'])) {
116                 require_once('login/pass.inc.php');
117                 if ($error = passform($user, $_POST)) {
118                         $colwarn['newpass'] = $error;
119                 }
120         }
121
122         if ($colwarn) {
123                 print "<p class=warn>Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.</p>\n\n";
124         }
125         else {
126                 print "<p>Alle instellingen zijn opgeslagen.</p>\n\n";
127         }
128 }
129
130 ?>
131 <form method="post" enctype="multipart/form-data">
132         <ul class="grid">
133 <?php
134 foreach ($cols as $col => &$colconf) {
135         print "\t";
136         printf('<li><label for="%s">%s:</label>', $col, ucfirst($colconf['label']));
137         if (@$colconf['type'] == 'file' and isset($colconf['value'])) {
138                 printf('<a href="/%s"><img src="/thumb/%s/%s?%s" /></a><br />',
139                         $colconf['target'],
140                         200, $colconf['target'], filemtime($colconf['target'])
141                 );
142         }
143
144         if ($hide = @$colconf['hide'] and empty($_POST[$col])) {
145                 printf('<a onclick="%s">Wijzigen</a><span id="%s" hidden>',
146                         "document.getElementById('$hide').removeAttribute('hidden'); this.remove()",
147                         $hide
148                 );
149         }
150
151         if (isset($colconf['input'])) {
152                 print $colconf['input'];
153         }
154         else {
155                 $attrs = [
156                         'type'        => @$colconf['type'] ?: 'text',
157                         'name'        => $col,
158                         'id'          => $col,
159                         'value'       => htmlspecialchars(@$colconf['value']),
160                         'placeholder' => "Niet ingesteld",
161                         'readonly'    => empty($colconf['target']),
162                 ];
163                 if (@$colconf['type'] == 'file') {
164                         $attrs['accept'] = "image/jpeg";
165                 }
166
167                 print '<input';
168                 foreach ($attrs as $attr => $attrval) {
169                         if ($attrval === FALSE) {
170                                 continue;
171                         }
172                         print ' ' . $attr;
173                         if ($attrval !== TRUE) {
174                                 printf('="%s"', $attrval);
175                         }
176                 }
177                 print ' />';
178         }
179         if (!empty($colconf['explain'])) {
180                 printf(' <span>(%s)</span>', $colconf['explain']);
181         }
182
183         if ($hide) {
184                 print '</span>';
185         }
186
187         if ($error = @$colwarn[$col]) {
188                 print " <span class=warn>$error</span>\n";
189         }
190         print "</li>\n";
191 }
192 ?>
193         </ul>
194         <p><input type="submit" value="Opslaan" /></p>
195 </form>