PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/modules/user/extlib/openid/urlbuilder.inc.php

https://github.com/jvinet/pronto
PHP | 189 lines | 123 code | 37 blank | 29 comment | 25 complexity | d7a719c5e71778ec3cc15ad7e891c226 MD5 | raw file
  1. <?PHP
  2. /* Poidsy 0.4 - http://chris.smith.name/projects/poidsy
  3. * Copyright (c) 2008 Chris Smith
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. require_once(dirname(__FILE__) . '/keymanager.inc.php');
  24. class URLBuilder {
  25. const NAMESPACE = 'http://openid.net/signon/1.1';
  26. public static function addArguments($base, $arguments) {
  27. $first = true;
  28. $res = $base === false ? '' : $base;
  29. if ($base !== false && strrpos($base, '?', -2) === false) {
  30. if ($base[strlen($base) - 1] != '?') {
  31. $res .= '?';
  32. }
  33. } else if ($base !== false) {
  34. $res .= '&';
  35. }
  36. foreach ($arguments as $key => $value) {
  37. if ($first) {
  38. $first = false;
  39. } else {
  40. $res .= '&';
  41. }
  42. $res .= urlencode($key) . '=' . urlencode($value);
  43. }
  44. return $res;
  45. }
  46. public static function buildRequest($type, $base, $delegate, $identity, $returnURL, $handle) {
  47. $args = array(
  48. 'openid.ns' => self::NAMESPACE,
  49. 'openid.mode' => 'checkid_' . $type,
  50. 'openid.identity' => $delegate,
  51. 'openid.claimed_id' => $identity,
  52. 'openid.trust_root' => self::getTrustRoot(),
  53. 'openid.return_to' => self::addArguments($returnURL,
  54. array('openid.nonce' => $_SESSION['openid']['nonce']))
  55. );
  56. if ($handle !== null) {
  57. $args['openid.assoc_handle'] = $handle;
  58. }
  59. self::addSRegArgs($args);
  60. return self::addArguments($base, $args);
  61. }
  62. private static function getTrustRoot() {
  63. if (defined('OPENID_TRUSTROOT')) {
  64. return OPENID_TRUSTROOT;
  65. } else {
  66. return self::getCurrentURL();
  67. }
  68. }
  69. private static function addSRegArgs(&$args) {
  70. if (defined('OPENID_SREG_REQUEST')) {
  71. $args['openid.sreg.required'] = OPENID_SREG_REQUEST;
  72. }
  73. if (defined('OPENID_SREG_OPTIONAL')) {
  74. $args['openid.sreg.optional'] = OPENID_SREG_OPTIONAL;
  75. }
  76. if (defined('OPENID_SREG_POLICY')) {
  77. $args['openid.sreg.policy_url'] = OPENID_SREG_POLICY;
  78. }
  79. }
  80. public static function buildAssociate($server) {
  81. $args = array(
  82. 'openid.ns' => self::NAMESPACE,
  83. 'openid.mode' => 'associate',
  84. 'openid.assoc_type' => 'HMAC-SHA1',
  85. );
  86. if (KeyManager::supportsDH()) {
  87. $args['openid.session_type'] = 'DH-SHA1';
  88. $args['openid.dh_modulus'] = KeyManager::getDhModulus();
  89. $args['openid.dh_gen'] = KeyManager::getDhGen();
  90. $args['openid.dh_consumer_public'] = KeyManager::getDhPublicKey($server);
  91. } else {
  92. $args['openid.session_type'] = '';
  93. }
  94. return self::addArguments(false, $args);
  95. }
  96. public static function buildAuth($params) {
  97. $args = array(
  98. 'openid.ns' => self::NAMESPACE,
  99. 'openid.mode' => 'check_authentication'
  100. );
  101. $toadd = array('assoc_handle', 'sig', 'signed');
  102. $toadd = array_merge($toadd, explode(',', $params['openid_signed']));
  103. foreach ($toadd as $arg) {
  104. if (!isset($args['openid.' . $arg])) {
  105. $args['openid.' . $arg] = $params['openid_' . $arg];
  106. }
  107. }
  108. return self::addArguments(false, $args);
  109. }
  110. public static function getCurrentURL() {
  111. $res = 'http';
  112. if (isset($_SERVER['HTTPS'])) {
  113. $res = 'https';
  114. }
  115. $res .= '://' . $_SERVER['SERVER_NAME'];
  116. if ($_SERVER['SERVER_PORT'] != 80) {
  117. $res .= ':' . $_SERVER['SERVER_PORT'];
  118. }
  119. $url = $_SERVER['REQUEST_URI'];
  120. while (preg_match('/([\?&])openid[\._](.*?)=(.*?)(&|$)/', $url, $m)) {
  121. $url = str_replace($m[0], $m[1], $url);
  122. }
  123. $url = preg_replace('/\??&*$/', '', $url);
  124. return $res . $url;
  125. }
  126. /**
  127. * Redirects the user back to their original page.
  128. */
  129. public static function redirect() {
  130. if (defined('OPENID_REDIRECTURL')) {
  131. $url = OPENID_REDIRECTURL;
  132. } else if (isset($_SESSION['openid']['redirect'])) {
  133. $url = $_SESSION['openid']['redirect'];
  134. } else {
  135. $url = self::getCurrentURL();
  136. }
  137. self::doRedirect($url);
  138. }
  139. /**
  140. * Redirects the user to the specified URL.
  141. *
  142. * @param $url The URL to redirect the user to
  143. */
  144. public static function doRedirect($url) {
  145. header('Location: ' . $url);
  146. echo '<html><head><title>Redirecting</title></head><body>';
  147. echo '<p>Redirecting to <a href="', htmlentities($url), '">';
  148. echo htmlentities($url), '</a></p></body></html>';
  149. exit();
  150. }
  151. }
  152. ?>