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