actions.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. require_once "lib/common.php";
  3. require_once "lib/session.php";
  4. require_once "lib/render.php";
  5. require_once "lib/render/login.php";
  6. require_once "lib/render/idpage.php";
  7. require_once "lib/render/idpXrds.php";
  8. require_once "lib/render/userXrds.php";
  9. require_once "Auth/OpenID.php";
  10. /**
  11. * Handle a standard OpenID server request
  12. */
  13. function action_default()
  14. {
  15. header('X-XRDS-Location: '.buildURL('idpXrds'));
  16. $server = getServer();
  17. $method = $_SERVER['REQUEST_METHOD'];
  18. $request = null;
  19. if ($method == 'GET') {
  20. $request = $_GET;
  21. } else {
  22. $request = $_POST;
  23. }
  24. $request = $server->decodeRequest();
  25. if (!$request) {
  26. return about_render();
  27. }
  28. setRequestInfo($request);
  29. if (in_array($request->mode,
  30. array('checkid_immediate', 'checkid_setup'))) {
  31. if ($request->idSelect()) {
  32. // Perform IDP-driven identifier selection
  33. if ($request->mode == 'checkid_immediate') {
  34. $response = $request->answer(false);
  35. } else {
  36. return trust_render($request);
  37. }
  38. } else if ((!$request->identity) &&
  39. (!$request->idSelect())) {
  40. // No identifier used or desired; display a page saying
  41. // so.
  42. return noIdentifier_render();
  43. } else if ($request->immediate) {
  44. $response = $request->answer(false, buildURL());
  45. } else {
  46. if (!getLoggedInUser()) {
  47. return login_render();
  48. }
  49. return trust_render($request);
  50. }
  51. } else {
  52. $response = $server->handleRequest($request);
  53. }
  54. $webresponse = $server->encodeResponse($response);
  55. if ($webresponse->code != AUTH_OPENID_HTTP_OK) {
  56. header(sprintf("HTTP/1.1 %d ", $webresponse->code),
  57. true, $webresponse->code);
  58. }
  59. foreach ($webresponse->headers as $k => $v) {
  60. header("$k: $v");
  61. }
  62. header(header_connection_close);
  63. print $webresponse->body;
  64. exit(0);
  65. }
  66. /**
  67. * Log out the currently logged in user
  68. */
  69. function action_logout()
  70. {
  71. setLoggedInUser(null);
  72. setRequestInfo(null);
  73. return authCancel(null);
  74. }
  75. /**
  76. * Check the input values for a login request
  77. */
  78. function login_checkInput($input)
  79. {
  80. $openid_url = false;
  81. $errors = array();
  82. if (!isset($input['openid_url'])) {
  83. $errors[] = 'Enter an OpenID URL to continue';
  84. }
  85. if (count($errors) == 0) {
  86. $openid_url = $input['openid_url'];
  87. }
  88. return array($errors, $openid_url);
  89. }
  90. /**
  91. * Log in a user and potentially continue the requested identity approval
  92. */
  93. function action_login()
  94. {
  95. $method = $_SERVER['REQUEST_METHOD'];
  96. switch ($method) {
  97. case 'GET':
  98. return login_render();
  99. case 'POST':
  100. $info = getRequestInfo();
  101. $fields = $_POST;
  102. if (isset($fields['cancel'])) {
  103. return authCancel($info);
  104. }
  105. list ($errors, $openid_url) = login_checkInput($fields);
  106. if (count($errors) || !$openid_url) {
  107. $needed = $info ? $info->identity : false;
  108. return login_render($errors, @$fields['openid_url'], $needed);
  109. } else {
  110. setLoggedInUser($openid_url);
  111. return doAuth($info);
  112. }
  113. default:
  114. return login_render(array('Unsupported HTTP method: $method'));
  115. }
  116. }
  117. /**
  118. * Ask the user whether he wants to trust this site
  119. */
  120. function action_trust()
  121. {
  122. $info = getRequestInfo();
  123. $trusted = isset($_POST['trust']);
  124. return doAuth($info, $trusted, true, @$_POST['idSelect']);
  125. }
  126. function action_idpage()
  127. {
  128. $identity = $_GET['user'];
  129. return idpage_render($identity);
  130. }
  131. function action_idpXrds()
  132. {
  133. return idpXrds_render();
  134. }
  135. function action_userXrds()
  136. {
  137. $identity = $_GET['user'];
  138. return userXrds_render($identity);
  139. }
  140. ?>