/fuel/app/classes/controller/account.php

https://github.com/Keilaron/TweetBeagle · PHP · 165 lines · 105 code · 29 blank · 31 comment · 9 complexity · 0cc26afccaea88b83619236da3942c96 MD5 · raw file

  1. <?php
  2. class Controller_Account extends Controller_Template {
  3. /**
  4. * This action simply guides unauthenticated users to login to their Twitter
  5. * accounts. Autheticated users will be redirected to their dashboard.
  6. */
  7. public function action_index()
  8. {
  9. $view = View::factory('account/index');
  10. // first, check if we have an access token in the session
  11. if (TwitterAccount::isLoggedIn())
  12. {
  13. $accessToken = TwitterAccount::accessToken();
  14. $ta = TwitterAccount::getUserAccount($accessToken['oauth_token'], $accessToken['oauth_token_secret']);
  15. $view->screenName = $accessToken['screen_name'];
  16. }
  17. else
  18. {
  19. Session::destroy();
  20. }
  21. $pub_collections = Model_Collection::find('all', array('where' => array(array('public', '=', '1'))));
  22. $collections = array();
  23. foreach ($pub_collections as $collection)
  24. {
  25. $collections[$collection->id] = $collection->name;
  26. }
  27. $view->public_collections = $collections;
  28. $this->template->title = 'Welcome to TweetBeagle';
  29. $this->template->content = $view;
  30. }
  31. /**
  32. * This action simply guides unauthenticated users to login to their Twitter
  33. * accounts.
  34. */
  35. public function action_signin()
  36. {
  37. $ta = TwitterAccount::getDefaultAccount();
  38. $requestToken = $ta->getRequestToken();
  39. Log::debug(var_export($requestToken, true));
  40. if ($requestToken)
  41. {
  42. Session::set('oauth_token', $requestToken['oauth_token']);
  43. Session::set('oauth_token_secret', $requestToken['oauth_token_secret']);
  44. $params = '';
  45. // Are they trying to switch accounts?
  46. if (!is_null(input::get_post('force')))
  47. $params .= '&force_login=true';
  48. $url = $ta->getAuthorizeUrl($requestToken['oauth_token']);
  49. $this->response->redirect($url.$params);
  50. }
  51. else
  52. {
  53. $this->template->title = 'Account &raquo; Sign in';
  54. $this->template->content = 'Error: Unable to connect to Twitter!';
  55. }
  56. }
  57. /**
  58. * This action destroys the Session and redirects the user to the login page.
  59. */
  60. public function action_signout()
  61. {
  62. Session::destroy();
  63. Response::redirect('account');
  64. }
  65. /**
  66. * This action is redirected to from Twitter and is given the request token
  67. * and a verifier. It must do the final requestToken/accessToken exchange and
  68. * store stuff in the session.
  69. */
  70. public function action_oauth_callback()
  71. {
  72. $twitterToken = Input::get_post('oauth_token', FALSE);
  73. $twitterTokenVerifier = Input::get_post('oauth_verifier', FALSE);
  74. $sessionToken = Session::get('oauth_token');
  75. Log::debug('oauth_token from Twitter: '.$twitterToken);
  76. Log::debug('oauth_token from Session: '.$sessionToken);
  77. Log::debug('oauth_verifier from Twitter: '.Input::get_post('oauth_verifier'));
  78. // check if the request token we got from Twitter is an old one
  79. if ($twitterToken && ($sessionToken !== $twitterToken)) {
  80. Session::set('oauth_status', 'oldtoken');
  81. Response::redirect('account/index');
  82. }
  83. $ta = TwitterAccount::getUserAccount($sessionToken['oauth_token'],
  84. $sessionToken['oauth_token_secret']);
  85. $accessToken = $ta->getAccessToken($twitterToken, $twitterTokenVerifier);
  86. $user = $ta->verifyCredentials();
  87. Log::debug(var_export($accessToken, true));
  88. Session::set('access_token', $accessToken);
  89. // clean up unnecessary session variables
  90. Session::delete('oauth_token');
  91. Session::delete('oauth_token_secret');
  92. // depending on the last response code, redirect the user to the dashboard
  93. // or the login page
  94. if ($ta->isLastRequestSuccessful())
  95. {
  96. $this->createUserAccount($user);
  97. Session::set('oauth_status', 'verified');
  98. Response::redirect('dashboard');
  99. }
  100. else
  101. {
  102. Response::redirect('account/signout');
  103. }
  104. }
  105. /**
  106. * Creates an account for the logged in user if it does not already exist.
  107. * It also retrieves their Twitter data.
  108. * @param object user Twitter user that just logged in.
  109. */
  110. protected function createUserAccount($user)
  111. {
  112. $accessToken = TwitterAccount::accessToken();
  113. // Check if we already know this account's full Twitter data
  114. // These checks are separate because someone else may have seen them before in a list or search.
  115. $tweeter = Model_Tweeter::find($accessToken['user_id']);
  116. if (!$tweeter)
  117. Harvester::parseUser($user, $tweeter, $dummy = array());
  118. Session::set('screen_name', $tweeter->screen_name);
  119. // If this is the first time the user logs into our system, create an account him/her
  120. $account = Model_Account::find($accessToken['user_id']);
  121. if (empty($account))
  122. {
  123. $account = new Model_Account(array(
  124. 'id' => $accessToken['user_id'],
  125. 'oauth_key' => $accessToken['oauth_token'],
  126. 'oauth_secret' => $accessToken['oauth_token_secret'],
  127. ));
  128. $account->save();
  129. } else {
  130. $account->oauth_key = $accessToken['oauth_token'];
  131. $account->oauth_secret = $accessToken['oauth_token_secret'];
  132. $account->save();
  133. }
  134. }
  135. }
  136. /* End of file account.php */