<?php
require __DIR__.'/settings.php';

ini_set('display_errors'1);
ini_set('display_startup_errors'1);
error_reporting(E_ALL);

function 
get_client_ip() {
    
$ipaddress '';
    if (isset(
$_SERVER['HTTP_CLIENT_IP']))
        
$ipaddress $_SERVER['HTTP_CLIENT_IP'];
    else if(isset(
$_SERVER['HTTP_X_FORWARDED_FOR']))
        
$ipaddress $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if(isset(
$_SERVER['HTTP_X_FORWARDED']))
        
$ipaddress $_SERVER['HTTP_X_FORWARDED'];
    else if(isset(
$_SERVER['HTTP_FORWARDED_FOR']))
        
$ipaddress $_SERVER['HTTP_FORWARDED_FOR'];
    else if(isset(
$_SERVER['HTTP_FORWARDED']))
        
$ipaddress $_SERVER['HTTP_FORWARDED'];
    else if(isset(
$_SERVER['REMOTE_ADDR']))
        
$ipaddress $_SERVER['REMOTE_ADDR'];
    else
        
$ipaddress 'UNKNOWN';
    return 
$ipaddress;
    
}

$g_iClientID = isset($_GET['id']) ? $_GET['id'] : 0;
$g_iClientSteamID = isset($_GET['steamid']) ? $_GET['steamid'] : 0;

?>
<!DOCTYPE HTML>
<html lang="pl">
<!-- HEAD -->
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=0.41, maximum-scale=1" />
    <meta name="description" content="<?php echo $settings['website_description']; ?>" />
    <link rel="icon" href="img/web_icon.png">
    <meta name="keywords" content="<?php echo $settings['website_description']; ?>" />
    <title><?php echo $settings['website_title']; ?></title>
    <!-- INCLUDE -->
    <link rel="stylesheet" href="css/main9.css" type="text/css" />
</head>
<body>
<?php

require __DIR__ '/SourceQuery/bootstrap.php';
use 
xPaw\SourceQuery\SourceQuery;

function 
updateClient($server$steamid32$spray_path) {
    require 
__DIR__.'/settings.php';

    
$Query = new SourceQuery( );
    try {
        
$Query->Connect($RCON_CONNECT[$server]['ipv4'], $RCON_CONNECT[$server]['port'], 2SourceQuery::SOURCE);
        
        
$Query->SetRconPassword($RCON_CONNECT[$server]['pass']);
        
$Query->Rcon('sm_updatespray "'.$steamid32.'" "'.$spray_path.'"');
    } catch( 
Exception $e ) {
        echo 
$e->getMessage();
    } finally {
        
$Query->Disconnect();
    }
}

$MySQL = new PDO('mysql:host='.$settings['mysql_host'].'; dbname='.$settings['mysql_database'].'; charset=utf8;',  $settings['mysql_user'],  $settings['mysql_password']);

$MySQL->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_SILENT);
$MySQL->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODEPDO::FETCH_ASSOC);

$Query $MySQL->prepare("SELECT * FROM sprays_auths WHERE addtime = :one");
$Query->bindValue(":one"$g_iClientID);
$Query->execute();

if (
$Query->rowCount() != 1) {
    echo 
'<div id="warrning"><div class="text">Niepoprawny <span style="color: #FFEF00 !important;">klucz sesji</span> - spróbuj ponownie!</a></div></div>';
    return;
} else {
    
$r_sAuthInfo $Query->fetch();
    
    if (
get_client_ip() != $r_sAuthInfo['ipv4']) {
        echo 
'<div id="warrning"><div class="text">Niepoprawny <span style="color: #FFEF00 !important;">adres ip sesji</span> - spróbuj ponownie!</a></div></div>';
        return;
    }
    
    if (
$r_sAuthInfo['steamid'] != $g_iClientSteamID) {
        echo 
'<div id="warrning"><div class="text">Niepoprawne <span style="color: #FFEF00 !important;">steamid sesji</span> - spróbuj ponownie!</a></div></div>';
        return;
    }
    
    if (isset(
$_GET['set'])) {
        
$g_sSpray $_GET['set'];
        
        
$Query $MySQL->prepare("SELECT * FROM sprays WHERE spray = = :one");
        
$Query->bindValue(":one"$g_sSpray);
        
$Query->execute();
        
        if (
$Query->rowCount() == 0) {
            echo 
'<div id="warrning"><div class="text">Spray jest <span style="color: #FFEF00 !important;">niepoprawny</span> - spróbuj ponownie!</a></div></div>';
            return;
        }
        
        
$r_vipspray $Query->fetch();
        if (!empty(
$r_vipspray['flags'])) {
            if (
strpos($r_sAuthInfo['flags'], $r_vipspray['flags']) === false) {
                echo 
'<div id="warrning"><div class="text">Nie posiadasz <span style="color: #FFEF00 !important;">Vip</span>\'a! <a class="text-link" href="'.$settings['website_vip_url'].'">Kliknij <span style="color: #FFEF00 !important;">tutaj</span> aby go zakupić.</a></div></div>';
                return;
            }
        }
        
        
/* Upadateinfo */
        
updateClient($r_sAuthInfo['sv'], $g_iClientSteamID$g_sSpray);
        
        
header("location: success.php");
        return;
    }
    
}

$Query $MySQL->query("SELECT * FROM sprays_overlays");

$ar_overlays = Array();
$ar_overlays_count 0;
while(
$r_overlays $Query->fetch()) {
    
$ar_overlays[$ar_overlays_count]['src'] = $r_overlays['img'];
    
$ar_overlays[$ar_overlays_count]['flags'] = $r_overlays['flags'];
    
$ar_overlays_count++;
}
?>

<div id="content">
    <div id="title">
        <div class="text">Wybierz swoje Graffitii</div>
    </div>
    <div id="box">
    
        <?php
            
            $Query 
$MySQL->query("SELECT * FROM sprays");
            
            while (
$r_sprays $Query->fetch()) {
                if (
$r_sprays['isDisabled'] == 0) {
                    echo 
'
                    <div id="plugin-box" onclick=\'location.href="index.php?id='
.$g_iClientID.'&steamid='.$g_iClientSteamID.'&set='.$r_sprays['spray'].'"\'>
                        <div id="img">
                            <img class="img" src="img/'
.$r_sprays['spray'].'.png">
                    '
;
                    if (!empty(
$r_sprays['flags'])) {
                        for (
$i 0$i $ar_overlays_count$i++) {
                            if (
strpos($r_sprays['flags'], $ar_overlays[$i]['flags']) !== false) {
                                echo 
'<img class="img-override" src="'.$ar_overlays[$i]['src'].'">';
                            }
                        }
                    }
                    echo 
'
                        </div>
                        <div id="name">
                            <div class="'
;
                            if(
strlen($r_sprays['spray_name']) >= 14) {
                                echo 
'text2';
                            } else {
                                echo 
'text1';
                            }
                            echo 
'">'.$r_sprays['spray_name'].'</div>
                        </div>
                    </div>
                    '
;
                }
            }
        
        
?>
        
    </div>
</div>

</body>
</html>