3 function user_oauth() {
5 \Codebird\Codebird::setConsumerKey(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET);
6 $cb = \Codebird\Codebird::getInstance();
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'],'/')
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;
23 // redirect to auth website
24 $auth_url = $cb->oauth_authorize();
25 header('Location: ' . $auth_url);
28 } // If there's an OAuth Token
29 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();
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';
108 // _user_save_cookie($_POST['stay-logged-in'] == 'yes');
109 // header('Location: '. BASE_URL);
119 function user_current_username() {
120 return $GLOBALS['user']['username'];
123 function user_is_current_user($username) {
124 return (strcasecmp($username, user_current_username()) == 0);
127 function user_type() {
128 return $GLOBALS['user']['type'];
131 function _user_save_cookie($stay_logged_in = 0) {
133 if ($stay_logged_in) {
134 $duration = time() + (3600 * 24 * 365);
139 // setcookie('oauth_token', $_SESSION['oauth_token'], $duration);
140 // setcookie('oauth_token_secret', $_SESSION['oauth_token_secret'], $duration);
142 $cookie = _user_encrypt_cookie();
144 setcookie('USER_AUTH', $cookie, $duration, '/');
147 function _user_encryption_key() {
148 return ENCRYPTION_KEY;
151 function _user_encrypt_cookie() {
152 $plain_text = $GLOBALS['user']['username'] . ':' . $GLOBALS['user']['password'] . ':' . $GLOBALS['user']['type'];
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);
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);
173 // TODO FIXME errr...
174 list($GLOBALS['user']['username'], $GLOBALS['user']['password'], $GLOBALS['user']['type']) = explode(':', $plain_text);
177 function theme_login() {
178 // Reset stale OAuth data
179 setting_clear_session_oauth();
181 $content = '<div class="tweet">
184 <img src="i/images/sign-in-with-twitter-gray.png"
185 alt="'._(TWITTER_SIGN_IN).'"
188 class="action" /></a>
190 <a href="oauth">'._(TWITTER_SIGN_IN).'</a>
196 function theme_logged_out() {
197 return "<p>"._(DABR_LOGIN_AGAIN)."</p>";