oauth.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. trait OAuthFunctions
  3. {
  4. public function traktOAuth()
  5. {
  6. $provider = new Bogstag\OAuth2\Client\Provider\Trakt([
  7. 'clientId' => $this->config['traktClientId'],
  8. 'clientSecret' => $this->config['traktClientSecret'],
  9. 'redirectUri' => $this->getServerPath() . 'api/v2/oauth/trakt'
  10. ], [
  11. 'httpClient' => new GuzzleHttp\Client(['verify' => getCert()]),
  12. ]);
  13. if (!isset($_GET['code'])) {
  14. $authUrl = $provider->getAuthorizationUrl();
  15. header('Location: ' . $authUrl);
  16. exit;
  17. } elseif (empty($_GET['state'])) {
  18. exit('Invalid state');
  19. } else {
  20. try {
  21. $token = $provider->getAccessToken('authorization_code', [
  22. 'code' => $_GET['code']
  23. ]);
  24. $traktDetails = [
  25. 'traktAccessToken' => $token->getToken(),
  26. 'traktAccessTokenExpires' => gmdate('Y-m-d\TH:i:s\Z', $token->getExpires()),
  27. 'traktRefreshToken' => $token->getRefreshToken()
  28. ];
  29. $this->updateConfig($traktDetails);
  30. echo '
  31. <!DOCTYPE html>
  32. <html lang="en">
  33. <head>
  34. <link rel="stylesheet" href="' . $this->getServerPath() . '/css/mvp.css">
  35. <meta charset="utf-8">
  36. <meta name="description" content="Trakt OAuth">
  37. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  38. <title>Trakt OAuth</title>
  39. </head>
  40. <script language=javascript>
  41. function closemyself() {
  42. window.opener=self;
  43. window.close();
  44. }
  45. </script>
  46. <body onLoad="setTimeout(\'closemyself()\',3000);">
  47. <main>
  48. <section>
  49. <aside>
  50. <h3>Details Saved</h3>
  51. <p><sup>(This window will close automatically)</sup></p>
  52. </aside>
  53. </section>
  54. </main>
  55. </body>
  56. </html>
  57. ';
  58. exit;
  59. } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
  60. exit($e->getMessage());
  61. }
  62. }
  63. }
  64. public function traktOAuthRefresh()
  65. {
  66. $exp = $this->config['traktAccessTokenExpires'];
  67. $exp = date('Y-m-d\TH:i:s\Z', strtotime($exp . ' - 30 days'));
  68. if (time() - 2592000 > strtotime($exp)) {
  69. $headers = [
  70. 'Content-Type' => 'application/json'
  71. ];
  72. $data = [
  73. 'refresh_token' => $this->config['traktRefreshToken'],
  74. 'clientId' => $this->config['traktClientId'],
  75. 'clientSecret' => $this->config['traktClientSecret'],
  76. 'redirectUri' => $this->getServerPath() . 'api/v2/oauth/trakt',
  77. 'grant_type' => 'refresh_token'
  78. ];
  79. $url = $this->qualifyURL('https://api.trakt.tv/oauth/token');
  80. try {
  81. $response = Requests::post($url, $headers, json_encode($data), []);
  82. if ($response->success) {
  83. $data = json_decode($response->body, true);
  84. $newExp = date('Y-m-d\TH:i:s\Z', strtotime($this->currentTime . ' + 90 days'));
  85. $traktDetails = [
  86. 'traktAccessToken' => $data['access_token'],
  87. 'traktAccessTokenExpires' => $newExp,
  88. 'traktRefreshToken' => $data['refresh_token']
  89. ];
  90. $this->updateConfig($traktDetails);
  91. return true;
  92. }
  93. } catch (Requests_Exception $e) {
  94. $this->writeLog('error', 'Trakt Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  95. $this->setAPIResponse('error', $e->getMessage(), 500);
  96. return false;
  97. }
  98. }
  99. }
  100. }