public function Tweets()
 {
     $twitterApp = TwitterApp::get()->first();
     if (!$twitterApp) {
         return null;
     }
     $siteConfig = SiteConfig::current_site_config();
     $twitter = $twitterApp->getTwitter();
     $twitter->setAccess(new OAuthToken($twitterApp->TwitterAccessToken, $twitterApp->TwitterAccessSecret));
     if ($twitter->hasAccess()) {
         $result = $twitter->api("1.1/statuses/user_timeline.json", "GET", array("screen_name" => $this->TwitterHandle, "count" => $this->NumberOfTweets));
         if ($result->statusCode() == 200) {
             $rawTweets = json_decode($result->body(), true);
             if (count($rawTweets) > 0) {
                 $tweets = new ArrayList();
                 foreach ($rawTweets as $tweet) {
                     // Parse tweet links, users and hashtags.
                     $parsed = preg_replace("#(^|[\n ])([\\w]+?://[\\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" target=\"_blank\">\\2</a>'", $tweet['text']);
                     $parsed = preg_replace("#(^|[\n ])@([A-Za-z0-9\\_]*)#ise", "'\\1<a href=\"http://www.twitter.com/\\2\" target=\"_blank\">@\\2</a>'", $parsed);
                     $parsed = preg_replace("#(^|[\n ])\\#([A-Za-z0-9]*)#ise", "'\\1<a href=\"http://www.twitter.com/search?q=\\2\" target=\"_blank\">#\\2</a>'", $parsed);
                     $t = new ArrayData(array());
                     $t->Tweet = DBField::create_field("HTMLText", $parsed, "Tweet");
                     $t->TweetDate = DBField::create_field("SS_Datetime", strtotime($tweet['created_at']));
                     $t->TweetLink = DBField::create_field("Varchar", "http://www.twitter.com/" . rawurlencode($tweet['user']['screen_name']) . "/status/" . rawurlencode($tweet['id_str']));
                     $tweets->push($t);
                 }
                 return $tweets;
             }
         }
     }
     return null;
 }
 /**
  * Sets up consumer keys and asserts that Twitter::curr() should return a valid instance of Twitter
  **/
 public function testGetApp()
 {
     $siteConfig = SiteConfig::current_site_config();
     $siteConfig->TwitterConsumerKey = "testkey";
     $siteConfig->TwitterConsumerSecret = "testsecret";
     $siteConfig->write();
     $twitter = TwitterApp::curr();
     $this->assertInstanceOf("Twitter", $twitter, "Unable to fetch Twitter App.");
 }
 /**
  * Creates and returns an instance of Twitter if the Consumer key & secret are set.
  *
  * @return Twitter or false
  **/
 public static function curr()
 {
     if (self::$twitter) {
         return self::$twitter;
     }
     $config = SiteConfig::current_site_config();
     if ($config->TwitterConsumerKey && $config->TwitterConsumerSecret) {
         return self::$twitter = new Twitter($config->TwitterConsumerKey, $config->TwitterConsumerSecret);
     }
     return false;
 }
 /**
  * Fetches the sites Twitter Applications.
  *
  * @return TwitterApp
  **/
 public function getTwitterApp()
 {
     return TwitterApp::get()->first();
 }
 /**
  * Log the user in via an existing Twitter account connection.
  *
  * @return SS_HTTPResponse
  **/
 public function login()
 {
     $form = $this->Form();
     $twitterApp = TwitterApp::get()->first();
     if (!$twitterApp || !$twitterApp->EnableTwitterLogin) {
         $form->sessionMessage("Twitter Login is disabled.", "bad");
     } else {
         if ($member = Member::currentUser()) {
             $member->logOut();
         }
         $twitter = $twitterApp->getTwitter();
         if ($twitter) {
             $twitter->setOAuthCallback(Director::absoluteURL(Controller::join_links("twitter", "login")));
             $request = Session::get("Twitter.Request");
             if ($request) {
                 $twitter->setRequest(unserialize($request));
                 Session::clear("Twitter.Request");
             } else {
                 $request = $twitter->getRequestToken();
                 if ($request) {
                     Session::set("Twitter.Request", serialize($request));
                     return $this->redirect($twitter->getLoginURL());
                 }
             }
             // Check to see if we have our access tokens stored in the db.
             if ($access = $twitter->getAccessToken()) {
                 $user = $twitter->getUser();
                 if ($user) {
                     // The twitter user is logged in. Find their account.
                     $member = Member::get()->filter("TwitterUserID", $user['id_str'])->first();
                     if ($member) {
                         $member->logIn();
                         $form->sessionMessage("You have logged in using Twitter.", "good");
                         $this->extend("onAfterTwitterLogin", $member);
                     } else {
                         $form->sessionMessage("Unable to find your account.", "bad");
                     }
                 } else {
                     $form->sessionMessage("Unable to fetch your Twitter account.", "bad");
                 }
             } else {
                 $form->sessionMessage("Unable to obtain access to Twitter.", "bad");
             }
         }
     }
     // Extend Failed twitter login
     if (!Member::currentUser()) {
         $this->extend("onAfterFailedTwitterLogin");
     }
     return $this->renderWith(array("TwitterController", "Page", "Controller"));
 }
 /**
  * Setup a default Twitter app
  **/
 public function requireDefaultRecords()
 {
     $twitter = TwitterApp::get()->count();
     if (!$twitter) {
         $twitter = TwitterApp::create();
         $twitter->write();
     }
 }