oauth.php 3.0 KB

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