<commit_before><?php
/**
* @package ivacuum.ru
* @copyright (c) 2013
*/

namespace app\ucp\oauth;

use app\models\page;
use Guzzle\Http\Client as http_client;
use Guzzle\Http\Exception\ClientErrorResponseException;

class vk extends page
{
	public function index()
	{
		$url = 'https://oauth.vk.com/authorize?' . http_build_query([
			'client_id'     => $this->config['oauth.vk.app_id'],
			'scope'         => 'friends',
			'redirect_uri'  => 'http://' . $this->request->server_name . ilink($this->get_handler_url('callback')),
			'response_type' => 'code',
		]);
			
		$this->request->redirect($url);
	}
	
	public function callback()
	{
		$code = $this->request->get('code', '');

		$client = new http_client('https://oauth.vk.com');
		
		try
		{
			$response = $client->get(['access_token{?client_id,client_secret,code,redirect_uri}', [
				'client_id'     => $this->config['oauth.vk.app_id'],
				'client_secret' => $this->config['oauth.vk.app_secret'],
				'code'          => $code,
				'redirect_uri'  => 'http://' . $this->request->server_name . ilink($this->get_handler_url('callback')),
			]])->send()->json();
		}
		catch (ClientErrorResponseException $e)
		{
			$this->profiler->log($e->getMessage());
			return;
		}
		
		if (isset($response['error']))
		{
			trigger_error($response['error_description']);
		}
		
		$this->profiler->log($response);

		extract($response);

		$client->setBaseUrl('https://api.vk.com/method');
		
		$response = $client->get(['users.get{?uids,fields}', [
			'uids'   => $user_id,
			'fields' => 'sex, bdate, photo_big',
		]])->send()->json();
		
		$this->profiler->log($response);
		
		$response = $client->get(['friends.getAppUsers{?access_token}', ['access_token' => $access_token]])->send()->json();
		$this->profiler->log($response);
	}
}<commit_msg>Возможность входа через vk<commit_after><?php
/**
* @package ivacuum.ru
* @copyright (c) 2013
*/

namespace app\ucp\oauth;

use Guzzle\Http\Client as http_client;
use Guzzle\Http\Exception\ClientErrorResponseException;

class vk extends base
{
	protected $authorize_endpoint    = 'https://oauth.vk.com/authorize';
	protected $access_token_endpoint = 'https://oauth.vk.com/access_token';
	protected $api_base_url          = 'https://api.vk.com/method';
	protected $api_provider          = 'vk';
	
	public function callback()
	{
		$this->check_oauth_state();
		$this->redirect_if_user_denied();
		
		$client = new http_client();
		$params = $this->access_token_endpoint . '?' . http_build_query($this->get_access_token_params());
		
		try
		{
			$json = $client->get($params)->send()->json();
		}
		catch (ClientErrorResponseException $e)
		{
			$this->profiler->log($e->getMessage());
			trigger_error('Произошла ошибка. Пожалуйста, повторите попытку позднее.');
		}
		
		if (isset($json['error']))
		{
			trigger_error($json['error_description']);
		}
		
		/**
		* $json = Array
		* (
		*     [access_token] => 304f1263064<PASSWORD>435<PASSWORD>6a49743b54acc82adac39a8d0c8bee10bc3e92429b641b7c3e62e34c1a1916a41
		*     [expires_in] => 86399
		*     [user_id] => 1
		* )
		*/
		$access_token = $json['access_token'];
		$fields       = 'sex, bdate, photo_max_orig';
		$uids         = (int) $json['user_id'];
		
		$client->setBaseUrl($this->api_base_url);
		$params = ['users.get{?uids,fields}', compact('uids', 'fields')];
		$json = $client->get($params)->send()->json()['response'][0];
		
		/**
		* $json = Array
		* (
		*     [uid] => 1
		*     [first_name] => Имя
		*     [last_name] => Фамилия
		*     [sex] => 2
		*     [bdate] => 1.1.1990
		*     [photo_max_orig] => http://cs319030.vk.me/v319030401/5cf9/XcoGsk3ltUI.jpg
		* )
		*
		* profile = https://vk.com/id{id}
		*/
		$uid = (int) $json['uid'];
		
		if (false === $user_id = $this->get_openid_user_id($uid))
		{
			/* Новые данные */
			$sql_ary = [
				'user_id'           => 0,
				'openid_time'       => $this->user->ctime,
				'openid_provider'   => $this->api_provider,
				'openid_uid'        => $uid,
				'openid_identity'   => "https://vk.com/id{$uid}",
				'openid_first_name' => $json['first_name'],
				'openid_last_name'  => $json['last_name'],
				'openid_dob'        => $json['bdate'],
				'openid_gender'     => $json['sex'],
				'openid_photo'      => $json['photo_max_orig'],
			];
			
			$sql = 'INSERT INTO site_openid_identities ' . $this->db->build_array('INSERT', $sql_ary);
			$this->db->query($sql);
		}
		
		if ($user_id > 0)
		{
			/* Данные закреплены за пользователем, можно аутентифицировать */
			$this->user->session_end(false);
			$this->user->session_create(false, $user_id, true, false, $this->api_provider);
			$this->request->redirect(ilink());
		}
		
		/* Кто из друзей уже зарегистрирован */
		// $params = ['friends.getAppUsers{?access_token}', compact('access_token')];
		// $json = $client->get($params)->send()->json();
		// $this->profiler->log($json);
		trigger_error('Дорегистрация');
	}
	
	protected function get_access_token_params()
	{
		return [
			'client_id'     => $this->config['oauth.vk.app_id'],
			'client_secret' => $this->config['oauth.vk.app_secret'],
			'code'          => $this->request->variable('code', ''),
			'redirect_uri'  => $this->get_redirect_uri(),
		];
	}
	
	protected function get_authorize_params()
	{
		$_SESSION["oauth.{$this->api_provider}.state"] = $state = make_random_string(10);

		/* scope notify нужен для работы параметра state */
		return [
			'client_id'     => $this->config['oauth.vk.app_id'],
			'redirect_uri'  => $this->get_redirect_uri(),
			'response_type' => 'code',
			'scope'         => 'friends, notify',
			'state'         => $state,
		];
	}
}
{'fraction_non_alphanumeric': 0.20178731009830206, 'fraction_numerical': 0.017336907953529938, 'mean_word_length': 3.553295362082994, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 120, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4300479', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2108, 'n_words': 287}