OSDN Git Service

xctcc/embrr
[embrj/master.git] / common / user.php
1 <?php
2
3 function user_oauth() {
4         
5         //require_once ('codebird.php');
6         $cb = \Codebird\Codebird::getInstance();
7         // Flag forces twitter_process() to use OAuth signing
8         // $GLOBALS['user']['type'] = 'oauth';
9
10         //      If there's no OAuth Token, take the user to Twiter's sign in page
11         if (! isset($_SESSION['oauth_token'])) {
12                 // get the request token
13                 $reply = $cb->oauth_requestToken(array(
14                         // 'oauth_callback' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
15                         'oauth_callback' => SERVER_NAME . $_SERVER['REQUEST_URI']
16                 ));
17
18                 // store the token
19                 $cb->setToken($reply->oauth_token, $reply->oauth_token_secret);
20                 $_SESSION['oauth_token']        = $reply->oauth_token;
21                 $_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;
22                 $_SESSION['oauth_verify']       = true;
23
24                 // redirect to auth website
25                 $auth_url = $cb->oauth_authorize();
26                 header('Location: ' . $auth_url);
27                 die();
28
29         }       //      If there's an OAuth Token 
30         elseif (isset($_GET['oauth_verifier']) && isset($_SESSION['oauth_verify'])) {
31                 // verify the token
32                 $cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
33                 unset($_SESSION['oauth_verify']);
34
35                 // get the access token
36                 $reply = $cb->oauth_accessToken(array(
37                         'oauth_verifier' => $_GET['oauth_verifier']
38                 ));
39
40                 // store the token (which is different from the request token!)
41                 $_SESSION['oauth_token']        = $reply->oauth_token;
42                 $_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;
43                 
44                 $cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
45                                 
46                 //      Verify and get the username
47                 $user = $cb->account_verifyCredentials();
48                 $GLOBALS['user']['username']    = $user->screen_name;
49
50                 // Store ACCESS tokens in COOKIE
51         $GLOBALS['user']['password'] = $_SESSION['oauth_token'] .'|'.$_SESSION['oauth_token_secret'];
52
53                 _user_save_cookie(1);
54                 // send to same URL, without oauth GET parameters
55                 header('Location: '. BASE_URL);
56                 die();
57         }
58         header('Location: '. BASE_URL); 
59 }
60
61 function user_ensure_authenticated() {
62         if (!user_is_authenticated()) {
63                 $content = theme('login');
64                 $content .= theme('about');
65                 theme('page', 'Login', $content);
66         }
67 }
68
69 function user_logout() {
70         //      Unset everything related to OAuth
71         unset($GLOBALS['user']);
72         unset($_SESSION['oauth_token']);
73         unset($_SESSION['oauth_token_secret']);
74         setcookie('USER_AUTH',          '', time() - 3600, '/');
75         setcookie('oauth_token',        '', time() - 3600, '/');
76         setcookie('oauth_token_secret', '', time() - 3600, '/');
77 }
78
79 function user_is_authenticated() {
80         if (!isset($GLOBALS['user'])) {
81
82                 if(array_key_exists('USER_AUTH', $_COOKIE)) {
83                          // _user_decrypt_cookie($_COOKIE['USER_AUTH']);
84
85                         $crypt_text = base64_decode($_COOKIE['USER_AUTH']);
86                         $td = mcrypt_module_open('blowfish', '', 'cfb', '');
87                         $ivsize = mcrypt_enc_get_iv_size($td);
88                         $iv = substr($crypt_text, 0, $ivsize);
89                         $crypt_text = substr($crypt_text, $ivsize);
90                         mcrypt_generic_init($td, _user_encryption_key(), $iv);
91                         $plain_text = mdecrypt_generic($td, $crypt_text);
92                         mcrypt_generic_deinit($td);
93
94                 //      TODO FIXME errr...
95                         list($GLOBALS['user']['username'], $GLOBALS['user']['password'], $GLOBALS['user']['type']) = explode(':', $plain_text);
96
97                 } else {
98                         $GLOBALS['user'] = array();
99                 }
100
101
102         }
103
104         if (!user_current_username()) {
105                 // if ($_POST['username'] && $_POST['password']) {
106                 //      $GLOBALS['user']['username'] = trim($_POST['username']);
107                 //      $GLOBALS['user']['password'] = $_POST['password'];
108                 //      $GLOBALS['user']['type'] = 'oauth';
109                                                 
110                 //      _user_save_cookie($_POST['stay-logged-in'] == 'yes');
111                 //      header('Location: '. BASE_URL);
112                 //      exit();
113                 // } else {
114                 //      return false;
115                 // }
116                 return false;
117         }
118         return true;
119 }
120
121 function user_current_username() {
122         return $GLOBALS['user']['username'];
123 }
124
125 function user_is_current_user($username) {
126         return (strcasecmp($username, user_current_username()) == 0);
127 }
128
129 function user_type() {
130         return $GLOBALS['user']['type'];
131 }
132
133 function _user_save_cookie($stay_logged_in = 0) {
134         
135         if ($stay_logged_in) {
136                 $duration = time() + (3600 * 24 * 365);
137         } else {
138                         $duration = 0;
139         }
140
141         // setcookie('oauth_token',        $_SESSION['oauth_token'],        $duration);
142         // setcookie('oauth_token_secret', $_SESSION['oauth_token_secret'], $duration);
143
144         $cookie = _user_encrypt_cookie();
145         
146         setcookie('USER_AUTH', $cookie, $duration, '/');
147 }
148
149 function _user_encryption_key() {
150         return ENCRYPTION_KEY;
151 }
152
153 function _user_encrypt_cookie() {
154         $plain_text = $GLOBALS['user']['username'] . ':' . $GLOBALS['user']['password'] . ':' . $GLOBALS['user']['type'];
155
156         $td = mcrypt_module_open('blowfish', '', 'cfb', '');
157         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
158         mcrypt_generic_init($td, _user_encryption_key(), $iv);
159         $crypt_text = mcrypt_generic($td, $plain_text);
160         mcrypt_generic_deinit($td);
161         return base64_encode($iv.$crypt_text);
162 }
163
164 function _user_decrypt_cookie($crypt_text) {
165         $crypt_text = base64_decode($crypt_text);
166         $td = mcrypt_module_open('blowfish', '', 'cfb', '');
167         $ivsize = mcrypt_enc_get_iv_size($td);
168         $iv = substr($crypt_text, 0, $ivsize);
169         $crypt_text = substr($crypt_text, $ivsize);
170         mcrypt_generic_init($td, _user_encryption_key(), $iv);
171         $plain_text = mdecrypt_generic($td, $crypt_text);
172         mcrypt_generic_deinit($td);
173
174 //      TODO FIXME errr...
175         list($GLOBALS['user']['username'], $GLOBALS['user']['password'], $GLOBALS['user']['type']) = explode(':', $plain_text);
176 }
177
178 function theme_login() {
179         //      Reset stale OAuth data
180         setting_clear_session_oauth();
181
182         $content = '<div class="tweet">
183                                         <p>
184                                                 <a href="oauth">
185                                                         <img src="images/sign-in-with-twitter-gray.png" 
186                                                              alt="Sign in with Twitter" 
187                                                              width="158" 
188                                                              height="28" 
189                                                              class="action" /></a>
190                                                 <br />
191                                                 <a href="oauth">Sign in via Twitter.com</a>
192                                         </p>';
193
194         return $content;
195 }
196
197 function theme_logged_out() {
198         return '<p>Logged out. <a href="">Login again</a></p>';
199 }