oc-social-plugin/formwidgets/InstagramLogin.php

139 lines
4.0 KiB
PHP

<?php namespace Zoomyboy\Social\FormWidgets;
use \Session;
use Backend\Classes\FormWidgetBase;
use GuzzleHttp\Client;
use Zoomyboy\Social\Models\InstagramUser;
use Zoomyboy\Social\Models\Page;
use Zoomyboy\Social\Models\Setting;
/**
* InstagramLogin Form Widget
*/
class InstagramLogin extends FormWidgetBase
{
/**
* @inheritDoc
*/
protected $defaultAlias = 'zoomyboy_social_instagram_login';
/**
* @inheritDoc
*/
public function init()
{
if (request()->has('code') && request()->query('state') == Session::get('instagram_auth_state')) {
$this->authenticate();
}
}
public function authenticate(): void
{
$response = $this->authClient()->post('/oauth/access_token', [
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
'form_params' => [
'client_id' => Setting::get('instagram_client_id'),
'redirect_uri' => $this->redirectUri(),
'client_secret' => Setting::get('instagram_client_secret'),
'grant_type' => 'authorization_code',
'code' => request()->query('code')
]
]);
$accessToken = json_decode((string) $response->getBody())->access_token;
$response = $this->client()->get('/access_token', ['query' => [
'grant_type' => 'ig_exchange_token',
'client_secret' => Setting::get('instagram_client_secret'),
'access_token' => $accessToken,
]]);
$accessToken = json_decode((string) $response->getBody())->access_token;
$this->storeUser($accessToken);
}
public function storePages(): void
{
$client = $this->client($auth = true);
$response = $client->get('/me');
$userId = json_decode((string) $response->getBody())->id;
$response = $client->get('/v11.0/'.$userId.'/accounts');
$response = json_decode((string) $response->getBody());
foreach ($response->data as $page) {
Page::updateOrCreate(
['remote_id' => $page->id],
['type' => 'facebook', 'remote_id' => $page->id, 'access_token' => $page->access_token, 'name' => $page->name],
);
}
}
public function render()
{
$this->prepareVars();
return $this->makePartial('instagramlogin');
}
public function redirectUri(): string
{
return env('INSTAGRAM_REDIRECT_URI', url()->current());
}
/**
* Prepares the form widget view data
*/
public function prepareVars()
{
$state = str_random('20');
Session::put('instagram_auth_state', $state);
$this->vars['name'] = $this->formField->getName();
$this->vars['value'] = $this->getLoadValue();
$this->vars['model'] = $this->model;
$this->vars['client_id'] = Setting::get('instagram_client_id');
$this->vars['redirect_url'] = $this->redirectUri();
$this->vars['state'] = $state;
}
public function loadAssets()
{
$this->addCss('css/facebooklogin.css', 'zoomyboy.social');
$this->addJs('js/facebooklogin.js', 'zoomyboy.social');
}
public function getSaveValue($value)
{
return $value;
}
private function client(bool $auth = false): Client
{
return new Client([
'base_uri' => 'https://graph.instagram.com',
]);
}
private function authClient(): Client
{
return new Client([
'base_uri' => 'https://api.instagram.com',
]);
}
private function storeUser(string $accessToken): void
{
$response = $this->client()->get('/me', ['query' => [
'access_token' => $accessToken,
'fields' => 'id,username',
]]);
$data = json_decode((string) $response->getBody());
InstagramUser::create([
'access_token' => $accessToken,
'name' => $data->username,
'id' => $data->id,
]);
}
}