html缓存openid,OpenID发现方法 - Yadis VS HTML

function discover($url)

{

if(!$url) throw new ErrorException('No identity supplied.');

# We save the original url in case of Yadis discovery failure.

# It can happen when we'll be lead to an XRDS document

# which does not have any OpenID2 services.

$originalUrl = $url;

# A flag to disable yadis discovery in case of failure in headers.

$yadis = true;

# We'll jump a maximum of 5 times, to avoid endless redirections.

for($i = 0; $i < 5; $i ++) {

if($yadis) {

$headers = explode("n",$this->request($url, 'HEAD'));

$next = false;

foreach($headers as $header) {

if(preg_match('#X-XRDS-Locations*:s*(.*)#', $header, $m)) {

$url = $this->build_url(parse_url($url), parse_url(trim($m[1])));

$next = true;

}

if(preg_match('#Content-Types*:s*application/xrds+xml#i', $header)) {

# Found an XRDS document, now let's find the server, and optionally delegate.

$content = $this->request($url, 'GET');

# OpenID 2

# We ignore it for MyOpenID, as it breaks sreg if using OpenID 2.0

$ns = preg_quote('http://specs.openid.net/auth/2.0/');

if (preg_match('#(.*)s*'.$ns.'(.*?)s*(.*)#s', $content, $m)

&& !preg_match('/myopenid.com/i', $this->identity)) {

$content = $m[1] . $m[3];

if($m[2] == 'server') $this->identifier_select = true;

$content = preg_match('#(.*)#', $content, $server);

$content = preg_match('#(.*)#', $content, $delegate);

if(empty($server)) {

return false;

}

# Does the server advertise support for either AX or SREG?

$this->ax = preg_match('#http://openid.net/srv/ax/1.0#', $content);

$this->sreg = preg_match('#http://openid.net/sreg/1.0#', $content);

$server = $server[1];

if(isset($delegate[1])) $this->identity = $delegate[1];

$this->version = 2;

$this->server = $server;

return $server;

}

# OpenID 1.1

$ns = preg_quote('http://openid.net/signon/1.1');

if(preg_match('#(.*)s*'.$ns.'s*(.*)#s', $content, $m)) {

$content = $m[1] . $m[2];

$content = preg_match('#(.*)#', $content, $server);

$content = preg_match('#<.>(.*)#', $content, $delegate);

if(empty($server)) {

return false;

}

# AX can be used only with OpenID 2.0, so checking only SREG

$this->sreg = preg_match('#http://openid.net/sreg/1.0#', $content);

$server = $server[1];

if(isset($delegate[1])) $this->identity = $delegate[1];

$this->version = 1;

$this->server = $server;

return $server;

}

$next = true;

$yadis = false;

$url = $originalUrl;

$content = null;

break;

}

}

if($next) continue;

# There are no relevant information in headers, so we search the body.

$content = $this->request($url, 'GET');

if($location = $this->htmlTag($content, 'meta', 'http-equiv', 'X-XRDS-Location', 'value')) {

$url = $this->build_url(parse_url($url), parse_url($location));

continue;

}

}

if(!$content) $content = $this->request($url, 'GET');

# At this point, the YADIS Discovery has failed, so we'll switch

# to openid2 HTML discovery, then fallback to openid 1.1 discovery.

$server = $this->htmlTag($content, 'link', 'rel', 'openid2.provider', 'href');

$delegate = $this->htmlTag($content, 'link', 'rel', 'openid2.local_id', 'href');

$this->version = 2;

# Another hack for myopenid.com...

if(preg_match('/myopenid.com/i', $server)) {

$server = null;

}

if(!$server) {

# The same with openid 1.1

$server = $this->htmlTag($content, 'link', 'rel', 'openid.server', 'href');

$delegate = $this->htmlTag($content, 'link', 'rel', 'openid.delegate', 'href');

$this->version = 1;

}

if($server) {

# We found an OpenID2 OP Endpoint

if($delegate) {

# We have also found an OP-Local ID.

$this->identity = $delegate;

}

$this->server = $server;

return $server;

}

throw new ErrorException('No servers found!');

}

throw new ErrorException('Endless redirection!');

}

[1]: http://gitorious.org/lightopenid

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值