Location: PHPKode > projects > Gemibloo > Gemibloo1_0_alpha1/application/plugins/export/class.ExportTwitter.php
<?php
	require_once(dirname(__FILE__).'/../../core/class/class.PluginExport.php');

	/**
	* ExportTwitter class  (SINGLETON see http://en.wikipedia.org/wiki/Singleton_pattern)
	*
	* This class updates Twitter account (adds an entry and updates location)
	*
	* @author Olivier G <hide@address.com>
	* @version 1.0
	* @license http://opensource.org/licenses/gpl-license.php GNU Public License
	* @link http://gemibloo.fr
	*/	
	final class ExportTwitter extends PluginExport{
		private static $TWITTER_URL_STATUS = 'http://twitter.com/statuses/update.xml';
		private static $TWITTER_URL_LOCATION = 'http://twitter.com/account/update_profile.xml';
		private static $TWITTER_MAXLENGTH = 140;
		private static $TWITTER_CONTENT_BEFORE = 'Just added on geoblog: ';
		private static $TWITTER_LOCATION_ALLOWED_LENGTH = 30;

		private static $_instance;

		/**
		* __construct method
		*/		
		private function __construct() {}

		/**
		* __clone method
		*/		
		private function __clone() {}

		/**
		* fGetInstance method
		*/		
		public static function fGetInstance() 
		{
			if (!isset(self::$_instance)) {
				$c = __CLASS__;
				self::$_instance = new $c;
			}
			return self::$_instance;
		}//getInstance

		/**
		* fExport method
		* 
		* Main method of the class. It uses Curl library to connect to Twitter and
		* -add a Tweet concerning a new entry on the geoblog
		* -update profile with the location of the entry
		* It throws ExportException typed exceptions if something goes wrong.
		*/
		public function fExport(){		
			if( (strlen(TWITTER_USR) != 0) && (strlen(TWITTER_PWD) != 0) ){
				$this->_fInitialize();		

				if(FALSE === ($curl_handle = curl_init()))
						throw new ExportException("Twitter export: Curl initialization failed.", ExportException::CURL_FAILED);
				curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
				curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($curl_handle, CURLOPT_POST, 1);
				curl_setopt($curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
				curl_setopt($curl_handle, CURLOPT_USERPWD, TWITTER_USR.':'.TWITTER_PWD);
			
				//Update status
				curl_setopt($curl_handle, CURLOPT_URL, self::$TWITTER_URL_STATUS);
				$twitter_content = $this->_content;
				$TWITTER_CONTENT_AFTER = ' See '.GEOBLOG_URL.'?entry_id='.$this->_id;
				$TWITTER_CONTENT_ALLOWED_LENGTH = self::$TWITTER_MAXLENGTH - strlen(self::$TWITTER_CONTENT_BEFORE) - strlen($TWITTER_CONTENT_AFTER);
				if(strlen($twitter_content) > $TWITTER_CONTENT_ALLOWED_LENGTH)
					$twitter_content = substr($twitter_content, 0, $TWITTER_CONTENT_ALLOWED_LENGTH-3).'...';
				curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'status='.urlencode(self::$TWITTER_CONTENT_BEFORE.$twitter_content.$TWITTER_CONTENT_AFTER));
				if(FALSE === curl_exec($curl_handle)){
						Log::fGetInstance()->fLog(Log::$TYPE['error'] ,__CLASS__, 'Tweet could not be added');					
						throw new ExportException("Twitter export: Curl execution failed with content.", ExportException::CURL_FAILED);
				}
				$responseInfo=curl_getinfo($curl_handle); 
				if(200 != intval($responseInfo['http_code'])){
					Log::fGetInstance()->fLog(Log::$TYPE['error'] ,__CLASS__, 'Tweet could not be added');					
					throw new ExportException("Twitter export: Status update failed.", ExportException::UPDATE_REQUEST_FAILED);
				}
				Log::fGetInstance()->fLog(Log::$TYPE['info'] ,__CLASS__,  'Tweet  from '.TWITTER_USR. ' added: "'.$twitter_content.'"');

				//Update location
				curl_setopt($curl_handle, CURLOPT_URL, self::$TWITTER_URL_LOCATION);	
				$twitter_loc = $this->_location;
				if(strlen($twitter_loc) > self::$TWITTER_LOCATION_ALLOWED_LENGTH)
					$twitter_loc = substr($twitter_loc, 0, self::$TWITTER_LOCATION_ALLOWED_LENGTH-3).'...';
				curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'location='.urlencode($twitter_loc));
				if(FALSE === curl_exec($curl_handle)){
						Log::fGetInstance()->fLog(Log::$TYPE['error'] ,__CLASS__, 'Location could not be updated');					
						throw new ExportException("Twitter export: Curl execution failed with location.", ExportException::CURL_FAILED);
				}
				$responseInfo = curl_getinfo($curl_handle); 
				if(200 != intval($responseInfo['http_code'])){
					Log::fGetInstance()->fLog(Log::$TYPE['error'] ,__CLASS__, 'Location could not be updated');
					throw new ExportException("Twitter export: Location update failed.", ExportException::UPDATE_REQUEST_FAILED);
				}
				Log::fGetInstance()->fLog(Log::$TYPE['info'] ,__CLASS__, 'Location from '.TWITTER_USR. ' changed to "'.$twitter_loc.'".');
				curl_close($curl_handle);
			}//if
		}//fExport
	}//ExportTwitter
?>
Return current item: Gemibloo