httpauth.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2004-2005 Patrick R. Michaud (pmichaud@pobox.com)
  3. This file is part of PmWiki; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version. See pmwiki.php for full details.
  7. This file defines an alternate authentication scheme based on the
  8. HTTP Basic authentication protocol (i.e., the scheme used by default
  9. in PmWiki 1).
  10. */
  11. ## If the webserver has already authenticated someone, then use
  12. ## that identifier for our authorization id. We also disable
  13. ## the use of the browser's Basic Auth form later, since it tends
  14. ## to confuse webservers.
  15. if (IsEnabled($EnableRemoteUserAuth, 1) && @$_SERVER['REMOTE_USER']) {
  16. SDV($EnableHTTPBasicAuth, 0);
  17. SDV($AuthId, $_SERVER['REMOTE_USER']);
  18. }
  19. ## If the browser supplied a password, add that password to the
  20. ## list of passwords used for authentication
  21. if (@$_SERVER['PHP_AUTH_PW']) {
  22. @session_start();
  23. @$_SESSION['authpw'][$_SERVER['PHP_AUTH_PW']]++;
  24. $_REQUEST[session_name()] = 1;
  25. }
  26. ## $EnableHTTPBasicAuth tells PmWikiAuth to use the browser's
  27. ## HTTP Basic protocol prompt instead of a form-based prompt.
  28. if (IsEnabled($EnableHTTPBasicAuth, 1))
  29. SDV($AuthPromptFmt, 'function:HTTPBasicAuthPrompt');
  30. ## HTTPBasicAuthPrompt replaces PmWikiAuth's form-based password
  31. ## prompt with the browser-based HTTP Basic prompt.
  32. function HTTPBasicAuthPrompt($pagename) {
  33. global $AuthRealmFmt, $AuthDeniedFmt;
  34. SDV($AuthRealmFmt,$GLOBALS['WikiTitle']);
  35. SDV($AuthDeniedFmt,'A valid password is required to access this feature.');
  36. $realm=FmtPageName($AuthRealmFmt,$pagename);
  37. header("WWW-Authenticate: Basic realm=\"$realm\"");
  38. header("Status: 401 Unauthorized");
  39. header("HTTP-Status: 401 Unauthorized");
  40. PrintFmt($pagename,$AuthDeniedFmt);
  41. exit;
  42. }