OSDN Git Service

https://github.com/rahuldottech/Dabr
[embrj/master.git] / i / common / user.php
1 <?php
2
3 function user_oauth() {
4
5         \Codebird\Codebird::setConsumerKey(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET);
6         $cb = \Codebird\Codebird::getInstance();
7
8         //      If there's no OAuth Token, take the user to Twiter's sign in page
9         if (! isset($_SESSION['oauth_token'])) {
10                 // get the request token
11                 $reply = $cb->oauth_requestToken(array(
12                         // 'oauth_callback' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
13                         //      Trim the first slash
14                         'oauth_callback' => SERVER_NAME . ltrim($_SERVER['REQUEST_URI'],'/')
15                 ));
16
17                 // store the token
18                 $cb->setToken($reply->oauth_token, $reply->oauth_token_secret);
19                 $_SESSION['oauth_token']        = $reply->oauth_token;
20                 $_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;
21                 $_SESSION['oauth_verify']       = true;
22
23                 // redirect to auth website
24                 $auth_url = $cb->oauth_authorize();
25                 header('Location: ' . $auth_url);
26                 die();
27
28         }       //      If there's an OAuth Token
29         elseif (isset($_GET['oauth_verifier']) && isset($_SESSION['oauth_verify'])) {
30
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         if (!user_current_username()) {
103                 // if ($_POST['username'] && $_POST['password']) {
104                 //      $GLOBALS['user']['username'] = trim($_POST['username']);
105                 //      $GLOBALS['user']['password'] = $_POST['password'];
106                 //      $GLOBALS['user']['type'] = 'oauth';
107
108                 //      _user_save_cookie($_POST['stay-logged-in'] == 'yes');
109                 //      header('Location: '. BASE_URL);
110                 //      exit();
111                 // } else {
112                 //      return false;
113                 // }
114                 return false;
115         }
116         return true;
117 }
118
119 function user_current_username() {
120         return $GLOBALS['user']['username'];
121 }
122
123 function user_is_current_user($username) {
124         return (strcasecmp($username, user_current_username()) == 0);
125 }
126
127 function user_type() {
128         return $GLOBALS['user']['type'];
129 }
130
131 function _user_save_cookie($stay_logged_in = 0) {
132
133         if ($stay_logged_in) {
134                 $duration = time() + (3600 * 24 * 365);
135         } else {
136                         $duration = 0;
137         }
138
139         // setcookie('oauth_token',        $_SESSION['oauth_token'],        $duration);
140         // setcookie('oauth_token_secret', $_SESSION['oauth_token_secret'], $duration);
141
142         $cookie = _user_encrypt_cookie();
143
144         setcookie('USER_AUTH', $cookie, $duration, '/');
145 }
146
147 function _user_encryption_key() {
148         return ENCRYPTION_KEY;
149 }
150
151 function _user_encrypt_cookie() {
152         $plain_text = $GLOBALS['user']['username'] . ':' . $GLOBALS['user']['password'] . ':' . $GLOBALS['user']['type'];
153
154         // $td = mcrypt_module_open('blowfish', '', 'cfb', '');
155         // $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
156         // mcrypt_generic_init($td, _user_encryption_key(), $iv);
157         // $crypt_text = mcrypt_generic($td, $plain_text);
158         // mcrypt_generic_deinit($td);
159         // return base64_encode($iv.$crypt_text);
160         return base64_encode($plain_text);
161 }
162
163 function _user_decrypt_cookie($crypt_text) {
164         $plain_text = base64_decode($crypt_text);
165         // $td = mcrypt_module_open('blowfish', '', 'cfb', '');
166         // $ivsize = mcrypt_enc_get_iv_size($td);
167         // $iv = substr($crypt_text, 0, $ivsize);
168         // $crypt_text = substr($crypt_text, $ivsize);
169         // mcrypt_generic_init($td, _user_encryption_key(), $iv);
170         // $plain_text = mdecrypt_generic($td, $crypt_text);
171         // mcrypt_generic_deinit($td);
172
173 //      TODO FIXME errr...
174         list($GLOBALS['user']['username'], $GLOBALS['user']['password'], $GLOBALS['user']['type']) = explode(':', $plain_text);
175 }
176
177 function theme_login() {
178         //      Reset stale OAuth data
179         setting_clear_session_oauth();
180
181         $content = '<div class="tweet">
182                                         <p>
183                                                 <a href="oauth">
184                                                         <img src="i/images/sign-in-with-twitter-gray.png"
185                                                              alt="'._(TWITTER_SIGN_IN).'"
186                                                              width="158"
187                                                              height="28"
188                                                              class="action" /></a>
189                                                 <br />
190                                                 <a href="oauth">'._(TWITTER_SIGN_IN).'</a>
191                                         </p>';
192
193         return $content;
194 }
195
196 function theme_logged_out() {
197         return "<p>"._(DABR_LOGIN_AGAIN)."</p>";
198 }