<?php
include_once($TOOLS_PATH.'\class.curl.php');
include_once($TOOLS_PATH.'\Database.class.inc');

class CheckRemoteServer extends curl {

	var $preset = array();
	var $errors = array();
	var $receiver = 'receiver.php';
	var $get_session = 'get_session.php';
	
	function CheckRemoteServer($preset = array()){
		if(!empty($preset)){
			if(is_array($preset)) $this->preset = $preset;
			else
				$this->errors[] = 'Server PreSet must be an array';
		}
	}

	function preActionCheck($type_server, $timeout = 30, $useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', $header = 0, $nobody = 1){
		$check = true;
		if($this->checkFTPConnect($timeout, $useragent) !== false){
			if(!$this->checkSignature($type_server)) $check = false;
		}
		else $check = false;
		if($this->checkHTTPConnect($timeout, $useragent, $header, $nobody) === false) $check = false;
		if($this->checkDBConnect() === false) $check = false;
		return $check;
	}

	function checkHTTPConnect($timeout = 30, $useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', $header = 0, $nobody = 1){
		$this->curl();
		$this->setopt(CURLOPT_URL, $this->preset['path']);
		$this->setopt(CURLOPT_HEADER, $header);
		$this->setopt(CURLOPT_NOBODY, $nobody);
		$this->setopt(CURLOPT_RETURNTRANSFER, 1);
		$this->setopt(CURLOPT_USERAGENT, $useragent);
		$this->setopt(CURLOPT_VERBOSE, 1);
		$this->setopt(CURLOPT_TIMEOUT, $timeout);
		$result = $this->exec();
		$this->close();
		if(!empty($this->m_status['error'])){
			$this->errors[] = $this->m_status['error'];
			return false;
		}
		if(!empty($this->m_status['errno'])){
			$this->errors[] = 'Unexpected transfer error #'.$this->m_status['errno'];
			return false;
		}
		return $result;
	}

	function checkFTPConnect($timeout = 30, $useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'){
		$this->curl();
		$this->setopt(CURLOPT_TIMEOUT, $timeout);
		$this->setopt(CURLOPT_USERAGENT, $useragent);
		$this->setopt(CURLOPT_RETURNTRANSFER, 1);
		$this->setopt(CURLOPT_VERBOSE, 1);
		$this->setopt(CURLOPT_FTPLISTONLY, 1);
		$this->setopt(CURLOPT_URL, 'ftp://'.$this->preset['ftp_user'].':'.$this->preset['ftp_password'].'@'.$this->preset['ftp_host'].'/%2f'.$this->preset['ftp_folder']);
		$result = $this->exec();
		$this->close();
		if(!empty($this->m_status['error'])){
			$this->errors[] = $this->m_status['error'];
			return false;
		}
		if(!empty($this->m_status['errno'])){
			$this->errors[] = 'Unexpected transfer error #'.$this->m_status['errno'];
			return false;
		}
		return $result;
	}

	function checkDBConnect(){
		$Database = new Database($this->preset['db_host'], $this->preset['db_name'], $this->preset['db_user'], $this->preset['db_password'], $this->preset['db_port']);
		$Database->connect();
		$Database->close();
		if(empty($Database->Errors)) return true;
		else{
			$this->errors = array_merge($this->errors, $Database->Errors);
			return false;
		}
	}

	function checkSignature($type_server){
		$this->curl();
		$this->setopt(CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
		$this->setopt(CURLOPT_URL, 'ftp://'.$this->preset['ftp_user'].':'.$this->preset['ftp_password'].'@'.$this->preset['ftp_host'].'/%2f'.$this->preset['ftp_folder'].'engine/signature.inc');
		$this->setopt(CURLOPT_RETURNTRANSFER, 1);
		$this->setopt(CURLOPT_UPLOAD, 0);
		$this->setopt('CURLOPT_FTPASCII', 0);
		$this->setopt(CURLOPT_TIMEOUT, 30);
		$result = $this->exec();
		$this->close();
		if(!empty($this->m_status['error'])){
			$this->errors[] = $this->m_status['error'];
			return false;
		}
		if(!empty($this->m_status['errno'])){
			$this->errors[] = 'Unexpected transfer error #'.$this->m_status['errno'];
			return false;
		}
		eval($result);
		$er_count1 = count($this->errors);
		if($signature['db_host'] != $this->preset['db_host']) $this->errors[] = 'PreSet database host and signature database host do not match';
		if($signature['db_name'] != $this->preset['db_name']) $this->errors[] = 'PreSet database name and signature database name do not match';
		if($signature['db_user'] != $this->preset['db_user']) $this->errors[] = 'PreSet database user and signature database user do not match';
		if($signature['db_password'] != $this->preset['db_password']) $this->errors[] = 'PreSet database password and signature database password do not match';
		if($signature['db_port'] != $this->preset['db_port']) $this->errors[] = 'PreSet database port and signature database port do not match';
		if(!empty($type_server) && !empty($signature['type_server']) && $signature['type_server'] != $type_server) $this->errors[] = 'Servers types do not match';
		$er_count2 = count($this->errors);
		if($er_count2 > $er_count1) return false;
		return true;
	}

	function getServerPriorityType(){
		$this->curl();
		$this->setopt(CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
		$this->setopt(CURLOPT_URL, 'ftp://'.$this->preset['ftp_user'].':'.$this->preset['ftp_password'].'@'.$this->preset['ftp_host'].'/%2f'.$this->preset['ftp_folder'].'engine/signature.inc');
		$this->setopt(CURLOPT_RETURNTRANSFER, 1);
		$this->setopt(CURLOPT_UPLOAD, 0);
		$this->setopt(CURLOPT_FTPASCII, 0);
		$this->setopt(CURLOPT_TIMEOUT, 30);
		$result = $this->exec();
		$this->close();
		if(!empty($this->m_status['error'])){
			$this->errors[] = $this->m_status['error'];
			return false;
		}
		if(!empty($this->m_status['errno'])){
			$this->errors[] = 'Unexpected transfer error #'.$this->m_status['errno'];
			return false;
		}
		eval($result);
		return $signature['type'];
	}

	function checkConnectToReceiver($timeout = 30, $useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)') {
		$this->curl();
		$this->setopt(CURLOPT_URL, $this->preset['path'].$this->receiver);
		$this->setopt(CURLOPT_POST, 1);
		$this->setopt(CURLOPT_POSTFIELDS, 'type=check');
		$this->setopt(CURLOPT_HEADER, 0);
		$this->setopt(CURLOPT_RETURNTRANSFER, 1);
		$this->setopt(CURLOPT_USERAGENT, $useragent);
		$this->setopt(CURLOPT_VERBOSE, 1);
		$this->setopt(CURLOPT_TIMEOUT, $timeout);
		$result['debug'] = $this->exec();
		$result['report'] = array();
		$this->close();
		if(!empty($this->m_status['error'])) $this->errors[] = $this->m_status['error'];
		else if(!empty($this->m_status['errno'])) $this->errors[] = 'Unexpected transfer error #'.$this->m_status['errno'];
		if(empty($this->errors)){
			$this->curl();
			$this->setopt(CURLOPT_USERAGENT, $useragent);
			$this->setopt(CURLOPT_URL, $this->preset['path'].$this->get_session);
			$SID = strtok($result['debug'], "\r\n");
			$SID = explode('=', $SID);
			$this->setopt(CURLOPT_POST, 1);
			$this->setopt(CURLOPT_POSTFIELDS, $SID[0].'='.$SID[1]);
			$this->setopt(CURLOPT_HEADER, 0);
			$this->setopt(CURLOPT_RETURNTRANSFER, 1);
			$this->setopt(CURLOPT_VERBOSE, 1);
			$this->setopt(CURLOPT_TIMEOUT, $timeout);
			$report = $this->exec();
			$this->close();
			if(!empty($this->m_status['error'])) $this->errors[] = $this->m_status['error'];
			else if(!empty($this->m_status['errno'])) $this->errors[] = 'Unexpected transfer error #'.$this->m_status['errno'];
			if(empty($this->errors)) @eval('$result[\'report\'] = '.$report.';');
		}
		return $result;
	}

	function clearErrors(){
		$this->errors = array();
	}
}
?>