<?PHP

class login extends engine{

	var $call_sql_class = array('users', 'clients', 'roles', 'client_details', 'company', 'menus', 'logs_session');
	var $call_class = array('Functions', 'Email', 'Curl');
	var $register_success = false;

	function get_register_check_data(){

		if(is_numeric($_GET['register_nr']))
			$this->company->Select("WHERE register_nr = ".$_GET['register_nr']);
		return $this;
	}
	function get_login_data(){

		return $this;
	}
	function sessionActive(){

		$interval = 10;

		if($_SESSION['session_lifetime'] && $_SESSION['session_lifetime'] < strtotime('now')){

			$this->logs_session->Insert(array(
				'action' => 'timeout',
				'created_by' => $_SESSION['UserData']['user_name'],
				'created_by_id' => $_SESSION['UserData']['user_id'],
				'created_at' => date('Y-m-d H:i:s', $_SESSION['session_lifetime'])
			));
		}
		$_SESSION['session_lifetime'] = strtotime('+'.$interval.' minutes');
		return $this;
	}
	function loginCheck(){

		if($_SESSION['logged'] == true){

			$this->users->columns("users.*");
			$this->users->Select(
				"JOIN roles AS p2 ON p2.id = users.role_id ".
				"WHERE (p2.level <= ".$_SESSION['UserData']['level']." AND p2.level > 2) ".($_SESSION['UserData']['from_user_id'] ? "OR users.id = ".$_SESSION['UserData']['from_user_id'] : "")
			);

			return true;
		}
		return false;
	}

	function getFrameworkMessages(){

		$this->fwMessages = $this->Curl->SendCurl('http://www.projectpartner.ee/documents/framework.messages.txt');

		return $this;
	}

	function is_temporary(){

		if($this->page_position == 'public' && $_SESSION['UserData']['is_temporary'] == 1 && $_GET['model'] != 'my_account'){

			die($this->Redirect($this->Link('?model=my_account&page=my_account&show_page='.$this->Select('my_account'))));
		}

		return $this;
	}

	function ajaxLoginCheck(){

		if($_SESSION['logged'] == false){

			die(json_encode(array('status' => 0, 'message' => $this->Language('Username or password invalid'), 'menu' => $this->Language('minu konto'))));
		}
		else{

			$this->company->Select("WHERE client_id = ".$_SESSION['UserData']['client_id']." AND status = 0");
			if($this->company->Numrows > 0){

				unset($_SESSION['UserData']);
				unset($_SESSION['logged']);
				die(json_encode(array('status' => 0, 'message' => $this->Language('Company not active'), 'menu' => '')));
			}
			else{

				die(json_encode(array('status' => 1, 'message' => $this->Language('Logged'), 'menu' => $this->Language('minu konto'))));
			}
		}
	}

	function confirm(){

		$this->confirmed = false;

		$this->users->Select(" activation_hash='".$_GET['hash']."' and is_enabled = 0 ");

		if($this->users->Numrows == 1){

			$this->users->Update("is_enabled = 1", "activation_hash='".$_GET['hash']."' ");
			$this->users->Select(" activation_hash='".$_GET['hash']."' and is_enabled = 1 ");
			if($this->users->Numrows > 0){

				$this->confirmed = true;
			}
		}
	}

	function lostpassword(){

		if($_SESSION['wrong_user_count'] < 3){

			$this->users->Select("WHERE name = '".$_POST['user']."' ", true);

			if($this->users->Numrows == 1){

				$this->clients->Select("WHERE user_id = ".$this->users->Rows[0]['id'], true);
				$this->company->Select("WHERE client_id = ".$this->clients->Rows[0]['id'], true);

				if($this->company->Rows[0]['status'] == 0){

					echo json_encode(array('status' => 0, 'message' => $this->Language('Company not active')));
				}
				else{

					$name = $this->company->Rows[0]['name'];
					$email = $this->company->Rows[0]['email'];

					$randompw = $this->Functions->RandomString(8);
					$email_data = $this->Functions->getMessage('lostpassword', 'EMAIL', $_SESSION['language']);
					$content = $this->Replace($email_data['content'], array('email' => $email, 'password' => $randompw));

					$this->Email->SendEmail($name, $email, $email_data['from_name'], $email_data['from_email'], $email_data['subject'], $content);
					$this->users->Update(array('temp_password' => md5($randompw)), "WHERE id = ".$this->users->Rows[0]['id']);

					echo json_encode(array('status' => 1, 'message' => $this->Language('New password sent to your email address')));
				}
			}
			else{

				echo json_encode(array('status' => 0, 'message' => $this->Language('Wrong user name')));
				$_SESSION['wrong_user_count']++;
			}
		}
		else{
			echo json_encode(array('status' => 0, 'message' => $this->Language('blocked attempt')));
		}
	}

	function register(){

		$this->roles->Select("WHERE type = 'CLIENT' ", true);
		$role_id = $this->roles->Rows[0]['id'];
		$role_type = $this->roles->Rows[0]['type'];

		$password = $this->Functions->RandomString(8);

		$this->company->Select("WHERE register_nr = '".$_POST['register_nr']."' ", true);
		if($this->company->Numrows > 0){

			if($this->company->Rows[0]['status'] == 0){

				die(json_encode(array('status' => 0, 'message' => $this->Language('Firma on kustutatud'))));
			}
		}
		if($this->company->Numrows > 1){

			die(json_encode(array('status' => 0, 'message' => $this->Language('Antud registri koodiga on registreeritud 2 firmat, palun võta ühendust info@infojuht.ee'))));
		}
		if(!$this->company->Rows[0]['email'] && $this->company->Numrows > 0){

			die(json_encode(array('status' => 0, 'message' => $this->Language('Teil ei ole emaili aadressi meie baasis, palun võtke ühendust info@infojuht.ee'))));
		}
		//if company exists
		if($this->company->Rows[0]['client_id'] == 0 && $this->company->Numrows > 0){

			$array = array(
				'role_id' => $role_id,
				'name' => $this->company->Rows[0]['email'],
				'password' => md5($password),
				'type' => $role_type
			);
			$this->users->Insert($array);
			$array = array(
				'user_id' => $this->users->Insertid,
				'first_name' => $this->company->Rows[0]['email']
			);
			$this->clients->Insert($array);
			$this->company->Update(array('client_id' => $this->clients->Insertid/*, 'status' => 1*/), "WHERE register_nr = '".$_POST['register_nr']."'");
			$array = array(
				'client_id' => $this->clients->Insertid
			);
			$this->client_details->Insert($array);



			//kasutaja emaili saatmine
			$name = $this->company->Rows[0]['name'];
			$email = $this->company->Rows[0]['email'];
			$email_data = $this->Functions->getMessage('register_user', 'EMAIL', $_SESSION['language']);
			$content = $this->Replace($email_data['content'], array('email' => $this->company->Rows[0]['email'], 'password' => $password));

			$this->Email->SendEmail($name, $email, $email_data['from_name'], $email_data['from_email'], $email_data['subject'], $content);
		}
		//if company not ecxists
		if($this->company->Numrows == 0){

			$array = array(
				'role_id' => $role_id,
				'name' => $_POST['email'],
				'password' => md5($password),
				'type' => $role_type
			);
			$this->users->Insert($array);
			$array = array(
				'user_id' => $this->users->Insertid,
				'first_name' => $_POST['email']
			);
			$this->clients->Insert($array);
			$this->company->Insert(array('client_id' => $this->clients->Insertid, 'status' => 1, 'name' => $_POST['name'], 'register_nr' => $_POST['register_nr'], 'email' => $_POST['email']));
			$array = array(
				'client_id' => $this->clients->Insertid
			);
			$this->client_details->Insert($array);



			//kasutaja emaili saatmine
			$name = $_POST['name'];
			$email = $_POST['email'];
			$email_data = $this->Functions->getMessage('register_user', 'EMAIL', $_SESSION['language']);
			$content = $this->Replace($email_data['content'], array('email' => $_POST['email'], 'password' => $password));

			$this->Email->SendEmail($name, $email, $email_data['from_name'], $email_data['from_email'], $email_data['subject'], $content);
			die(json_encode(array('status' => 1, 'message' => $this->Language('Teie andmeid kontrollitakse! Vastus saadetakse epostile.'))));
		}
		die(json_encode(array('status' => 1, 'message' => $this->Language('Firma edukalt registreeritud, parool saadetud emailile'))));
	}
}


?>