<?php

GLOBAL $ROOT_PATH, $TOOLS_PATH, $COMMON_VARS;
include_once($TOOLS_PATH.'/common_vars.inc');
include_once($TOOLS_PATH.'/func.php');
include_once($TOOLS_PATH.'/Archive.class.inc');

class EnginePacketDump {
	
	var $packet = array();
	var $path = '';
	var $errors = array();
	var $debug = false;

	function EnginePacketDump(&$packet, $path = '', $debug = false){
		$this->debug = $debug;
		if($this->debug) $this->printDebug('EnginePacketDump construct');
		if(!file_exists($path)) return $this->returnError('Packets storage path "'.$path.'" does not exist');
		if(!is_writable($path)) return $this->returnError('Packets storage path "'.$path.'" is not writable');
		$this->packet = &$packet;
		$this->path = $path;
		return true;
	}
	
	function dumpEnginePacket(){
		GLOBAL $ROOT_PATH, $COMMON_VARS;
		if($this->debug) $this->printDebug('Dump engine packet "'.$this->packet['name'].'" ['.$this->packet['id'].'] (Unique ID ['.$this->packet['uid'].'])');
		if (empty($this->packet['id'])) return $this->returnError('Packet ID is not specified');
		if (empty($this->packet['uid'])) return $this->returnError('Packet Unique ID is not specified');
		if (empty($this->path)) return $this->returnError('Packet dump path "'.$this->path.'" is not specified');
		if (!file_exists($this->path)) return $this->returnError('Packet dump path "'.$this->path.'" doesn\'t exist');
		$this->packet['filename'] = $this->packet['uid'].$COMMON_VARS['enginepacket_ext'];
		$this->packet['filepath'] = $this->path.'/'.$this->packet['filename'];
		if(file_exists($this->packet['filepath'])) mysystem('rm -rf '.$this->packet['filepath']);
		$temp_dir_path = $ROOT_PATH.'/'.$COMMON_VARS['enginepacket_dir_temp'].'/'.$this->packet['uid'];
		if(!file_exists($temp_dir_path) && !mkdir($temp_dir_path)) return $this->returnError('Can not make temp packet dir');
		$temp_file_path = $temp_dir_path.'/enginepacket.inc';
		$file = fopen($temp_file_path, "w");
		if(fwrite($file, '<?php'."\r\n".'$packet = '.var_export($this->packet, true).';'."\r\n".'?>') === false) return $this->returnError('Can not make temp packet file');
		fclose($file);
		$files = array();
		$count = count($this->packet['items']);
		for ($i = 0; $i < $count; $i++){
			if(empty($this->packet['items'][$i]['parent'])) $files[substr($this->packet['items'][$i]['object'], 1)] = true;
			if(!is_dir($ROOT_PATH.$this->packet['items'][$i]['object'])){
				$this->packet['items'][$i]['date_changed'] = date('Y-m-d H:i:s', filemtime($ROOT_PATH.$this->packet['items'][$i]['object']));
				$this->packet['items'][$i]['hash'] = md5_file($ROOT_PATH.$this->packet['items'][$i]['object']);
			}
		}
		$files = array_keys($files);
		$files[] = substr($temp_file_path, strlen($ROOT_PATH.'/'));
		$Archive = new Archive($ROOT_PATH.'/', dirname($this->packet['filepath']).'/', basename($this->packet['filepath']), false);
		$Archive->setTarPath($COMMON_VARS['tar_dir']);
		$Archive->setFilesNames($files);
		$Archive->createArchive();
		mysystem('rm -rf '.$temp_dir_path);
		if(empty($Archive->errors)) return $this->returnNoError();
		else{
			$this->errors = array_merge($this->errors, $Archive->errors);
			return $this->returnError();
		}
	}
	
	function restoreEnginePacket(){
		GLOBAL $ROOT_PATH, $COMMON_VARS;
		if($this->debug) $this->printDebug('Restore engine packet "'.$this->packet['name'].'" ['.$this->packet['id'].'] (Unique ID ['.$this->packet['uid'].'])');
		if (empty($this->packet['id'])) return $this->returnError('Packet ID is not specified');
		if (empty($this->packet['filename'])) return $this->returnError('Packet file name is not specified');
		if (empty($this->path)) return $this->returnError('Packet dump path "'.$this->path.'" is not specified');
		if (!file_exists($this->path)) return $this->returnError('Packet dump path "'.$this->path.'" doesn\'t exist');
		$this->packet['filepath'] = $this->path.'/'.$this->packet['filename']; 
		if (!file_exists($this->packet['filepath'])) return $this->returnError('Packet file "'.$this->packet['filepath'].'" doesn\'t exist');
		$success = false;
		$Archive = new Archive(dirname($this->packet['filepath']).'/', $ROOT_PATH.'/', $this->packet['filename'], true);
		$Archive->setTarPath($COMMON_VARS['tar_dir']);
		$Archive->extractArchive();
		$temp_dir_path = $ROOT_PATH.'/'.$COMMON_VARS['enginepacket_dir_temp'].'/'.$this->packet['uid'];
		mysystem('rm -rf '.$temp_dir_path);
		if(empty($Archive->errors)) $success = true;
		else {
			$this->errors = array_merge($this->errors, $Archive->errors);
			$success = false;
		}
		$count = count($this->packet['items']);
		for ($i = 0; $i < $count; $i++){
			$path = substr($this->packet['items'][$i]['object'], 1);
			@chmod($path, 0774);
			$command = './chowner '.$path;
			$res = system($command, $var);
		}
		return $success ? $this->returnNoError() : $this->returnError();
	}
	
	function returnNoError($message = ''){
		if($this->debug){
			if(!empty($message)) $this->printDebug($message);
			$this->printDebug('EnginePacketDump success');
		}
		return true;
	}
	
	function returnError($error = ''){
		if($this->debug){
			if(!empty($error)) $this->printDebug($error);
			$this->printDebug('EnginePacketDump failed');
		}
		if(!empty($error)) $this->errors[] = $error;
		return false;
	}
	
	function printDebug($text){
		print(date("H:i:s", time()).' '.$text.'<br>'."\r\n");
	}
}

?>