<?php
if (isset($_COOKIE['uid']) && isset($_COOKIE['gender']) && !isset($_COOKIE['login']) && !isset($_COOKIE['password'])) {
    $uid = intval($_COOKIE['uid']);
    if (mysql_result(mysql_query("SELECT COUNT(*) FROM `users` WHERE `uid_ok` = '" . $uid . "'"), 0) == false) {
        $pass = mt_rand(123456, 1234567890);
        $sex = 'w';
        if ($_COOKIE['gender'] == 'male') {
            $sex = 'm';
        }
        mysql_query("INSERT INTO `users` SET `login` = '" . $uid . "', `password` = '" . md5($pass) . "', `sex` = '" . $sex . "', `uid_ok` = '" . $uid . "',  `side` = 'good', `duel_fight` = '20' ,`save` = '0', `almaz` = '100', `registr` = '" . time() . "'");


        if (isset($_SESSION['ref'])) {
            mysql_query("UPDATE `users` SET `id_partner` = '" . intval($_SESSION['ref']) . "' WHERE (`uid_ok` = '" . $uid . "')");
        }

        setcookie('login', $uid, time() + 86400 * 365, '/');
        setcookie('password', md5($pass), time() + 86400 * 365, '/');

    } else {
        $userUid = mysql_fetch_assoc(mysql_query("SELECT `login`, `password` FROM `users` WHERE `uid_ok` = '" . $uid . "' LIMIT 1"));

        setcookie('login', $userUid['login'], time() + 86400 * 365, '/');
        setcookie('password', $userUid['password'], time() + 86400 * 365, '/');

    }
    setcookie('uid', '', time() - 365, '/');
    setcookie('gender', '', time() - 365, '/');
    header('location: /');
    exit();
}
?>

<html>
<head>
    <meta charset="UTF-8">
    <title>OKSDK - REST notifications.sendSimple (nosession)</title>

    <script type="text/javascript" src="oksdk.js"></script>
</head>
<body>
<script type="text/javascript">
    document.addEventListener('DOMContentLoaded', function () {
        var config = {
            app_id: 1261984000,      // <-- insert APP ID here
            app_key: 'CBAKFJDMEBABABABA'     // <-- insert APP PUBLIC KEY here
        };
        OKSDK.init(config, function () {

            OKSDK.REST.call('users.getCurrentUser', null, function (status, data, error) {
                if (status === 'ok') {
                    //document.getElementById('content').innerHTML = 'Hello World and hi, ' + data.name + '.';
                    document.cookie = "uid=" + data.uid;
                    document.cookie = "gender=" + data.gender;
                } else {
                    alert('Unable to retrieve current user ' + OKSDK.Util.toString(error));
                }
            });
        }, function (error) {
            alert('OKSDK error' + OKSDK.Util.toString(error));
        })
    });
</script>

</body>
</html>