]> git.wh0rd.org - tt-rss.git/blob - twitter.php
add Public_Handler
[tt-rss.git] / twitter.php
1 <?php
2 set_include_path(get_include_path() . PATH_SEPARATOR . "include");
3
4 require_once "functions.php";
5 require_once "sessions.php";
6 require_once "sanity_check.php";
7 require_once "config.php";
8 require_once "db.php";
9 //require_once "lib/twitteroauth/twitteroauth.php";
10 require_once "lib/tmhoauth/tmhOAuth.php";
11
12 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
13
14 if (!init_connection($link)) return;
15 login_sequence($link);
16
17 $owner_uid = $_SESSION["uid"];
18 $op = $_REQUEST['op'];
19
20 if (!SINGLE_USER_MODE && !$_SESSION['uid']) {
21 render_login_form($link);
22 exit;
23 }
24
25 $callback_url = get_self_url_prefix() . "/twitter.php?op=callback";
26
27 $tmhOAuth = new tmhOAuth(array(
28 'consumer_key' => CONSUMER_KEY,
29 'consumer_secret' => CONSUMER_SECRET,
30 ));
31
32 if ($op == 'clear') {
33 unset($_SESSION['oauth']);
34
35 header("Location: twitter.php");
36 return;
37 }
38
39 if (isset($_REQUEST['oauth_verifier'])) {
40
41 $op = 'callback';
42
43 $tmhOAuth->config['user_token'] = $_SESSION['oauth']['oauth_token'];
44 $tmhOAuth->config['user_secret'] = $_SESSION['oauth']['oauth_token_secret'];
45
46 $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
47 'oauth_verifier' => $_REQUEST['oauth_verifier']));
48
49 if ($code == 200) {
50
51 $access_token = json_encode($tmhOAuth->extract_params($tmhOAuth->response['response']));
52
53 unset($_SESSION['oauth']);
54
55 db_query($link, "UPDATE ttrss_users SET twitter_oauth = '$access_token'
56 WHERE id = ".$_SESSION['uid']);
57
58 } else {
59 header('Location: twitter.php?op=clear');
60 return;
61 }
62
63 }
64
65 if ($op == 'register') {
66
67 $code = $tmhOAuth->request('POST',
68 $tmhOAuth->url('oauth/request_token', ''), array(
69 'oauth_callback' => $callback));
70
71 if ($code == 200) {
72 $_SESSION['oauth'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
73
74 $method = isset($_REQUEST['signin']) ? 'authenticate' : 'authorize';
75 $force = isset($_REQUEST['force']) ? '&force_login=1' : '';
76 $forcewrite = isset($_REQUEST['force_write']) ? '&oauth_access_type=write' : '';
77 $forceread = isset($_REQUEST['force_read']) ? '&oauth_access_type=read' : '';
78
79 $location = $tmhOAuth->url("oauth/{$method}", '') .
80 "?oauth_token={$_SESSION['oauth']['oauth_token']}{$force}{$forcewrite}{$forceread}";
81
82 header("Location: $location");
83
84 }
85 }
86 ?>
87
88 <html>
89 <head>
90 <title>Register with Twitter</title>
91 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
92 <link rel="stylesheet" type="text/css" href="utility.css">
93 </head>
94
95 <body>
96
97 <h1><?php echo __('Register with Twitter') ?></h1>
98
99 <?php if ($op == 'register') { ?>
100
101 <p><?php print_error(__('Could not connect to Twitter. Refresh the page or try again later.')) ?></p>
102
103 <?php } else if ($op == 'callback') { ?>
104
105 <p><?php print_notice(__('Congratulations! You have successfully registered with Twitter.')) ?>
106 </p>
107
108 <form method="GET" action="prefs.php">
109 <input type="hidden" name="tab" value="feedConfig">
110 <button type="submit"><?php echo __('Return to Tiny Tiny RSS') ?></button>
111 </form>
112
113 <?php } else { ?>
114
115 <form method="GET" action="twitter.php" style='display : inline'>
116 <input type="hidden" name="op" value="register">
117 <button type="submit"><?php echo __('Register') ?></button>
118 </form>
119
120 <form method="GET" action="prefs.php" style='display : inline'>
121 <input type="hidden" name="tab" value="feedConfig">
122 <button type="submit"><?php echo __('Return to Tiny Tiny RSS') ?></button>
123 </form>
124
125 <?php } ?>
126
127
128 </body>
129 </html>