auth.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?
  2. # Always start a session
  3. session_start();
  4. function login($username, $password) {
  5. require 'config.php';
  6. $username = preg_replace('/\W/', '_', $username);
  7. # Set the PAM service name
  8. ini_set('pam.servicename', $PAM_SERVICE);
  9. if (pam_auth($username, $password)) {
  10. $_SESSION['USER'] = $username;
  11. session_regenerate_id(true);
  12. } else {
  13. logout();
  14. }
  15. # Redirect, if we have a target
  16. if (isset($_GET['dest'])) {
  17. header('Location: ' . $_GET['dest']);
  18. }
  19. }
  20. function logout() {
  21. require 'config.php';
  22. unset($_SESSION['USER']);
  23. session_regenerate_id(true);
  24. header('Location: ./');
  25. exit();
  26. }
  27. function username() {
  28. return $_SESSION['USER'];
  29. }
  30. function authenticated() {
  31. return isset($_SESSION['USER']);
  32. }
  33. function require_authentication() {
  34. require 'config.php';
  35. if (!authenticated()) {
  36. # Provide the current URL for post-auth redirect, if possible
  37. $dest = '';
  38. if (!preg_match('/' . preg_quote($LOGIN_PAGE) . '/', $_SERVER['PHP_SELF'])) {
  39. $dest = $_SERVER['PHP_SELF'];
  40. if ($_GET['series']) {
  41. $dest .= '?series=' . $_GET['series'];
  42. }
  43. }
  44. $url = $LOGIN_PAGE;
  45. if (strlen($dest)) {
  46. $url .= '?dest=' . urlencode($dest);
  47. }
  48. header('Location: ' . $url);
  49. exit();
  50. }
  51. }
  52. ?>