Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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