session.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. require_once "config.php";
  3. require_once "lib/render.php";
  4. require_once "Auth/OpenID/Server.php";
  5. /**
  6. * Set up the session
  7. */
  8. function init()
  9. {
  10. session_name('openid_server');
  11. session_start();
  12. }
  13. /**
  14. * Get the style markup
  15. */
  16. function getStyle()
  17. {
  18. $parent = rtrim(dirname(getServerURL()), '/');
  19. $url = htmlspecialchars($parent . '/openid-server.css', ENT_QUOTES);
  20. return sprintf('<link rel="stylesheet" type="text/css" href="%s" />', $url);
  21. }
  22. /**
  23. * Get the URL of the current script
  24. */
  25. function getServerURL()
  26. {
  27. $path = $_SERVER['SCRIPT_NAME'];
  28. $host = $_SERVER['HTTP_HOST'];
  29. $port = $_SERVER['SERVER_PORT'];
  30. $s = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 's' : '';
  31. if (($s && $port == "443") || (!$s && $port == "80")) {
  32. $p = '';
  33. } else {
  34. $p = ':' . $port;
  35. }
  36. return "http$s://$host$p$path";
  37. }
  38. /**
  39. * Build a URL to a server action
  40. */
  41. function buildURL($action=null, $escaped=true)
  42. {
  43. $url = getServerURL();
  44. if ($action) {
  45. $url .= '/' . $action;
  46. }
  47. return $escaped ? htmlspecialchars($url, ENT_QUOTES) : $url;
  48. }
  49. /**
  50. * Extract the current action from the request
  51. */
  52. function getAction()
  53. {
  54. $path_info = @$_SERVER['PATH_INFO'];
  55. $action = ($path_info) ? substr($path_info, 1) : '';
  56. $function_name = 'action_' . $action;
  57. return $function_name;
  58. }
  59. /**
  60. * Write the response to the request
  61. */
  62. function writeResponse($resp)
  63. {
  64. list ($headers, $body) = $resp;
  65. array_walk($headers, 'header');
  66. header(header_connection_close);
  67. print $body;
  68. }
  69. /**
  70. * Instantiate a new OpenID server object
  71. */
  72. function getServer()
  73. {
  74. static $server = null;
  75. if (!isset($server)) {
  76. $server = new Auth_OpenID_Server(getOpenIDStore(),
  77. buildURL());
  78. }
  79. return $server;
  80. }
  81. /**
  82. * Return a hashed form of the user's password
  83. */
  84. function hashPassword($password)
  85. {
  86. return bin2hex(Auth_OpenID_SHA1($password));
  87. }
  88. /**
  89. * Get the openid_url out of the cookie
  90. *
  91. * @return mixed $openid_url The URL that was stored in the cookie or
  92. * false if there is none present or if the cookie is bad.
  93. */
  94. function getLoggedInUser()
  95. {
  96. return isset($_SESSION['openid_url'])
  97. ? $_SESSION['openid_url']
  98. : false;
  99. }
  100. /**
  101. * Set the openid_url in the cookie
  102. *
  103. * @param mixed $identity_url The URL to set. If set to null, the
  104. * value will be unset.
  105. */
  106. function setLoggedInUser($identity_url=null)
  107. {
  108. if (!isset($identity_url)) {
  109. unset($_SESSION['openid_url']);
  110. } else {
  111. $_SESSION['openid_url'] = $identity_url;
  112. }
  113. }
  114. function getRequestInfo()
  115. {
  116. return isset($_SESSION['request'])
  117. ? unserialize($_SESSION['request'])
  118. : false;
  119. }
  120. function setRequestInfo($info=null)
  121. {
  122. if (!isset($info)) {
  123. unset($_SESSION['request']);
  124. } else {
  125. $_SESSION['request'] = serialize($info);
  126. }
  127. }
  128. function getSreg($identity)
  129. {
  130. // from config.php
  131. global $openid_sreg;
  132. if (!is_array($openid_sreg)) {
  133. return null;
  134. }
  135. return $openid_sreg[$identity];
  136. }
  137. function idURL($identity)
  138. {
  139. return buildURL('idpage') . "?user=" . $identity;
  140. }
  141. function idFromURL($url)
  142. {
  143. if (strpos($url, 'idpage') === false) {
  144. return null;
  145. }
  146. $parsed = parse_url($url);
  147. $q = $parsed['query'];
  148. $parts = array();
  149. parse_str($q, $parts);
  150. return @$parts['user'];
  151. }
  152. ?>