* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Service_Twitter
*/
/* Keys - these are obtained when registering for the service */
$keys = array(
'consumer_key' => '*****',
'consumer_secret' => '*****',
'access_token' => '*****-*****',
'access_token_secret' => '*****'
);
/* Enable autoloading. */
require 'Horde/Autoloader/Default.php';
/* Create the Twitter client */
$twitter = Horde_Service_Twitter::create(array('oauth' => $keys));
/* Do something cool.... */
try {
$result = $twitter->statuses->update('Testing Horde/Twitter integration 2');
print_r(Horde_Serialize::unserialize($result, Horde_Serialize::JSON));
} catch (Horde_Service_Twitter_Exception $e) {
$error = Horde_Serialize::unserialize($e->getMessage(), Horde_Serialize::JSON);
echo "$error->error\n";
}
Horde_Service_Twitter-2.1.5/doc/Horde/Service/Twitter/COPYING 0000664 0000765 0000024 00000002430 12535652765 020624 0 ustar Copyright 1999-2015 Horde LLC. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE HORDE PROJECT
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Horde_Service_Twitter-2.1.5/lib/Horde/Service/Twitter/Auth/Oauth.php 0000664 0000765 0000024 00000005336 12535652765 022274 0 ustar
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Service_Twitter
*/
class Horde_Service_Twitter_Auth_Oauth extends Horde_Service_Twitter_Auth
{
/**
*
* @var Horde_OAuth_Token
*/
protected $_token;
public function __construct(Horde_OAuth_Consumer $oauth)
{
$this->_config['oauth'] = $oauth;
}
/**
* Obtain the URL used to get an authorization token.
*
* @param Horde_Oauth_Token $requestToken The request token
*
* @return string The Url
*/
public function getUserAuthorizationUrl($requestToken)
{
return $this->oauth->getUserAuthorizationUrl($requestToken);
}
/**
* Set the access token
*
* @param Horde_OAuth_Token $token
*/
public function setToken(Horde_OAuth_Token $token)
{
$this->_token = $token;
}
/**
* Obtain the access token. This is the token that should be persisted to
* storage.
*
* @param Horde_Controller_Request_Http Http request object
* @param string $requestSecret The token secret returned by
* Twitter after the user authorizes
* the application.
* @return Horde_Oauth_Token
* @throws Horde_Service_Twitter_Exception
*/
public function getAccessToken(Horde_Controller_Request_Http $request, $requestSecret = null)
{
if (!empty($this->_token)) {
return $this->_token;
}
$params = $request->getGetVars();
if (empty($params['oauth_token'])) {
return false;
}
$token = new Horde_Oauth_Token($params['oauth_token'], $requestSecret);
try {
return $this->oauth->getAccessToken($token, array('oauth_verifier' => $requestSecret));
} catch (Horde_Oauth_Exception $e) {
throw new Horde_Service_Twitter_Exception($e->getMessage());
}
}
/**
* Obtain the OAuth request token
*
* @param array $params
*
* @return Horde_OAuth_Token The request token
* @throws Horde_Service_Twitter_Exception
*/
public function getRequestToken($params = array())
{
try {
return $this->oauth->getRequestToken($params);
} catch (Horde_Oauth_Exception $e) {
throw new Horde_Service_Twitter_Exception($e->getMessage());
}
}
}
Horde_Service_Twitter-2.1.5/lib/Horde/Service/Twitter/Request/Oauth.php 0000664 0000765 0000024 00000006426 12535652765 023024 0 ustar
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Service_Twitter
*/
class Horde_Service_Twitter_Request_Oauth extends Horde_Service_Twitter_Request
{
/**
* Perform a GET request with OAuth authorization.
*
* @param mixed (string | Horde_Url) $url The url to request.
* @param array $params URL parameters.
*
* @return string Call results.
* @throws Horde_Service_Twitter_Exception
*/
public function get($url, array $params = array())
{
$key = md5($url . 'get' . serialize($params) . serialize($this->_twitter->auth->getAccessToken($this->_request)));
$cache = $this->_twitter->responseCache;
if (!empty($cache) && $results = $cache->get($key, $this->_twitter->cacheLifetime)) {
return $results;
}
$request = new Horde_Oauth_Request($url, $params, 'GET');
$request->sign($this->_twitter->auth->oauth->signatureMethod,
$this->_twitter->auth->oauth,
$this->_twitter->auth->getAccessToken($this->_request));
$url = ($url instanceof Horde_Url) ? $url : new Horde_Url($url);
$url->add($params);
try {
$response = $this->_twitter->getHttpClient()->get((string)$url->setRaw(true), array('Authorization' => $request->buildAuthorizationHeader('Twitter API')));
} catch (Horde_Http_Exception $e) {
throw new Horde_Service_Twitter_Exception($e);
}
// Looks like some of the http clients (like Fopen) will thrown an
// exception if we try to read an empty stream. Ignore this.
try {
$body = $response->getBody();
if ($response->code >= 400 && $response->code <= 500) {
throw new Horde_Service_Twitter_Exception($body);
}
} catch (Horde_Http_Exception $e) {}
if (!empty($cache)) {
$cache->set($key, $body);
}
return $body;
}
/**
* Send a POST request to the twitter API. Purposely do not cache results
* from these since POST requests alter data on the server.
*
* @see self::get
*/
public function post($url, array $params = array())
{
$request = new Horde_Oauth_Request($url, $params);
$request->sign($this->_twitter->auth->oauth->signatureMethod,
$this->_twitter->auth->oauth,
$this->_twitter->auth->getAccessToken($this->_request));
$url = ($url instanceof Horde_Url) ? $url : new Horde_Url($url);
try {
$response = $this->_twitter->getHttpClient()->post((string)$url->setRaw(true), $params, array('Authorization' => $request->buildAuthorizationHeader('Twitter API')));
} catch (Horde_Http_Exception $e) {
throw new Horde_Service_Twitter_Exception($e);
}
if ($response->code >= 400 && $response->code <= 500) {
throw new Horde_Service_Twitter_Exception($response->getBody());
}
return $response->getBody();
}
}
Horde_Service_Twitter-2.1.5/lib/Horde/Service/Twitter/Account.php 0000664 0000765 0000024 00000005162 12535652765 021704 0 ustar
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Service_Twitter
*/
class Horde_Service_Twitter_Account
{
/**
* Twitter endpoint for account api calls
*
* @var string
*/
protected $_endpoint = 'https://api.twitter.com/1.1/account/';
/**
* The request/response format to use, xml or json.
*
* @var string
*/
protected $_format = 'json';
/**
*
* @param Horde_Service_Twitter $twitter
*/
public function __construct($twitter)
{
$this->_twitter = $twitter;
}
/**
* Used to verify current credentials, and obtain some basic profile
* information about the current user.
*
* http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-account%C2%A0verify_credentials
*
* @return string JSON reprentation of profile.
* @throws Horde_Service_Twitter_Exception
*/
public function verifyCredentials()
{
$url = $this->_endpoint . 'verify_credentials.' . $this->_format;
return $this->_twitter->request->get($url);
}
/**
* Obtain the current user's (if authenticated) or IP address' (if not
* authenticated) remaining number of requests left for the hour.
*
* http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-account%C2%A0rate_limit_status
*
* @return string JSON representation of result object.
*/
public function rateLimitStatus()
{
$url = $this->_endpoint . 'rate_limit_status.' . $this->_format;
return $this->_twitter->request->get($url);
}
/**
* Ends the current session, invalidates the current auth token if using
* OAuth.
*
* @return string
*/
public function endSession()
{
// NOOP - no longer part of the Twitter API, no replacement.
}
/**
* Update/reset where twitter sends automatic updates to
* (im/sms etc...)
*
* @TODO
* @param string $device
*
* @return void
*/
public function updateDeliveryDevice($device = '')
{
}
/**
* Update user's profile data.
*
* http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-account%C2%A0update_profile
*
* @TODO
* @param array $profile Profile data see API docs for key-values
*
* @return string JSON representation of user's updated profile data
*/
public function updateProfile($profile)
{
}
}
Horde_Service_Twitter-2.1.5/lib/Horde/Service/Twitter/Auth.php 0000664 0000765 0000024 00000002117 12535652765 021206 0 ustar
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Service_Twitter
*/
abstract class Horde_Service_Twitter_Auth
{
/**
*
* @var Horde_Service_Twitter
*/
protected $_twitter;
/**
* Configuration parameters
*
* @param array
*/
protected $_config;
public function setTwitter(Horde_Service_Twitter $twitter)
{
$this->_twitter = $twitter;
}
/**
* Getter
*
* @param string $value
*
* @return mixed The value of the requested property.
* @throws Horde_Service_Twitter_Exception
*/
public function __get($value)
{
if (!empty($this->_config[$value])) {
return $this->_config[$value];
}
throw new Horde_Service_Twitter_Exception(sprintf("The property %s does not exist", $value));
}
}
Horde_Service_Twitter-2.1.5/lib/Horde/Service/Twitter/Exception.php 0000664 0000765 0000024 00000000455 12535652765 022246 0 ustar
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Service_Twitter
*/
class Horde_Service_Twitter_Exception extends Horde_Exception_Wrapped
{
}
Horde_Service_Twitter-2.1.5/lib/Horde/Service/Twitter/Favorites.php 0000664 0000765 0000024 00000003200 12535652765 022241 0 ustar
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Service_Twitter
*/
class Horde_Service_Twitter_Favorites
{
/**
* Endpoint for status api requests
*
* @var string
*/
private $_endpoint = 'https://api.twitter.com/1.1/favorites/';
/**
* Format to use json or xml
*
* @var string
*/
private $_format = 'json';
/**
* Constructor
*
* @param Horde_Service_Twitter $twitter
*/
public function __construct($twitter)
{
$this->_twitter = $twitter;
}
/**
* Obtain the requested status
*
* @return string The method call results.
*/
public function get()
{
$url = $this->_endpoint . 'list.' . $this->_format;
return $this->_twitter->request->post($url);
}
/**
* Destroy the specified favorite.
*
* @param string $id The status id
*
* @return string
*/
public function destroy($id)
{
$url = $this->_endpoint . 'destroy.' . $this->_format;
return $this->_twitter->request->post($url, array('id' => $id));
}
/**
* Add a new favorite
*
* @param string $id The status id
*
* @return string The favorited tweet.
*/
public function create($id)
{
$url = $this->_endpoint . 'create.' . $this->_format;
return $this->_twitter->request->post($url, array('id' => $id));
}
} Horde_Service_Twitter-2.1.5/lib/Horde/Service/Twitter/Request.php 0000664 0000765 0000024 00000001662 12535652765 021741 0 ustar
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Service_Twitter
*/
abstract class Horde_Service_Twitter_Request
{
/**
*
* @var Horde_Service_Twitter
*/
protected $_twitter;
/**
*
* @var Horde_Controller_Request_Http
*/
protected $_request;
public function __construct(Horde_Controller_Request_Http $request)
{
$this->_request = $request;
}
public function setTwitter(Horde_Service_Twitter $twitter)
{
$this->_twitter = $twitter;
}
abstract public function get($url, array $params = array());
abstract public function post($url, array $params = array());
}
Horde_Service_Twitter-2.1.5/lib/Horde/Service/Twitter/Statuses.php 0000664 0000765 0000024 00000027452 12535652765 022131 0 ustar
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Service_Twitter
*/
class Horde_Service_Twitter_Statuses
{
/**
* Endpoint for status api requests
*
* @var string
*/
private $_endpoint = 'https://api.twitter.com/1.1/statuses/';
/**
* Format to use json or xml
*
* @var string
*/
private $_format = 'json';
/**
* Constructor
*
* @param Horde_Service_Twitter $twitter
*/
public function __construct($twitter)
{
$this->_twitter = $twitter;
}
/**
* Obtain the requested status
*
* @return string The method call results.
*/
public function show($id)
{
$url = $this->_endpoint . 'show.' . $this->_format;
return $this->_twitter->request->post($url, array('id' => $id));
}
/**
* Destroy the specified status update, obviously only if the current user
* is the author of the update.
*
* http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses%C2%A0destroy
*
* @param string $id The status id
*
* @return string
*/
public function destroy($id)
{
$url = $this->_endpoint . 'destroy.' . $this->_format;
return $this->_twitter->request->post($url, array('id' => $id));
}
/**
* Update the current user's status.
*
* @param string $status The new status text.
* @param array $params Any additional parameters.
*
* in_reply_to_status_id - the status id this tweet is in response to.
*
*
* @return string
*/
public function update($status, $params = array())
{
$url = $this->_endpoint . 'update.' . $this->_format;
$params['status'] = $status;
return $this->_twitter->request->post($url, $params);
}
/**
* Obtain the friendsTimeline.
*
* http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-friends_timeline
*
* NOTE: According to the API docs, this method is deprecated and will be
* going away in a future version of the API. This is to be replaced by
* home_timeline.
*
* http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-home_timeline
*
* @param array $params Parameters for the friends_timeline call
*
* since_id - Only tweets more recent the indicated tweet id
* max_id - Only tweets older then the indeicated tweet id
* count - Only return this many tweets (twitter limit = 200)
* page - The page number to return (note there are
* pagination limits)
* include_rts - Include retweets
* include_entities - Include twitter entities (will be mandatory in
* future twitter api release).
*
*
* @return string
*/
public function friendsTimeline($params = array())
{
$url = $this->_endpoint . 'friends_timeline.' . $this->_format;
return $this->_twitter->request->get($url, $params);
}
/**
* Returns the 20 most recent statuses, including retweets, posted by the
* authenticating user and that user's friends. This is the equivalent of
* /timeline/home on the Web.
*
* http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-home_timeline
*
* @param array $params Parameters for the friends_timeline call
*
* since_id - Only tweets more recent the indicated tweet id
* max_id - Only tweets older then the indeicated tweet id
* count - Only return this many tweets (twitter limit = 200)
* page - The page number to return (note there are pagination limits)
*
*
* @return string
*/
public function homeTimeline($params = array())
{
$url = $this->_endpoint . 'home_timeline.' . $this->_format;
return $this->_twitter->request->get($url, $params);
}
/**
* Returns the 20 most recent retweets posted by the authenticating user.
*
* @param array $params Parameters for the friends_timeline call
*
* since_id - Only tweets more recent the indicated tweet id
* max_id - Only tweets older then the indeicated tweet id
* count - Only return this many tweets (twitter limit = 200)
* page - The page number to return (note there are pagination limits)
*
*
* @return string
*/
public function retweetedByMe($params = array())
{
$url = $this->_endpoint . 'retweeted_by_me.' . $this->_format;
return $this->_twitter->request->get($url, $params);
}
/**
* Returns the 20 most recent retweets posted by the authenticating user's
* friends.
*
* @param array $params Parameters for the friends_timeline call
*
* since_id - Only tweets more recent the indicated tweet id
* max_id - Only tweets older then the indeicated tweet id
* count - Only return this many tweets (twitter limit = 200)
* page - The page number to return (note there are pagination limits)
*
*
* @return string
*/
public function retweetedToMe($params = array())
{
$url = $this->_endpoint . 'retweetedToMe.' . $this->_format;
return $this->_twitter->request->get($url, $params);
}
/**
* Returns the 20 most recent tweets of the authenticated user that have
* been retweeted by others.
*
* @param array $params Parameters for the friends_timeline call
*
* since_id - Only tweets more recent the indicated tweet id
* max_id - Only tweets older then the indeicated tweet id
* count - Only return this many tweets (twitter limit = 200)
* page - The page number to return (note there are pagination limits)
*
*
* @return string
*/
public function retweetsOfMe($params = array())
{
$url = $this->_endpoint . 'retweets_of_me.' . $this->_format;
return $this->_twitter->request->get($url, $params);
}
/**
* Retweets a tweet. Requires the id parameter of the tweet you are
* retweeting. Request must be a POST or PUT.
* Returns the original tweet with retweet details embedded.
*
* @params string id The id for the tweet that is being retweeted.
*
* @return string
*/
public function retweet($id)
{
$url = $this->_endpoint . 'retweet/' . $id . '.' . $this->_format;
return $this->_twitter->request->post($url, array());
}
/**
* Obtain the last 20 tweets from the public timeline. This is cached every
* 60 seconds on Twitter's servers so we should eventually ensure this is
* only actually requested every 60 seconds or greater.
*
* @return string
*/
public function publicTimeline()
{
$url = $this->_endpoint . 'public_timeline.' . $this->_format;
return $this->_twitter->request->get($url);
}
/**
* Obtain the friendsTimeline.
*
* http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-user_timeline
*
* @param array $params Parameters for the friends_timeline call
*
* id - For this user id or screen name.
* Current user if left out.
* user_id - Specfies the ID of the user for whom to return the
* user_timeline. Helpful for disambiguating when a valid
* user ID is also a valid screen name.
* screen_id - Specfies the screen name of the user for whom to return
* the user_timeline. Helpful for disambiguating when a
* valid screen name is also a user ID.
* since_id - Only tweets more recent the indicated tweet id
* max_id - Only tweets older then the indeicated tweet id
* count - Only return this many tweets (twitter limit = 200)
* page - The page number to return (note there are pagination limits)
*
*
* @return string
*/
public function userTimeline($params = array())
{
$url = $this->_endpoint . 'user_timeline.' . $this->_format;
return $this->_twitter->request->get($url, $params);
}
/**
* Obtain most recent 'mentions' for the current user. (i.e. all messages
* that contain @username in the text).
*
* http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-mentions
*
* @param array $params Parameters for the friends_timeline call
*
* since_id - Only tweets more recent the indicated tweet id
* max_id - Only tweets older then the indeicated tweet id
* count - Only return this many tweets (twitter limit = 200)
* page - The page number to return (note there are pagination limits)
*
*
* @return string
*/
public function mentions($params = array())
{
$url = $this->_endpoint . 'mentions_timeline.' . $this->_format;
return $this->_twitter->request->get($url, $params);
}
/**
* Returns a user's friends, each with current status inline. They are
* ordered by the order in which they were added as friends, 100 at a time.
*
* http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses%C2%A0friends
*
* @param array $params Parameters for the friends_timeline call
*
* id - For this user id or screen name.
* Current user if left out.
* user_id - Specfies the ID of the user for whom to return the
* user_timeline. Helpful for disambiguating when a valid
* user ID is also a valid screen name.
* screen_id - Specfies the screen name of the user for whom to return
* the user_timeline. Helpful for disambiguating when a
* valid screen name is also a user ID.
* page - The page number to return (note there are pagination limits)
*
* @return unknown_type
*/
public function friends($params = array())
{
$url = $this->_endpoint . 'friends.' . $this->_format;
return $this->_twitter->request->get($url, $params);
}
/**
* Returns a user's followers, each with current status inline. They are
* ordered by the order in which they were added as friends, 100 at a time.
*
* http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses%C2%A0friends
*
* @param array $params Parameters for the friends_timeline call
*
* id - For this user id or screen name.
* Current user if left out.
* user_id - Specfies the ID of the user for whom to return the
* user_timeline. Helpful for disambiguating when a valid
* user ID is also a valid screen name.
* screen_id - Specfies the screen name of the user for whom to return
* the user_timeline. Helpful for disambiguating when a
* valid screen name is also a user ID.
* page - The page number to return (note there are pagination limits)
*
* @return unknown_type
*/
public function followers($params = array())
{
$url = $this->_endpoint . 'followers.' . $this->_format;
return $this->_twitter->request->get($url, $params);
}
}
Horde_Service_Twitter-2.1.5/lib/Horde/Service/Twitter.php 0000664 0000765 0000024 00000016175 12535652765 020316 0 ustar
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Service_Twitter
*/
/**
* Horde_Service_Twitter class abstracts communication with Twitter's
* rest interface.
*
* @property-read Horde_Service_Twitter_Account $account
* The twitter account object for calling account methods.
* @property-read Horde_Service_Twitter_Statuses $statuses
* The twitter status object for updating and retrieving user
* statuses.
* @property-read Horde_Service_Twitter_Auth $auth
* The twitter authentication object.
* @property-read Horde_Service_Twitter_Request $request
* The twitter request object that wraps sending requests to
* Twitter's REST API.
* @property-read Horde_Cache $responseCache
* The cache object.
* @property-read integer $cacheLifetime
* The default cache lifetime.
*
* @author Michael J. Rubinsky
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Service_Twitter
*/
class Horde_Service_Twitter
{
/* Constants */
const REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token';
const USER_AUTHORIZE_URL = 'https://api.twitter.com/oauth/authorize';
const ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token';
/**
* Cache for the various objects we lazy load in __get()
*
* @var hash of Horde_Service_Twitter_* objects
*/
protected $_objCache = array();
/**
* (Optional) Cache object.
*
* @var Horde_Cache
*/
protected $_responseCache;
/**
* Default cache lifetime.
*
* @var integer
*/
protected $_cacheLifetime = 300;
/**
* Optional logger.
*
* @var Horde_Log_Logger
*/
protected $_logger;
/**
* Can't lazy load the auth or request class since we need to know early if
* we are OAuth or Basic
*
* @var Horde_Service_Twitter_Auth
*/
protected $_auth;
/**
* The twitter request object.
*
* @var Horde_Service_Twitter_Request
*/
protected $_request;
/**
* The http client.
*
* @var Horde_Http_Client
*/
protected $_httpClient;
/**
* Constructor.
*
* @param Horde_Service_Twitter_Auth $auth An authentication object
* @param Horde_Service_Twitter_Request $request A request object.
*/
public function __construct(Horde_Service_Twitter_Auth $auth,
Horde_Service_Twitter_Request $request)
{
$this->_auth = $auth;
$this->_auth->setTwitter($this);
$this->_request = $request;
$this->_request->setTwitter($this);
}
/**
* Factory method to easily build a working Twitter client object.
*
* @param array $params Configuration parameters, with the following keys:
* - 'oauth' (required):
* - 'consumer_key' (required): The application's
* consumer key
* - 'consumer_secret' (required): The application's
* consumer secret
* - 'access_token' (optional): The user's access
* token
* - 'access_token_secret' (optional): The user's
* access token secret.
* - 'http' (optional): any configuration parameters
* for Horde_Http_Client, e.g. proxy settings.
*
* @return Horde_Service_Twitter A twitter object that can be used
* immediately to update and receive
* statuses etc.
*/
public static function create($params)
{
if (!isset($params['oauth'])) {
throw new Horde_Service_Twitter_Exception('Only OAuth authentication is supported.');
}
/* Parameters required for the Horde_Oauth_Consumer */
$consumer_params = array(
'key' => $params['oauth']['consumer_key'],
'secret' => $params['oauth']['consumer_secret'],
'requestTokenUrl' => self::REQUEST_TOKEN_URL,
'authorizeTokenUrl' => self::USER_AUTHORIZE_URL,
'accessTokenUrl' => self::ACCESS_TOKEN_URL,
'signatureMethod' => new Horde_Oauth_SignatureMethod_HmacSha1());
/* Create the Consumer */
$oauth = new Horde_Oauth_Consumer($consumer_params);
/* Create the Twitter client */
$twitter = new Horde_Service_Twitter(
new Horde_Service_Twitter_Auth_Oauth($oauth),
new Horde_Service_Twitter_Request_Oauth(
new Horde_Controller_Request_Http()));
/* Create HTTP client. */
$http_params = isset($params['http']) ? $params['http'] : array();
$twitter->setHttpClient(new Horde_Http_Client($http_params));
/* Check for an existing token */
if (!empty($params['oauth']['access_token']) &&
!empty($params['oauth']['access_token_secret'])) {
$auth_token = new Horde_Oauth_Token(
$params['oauth']['access_token'],
$params['oauth']['access_token_secret']);
$twitter->auth->setToken($auth_token);
}
return $twitter;
}
public function setCache(Horde_Cache $cache)
{
$this->_responseCache = $cache;
}
public function setLogger(Horde_Log_Logger $logger)
{
$this->_logger = $logger;
}
/**
* Set the http client.
*
* @param Horde_Http_Client $client The http client
*/
public function setHttpClient(Horde_Http_Client $client)
{
$this->_httpClient = $client;
}
/**
* Get the http client.
*
* @return Horde_Http_Client
*/
public function getHttpClient()
{
return $this->_httpClient;
}
/**
* Lazy load the twitter classes.
*
* @param string $value The lowercase representation of the subclass.
*
* @throws Horde_Service_Twitter_Exception
* @return Horde_Service_Twitter_* object.
*/
public function __get($value)
{
// First, see if it's an allowed protected value.
switch ($value) {
case 'auth':
return $this->_auth;
case 'request':
return $this->_request;
case 'responseCache':
return $this->_responseCache;
case 'cacheLifetime':
return $this->_cacheLifetime;
}
// If not, assume it's a method/action class...
$class = 'Horde_Service_Twitter_' . Horde_String::ucfirst($value);
if (!empty($this->_objCache[$class])) {
return $this->_objCache[$class];
}
if (!class_exists($class)) {
throw new Horde_Service_Twitter_Exception(sprintf("%s class not found", $class));
}
$this->_objCache[$class] = new $class($this);
return $this->_objCache[$class];
}
}