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