Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

user.php 2.6KB

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