<?php

class Login {
	public function __construct() {}
	
	public static function login($username, $password) {
		global $mm;
		$uid = $mm->database()->getUID($username);
		if ($uid===false) {
			// user does not exist
			return 2;
		}
		$hash = $mm->database()->getPasswordHash($uid);
		if ($mm->hasher()->CheckPassword($password, $hash)) {
			// success
			$_SESSION['uid'] = $uid;
			return 0;
		} else {
			// wrong password
			return 1;
		}
	}
	public static function isLoggedIn() {
		if ( isset($_SESSION['uid']) && ($_SESSION['uid']!='') ) {
			return $_SESSION['uid'];
		} else {
			return false;
		}
	}
	public static function logout() {
		$_SESSION['uid'] = '';
	}
}