<?php

class Install {
	var $srvlic;
	var $version_data = array();
	var $client_licence = array();
	var $mysql;
	
	function __construct($mysql){
		//Nastavi mysql
		$this->mysql = $mysql;
		
		//Preveri ali smo na loginu
		if($_SESSION['prijavljen']){
			$this->ReadClientLicence();
		}else{
			$this->srvlic = "http://licence.cms.zabec.net/";
			$this->GetClientLicence();
			$this->GetVersionData();
			$this->PreveriVeljavnost();
		}
	}
	
	function ReadClientLicence() {
		$result = $this->mysql->query("SELECT * FROM core_licence WHERE id='1'");
		$r = $result->fetch();
		$this->client_licence['version_id'] = $r['version_id'];
		$this->client_licence['versionname'] = $r['versionname'];		
		$this->client_licence['licence_id'] = $r['licence_id'];
		$this->client_licence['lastversion'] = $r['lastversion'];
		$this->client_licence['buildid'] = $r['buildid'];
		$this->client_licence['builddate'] = $r['builddate'];
		$this->client_licence['buildsigned'] = $r['buildsigned'];
		$this->client_licence['changelog'] = $r['changelog'];
		$this->client_licence['client_name'] = $r['client_name'];
		$this->client_licence['client_reseller'] = $r['client_reseller'];
		$this->client_licence['valid'] = $r['valid'];
		$this->client_licence['upgrade'] = $r['upgrade'];
	}

	function GetClientLicence(){
		//Preberi podatke za licenco
		$dom = new DOMDocument();
		$dom->load($this->srvlic . "/get-licence.php?server=" . $_SERVER['HTTP_HOST']);
		$nodes = $dom->getElementsByTagName("licence");
	
		$firstnode = $nodes->item(0)->childNodes;
		$size = $firstnode->length;
		for ($i = 0; $i<$size; $i++){
			if ($firstnode->item($i)->nodeType == 1){
				$this->client_licence[$firstnode->item($i)->nodeName] = $firstnode->item($i)->nodeValue;
			}
		}
		$this->UpdateClientLicence();
	}
	
	function UpdateClientLicence(){
		$result = $this->mysql->query("UPDATE core_licence SET versionid='" . $this->client_licence[versionid] . "', versionname='" . $this->client_licence[versionname] . "', buildid='" . $this->client_licence[buildid] . "', builddate='" . $this->client_licence[builddate] . "', buildsigned='" . $this->client_licence[buildsigned] . "', changelog='" . $this->client_licence[changelog] . "', client_name='" . $this->client_licence[client_name] . "', client_reseller='" . $this->client_licence[client_reseller] . "', valid='" . $this->client_licence[valid] . "', upgrade='" . $this->client_licence[upgrade] . "', licence_id='" . $this->client_licence[licence_id] . "' WHERE id=1");
	}
	
	function GetVersionData(){
		//Prenesi zadnjo verzijo
		$dom = new DOMDocument();
		$dom->load($this->srvlic . "last-ver.php?version=" . $this->client_licence[versionid]);
		$nodes = $dom->getElementsByTagName("version");
	
		$firstnode = $nodes->item(0)->childNodes;
		$size = $firstnode->length;
		for ($i = 0; $i<$size; $i++){
			if ($firstnode->item($i)->nodeType == 1){
				$this->version_data[$firstnode->item($i)->nodeName] = $firstnode->item($i)->nodeValue;
			}
		}	
	}
	
	function GetLicenceData() {
		return $this->client_licence;
	}
	
	function PreveriVeljavnost() {
		if($this->client_licence['valid'] < date("Y-m-d\TH:i:s")){
			redirect($this->srvlic . "licence.php?type=licence_notvalid&client_url=" . $_SERVER['HTTP_HOST'] . "&serverip=" . $_SERVER['SERVER_ADDR'] . "&licenceid=" . $this->client_licence[licence_id] ."");
		}
	}

	function PreveriVerzijo(){
		print $this->version_data['buildid'] . "|" . $this->client_licence['buildid'];
		if($this->version_data['buildid'] > $this->client_licence['buildid']){
			//Nadgradnja je možna
			if($this->client_licence['upgrade']){
				//Nadgradi
			}
		}
	}
}

?>