From 51f7ce9dbdcf3e5f05d272755459ce5f15242040 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Sat, 3 Oct 2020 21:44:09 +0200 Subject: [PATCH] Spotify #2 --- app/Routes.php | 3 +++ app/models/managers/SettingsManager.php | 33 +++++++++++++++++++++++++ app/plugins/Spotify.php | 26 +++++++++++++++---- 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 app/models/managers/SettingsManager.php diff --git a/app/Routes.php b/app/Routes.php index c58cfa4..385d0c5 100644 --- a/app/Routes.php +++ b/app/Routes.php @@ -52,4 +52,7 @@ $router->any('/api/example', 'ExampleApi@example'); $router->any('/example', 'ExampleController@index'); $router->any('/example/subpage', 'ExampleController@subpage'); +//module routes +//$router->any('/plugins/spotify/callback', 'Spotify@callback'); + $router->run($_SERVER['REQUEST_METHOD'], '/'.(isset($_GET['url']) ? $_GET['url'] : '')); diff --git a/app/models/managers/SettingsManager.php b/app/models/managers/SettingsManager.php new file mode 100644 index 0000000..f5aae96 --- /dev/null +++ b/app/models/managers/SettingsManager.php @@ -0,0 +1,33 @@ + $name, + 'value' => $value, + ); + try { + Db::add ('settings', $setting); + } catch(PDOException $error) { + echo $error->getMessage(); + die(); + } + } + + public static function update ($name, $value) { + try { + Db::edit ('settings', ['value' => $value], 'WHERE name = ?', array($name)); + } catch(PDOException $error) { + echo $error->getMessage(); + die(); + } + } +} +?> diff --git a/app/plugins/Spotify.php b/app/plugins/Spotify.php index 0da044e..bfef6ab 100644 --- a/app/plugins/Spotify.php +++ b/app/plugins/Spotify.php @@ -1,13 +1,29 @@ client_id . '&response_type=token&redirect_uri='.urlencode($this->redirect_uri).'&scope=user-read-playback-state'); + } private function setToken($token){ $this->token = $token; } - private function autorize(){ - $client_id = '76840e2199e34dcd903d19877bd726dd'; + public function callback(){ + $token = $_GET; + $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; + var_dump($actual_link); + (new SettingsManager)->create('spotify_token', $token); + } + + public function autorize(){ + $client_secret = '0f94ed2c0bd64bf791ea13b7e6310ba3'; $ch = curl_init(); @@ -15,7 +31,7 @@ class Spotify extends VirtualDeviceManager { curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($ch, CURLOPT_POST, 1 ); curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials&scope=user-read-playback-state' ); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.base64_encode($client_id.':'.$client_secret))); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.base64_encode($this->client_id.':'.$client_secret))); $result=curl_exec($ch); @@ -25,9 +41,9 @@ class Spotify extends VirtualDeviceManager { private function getPlayerData(){ $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/users/byzolscj4vc1p0xcjqykbesn8' ); + curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/me/player' ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $this->token)); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . (new SettingsManager)->getByName('spotify_token')['value'])); $result=curl_exec($ch); echo $result;