<?php
/**
** API GAMESBW
*/
class APIGames
{
private $key = '';
private $pid = 1;
private $debug = false;
private $srv = 'http://gamesbw.ru';
private $db = array(
'dbname' => '',
'dbhost' => '',
'dbuser' => '',
'dbpass' => ''
);
function __construct()
{
try {
$this->db = new PDO('mysql:dbname=' . $this->db['dbname'] . ';host=' . $this->db['dbhost'], $this->db['dbuser'], $this->db['dbpass']);
} catch (PDOException $e) {
die($e->getMessage());
}
}
function connect($method, $key, $other = '')
{
$curl = curl_init($this->srv . '/games/APIGames.php');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'key=' . $key . '&pid=' . $this->pid . '&method=' . $method . '&' . $other);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$q = curl_exec($curl);
if ($this->debug) var_dump($q);
return json_decode($q);
curl_close($curl);
}
function query($method, $other = '')
{
$key = sha1($this->key . $this->pid . $method);
return $this->connect($method, $key, $other);
}
function checkData($hash, $method)
{
$key = sha1($this->key . $this->pid . $method);
return $hash == $key;
}
function initPayment($money, $amount, $user)
{
$q = $this->query('initPay', 'money=' . $money . '&amount=' . $amount);
if (strlen($q) > 15) {
$j = $this->db->prepare('insert into gp(`uid`,`sid`,`amount`,`money`,`date`,`status`)values(?,?,?,?,?,?)');
$j->execute(array($user, $q, $amount, $money, time(), 'process'));
header('location: ' . $this->srv . '/games/UserPay.php?payID=' . $q);
exit;
}
return $q;
}
function checkPay()
{
if (isset($_GET['payID']) and isset($_GET['key'])) {
$q = $this->db->prepare('select * from gp WHERE sid=?');
$q->execute(array($_GET['payID']));
$item = $q->fetch();
$hash = sha1($this->key . $this->pid . $item['money'] . $item['amount'] . $_GET['payID']);
if ($hash == $_GET['key'] and $item['status'] == 'process') {
$q = $this->db->prepare('update gp set status="end" WHERE sid=?');
$q->execute(array($_GET['payID']));
return true;
} else return false;
} else return false;
}
function goodPay()
{
//Реализация успешной оплаты
$q=$this->db->prepare('select * from gp where sid=?');
$q->execute(array($_GET['payID']));
$item=$q->fetch();
$q=$this->db->prepare('update gp set `status`="end" where sid=?');
$q->execute(array($item['sid']));
$q=$this->db-> prepare('update users set `g` = `g` + ? where id = ?');
$q->execute(array((int)$item['amount'],$item['uid']));
}
function userAuth($arr){
//Реализация авторизации
$q=$this->db->prepare('select `id`,`password` from users where usg=?');
$q->execute(array($arr->id));
$item=$q->fetch();
if(empty($item['id'])){
$password=sha1(microtime());
$q=$this->db->prepare('insert into users (`login`,`sex`,`usg`,`password`)values(?,?,?,?)');
$q->execute(array($arr->login.time(),$arr->sex,$arr->id,$password));
setCookie('id', $this->db->lastInsertId(), time() + 86400, '/');
setCookie('password', $password, time() + 86400, '/');
}else{
setCookie('id', $item['id'], time() + 86400, '/');
setCookie('password', $item['password'], time() + 86400, '/');
}
header('location: /');
}
}