login/edit: generic multi-values declaration
[minimedit.git] / login / edit.php
index 563a3a567b6771c2962ea03a08589acce23676f5..4a2e6f4665c39d429d08a8fea4203ee401878150 100644 (file)
@@ -1,32 +1,82 @@
 <?php
 global $User;
-if (empty($user = $User)) {
+if (empty($user = &$User)) {
        return;
 }
 
-if (!empty($User['admin']) and $Page == 'login/edit' and $Args) {
-       $username = ltrim($Args, '/');
+if (!empty($User['admin'])
+and !empty($Place['user']) and $Place['user'] !== $User['name']) {
+       $username = strtolower($Place['user']);
+       unset($user);
        $user = [
                'dir' => "profile/$username",
                'name' => $username,
        ];
-       if (!is_writable($user['dir'])) {
-               print "<p class=warn>Het is niet mogelijk om de gebruiker <em>{$user['name']}</em> aan te passen.</p>\n\n";
-               return;
-       }
 }
 
 $cols = [
-       'name'  => ['label' => 'volledige naam'],
-       'email' => ['label' => 'e-mailadres', 'type' => 'email'],
+       'name'  => [
+               'label' => 'volledige naam',
+               'explain' => "Alleen zichtbaar voor andere leden.",
+               'filter' => ["\n", '; '],
+               'size' => 30,
+               'filename' => 'name.txt',
+       ],
+       'email' => [
+               'label' => 'e-mailadres',
+               'type' => 'email',
+               'explain' => "Voor contact van of met deze site. Wij zullen dit nooit vrij- of doorgeven.",
+               'size' => 30,
+               'filename' => 'email.txt',
+       ],
+       'avatar' => [
+               'label' => 'portretfoto',
+               'type' => 'file',
+               'filename' => 'avatar.jpg',
+       ],
+       'tags' => [
+               'label' => 'groepen',
+               'values' => [],
+               'filename' => 'profile/.tags',
+       ],
 ];
 
 foreach ($cols as $col => &$colconf) {
-       $colpath = "{$user['dir']}/$col.txt";
+       if (!isset($colconf['filename'])) {
+               continue;  # exceptional storage
+       }
+
+       if (isset($colconf['values'])) {
+               if (!file_exists($colconf['filename'])) {
+                       continue;
+                       #TODO: drop key
+               }
+               $tags = [];
+               foreach (glob($colconf['filename'] . '/*') as $tag) {
+                       $tagname = pathinfo($tag, PATHINFO_BASENAME);
+                       $target = "$tag/{$user['name']}";
+                       $val = file_exists($target);
+                       $tags[$tagname] = ['value' => $val];
+                       if (empty($User['admin'])) {
+                               continue;  # forbidden
+                       }
+                       if (!is_writable($tag)) {
+                               continue;  # locked tag directory
+                       }
+                       if ($val and !is_writable($target)) {
+                               continue;  # existing file locked
+                       }
+                       $tags[$tagname]['target'] = $target;
+               }
+       }
+
+       $filetype = @$colconf['type'] == 'file' ? 'jpg' : 'txt';
+       $colpath = $user['dir'] . '/' . $colconf['filename'];
        if (file_exists($colpath)) {
-               $colconf['value'] = file_get_contents($colpath);
+               $colconf['value'] = $filetype != 'txt' ? '' :
+                       file_get_contents($colpath);
        }
-       if (!is_writable($user['dir'])) {
+       if (file_exists($user['dir']) and !is_writable($user['dir'])) {
                continue;  # locked parent directory
        }
        if (isset($colconf['value']) and !is_writable($colpath)) {
@@ -36,26 +86,119 @@ foreach ($cols as $col => &$colconf) {
 }
 
 $cols = [
-       'login' => ['label' => 'login', 'value' => $user['name'], 'target' => NULL],
+       'username' => [
+               'label' => 'login',
+               'value' => $user['name'],
+               'target' => NULL,
+               'pattern' => "[a-z0-9-]+",
+               'size' => 10,
+       ],
 ] + $cols;
 
+if (isset($user['pass'])) {
+       $cols['newpass'] = [
+               'label' => 'wachtwoord',
+               'input' => <<<'EOT'
+                       <input type="password" name="oldpass" value="" placeholder="Huidig wachtwoord" />
+                       <input type="password" id="newpass" name="newpass" value="" placeholder="Nieuw wachtwoord" />
+                       <input type="password" name="passconf" value="" placeholder="Nogmaals" />
+EOT
+               ,
+               'hide'  => 'pass',
+       ];
+}
+
 $colwarn = [];
 if ($_POST) {
+       if (!file_exists($user['dir']) and !@mkdir($user['dir'])) {
+               print "<p class=warn>Fout bij het aanmaken van gebruikersprofiel voor <em>{$user['name']}</em>.</p>\n\n";
+               return;
+       }
+
        foreach ($_POST as $col => $val) {
                if (!isset($cols[$col])) {
                        continue; # unknown
                }
+               if (isset($cols[$col]['values'])) {
+                       $optwarn = [];
+                       foreach ($val as $optcol => $optval) {
+                               $option = &$cols[$col]['values'][$optcol];
+                               if (!isset($option['target'])) {
+                                       $optok = FALSE;  # forbidden
+                               }
+                               if ($option['value'] === !empty($optval)) {
+                                       continue;  # unaltered
+                               }
+                               elseif (empty($optval)) {
+                                       $optok = @unlink($option['target']);
+                               }
+                               else {
+                                       # link option target to current user dir
+                                       $optok = @symlink("../../{$user['name']}", $option['target']);
+                               }
+                               $option['value'] = $optval;  # update form value
+                               if (!$optok) {
+                                       $optwarn[$optcol] = TRUE;
+                               }
+                       }
+                       if ($optwarn) {
+                               $colwarn[$col] = "Wijziging niet opgeslagen voor "
+                                       . implode(', ', array_keys($optwarn));
+                       }
+                       continue;
+               }
+
+               if (isset($cols[$col]['filter'])) {
+                       list ($targetstr, $inputstr) = $cols[$col]['filter'];
+                       $val = str_replace($inputstr, $targetstr, $val);
+               }
                if (isset($cols[$col]['value']) and $cols[$col]['value'] === $val) {
                        continue; # unaltered
                }
                $cols[$col]['value'] = $val;  # update form value
+               if (empty($cols[$col]['target'])) {
+                       if (empty($cols[$col]['input'])) {
+                               $colwarn[$col] = "Kan niet worden aangepast.";
+                       }
+                       continue;
+               }
+               if (file_put_contents($cols[$col]['target'], $val) === FALSE) {
+                       $colwarn[$col] = "Fout bij opslaan.";
+               }
+       }
+
+       foreach ($_FILES as $col => $val) {
+               if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
+                       continue; # unknown
+               }
+               switch ($val['error']) {
+               case UPLOAD_ERR_OK:
+                       break;
+               case UPLOAD_ERR_NO_FILE:
+                       continue 2; # current
+               default:
+                       $colwarn[$col] = "Afbeelding niet goed ontvangen.";
+                       continue 2;
+               }
                if (empty($cols[$col]['target'])) {
                        $colwarn[$col] = "Kan niet worden aangepast.";
                        continue;
                }
-               if (!file_put_contents($cols[$col]['target'], $val)) {
+               if (!@move_uploaded_file($val['tmp_name'], $cols[$col]['target'])) {
                        $colwarn[$col] = "Fout bij opslaan.";
                }
+               foreach (@glob('thumb/*/') as $thumbres) {
+                       # attempt to remove old derivations
+                       @unlink($thumbres.'/'.$cols[$col]['target']);
+               }
+               $cols[$col]['value'] = '';
+       }
+
+       if (!empty($_POST['newpass'])) {
+               require_once('login/pass.inc.php');
+               if ($error = passform($user, $_POST)) {
+                       $colwarn['newpass'] = $error;
+               }
        }
 
        if ($colwarn) {
@@ -67,32 +210,90 @@ if ($_POST) {
 }
 
 ?>
-<form method="post">
-       <p>
-       Geef een e-mailadres op waarmee we u kunnen bereiken indien nodig.
-       Wij zullen dit adres nooit vrij- of doorgeven.
-       </p>
-       <p>
+<form method="post" enctype="multipart/form-data">
+       <ul class="grid">
 <?php
 foreach ($cols as $col => &$colconf) {
        print "\t";
-       printf('<label for="%s">%s:</label> ', $col, ucfirst($colconf['label']));
-       print "<input";
-       if (empty($colconf['target'])) print ' readonly';
-       printf(' type="%s" name="%s" id="%1$s" value="%s"',
-               @$colconf['type'] ?: 'text',
-               $col,
-               htmlspecialchars(@$colconf['value'])
-       );
-       print ' placeholder="Niet ingesteld"';
-       print " />";
+       printf('<li><label for="%s">%s:</label>', $col, ucfirst($colconf['label']));
+       if (@$colconf['type'] == 'file' and isset($colconf['value'])) {
+               printf('<a href="/%s"><img src="/thumb/%s/%s?%s" /></a><br />',
+                       $colconf['target'],
+                       200, $colconf['target'], filemtime($colconf['target'])
+               );
+       }
+
+       if ($hide = @$colconf['hide'] and empty($_POST[$col])) {
+               printf('<a onclick="%s">Wijzigen</a><span id="%s" hidden>',
+                       "document.getElementById('$hide').removeAttribute('hidden'); this.remove()",
+                       $hide
+               );
+       }
+
+       if (isset($colconf['input'])) {
+               print $colconf['input'];
+       }
+       elseif (isset($colconf['values'])) {
+               foreach ($colconf['values'] as $tag => $val) {
+                       printf(
+                               "\n\t\t" .
+                               '<input type="hidden" name="%1$s" value="" />' .
+                               '<input type="checkbox" name="%s" value="1" id="%s"%s%s />' .
+                               '<label for="%2$s"> %s</label>',
+                               "tags[$tag]", "tag-$tag",
+                               $val['value'] ? ' checked' : '',
+                               isset($val['target']) ? '' : ' readonly',
+                               ucfirst($tag)
+                       );
+               }
+       }
+       else {
+               if (isset($cols[$col]['filter'])) {
+                       list ($targetstr, $inputstr) = $cols[$col]['filter'];
+                       $colconf['value'] = str_replace($targetstr, $inputstr, $colconf['value']);
+               }
+
+               $attrs = [
+                       'type'        => @$colconf['type'] ?: 'text',
+                       'name'        => $col,
+                       'id'          => $col,
+                       'value'       => htmlspecialchars(@$colconf['value']),
+                       'placeholder' => "Niet ingesteld",
+                       'readonly'    => empty($colconf['target']),
+                       'pattern'     => @$colconf['pattern'] ?: FALSE,
+                       'size'        => @$colconf['size'] ?: FALSE,
+               ];
+               if (@$colconf['type'] == 'file') {
+                       $attrs['accept'] = "image/jpeg";
+               }
+
+               print '<input';
+               foreach ($attrs as $attr => $attrval) {
+                       if ($attrval === FALSE) {
+                               continue;
+                       }
+                       print ' ' . $attr;
+                       if ($attrval !== TRUE) {
+                               printf('="%s"', $attrval);
+                       }
+               }
+               print ' />';
+       }
+
+       if (!empty($colconf['explain'])) {
+               printf(' <span>(%s)</span>', $colconf['explain']);
+       }
+
+       if ($hide) {
+               print '</span>';
+       }
 
        if ($error = @$colwarn[$col]) {
                print " <span class=warn>$error</span>\n";
        }
-       print "<br />\n";
+       print "</li>\n";
 }
 ?>
-       <input type="submit" value="Opslaan" />
-       </p>
+       </ul>
+       <p><input type="submit" value="Opslaan" /></p>
 </form>