<?php
$t = 'Подтверждение заказа услуги';
require_once '../../wcore/core.php';
require_once '../../wcore/head.php';
iank();
$id = (isset($_GET['id']) && !empty($_GET['id'])?intval($_GET['id']):0);
$sql = mysqli_query($mysqli,"SELECT * FROM `servers`,`tarifs` WHERE `servers`.`id`=`tarifs`.`sid` AND `tarifs`.`tid` = '$id' LIMIT 1");
if (mysqli_num_rows($sql) == 0){echo msg_err('err',$lang['ord_t_na']);require_once '../../wcore/foot.php';exit();}
$tarifs = mysqli_fetch_object($sql);
if (file_exists(WCORE_ROOT.'/modules/order/buy_'.$tarifs->order_init.'.php')){
    go('/modules/order/buy_'.$tarifs->order_init.'.php?id='.$id);
}
if ($tarifs->type == 1){
    $_tarifs_type_list = 'vhost';
} elseif ($tarifs->type == 2){
    $_tarifs_type_list = 'ress';
} elseif ($tarifs->type == 3){
    $_tarifs_type_list = 'vds';
} elseif ($tarifs->type == 4){
    $_tarifs_type_list = 'dedic';
} elseif ($tarifs->type == 5){
    $_tarifs_type_list = 'domain';
} else {
    $_tarifs_type_list = 'none';
}
if ((isset($_POST['ok_combo']) && $tarifs->eternal_service == 1 && $tarifs->block_a_monthly_payment == 0 && $_POST['combo'] == 1) || (isset($_POST['ok']) && $tarifs->eternal_service == 1 && $tarifs->block_a_monthly_payment == 1)){
    //echo "ED\n";
    if ($_SESSION['csrf_token'] != antixs($_POST['csrf'])){echo msg_err('err',$lang['csrf']);require_once '../../wcore/foot.php';exit();}
        $_bits_pay = $tarifs->bits_eternal;
        if ($_bits_pay > $ank->bits){
            echo msg_err('err',$lang['ord_t_err_bits']);
            require_once '../../wcore/foot.php';
            exit();
        }
        if (!file_exists(WCORE_ROOT.'/wcore/order_'.$tarifs->order_init.'.php')){echo msg_err('err',$lang['ord_t_err_module']);require_once '../../wcore/foot.php';exit();}
        require_once '../../wcore/order_'.$tarifs->order_init.'.php';
        if (!function_exists('order_'.$tarifs->order_init)) {echo msg_err('err',$lang['ord_t_err_module_s2']);require_once '../../wcore/foot.php';exit();}
        $OI = 'order_'.$tarifs->order_init;
        $created_isp_login_new = substr($ank->login, 0, 5);
        $created_isp_login_new = $created_isp_login_new.rand(1000,99999);
        $jcmd = json_decode($OI(($_tarifs_type_list == 'vhost' || $_tarifs_type_list == 'ress'?'rop':'open'),$tarifs->id,$tarifs->preset,$_tarifs_type_list,$created_isp_login_new), true);
        if ($jcmd['status'] == 'err'){
            $syserr = (isset($jcmd['syserr'])?$jcmd['syserr']:false);
            logFile('[ORDER] ['.$tarifs->order_init.'] [SERVER:'.$tarifs->id.'] [ERR] [Code: '.$jcmd['err'].'] '.$jcmd['errmsg'].' '.(isset($jcmd['syserr'])?'{Return: '.$syserr.'}':''));
            echo msg_err('err',$lang['ord_t_err_module_s3']);require_once '../../wcore/foot.php';exit();
        } elseif ($jcmd['status'] == 'warn'){
            logFile('[ORDER] ['.$tarifs->order_init.'] [SERVER:'.$tarifs->id.'] [WARN] [Code: '.$jcmd['err'].'] '.$jcmd['errmsg'].' '.(isset($jcmd['syserr'])?'{Return: '.$syserr.'}':''));
            $data = base64_encode(json_encode(array(
    	        'url' 		=> 	$tarifs->url,
    	        'login' 	=> 	'',
    	        'pwd' 		=> 	'',
    	        'ip' 		=> 	$tarifs->ip,
    	        'packname'  =>  $tarifs->preset,
    	        'tarifs_id' =>	$tarifs->tid,
    	        'server_id' =>	$tarifs->sid,
    	        'order_init'=>  $tarifs->order_init,
            )));
            $motime = time()+9999999999; # 1 месяц
            mysqli_query($mysqli,"INSERT INTO `orders`(`uid`, `nametar`, `data`, `status`, `time`, `bits`,`l_s`) VALUES ('$ank->id','$tarifs->namepack','$data','1','$motime','$_bits_pay','$tarifs->sid')")or die("Ошибка запроса: ".mysqli_error($mysqli));
            mysqli_query($mysqli,"UPDATE `users` SET `bits`=`bits`-'$_bits_pay' WHERE `id` = '$ank->id'") or die("Ошибка запроса: ".mysqli_error($mysqli));
    
            echo msg_err('warn',$lang['ord_t_err_module_s4']);require_once '../../wcore/foot.php';exit();
        }
} elseif ((isset($_POST['ok'])) || (isset($_POST['ok_combo']) && $tarifs->eternal_service == 1 && $tarifs->block_a_monthly_payment == 0 && $_POST['combo'] == 0)){
    //echo "EX\n";
    if ($_SESSION['csrf_token'] != antixs($_POST['csrf'])){echo msg_err('err',$lang['csrf']);require_once '../../wcore/foot.php';exit();}
    if ($tarifs->bits > $ank->bits){echo msg_err('err',$lang['ord_t_err_bits']);require_once '../../wcore/foot.php';exit();}
    if (!file_exists(WCORE_ROOT.'/wcore/order_'.$tarifs->order_init.'.php')){echo msg_err('err',$lang['ord_t_err_module']);require_once '../../wcore/foot.php';exit();}
    require_once '../../wcore/order_'.$tarifs->order_init.'.php';
    if (!function_exists('order_'.$tarifs->order_init)) {echo msg_err('err',$lang['ord_t_err_module_s2']);require_once '../../wcore/foot.php';exit();}
    $OI = 'order_'.$tarifs->order_init;
    $created_isp_login_new = substr($ank->login, 0, 5);
    $created_isp_login_new = $created_isp_login_new.rand(1000,99999);
    $jcmd = json_decode($OI(($_tarifs_type_list == 'vhost' || $_tarifs_type_list == 'ress'?'rop':'open'),$tarifs->id,$tarifs->preset,$_tarifs_type_list,$created_isp_login_new), true);
    if ($jcmd['status'] == 'err'){
        $syserr = (isset($jcmd['syserr'])?$jcmd['syserr']:false);
        logFile('[ORDER] ['.$tarifs->order_init.'] [SERVER:'.$tarifs->id.'] [ERR] [Code: '.$jcmd['err'].'] '.$jcmd['errmsg'].' '.(isset($jcmd['syserr'])?'{Return: '.$syserr.'}':''));
        echo msg_err('err',$lang['ord_t_err_module_s3']);require_once '../../wcore/foot.php';exit();
    } elseif ($jcmd['status'] == 'warn'){
        logFile('[ORDER] ['.$tarifs->order_init.'] [SERVER:'.$tarifs->id.'] [WARN] [Code: '.$jcmd['err'].'] '.$jcmd['errmsg'].' '.(isset($jcmd['syserr'])?'{Return: '.$syserr.'}':''));
        $data = base64_encode(json_encode(array(
	        'url' 		=> 	$tarifs->url,
	        'login' 	=> 	'',
	        'pwd' 		=> 	'',
	        'ip' 		=> 	$tarifs->ip,
	        'packname'  =>  $tarifs->preset,
	        'tarifs_id' =>	$tarifs->tid,
	        'server_id' =>	$tarifs->sid,
	        'order_init'=>  $tarifs->order_init,
        )));
        $motime = time()+2592000; # 1 месяц
        mysqli_query($mysqli,"INSERT INTO `orders`(`uid`, `nametar`, `data`, `status`, `time`, `bits`,`l_s`) VALUES ('$ank->id','$tarifs->namepack','$data','1','$motime','$tarifs->bits','$tarifs->sid')")or die("Ошибка запроса: ".mysqli_error($mysqli));
        mysqli_query($mysqli,"UPDATE `users` SET `bits`=`bits`-'$tarifs->bits' WHERE `id` = '$ank->id'") or die("Ошибка запроса: ".mysqli_error($mysqli));

        echo msg_err('warn',$lang['ord_t_err_module_s4']);require_once '../../wcore/foot.php';exit();
    } else {
        logFile('[ORDER] ['.$tarifs->order_init.'] [SERVER:'.$tarifs->id.'] [OK] Создан пользователь '.$jcmd['login'].' на '.$jcmd['url'].' ');
        $data = base64_encode(json_encode(array(
	        'url' 		=> 	$jcmd['url'],
	        'login' 	=> 	$jcmd['login'],
	        'pwd' 		=> 	$jcmd['passwd'],
	        'ip' 		=> 	$jcmd['ip'],
	        'packname'  =>  $tarifs->preset,
	        'tarifs_id' =>	$tarifs->tid,
	        'server_id' =>	$tarifs->sid,
	        'order_init'=>  $tarifs->order_init,
        )));
        $motime = time()+2592000; # 1 месяц
        mysqli_query($mysqli,"INSERT INTO `orders`(`uid`, `nametar`, `data`, `status`, `time`, `bits`,`l_s`) VALUES ('$ank->id','$tarifs->namepack','$data','2','$motime','$tarifs->bits','$tarifs->sid')")or die("Ошибка запроса: ".mysqli_error($mysqli));
        mysqli_query($mysqli,"UPDATE `users` SET `bits`=`bits`-'$tarifs->bits' WHERE `id` = '$ank->id'") or die("Ошибка запроса: ".mysqli_error($mysqli));

        echo msg_err('suc',$lang['ord_t_suc_buy']);require_once '../../wcore/foot.php';exit();
    }
} else {
    echo $twig->render('page_modules_order_buy.tpl', array(
    'lang'  =>  $lang,
    'id'    =>  $id,
    'tarifs'=>  $tarifs,
    'desc'  =>  json_decode(base64_decode($tarifs->deskpack)),
    'csrf'=>_csrf(),
    )
    );
}
require_once '../../wcore/foot.php';
?>