Skip to content

Instantly share code, notes, and snippets.

@Kirill89
Created November 28, 2012 06:26
Show Gist options
  • Save Kirill89/4159393 to your computer and use it in GitHub Desktop.
Save Kirill89/4159393 to your computer and use it in GitHub Desktop.
abstract class SocialAuth
{
protected $id, $key, $url, $token, $userId, $service;
public function setParams($id, $key, $url)
{
$this->id = $id;
$this->key = $key;
$this->url = $url;
}
public abstract function getAuthUrl();
public abstract function tryLogin($code);
public abstract function getUserInfo();
protected function getUrlJson($url, $method = 'get')
{
$response = json_decode($this->getUrl($url, $method), true);
return $response;
}
protected function getUrl($url, $method = 'get')
{
if($method == 'post')
{
$urlParts = explode('?', $url);
$ch = curl_init($urlParts['0']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $urlParts['1']);
}
else
{
$ch = curl_init($url);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
protected function getOutput($userId, $nickname, $birthday, $email, $firstName, $lastName, $sex, $photo)
{
return Array(
'identity' => $this->service.$userId,
'nickname' => $nickname,
'birthday' => $birthday,
'email' => $email,
'firstName' => $firstName,
'lastName' => $lastName,
'sex' => $sex,
'photo' => $photo
);
}
}
class SocialAuthVk extends SocialAuth
{
public function __construct()
{
$this->service = 'vk.com/';
}
public function getAuthUrl()
{
return "https://oauth.vk.com/oauth/authorize?redirect_uri={$this->url}&client_id={$this->id}";
}
public function tryLogin($code)
{
$url = "https://oauth.vk.com/access_token?client_id={$this->id}&redirect_uri={$this->url}&client_secret={$this->key}&code=".$code;
$data = $this->getUrlJson($url);
if(!isset($data['access_token']) || $data['access_token'] == '')
return false;
if(!isset($data['user_id']) || $data['user_id'] == '')
return false;
$this->token = $data['access_token'];
$this->userId = $data['user_id'];
return true;
}
public function getUserInfo()
{
$url = "https://api.vk.com/method/getProfiles?uid={$this->userId}&access_token={$this->token}&fields=uid,first_name,last_name,nickname,screen_name,sex,bdate,city,country,timezone,photo,photo_medium,photo_big,has_mobile,rate,contacts,education,online,counter";
$data = $this->getUrlJson($url);
if(!isset($data['response']['0']) || !isset($data['response']['0']['uid']))
return false;
if(!isset($data['response']['0']['screen_name']))$data['response']['0']['screen_name'] = '';
if(!isset($data['response']['0']['bdate']))$data['response']['0']['bdate'] = '';
if(!isset($data['response']['0']['first_name']))$data['response']['0']['first_name'] = '';
if(!isset($data['response']['0']['last_name']))$data['response']['0']['last_name'] = '';
if(!isset($data['response']['0']['sex']))$data['response']['0']['sex'] = '2';
if(!isset($data['response']['0']['photo_big']))$data['response']['0']['photo_big'] = '';
return $this->getOutput(
$data['response']['0']['uid'],
'',
$data['response']['0']['bdate'],
'',
$data['response']['0']['first_name'],
$data['response']['0']['last_name'],
$data['response']['0']['sex'] == '2' ? 'M' : 'F',
$data['response']['0']['photo_big']
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment