<?php 

require_once("config.php");
require_once("app/functions.php");
require_once("app/table-functions.php");
require_once("app/query-functions.php");
require_once("app/do_action.php");
require_once("app/html_elements.php");
require_once("app/pages-display.php");
require_once("app/filters.php");
// require_once("app/lib/sendgrid-php/sendgrid-php.php");

spl_autoload_register(function($class_name) {
	$file = "app/classes/class-".$class_name.".php";
	if(file_exists($file)) include $file;
});

/**
 * 
 */
class Bootstrap extends app
{
	
	function __construct()
	{
		parent::__construct();
		// sets the current session
		$this->session = Session::getInstance();
		$this->landing_page = ($option = get_option("landing_page")) ? $option : "dashboard";

		//sets the request link
		$this->request_link = isset($_GET['url']) ? $_GET['url'] : $this->landing_page;
		unset($_GET['url']);
		$redirect_params = $_GET;
		$redirect_params['redirect'] = $this->request_link;
		if($this->request_link == '') {
			header("Location: ".$this->landing_page);
			exit();
		}
		if($this->request_link == 'login' && isset($_COOKIE["user_login"])) {
			if($_COOKIE["user_login"] !== "") {
				$user = new user;
				$user_meta = $user->get_user_meta($_COOKIE["user_login"]);
				if($user_meta !== false) {
					$loggon_key = SHA1(time(). SALT);
					$login_meta = (isset($user_meta['login_meta'])) ? unserialize($user_meta['login_meta']) : array();
					$login_meta = [
						'last_login' => time(),
						'loggon_key' => $loggon_key,
						'state' => 'online'
					];
					$login_meta = serialize($login_meta);
					unset($this->session->menu);
					$user->update_user_meta($user_meta['id'], array('loggon_key' => $loggon_key, 'login_meta' => $login_meta));
					$this->session->id = $user_meta['id'];
					$this->session->key = $loggon_key;
					$this->session->valid = true;
				}
			}
		}
		if($this->request_link == 'login' && isset($this->session->id)) {
			$query = new query;
			$sql = "SELECT * FROM `visitors` WHERE `visitor_id` =  ".$this->session->id." AND `page_link` NOT LIKE 'login%' AND `page_link` NOT LIKE 'logout%' ORDER BY `request_time` DESC LIMIT 1";
			$last_visit = $query->get_results( $sql, ARRAY_A );
			if($last_visit !== null) {
				$last_visit = array_shift($last_visit);
				header("Location: ".str_replace("ispopup=true", "", $last_visit['page_link']));
				exit();
			}
		}
		
		$this->post = new post;
		$this->post_id = $this->post->search_post_by_permalink($this->request_link);
		if(!$this->post_id) {
			header("Location: error404?link=".$this->request_link);
			exit();
		}
		$this->post_meta = $this->post->get_post_meta($this->post_id);
		$this->post_meta['availability'] = isset($this->post_meta['availability']) ? $this->post_meta['availability'] : "protected";
		// security check
		$this->user_meta = get_user_meta($this->session->id);

		if(!$this->user_meta && $this->post_meta['availability'] != 'public') {
			header("Location: ".LOGIN_PAGE."?".http_build_query($redirect_params));
			exit();
		}

		if(!$this->session->valid && $this->post_meta['availability'] != 'public')
		{
			
			$this->session->user_meta = $this->user_meta;
			if(!isset($this->session->id)) {
				header("Location: ".LOGIN_PAGE."?".http_build_query($redirect_params));
				exit();
			}
			if(!isset($this->session->key)) {
				header("Location: ".LOGIN_PAGE."?".http_build_query($redirect_params));
				exit();
			}
			if($this->session->id !== $this->user_meta['id']) {
				header("Location: ".LOGIN_PAGE."?".http_build_query($redirect_params));
				exit();
			}
			if($this->session->key !== $this->user_meta['loggon_key']) {
				header("Location: ".LOGIN_PAGE."?".http_build_query($redirect_params));
				exit();
			}
			$this->session->valid = true;
		}

		
		$this->post_meta['actions'] = array();
		$this->get_params = $_GET;
		$this->post_params = $_POST;
		
	} 

	function process_actions() {
		if($this->request_link == 'login') {
			return;
		}
		if(isset($this->get_params['action'])) :
		    $action = base64_decode($this->get_params['action']);
		    if(isset($this->session->id) or $action == 'activate_account') {
		        if($action) {
		        	$action($this->get_params);
		        }
		    } else {
		    	header("Location: ".LOGIN_PAGE."?".http_build_query($this->get_params));
		    }
		endif;
	}
}