<?php

class BackUpSystemView extends Slot {

	var $SectionID = 39;
	var $ModuleID = 4;
	var $backups = array();
	var $backup = array (
		'id' => '',
		'name' => '',
		'date_created' => '',
		'filename' => '',
		'uid' => '',
		'action' => array(),
		'fileinfo' => array(),
	);
	var $report_type = array();
	var $errors = array();
	var $success = false;
	var $date_actioned;
	var $servers = array();
	var $presets = array();
	var $report_receivers = array();
	var $MailTemplate = array();
	var $old_backups = array();

	function __construct($pquery) {
		parent::__construct($pquery);
	}

	function init() {
		GLOBAL $ROOT_PATH, $TOOLS_PATH, $CORE_PATH, $COMMON_VARS, $MailBroker, $REGISTRY;
		include_once($ROOT_PATH.'/engine/Tools/HandleBackup.class.inc');
		$this->backups = $this->Action->Server->getGuide('backup');
		$this->types = $this->Action->Server->getGuide('backupactiontype');
		$this->Action->processEvent($this->SectionID, 'select_report_types', array(), $this);
		$this->report_type = $this->PreparedTemplates;
		if(!empty($this->LocalTarget[0]) && (($this->LocalTarget[0] != 'Backup' && $this->LocalTarget[0] != 'FullSiteCopy' && $this->LocalTarget[0] != 'InitFullSiteCopy' && $this->LocalTarget[0] != 'DeleteOld' && (empty($this->LocalTarget[1]) || !array_key_exists($this->LocalTarget[1], $this->backups))) || (($this->LocalTarget[0] == 'Publish' || $this->LocalTarget[0] == 'FullSiteCopy') && $this->server_type != 'dev' && $this->server_type != 'sta'))){
			$this->clearAllTemplates();
			header('Location: '.$this->Home.'/Registar/BackUpSystem'.$this->SID);
			return;
		}
		switch(@$this->LocalTarget[0]) {
			case 'Backup': {
				$this->Scheme = 'action';
				break;
			}
			case 'Look': case 'Restore': case 'Delete': case 'Publish': {
				$this->Action->processEvent($this->SectionID, 'open_backup', array('id' => $this->LocalTarget[1]), $this);
				$this->backup = $this->PreparedTemplates;
				if($this->LocalTarget[0] == 'Restore' || $this->LocalTarget[0] == 'Publish'){
					$backup_engine_version = explode('.', $this->backup['engine_version']);
					$engine_version = explode('.', $REGISTRY['Core']['EngineVersion']);
					if($backup_engine_version[0] != $engine_version[0] || $backup_engine_version[1] != $engine_version[1]){
						$this->clearAllTemplates();
						header('Location: '.$this->Home.'/Registar/BackUpSystem'.$this->SID);
						return;
					}
				}
				$this->Scheme = 'action';
				if($this->LocalTarget[0] == 'Publish'){
					$this->Action->processEvent($this->SectionID, 'select_presets', array(), $this);
					$this->servers = $this->PreparedTemplates;
					$this->Action->processEvent($this->SectionID, 'select_report_receivers', array(), $this);
					$this->report_receivers = $this->PreparedTemplates;
				}
				$this->backup['action_type'] = $this->LocalTarget[0];
				break;
			}
			case 'FullSiteCopy': {
				$this->Scheme = 'action';
				$this->Request['form']['name'] = 'Full Site Copy '.date('D j M Y H-i-s', time());
				break;
			}
			case 'DeleteOld': {
				$this->Scheme = 'deleteold';
				break;
			}
			default: {
				$this->Action->processEvent($this->SectionID, 'select_backups', array(), $this);
				$this->Scheme = 'default';
				break;
			}
		}
		if(empty($this->Request)){
			$this->setSchemeTemplate($this->Scheme);
			return;
		}
		if($this->LocalTarget[0] != 'DeleteOld')
			foreach($this->Request['form'] as $key => $value) $this->backup[$key] = $value;
		switch(@$this->LocalTarget[0]){
			case 'Backup': case 'FullSiteCopy': {
				if(!$this->validateForm()) break;
				$this->backup['action_type'] = 'Backup';
				$this->backup['uid'] = str_replace(array('.', ' '), '', microtime());
				$this->backup['filename'] = $this->backup['uid'].$COMMON_VARS['backup_ext'];
				$this->date_actioned = date('Y-m-d H:i:s', time());
				$this->backup['date_created'] = $this->date_actioned;
				$this->backup['engine_version'] = $REGISTRY['Core']['EngineVersion'];
				$this->backup['action'][0] = array(
					'date_actioned' => $this->date_actioned,
					'type' => 1,
					'userid' => $this->Action->Security->currentuser->id,
				);
				$this->addReport($this->backup['action'][0], 'Begin backuping', 2);
				$HandleBackup = new HandleBackup($COMMON_VARS['backup_dir'], $COMMON_VARS['backup_dir_temp'], $this->backup['uid'], $COMMON_VARS['backup_ext']);
				$HandleBackup->makeBackup();
				$this->backup['action'][0]['report'] = array_merge($this->backup['action'][0]['report'], $HandleBackup->messages);
				if(empty($HandleBackup->errors)){
					$this->backup['action'][0]['successful'] = 1;
					$this->addReport($this->backup['action'][0], 'End backuping', 1);
					$this->Action->processEvent($this->SectionID, 'save_backup', $this->backup, $this);
					$this->backup = $this->PreparedTemplates;
					$this->Action->processEvent($this->SectionID, 'open_backup', $this->backup, $this);
					$this->backup = $this->PreparedTemplates;
					$this->success = true;
				}
				else{
					$this->backup['action'][0]['report'] = array_merge($this->backup['action'][0]['report'], $HandleBackup->errors);
					$this->addReport($this->backup['action'][0], 'Backuping failed', 7);
					$this->success = false;
					mysystem('rm -rf '.$ROOT_PATH.'/'.$COMMON_VARS['backup_dir'].'/'.$this->backup['filename']);
				}
				if($this->LocalTarget[0] == 'FullSiteCopy'){
					header('Location: '.$this->Home.'/Registar/BackUpSystem/Publish/'.$this->backup['id'].$this->SID);
					$this->clearAllTemplates();
					return;
				}
				break;
			}
			case 'Restore': {
				$this->date_actioned = date('Y-m-d H:i:s', time());
				$key = count($this->backup['action']);
				$this->backup['action'][$key] = array(
					'date_actioned' => $this->date_actioned,
					'type' => 2,
					'userid' => $this->Action->Security->currentuser->id,
				);
				$this->addReport($this->backup['action'][$key], 'Begin restoring', 2);
				$HandleBackup = new HandleBackup($COMMON_VARS['backup_dir'], $COMMON_VARS['backup_dir_temp'], $this->backup['uid'], substr($this->backup['filename'], strlen($this->backup['uid'])));
				$HandleBackup->restoreFromBackup();
				$this->backup['action'][$key]['report'] = array_merge($this->backup['action'][$key]['report'], $HandleBackup->messages);
				if(empty($HandleBackup->errors)){
					$this->Action->processEvent($this->SectionID, 'log', array('type' => 2, 'entity' => 3, 'event' => 6, 'value' => 'Restore backup "'.$this->backup['name'].'" ['.$this->backup['id'].'] from file "'.$this->backup['filename'].'"'), $this);
					$this->addReport($this->backup['action'][$key], 'End restoring', 1);
					$this->backup['action'][$key]['successful'] = 1;
					$this->success = true;
				}
				else{
					$this->backup['action'][$key]['report'] = array_merge($this->backup['action'][$key]['report'], $HandleBackup->errors);
					$this->addReport($this->backup['action'][$key], 'Restoring failed', 7);
					$this->success = false;
				}
				$this->backup['date_restored'] = 'now';
				$this->Action->processEvent($this->SectionID, 'save_backup', $this->backup, $this);
				$this->backup = $this->PreparedTemplates;
				$this->Action->processEvent($this->SectionID, 'open_backup', $this->backup, $this);
				$this->backup = $this->PreparedTemplates;
				break;
			}
			case 'Delete': {
				$this->Action->processEvent($this->SectionID, 'delete_backup', $this->backup, $this)
				and mysystem('rm -rf '.$ROOT_PATH.'/'.$COMMON_VARS['backup_dir'].'/'.$this->backup['filename']);
				$this->clearAllTemplates();
				header('Location: '.$this->Home.'/Registar/BackUpSystem'.$this->SID);
				return;
				break;
			}
			case 'DeleteOld': {
				$this->Action->processEvent($this->SectionID, 'delete_old_backups', array(), $this);
				$this->old_backups = $this->PreparedTemplates;
				break;
			}
			case 'Publish': {
				if(empty($this->Request['form']['servers']) && empty($this->Request['form']['groups'])) break;
				include_once($TOOLS_PATH.'/Transfer.class.inc');
				include_once($TOOLS_PATH.'/Database.class.inc');
				include_once($TOOLS_PATH.'/CheckRemoteServer.class.inc');
				if(empty($this->Request['form']['servers'])) $this->Request['form']['servers'] = array();
				if(empty($this->Request['form']['groups'])) $this->Request['form']['groups'] = array();				$this->date_actioned = date('Y-m-d H:i:s', time());
				eval(file_get_contents($CORE_PATH.'/signature.inc'));
				foreach($this->Request['form']['servers'] as $value){
					foreach($this->servers['1']['presets'] as $val){
						if ($val['id'] != $value) continue;
						$this->backup['action']['0'.$val['id']] = array(
							'id' => '0'.$val['id'],
							'date_actioned' => $this->date_actioned,
							'type' => 3,
							'userid' => $this->Action->Security->currentuser->id,
							'username' => $this->Action->Security->currentuser->username,
							'report' => array(),
							'host' => $val['id'],
							'hostpath' => $val['name'],
							'successful' => 0,
						);
						$CheckRemoteServer = new CheckRemoteServer($val);
						if($CheckRemoteServer->preActionCheck($signature['type_server'], 30))
							$this->presets[] = $val;
						else{
							foreach($CheckRemoteServer->errors as $v)
								$this->addReport($this->backup['action']['0'.$val['id']], $v, 0);
							$this->addToMailTemplate(array('hostname' => $val['name'], 'hostpath' => $val['path'], 'result' => 'Failed',));
						}
						break;
					}
				}
				foreach($this->Request['form']['groups'] as $value){
					if(empty($this->servers[$value])) continue;
					$check = true;
					$array = array();
					foreach($this->servers[$value]['presets'] as $val){
						$array['0'.$val['id']] = array('name' => $val['name'], 'path' => $val['path']);
						$this->backup['action']['0'.$val['id']] = array(
							'id' => '0'.$val['id'],
							'date_actioned' => $this->date_actioned,
							'type' => 3,
							'userid' => $this->Action->Security->currentuser->id,
							'username' => $this->Action->Security->currentuser->username,
							'host' => $val['id'],
							'hostpath' => $val['name'],
							'report' => array(),
							'successful' => 0,
						);
						$CheckRemoteServer = new CheckRemoteServer($val);
						if(!$CheckRemoteServer->preActionCheck($signature['type_server'], 30)){
							$check = false;
							foreach($CheckRemoteServer->errors as $v)
								$this->addReport($this->backup['action']['0'.$val['id']], $v, 0);
							$this->addToMailTemplate(array('hostname' => $val['name'], 'hostpath' => $val['path'], 'result' => 'Failed',));
						}
					}
					if($check) $this->presets = array_merge($this->presets, $this->servers[$value]['presets']);
					else foreach($array as $ind => $val){
						if(empty($this->backup['action'][$ind]['report'])){
							$this->addReport($this->backup['action'][$ind], 'Some server from the same PreSet group doesn\'t responding', 0);
							$this->addToMailTemplate(array('hostname' => $val['name'], 'hostpath' => $val['path'], 'result' => 'Failed',));
						}
					}
				}
				foreach ($this->presets as $key => $value){
					if($this->transferBackup($value, false)){
						$this->launchScript($value);
						$info = $this->getReport($value);
						$result = end($this->backup['action']['0'.$value['id']]['report']);
						if($result['type'] == array_search('Good', $this->report_type)){
							$this->backup['action']['0'.$value['id']]['successful'] = 1;
							$this->Action->processEvent($this->SectionID, 'log', array('type' => 2, 'entity' => 3, 'event' => 14, 'value' => 'Publish backup "'.$this->backup['name'].'" ['.$this->backup['id'].'] from the file "'.$this->backup['filename'].'" to "'.$value['name'].'" ['.$value['id'].']'), $this);
							$this->Action->processEvent($this->SectionID, 'delete_changed_url', array('preset' => $value['id'], 'objects_state' => $this->backup['objects_state']), $this);
							//$this->Action->processEvent($this->SectionID, 'delete_changed_url', array('preset' => $value['id']), $this);
							//$this->Action->processEvent($this->SectionID, 'add_deleted_objects', array('preset' => $value['id'], 'objects_state' => $this->backup['objects_state']), $this);
						}
						$this->putRemoteReport($value, $info);
						$this->addToMailTemplate(array('hostname' => $value['name'], 'hostpath' => $value['path'], 'result' => $this->backup['action']['0'.$value['id']]['successful'] == 1 ? 'Successful' : 'Failed',));
					}
				}
				$this->Action->processEvent($this->SectionID, 'save_backup', $this->backup, $this);
				$this->backup = $this->PreparedTemplates;
				$MailBroker->sendMail('publish', $this->report_receivers, $this->MailTemplate);
				$this->Action->processEvent($this->SectionID, 'open_backup', $this->backup, $this);
				$this->backup = $this->PreparedTemplates;
				break;
			}
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function fillElements() {
		switch($this->Scheme) {
			case 'default': default: $this->fillDefault(); break;
			case 'action': $this->fillAction(); break;
			case 'deleteold': $this->fillDeleteOld(); break;
		}
	}
	
	function validateForm(){
		GLOBAL $ROOT_PATH, $COMMON_VARS;
		if(!is_NumText($this->Request['form']['name'], 1, 128))
			$this->errors['name'] = 'Backup Name is not valid';
		if(($id = array_search($this->Request['form']['name'], $this->backups) !== false) && $id != $this->Request['form']['id'])
			$this->errors['name'] = 'Backup with such name already exists';
		return empty($this->errors) ? true : false;
	}
	
	function fillDefault(){
		GLOBAL $COMMON_VARS, $REGISTRY;
		$engine_version = explode('.', $REGISTRY['Core']['EngineVersion']);
		$bad_server_type = ($this->server_type != 'dev' && $this->server_type != 'sta') ? true : false;
		if($bad_server_type) $this->TempFillEls['btnFullSiteCopy_vis'] = 'hidden';
		foreach($this->PreparedTemplates as $key => $value){
			$this->TempFillEls['backup'][$key] = $value;
			if($value['date_restored'] == '1970-01-01 00:00:00' || empty($value['date_restored'])) $this->TempFillEls['backup'][$key]['date_restored'] = '&nbsp;';
			if(empty($value['engine_version'])) $this->TempFillEls['backup'][$key]['engine_version'] = '<unknown>';
			$this->TempFillEls['backup'][$key]['path'] = '/'.$COMMON_VARS['backup_dir'].'/'.$value['filename'];
			if(empty($value['_error'])) $this->TempFillEls['backup'][$key]['img'] = $value['date_restored'] == '1970-01-01 00:00:00' ? 'pack1' : 'pack3';
			else{
				$this->TempFillEls['backup'][$key]['img'] = $value['date_restored'] == '1970-01-01 00:00:00' ? 'pack4' : 'pack6';
				$this->TempFillEls['backup'][$key]['btnDownload_vis'] = 'hidden';
				$this->TempFillEls['backup'][$key]['btnRestore_vis'] = 'hidden';
				$this->TempFillEls['backup'][$key]['btnPublish_vis'] = 'hidden';
			}
			if($bad_server_type) $this->TempFillEls['backup'][$key]['btnPublish_vis'] = 'hidden';
			$backup_engine_version = explode('.', $value['engine_version']);
			if($engine_version[0] != $backup_engine_version[0] || $engine_version[1] != $backup_engine_version[1]){
				$this->TempFillEls['backup'][$key]['btnRestore_vis'] = 'hidden';
				$this->TempFillEls['backup'][$key]['btnPublish_vis'] = 'hidden';
				$this->TempFillEls['backup'][$key]['obsolete'] = '<span style="color: red;">(obsolete)</span>';
			}
		}
		$this->TempFillEls['InitFullSiteCopy'] = @$this->LocalTarget[0] == 'InitFullSiteCopy' ? 'true' : 'false';
		$this->clearAllTemplates();
	}
	
	function fillAction() {
		foreach($this->backup as $key => $value)
			if(!is_array($value)) $this->TempFillEls[$key] = $value;
		$this->TempFillEls['action'] = $this->LocalTarget[0];
		$this->TempFillEls['form_id'] = inputHidden('form[id]', $this->backup['id']);
		$this->TempFillEls['form_name'] = inputText('form[name]', $this->backup['name'], 80, ($this->LocalTarget[0] == 'Backup' ? '' : 'readonly'), 128);
		$this->TempFillEls['filename'] = $this->backup['filename'];
		$this->TempFillEls['publishable'] = empty($this->backup['_error']) ? 'true' : 'false';
		if(empty($this->backup['_error'])) $this->TempFillEls['error_disp'] = 'none';
		else if($this->LocalTarget[0] != 'Delete') $this->TempFillEls['submit_vis'] = 'hidden';
		if(!empty($this->Request) && ($this->LocalTarget[0] == 'Backup' || $this->LocalTarget[0] == 'Restore') && $this->success) $this->TempFillEls['submit_vis'] = 'hidden';
		foreach($this->backup['fileinfo'] as $key => $value)
			$this->TempFillEls['info'][] = array(
				'param' => ucwords($key),
				'value' => $value,
			);
		if(empty($this->backup['fileinfo'])) $this->TempFillEls['info_disp'] = 'none';
		foreach($this->backup['action'] as $key => $value){
			$host = '';
			if($value['type'] == 3) $host = 'to "'.$value['hostpath'].'" ['.$value['host'].']';
			if($value['type'] == 4) $host = 'from "'.$value['hostpath'].'" ['.$value['host'].']';
			$this->TempFillEls['backupaction'][$key] = array(
				'id' => @$value['id'],
				'type' => ucwords($this->types[$value['type']]),
				'host' => $host,
				'username' => @$value['username'],
				'date_actioned' => $value['date_actioned'],
			);
			if($value['date_actioned'] == $this->date_actioned){
				$this->TempFillEls['backupaction'][$key]['report_box_disp'] = 'inline';
				$this->TempFillEls['backupaction'][$key]['imgActionReportBox'] = '2';
			}
			else $this->TempFillEls['backupaction'][$key]['report_box_disp'] = 'none';
			foreach($value['report'] as $ind => $val){
				$this->TempFillEls['backupaction'][$key]['report'][$ind] = array(
					'datetime' => $val['datetime'],
					'message' => $val['message'],
					'type' => strtolower($this->report_type[$val['type']]),
				);
			}
			if (empty($value['result'])) $this->TempFillEls['backupaction'][$key]['script_result_disp'] = 'none';
			else $this->TempFillEls['backupaction'][$key]['script_result'] = @$value['result'];
		}
		if(empty($this->backup['action'])) $this->TempFillEls['reports_disp'] = 'none';
		$this->TempFillEls['action_box_disp'] = 'none';
		if($this->LocalTarget[0] == 'Backup'){
			if(!$this->success){
				$this->TempFillEls['header_info_disp'] = 'none';
				$this->TempFillEls['date_created_disp'] = 'none';
				$this->TempFillEls['engine_version_disp'] = 'none';
				$this->TempFillEls['filename_disp'] = 'none';
			}
		}
		if(($this->LocalTarget[0] == 'Backup' || $this->LocalTarget[0] == 'Restore' || $this->LocalTarget[0] == 'Publish') && !empty($this->Request) && !empty($this->backup['action'])){
			$this->TempFillEls['action_box_disp'] = 'inline';
			$this->TempFillEls['imgActionActionBox'] = '2';
		}
		$this->TempFillEls['servers_display'] = 'none';
		if($this->LocalTarget[0] == 'Publish' && empty($this->Request) && empty($this->backup['_error'])){
			$this->TempFillEls['servers_display'] = 'inline';
			$this->tempFillServersEls();
		}
		foreach ($this->errors as $key => $value)
			$this->TempFillEls[$key.'_error'] = $this->printError($value);
		$this->clearAllTemplates();
	}
	
	function fillDeleteOld(){//printArray($this->old_backups);
		if(empty($this->Request)) $this->TempFillEls['tblOldBackUps_disp'] = 'none';
		$count = count($this->old_backups);
		for ($i = 0; $i < $count; $i++){
			$this->TempFillEls['old_backups'][] = array(
				'id' => $this->old_backups[$i]['id'],
				'name' => $this->old_backups[$i]['name'],
				'message' => $this->old_backups[$i]['message'].$this->old_backups[$i]['error'],
				'img' => empty($this->old_backups[$i]['error']) ? 'end' : 'attention',
			);
		}
		$this->clearAllTemplates();
	}
	
	function addReport(&$action, $message, $type){
		$action['report'][] = array(
			'datetime' => date('Y-m-d H:i:s', time()),
			'message' => $message,
			'type' => $type,
		);
	}
	
	function addToMailTemplate($template){
		$this->MailTemplate['presets'][] = array(
			'type' => 'Backup',
			'name' => $this->backup['name'],
			'id' => $this->backup['id'],
			'comment' => '',
			'comment_disp' => 'none',
			'host' => $_SERVER['SERVER_NAME'],
			'url' => 'http://'.$_SERVER['HTTP_HOST'].'/Registar/BackUpSystem/Look/'.$this->backup['id'],
			'user' => $this->Action->Security->currentuser->username,
			'datetime' => date('Y-m-d H:i:s', time()),
			'hostname' => $template['hostname'],
			'hostpath' => $template['hostpath'],
			'result' => $template['result'],
		);
	}
	
	function tempFillServersEls(){
		foreach($this->servers as $value) {
			if($value['id'] == 1) {
				foreach($value['presets'] as $v)
					if(!$v['canceled']) $this->TempFillEls['servers'][] = array(
						'id' => inputCheckbox('form[servers][]', false, 'onClick = "CountCheckedPreset(this)"', $v['id']),
						'type' => '[SINGLE]',
						'name' => $v['name'],
						'list' => '&nbsp;',
					);
			}
			else {
				if (empty($value['presets']) || $value['canceled']) continue;
				$servers = array();
				$checked = false;
				foreach($value['presets'] as $v)
					if(!$v['canceled']) $servers[$v['id']] = $v['name'];
				if(!empty($servers)) $this->TempFillEls['servers'][] = array(
					'id' => inputCheckbox('form[groups][]', false, 'onClick = "CountCheckedPreset(this)"', $value['id']),
					'type' => '[GROUP]',
					'name' => $value['name'],
					'list' => '('.implode(', ', $servers).')',
				);
			}
		}
	}
	
	function transferBackup($preset, $delete = false){
		GLOBAL $ROOT_PATH, $COMMON_VARS;
		$Transfer = new Transfer();
		$Transfer->setopt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
		$Transfer->min_speed = 3600;
		$Transfer->dest_path = 'ftp://'.$preset['ftp_user'].':'.$preset['ftp_password'].'@'.$preset['ftp_host'].'/%2f'.$preset['ftp_folder'].$COMMON_VARS['upload_dir'].'/';
		$Transfer->src_path = $ROOT_PATH.'/'.$COMMON_VARS['backup_dir'].'/';
		$Transfer->file = $this->backup['filename'];
		if($delete) $Transfer->delete = true;
		$Transfer->sendFile();
		$Transfer->close();
		if(!empty($Transfer->errors)){
			foreach($Transfer->errors as $value)
				$this->addReport($this->backup['action']['0'.$preset['id']], 'Transfer Backup failed. '.$value, 0);
			return false;
		}
		return true;
	}
	
	function launchScript($preset){
		$Transfer = new Transfer();
		$Transfer->setopt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
		$Transfer->dest_path = $preset['path'];
		$Transfer->file = 'receiver.php';
		$Transfer->variables = array(
			'type' => 'backup',
			'uid' => $this->backup['uid'],
			'filename' => $this->backup['filename'],
			'name' => $this->backup['name'],
			'date_created' => $this->backup['date_created'],
			'engine_version' => $this->backup['engine_version'],
			'username' => $this->Action->Security->currentuser->username,
			'hostpath' => $_SERVER['HTTP_HOST'],
		);
		$this->backup['action']['0'.$preset['id']]['result'] = $Transfer->launchScript();
		if(empty($this->backup['action']['0'.$preset['id']]['result']))
			$this->backup['action']['0'.$preset['id']]['result'] = 'No debug result was retrieved from remote server';
		$Transfer->close();
		if (!empty($Transfer->errors)){
			foreach($Transfer->errors as $value)
				$this->addReport($this->backup['action']['0'.$preset['id']], 'Launch Script failed. '.$value, 0);
			return false;
		}
		return true;
	}
	
	function getReport($preset){
		$Transfer = new Transfer();
		$Transfer->setopt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
		$Transfer->dest_path = $preset['path'];
		$SID = strtok($this->backup['action']['0'.$preset['id']]['result'], "\r\n");
		$SID = explode('=', $SID);
		$Transfer->file = 'get_session.php';
		$Transfer->variables = array($SID[0] => $SID[1],);
		$report = $Transfer->launchScript();
		eval('$array = '.$report.';');
		$Transfer->close();
		$key = '0'.$preset['id'];
		if(@eval('$this->backup[\'action\'][$key][\'report\'] = '.$report.';') === false)
			$this->addReport($this->backup['action']['0'.$preset['id']], 'No publish report was retrieved from remote server', 0);
		$backupid = 0;
		$actionid = 0;
		foreach($this->backup['action']['0'.$preset['id']]['report'] as $value){
			if(preg_match('/Backup remote ID \[[0-9]+\]/i', $value['message'])){
				preg_match('/[0-9]+/', $value['message'], $matches);
				$backupid = $matches[0];
				continue;
			}
			if(preg_match('/Action remote ID \[[0-9]+\]/i', $value['message'])){
				preg_match('/[0-9]+/', $value['message'], $matches);
				$actionid = $matches[0];
				continue;
			}
		}
		if (!empty($Transfer->errors)){
			foreach($Transfer->errors as $value)
				$this->backup['action']['0'.$preset['id']]['report'][] = array('datetime' => '', 'message' => 'Getting Report failed. '.$value, 6);
			return false;
		}
		return array('backupid' => $backupid, 'actionid' => $actionid,);
	}
	
	function putRemoteReport($preset, $info){
		if(empty($info['actionid'])) return false;
		$Database = new Database($preset['db_host'], $preset['db_name'], $preset['db_user'], $preset['db_password'], $preset['db_port']);
		if($Database->connect()){
			if(!$Database->update('backupaction', array('result' => $this->backup['action']['0'.$preset['id']]['result']), array('id' => $info['actionid'])))
				$this->backup['action']['0'.$preset['id']]['report'][] = array('datetime' => date('Y-m-d H:i:s', time()), 'message' => 'Couldn\'t put publish debug result to remote database', 'type' => 6);
			$Database->close();
		}
		else return false;
	}
	
	function clearAllTemplates(){
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'select_report_types');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'select_backups');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'open_backup');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'save_backup');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'delete_backup');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'select_report_receivers');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'delete_changed_url');
	}

}
?>