3 function user_oauth() {
5 //require_once ('codebird.php');
6 $cb = \Codebird\Codebird::getInstance();
7 // Flag forces twitter_process() to use OAuth signing
8 // $GLOBALS['user']['type'] = 'oauth';
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']
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;
24 // redirect to auth website
25 $auth_url = $cb->oauth_authorize();
26 header('Location: ' . $auth_url);
29 } // If there's an OAuth Token
30 elseif (isset($_GET['oauth_verifier']) && isset($_SESSION['oauth_verify'])) {
32 $cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
33 unset($_SESSION['oauth_verify']);
35 // get the access token
36 $reply = $cb->oauth_accessToken(array(
37 'oauth_verifier' => $_GET['oauth_verifier']
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;
44 $cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
46 // Verify and get the username
47 $user = $cb->account_verifyCredentials();
48 $GLOBALS['user']['username'] = $user->screen_name;
50 // Store ACCESS tokens in COOKIE
51 $GLOBALS['user']['password'] = $_SESSION['oauth_token'] .'|'.$_SESSION['oauth_token_secret'];
54 // send to same URL, without oauth GET parameters
55 header('Location: '. BASE_URL);
58 header('Location: '. BASE_URL);
61 function user_ensure_authenticated() {
62 if (!user_is_authenticated()) {
63 $content = theme('login');
64 $content .= theme('about');
65 theme('page', 'Login', $content);
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, '/');
79 function user_is_authenticated() {
80 if (!isset($GLOBALS['user'])) {
82 if(array_key_exists('USER_AUTH', $_COOKIE)) {
83 // _user_decrypt_cookie($_COOKIE['USER_AUTH']);
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);
95 list($GLOBALS['user']['username'], $GLOBALS['user']['password'], $GLOBALS['user']['type']) = explode(':', $plain_text);
98 $GLOBALS['user'] = array();
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';
110 // _user_save_cookie($_POST['stay-logged-in'] == 'yes');
111 // header('Location: '. BASE_URL);
121 function user_current_username() {
122 return $GLOBALS['user']['username'];
125 function user_is_current_user($username) {
126 return (strcasecmp($username, user_current_username()) == 0);
129 function user_type() {
130 return $GLOBALS['user']['type'];
133 function _user_save_cookie($stay_logged_in = 0) {
135 if ($stay_logged_in) {
136 $duration = time() + (3600 * 24 * 365);
141 // setcookie('oauth_token', $_SESSION['oauth_token'], $duration);
142 // setcookie('oauth_token_secret', $_SESSION['oauth_token_secret'], $duration);
144 $cookie = _user_encrypt_cookie();
146 setcookie('USER_AUTH', $cookie, $duration, '/');
149 function _user_encryption_key() {
150 return ENCRYPTION_KEY;
153 function _user_encrypt_cookie() {
154 $plain_text = $GLOBALS['user']['username'] . ':' . $GLOBALS['user']['password'] . ':' . $GLOBALS['user']['type'];
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);
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);
174 // TODO FIXME errr...
175 list($GLOBALS['user']['username'], $GLOBALS['user']['password'], $GLOBALS['user']['type']) = explode(':', $plain_text);
178 function theme_login() {
179 // Reset stale OAuth data
180 setting_clear_session_oauth();
182 $content = '<div class="tweet">
185 <img src="images/sign-in-with-twitter-gray.png"
186 alt="Sign in with Twitter"
189 class="action" /></a>
191 <a href="oauth">Sign in via Twitter.com</a>
197 function theme_logged_out() {
198 return '<p>Logged out. <a href="">Login again</a></p>';