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