This repository has been archived by the owner on Feb 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuth.class.php
106 lines (84 loc) · 3.01 KB
/
Auth.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
class Auth
{
private $username;
private $password;
private $config;
private $requestTokenPair = array();
private $accessTokenPair = array();
public function __construct($username, $password, Config $config)
{
$this->username = trim($username);
$this->password = trim($password);
$this->config = $config;
}
public function getAccessToken()
{
$auth = new WeiboOAuth($this->config->getParameter('key'),
$this->config->getParameter('secret'));
$this->requestTokenPair = $auth->getRequestToken();
$aurl = $auth->getAuthorizeURL($this->requestTokenPair['oauth_token'], false, 'oob');
//echo $aurl, "\n";
$re = @file_get_contents($aurl);
$doc = new DOMDocument();
@$doc->loadHTML($re);
//echo $doc->saveHTML();
$domNodeList = $doc->getElementsByTagName('form');
$node = $domNodeList->item(0);
$nodeMap = $node->attributes;
if ($nodeMap->getNamedItem('name')->nodeValue == 'authZForm')
{
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0\r\n'
/* .'Content-type: application/x-www-form-urlencoded\r\n' */,
'content' => http_build_query(array(
'action' => 'submit',
'forcelogin' => '',
'from' => '',
'oauth_callback' => 'oob',
'oauth_token' => $this->requestTokenPair['oauth_token'],
'userId' => $this->username,
'passwd' => $this->password,
'regCallback' => 'http://api.t.sina.com.cn/oauth/authorize?
oauth_token='.$this->requestTokenPair['oauth_token']
.'&oauth_callback=oob
&from=
&with_cookie='
)),
'timeout' => 5
)
);
$context = stream_context_create($opts);
$ret = @file_get_contents('http://api.t.sina.com.cn/oauth/authorize', false, $context);
}
else
{
$ret = @file_get_contents('http://api.t.sina.com.cn/oauth/authorize?oauth_token='.$this->requestTokenPair['oauth_token'].'&oauth_callback=oob');
}
$o = new WeiboOAuth( $this->config->getParameter('key'),
$this->config->getParameter('secret'),
$this->requestTokenPair['oauth_token'],
$this->requestTokenPair['oauth_token_secret']);
$this->accessTokenPair = $o->getAccessToken($this->getPIN($ret));
return $this->accessTokenPair;
}
private function getPIN($html)
{
$doc = new DOMDocument();
@$doc->loadHTML($html);
$domNodeList = $doc->getElementsByTagName('span');
if (!$domNodeList->length)
{
return null;
}
$node = $domNodeList->item(0);
$pin = trim($node->textContent);
if (empty($pin))
{
return null;
}
return $pin;
}
}