<?php

/*
* @package BANANA WARS
* @copyright: (c) 2017 G.P. aka Mvcshka (mvcshka@yandex.ru)
* @license: GNU General Public License version 2; see LICENSE.txt
* @files: /process/account.php
*
*/

session_start();
header('Content-type: application/json');
Header("Content-Type: text/html;charset=UTF-8");
if($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest' ) { exit();}
include($_SERVER['DOCUMENT_ROOT'].'/lib/connect.php');
include($_SERVER['DOCUMENT_ROOT'].'/lib/func.php');
include($_SERVER['DOCUMENT_ROOT'].'/lib/CofigFarm.php');
include($_SERVER['DOCUMENT_ROOT'].'/lib/cpayeer.php');


if(isset($_GET['action']))
{
    if($_GET['action'] == 'purse')
    {
        $purse = clean($_POST['purse']);
        if(GetUser("Purse", $_SESSION['id']) == '')
        {
            $accountNumber = $PurseAPIPayeer; 
            $apiId = $idAPIPayeer;
            $apiKey = $SecretKeyAPIPayeer;
            $payeer = new CPayeer($accountNumber, $apiId, $apiKey);
            if ($payeer->isAuth())
            {
                if($payeer->checkUser(array(
                    'user' => $purse,
                )))
                {
                    $q = $mysql->prepare("UPDATE db_users SET Purse = ? WHERE Uid = ?");
                    $q->execute(array($purse, $_SESSION['id']));
                    $tmp = 'Установка кошелька';
        $g = $mysql->prepare("INSERT INTO db_log SET UserId = ?, DateAdd = ?, `Text` = ?, MoneyIn = ?, MoneyOut = ?");
        $g->execute(array($_SESSION['id'], time(), $tmp, '-', '-'));
                    echo json_encode($result = array("purse .block" => "<span class=\"orange\">$purse</span>", "purse .error" => "")); exit;    
                }
                else
                {
                    echo json_encode($result = array("purse .error" => "Не верный кошелек")); exit;    
                }
            }
            else
            {
                echo json_encode($result = array("purse .error" => "Ошибка!!! Попробуйте позже!")); exit;    
            }
        }
        else
        {
            echo json_encode($result = array("purse .error" => "Кошелек был установлен ранее")); exit;
        }
        
    }
    
    if($_GET['action'] == 'newpass')
    {
        
        $old = mdHashPass($_POST['oldpass']);
        $new = mdHashPass($_POST['newpass']);
        $new1 = mdHashPass($_POST['newpassdubl']);
        
        if($old == GetUser("Password", $_SESSION['id']))
        {
            if($new == $new1)
            {
                $q = $mysql->prepare("UPDATE db_users SET Password = ? WHERE Uid = ?");
                $q->execute(array($new1, $_SESSION['id']));
                $tmp = 'Изменение пароля воина';
                $g = $mysql->prepare("INSERT INTO db_log SET UserId = ?, DateAdd = ?, `Text` = ?, MoneyIn = ?, MoneyOut = ?");
                $g->execute(array($_SESSION['id'], time(), $tmp, '-', '-'));
                
                echo json_encode($result = array("newpassErr" => "Пароль успешно изменен!")); exit;
            }
            else
            {
                echo json_encode($result = array("newpassErr" => "Новые пароли не совпадают!")); exit;
            }
        }
        else
        {
            echo json_encode($result = array("newpassErr" => "Старый пароль не верный!")); exit;
        }
    }
}
?>