<?php 

/*
auth login password
auth rs
sign-in 

id,login,password,uid,token,name,photo,email,active,etat,role

user::def("¤TABLE_USERS",[
	"id"       => "id",
	"login"    => "login",
	"password" => "password",
	"uid"      => "uid",
	"token"    => "token",
	"name"     => "name",
	"photo"    => "photo",
	"email"    => "email",
	"active"   => "active",
	"etat"     => "etat",
	"role"     => "role",
]);


user::onFormAuth(function($data){
	switch($data["auth"]){
		case "rs":
			$uid = $data["uid"];
		break;
		default:
			$login = $data["login"];
			$password = $data["password"];
			$rememberme = $data["rememberme"];
		break;
	}
});




*/






class user{
	static function get($names = null,$default = null){
		if (is_null($names)) {
			return isset($_SESSION["user"])?$_SESSION["user"]:false;
		}
		if (is_string($names)) {
			$names = explode(",",$names);
		}
		if (is_array($names)) {
			foreach ($names as $name) {
				if (in_array($name, ["id","login","password","nom","type","etat"])) {
					if (isset($_SESSION["user"][$name])) {
						return $_SESSION["user"][$name];
					}
				}else{
					if ($row = db::row("SELECT data FROM ¤USERS_INFOS where id_user=:id and name = :name",[":id"=>$_SESSION["user"]["id"],":name"=>$name])) {
						return $row["data"];
					}
				}
			}
		}
		return $default;
	}
	static function isAdmin($id=null){
		return self::get("type") == "Administrateur";
	}
	static function connected(){
		return self::get()==false?false:true;
	}
	static function set($name,$value){
		if (in_array($name, ["id","login","password","nom","type","etat"])) {
			db::save("¤USERS",[
				"id"=>$_SESSION["user"]["id"],
				$name => $value,
			]);
			$_SESSION["user"][$name] = $value;
		}else{
			db::save("¤USERS_INFOS",[
				"id_user"=>$_SESSION["user"]["id"],
				"name" => $name,
				"data" => $value
			]);
		}
	}
	static function logout(){
		self::run("logout");
		unset($_SESSION["user"]);
	}
	static $events = [
		"new" => [],
		"logout"=> [],
		"login"=> [],
		"auth"=> [],
	];
	static function onNew($callback){
		self::$events["new"][] = [
			"fn" => $callback
		];
	}
	static function onLogout($callback){
		self::$events["logout"][] = [
			"fn" => $callback
		];
	}
	static function onLogin($callback){
		self::$events["login"][] = [
			"fn" => $callback
		];
	}
	static function onFormAuth($callback){
		self::$events["auth"][] = [
			"fn" => $callback
		];
	}
	static function run($event,$data = null){
		if (!empty(self::$events[$event])) {
			foreach(self::$events[$event] as $key => $value) {
				$fn = $value["fn"];
				if(is_callable($fn)){
					$v = $fn($data);
					if (!is_null($v)) {
						$data = $v;
					}
				}
			}
		}
		return $data;
	}
	static function connect($login,$password,&$control = []){
		$control = [];
		if ($row = db::row("SELECT * FROM ¤USERS where login = :login limit 0,1",[":login"=>$login])) {
			if ($row["password"]."" == $password."") {
				$_SESSION["user"] = $row;
				$control["login"] = ["etat"=>true];
				$control["password"] = ["etat"=>true];
				self::run("login");
				return true;
			}else{
				$control["login"] = ["etat"=>true];
				$control["password"] = ["etat"=>false,"msg"=>"Mot de passe invalid!"];
				return false;
			}
		}else{
			$control["login"] = [
				"etat"=>false,
				"msg"=> "Login invalid!"
			];
			return false;
		}
	}
	static function has($modules = "",$default = false){
		
	}
	static function reload(){
		if ($id = self::get("id")) {
			$_SESSION["user"] = db::row("SELECT * FROM ¤USERS where id=$id");
		}
	}
	static function is($type = null){
		if (!is_null($type)) {
			if (strtolower(trim(self::get("type","Utilisateur"))) == strtolower(trim($type))) {
				return true;
			}
			return false;
		}
		return self::get("type","Utilisateur");
	}
	static function install(){
		db::create("¤USERS",function($table){
			$table->int("id")->pri()->auto_increment();
			$table->string("login");
			$table->string("password");
			$table->string("type")->def("'Utilisateur'");
			$table->string("nom");
			$table->int("etat");
		})->install();
		db::create("¤USERS_INFOS",function($table){
			$table->int("id_user")->unique("nameuserid");
			$table->string("name")->unique("nameuserid");
			$table->text("data");
		})->install();
		if (!db::row("SELECT id FROM ¤USERS where type='Administrateur' limit 0,1")) {
			db::save("¤USERS",[
				"login"    => "admin",
				"password" => "admin",
				"type"     => "Administrateur",
				"nom"      => "Administrateur",
				"etat"     => 1,
			]);
		}
	}
}

api::install(function(){
	user::install();
});


