| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | namespace SimpleID\Protocols\XRDS; |
| 24: | |
| 25: | use \Prefab; |
| 26: | use SimpleID\Protocols\HTTPResponse; |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | class XRDSDiscovery extends Prefab { |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | public function discover($identifier, $openid = FALSE) { |
| 49: | $identifier = $this->normalize($identifier); |
| 50: | $url = $this->getURL($identifier); |
| 51: | |
| 52: | $xrds = $this->getXRDSDocument($url); |
| 53: | |
| 54: | if ($xrds) { |
| 55: | return $this->parseXRDS($xrds); |
| 56: | } else { |
| 57: | if ($openid) return $this->discoverByHTMLLinks($url); |
| 58: | return null; |
| 59: | } |
| 60: | } |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | public function discoverByHTMLLinks($url) { |
| 74: | $services = new XRDSServices(); |
| 75: | |
| 76: | $response = $this->request($url); |
| 77: | $html = $response->getBody(); |
| 78: | |
| 79: | $uri = $this->getLinkRel('openid2.provider', $html); |
| 80: | $delegate = $this->getLinkRel('openid2.local_id', $html); |
| 81: | |
| 82: | if ($uri) { |
| 83: | $service = [ |
| 84: | 'type' => [ 'http://specs.openid.net/auth/2.0/signon' ], |
| 85: | 'uri' => [ $uri ] |
| 86: | ]; |
| 87: | if ($delegate) $service['localid'] = $delegate; |
| 88: | $services->add($service, false); |
| 89: | } |
| 90: | |
| 91: | $uri = $this->getLinkRel('openid.server', $html); |
| 92: | $delegate = $this->getLinkRel('openid.delegate', $html); |
| 93: | |
| 94: | if ($uri) { |
| 95: | $service = [ |
| 96: | 'type' => [ 'http://openid.net/signon/1.0' ], |
| 97: | 'uri' => [ $uri ] |
| 98: | ]; |
| 99: | if ($delegate) $service['localid'] = $delegate; |
| 100: | $services->add($service, false); |
| 101: | } |
| 102: | |
| 103: | return $services; |
| 104: | } |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | protected function getXRDSDocument($url, $check = TRUE, $retries = 5) { |
| 117: | if ($retries == 0) return NULL; |
| 118: | |
| 119: | $response = $this->request($url, 'Accept: application/xrds+xml'); |
| 120: | |
| 121: | if ($response->isHTTPError()) return NULL; |
| 122: | if (($response->getHeader('Content-Type') == 'application/xrds+xml') || ($check == FALSE)) { |
| 123: | return $response->getBody(); |
| 124: | } elseif ($response->hasHeader('X-XRDS-Location')) { |
| 125: | return $this->getXRDSDocument($response->getHeader('X-XRDS-Location'), false, $retries - 1); |
| 126: | } else { |
| 127: | $location = $this->getMetaHttpEquiv('X-XRDS-Location', $response->getBody()); |
| 128: | if ($location) { |
| 129: | return $this->getXRDSDocument($location, false, $retries - 1); |
| 130: | } |
| 131: | return NULL; |
| 132: | } |
| 133: | } |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | protected function normalize($identifier) { |
| 146: | $normalized = $identifier; |
| 147: | |
| 148: | if ($this->isXRI($identifier)) { |
| 149: | if (stristr($identifier, 'xri://') !== false) $normalized = substr($identifier, 6); |
| 150: | } elseif ($this->isEmail($identifier)) { |
| 151: | if (stristr($identifier, 'acct:') !== false) $normalized = substr($identifier, 5); |
| 152: | if (stristr($identifier, 'mailto:') !== false) $normalized = substr($identifier, 7); |
| 153: | } else { |
| 154: | if (stristr($identifier, '://') === false) $normalized = 'http://'. $identifier; |
| 155: | if (substr_count($normalized, '/') < 3) $normalized .= '/'; |
| 156: | } |
| 157: | |
| 158: | return $normalized; |
| 159: | } |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | private function getURL($identifier) { |
| 169: | if ($this->isXRI($identifier)) { |
| 170: | return 'http://xri.net/' . $identifier; |
| 171: | } else { |
| 172: | return $identifier; |
| 173: | } |
| 174: | |
| 175: | } |
| 176: | |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | private function isXRI($identifier) { |
| 186: | $firstchar = substr($identifier, 0, 1); |
| 187: | if ($firstchar == "@" || $firstchar == "=" || $firstchar == "+" || $firstchar == "\$" || $firstchar == "!") return true; |
| 188: | if (stristr($identifier, 'xri://') !== FALSE) return true; |
| 189: | return false; |
| 190: | } |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | private function isEmail($identifier) { |
| 204: | |
| 205: | if (stristr($identifier, 'acct:') !== false) $identifier = substr($identifier, 5); |
| 206: | if (stristr($identifier, 'mailto:') !== false) $identifier = substr($identifier, 7); |
| 207: | |
| 208: | |
| 209: | if (strpos($identifier, "/") !== false) return false; |
| 210: | |
| 211: | $at = strpos($identifier, "@"); |
| 212: | |
| 213: | |
| 214: | if ($at === false) return false; |
| 215: | |
| 216: | |
| 217: | if (strrpos($identifier, "@") != $at) return false; |
| 218: | |
| 219: | return true; |
| 220: | } |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 229: | |
| 230: | protected function parseXRDS($xrds) { |
| 231: | $parser = new XRDSParser(); |
| 232: | $parser->load($xrds); |
| 233: | $services = $parser->parse(); |
| 234: | $parser->close(); |
| 235: | |
| 236: | return $services; |
| 237: | } |
| 238: | |
| 239: | |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: | protected function getMetaHttpEquiv($equiv, $html) { |
| 249: | $html = preg_replace('/<!(?:--(?:[^-]*|-[^-]+)*--\s*)>/', '', $html); |
| 250: | |
| 251: | $equiv = preg_quote($equiv); |
| 252: | preg_match('|<meta\s+http-equiv=["\']'. $equiv .'["\'](.*)/?>|iUs', $html, $matches); |
| 253: | if (isset($matches[1])) { |
| 254: | preg_match('|content=["\']([^"]+)["\']|iUs', $matches[1], $content); |
| 255: | if (isset($content[1])) { |
| 256: | return $content[1]; |
| 257: | } |
| 258: | } |
| 259: | return FALSE; |
| 260: | } |
| 261: | |
| 262: | |
| 263: | |
| 264: | |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: | |
| 270: | |
| 271: | protected function getLinkRel($rel, $html) { |
| 272: | $html = preg_replace('/<!(?:--(?:[^-]*|-[^-]+)*--\s*)>/s', '', $html); |
| 273: | |
| 274: | $rel = preg_quote($rel); |
| 275: | preg_match('|<link\s+rel=["\'](.*)'. $rel .'(.*)["\'](.*)/?>|iUs', $html, $matches); |
| 276: | if (isset($matches[3])) { |
| 277: | preg_match('|href=["\']([^"]+)["\']|iU', $matches[3], $href, PREG_UNMATCHED_AS_NULL); |
| 278: | if (isset($href[1])) return trim($href[1]); |
| 279: | } |
| 280: | return FALSE; |
| 281: | } |
| 282: | |
| 283: | |
| 284: | |
| 285: | |
| 286: | |
| 287: | |
| 288: | |
| 289: | |
| 290: | |
| 291: | |
| 292: | |
| 293: | |
| 294: | |
| 295: | |
| 296: | |
| 297: | protected function request($url, $headers = '') { |
| 298: | $web = \Web::instance(); |
| 299: | $result = $web->request($url, [ 'header' => $headers ]); |
| 300: | return new HTTPResponse($result); |
| 301: | } |
| 302: | } |
| 303: | |
| 304: | ?> |
| 305: | |