auth.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /* GNU FM -- a free network service for sharing your music listening habits
  3. Copyright (C) 2009 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. require_once('database.php');
  16. require_once('data/User.php');
  17. require_once($install_path . '/temp-utils.php'); // this is extremely dodgy and shameful
  18. $logged_in = false;
  19. session_start();
  20. if (isset($_COOKIE['session_id'])) {
  21. $err = 0;
  22. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  23. try {
  24. $row = $adodb->GetRow('SELECT userid FROM Scrobble_Sessions WHERE '
  25. . 'sessionid = ' . $adodb->qstr($_COOKIE['session_id'])
  26. . ' AND expires > ' . (int)(time()));
  27. } catch (Exception $e) {
  28. $err = 1;
  29. }
  30. if ($err || !$row) {
  31. // Session is invalid
  32. setcookie('session_id', '', time() - 3600);
  33. session_unset();
  34. session_destroy();
  35. } else {
  36. $logged_in = true;
  37. $username = uniqueid_to_username($row['userid']);
  38. try {
  39. $this_user = new User($username);
  40. } catch (Exception $e) {
  41. $err = 1;
  42. }
  43. }
  44. header("Cache-Control:no-cache");
  45. header("Pragma: no-cache");
  46. header("Expires = -1");
  47. } else {
  48. $seconds_to_cache = 3600;
  49. $ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
  50. header("Expires: $ts");
  51. header("Pragma: no-cache");
  52. header("Cache-Control: max-age=$seconds_to_cache");
  53. setcookie('lang', '', time() - 3600);
  54. }