auth.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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, using the provided target if available
  16. login_redirect();
  17. }
  18. function login_redirect() {
  19. $url = './';
  20. if (isset($_GET['dest'])) {
  21. $url = $_GET['dest'];
  22. }
  23. header('Location: ' . $url);
  24. }
  25. function logout() {
  26. require 'config.php';
  27. unset($_SESSION['USER']);
  28. session_regenerate_id(true);
  29. header('Location: ./');
  30. exit();
  31. }
  32. function username() {
  33. return $_SESSION['USER'];
  34. }
  35. function authenticated() {
  36. return isset($_SESSION['USER']);
  37. }
  38. function require_authentication() {
  39. require 'config.php';
  40. if (!authenticated()) {
  41. # Provide the current URL for post-auth redirect, if possible
  42. $dest = '';
  43. if (!preg_match('/' . preg_quote($LOGIN_PAGE) . '/', $_SERVER['PHP_SELF'])) {
  44. $dest = $_SERVER['PHP_SELF'];
  45. if ($_GET['series']) {
  46. $dest .= '?series=' . $_GET['series'];
  47. }
  48. }
  49. $url = $LOGIN_PAGE;
  50. if (strlen($dest)) {
  51. $url .= '?dest=' . urlencode($dest);
  52. }
  53. header('Location: ' . $url);
  54. exit();
  55. }
  56. }
  57. ?>