auth.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?
  2. # Always start a session, unless we're on the CLI
  3. if (php_sapi_name() != 'cli') {
  4. session_start();
  5. }
  6. function login($username, $password) {
  7. global $PAM_SERVICE;
  8. $username = preg_replace('/\W/', '_', $username);
  9. # Set the PAM service name
  10. ini_set('pam.servicename', $PAM_SERVICE);
  11. if (pam_auth($username, $password)) {
  12. $_SESSION['USER'] = $username;
  13. session_regenerate_id(true);
  14. } else {
  15. logout();
  16. }
  17. # Redirect, using the provided target if available
  18. login_redirect();
  19. }
  20. function login_redirect() {
  21. global $MAIN_PAGE;
  22. $url = $MAIN_PAGE;
  23. if (isset($_GET['dest'])) {
  24. $url = $_GET['dest'];
  25. }
  26. header('Location: ' . $url);
  27. }
  28. function logout() {
  29. global $MAIN_PAGE;
  30. unset($_SESSION['USER']);
  31. session_regenerate_id(true);
  32. header('Location: ' . $MAIN_PAGE);
  33. exit();
  34. }
  35. function username() {
  36. return $_SESSION['USER'];
  37. }
  38. function authenticated() {
  39. return isset($_SESSION['USER']);
  40. }
  41. function require_authentication() {
  42. global $LOGIN_PAGE;
  43. if (!authenticated()) {
  44. # Provide the current URL for post-auth redirect, if possible
  45. $dest = '';
  46. if (!preg_match('/' . preg_quote($LOGIN_PAGE) . '/', $_SERVER['PHP_SELF'])) {
  47. $dest = $_SERVER['PHP_SELF'];
  48. if ($_GET['series']) {
  49. $dest .= '?series=' . $_GET['series'];
  50. }
  51. }
  52. $url = $LOGIN_PAGE;
  53. if (strlen($dest)) {
  54. $url .= '?dest=' . urlencode($dest);
  55. }
  56. header('Location: ' . $url);
  57. exit();
  58. }
  59. }
  60. function die_if_not_authenticated() {
  61. if (!authenticated) {
  62. die('Failure: Auth');
  63. }
  64. }
  65. ?>