Exemplo n.º 1
0
 public function sendTweet($text, $shortenURLs = true)
 {
     if (!$this->_config['twitter']['enabled']) {
         return false;
     }
     if ($shortenURLs) {
         $url_match = '@(https?://([-\\w\\.]+)+(:\\d+)?(/([-\\w/_\\.]*(\\?\\S+)?)?)?)@';
         $text = preg_replace_callback($url_match, array($this, 'parseURL'), $text);
     }
     try {
         $twitter = new Zend_Service_Twitter($this->_config['twitter']['username'], $this->_config['twitter']['password']);
         /* @var $response Zend_Rest_Client_Result */
         $response = $twitter->account->verifyCredentials();
         //die(var_dump($response));
         //if ($response->isError())
         //throw new sfException($response->error);
         $response = $twitter->statusUpdate($text);
         if ($response->isError()) {
             throw new sfException($response->error);
         }
     } catch (Exception $exc) {
         return false;
     }
     return true;
 }
 public function userAction()
 {
     $otwitter = Mage::getModel('sociallogin/twlogin');
     $requestToken = Mage::getSingleton('core/session')->getRequestToken();
     $oauth_data = array('oauth_token' => $this->getRequest()->getParam('oauth_token'), 'oauth_verifier' => $this->getRequest()->getParam('oauth_verifier'));
     $token = $otwitter->getAccessToken($oauth_data, unserialize($requestToken));
     $params = array('consumerKey' => Mage::helper('sociallogin')->getTwConsumerKey(), 'consumerSecret' => Mage::helper('sociallogin')->getTwConsumerSecret(), 'accessToken' => $token);
     $twitter = new Zend_Service_Twitter($params);
     $response = $twitter->userShow($token->user_id);
     $twitterId = (string) $response->id;
     // get twitter account ID
     $customerId = $this->getCustomerId($twitterId);
     if ($customerId) {
         //login
         $customer = Mage::getModel('customer/customer')->load($customerId);
         Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
         die("<script type=\"text/javascript\">try{window.opener.location.reload(true);}catch(e){window.opener.location.href=\"" . Mage::getBaseUrl() . "\"} window.close();</script>");
     } else {
         // redirect to login page
         Mage::getSingleton('core/session')->setTwitterId($twitterId);
         $connectingNotice = Mage::helper('sociallogin')->getTwConnectingNotice();
         $storeName = Mage::app()->getStore()->getName();
         Mage::getSingleton('core/session')->addNotice(str_replace('{{store}}', $storeName, $connectingNotice));
         $nextUrl = Mage::helper('sociallogin')->getLoginUrl();
         $backUrl = Mage::getSingleton('core/session')->getBackUrl();
         Mage::getSingleton('customer/session')->setBeforeAuthUrl($backUrl);
         // after login redirect to current url
         die("<script>window.close();window.opener.location = '{$nextUrl}';</script>");
     }
 }
Exemplo n.º 3
0
 public function send($message, $logRow)
 {
     if (!$this->_getAuthRow()->auth_token) {
         throw new Kwf_Exception('no auth token saved');
     }
     $accessToken = unserialize($this->_getAuthRow()->auth_token);
     Zend_Service_Twitter::setHttpClient($accessToken->getHttpClient($this->_config));
     $twitter = new Zend_Service_Twitter(null, null);
     $response = $twitter->account->verifyCredentials();
     if (!$response->isSuccess()) {
         throw new Kwf_Exception('verifyCredentials failed: ' . $response->__toString());
     }
     $response = $twitter->statusUpdate($message);
     if (!$response->isSuccess()) {
         throw new Kwf_Exception('statusUpdate failed: ' . $response->__toString());
     }
     $logRow->status_id = (string) $response->id;
     $logRow->user_id = (string) $response->user->id;
     $logRow->screen_name = (string) $response->user->screen_name;
 }
 /**
  * Show my tweets 
  */
 public function myfeedAction()
 {
     $params = $this->getRequest()->getParams();
     $this->_helper->layout->disableLayout();
     // Get the Twitter params from config
     $userToken = $this->config->api->access_token;
     $userSecret = $this->config->api->token_secret;
     $appConsumerKey = $this->config->api->consumer_key;
     $appConsumerSecret = $this->config->api->consumer_secret;
     // Create the Oauth Token
     $token = new Zend_Oauth_Token_Access();
     $token->setToken($userToken)->setTokenSecret($userSecret);
     // Create the Twitter Service object
     $options = array('accessToken' => $token, 'consumerKey' => $appConsumerKey, 'consumerSecret' => $appConsumerSecret);
     $twitter = new Zend_Service_Twitter($options);
     // Get my tweets
     $tweets = $twitter->statusFriendsTimeline();
     var_dump($tweets);
     die;
     // Send JSON to browser
     $this->getResponse()->setHeader(Zend_Http_Client::CONTENT_TYPE, 'application/json')->appendBody(json_encode($tweets));
 }
Exemplo n.º 5
0
 public function __construct($username, $config, Zend_Oauth_Token_Access $token)
 {
     $this->_authInitialized = true;
     $this->_client = $token->getHttpClient($config);
     self::setHttpClient($this->_client);
     parent::__construct($username, null);
 }
 /**
  * @group ZF-10644
  */
 public function testStatusUserTimelineShouldHonorAllFlags()
 {
     $params = array('screen_name' => 'allzend', 'page' => 1, 'include_rts' => '1', 'trim_user' => '1', 'include_entities' => '1');
     $twitter = new Zend_Service_Twitter();
     $twitter->setLocalHttpClient($this->_stubTwitter('statuses/user_timeline.xml', Zend_Http_Client::GET, 'user_timeline.twitter.xml', $params));
     // Assertions are part of mocking
     $timeline = $twitter->statusUserTimeline($params);
 }
Exemplo n.º 7
0
 /**
  * Get Timeline off Twitter
  * @param  string	$user		Tiki username to get timeline for
  * @param  string	$timelineType	Timeline to get: public/friends/search - Default: public
  * @param  string	$search		Search string
  * @return string|int			-1 if the user did not authorize the site with twitter, a negative number corresponding to the HTTP response codes from twitter (https://dev.twitter.com/docs/streaming-api/response-codes) or the requested timeline (json encoded object)
  */
 function getTwitterTimeline($user, $timelineType = 'public', $search = 'tikiwiki')
 {
     global $prefs;
     $token = $this->get_user_preference($user, 'twitter_token', '');
     if ($token == '') {
         $this->add_log('tweet', 'user not registered with twitter');
         return -1;
     }
     $token = unserialize($token);
     $twitter = new Zend_Service_Twitter(array('oauthOptions' => array('consumerKey' => $prefs['socialnetworks_twitter_consumer_key'], 'consumerSecret' => $prefs['socialnetworks_twitter_consumer_secret']), 'accessToken' => $token));
     if ($timelineType == 'friends') {
         $response = $twitter->statusesHomeTimeline();
     } elseif ($timelineType == 'search') {
         $response = $twitter->search->tweets($search, array('include_entities' => true));
     } else {
         $response = $twitter->statusesUserTimeline();
     }
     if (!$response->isSuccess()) {
         $errors = $response->getErrors();
         $this->add_log('tweet', 'twitter response: ' . $errors[0]->message . ' - Code: ' . $errors[0]->code);
         return -$errors['code'];
     } else {
         return $response->toValue();
     }
 }
Exemplo n.º 8
0
 /**
  * @group ZF-6284
  */
 public function testTwitterObjectsSoNotShareSameHttpClientToPreventConflictingAuthentication()
 {
     $twitter1 = new Zend_Service_Twitter(array('username' => 'zftestuser1'));
     $twitter2 = new Zend_Service_Twitter(array('username' => 'zftestuser2'));
     $this->assertFalse($twitter1->getLocalHttpClient() === $twitter2->getLocalHttpClient());
 }
Exemplo n.º 9
0
 /**
  * Get Timeline off Twitter
  * @param  string	$user		Tiki username to get timeline for
  * @param  string	$timelineType	Timeline to get: public/friends - Default: public
  * @return string|int			-1 if the user did not authorize the site with twitter, a negative number corresponding to the HTTP response codes from twitter (https://dev.twitter.com/docs/streaming-api/response-codes) or the requested timeline (json encoded object)
  */
 function getTwitterTimeline($user, $timelineType = 'public')
 {
     global $prefs;
     $token = $this->get_user_preference($user, 'twitter_token', '');
     if ($token == '') {
         $this->add_log('tweet', 'user not registered with twitter');
         return -1;
     }
     $token = unserialize($token);
     $token = (object) $token;
     $this->options['callbackUrl'] = $this->getURL();
     $this->options['consumerKey'] = $prefs['socialnetworks_twitter_consumer_key'];
     $this->options['consumerSecret'] = $prefs['socialnetworks_twitter_consumer_secret'];
     $client = $token->getHttpClient($this->options);
     $clientconfig['timeout'] = 30;
     $client->setConfig($clientconfig);
     $twitter = new Zend_Service_Twitter();
     $twitter->setLocalHttpClient($client);
     try {
         if ($timelineType == 'friends') {
             $response = $twitter->status->friendsTimeline();
         } else {
             $response = $twitter->status->userTimeline();
         }
     } catch (Zend_Http_Client_Exception $e) {
         $this->add_log('tweet', 'twitter error ' . $e->getMessage());
         return -$e->getCode();
     }
     $status = $response->getStatus();
     if ($status != 200) {
         $this->add_log('tweet', 'twitter response ' . $status);
         return -$status;
     } else {
         return $response;
     }
 }
Exemplo n.º 10
0
 /** Call Twitter service
  *
  * @return array
  */
 private function _callTwitter()
 {
     if (!$this->getCache()->test(md5($this->getKey()))) {
         $tokens = new OauthTokens();
         $token = $tokens->fetchRow($tokens->select()->where('service = ?', 'twitterAccess'));
         $twitter = new Zend_Service_Twitter(array('username' => 'findsorguk', 'accessToken' => unserialize($token->accessToken), 'oauthOptions' => array('consumerKey' => $this->getConfig()->webservice->twitter->consumerKey, 'consumerSecret' => $this->getConfig()->webservice->twitter->consumerSecret)));
         $tweets = $twitter->statusesUserTimeline(array('count' => $this->getCount()))->toValue();
         $this->getCache()->save($tweets);
     } else {
         $tweets = $this->getCache()->load(md5($this->getKey()));
     }
     return $this->buildHtml($tweets);
 }
Exemplo n.º 11
0
 /**
  * Deletes a tweet with the given tweet id
  *
  * @param int		$id		Id of the tweet to delete
  * @param string	$user		UserId of the user who sent the tweet
  *
  * @return bool					true on success
  */
 function destroyTweet($id, $user)
 {
     global $prefs;
     $token = $this->get_user_preference($user, 'twitter_token', '');
     if ($token == '') {
         return false;
     }
     $token = unserialize($token);
     $token = (object) $token;
     $this->options['callbackUrl'] = $this->getURL();
     $this->options['consumerKey'] = $prefs['socialnetworks_twitter_consumer_key'];
     $this->options['consumerSecret'] = $prefs['socialnetworks_twitter_consumer_secret'];
     $client = $token->getHttpClient($this->options);
     $twitter = new Zend_Service_Twitter();
     $twitter->setLocalHttpClient($client);
     try {
         $response = $twitter->status->destroy($id);
     } catch (Zend_Http_Client_Adapter_Exception $e) {
         return false;
     }
     return true;
 }
Exemplo n.º 12
0
 public function testUsersSearch()
 {
     $twitter = new Zend_Service_Twitter();
     $twitter->setHttpClient($this->stubTwitter('users/search.json', Zend_Http_Client::GET, 'users.search.json', array('q' => 'Zend')));
     $response = $twitter->users->search('Zend');
     $this->assertTrue($response instanceof Zend_Service_Twitter_Response);
 }
Exemplo n.º 13
0
 /**
  * @group ZF-11023
  */
 public function testConstructorPassedObjectZendConfig()
 {
     require_once 'Zend/Config.php';
     $config = new Zend_Config(array('username' => 'zf'));
     $twitter = new Zend_Service_Twitter($config);
     $this->assertEquals('zf', $twitter->getUsername());
 }
Exemplo n.º 14
0
 /**
  * Initializes the service and adds the list to the method types 
  * of the parent service class.
  *
  * @param string $username The Twitter account name.
  * @param string $password The Twitter account password.
  * @see Zend_Service_Twitter::_methodTypes
  */
 public function __construct($username = null, $password = null)
 {
     parent::__construct($username, $password);
     $this->_methodTypes[] = 'list';
 }