Advertisement
stuppid_bot

Untitled

Jun 29th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. // <?php
  2.  
  3. $router->defaultRoute = 'error_404';
  4.  
  5. function login() {
  6.     global $db;
  7.     if (isset($_GET['code'])) {
  8.         @$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));      
  9.         if (is_object($auth) && isset($auth->access_token)) {
  10.             @$users = json_decode(file_get_contents('https://api.vk.com/method/users.get?uids=' . $auth->user_id . '&fields=sex,photo_50'));
  11.             if (is_object($users) && isset($users->response)) {              
  12.                 $user = $users->response[0];                              
  13.                 $data = (array) $user;
  14.                 $data['auth_hash'] = md5($auth->access_token);
  15.                 $id = $db->single('select id from ?_users where uid=' . $user->uid);
  16.                 if ($id) {
  17.                     $ok = $db->update('users', $data, $id);
  18.                 }
  19.                 else {                  
  20.                     $id = $db->insert('users', $data);
  21.                     if ($id) $ok = 1;
  22.                 }
  23.                 if ($ok) {
  24.                     $ts = time() + 60 * 60 * 24 * 30;
  25.                     setcookie('id', $id, $ts);
  26.                     setcookie('auth_hash', $data['auth_hash'], $ts);
  27.                     $_SESSION['user'] = array();
  28.                     foreach ($user as $k => $v) {
  29.                         $_SESSION['user'][$k] = $v;
  30.                     }
  31.                     return header('Location: ./');
  32.                 }
  33.             }
  34.         }
  35.     }
  36. }
  37.  
  38. $router->get('auth', 'login');
  39.  
  40. function logout() {
  41.     unset($_SESSION['user']);
  42.     setcookie('id', '', 0);
  43.     setcookie('hash', '', 0);
  44.     header('Location: ./');
  45. }
  46.  
  47. $router->get('exit', 'logout');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement