<?php  

class app {

	public $database;
	public $version;

	public function __construct() {
		$this->database = new database; //creates the database
		$this->session = Session::getInstance();
		$this->install();
		$this->verify_server_auth();
	}

	public function run() {
		if(isset($this->session->id)) :
			$this->set_last_active();
			// $this->destroy_inactive_sessions();
		endif;
		
	}

	public function refresh() {
		ignore_user_abort(true);
		$update = new check_update($this); //updates database for upgrades
		$update->run();
		unset($this->session->menu);
	}

	private function install() {
		ignore_user_abort(true);
		$options = new options;
		if (isset($_GET['update']) && strtolower($_GET['update']) == 1 && $_GET['auth'] == AUTHENTICATE && isset($_GET['auth']) ) {
			// if(!DEVMODE) {
			// 	return;
			// }
			if(!current_user_can('update_software') && $options->option_exists('software_version')) {
				return;
			}
			$this->run_update();
			return;
		}	
	}

	private function create_server_auth() {
		$server_auth = base64_encode($_SERVER['SERVER_ADDR']);
		update_option("server_auth", $server_auth);
	}

	private function verify_server_auth() {
		if(!get_option("authenticate_server")) {
			return true;
		}
		if(!get_option("server_auth")) {
			$this->create_server_auth();
			return true;
		}
		$server_auth = get_option("server_auth");
		$server_auth = base64_decode($server_auth);
		if($_SERVER['SERVER_ADDR'] != $server_auth) {
			die("You do not have permission to run this application on this server");
		}
	}

	public function run_update() {
		set_time_limit(300);
		$update = new check_update($this); //updates database for upgrades
		$update->app = $this;
		$update->run();
		$this->version = $update->version;
		$this->create_default_user();
	}

	private function create_default_user() {
		$user = new user;
		$args = array('email' => ADMIN_EMAIL, 'password' => ADMIN_PASS, 'username' => ADMIN_USER, 'role' => 'software_engineer', 'status' => 'active');
		$user->register_user($args);
	}

	private function set_last_active() {
		$user = new user;
		$args = array('last_active' => time());
		$selector = $this->session->id;
		$user->update_user_meta($selector, $args);
	}

	private function destroy_inactive_sessions() {
		$query = new query;
		$user = new user;
		$all_users = $query->query_to_array($query->select('users'));
		$current_user = $this->session->id;
		unset($all_users[$current_user]);
		foreach ($all_users as $user_id => $value) {
			$meta = $user->get_user_meta($user_id);
			if(isset($meta['last_active']) && ($meta['last_active'] + DESTROY_SESSION) < time()) {
				$login_meta = (isset($meta['login_meta'])) ? unserialize($meta['login_meta']) : array();
				$login_meta = [
					'last_logout' => time(),
					'state' => 'offline',
					'last_login' => $login_meta['last_login']
				];
				$login_meta = serialize($login_meta);
				$user->update_user_meta($user_id, array('loggon_key' => 0, 'login_meta' => $login_meta));
				$query->delete( 'user_meta', array('user_id' => $meta['id'], 'meta_name' => 'last_active'));
				$query->delete( 'user_meta', array('user_id' => $meta['id'], 'meta_name' => 'loggon_key'));
			}
		}
	}
}

?>