Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // <?php
- $router->defaultRoute = 'error_404';
- function login() {
- global $db;
- if (isset($_GET['code'])) {
- @$auth = json_decode(file_get_contents('https://oauth.vk.com/access_token?client_id=' . APP_ID . '&client_secret=' . APP_SECRET . '&code=' . $_GET['code'] . '&redirect_uri=' . REDIRECT_URI));
- if (is_object($auth) && isset($auth->access_token)) {
- @$users = json_decode(file_get_contents('https://api.vk.com/method/users.get?uids=' . $auth->user_id . '&fields=sex,photo_50'));
- if (is_object($users) && isset($users->response)) {
- $user = $users->response[0];
- $data = (array) $user;
- $data['auth_hash'] = md5($auth->access_token);
- $id = $db->single('select id from ?_users where uid=' . $user->uid);
- if ($id) {
- $ok = $db->update('users', $data, $id);
- }
- else {
- $id = $db->insert('users', $data);
- if ($id) $ok = 1;
- }
- if ($ok) {
- $ts = time() + 60 * 60 * 24 * 30;
- setcookie('id', $id, $ts);
- setcookie('auth_hash', $data['auth_hash'], $ts);
- $_SESSION['user'] = array();
- foreach ($user as $k => $v) {
- $_SESSION['user'][$k] = $v;
- }
- return header('Location: ./');
- }
- }
- }
- }
- }
- $router->get('auth', 'login');
- function logout() {
- unset($_SESSION['user']);
- setcookie('id', '', 0);
- setcookie('hash', '', 0);
- header('Location: ./');
- }
- $router->get('exit', 'logout');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement