public function saveAction()
 {
     if (null !== ($response = $this->checkAuth(AdminResources::MODULE, ['Twitter'], AccessManager::UPDATE))) {
         return $response;
     }
     $form = new ConfigurationForm($this->getRequest());
     $configurationForm = $this->validateForm($form);
     $consumer_key = $configurationForm->get('consumer_key')->getData();
     $consumer_secret = $configurationForm->get('consumer_secret')->getData();
     $screen_name = $configurationForm->get('screen_name')->getData();
     $count = $configurationForm->get('count')->getData();
     $cache_lifetime = $configurationForm->get('cache_lifetime')->getData();
     // $debug_mode     = $configurationForm->get('debug_mode')->getData();
     $errorMessage = null;
     $response = null;
     // Save config values
     ConfigQuery::write('twitter_consumer_key', $consumer_key, 1, 1);
     ConfigQuery::write('twitter_consumer_secret', $consumer_secret, 1, 1);
     ConfigQuery::write('twitter_screen_name', $screen_name, 1, 1);
     ConfigQuery::write('twitter_count', $count, 1, 1);
     ConfigQuery::write('twitter_cache_lifetime', $cache_lifetime * 60, 1, 1);
     // Minutes
     ConfigQuery::write('twitter_last_updated', 0, 1, 1);
     if ($screen_name && $consumer_key && $consumer_secret) {
         if (!extension_loaded('openssl')) {
             $sslError = $this->getTranslator()->trans("This module requires the PHP extension open_ssl to work.", [], Twitter::DOMAIN_NAME);
         } else {
             $config = array('consumer_key' => $consumer_key, 'consumer_secret' => $consumer_secret, 'output_format' => 'array');
             try {
                 $connection = new TwitterOAuth($config);
                 $bearer_token = $connection->getBearerToken();
             } catch (\Exception $e) {
                 $errorMessage = $e->getMessage();
             }
             try {
                 $params = array('screen_name' => $screen_name, 'count' => 1, 'exclude_replies' => true);
                 $response = $connection->get('statuses/user_timeline', $params);
                 if ($response['error']) {
                     throw new TwitterException($response['error']);
                 }
             } catch (\Exception $e) {
                 $erroMessage = $this->getTranslator()->trans("Unrecognized screen name", [], Twitter::DOMAIN_NAME);
             }
         }
     }
     $response = RedirectResponse::create(URL::getInstance()->absoluteUrl('/admin/module/Twitter'));
     if (null !== $errorMessage) {
         $this->setupFormErrorContext($this->getTranslator()->trans("Twitter configuration failed.", [], Twitter::DOMAIN_NAME), $errorMessage, $form);
         $response = $this->render("module-configure", ['module_code' => 'Twitter']);
     }
     return $response;
 }
예제 #2
0
 public function getTweets()
 {
     $cache_path = realpath('.') . '/cache/tweets';
     $last_updated = ConfigQuery::read('twitter_last_updated');
     $tweets_file = $cache_path . '/' . Twitter::TWEETS_FILENAME;
     $tweets = [];
     // Is the cache stale ?
     // if( $last_updated + 0 < time() )
     if ($last_updated + $this->cache_lifetime < time()) {
         if (!is_dir($cache_path)) {
             mkdir($cache_path);
             chmod($cache_path, 0755);
         }
         // Get the tweets
         $config = ['consumer_key' => $this->consumer_key, 'consumer_secret' => $this->consumer_secret];
         try {
             $connection = new TwitterOAuth($config);
             $bearer_token = $connection->getBearerToken();
         } catch (\Exception $e) {
             $errorMessage = $e->getMessage();
         }
         try {
             $params = array('screen_name' => $this->screen_name, 'count' => $this->count, 'exclude_replies' => true);
             $tweets = $connection->get('statuses/user_timeline', $params);
             if ($tweets['error']) {
                 throw new TwitterException($tweets['error']);
             }
             // Cache tweets
             unlink($tweets_file);
             $fh = fopen($tweets_file, 'w');
             fwrite($fh, json_encode($tweets));
             fclose($fh);
             // Update cache refresh timestamp
             ConfigQuery::write('twitter_last_updated', time(), 1, 1);
         } catch (\Exception $e) {
             $erroMessage = Translator::getInstance()->trans("Unrecognized screen name", [], Twitter::DOMAIN_NAME);
         }
     } else {
         // Get tweets from the cache
         $tweets = json_decode(file_get_contents($tweets_file));
     }
     return $tweets;
 }
예제 #3
0
require_once "lib/TwitterOAuth/TwitterOAuth.php";
require_once "lib/TwitterOAuth/Exception/TwitterException.php";
require_once "conf/config.php";
use TwitterOauth\TwitterOAuth;
date_default_timezone_set('UTC');
$cache_filename = "state/twitter_trends";
$trends_json = "";
// If there is no recent cache file, try reading from twitter
// (By recent we currently mean modification time < an hour ago)
if (!(file_exists($cache_filename) && filemtime($cache_filename) > time() - 3600)) {
    // Read from Twitter and write to the cache file
    try {
        // Instantiate TwitterOAuth class with API tokens
        $connection = new TwitterOAuth($config['twitter_api_key']);
        // Get an application-only token
        $bearer_token = $connection->getBearerToken();
        $params = array('id' => 1, 'exclude' => true);
        // Ask for the trends
        $trends_json = $connection->get('trends/place', $params);
        // json_encode in this context is not used to TURN IT INTO JSON, it already
        // is, but rather to encode any special characters so they won't cause any
        // trouble when being output
        $trends_json = json_encode($trends_json);
        // Write to the cache file
        file_put_contents($cache_filename, $trends_json);
    } catch (Exception $e) {
        // Fetch failed - fallback to file
        $trends_json = "";
    }
}
if ($trends_json == "") {