auth: store global user metadata in User object
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 1 Nov 2018 16:30:01 +0000 (17:30 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Fri, 28 Jun 2019 01:20:30 +0000 (03:20 +0200)
Common access for user data everywhere.

18 files changed:
auth.inc.php
contact.php
edit/foto/cover/index.php
edit/nieuws/tag/index.php
edit/page/index.php
foto/album.inc.php
foto/index.php
login/admin/index.php
login/edit.php
login/index.php
login/list.php
login/name.php
login/pass.inc.php
login/pass/index.php
nieuws/index.php
nieuws/replies.php
page.inc.php
page.php

index 65b7f72571b9123f036a39b845f82fa93e7fdb1e..ca34e4f9de909316bcb904955341913f64e6dee0 100644 (file)
@@ -36,6 +36,14 @@ class User
        {
                return @filemtime("{$this->dir}/last.log");
        }
+
+       function logclient()
+       {
+               if ($log = @fopen("{$this->dir}/last.log", 'w')) {
+                       $line = $_SERVER['REMOTE_ADDR'].' '.$_SERVER['HTTP_USER_AGENT'];
+                       fwrite($log, $line."\n");
+               }
+       }
 }
 
 function login_password_verify($input, $test)
@@ -50,7 +58,7 @@ function login_password_verify($input, $test)
 function login_setcookie()
 {
        global $User;
-       return setcookie('login', $User['auth'], 0, '/');
+       return setcookie('login', $User->auth, 0, '/');
 }
 
 function login($inuser, $inpass = NULL)
@@ -78,17 +86,11 @@ function login($inuser, $inpass = NULL)
 
        if (function_exists('apache_note')) apache_note('user', $inuser);
 
-       if ($log = @fopen("$userdir/last.log", 'w')) {
-               fwrite($log, "{$_SERVER['REMOTE_ADDR']} {$_SERVER['HTTP_USER_AGENT']}\n");
-       }
-
-       return [
-               'name'  => $inuser,
-               'dir'   => $userdir,
-               'admin' => file_exists("$userdir/.admin"),
-               'pass'  => $usertest,
-               'auth'  => "$inuser:$authhash",
-       ];
+       $user = new User($userdir);
+       $user->logclient();
+       $user->pass = $usertest;
+       $user->auth = "$inuser:$authhash";
+       return $user;
 }
 
 if (isset($_COOKIE['login'])) {
index 7ffa40926f520546ec9567c99705b94a91800800..b50537517d7b68952f50922a53c244b7763e8e91 100644 (file)
@@ -2,8 +2,8 @@
 $error = NULL;
 if ($_POST) {
        global $User;
-       if (!empty($User)) {
-               $_REQUEST['login'] = $User['name'];
+       if ($User) {
+               $_REQUEST['login'] = $User->login;
        }
        $error = mailform($_REQUEST);
        if (!empty($error)) {
index feddc8d04e963e6e986a5089a6788c699223419e..b863f02b0a8bab5bbca8da87abcf1348f2ac6895 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 ob_clean();
 
-if (empty($User['admin'])) {
+if (!$User or !$User->admin) {
        http_response_code(403);
        print "Beheerrechten verplicht voor instellen van covers\n";
        exit;
index d4edb715f3b5370b9637a7951fc19b025868775a..0152be4786fd0f1003b5a2c7e75b58a1dca408a5 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 ob_clean();
 
-if (empty($User['admin']))
+if (!$User or !$User->admin)
        abort("geen beheersrechten", '401 unauthorised');
 
 if (!$_POST)
index cd222163a5a3615a7ad39193a0bcf218e471ae56..1daed8542c798f9b64e087c4afd523c7be933b1a 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 ob_clean();
 
-if (empty($User['admin']))
+if (!$User or !$User->admin)
        abort("geen beheersrechten", '401 unauthorised');
 
 if ($_FILES) {
@@ -74,8 +74,8 @@ if (!file_put_contents($filename, $upload))
 if (is_writable('../.git')) {
        $gitmsg = preg_replace('/\.html$/', '', $filename).": edit from {$_SERVER['REMOTE_ADDR']}";
        $gitcmd = 'git';
-       $gitcmd .= ' -c user.name='.escapeshellarg($User['name']);
-       $gitcmd .= ' -c user.email='.escapeshellarg("{$User['name']}@lijtweg.nl");
+       $gitcmd .= ' -c user.name='.escapeshellarg($User->name ?: $User->login);
+       $gitcmd .= ' -c user.email='.escapeshellarg($User->email ?: "{$User->login}@lijtweg.nl");
        $gitcmd .= ' commit -q';
        $gitcmd .= ' -m '.escapeshellarg($gitmsg);
        $gitcmd .= ' -- '.escapeshellarg($filename);
index a0a6c70b653ee6b264e49facebd354ffc16e60e8..6d706ca99769bd5c37a3dfc7c46d20c2f55f1cb4 100644 (file)
@@ -24,7 +24,7 @@ function openphotoswipe(index) {
                closeElClasses: [], 
                shareButtons: [
 <?php
-if (!empty($User['admin'])) {
+if ($User and $User->admin) {
        printf("\t\t\t{id:'%s', label:'%s', url:'%s'},\n",
                'cover', 'Cover instellen', "/edit/foto/cover$Args?img={{image_url}}"
        );
index ee37df186eb0095ec256a957f22be642d730c33b..f438cb96a7f92dc4e8f9ad86375f4cc19ad0201e 100644 (file)
@@ -3,7 +3,7 @@ $intro = ob_get_clean();
 
 $rootdir = $Page . $Args;
 
-if (!empty($User['admin'])) {
+if ($User and $User->admin) {
        $access = '🔓 Openbaar';
        if (isset($PageAccess)) {
                $access = "🔒 Bewoners";
index 08fe36edcb1551218b8377366ac796cfd1877a5a..1677b1b8b592bb0b994a90b115064af691aeba85 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-if (empty($User['admin'])) {
+if (!$User or !$User->admin) {
        ob_clean();
        http_response_code(403);
 ?>
index 88272e0c7917457592117fdb01e27a6e252d1ee6..d6ee4c905c8dc5663e76a9d0febfcd478c0eb408 100644 (file)
@@ -4,21 +4,18 @@ if (empty($user = &$User)) {
        return;
 }
 
-if (!empty($User['admin'])
-and !empty($Place['user']) and $Place['user'] !== $User['name']) {
+if ($User->admin
+and !empty($Place['user']) and $Place['user'] !== $User->login) {
        $username = strtolower($Place['user']);
        unset($user);
-       $user = [
-               'dir' => "profile/$username",
-               'name' => $username,
-       ];
+       $user = new User("profile/$username");
 }
 
 require_once('edit.inc.php');
 
 foreach ($cols as $col => &$colconf) {
        if (isset($colconf['visible'])) {
-               if ($colconf['visible'] == 'admin' and empty($User['admin'])) {
+               if ($colconf['visible'] == 'admin' and !$User->admin) {
                        $colconf['visible'] = FALSE;
                        continue;
                }
@@ -39,7 +36,7 @@ foreach ($cols as $col => &$colconf) {
                $tags = [];
                foreach (glob($colconf['filename'] . '/*') as $tag) {
                        $tagname = pathinfo($tag, PATHINFO_BASENAME);
-                       $target = "$tag/{$user['name']}";
+                       $target = "$tag/{$user->login}";
                        $val = file_exists($target);
                        $tagopt = &$colconf['values'][$tagname] ?: [];
                        $tagopt['value'] = $val;
@@ -54,12 +51,12 @@ foreach ($cols as $col => &$colconf) {
        }
 
        $filetype = @$colconf['type'] == 'file' ? 'jpg' : 'txt';
-       $colpath = $user['dir'] . '/' . $colconf['filename'];
+       $colpath = $user->dir . '/' . $colconf['filename'];
        if (file_exists($colpath)) {
                $colconf['value'] = $filetype != 'txt' ? '' :
                        rtrim(file_get_contents($colpath));
        }
-       if (file_exists($user['dir']) and !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)) {
@@ -70,8 +67,8 @@ foreach ($cols as $col => &$colconf) {
 
 $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";
+       if (!file_exists($user->dir) and !@mkdir($user->dir)) {
+               print "<p class=warn>Fout bij het aanmaken van gebruikersprofiel voor <em>{$user->login}</em>.</p>\n\n";
                return;
        }
 
@@ -94,7 +91,7 @@ if ($_POST) {
                                }
                                else {
                                        # link option target to current user dir
-                                       $optok = @symlink("../../{$user['name']}", $option['target']);
+                                       $optok = @symlink("../../{$user->login}", $option['target']);
                                }
                                $option['value'] = $optval;  # update form value
                                if (!$optok) {
index c7d705df7c2c69a337a001383591dc12877df3ab..9ebc4cb846159ef04c715e922f9f46d90ef84ec2 100644 (file)
@@ -61,7 +61,7 @@ if (isset($_REQUEST['goto'])) {
        exit;
 }
 
-if (empty($Args) and !empty($User['admin'])) {
+if (empty($Args) and $User and $User->admin) {
        include_once 'login/admin.html';
 }
 
index 7872bd64f32838dbeb3e3b92ca93b47ca219d788..698b805dc30bb9989e4bb780be94d50aa05b0957 100644 (file)
@@ -38,7 +38,7 @@ print ">\n";
 
 foreach ($users as $user) {
        $name = $user->name ?: $user->login;
-       if (!empty($GLOBALS['User']['admin'])) {
+       if ($GLOBALS['User'] and $GLOBALS['User']->admin) {
                $link = '/login/edit/'.$user->login;
                $name = sprintf('<a href="%s">%s</a>', $link, $name);
        }
index 37557bc50e1b146685558a74851d59e949acf991..df934a5e0e422dbe45b42fde2e6748a8c1f7392c 100644 (file)
@@ -5,5 +5,4 @@ if (empty($User)) {
        return;
 }
 
-$info = new User($User['dir']);
-print $info->name;
+print $User->name;
index 986b4c262a91d4b8e8515c14423f24726bb027a0..5b03c8090eedd5ff9ce198e4cea43f2cf7eb5466 100644 (file)
@@ -5,17 +5,17 @@ function passform(&$user, $input = [])
                return "Log eerst (opnieuw?) in.";
        }
 
-       $pwfile = "{$user['dir']}/.passwd";
+       $pwfile = "{$user->dir}/.passwd";
        if (file_exists($pwfile) and !is_writable($pwfile)) {
                return "Het wachtwoord kan niet worden aangepast voor deze gebruiker.";
        }
 
-       if (!empty($user['pass'])) {
+       if (!empty($user->pass)) {
                if (empty($input['oldpass'])) {
                        return "Als extra beveiliging tegen ongewenste aanpassingen moet het bestaande wachtwoord worden ingevoerd.";
                }
 
-               if (!login_password_verify($input['oldpass'], $user['pass'])) {
+               if (!login_password_verify($input['oldpass'], $user->pass)) {
                        return "Het bestaande wachtwoord is onjuist ingevoerd; niet aangepast.";
                }
        }
@@ -28,7 +28,7 @@ function passform(&$user, $input = [])
                return "Zo'n kort wachtwoord is een slecht idee.";
        }
 
-       if ($input['newpass'] == $user['name']) {
+       if ($input['newpass'] == $user->login) {
                return "De loginnaam is wel heel makkelijk raadbaar als wachtwoord.";
        }
 
@@ -41,10 +41,10 @@ function passform(&$user, $input = [])
                return "Het nieuwe wachtwoord kon niet worden opgeslagen. Het oude wachtwoord is behouden.";
        }
 
-       @unlink("{$user['dir']}/.token"); # invalidate reset token
+       @unlink("{$user->dir}/.token"); # invalidate reset token
 
        $authhash = md5($passstore);
-       $user['auth'] = "{$user['name']}:$authhash";
+       $user->auth = "{$user->login}:$authhash";
        if ($GLOBALS['User'] === $user) {
                login_setcookie();
        }
index 626a6e552cf7b5be4405084aab17795560849999..735770a791cb7aea185d287b1b4880301bbca7b3 100644 (file)
@@ -4,11 +4,8 @@ if (isset($_GET['token'])) {
        $userdir = strtolower("profile/$username");
        if ($verify = @file_get_contents("$userdir/.token")
        and $verify == $token) {
-               $User = [
-                       'name' => $username,
-                       'dir'  => $userdir,
-                       'pass' => NULL,
-               ];
+               $User = new User($userdir);
+               $User->pass = NULL;
        }
        else {
                http_response_code(403);
@@ -27,7 +24,7 @@ if ($_POST) {
        require_once('login/pass.inc.php');
        $error = passform($User, $_POST);
        if (empty($error)) {
-               print "<p>Het wachtwoord is aangepast voor <em>{$User['name']}</em>. Voortaan met het nieuwe wachtwoord inloggen.</p>\n\n";
+               print "<p>Het wachtwoord is aangepast voor <em>{$User->login}</em>. Voortaan met het nieuwe wachtwoord inloggen.</p>\n\n";
                return;
        }
        http_response_code(400);
@@ -37,11 +34,11 @@ if ($_POST) {
 ?>
 <form method="post">
 <p>
-Hier kun je een nieuwe inlogcode instellen voor <em><?= htmlspecialchars($User['name']) ?></em>.
+Hier kun je een nieuwe inlogcode instellen voor <em><?= htmlspecialchars($User->login) ?></em>.
 Dit zal de huidige code vervangen.
 </p>
 <p>
-<?php if (!empty($User['pass'])) { ?>
+<?php if ($User and strlen($User->pass)) { ?>
 <input type="password" name="oldpass" value="" placeholder="Huidig wachtwoord" />
 <?php } ?>
 <input type="password" name="newpass" value="" placeholder="Nieuw wachtwoord" />
index 21ee366b22643230f01b4cfc222e93b991f0340d..cc2a8bf870ed0fa150d100f9268841bcb8798438 100644 (file)
@@ -4,12 +4,12 @@ include 'nieuws.inc.php';
 $replyform = $Page == 'melding' && !empty($User);
 @list ($year, $page) = explode('/', trim($Args, '/'));
 
-if (!empty($User['admin'])) {
+if ($User and $User->admin) {
        print '<script src="/nieuws/edit.js"></script>'."\n";
 }
 
 if ($page and !is_numeric($page)) {
-       $edit = !empty($User['admin']) ? htmlspecialchars(@$_GET['edit']) : NULL;
+       $edit = $User && $User->admin ? htmlspecialchars(@$_GET['edit']) : NULL;
        $article = new ArchiveArticle("$Page$Args.html");
        $Place['title'] = $edit ?: $article->title;
        if ($article->file) {
@@ -20,7 +20,7 @@ if ($page and !is_numeric($page)) {
        if ($article->file and $article->image) {
                $Place['image'] = "/".$article->thumb('600x');
        }
-       if (!empty($User['admin'])) {
+       if ($User and $User->admin) {
                $taglist = [];
                foreach (glob("$Page/.tags/*") as $tagpath) {
                        $tagname = pathinfo($tagpath, PATHINFO_BASENAME);
index 61ccc2599dd647bee80b1a11cb4f14e8a90b7aaf..f762c23ccf8da577faab77c06e7eb90165890f44 100644 (file)
@@ -7,7 +7,7 @@ $pagelink = $Page.$Args;
 if ($_POST) {
        try {
                @mkdir($pagelink);
-               $target = $pagelink.'/'.date('YmdHis').':'.$User['name'].'.html';
+               $target = $pagelink.'/'.date('YmdHis').':'.$User->login.'.html';
                $html = nl2br(htmlspecialchars($_POST['reply']));
                $html = "<p>$html</p>\n";
                $written = file_put_contents($target, $html);
@@ -40,7 +40,7 @@ print '<li>';
 print '<form method="post" action="">';
 printf('<textarea id="%s" name="%1$s" cols=60 rows=3 placeholder="%s">%s</textarea>'."\n",
        'reply',
-       "Bericht van {$User['name']}",
+       "Bericht van {$User->login}",
        ''
 );
 print '<input type="submit" value="Plaatsen" />'."\n";
index b6723e9917c629b4483456281b3ff636276b0809..8298c6d4482ca806b747380433696f0634334cbf 100644 (file)
@@ -7,10 +7,10 @@ print "<header>\n";
 ob_start();
 include 'menu.html';
 ob_start();
-if (!empty($User)) {
+if ($User) {
        print '<div class="login"><p>';
-       printf('<span>Ingelogd: <b><a href="%s">%s</a></b></span>', '/login', $User['name']);
-       if (!empty($User['admin'])) {
+       printf('<span>Ingelogd: <b><a href="%s">%s</a></b></span>', '/login', $User->login);
+       if ($User->admin) {
                $editpage = $Page.$Args;
                if (is_dir($editpage)) {
                        if (file_exists("$editpage/index.html")) {
@@ -44,7 +44,7 @@ register_shutdown_function(function () {
        print "</footer>\n";
 
        global $User;
-       if (!empty($User['admin'])) {
+       if ($User and $User->admin) {
                $ckesrc = '/lib/ckeditor'; # local install
                if (!file_exists(DOCROOT . $ckesrc)) {
                        $ckesrc = '//cdn.ckeditor.com/4.7.3/full-all'; # remote fallback
index bca7a6745e7e9809e5b8c746818c5041933a54b9..894efc003cc2671014e5115b2012870a4c55da2e 100644 (file)
--- a/page.php
+++ b/page.php
@@ -113,6 +113,7 @@ error_reporting(error_reporting() & ~E_FATAL);
 
 # user login and control
 
+$User = NULL;
 include_once 'auth.inc.php';
 $Edit = isset($_GET['edit']);
 
@@ -158,7 +159,7 @@ if (file_exists("$Page$Args.html")) {
 elseif (file_exists("$Page$Args/index.html")) {
        $staticpage = "$Page$Args/index.html";
 }
-elseif (!empty($User['admin'])) {
+elseif ($User and $User->admin) {
        $staticpage = (file_exists("$Page/template.html") ? "$Page/template.html" : 'template.html');
 }
 
@@ -184,7 +185,7 @@ if ($Page) {
 }
 
 $Place += [
-       'user'  => empty($User) ? '' : $User['name'],
+       'user'  => $User ? $User->login : '',
        'url'   => htmlspecialchars($_SERVER['REQUEST_URI']),
 ];