login/edit: password option in label
[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'  => ['label' => 'volledige naam'],
17         'email' => ['label' => 'e-mailadres', 'type' => 'email'],
18         'avatar' => [
19                 'label' => 'portretfoto',
20                 'type' => 'file',
21         ],
22 ];
23
24 foreach ($cols as $col => &$colconf) {
25         $filetype = @$colconf['type'] == 'file' ? 'jpg' : 'txt';
26         $colpath = "{$user['dir']}/$col.$filetype";
27         if (file_exists($colpath)) {
28                 $colconf['value'] = $filetype != 'txt' ? '' :
29                         file_get_contents($colpath);
30         }
31         if (file_exists($user['dir']) and !is_writable($user['dir'])) {
32                 continue;  # locked parent directory
33         }
34         if (isset($colconf['value']) and !is_writable($colpath)) {
35                 continue;  # locked column file
36         }
37         $colconf['target'] = $colpath;  # editing allowed
38 }
39
40 $cols = [
41         'login' => ['label' => 'login', 'value' => $user['name'], 'target' => NULL],
42 ] + $cols;
43
44 $colwarn = [];
45 if ($_POST) {
46         if (!file_exists($user['dir']) and !@mkdir($user['dir'])) {
47                 print "<p class=warn>Fout bij het aanmaken van gebruikersprofiel voor <em>{$user['name']}</em>.</p>\n\n";
48                 return;
49         }
50
51         foreach ($_POST as $col => $val) {
52                 if (!isset($cols[$col])) {
53                         continue; # unknown
54                 }
55                 if (isset($cols[$col]['value']) and $cols[$col]['value'] === $val) {
56                         continue; # unaltered
57                 }
58                 $cols[$col]['value'] = $val;  # update form value
59                 if (empty($cols[$col]['target'])) {
60                         $colwarn[$col] = "Kan niet worden aangepast.";
61                         continue;
62                 }
63                 if (file_put_contents($cols[$col]['target'], $val) === FALSE) {
64                         $colwarn[$col] = "Fout bij opslaan.";
65                 }
66         }
67
68         foreach ($_FILES as $col => $val) {
69                 if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
70                         continue; # unknown
71                 }
72                 switch ($val['error']) {
73                 case UPLOAD_ERR_OK:
74                         break;
75                 case UPLOAD_ERR_NO_FILE:
76                         continue 2; # current
77                 default:
78                         $colwarn[$col] = "Afbeelding niet goed ontvangen.";
79                         continue 2;
80                 }
81                 if (empty($cols[$col]['target'])) {
82                         $colwarn[$col] = "Kan niet worden aangepast.";
83                         continue;
84                 }
85                 if (!@move_uploaded_file($val['tmp_name'], $cols[$col]['target'])) {
86                         $colwarn[$col] = "Fout bij opslaan.";
87                 }
88                 foreach (@glob('thumb/*/') as $thumbres) {
89                         # attempt to remove old derivations
90                         @unlink($thumbres.'/'.$cols[$col]['target']);
91                 }
92                 $cols[$col]['value'] = '';
93         }
94
95         if (!empty($_POST['newpass'])) {
96                 require_once('login/pass.inc.php');
97                 if ($error = passform($user, $_POST)) {
98                         $colwarn['pass'] = $error;
99                 }
100         }
101
102         if ($colwarn) {
103                 print "<p class=warn>Instellingen zijn niet (volledig) opgeslagen. Probeer het later nog eens.</p>\n\n";
104         }
105         else {
106                 print "<p>Alle instellingen zijn opgeslagen.</p>\n\n";
107         }
108 }
109
110 ?>
111 <form method="post" enctype="multipart/form-data">
112         <p>
113         Geef een e-mailadres op waarmee we u kunnen bereiken indien nodig.
114         Wij zullen dit adres nooit vrij- of doorgeven.
115         </p>
116         <ul class="grid">
117 <?php
118 foreach ($cols as $col => &$colconf) {
119         print "\t";
120         printf('<li><label for="%s">%s:</label>', $col, ucfirst($colconf['label']));
121         if (@$colconf['type'] == 'file' and isset($colconf['value'])) {
122                 printf('<a href="/%s"><img src="/thumb/%s/%s?%s" /></a><br />',
123                         $colconf['target'],
124                         200, $colconf['target'], filemtime($colconf['target'])
125                 );
126         }
127         print "<input";
128         if (empty($colconf['target'])) print ' readonly';
129         printf(' type="%s" name="%s" id="%2$s" value="%s"',
130                 @$colconf['type'] ?: 'text',
131                 $col,
132                 htmlspecialchars(@$colconf['value'])
133         );
134         if (@$colconf['type'] == 'file') {
135                 printf(' accept="%s"', 'image/jpeg');
136         }
137         print ' placeholder="Niet ingesteld"';
138         print " />";
139
140         if ($error = @$colwarn[$col]) {
141                 print " <span class=warn>$error</span>\n";
142         }
143         print "</li>\n";
144 }
145
146 if (isset($user['pass'])) {
147 ?>
148         <li><label for="newpass">Wachtwoord:</label>
149 <?php
150         if ($hide = empty($_POST['newpass'])) {
151 ?>
152                 <a onclick="document.getElementById('pass').removeAttribute('hidden'); this.remove()">Wijzigen</a>
153 <?php
154         }
155 ?>
156                 <span id="pass"<?php if ($hide) print ' hidden'; ?>>
157                         <input type="password" name="oldpass" value="" placeholder="Huidig wachtwoord" />
158                         <input type="password" name="newpass" value="" placeholder="Nieuw wachtwoord" />
159                         <input type="password" name="passconf" value="" placeholder="Nogmaals" />
160 <?php
161         if ($error = @$colwarn['pass']) {
162                 print " <span class=warn>$error</span>\n";
163         }
164 ?>
165         </li>
166 <?php
167 }
168 ?>
169         </ul>
170         <p><input type="submit" value="Opslaan" /></p>
171 </form>