Advertisement
jegtheme

jeg-twitter.php

Jun 29th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.22 KB | None | 0 0
  1. <?php
  2.  
  3. class Creare_Twitter
  4. {
  5.  
  6.     public $screen_name = "crearegroup";
  7.     public $not = 1;
  8.     public $cachefile = "twitter.txt";
  9.     public $consumerkey = "XXXX";
  10.     public $consumersecret = "XXXX";
  11.     public $accesstoken = "XXXX";
  12.     public $accesstokensecret = "XXXX";
  13.  
  14.     public $tags = true;
  15.     public $nofollow = true;
  16.     public $newwindow = true;
  17.     public $hashtags = true;
  18.     public $attags = true;
  19.  
  20.     private function cleanTwitterName($twitterid)
  21.     {
  22.         $test = substr($twitterid,0,1);
  23.  
  24.         if($test == "@"){
  25.             $twitterid = substr($twitterid,1);
  26.         }
  27.  
  28.         return $twitterid;
  29.  
  30.     }
  31.  
  32.     private function changeLink($string)
  33.     {
  34.         if(!$this->tags){
  35.             $string = strip_tags($string);
  36.         }
  37.         if($this->nofollow){
  38.             $string = str_replace('<a ','<a rel="nofollow"', $string);
  39.         }
  40.         if($this->newwindow){
  41.             $string = str_replace('<a ','<a target="_blank"', $string);
  42.         }
  43.         return $string;
  44.     }
  45.  
  46.     private function getTimeAgo($time)
  47.     {
  48.             $tweettime = strtotime($time); // This is the value of the time difference - UK + 1 hours (3600 seconds)
  49.             $nowtime = time();
  50.             $timeago = ($nowtime-$tweettime);
  51.             $thehours = floor($timeago/3600);
  52.             $theminutes = floor($timeago/60);
  53.             $thedays = floor($timeago/86400);
  54.             /********************* Checking the times and returning correct value */
  55.             if($theminutes < 60){
  56.                 if($theminutes < 1){
  57.                     $timemessage =  "Less than 1 minute ago";
  58.                 } else if($theminutes == 1) {
  59.                     $timemessage = $theminutes." minute ago";
  60.                 } else {
  61.                     $timemessage = $theminutes." minutes ago";
  62.                 }
  63.             } else if($theminutes > 60 && $thedays < 1){
  64.                  if($thehours == 1){
  65.                     $timemessage = $thehours." hour ago";
  66.                  } else {
  67.                     $timemessage = $thehours." hours ago";
  68.                  }
  69.             } else {
  70.                  if($thedays == 1){
  71.                     $timemessage = $thedays." day ago";
  72.                  } else {
  73.                     $timemessage = $thedays." days ago";
  74.                  }
  75.             }
  76.         return $timemessage;
  77.     }
  78.  
  79.     private function removeSpamCharacters($string)
  80.     {
  81.         $string = preg_replace('/[^(\x20-\x7F)]*/','', $string);
  82.         return $string;
  83.     }
  84.  
  85.     public function getTweets($tweets)
  86.     {
  87.         $t = array();
  88.         $i = 0;
  89.         foreach($tweets as $tweet)
  90.         {
  91.             if(isset($tweet->retweeted_status)){
  92.                 $text = $this->removeSpamCharacters($tweet->retweeted_status->text);
  93.             } else {
  94.                 $text = $this->removeSpamCharacters($tweet->text);
  95.             }
  96.             $urls = $tweet->entities->urls;
  97.             $mentions = $tweet->entities->user_mentions;
  98.             $hashtags = $tweet->entities->hashtags;
  99.             if($urls){
  100.                 foreach($urls as $url){
  101.                     if(strpos($text,$url->url) !== false){
  102.                         $text = str_replace($url->url,'<a href="'.$url->url.'">'.$url->url.'</a>',$text);
  103.                     }
  104.                 }
  105.             }
  106.             if($mentions && $this->attags){
  107.                 foreach($mentions as $mention){
  108.                     if(strpos($text,$mention->screen_name) !== false){
  109.                         $text = str_replace("@".$mention->screen_name." ",'<a href="http://twitter.com/'.$mention->screen_name.'">@'.$mention->screen_name.'</a> ',$text);
  110.                     }
  111.                 }
  112.             }
  113.             if($hashtags && $this->hashtags){
  114.                 foreach($hashtags as $hashtag){
  115.                     if(strpos($text,$hashtag->text) !== false){
  116.                         $text = str_replace('#'.$hashtag->text." ",'<a href="http://twitter.com/search?q=%23'.$hashtag->text.'">#'.$hashtag->text.'</a> ',$text);
  117.                     }
  118.                 }
  119.             }
  120.             $t[$i]["tweet"] = trim($this->changeLink($text));
  121.             $t[$i]["time"] = trim($this->getTimeAgo($tweet->created_at));
  122.             $i++;
  123.         }
  124.  
  125.         $this->saveCachedTweets($t);
  126.         return $t;
  127.     }
  128.  
  129.     private function saveCachedTweets($data)
  130.     {
  131.         $data = json_encode($data);
  132.         $f = file_put_contents($this->cachefile, $data);
  133.     }
  134.  
  135.     private function getCachedTweets()
  136.     {
  137.         return file_get_contents($this->cachefile);
  138.     }
  139.  
  140.     public function getLatestTweets()
  141.     {
  142.         if(class_exists('TwitterOAuth')) {
  143.             require_once('twitteroauth/twitteroauth.php');
  144.         }
  145.         $twitterconn = new TwitterOAuth($this->consumerkey, $this->consumersecret, $this->accesstoken, $this->accesstokensecret);
  146.  
  147.         $latesttweets = $twitterconn->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$this->screen_name."&count=".$this->not);
  148.  
  149.         if(!isset($latesttweets->errors)){
  150.             return $this->getTweets($latesttweets);
  151.         } else {
  152.             return json_decode($this->getCachedTweets(), true);
  153.         }
  154.  
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement