GitHub.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_HTTPHEADER, $headers);
  38. \curl_setopt($ch, \CURLOPT_POST, false);
  39. \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true);
  40. \curl_setopt($ch, \CURLOPT_HEADER, false);
  41. $result = \curl_exec($ch);
  42. \curl_close($ch);
  43. if (false === $result) {
  44. $this->error = 'cURL error';
  45. $this->curlError = \curl_error($ch);
  46. return false;
  47. }
  48. if (
  49. ! isset($result[1])
  50. || '{' !== $result[0]
  51. || '}' !== $result[-1]
  52. || ! \is_array($userInfo = \json_decode($result, true, 20, self::JSON_OPTIONS))
  53. || empty($userInfo['id'])
  54. ) {
  55. $this->error = 'User error';
  56. return false;
  57. }
  58. $this->userInfo = $this->c->Secury->replInvalidChars($userInfo);
  59. return true;
  60. }
  61. /**
  62. * Возвращает идентификатор пользователя (от провайдера)
  63. */
  64. protected function getuserId(): string
  65. {
  66. return (string) ($this->userInfo['id'] ?? '');
  67. }
  68. /**
  69. * Возвращает логин пользователя (от провайдера)
  70. */
  71. protected function getuserLogin(): string
  72. {
  73. return (string) ($this->userInfo['login'] ?? '');
  74. }
  75. /**
  76. * Возвращает имя пользователя (от провайдера)
  77. */
  78. protected function getuserName(): string
  79. {
  80. return (string) ($this->userInfo['name'] ?? '');
  81. }
  82. /**
  83. * Возвращает email пользователя (от провайдера)
  84. */
  85. protected function getuserEmail(): string
  86. {
  87. return $this->c->Mail->valid($this->userInfo['email'] ?? null) ?: "{$this->origName}-{$this->userId}@localhost";
  88. }
  89. /**
  90. * Возвращает флаг подлинности email пользователя (от провайдера)
  91. */
  92. protected function getuserEmailVerifed(): bool
  93. {
  94. return false;
  95. }
  96. /**
  97. * Возвращает ссылку на аватарку пользователя (от провайдера)
  98. */
  99. protected function getuserAvatar(): string
  100. {
  101. return (string) ($this->userInfo['avatar_url'] ?? '');
  102. }
  103. /**
  104. * Возвращает ссылку на профиль пользователя (от провайдера)
  105. */
  106. protected function getuserURL(): string
  107. {
  108. return $this->userInfo['html_url'];
  109. }
  110. /**
  111. * Возвращает местоположение пользователя (от провайдера)
  112. */
  113. protected function getuserLocation(): string
  114. {
  115. return \mb_substr((string) ($this->userInfo['location'] ?? ''), 0, 30, 'UTF-8');
  116. }
  117. /**
  118. * Возвращает пол пользователя (от провайдера)
  119. */
  120. protected function getuserGender(): int
  121. {
  122. return FORK_GEN_NOT;
  123. }
  124. }