src-empire/Empire/Core/Login.php line 27

Open in your IDE?
  1. <?php
  2. namespace Empire\Core;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\FetchMode;
  5. use Empire\Legacy\db;
  6. use Faker\Provider\DateTime;
  7. use Symfony\Component\HttpFoundation\Request;
  8. class Login {
  9.     
  10. //    public static function initialize(){
  11. //        if(Login::isLoggedIn()){
  12. //            global $user;
  13. //
  14. //            $user = Login::getLoggedIn();
  15. //
  16. //            if(!$user){
  17. //                Login::logout();
  18. //                return;
  19. //            }
  20. //
  21. //            Login::log($user->id, $_SERVER['REMOTE_ADDR']);
  22. //        }
  23. //    }
  24.     
  25.     public static function isLoggedIn(){
  26.         return null !== Core::session()->get('member_id');
  27.     }
  28. //    //function for potential future "log in as" use
  29. //    public static function isAdminLoggedIn(){
  30. //        return Login::isLoggedIn() && isset($_SESSION['ADMIN']);
  31. //    }
  32.     /**
  33.      * @return User|null
  34.      */
  35.     public static function getLoggedIn() {
  36.         if(!Login::isLoggedIn()) {
  37.             return null;
  38.         }
  39.         if(is_null(Core::user())) {
  40.            Core::setUser(User::load(array(Core::session()->get('member_id'))));
  41.         }
  42.         return Core::user();
  43.     }
  44.     /**
  45.      * @param Request $request
  46.      * @return bool
  47.      * @throws
  48.      */
  49.     public static function processLogin(Request $request) {
  50.         $username str_replace("'"'`'$request->request->get('handle'));
  51.         $user User::getByName($username);
  52.         if(!$user){
  53.             throw new \Exception("Incorrect username or password!");
  54.         }
  55.         
  56.         if(!$user->isActive()) {
  57.             throw new \Exception("Your account is currently inactive! Contact ISB or RADE if you feel this is incorrect.");
  58.         }
  59.         if(!self::verifyPassword($request->request->get('pass'), $user)) {
  60.             throw new \Exception("Incorrect username or password!");
  61.         }
  62.         Core::session()->set('member_id'$user->getID());
  63.         Core::setUser($user);
  64.         Login::log($user->getID(), $request->server->get('REMOTE_ADDR'));
  65.         return true;
  66.     }
  67.     public static function verifyPassword($passwordUser $user) {
  68.         $verfied false;
  69.         if(password_verify($password$user->MEMB_PASSWORD)) {
  70.             $verfied true;
  71.         } elseif(sha1($password) === $user->MEMB_PASSWORD) {
  72.             $verfied true;
  73.         }
  74.         if($verfied && password_needs_rehash($user->MEMB_PASSWORDPASSWORD_DEFAULT)) {
  75.             $hash self::hashPassword($password);
  76.             $user->setPassword($hash);
  77.         }
  78.         return $verfied;
  79.     }
  80.     public static function hashPassword($password) {
  81.         return password_hash($passwordPASSWORD_DEFAULT);
  82.     }
  83.     /**
  84.      * @param $token
  85.      * @return bool
  86.      * @throws \Doctrine\DBAL\DBALException
  87.      * @throws \Exception
  88.      */
  89.     public static function cookieLogin($token) {
  90.         $db Core::db();
  91.         list($token$signature) = explode(":"$token2);
  92.         if ($signature != hash_hmac('md5'$token$_ENV['APP_SECRET'])) {
  93.             throw new \Exception('Invalid cookie!');
  94.         }
  95.         $stmt $db->executeQuery('SELECT MEMB_ID, COOK_EXPIRE FROM COOKIE WHERE COOK_TOKEN = ? AND COOK_EXPIRE > NOW()', [$token]);
  96.         while ($cookie $stmt->fetch(FetchMode::STANDARD_OBJECT)) {
  97.             $user User::load([$cookie->MEMB_ID]);
  98.             Core::session()->set('member_id'$user->getID());
  99.             Core::setUser($user);
  100.             if ((new \DateTime($cookie->COOK_EXPIRE))->diff(new \DateTime(), true)->1) {
  101.                 self::setLoginCookie();
  102.             }
  103.             Login::log($user->getID(), $_SERVER['REMOTE_ADDR']);
  104.             return true;
  105.         }
  106.         throw new \Exception('Cookie expired or not found');
  107.     }
  108.     /**
  109.      * @throws
  110.      */
  111.     public static function setLoginCookie() {
  112.         if(!Login::isLoggedIn()){
  113.             throw new \Exception('Unable to set login cookie, not logged in');
  114.         }
  115.         $user Login::getLoggedIn();
  116.         //Setting expiration date.
  117.         $expires = new \DateTime('+3 months');
  118.         //Generating a token.
  119.         $token hash('sha256'random_bytes(32));
  120.         //Insert token into DB.
  121.         db::insert('COOKIE', ['COOK_TOKEN' => $token'COOK_EXPIRE' => $expires->format('Y-m-d H:i:s'), 'MEMB_ID' => $user->getID()]);
  122.         //Sign token with server key to protect against tampering
  123.         $token .= ":" hash_hmac('md5'$token$_ENV["APP_SECRET"]);
  124.         //Finally setting the cookie
  125.         setcookie('irms_token'$token$expires->format('U'));
  126.     }
  127.     public static function unsetLoginCookie() {
  128.         setcookie('irms_token''', [
  129.             "expires" => time()-3600,
  130.             "path"    => '/',
  131.             'samesite' => 'None',
  132.           ]
  133.         );
  134.     }
  135.     
  136.     public static function logout(){
  137.         self::unsetLoginCookie();
  138.         $_SESSION = array();
  139.     }
  140.     
  141.     public static function log($userID$ipAddr) {
  142.         db::insert('MEMBER_LOGIN', array('MELO_IP' => $ipAddr'MELO_STAMP' => time(), 'MEMB_ID' => $userID));
  143.     }
  144.     public static function expireCookies($id) {
  145.         $db Core::db();
  146.         $now date("Y-m-d H:i:s"strtotime("-1 hours"));
  147.         $db->executeQuery("UPDATE COOKIE SET COOK_EXPIRE = $now WHERE MEMB_ID = $id");
  148.     }
  149. }