m1k1oblog/app/user.class.php

64 lines
1.7 KiB
PHP
Raw Normal View History

2016-12-28 14:46:07 +00:00
<?php
defined('PROJECT_PATH') OR exit('No direct script access allowed');
2016-12-28 14:46:07 +00:00
class user
{
const SESSION_NAME = "logged_in";
2019-12-20 17:38:48 +00:00
2017-09-25 18:43:27 +00:00
public static function is_visitor(){
if(!Config::get_safe("force_login", false)){
return true;
}
2019-12-20 17:38:48 +00:00
2019-12-20 17:30:35 +00:00
return !empty($_SESSION[User::SESSION_NAME]) && $_SESSION[User::SESSION_NAME] === 'visitor';
2017-09-25 18:43:27 +00:00
}
2016-12-28 14:46:07 +00:00
public static function is_logged_in(){
if(!Config::get_safe("force_login", false)){
return true;
}
2019-12-20 17:38:48 +00:00
2019-12-20 17:31:32 +00:00
return !empty($_SESSION[User::SESSION_NAME]) && $_SESSION[User::SESSION_NAME] === hash("crc32", Config::get("nick").Config::get_safe("pass", ""), false);
2016-12-28 14:46:07 +00:00
}
2019-12-20 17:38:48 +00:00
2016-12-28 14:46:07 +00:00
public static function login($nick, $pass){
if(!Config::get_safe("force_login", false)){
return true;
}
2019-12-20 17:38:48 +00:00
2016-12-28 14:46:07 +00:00
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
}
2019-12-20 17:38:48 +00:00
2019-12-20 17:30:35 +00:00
if(Config::get("nick") === $nick && Config::get_safe("pass", "") === $pass){
2019-12-20 17:31:32 +00:00
$_SESSION[User::SESSION_NAME] = hash("crc32", $nick.$pass, false);
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
2020-07-05 17:41:45 +00:00
// Legacy: Visitors and Friends.
$visitors = array_merge(
Config::get_safe("friends", []),
Config::get_safe("visitor", [])
);
2019-12-20 17:30:35 +00:00
if(!empty($visitors) && isset($visitors[$nick]) && $visitors[$nick] === $pass){
2017-09-25 18:43:27 +00:00
$_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
}
2019-12-20 17:38:48 +00:00
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
}
2019-12-20 17:38:48 +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
}
2019-12-20 17:38:48 +00:00
2016-12-28 14:46:07 +00:00
$_SESSION[User::SESSION_NAME] = false;
return true;
}
}