GitHub.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Provider\Driver;
  10. use ForkBB\Models\Provider\Driver;
  11. use RuntimeException;
  12. class GitHub extends Driver
  13. {
  14. protected string $origName = 'github';
  15. protected string $authURL = 'https://github.com/login/oauth/authorize';
  16. protected string $tokenURL = 'https://github.com/login/oauth/access_token';
  17. protected string $userURL = 'https://api.github.com/user';
  18. protected string $scope = 'read:user';
  19. /**
  20. * Запрашивает информацию о пользователе
  21. * Проверяет ответ
  22. * Запоминает данные пользователя
  23. */
  24. public function reqUserInfo(): bool
  25. {
  26. $this->userInfo = [];
  27. $headers = [
  28. 'Accept: application/json',
  29. "Authorization: Bearer {$this->access_token}",
  30. "User-Agent: ForkBB (Client ID: {$this->client_id})",
  31. ];
  32. if (empty($ch = \curl_init($this->userURL))) {
  33. $this->error = 'cURL error';
  34. $this->curlError = \curl_error($ch);
  35. return false;
  36. }
  37. \curl_setopt($ch, \CURLOPT_MAXREDIRS, 10);
  38. \curl_setopt($ch, \CURLOPT_TIMEOUT, 10);
  39. \curl_setopt($ch, \CURLOPT_HTTPHEADER, $headers);
  40. \curl_setopt($ch, \CURLOPT_HTTPGET, true);
  41. // \curl_setopt($ch, \CURLOPT_POST, false);
  42. \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true);
  43. \curl_setopt($ch, \CURLOPT_HEADER, false);
  44. $result = \curl_exec($ch);
  45. \curl_close($ch);
  46. if (false === $result) {
  47. $this->error = 'cURL error';
  48. $this->curlError = \curl_error($ch);
  49. return false;
  50. }
  51. if (
  52. ! isset($result[1])
  53. || '{' !== $result[0]
  54. || '}' !== $result[-1]
  55. || ! \is_array($userInfo = \json_decode($result, true, 20, self::JSON_OPTIONS))
  56. || empty($userInfo['id'])
  57. ) {
  58. $this->error = 'User error';
  59. return false;
  60. }
  61. $this->userInfo = $this->c->Secury->replInvalidChars($userInfo);
  62. return true;
  63. }
  64. /**
  65. * Возвращает идентификатор пользователя (от провайдера)
  66. */
  67. protected function getuserId(): string
  68. {
  69. return (string) ($this->userInfo['id'] ?? '');
  70. }
  71. /**
  72. * Возвращает логин пользователя (от провайдера)
  73. */
  74. protected function getuserLogin(): string
  75. {
  76. return (string) ($this->userInfo['login'] ?? '');
  77. }
  78. /**
  79. * Возвращает имя пользователя (от провайдера)
  80. */
  81. protected function getuserName(): string
  82. {
  83. return (string) ($this->userInfo['name'] ?? '');
  84. }
  85. /**
  86. * Возвращает email пользователя (от провайдера)
  87. */
  88. protected function getuserEmail(): string
  89. {
  90. return $this->c->Mail->valid($this->userInfo['email'] ?? null) ?: "{$this->origName}-{$this->userId}@localhost";
  91. }
  92. /**
  93. * Возвращает флаг подлинности email пользователя (от провайдера)
  94. */
  95. protected function getuserEmailVerifed(): bool
  96. {
  97. return false;
  98. }
  99. /**
  100. * Возвращает ссылку на аватарку пользователя (от провайдера)
  101. */
  102. protected function getuserAvatar(): string
  103. {
  104. return (string) ($this->userInfo['avatar_url'] ?? '');
  105. }
  106. /**
  107. * Возвращает ссылку на профиль пользователя (от провайдера)
  108. */
  109. protected function getuserURL(): string
  110. {
  111. return $this->userInfo['html_url'];
  112. }
  113. /**
  114. * Возвращает местоположение пользователя (от провайдера)
  115. */
  116. protected function getuserLocation(): string
  117. {
  118. return \mb_substr((string) ($this->userInfo['location'] ?? ''), 0, 30, 'UTF-8');
  119. }
  120. /**
  121. * Возвращает пол пользователя (от провайдера)
  122. */
  123. protected function getuserGender(): int
  124. {
  125. return FORK_GEN_NOT;
  126. }
  127. }