login/pass: error messages below page title
[minimedit.git] / auth.inc.php
index 4d9b29e2a297f00e05184cdb954e54c802d1c958..34c83db0c9bd9d5952b7b023878a7d915f07ba53 100644 (file)
@@ -3,13 +3,18 @@ date_default_timezone_set('Europe/Amsterdam');
 
 class User
 {
-       function __construct($dir)
+       public $dir, $login;
+
+       function __construct($dir = NULL, $existing = TRUE)
        {
-               if (!file_exists($dir)) {
+               if (empty($dir)) {
+                       return;
+               }
+               if (!file_exists($dir) and $existing) {
                        throw new Exception("Gebruiker niet gevonden in $dir");
                }
                $this->dir = $dir;
-               $this->login = basename($dir);
+               $this->login = preg_replace('{.*/}', '', $dir);
        }
 
        function __get($col)
@@ -29,7 +34,11 @@ class User
 
        function html()
        {
-               return $this->name ?: $this->login;
+               $name = htmlspecialchars($this->login);
+               if ($this->name and $this->name != $name) {
+                       $name = "{$this->name}<small> @$name</small>";
+               }
+               return $name;
        }
 
        function email()
@@ -37,9 +46,24 @@ class User
                return rtrim(@file_get_contents("{$this->dir}/email.txt"));
        }
 
-       function admin()
+       function admin($permission = NULL)
        {
-               return @file_exists("{$this->dir}/.admin");
+               if (isset($permission)) {
+                       if (!$this->admin) {
+                               return FALSE;  # empty results
+                       }
+                       preg_match_all('{[ /]}', $permission, $parts, PREG_OFFSET_CAPTURE);
+                       foreach ($parts[0] as $part) {
+                               if (isset($this->admin[substr($permission, 0, $part[1])])) {
+                                       return TRUE;  # partial match
+                               }
+                       }
+                       return isset($this->admin[$permission]);  # check level
+               }
+               if (!$this->dir or !@file_exists("{$this->dir}/.admin")) {
+                       return FALSE;  # not an admin
+               }
+               return array_fill_keys(explode("\n", file_get_contents("{$this->dir}/.admin")), TRUE);
        }
 
        function seen()
@@ -103,8 +127,11 @@ function login($inuser, $inpass = NULL)
        return $user;
 }
 
+global $User;
 if (isset($_COOKIE['login'])) {
-       global $User;
        $User = login($_COOKIE['login']);
 }
+if (!$User) {
+       $User = new User;
+}