<?php
include '../../system/db.class.php';
if(!empty($_FILES)){
	$filepart = pathinfo($_FILES['file']['name']);
	$extension = strtolower($filepart['extension']);
	if($extension == 'zip'){
		$json = json_decode(stripslashes($_POST['post']), true);
		$time = $json['_time'];
		$aid = intval($json['_aid']);
		$apw = $json['_apw'];
		if(!getfield('admin', 'in_adminid', 'in_adminid', $aid) || md5(getfield('admin', 'in_adminpassword', 'in_adminid', $aid)) !== $apw){
			exit('-2');
		}
		PHP_OS == 'Linux' or exit('-3');
		extension_loaded('openssl') or exit('-4');
		function_exists('exec') or exit('-5');
		$path = '../../../data/tmp/'.$time.'/';
		creatdir($path);
		$dir = basename($_FILES['file']['name'], '.zip');
		$file = $path.$dir.'.zip';
		@move_uploaded_file($_FILES['file']['tmp_name'], $file);
		include_once '../zip/zip.php';
		$zip = new PclZip($file);
		if(($list = $zip->listContent()) == 0){
	 		exit($zip->errorInfo(true));
		}
		$zip->extract(PCLZIP_OPT_PATH, $path, PCLZIP_OPT_REPLACE_NEWER);
		is_file($path.'chain.crt') and is_file($path.'domain.crt') and is_file($path.'domain.key') or exit('-6');
		@rename($path.'chain.crt', '../../../data/cert/chain.crt');
		@rename($path.'domain.crt', '../../../data/cert/domain.crt');
		exec('openssl rsa -in '.$path.'domain.key -out ../../../data/cert/domain.key');
	 	echo '1';
	}else{
	 	echo '-1';
	}
}
?>