<?php
$t = 'Редактор серверов';
require_once '../../wcore/core.php';
require_once '../../wcore/head.php';
iank(3);
$id = (isset($_GET['id']) && !empty($_GET['id'])?intval($_GET['id']):0);
$act = (isset($_GET['act']) && !empty($_GET['act'])?antixs($_GET['act']):'home');

$type_name = array(
        1=>$lang['ord_t_vhost'],
        2=>$lang['ord_t_ress'],
        3=>$lang['ord_t_vds'],
        4=>$lang['ord_t_dedic'],
        5=>$lang['ord_t_domain'],
);

$tlist[] = array('id'=>1,'name'=>$type_name[1]);
$tlist[] = array('id'=>2,'name'=>$type_name[2]);
$tlist[] = array('id'=>3,'name'=>$type_name[3]);
$tlist[] = array('id'=>4,'name'=>$type_name[4]);
$tlist[] = array('id'=>5,'name'=>$type_name[5]);

if ($act == 'info' && $id != 0){
    $sql = mysqli_query($mysqli,"SELECT * FROM `servers` WHERE `id` = '$id' LIMIT 1");
    if (mysqli_num_rows($sql) == 0){echo msg_err('err',$lang['amd_none_search']);require_once '../../wcore/foot.php';exit();}
    $data = mysqli_fetch_object($sql);
    


    $array = array(
        'id'        =>$data->id,
        'name'      =>$data->name,
        'order_init'=>$data->order_init,
        'type'      =>$data->type,
        'type_n'    =>$type_name[$data->type],
        'url'       =>$data->url,
        'ip'        =>$data->ip,
        'login'     =>$data->login,
        'pwd'       =>base64_decode($data->pwd),
        'notify'    =>json_decode(base64_decode($data->notify)),
        'texzone'   =>$data->texzone,
        'count_orders'=>mysqli_num_rows(mysqli_query($mysqli,"SELECT * FROM `orders` WHERE `l_s` = '$id'")),
        'count_tarifs'=>mysqli_num_rows(mysqli_query($mysqli,"SELECT * FROM `tarifs` WHERE `sid` = '$id'")),
    );
	
} elseif ($act == 'check' && $id != 0){
    $sql = mysqli_query($mysqli,"SELECT * FROM `servers` WHERE `id` = '$id' LIMIT 1");
    if (mysqli_num_rows($sql) == 0){echo msg_err('err',$lang['amd_none_search']);require_once '../../wcore/foot.php';exit();}
    $data = mysqli_fetch_object($sql);
    if (!file_exists(WCORE_ROOT.'/wcore/order_'.$data->order_init.'.php')){echo msg_err('err',$lang['ord_t_err_module']);require_once '../../wcore/foot.php';exit();}
    require_once '../../wcore/order_'.$data->order_init.'.php';
    if (!function_exists('order_'.$data->order_init)) {echo msg_err('err',$lang['ord_t_err_module_s2']);require_once '../../wcore/foot.php';exit();}
    $OI = 'order_'.$data->order_init;
    $isp = json_decode($OI('info',$id));
    //var_dump($isp);
    if ($isp->status == 'ok'){
        logFile('[CheckConnect] ['.$data->order_init.'] [SERVER:'.$data->id.'] [OK] Успешная авторизация с уровнем доступа '.$isp->level.'.');
        echo msg_err('suc',$lang['adm_serv_check_1'].$lang['adm_serv_check_1_1'].':'.$isp->level);require_once '../../wcore/foot.php';exit();
    } elseif ($isp->status == 'warn') {
        logFile('[CheckConnect] ['.$data->order_init.'] [SERVER:'.$data->id.'] [WARN] [Code: '.$isp->err.'] '.$isp->errmsg.' ');
        echo msg_err('warn',$lang['adm_serv_check_2']);require_once '../../wcore/foot.php';exit();
    } else {
        logFile('[CheckConnect] ['.$data->order_init.'] [SERVER:'.$data->id.'] [ERR] [Code: '.$isp->err.'] '.$isp->errmsg.' ');
        echo msg_err('err',$lang['adm_serv_check_3']);require_once '../../wcore/foot.php';exit();
    }
} elseif ($act == 'create' && $id == 0){
    if (isset($_POST['ok'])){
		 if (empty($_SESSION['csrf_token']) || $_SESSION['csrf_token'] != antixs($_POST['csrf'])){echo msg_err('err',$lang['csrf']);require_once '../../wcore/foot.php';exit();}
		 if (empty($_POST['name'])){echo msg_err('err',$lang['adm_serv_c_err_1']);require_once '../../wcore/foot.php';exit();}
		 if (empty($_POST['type'])){echo msg_err('err',$lang['adm_serv_c_err_2']);require_once '../../wcore/foot.php';exit();}
		 if (empty($_POST['order_init'])){echo msg_err('err',$lang['adm_serv_c_err_3']);require_once '../../wcore/foot.php';exit();}
		 $name = antixs($_POST['name']);
		 $type = antixs($_POST['type']);
		 $order_init = antixs($_POST['order_init']);
		 $url = antixs($_POST['url']);
		 $login = antixs($_POST['login']);
		 $pwd = base64_encode(antixs($_POST['pwd']));
		 $ip = antixs($_POST['ip']);
		 $texzone = antixs($_POST['texzone']);
		 mysqli_query($mysqli,"INSERT INTO `servers`(`name`, `order_init`, `type`, `url`, `ip`, `login`, `pwd`, `texzone`) VALUES ('$name','$order_init','$type','$url','$ip','$login','$pwd','$texzone')");
		 echo msg_err('suc',$lang['adm_serv_added']);
		 require_once '../../wcore/foot.php';
		 exit();
	}
} elseif ($act == 'edit' && $id != 0){
    $sql = mysqli_query($mysqli,"SELECT * FROM `servers` WHERE `id` = '$id' LIMIT 1");
    if (mysqli_num_rows($sql) == 0){echo msg_err('err',$lang['amd_none_search']);require_once '../../wcore/foot.php';exit();}
    $array = mysqli_fetch_object($sql);
    $array->pwd = base64_decode($array->pwd);
	if (isset($_POST['ok'])){
		 if (empty($_SESSION['csrf_token']) || $_SESSION['csrf_token'] != antixs($_POST['csrf'])){echo msg_err('err',$lang['csrf']);require_once '../../wcore/foot.php';exit();}
		 if (empty($_POST['name'])){echo msg_err('err',$lang['adm_serv_c_err_1']);require_once '../../wcore/foot.php';exit();}
		 if (empty($_POST['type'])){echo msg_err('err',$lang['adm_serv_c_err_2']);require_once '../../wcore/foot.php';exit();}
		 if (empty($_POST['order_init'])){echo msg_err('err',$lang['adm_serv_c_err_3']);require_once '../../wcore/foot.php';exit();}
		 $name = antixs($_POST['name']);
		 $type = antixs($_POST['type']);
		 $order_init = antixs($_POST['order_init']);
		 $url = antixs($_POST['url']);
		 $login = antixs($_POST['login']);
		 $pwd = base64_encode(antixs($_POST['pwd']));
		 $ip = antixs($_POST['ip']);
		 $texzone = antixs($_POST['texzone']);
		 mysqli_query("UPDATE `servers` SET `name` = '$name', `type` = '$type', `order_init` = '$order_init', `url` = '$url', `ip` = '$ip', `login` = '$login', `pwd` = '$pwd', `texzone` = '$texzone' WHERE `id` = '$id'");
		 echo msg_err('suc',$lang['adm_serv_added']);
		 require_once '../../wcore/foot.php';
		 exit();
	}
} elseif ($act == 'delete' && $id != 0){
    $sql = mysqli_query($mysqli,"SELECT * FROM `servers` WHERE `id` = '$id' LIMIT 1");
    if (mysqli_num_rows($sql) == 0){echo msg_err('err',$lang['amd_none_search']);require_once '../../wcore/foot.php';exit();}
    $array = mysqli_fetch_object($sql);
	if (isset($_POST['ok'])){
		mysqli_query($mysqli,"DELETE FROM `servers` WHERE `id` = '$id'");
		echo msg_err('suc',$lang['adm_serv_del_ok']);
		require_once '../../wcore/foot.php';
		exit();
	}
} else {
    $_tmp = mysqli_query($mysqli,"SELECT * FROM `servers` ORDER BY `id` ASC") or die("Ошибка запроса: ".mysqli_error($mysqli));
    while ($serv = mysqli_fetch_assoc($_tmp)){
        $array[] = $serv;
    }
}
if (!isset($array)){$array = FALSE;}
echo $twig->render('admin_servers.tpl', array(
    'data' => $array,
    'lang' =>$lang,
    'act'=>$act,
	'type_list'=>$tlist,
	'crfs'=>_csrf()));
require_once '../../wcore/foot.php';