Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 ignore 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) . '"') . ')');
  51. // Get voter ID
  52. $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));
  53. $admincheck = $db->fetchRow('select members.pollworker from members where skymanager_id=' . ((int) $this->uid));
  54. if ($result) {
  55. $this->voterId = $result['voting_id'];
  56. // Auto check in
  57. $_ = $db->query('update members set checkedin=1 where voting_id is not null and skymanager_id=' . ((int) $this->uid));
  58. } else {
  59. $this->voterId = null;
  60. }
  61. if ($admincheck)
  62. $this->role = $admincheck['pollworker'];
  63. else
  64. $this->role = 0;
  65. return true;
  66. }
  67. public function username(){
  68. return $this->username;
  69. }
  70. public function name(){
  71. return $this->name;
  72. }
  73. public function voterId(){
  74. return $this->voterId;
  75. }
  76. public function email(){
  77. return $this->email;
  78. }
  79. public function gravatarUrl($size = 128){
  80. return 'https://www.gravatar.com/avatar/' . md5($this->email) . ".png?r=pg&s=$size";
  81. }
  82. public function loggedin(){
  83. return $this->loggedin;
  84. }
  85. public function getRole(){
  86. return $this->role ? 'admin' : 'voter';
  87. //return $this->role;
  88. }
  89. public function logout(){
  90. $_SESSION['token'] = "";
  91. $this->username = "";
  92. $this->uid = -1;
  93. $this->loggedin = false;
  94. }
  95. public function getUserId(){
  96. return $this->uid;
  97. }
  98. }
  99. $user = new User();