OSDN Git Service

embrj
[embrj/master.git] / oauth.php
1 <?php
2         /* Start session and load lib */
3         if(!isset($_SESSION)){
4                 session_start();
5         }
6         include_once('lib/twitese.php');
7         foreach ($AUTH_ID as &$id) {
8                 $id = strtoupper($id);
9         }
10         if (isset($_REQUEST['oauth_token'])) {
11                 if($_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
12                         $_SESSION['oauth_status'] = 'oldtoken';
13                         session_destroy();
14                         header('Location: login.php?oauth=old');exit();
15                 }else{
16                         /* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
17                         $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
18
19                         /* Request access tokens from twitter */
20                         $access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
21
22                         /* Save the access tokens. Normally these would be saved in a database for future use. */
23                         $_SESSION['access_token'] = $access_token;
24
25                         /* Remove no longer needed request tokens */
26                         unset($_SESSION['oauth_token']);
27                         unset($_SESSION['oauth_token_secret']);
28
29                         /* If HTTP response is 200 continue otherwise send to connect page to retry */
30                         if (200 == $connection->http_code) {
31                                 /* The user has been verified and the access tokens can be saved for future use */
32                                 $_SESSION['login_status'] = 'verified';
33                                 $t = getTwitter();
34                                 $user = $t->veverify();
35                                 
36                                 if ( ID_AUTH && (!in_array(strtoupper($t->screen_name),$AUTH_ID)) ){
37                                         session_destroy();
38                                         header("Location: login.php?oauth=denied");exit;
39                                 }
40                                 /* And set new cookies */
41                                 $time = $_SERVER['REQUEST_TIME']+3600*24*365;
42                                 setEncryptCookie('oauth_token', $access_token['oauth_token'], $time, '/');
43                                 setEncryptCookie('oauth_token_secret', $access_token['oauth_token_secret'], $time, '/');
44                                 setEncryptCookie('user_id', $access_token['user_id'], $time, '/');
45                                 setEncryptCookie('twitese_name', $t->screen_name, $time, '/');
46                                 refreshProfile();
47                                 
48                                 if(!isset($_COOKIE['showpic'])){
49                                         setcookie('showpic', 'true', $time, '/');
50                                 }
51                                 if(!isset($_COOKIE['shownick'])){
52                                         setcookie('shownick', 'false', $time, '/');
53                                 }
54                                 if(!isset($_COOKIE['mediaPre'])){
55                                         setcookie('mediaPre', 'true', $time, '/');
56                                 }
57                                 if(!isset($_COOKIE['loginPage'])) {
58                                         header('Location: index.php');exit();
59                                 } else {
60                                         $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") ? 'http' : 'https';
61                                         $port = $_SERVER['SERVER_PORT'] != 80 ? ':'.$_SERVER['SERVER_PORT'] : '';
62                                         $login_page = $scheme . '://' . $_SERVER['HTTP_HOST'] . $port . $_COOKIE['loginPage'];
63                                         header('Location: '. $login_page);exit();
64                                 }
65                                 
66                         } else {
67                                 session_destroy();
68                                 header('Location: login.php?oauth=error');exit();
69                         }
70                 }
71         }else{
72                 /* Create TwitterOAuth object and get request token */
73                 $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
74                 
75                 /* Get callback URL */
76                 $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") ? 'http' : 'https';
77                 $port = $_SERVER['SERVER_PORT'] != 80 ? ':'.$_SERVER['SERVER_PORT'] : '';
78                 $oauth_callback = $scheme . '://' . $_SERVER['HTTP_HOST'] . $port . $_SERVER['REQUEST_URI'];
79         
80                 /* Get request token */
81                 $request_token = $connection->getRequestToken($oauth_callback);
82
83                 /* Save request token to session */
84                 $_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
85                 $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
86
87                 /* If last connection fails don't display authorization link */
88                 switch ($connection->http_code) {
89                         case 200:
90                                 
91                                 $time = $_SERVER['REQUEST_TIME']+3600*24*365;
92                                 $url = $connection->getAuthorizeURL($token);
93                                 if ( isset($_POST['proxify']) ) { 
94                                         $raw= processCurl($url);
95                                         $new = str_replace('https://api.twitter.com/oauth/authorize', 'authorize.php',$raw); 
96                                         $new = str_replace('html { display:none; }','.error,a.sign-up,input[name="deny"]{display:none !important;}',$new);
97                                         $new = preg_replace('/https?:\/\/\w+([0-9])\.twimg\.com/i','https://s3.amazonaws.com/twitter_production',$new);
98                                 echo $new;
99                                 } //OAuth Proxy End
100                                 else {
101                                         header('Location: ' . $url); 
102                                 }
103                                 break;
104                         default:
105                                 header('Location: error.php?t=1');exit();
106                                 break;
107                 }
108         }
109 ?>