consistently use empty() to check user existence
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 4 Oct 2017 22:58:16 +0000 (00:58 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 4 Oct 2017 23:28:17 +0000 (01:28 +0200)
Succinct without causing PHP notices even for array access.

edit.php
login.php
page.inc.php
page.php

index 9ba0d04c02c34d2265851839eedd045cdcbfba95..4e51097c68a4acc599658ac9b0d172325da8507f 100644 (file)
--- a/edit.php
+++ b/edit.php
@@ -7,7 +7,7 @@ function abort($body, $status = NULL) {
        exit;
 }
 
-if (!@$User['admin'])
+if (empty($User['admin']))
        abort("geen beheersrechten", '401 unauthorised');
 
 if ($_FILES) {
index 3b590641649ed69d6237d8fba0e78a144114acc6..9c5f06ecff8cd4b31a75ce26bcd710dfe22117e2 100644 (file)
--- a/login.php
+++ b/login.php
@@ -15,19 +15,19 @@ elseif (isset($_GET['logout'])) {
        $message = 'Uitgelogd.';
 }
 
-if (isset($_GET['goto']) and isset($User)) {
+if (empty($User)) {
+       ob_clean();
+       require_once 'login.inc.php';
+       return TRUE;
+}
+
+if (isset($_GET['goto'])) {
        ob_clean();
        $target = ltrim($_GET['goto'], '/');
        header("Location: /$target");
        http_response_code(302);
        exit;
 }
-
-if (empty($User)) {
-       ob_clean();
-       require_once 'login.inc.php';
-       return TRUE;
-}
 ?>
 <p>Ingelogd als <em><?php print $User['name']; ?></em>.</p>
 
index 393d614dade2fd2004ba5ad94c40da611da8d302..54a39445b42994d777bc5e97fc23d47b7499c888 100644 (file)
@@ -7,10 +7,10 @@ print "<header>\n";
 ob_start();
 include DOCROOT.'menu.html';
 ob_start();
-if (isset($User)) {
+if (!empty($User)) {
        print '<div class="login"><p>';
        printf('<span>Ingelogd: <b><a href="%s">%s</a></b></span>', '/login', $User['name']);
-       if ($User['admin']) {
+       if (!empty($User['admin'])) {
                $editpage = $Page.$Args;
                if (is_dir($editpage)) {
                        if (file_exists("$editpage/index.html")) {
@@ -33,7 +33,7 @@ $nav = preg_replace_callback('{<a href="([^"]+)">(.*?)</a>}', function ($m) {
        $html = $request == $m[1] ? $m[2] : $m[0]; # text or full link
        return $m[1] == substr($request, 0, strlen($m[1])) ? "<b>$html</b>" : $html;
 }, $nav);
-if (isset($User)) {
+if (!empty($User)) {
        $nav = preg_replace('{\s*<li class="logout">.*?</li>}', '', $nav);
 }
 else {
@@ -50,7 +50,7 @@ register_shutdown_function(function () {
        print "</footer>\n";
 
        global $User;
-       if (isset($User) and $User['admin']) {
+       if (!empty($User['admin'])) {
                print '<script src="//cdn.ckeditor.com/4.7.3/full-all/ckeditor.js"></script>'."\n";
                print '<script src="/edit.js"></script>'."\n";
        }
index 21cc0a06783982d838210aeabeadb1c1d7bb21d8..20f8244ac0684bd973df5e6bd9a924d5e9ad8695 100644 (file)
--- a/page.php
+++ b/page.php
@@ -61,7 +61,7 @@ $Page = urldecode(trim($Page, '/')) ?: 'index';
 while (TRUE) {
        if (file_exists("$Page/.private")) {
                # access restriction
-               if (!isset($User)) {
+               if (empty($User)) {
                        http_response_code(303);
                        $target = urlencode($_SERVER['REQUEST_URI']);
                        header("Location: /login?goto=$target");
@@ -95,7 +95,7 @@ if (file_exists("$Page$Args/index.html")) {
 elseif (file_exists("$Page$Args.html")) {
        $found = include "./$Page$Args.html";
 }
-elseif (isset($User) and $User['admin']) {
+elseif (!empty($User['admin'])) {
        $found = include (file_exists("$Page/template.html") ? "$Page/template.html" : './template.html');
 }