<?php

ini_set('include_path', '.:./include');
include_once ('msession.inc.php');
include_once ('config.php');
include_once ('mysql_class.inc.php');
include_once ('forms.php');
include_once ('formelements.php');
include_once ('template.inc.php');
include_once ('template_utils.inc.php');
include_once ('authenticator.inc.php');


class UserManager {
	
	private $tpl;
	private $template_settings = array();
	private $js_includes = array();
	private $css_includes = array();
	protected $sql;
	protected $m_auth;
	
	public function __construct () {
	
		$this->sql = new SQL_class();
		$this->sql->Create(DEFAULT_HOST, DEFAULT_DB, DEFAULT_USER, DEFAULT_PASS);
	
		//$this->m_auth = new Authorisation();
	
	}
	
	private function show_login($u = '', $custom_msg = '') {
			
		$posturl = 'index.php';
	
		$form_json = file_get_contents('js/login.json');
	
		$f = new Forms($posturl);
		$f->form_set_name('author_login_screen');
		$f->form_name = 'loginForm';
		$fd = json_decode($form_json, true);
		$fd['label']['fvalue'] = '<p>'.$custom_msg.'</p>';
		$form = $f->form_html_create($fd, false);
		$panel = "<div id=\"loginForm\">". $form ."</div>";
			
		return $panel;
	}
	
	public function show_login_js () {
	
		$doc = '
	function on_submit_login_clicked () {
		var username = document.forms.loginForm.user.value;
		var password = document.forms.loginForm.passwd.value;
		localStorage.setItem ("username", username);
    	localStorage.setItem ("password", password);
    	console.log ("Set username/password");
	}';
		return $doc;
	}
	
	
	public function init_template () {
	
		global $default_author_template;
		
		$this->tpl = new Template($_SESSION['th']);
		if ($_SESSION['th'] == '') {				
			$this->tpl->set_template_name($default_author_template);
		} 
	
		$this->template_settings = $this->tpl->get_settings_ref();
	
	}
	
	private function get_menu () {
	
	
		$menu = "<a class=\"menu\" href=\"".BASEURL."/\"><div class=\"border-menu\">M E N U</div></a>";
		return $menu;
	}
	
	private function get_admin_interface () {
		
		$panel = file_get_contents('include/admin.html');
		return $panel;
	}
	
	public function run () {
		
		$form = '';
		$js_text = 'function init() {update_last_log(); update_last_users(); start_heartbeat();}';
		
		if (isset($_POST['login'])) {
			// login
			$auth = new Authenticator();
			$ok = $auth->login($_POST['user'], md5($_POST['passwd']), USERADMIN);
			if ($ok) {
				
				$form .= $this->get_admin_interface();
			} else {
				$form .= $this->show_login($_POST['user'], gettext("Unable to login: ").$auth->get_last_error());
				$js_text .= $this->show_login_js ();
			}
			//print_r($_SESSION);
		} else if (isset($_SESSION['c_user_id']) && $_SESSION['c_user_id'] > 0) {
			//is logged in
			$auth = new Authenticator();
			$ok = $auth->check_credentials();
			if ($ok) {
				$form .= $this->get_admin_interface();
			} else {
				$form .= $this->show_login($_POST['user'], gettext("Unable to login or timeout: ").$auth->get_last_error());
				$js_text .= $this->show_login_js ();
			}				
		} else {
			if ($_SERVER['SERVER_NAME'] == 'trial.ivs.eu') {
				header('Location: https://trial.ivs.eu/trial.php');
				$form = "Redirecting to <a href='https://trial.ivs.eu/trial.php'>trial account setup page</a>";
			} else {
				
			
				$form .= $this->show_login();
				$js_text .= $this->show_login_js ();
			}
		}
		// include moderator js functions
		array_push ($this->js_includes, "js/support.js");
		array_push ($this->js_includes, "js/dialog.js");
		array_push ($this->js_includes, "js/useradmin.js");
		array_push ($this->js_includes, 'js/jsdatepick/jsDatePick.min.1.3.js');
		
		// date picker css
		array_push ($this->css_includes, 'js/jsdatepick/jsDatePick_ltr.min.css');
		// css directives specific for useradmin
		array_push ($this->css_includes, 'include/css/useradmin.css');
		$this->init_template();
		$doc_elem['menu'] = $this->get_menu();
		$doc_elem['doc_title'] = 'User admin';
		$doc_elem['content'] = $form;
		$doc_elem['scripting'] = $js_text;
		$doc_elem['tracker_code'] = file_get_contents (TRACKER_CODE);
		$doc = $this->tpl->run($doc_elem, $this->js_includes, $this->css_includes);
		return $doc;
	}
	
	
	
}

$mod = new UserManager();
$out = $mod->run();

echo $out;


?>