From 5f602f2af23023c39829b262b98f101fa93e95f0 Mon Sep 17 00:00:00 2001 From: Sam Serrien Date: Wed, 7 Apr 2021 14:37:07 +0200 Subject: [PATCH] Update plugin with longlived token and refresh functionality --- classes/Instagram.php | 113 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 92 insertions(+), 21 deletions(-) diff --git a/classes/Instagram.php b/classes/Instagram.php index b69bfd1..cb1cc8d 100644 --- a/classes/Instagram.php +++ b/classes/Instagram.php @@ -4,6 +4,8 @@ class Instagram { private $db; + private $token; + private $tokenRefreshed; public function __construct() { @@ -24,6 +26,7 @@ public function __construct() $this->db = kirby()->root('assets').DS.option('genxbe.instagram.assetFolder').DS.option('genxbe.instagram.db'); $this->token = site()->instagramToken(); + $this->tokenRefreshed = page('sitesettings')->instagramTokenRefreshed(); } public function feed() @@ -33,32 +36,84 @@ public function feed() public function getToken() { - if(get('code')) + try { - $options = [ - 'headers' => [ - 'Content-Type: application/x-www-form-urlencoded; charset=utf-8', - ], - 'method' => 'POST', - 'data' => http_build_query([ - 'client_id' => option('genxbe.instagram.client_id'), - 'client_secret' => option('genxbe.instagram.client_secret'), - 'grant_type' => 'authorization_code', - 'redirect_uri' => option('genxbe.instagram.redirect_uri'), - 'code' => get('code'), - ]), - ]; - - $data = \Remote::request('https://api.instagram.com/oauth/access_token', $options); + if(get('code')) + { + $options = [ + 'headers' => [ + 'Content-Type: application/x-www-form-urlencoded; charset=utf-8', + ], + 'method' => 'POST', + 'data' => http_build_query([ + 'client_id' => option('genxbe.instagram.client_id'), + 'client_secret' => option('genxbe.instagram.client_secret'), + 'grant_type' => 'authorization_code', + 'redirect_uri' => option('genxbe.instagram.redirect_uri'), + 'code' => get('code'), + ]), + ]; + + $data = \Remote::request('https://api.instagram.com/oauth/access_token', $options); + + $access_token = $data->json()['access_token']; + $client_secret = option('genx.blocks-instagram.client_secret'); + + $data = \Remote::get("https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret={$client_secret}&access_token={$access_token}"); + + $access_token = $data->json()['access_token']; + + site()->update([ + 'instagramAuth' => (boolean)true, + 'instagramToken' => $data->json()['access_token'], + 'instagramTokenRefreshed' => date('Y-m-d'), + ]); + + exec("php {__DIR__} fetch.php > /dev/null &"); + } + } + catch(\Exception $e) + { + site()->update([ + 'instagramAuth' => (boolean)false, + 'instagramToken' => '', + ]); + } + + go(site()->panelUrl(), 303); + } + + public function refreshToken() + { + $kirby = kirby(); + $kirby->impersonate('kirby'); + + try + { + if(site()->instagramAuth()->isFalse()) + { + exit; + } + + $access_token = $this->token; + $client_secret = option('genxbe.instagram.client_secret'); + + $data = \Remote::get("https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&client_secret={$client_secret}&access_token={$access_token}"); + + $access_token = $data->json()['access_token']; site()->update([ 'instagramAuth' => (boolean)true, 'instagramToken' => $data->json()['access_token'], + 'instagramTokenRefreshed' => date('Y-m-d'), + ]); + } + catch(\Exception $e) + { + site()->update([ + 'instagramAuth' => (boolean)false, + 'instagramToken' => '', ]); - - exec("php {__DIR__} fetch.php > /dev/null &"); - - go(site()->panelUrl(), 303); } } @@ -121,7 +176,7 @@ public function fetch() } } - $newDb = $db->limit(20); + $newDb = $db->sortBy('timestamp', 'desc'); foreach(array_diff(scandir($path), array('.', '..')) as $file) { @@ -132,6 +187,22 @@ public function fetch() } $this->writeDb($newDb); + + if($this->dateDifference(date('Y-m-d'), $this->tokenRefreshed) > 50) + { + self::refreshToken(); + } + } + + private function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' ) + { + $datetime1 = date_create($date_1); + $datetime2 = date_create($date_2); + + $interval = date_diff($datetime1, $datetime2); + + return $interval->format($differenceFormat); + } private function getDb()