]> git.wh0rd.org - tt-rss.git/blame - twitter.php
add twitter register/callback server
[tt-rss.git] / twitter.php
CommitLineData
ff3e303a
AD
1<?php
2 require_once "functions.php";
3 require_once "sessions.php";
4 require_once "sanity_check.php";
5 require_once "config.php";
6 require_once "db.php";
7 require_once "lib/twitteroauth/twitteroauth.php";
8
9 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
10
11 init_connection($link);
12 login_sequence($link);
13
14 $owner_uid = $_SESSION["uid"];
15 $op = $_REQUEST['op'];
16
17 if (!SINGLE_USER_MODE && !$_SESSION['uid']) {
18 render_login_form($link);
19 exit;
20 }
21
22 $callback_url = get_self_url_prefix() . "/twitter.php?op=callback";
23
24 if ($op == 'clear') {
25 /* Remove no longer needed request tokens */
26 unset($_SESSION['oauth_token']);
27 unset($_SESSION['oauth_token_secret']);
28 unset($_SESSION['access_token']);
29
30 header("Location: twitter.php");
31 return;
32 }
33
34 if ($op == 'callback') {
35 /* If the oauth_token is old redirect to the connect page. */
36 if (isset($_REQUEST['oauth_token']) &&
37 $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
38
39 $_SESSION['oauth_status'] = 'oldtoken';
40 header('Location: twitter.php?op=clear');
41 return;
42 }
43
44 /* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
45 $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
46
47 /* Request access tokens from twitter */
48 $access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
49
50 /* If HTTP response is 200 continue otherwise send to connect page to retry */
51 if ($connection->http_code == 200) {
52 $access_token = db_escape_string(json_encode($access_token));
53
54 db_query($link, "UPDATE ttrss_users SET twitter_oauth = '$access_token'
55 WHERE id = ".$_SESSION['uid']);
56
57 } else {
58 header('Location: twitter.php?op=clear');
59 return;
60 }
61
62 }
63
64 if ($op == 'register') {
65
66 /* Build TwitterOAuth object with client credentials. */
67 $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
68
69 /* Get temporary credentials. */
70 $request_token = $connection->getRequestToken($callback_url);
71
72 /* Save temporary credentials to session. */
73 $_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
74 $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
75
76 if ($connection->http_code == 200) {
77 $url = $connection->getAuthorizeURL($token);
78 header('Location: ' . $url);
79 return;
80 }
81 }
82?>
83
84<html>
85<head>
86<title>Register with Twitter</title>
87<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
88<link rel="stylesheet" type="text/css" href="utility.css">
89</head>
90
91<body>
92
93<h1>Register with Twitter</h1>
94
95<?php if ($op == 'register') { ?>
96
97<p><?php print_error('Could not connect to Twitter. Refresh the page or try again later.') ?></p>
98
99<?php } else if ($op == 'callback') { ?>
100
101 <?php print_notice('Congratulations! You have successfully registered with Twitter.') ?>
102 </p>
103
104 <form method="GET" action="prefs.php">
105 <input type="hidden" name="tab" value="feedConfig">
106 <button type="submit">Return to Tiny Tiny RSS</button>
107 </form>
108
109<?php } else { ?>
110 <form method="GET" action="twitter.php">
111 <input type="hidden" name="op" value="register">
112 <button type="submit">Register with Twitter</button>
113 </form>
114
115<?php } ?>
116
117
118</body>
119</html>