From 5d300a80adfd980c6cb5abc3f2ab380ee9a40464 Mon Sep 17 00:00:00 2001 From: m1k1o Date: Sun, 5 Jul 2020 19:41:45 +0200 Subject: [PATCH] visitors as friends in envs #17 --- app/config.class.php | 21 +++++++++++++++++++++ app/user.class.php | 6 +++++- config.ini | 6 +++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/config.class.php b/app/config.class.php index f1d18e2..4e17bda 100644 --- a/app/config.class.php +++ b/app/config.class.php @@ -66,6 +66,11 @@ class Config $value = false; } + // Associative arrays in environment variables + if($key === 'visitor' || $key === 'friends'){ + $value = self::parse_env_assoc($value); + } + self::$_settings[$key] = $value; } } @@ -92,6 +97,22 @@ class Config return $value; } + + // Parse associative array from string in format key:value + private static function parse_env_assoc($data){ + if(!preg_match_all("/([^\s]+):([^\s]+)/s", $data, $matches)){ + return []; + } + + list($_, $keys, $values) = $matches; + + $array = []; + foreach ($values as $key => $value) { + $array[$keys[$key]] = $value; + } + + return $array; + } } class ConfigException extends Exception {} \ No newline at end of file diff --git a/app/user.class.php b/app/user.class.php index 19adb50..4548b3b 100644 --- a/app/user.class.php +++ b/app/user.class.php @@ -35,7 +35,11 @@ class user return ["logged_in" => true, "is_visitor" => false]; } - $visitors = Config::get_safe("visitor", []); + // Legacy: Visitors and Friends. + $visitors = array_merge( + Config::get_safe("friends", []), + Config::get_safe("visitor", []) + ); if(!empty($visitors) && isset($visitors[$nick]) && $visitors[$nick] === $pass){ $_SESSION[User::SESSION_NAME] = 'visitor'; return ["logged_in" => false, "is_visitor" => true]; diff --git a/config.ini b/config.ini index 74a3447..f411d8e 100644 --- a/config.ini +++ b/config.ini @@ -39,9 +39,9 @@ force_login = true nick = demo pass = demo -[visitors] -;visitor[user] = pass -;visitor[user] = pass +[friends] +;friends[user] = pass +;friends[user] = pass [directories] images_path = data/i/