token_file_path = $_SERVER["HOME"]."/.twitter_tokens"; // The App's credentials (http://twitter.com/apps) $this->consumer_key = "4eEizUgVyT3TRdlAy1rlgQ"; $this->consumer_secret = "p2ufMEFn0Ly20gF1hAfCtrVIuPVceZh9Ggp2JBCwk"; // The user's secret tokens passed back from initial oAuth $this->twitter_token = ""; $this->twitter_token_secret = ""; // OAuth class $this->oauth = null; // Console output colourisation /* 30 black foreground 31 red foreground 32 green foreground 33 brown foreground 34 blue foreground 35 magenta (purple) foreground 36 cyan (light blue) foreground 37 gray foreground */ $this->date_colour = 91; $this->user_colour = 36; // The fail whale :) $this->whale = " W W W W W W W '. W .-\"\"-._ \\ \\.--| / \"-..__) .-' | _ / \\'-.__, .__.,' `'----'._\\--' VVVVVVVVVVVVVVVVVVVVV\n"; } /** * Load up tokens from file */ function loadTokens(){ if( !file_exists( $this->token_file_path ) ) return false; $a = file( $this->token_file_path ); foreach( $a as $row ){ if( preg_match( "/^([a-z_]+)=(.+)$/", $row, $m ) ){ if( property_exists( $this, $m[1] ) ) $this->$m[1] = trim( $m[2] ); } } return true; } /** * Save tokens to file */ function saveTokens(){ $aTokens = array( "consumer_key", "consumer_secret", "twitter_token", "twitter_token_secret" ); $str = ""; foreach( $aTokens as $token ){ $str .= $token."=".$this->$token."\n"; } file_put_contents( $this->token_file_path, $str ); } /** * Handle exceptions from oAuth */ function handleExceptions($E){ /// echo $E->message; } /** * Set up the oAuth object */ function initOAuth(){ // if( !$this->loadTokens() ) die( "No tokens file at ".$this->token_file_path."\n" ); $this->loadTokens() $oauth = new OAuth( $this->consumer_key, $this->consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI ); if (!( $this->twitter_token && $this->twitter_token_secret )){ $request_token = $oauth->getRequestToken('https://api.twitter.com/oauth/request_token'); $oauth->setToken($request_token['oauth_token'], $request_token['oauth_token_secret']); $url = 'https://api.twitter.com/oauth/authorize?' . http_build_query(array('oauth_token' => $request_token['oauth_token'])); //system('open ' . escapeshellarg($url)); print "Authorize:\n$url\nEnter the PIN: "; $access_token = $oauth->getAccessToken('https://api.twitter.com/oauth/access_token', NULL, trim(fgets(STDIN))); $this->twitter_token = $access_token["oauth_token"]; $this->twitter_token_secret = $access_token["oauth_token_secret"]; $this->saveTokens(); // printf("define('TWITTER_TOKEN', '%s');\ndefine('TWITTER_TOKEN_SECRET', '%s');\n", $access_token['oauth_token'], $access_token['oauth_token_secret']); } $oauth->setToken( $this->twitter_token, $this->twitter_token_secret ); $oauth->disableDebug(); $this->oauth = $oauth; } /** * Send a tweet * @param string $status * @return string */ function tweet( $status ){ // Update status $url = "http://twitter.com/statuses/update.xml"; if( !$this->oauth ) $this->initOAuth(); $args = array( "status" => $status ); try{ $this->oauth->fetch($url,$args,"POST"); } catch( OAuthException $E ) { $this->handleExceptions( $E ); } $result = $this->oauth->getLastResponse(); $xml = @simplexml_load_string( $result ); if( !isset( $xml->text ) ) return false; return $xml; // return "\033[".$this->user_colour."m".$xml->user->screen_name."\033[0m: ".$xml->text."\n"; } function readUserStream(){ $url = "https://userstream.twitter.com/2/user.json"; if( !$this->oauth ) $this->initOAuth(); $args = array(); try{ $this->oauth->fetch($url,$args); } catch( OAuthException $E ) { $this->handleExceptions( $E ); } $result = $this->oauth->getLastResponse(); $xml = @simplexml_load_string( $result ); if( !isset( $xml->text ) ) return false; return $xml; } /** * Follow a user * @param string $user user name to follow * @return mixed false on fail, screen name on success */ function follow( $user ){ $url = "http://api.twitter.com/1/friendships/create.xml"; if( !$this->oauth ) $this->initOAuth(); $args = array( "screen_name" => $user ); try{ $this->oauth->fetch($url,$args,"POST"); } catch( OAuthException $E ) { $this->handleExceptions( $E ); } $result = $this->oauth->getLastResponse(); $xml = @simplexml_load_string( $result ); if( isset( $xml->screen_name ) ) return $xml->screen_name; if( isset( $xml->error ) ){ if( strstr( $xml->error, "already on your list" ) !== false ) return true; } return false; } /** * Unfollow a user * @param string $user user name to unfollow * @return bool success */ function unfollow( $user ){ $url = "http://api.twitter.com/1/friendships/destroy.xml?screen_name=".urlencode( $user ); if( !$this->oauth ) $this->initOAuth(); $args = array( "screen_name" => $user ); try{ $this->oauth->fetch($url,array(),"DELETE"); } catch( OAuthException $E ) { $this->handleExceptions( $E ); } $result = $this->oauth->getLastResponse(); $xml = @simplexml_load_string( $result ); if( !isset( $xml->screen_name ) ) return false; return true; } /** * Check istwitterdown.com to see if twitter is down * @return bool */ function isUp(){ ini_set('default_socket_timeout', 3 ); $page = @file_get_contents( "http://istwitterdown.com/" ); if( !$page ){ // echo "istwitterdown.com is down! The irony!\n"; return false; } if( preg_match( "/