-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoauth.php
163 lines (150 loc) · 6.86 KB
/
oauth.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
<?php
session_start();
require('include/twitteroauth.php');
require('config.php');
if (isset($_POST['url_suffix'])) {
$_SESSION['url_suffix'] = preg_replace('/[^a-zA-Z0-9]/', '', $_POST['url_suffix']);
}
if (!empty($_POST)) {
$_SESSION['oauth_proxy'] = array(
'username' => $_POST['username'],
'password' => $_POST['password'],
);
$_SESSION['userapikey'] = $_POST['userapikey'];
$_SESSION['userapisecret'] = $_POST['userapisecret'];
if ($_SESSION['userapikey'] == '' || $_SESSION['userapisecret'] == '') {
$_SESSION['userapikey'] = OAUTH_KEY;
$_SESSION['userapisecret'] = OAUTH_SECRET;
}
$connection = new TwitterOAuth($_SESSION['userapikey'], $_SESSION['userapisecret']);
$request_token = $connection->getRequestToken(BASE_URL . 'oauth.php');
/* Save request token to session */
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
switch ($connection->http_code) {
case 200:
/* Build authorize URL */
$url = $connection->getAuthorizeURL($_SESSION['oauth_token'], FALSE);
$_SESSION['oauth_proxy']['url'] = $url;
// encode user and password for decode.
header('HTTP/1.1 302 Found');
header('Status: 302 Found');
header('Location: oauth_proxy.php');
break;
default:
echo 'Could not connect to Twitter. Refresh the page or try again later.';
echo "\n Error code:" . $connection->http_code . ".";
if ($connection->http_code == 0) {
echo "Don't report bugs or issues if you got this error code. Twitter is not accessible on this host. Perhaps the hosting company blocked Twitter.";
}
break;
}
exit();
}
if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) {
$connection = new TwitterOAuth($_SESSION['userapikey'], $_SESSION['userapisecret'], $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$access_token = $connection->getAccessToken($_GET['oauth_verifier']);
$access_token['userapikey'] = $_SESSION['userapikey'];
$access_token['userapisecret'] = $_SESSION['userapisecret'];
if ($connection->http_code == 200) {
if (INVITEMODE) {
$allowed_uids = file("allowed_uids");
for ($i = 0; $i < count($allowed_uids); $i++) {
$allowed_uids[$i] = rtrim($allowed_uids[$i]);
}
if (!in_array($access_token['user_id'], $allowed_uids)) {
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Twip4 Override Mode"');
echo 'This user is not allowed to use the api proxy.' . "\n";
exit();
}
}
$old_tokens = glob('oauth/*.' . $access_token['user_id']);
if (!empty($old_tokens)) {
foreach ($old_tokens as $file) {
unlink($file);
}
}
if ($_SESSION['url_suffix'] == '') {
for ($i = 0; $i < 6; $i++) {
$d = rand(1, 30) % 2;
$suffix_string .= $d ? chr(rand(65, 90)) : chr(rand(48, 57));
}
} else {
$suffix_string = $_SESSION['url_suffix'];
}
if (file_put_contents('oauth/' . $suffix_string . '.' . $access_token['user_id'], serialize($access_token)) === FALSE) {
echo 'Error failed to write access_token file.Please check if you have write permission to oauth/ directory' . "\n";
exit();
}
$url = BASE_URL . 'o/' . $suffix_string;
header('HTTP/1.1 302 Found');
header('Status: 302 Found');
header('Location: getapi.php?api=' . $url);
} else {
echo 'Error ' . $connection->http_code . "\n";
print_r($connection);
}
exit();
}
?>
<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Twip 4 - Configuration</title>
<link rel="stylesheet" type="text/css" href="style.css" media="all" />
<meta name="robots" content="noindex,nofollow">
</head>
<body>
<script type="text/javascript">
function checkValid() {
username=document.mainform.username.value;
password=document.mainform.password.value;
userapikey=document.mainform.userapikey.value;
userapisecret=document.mainform.userapisecret.value;
if (username.length==0 || password.length==0) {
alert("Both username and password are needed.");
return false;
}
if (userapikey.length==0 ^ userapisecret.length==0) {
alert("Please completely fill your api consumer token or leave them blank.");
return false;
}
return true;
}
</script>
<h1>Twip<sup title="Version 4">4</sup></h1>
<h2>Twitter API Proxy, redefined.</h2>
<div>
<form action="" method="post" onSubmit="return checkValid()" name="mainform">
<p>
<label for="url_suffix">1. Preferred URL<span class="small"></span></label>
<input class="half" type="text" value="<?php echo BASE_URL . 'o/'; ?>" id="base_url" disabled autocomplete="off" />
<input class="half" type="text" value="" id="url_suffix" name="url_suffix" autocomplete="off" />
</p>
<p>
<label for="username">2. Twitter Username<span class="small">REQUIRED*</span></label>
<input type="text" value="" id="username" name="username" autocomplete="off" />
</p>
<p>
<label for="password">3. Twitter Password<span class="small">REQUIRED*</span></label>
<input type="password" value="" id="password" name="password" autocomplete="off" />
</p>
<p>
<label for="userapikey">4. API Key<span class="small">Left it empty or use your API Key</span></label>
<input type="text" value="" id="userapikey" name="userapikey" autocomplete="off" />
</p>
<p>
<label for="userapisecret">5. API Secret<span class="small">Left it empty or use your API Secret</span></label>
<input type="text" value="" id="userapisecret" name="userapisecret" autocomplete="off" />
</p>
<p>If you use your own API Key & Secret, lease set the callback url of your twitter app to <?php echo BASE_URL; ?></p>
<div id="submit"><input type="submit" value="Authorization" /></div>
</form>
</div>
<div id="footer">
2013 © <a href="http://code.google.com/p/twip/">Twip Project</a>
</div>
</body>
</html>