forked from sbwtw/TBsign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetCookie.action.php
executable file
·218 lines (181 loc) · 5.13 KB
/
getCookie.action.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
require_once 'CURL.class.php';
class Cookie{
private $cookie = null;
function setCookie($str){
if (preg_match_all('/Set-Cookie: ([^=]+)=([^;]+)/',$str,$what)){
foreach ($what[1] as $key => $val){
$this->cookie[$what[1][$key]] = $what[2][$key];
}
}
}
// 专用于第二步
function setCookie2($str){
if (preg_match_all('/(\w+?)=([^;]+)/',$str,$what)){
foreach ($what[1] as $key => $val){
$this->cookie[$what[1][$key]] = $what[2][$key];
}
}
}
function getAll(){
$res = '';
foreach ($this->cookie as $key => $val){
$res .= $key . '=' . $val . '; ';
}
return $res;
}
function getCookie($list){
$res = '';
foreach ($list as $key){
$res .= $key . '=' . $this->cookie[$key] . '; ';
}
return $res;
}
}
class Work{
private $config;
private $args;
private $cookie;
private $curl;
private $returns;
function __construct(){
// 加载配置文件
$this->config = require 'getCookieConfig.include.php';
// 加载参数
foreach ($this->config['args'] as $arg){
if (isset($_POST[$arg])){
$this->args[$arg] = $_POST[$arg];
}
}
// 验证参数
if (!isset($this->args['userName']) || !isset($this->args['password'])){
$this->error('参数不足!');
}
// 不在第二步时将验证信息清空
if (!isset($this->args['step']) || $this->args['step'] != 2){
$this->args['verifyAddress'] = '';
$this->args['verifyCode'] = '';
}
// 实例化类
$this->cookie = new Cookie();
$this->curl = new CURL();
}
// 检查是否需要验证码
function needVerify($token){
$this->curl->setUrl(sprintf($this->config['verifyUrl'],$token,$this->args['userName']));
$this->curl->setCookie($this->cookie->getAll());
$data = $this->curl->execute();
$this->cookie->setCookie($data);
if (preg_match('/(captchaservice\w{200,})/',$data,$what)){
return $what[1];
} else {
return false;
}
}
// 保存到数据库
function save(){
require_once 'dataBase.class.php';
$db = new DataBase();
$db->table('user');
$db->data(array(array('password' => md5($this->args['userName'] . md5($this->returns['password'])),'name' => $this->args['userName'],'cookie' => $this->returns['cookie'])));
return $db->insert();
}
// 登陆函数
function login(){
$data['apiver']='v3';
$data['callback']='parent.bd__pcbs__sbw';
$data['charset']='UTF-8';
$data['codestring']=$this->args['verifyAddress'];
$data['isPhone']='false';
$data['mem_pass']='on';
$data['password']=$this->args['password'];
$data['ppui_logintime']='29876';
$data['safelog']='0';
$data['staticpage']='http://tieba.baidu.com/tb/static-common/html/pass/v3Jump.html';
$data['token']=$this->args['token'];
$data['tpl']='tb';
$data['tt'] = time() . '520';
$data['u']='http://tieba.baidu.com/#';
$data['usernamelogin']='1';
$data['username']=$this->args['userName'];
$data['verifycode']=$this->args['verifyCode'];
$this->curl->setUrl($this->config['loginUrl']);
$this->curl->setPost($data);
$this->curl->setCookie($this->cookie->getAll());
$res = $this->curl->execute();
if (preg_match('/err_no=(\d+)/',$res,$what)){
if ($what[1]){
$this->error('验证错误',$what[1]);
} else {
// 登陆成功,获得最终cookie
$this->cookie->setCookie($res);
$this->curl->setUrl($this->config['finalUrl']);
$this->curl->setCookie($this->cookie->getAll());
$res = $this->curl->execute();
// 成功
$this->cookie->setCookie($res);
$this->returns['cookie'] = $this->cookie->getCookie(array('BAIDUID','BDUSS','TIEBAUID'));
$this->returns['password'] = $this->args['password'];
if ($this->save()){
$this->success('ok');
} else {
$this->error('添加帐号失败',-2);
}
}
} else {
$this->error('未知错误',-1);
}
}
// 启动函数
function init(){
if (!isset($this->args['step']) || $this->args['step'] == 1){
// 第一步
// 第一次,获得BAIDUID
$this->curl->setUrl($this->config['tokenUrl']);
$data = $this->curl->execute();
$this->cookie->setCookie($data);
// 第二次,获得token
$this->curl->setUrl($this->config['tokenUrl']);
$this->curl->setCookie($this->cookie->getAll());
$data = $this->curl->execute();
$this->cookie->setCookie($data);
if (preg_match('/"token"\s+:\s+"(\w+)"/',$data,$what)){
$token = $what[1];
} else {
$this->error('get token error');
}
$verify = $this->needVerify($token);
if ($verify){
// 需要验证码
$this->returns['cookie'] = $this->cookie->getAll();
$this->returns['verifyAddress'] = $verify;
$this->returns['token'] = $token;
$this->success('next');
} else {
// 直接登陆,要传递一个token
$this->args['token'] = $token;
$this->login();
}
} else {
// 第二步,直接登陆
$this->cookie->setCookie2($_POST['cookie']);
$this->login();
}
}
// 成功
function success($str=''){
$this->returns['result'] = 1;
$this->returns['info'] = $str;
echo json_encode($this->returns);
exit(0);
}
// 出错
function error($str = '',$code = 0){
$this->returns['result'] = $code;
$this->returns['info'] = $str;
die(json_encode($this->returns));
}
}
$work = new Work();
$work->init();
?>