m1k1oblog/app/user.class.php

58 lines
1.6 KiB
PHP
Raw Normal View History

2016-12-28 14:46:07 +00:00
<?php
class user
{
const SESSION_NAME = "logged_in";
2017-09-25 18:43:27 +00:00
public static function is_visitor(){
if(!Config::get_safe("force_login", false)){
return true;
}
return !empty($_SESSION[User::SESSION_NAME]) && $_SESSION[User::SESSION_NAME] == 'visitor';
}
2016-12-28 14:46:07 +00:00
public static function is_logged_in(){
if(!Config::get_safe("force_login", false)){
return true;
}
return !empty($_SESSION[User::SESSION_NAME]) && $_SESSION[User::SESSION_NAME] == md5(Config::get("nick").Config::get_safe("pass", ""));
}
public static function login($nick, $pass){
if(!Config::get_safe("force_login", false)){
return true;
}
if(self::is_logged_in()){
2017-06-19 17:51:59 +00:00
throw new Exception(__("You are already logged in."));
2016-12-28 14:46:07 +00:00
}
if(Config::get("nick") == $nick && Config::get_safe("pass", "") == $pass){
$_SESSION[User::SESSION_NAME] = md5($nick.$pass);
2017-09-25 18:43:27 +00:00
return ["logged_in" => true, "is_visitor" => false];
2016-12-28 14:46:07 +00:00
}
2017-09-25 18:43:27 +00:00
if(($visitors = Config::get_safe("visitor", [])) && !empty($visitors) && isset($visitors[$nick]) && $visitors[$nick] === $pass){
$_SESSION[User::SESSION_NAME] = 'visitor';
return ["logged_in" => false, "is_visitor" => true];
}
2016-12-28 14:46:07 +00:00
Log::put("login_fails", $nick);
2017-06-19 17:51:59 +00:00
throw new Exception(__("The nick or password is incorrect."));
2016-12-28 14:46:07 +00:00
}
public static function logout(){
if(!Config::get_safe("force_login", false)){
2017-06-19 17:51:59 +00:00
throw new Exception(__("You can't log out. There is no account."));
2016-12-28 14:46:07 +00:00
}
2017-09-25 18:43:27 +00:00
if(!self::is_logged_in() && !self::is_visitor()){
2017-06-19 17:51:59 +00:00
throw new Exception(__("You are not even logged in."));
2016-12-28 14:46:07 +00:00
}
$_SESSION[User::SESSION_NAME] = false;
return true;
}
}