Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. require_once('db.php');
  3. class User{
  4. private $username = "";
  5. private $email = "";
  6. private $name = "";
  7. private $uid = -1;
  8. private $voterId = -1;
  9. private $loggedin = false;
  10. private $role = 0;
  11. function __construct(){
  12. if(isset($_SESSION['token']) && strlen($_SESSION['token']) > 41) {
  13. $this->parseToken($_SESSION['token']);
  14. }
  15. }
  16. public function login($username, $password){
  17. $data = http_build_query([
  18. 'username' => $username,
  19. 'password' => $password,
  20. 'grant_type' => 'password'
  21. ]);
  22. $opt = [
  23. 'http' => [
  24. 'method' => 'POST',
  25. 'header' => "Content-type: application/x-www-form-urlencoded\r\n"
  26. . "Content-Length: " . strlen($data) . "\r\n",
  27. 'content' => $data
  28. ]
  29. ];
  30. $ctx = stream_context_create($opt);
  31. $token = file_get_contents('https://beta.schedule.michiganflyers.org/api/oauth/token', false, $ctx);
  32. if (!empty($token)) {
  33. $_SESSION['token'] = json_decode($token)->access_token;
  34. return $this->parseToken($_SESSION['token']);
  35. }
  36. return false;
  37. }
  38. private function parseToken($token) {
  39. global $db;
  40. $data = explode('.', $token);
  41. if (count($data) != 3)
  42. return false;
  43. $obj = json_decode(base64_decode($data[1]));
  44. $this->username = $obj->preferred_username;
  45. $this->name = $obj->name;
  46. $this->uid = $obj->sub;
  47. $this->email = $obj->email ?? null;
  48. $this->loggedin = true;
  49. // Create user automatically on login
  50. //$_ = $db->query('insert into members (skymanager_id, name, username, email) VALUES (' . ((int) $this->uid) . ', "' . $db->sanitize($this->name) . '", "' . $db->sanitize($this->username) . '", ' . (empty($this->email) ? 'NULL' : '"' . $db->sanitize($this->email) . '"') . ') ON DUPLICATE KEY UPDATE skymanager_id=skymanager_id');
  51. $_ = $db->insert('members', ['skymanager_id', 'name', 'username', 'email'], [[((int) $this->uid), $this->name, $this->username, (empty($this->email) ? 'NULL' : $this->email)]], true);
  52. // Get voter ID
  53. $result = $db->fetchRow('select members.voting_id from members left join proxy on (members.voting_id=proxy.voting_id) where proxy.delegate_id is null and skymanager_id=' . ((int) $this->uid));
  54. $admincheck = $db->fetchRow('select members.pollworker from members where skymanager_id=' . ((int) $this->uid));
  55. if ($result) {
  56. $this->voterId = $result['voting_id'];
  57. // Auto check in
  58. $_ = $db->query('update members set checkedin=1 where voting_id is not null and skymanager_id=' . ((int) $this->uid));
  59. } else {
  60. $this->voterId = null;
  61. }
  62. if ($admincheck)
  63. $this->role = $admincheck['pollworker'];
  64. else
  65. $this->role = 0;
  66. return true;
  67. }
  68. public function username(){
  69. return $this->username;
  70. }
  71. public function name(){
  72. return $this->name;
  73. }
  74. public function voterId(){
  75. return $this->voterId;
  76. }
  77. public function email(){
  78. return $this->email;
  79. }
  80. public function gravatarUrl($size = 128){
  81. return 'https://www.gravatar.com/avatar/' . md5($this->email) . ".png?r=pg&s=$size";
  82. }
  83. public function loggedin(){
  84. return $this->loggedin;
  85. }
  86. public function getRole(){
  87. return $this->role ? 'admin' : 'voter';
  88. //return $this->role;
  89. }
  90. public function logout(){
  91. $_SESSION['token'] = "";
  92. $this->username = "";
  93. $this->uid = -1;
  94. $this->loggedin = false;
  95. }
  96. public function getUserId(){
  97. return $this->uid;
  98. }
  99. }
  100. $user = new User();