auth.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?
  2. # Always start a session
  3. session_start();
  4. function login($username, $password) {
  5. global $PAM_SERVICE;
  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. global $MAIN_PAGE;
  20. $url = $MAIN_PAGE;
  21. if (isset($_GET['dest'])) {
  22. $url = $_GET['dest'];
  23. }
  24. header('Location: ' . $url);
  25. }
  26. function logout() {
  27. global $MAIN_PAGE;
  28. unset($_SESSION['USER']);
  29. session_regenerate_id(true);
  30. header('Location: ' . $MAIN_PAGE);
  31. exit();
  32. }
  33. function username() {
  34. return $_SESSION['USER'];
  35. }
  36. function authenticated() {
  37. return isset($_SESSION['USER']);
  38. }
  39. function require_authentication() {
  40. global $LOGIN_PAGE;
  41. if (!authenticated()) {
  42. # Provide the current URL for post-auth redirect, if possible
  43. $dest = '';
  44. if (!preg_match('/' . preg_quote($LOGIN_PAGE) . '/', $_SERVER['PHP_SELF'])) {
  45. $dest = $_SERVER['PHP_SELF'];
  46. if ($_GET['series']) {
  47. $dest .= '?series=' . $_GET['series'];
  48. }
  49. }
  50. $url = $LOGIN_PAGE;
  51. if (strlen($dest)) {
  52. $url .= '?dest=' . urlencode($dest);
  53. }
  54. header('Location: ' . $url);
  55. exit();
  56. }
  57. }
  58. function die_if_not_authenticated() {
  59. if (!authenticated) {
  60. die('Failure: Auth');
  61. }
  62. }
  63. ?>