login/edit: apply tags input to target links
[minimedit.git] / login / edit.php
index bb2278faf2b02f22250656a9b6f2158d8329111a..894264b336ac3cc638a5d8a6ab85b61838e8f8ab 100644 (file)
@@ -13,8 +13,15 @@ if (!empty($User['admin']) and $Page == 'login/edit' and $Args) {
 }
 
 $cols = [
-       'name'  => ['label' => 'volledige naam'],
-       'email' => ['label' => 'e-mailadres', 'type' => 'email'],
+       'name'  => [
+               'label' => 'volledige naam',
+               'explain' => "Alleen zichtbaar voor andere leden.",
+       ],
+       'email' => [
+               'label' => 'e-mailadres',
+               'type' => 'email',
+               'explain' => "Voor contact van of met deze site. Wij zullen dit nooit vrij- of doorgeven.",
+       ],
        'avatar' => [
                'label' => 'portretfoto',
                'type' => 'file',
@@ -22,9 +29,10 @@ $cols = [
 ];
 
 foreach ($cols as $col => &$colconf) {
-       $colpath = "{$user['dir']}/$col.txt";
+       $filetype = @$colconf['type'] == 'file' ? 'jpg' : 'txt';
+       $colpath = "{$user['dir']}/$col.$filetype";
        if (file_exists($colpath)) {
-               $colconf['value'] = @$colconf['type'] == 'file' ? '' :
+               $colconf['value'] = $filetype != 'txt' ? '' :
                        file_get_contents($colpath);
        }
        if (file_exists($user['dir']) and !is_writable($user['dir'])) {
@@ -37,9 +45,69 @@ foreach ($cols as $col => &$colconf) {
 }
 
 $cols = [
-       'login' => ['label' => 'login', 'value' => $user['name'], 'target' => NULL],
+       'login' => [
+               'label' => 'login',
+               'value' => $user['name'],
+               'target' => NULL,
+               'pattern' => "[a-z0-9-]+",
+       ],
 ] + $cols;
 
+$tagdir = 'profile/.tags';
+if (file_exists($tagdir)) {
+       $tags = [];
+       foreach (glob("$tagdir/*") 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;
+       }
+
+       if ($tags) {
+               $options = '';
+               foreach ($tags as $tag => $val) {
+                       $options .= sprintf(
+                               "\n\t\t" .
+                               '<input type="hidden" name="tags[%1$s]" value="" />' .
+                               '<input type="checkbox" name="tags[%s]" value="1"%s%s /> %s',
+                               $tag,
+                               $val['value'] ? ' checked' : '',
+                               isset($val['target']) ? '' : ' readonly',
+                               ucfirst($tag)
+                       );
+               }
+
+               $cols['tags'] = [
+                       'label' => 'groepen',
+                       'input' => $options,
+                       'values' => $tags,
+               ];
+       }
+}
+
+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'])) {
@@ -64,6 +132,29 @@ if ($_POST) {
                }
        }
 
+       if (isset($cols['tags']) and !empty($_POST['tags'])) {
+               $tagok = [];
+               foreach ($_POST['tags'] as $col => $val) {
+                       $tag = $cols['tags']['values'][$col];
+                       if (!isset($tag['target'])) {
+                               $tagok[$col] = 'forbidden';
+                       }
+                       if ($tag['value'] === !empty($val)) {
+                               $tagok[$col] = NULL;  # unaltered
+                       }
+                       elseif (empty($val)) {
+                               $tagok[$col] = !@unlink($tag['target']);
+                       }
+                       else {
+                               $tagok[$col] = !@symlink("../../{$user['name']}", $tag['target']);
+                       }
+               }
+               if ($tagok = array_filter($tagok)) {
+                       $colwarn['tags'] = "Wijziging niet opgeslagen voor "
+                               . implode(', ', array_keys($tagok));
+               }
+       }
+
        foreach ($_FILES as $col => $val) {
                if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
                        continue; # unknown
@@ -84,13 +175,17 @@ if ($_POST) {
                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['pass'] = $error;
+                       $colwarn['newpass'] = $error;
                }
        }
 
@@ -104,50 +199,68 @@ if ($_POST) {
 
 ?>
 <form method="post" enctype="multipart/form-data">
-       <p>
-       Geef een e-mailadres op waarmee we u kunnen bereiken indien nodig.
-       Wij zullen dit adres nooit vrij- of doorgeven.
-       </p>
+       <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 ($error = @$colwarn[$col]) {
-               print " <span class=warn>$error</span>\n";
+       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
+               );
        }
-       print "<br />\n";
-}
 
-if (isset($user['pass'])) {
-       if ($hide = empty($_POST['newpass'])) {
-?>
-       <p><a onclick="document.getElementById('pass').removeAttribute('hidden'); this.remove()">Wachtwoord wijzigen</a></p>
-<?php
+       if (isset($colconf['input'])) {
+               print $colconf['input'];
        }
-?>
-       <div id="pass"<?php if ($hide) print ' hidden'; ?>>
-               <label for="newpass">Wachtwoord:</label>
-               <input type="password" name="oldpass" value="" placeholder="Huidig wachtwoord" />
-               <input type="password" name="newpass" value="" placeholder="Nieuw wachtwoord" />
-               <input type="password" name="passconf" value="" placeholder="Nogmaals" />
-<?php
-       if ($error = @$colwarn['pass']) {
+       else {
+               $attrs = [
+                       'type'        => @$colconf['type'] ?: 'text',
+                       'name'        => $col,
+                       'id'          => $col,
+                       'value'       => htmlspecialchars(@$colconf['value']),
+                       'placeholder' => "Niet ingesteld",
+                       'readonly'    => empty($colconf['target']),
+                       'pattern'     => @$colconf['pattern'] ?: 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";
        }
-?>
-       </div>
-<?php
+       print "</li>\n";
 }
 ?>
-       <input type="submit" value="Opslaan" />
+       </ul>
+       <p><input type="submit" value="Opslaan" /></p>
 </form>