PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/auth/mtrack.php

https://bitbucket.org/yoander/mtrack
PHP | 121 lines | 91 code | 18 blank | 12 comment | 21 complexity | ed5aeff14cdfa334bd6e5f392230705f MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. <?php # vim:ts=2:sw=2:et:
  2. /* For licensing and copyright terms, see the file named LICENSE */
  3. /* mtrack locally maintained authentication */
  4. class MTrackAuth_MTrack implements IMTrackAuth, IMTrackNavigationHelper {
  5. function __construct() {
  6. MTrackAuth::registerMech($this);
  7. MTrackNavigation::registerHelper($this);
  8. }
  9. function augmentUserInfo(&$content) {
  10. if (MTrackAuth::whoami() == 'anonymous' && !$this->authenticate()) {
  11. $content = "<a href='$GLOBALS[ABSWEB]auth/'>Log In</a>";
  12. }
  13. }
  14. function augmentNavigation($id, &$items) {
  15. }
  16. /* If we're running under the REST bits, we may want to use HTTP
  17. * auth instead of cookies.
  18. *
  19. * if we've already got a session, and it is not empty,
  20. * we assume that the session was started via browser based auth,
  21. * or by some cookie aware client.
  22. *
  23. * Otherwise, we want to use HTTP auth
  24. */
  25. function shouldUseHTTPAuth() {
  26. if (defined('MTRACK_IS_REST_API')) {
  27. if (isset($_COOKIE[session_name()])) {
  28. /* client sent us a cookie */
  29. return false;
  30. }
  31. return true;
  32. }
  33. return false;
  34. }
  35. function authenticate() {
  36. if ($this->shouldUseHTTPAuth()) {
  37. if (isset($_SERVER['PHP_AUTH_USER'])) {
  38. $user = MTrackUser::loadUser($_SERVER['PHP_AUTH_USER'], true);
  39. if (!$user) {
  40. return null;
  41. }
  42. if ($user->verifyPassword($_SERVER['PHP_AUTH_PW'])) {
  43. return $user->userid;
  44. }
  45. }
  46. return null;
  47. }
  48. if (!strlen(session_id()) && php_sapi_name() != 'cli') {
  49. session_start();
  50. }
  51. if (isset($_SESSION['auth.mtrack'])) {
  52. return $_SESSION['auth.mtrack'];
  53. }
  54. return null;
  55. }
  56. function doAuthenticate($force = false) {
  57. if (defined('MTRACK_IS_REST_API')) {
  58. if ($this->shouldUseHTTPAuth()) {
  59. header("WWW-Authenticate: Basic realm=\"$_SERVER[SERVER_NAME]\"");
  60. exit;
  61. }
  62. }
  63. if ($force) {
  64. if ($this->shouldUseHTTPAuth()) {
  65. header("WWW-Authenticate: Basic realm=\"$_SERVER[SERVER_NAME]\"");
  66. } else {
  67. header("Location: $GLOBALS[ABSWEB]auth/");
  68. }
  69. exit;
  70. }
  71. return null;
  72. }
  73. function enumGroups() {
  74. return null;
  75. }
  76. function getGroups($username) {
  77. return null;
  78. }
  79. function addToGroup($username, $groupname)
  80. {
  81. return null;
  82. }
  83. function removeFromGroup($username, $groupname)
  84. {
  85. return null;
  86. }
  87. function getUserData($username) {
  88. return null;
  89. }
  90. function canLogOut() {
  91. return true;
  92. }
  93. function LogOut() {
  94. if (isset($_COOKIE[session_name()])) {
  95. if (!session_id()) session_start();
  96. if (isset($_SESSION['auth.mtrack'])) {
  97. session_destroy();
  98. header('Location: ' . $GLOBALS['ABSWEB']);
  99. exit;
  100. }
  101. }
  102. }
  103. }