forked from yegle/twip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth_proxy.php
More file actions
112 lines (108 loc) · 3.82 KB
/
oauth_proxy.php
File metadata and controls
112 lines (108 loc) · 3.82 KB
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
<?php
session_start();
/*
* @author tifan
*/
include('include/simple_html_dom.php');
/* Credit: */
if(isset($_SESSION['oauth_proxy'])){
$oAuthEntryPage = $_SESSION['oauth_proxy']['url'];
$twitterAccount = $_SESSION['oauth_proxy']['username'];
$twitterPassword = $_SESSION['oauth_proxy']['password'];
}
else{
header("Location: oauth.php");
exit();
}
/* form: https://twitter.com/oauth/authenticate
authenticity_token -> 抓
oauth_token -> 抓
session[username_or_email] -> twitterAccount
session[password] -> twitterPassword
*/
/* After this page, we should validate the returning page.
Statud 403 -> No longer valid / wrong password.
Status 200 ->
if (contain_Allow) {
post_allow;
get_oauth_strings;
post_oauth_strings_to_oauth.php;
} else {
get_oauth_strings;
...
}
*/
$page_auth = file_get_html($oAuthEntryPage);
if($page_auth === FALSE){
echo "Cannot load http resource using file_get_contents";
exit();
}
$oauth_token = $page_auth->find('input[name=oauth_token]', 0)->attr['value'];
$authenticity_token = $page_auth->find('input[name=authenticity_token]', 0)->attr['value'];
$login_fields = Array(
'oauth_token' => urlencode($oauth_token),
'authenticity_token' => urlencode($authenticity_token),
'session[username_or_email]' => urlencode($twitterAccount),
'session[password]' => urlencode($twitterPassword)
);
foreach($login_fields as $key=>$value) {
$login_string .= $key.'='.$value.'&';
}
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.twitter.com/oauth/authorize');
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, count($login_fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $login_string);
$login_result = curl_exec($ch);
curl_close($ch);
$login_obj = str_get_html($login_result);
$login_error = $login_obj->find('div[class=error notice] p', 0)->innertext;
if(strlen($login_error) > 8) {
/* This is a workaround coz oauth_errors can be " " */
echo "There must be something wrong with your user account and password combination.<br/>";
echo "Twitter said: <b>$login_error</b>\n";
die(-1);
}
/*
// Now, see if we have to manually approve this request.
$oauth_approve_form = $login_obj->find('form[id=login_form]', 0);
if($oauth_approve_form) {
echo "We have to manually approve this request.\r";
$newAToken = $login_obj->find('input[name=authenticity_token]', 0)->attr['value'];
$newOToken = $login_obj->find('input[name=oauth_token]', 0)->attr['value'];
echo ">>>New Request<<<\r";
echo "oauth_token->$oauth_token\rauthenticity_token->$authenticity_token\r";
$oauth_fields = Array(
'oauth_token' => urlencode($newOToken),
'authenticity_token' => urlencode($newAToken),
'submit' => 'Allow'
);
foreach($oauth_fields as $key=>$value) {
$oauth_strings .= $key.'='.$value.'&';
}
//echo $login_result;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://twitter.com/oauth/authorize');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_POST, count($oauth_fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $oauth_strings);
$oauthResult = curl_exec($ch);
curl_close($ch);
echo $oauthResult;
$oauthObject = str_get_html($oauthResult);
$targetURL = $oauthObject->find('div[class=message-content] a', 0)->href;
echo "Click <a href='$targetURL'>here</a> to continue.";
} else {
$targetURL = $login_obj->find('div[class=message-content] a', 0)->href;
echo "Click <a href='$targetURL'>here</a> to continue.";
}
*/
$targetURL = $login_obj->find('div[class=happy notice callback] a', 0)->href;
header('HTTP/1.1 302 Found');
header('Status: 302 Found');
header("Location: $targetURL");
echo "Please click <a href='$targetURL'>here</a> to continue.";
?>