<?php

require_once "get.model.php";

class Connection{


	static public function infoDatabase(){

		$infoDB = array(

			"database" => "lastlink_system_do",
			"user" => "lastlink_UsrSyDo",
			"pass" => "8Fle]ms[Dk(["
		);

		return $infoDB;

	}

	static public function connect(){

		try{

			$link = new PDO("mysql:host=localhost;dbname=".Connection::infoDatabase()['database'],Connection::infoDatabase()['user'],Connection::infoDatabase()['pass']);

			$link->exec("set names utf8"); 


		}catch(PDOException $e){

			die("Error: ".$e->getMessage());

		}

		return $link;
	}

	static public function getColumnsData($table,$columns){

		$database = Connection::infoDatabase()['database'];

		$validate = Connection::connect()
		->query("SELECT COLUMN_NAME AS item FROM information_schema.columns WHERE table_schema = '$database' AND table_name = '$table'")
		->fetchAll(PDO::FETCH_OBJ);

		if(empty($validate)){

			return null;

		}else{


			if($columns[0] == "*"){

				array_shift($columns);

			}


			$sum = 0;

			foreach($validate as $key=> $value){

				$sum += in_array($value->item,$columns);

			}

			return $sum == count($columns) ? $validate : null;

		}

	}

	static public function jwt($id,$patente){

		$time = time();
		$token = array(

			"iat" => $time,
			"exp" => $time + (60*60*24),
			"data" => [

				"id" => $id,
				"Patente" => $patente

			]

		);

		return $token;

	}

	static public function tokenValidate($token,$table){

		$user = GetModel::getDataFilter($table,"*","Token",$token);

		if(!empty($user)){

			return "ok";

		}else{

			return "no-auth";

		}


	}

	static public function apikey(){


		return "c12asdq8w74qwe56yutj4123oi";

	}

}

?>