Пример #1
0
	protected function updateTwitter($items) {
		// Get the user
		$users = new Users();
		$shortUrl = new ShortUrl(); 
		$user  = $users->getUser($this->getUserID());
		
		// Get twitter consumer tokens and user secrets
		$config = Zend_Registry::get("configuration");
		$consumer_key = $config->twitter->consumer_key;
		$consumer_secret = $config->twitter->consumer_secret;
		
		// Get twitter credentials
		$properties = new Properties(array(Properties::KEY => $user->id));
		$auth	    = $properties->getProperty('twitter_auth');
		$services   = $properties->getProperty('twitter_services');
		$oauth_token = $properties->getProperty('twitter_oauth_token');
		$oauth_token_secret = $properties->getProperty('twitter_oauth_token_secret');
		
		if (!$consumer_key || !$consumer_secret || !$oauth_token || !$oauth_token_secret) {
			return;
		}
		
		$has_preamble   = $properties->getProperty('preamble', true);
		
		// Return if not all conditions are met
		if (!$auth || !in_array($this->getID(), unserialize($services))) {
			return;
		}
		
		// Get an item
		$count		= count($items);
		$data		= new Data();
		$source_id	= $this->_source['id'];
					
		if ($count <= 3) {
			foreach($items as $id) {
				$item		= $data->getItem($source_id, $id);
				$title		= strip_tags($item->getTitle());
				$service	= $this->getServiceName();
				
				if (($item->getType() == SourceItem::STATUS_TYPE ) && strlen($title) < 140) {
					$tweet = $title;
				} 
				else {
					$preamble = $has_preamble ? $item->getPreamble() : "";
					$tweet	  = $preamble . $title;
					$url = $users->getUrl($user->id, "s/" . $shortUrl->shorten($item->getSlug()));
					if (strlen($tweet) + strlen($url) > 140) {
						$tweet = substr($tweet, 0, 140 - strlen($url) - 5) . "... $url"; 
					} else {
						$tweet 	= "$tweet $url";	
					}
				}
				
				try {
					$connection = new TwitterOAuth_Client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
					$response = $connection->post('statuses/update', array('status' => $tweet));	
				} catch (Exception $e) {}
			}
		} else {
			$url = $users->getUrl($user->id);
			$tweet  = sprintf($this->_update_tweet, $count, $url);
			try {
				$twitter = new Stuffpress_Services_Twitter($username, $password);
				$twitter->sendTweet($tweet);
			} catch (Exception $e) {}			
			}
	}
Пример #2
0
	private function notifyTwitter($item) {		
		// Get twitter consumer tokens and user secrets
		$consumer_key = $this->_config->twitter->consumer_key;
		$consumer_secret = $this->_config->twitter->consumer_secret;
		$oauth_token = $this->_properties->getProperty('twitter_oauth_token');
		$oauth_token_secret = $this->_properties->getProperty('twitter_oauth_token_secret');
		
		if (!$consumer_key || !$consumer_secret || !$oauth_token || !$oauth_token_secret) {
			$this->addErrorMessage("Missing twitter OAuth credentials to continue");
		}

		// Should we append a text before the tweet ?
		$has_preamble   = $this->_properties->getProperty('preamble', true);		
		
		// Get item
		$preamble	= $has_preamble ? $item->getPreamble() : "";
		$title		= $preamble . $item->getTitle();
		
		// Assemble tweet depending on type
		if (($item->getType() == SourceItem::STATUS_TYPE ) && strlen($title) < 140) {
			$tweet = $title;
		} else {
			$users  = new Users();
			$shortUrl = new ShortUrl(); 
			$url = $users->getUrl($this->_application->user->id, "s/" . $shortUrl->shorten($item->getSlug()));
			if (strlen($title) + strlen($url) > 140) {
				$tweet = substr($title, 0, 140 - strlen($url) - 5) . "... $url"; 
			} else {
				$tweet 	= "$title $url";	
			}
		}

		try {
			$connection = new TwitterOAuth_Client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
			$response = $connection->post('statuses/update', array('status' => $tweet));
			if (isset($response->error)) {
				$this->addErrorMessage("Failed posting to Twitter with error " . $response->error);
			}			
		} catch (Exception $e) {
			$this->addErrorMessage("Failed posting to Twitter with unknwon error");
		}
	}