From ba321d2d1745ffb077c313bd60670b8d0b7f28f0 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Thu, 1 Jul 2010 12:50:52 -0400 Subject: [PATCH] Inject dependencies, remove the Basic Auth classes. Twitter is removing the HTTP Basic Auth ability from it's API in August 2010. All clients MUST use OAuth. --- .../Service_Twitter/lib/Horde/Service/Twitter.php | 63 ++++++---------- .../lib/Horde/Service/Twitter/Auth.php | 10 +-- .../lib/Horde/Service/Twitter/Auth/Basic.php | 28 -------- .../lib/Horde/Service/Twitter/Auth/Oauth.php | 5 ++ .../lib/Horde/Service/Twitter/Request.php | 27 +++---- .../lib/Horde/Service/Twitter/Request/Basic.php | 83 ---------------------- .../lib/Horde/Service/Twitter/Request/Oauth.php | 16 ----- framework/Service_Twitter/package.xml | 60 ++++++++++------ 8 files changed, 77 insertions(+), 215 deletions(-) delete mode 100644 framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Basic.php delete mode 100644 framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter.php b/framework/Service_Twitter/lib/Horde/Service/Twitter.php index 77aaf7a0f..a8adcff8d 100644 --- a/framework/Service_Twitter/lib/Horde/Service/Twitter.php +++ b/framework/Service_Twitter/lib/Horde/Service/Twitter.php @@ -12,7 +12,6 @@ */ class Horde_Service_Twitter { - /* Constants */ const REQUEST_TOKEN_URL = 'http://twitter.com/oauth/request_token'; const USER_AUTHORIZE_URL = 'http://twitter.com/oauth/authorize'; @@ -32,10 +31,15 @@ class Horde_Service_Twitter */ protected $_responseCache; + /** + * Default cache lifetime. + * + * @var integer + */ protected $_cacheLifetime = 300; - /** + * Optional logger. * * @var Horde_Log_Logger */ @@ -49,28 +53,22 @@ class Horde_Service_Twitter protected $_config; /** - * Type of authentication (Oauth, Basic) - * - * @var string - */ - protected $_authType; - - /** * Can't lazy load the auth or request class since we need to know early if - * we are OAuth or Basic + * we are OAuth or Basic * * @var Horde_Service_Twitter_Auth */ protected $_auth; /** + * The twitter request object. * * @var Horde_Service_Twitter_Request */ protected $_request; /** - * Hold the http client. + * The http client. * * @var Horde_Http_Client */ @@ -86,39 +84,22 @@ class Horde_Service_Twitter * 'password' - if using Basic auth * */ - public function __construct($config) + public function __construct(Horde_Service_Twitter_Auth $auth, Horde_Service_Twitter_Request $request) { - // TODO: Check for req'd config - $this->_config = $config; - - if (!empty($config['cache'])) { - $this->_responseCache = $config['cache']; - if (!empty($config['cache_lifetime'])) { - $this->_cacheLifetime = $config['cache_lifetime']; - } - } - - if (!empty($config['logger'])) { - $this->_logger = $config['logger']; - } - - // Need to determine the type of authentication we will be using early.. - if (!empty($config['oauth'])) { - // OAuth - $this->_authType = 'Oauth'; - $params = array('oauth' => $config['oauth']); - } elseif (!empty($config['username']) && !empty($config['password'])) { - // Http_Basic - $this->_authType = 'Basic'; - $params = array('username' => $config['username'], - 'password' => $config['password']); - } + $this->_auth = $auth; + $this->_auth->setTwitter($this); + $this->_request = $request; + $this->_request->setTwitter($this); + } - $aclass = 'Horde_Service_Twitter_Auth_' . $this->_authType; - $rclass = 'Horde_Service_Twitter_Request_' . $this->_authType; + public function setCache(Horde_Cache_Base $cache) + { + $this->_responseCache = $cache; + } - $this->_auth = new $aclass($this, $params); - $this->_request = new $rclass($this); + public function setLogger(Horde_Log_Logger $logger) + { + $this->_logger = $logger; } /** diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth.php index 1b6a82ed2..050d20138 100644 --- a/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth.php +++ b/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth.php @@ -25,16 +25,10 @@ abstract class Horde_Service_Twitter_Auth */ protected $_config; - /** - * Const'r - * - * @param Horde_Serivce_Twitter $twitter - * @param array $config - */ - public function __construct($twitter, $config) + + public function setTwitter(Horde_Service_Twitter $twitter) { $this->_twitter = $twitter; - $this->_config = $config; } /** diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Basic.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Basic.php deleted file mode 100644 index 5d3e8e449..000000000 --- a/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Basic.php +++ /dev/null @@ -1,28 +0,0 @@ - - * @license http://opensource.org/licenses/bsd-license.php BSD - * @category Horde - * @package Horde_Service_Twitter - */ -class Horde_Service_Twitter_Auth_Basic extends Horde_Service_Twitter_Auth -{ - protected static $_authorizationHeader; - - public function buildAuthorizationHeader() - { - if (empty(self::$_authorizationHeader)) { - self::$_authorizationHeader = 'Basic ' . base64_encode($this->username . ':' . $this->password); - } - - return self::$_authorizationHeader; - } - -} diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Oauth.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Oauth.php index 624a96db2..a50578629 100644 --- a/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Oauth.php +++ b/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Oauth.php @@ -19,6 +19,11 @@ class Horde_Service_Twitter_Auth_Oauth extends Horde_Service_Twitter_Auth */ protected $_token; + public function __construct(Horde_OAuth_Consumer $oauth) + { + $this->_config['oauth'] = $oauth; + } + /** * Obtain the URL used to get an authorization token. * diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request.php index 3d03c2a4a..30942b391 100644 --- a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request.php +++ b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request.php @@ -12,23 +12,18 @@ */ abstract class Horde_Service_Twitter_Request { - /** - * - * @var Horde_Service_Twitter - */ - protected $_twitter; + /** + * + * @var Horde_Service_Twitter + */ + protected $_twitter; - /** - * Const'r - * - * @param Horde_Service_Twitter $twitter - */ - public function __construct($twitter) - { - $this->_twitter = $twitter; - } + public function setTwitter(Horde_Service_Twitter $twitter) + { + $this->_twitter = $twitter; + } - abstract public function get($url, $params = array()); - abstract public function post($url, $params = array()); + abstract public function get($url, $params = array()); + abstract public function post($url, $params = array()); } diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php deleted file mode 100644 index f4dc34a5c..000000000 --- a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php +++ /dev/null @@ -1,83 +0,0 @@ - - * @license http://opensource.org/licenses/bsd-license.php BSD - * @category Horde - * @package Horde_Service_Twitter - */ -class Horde_Service_Twitter_Request_Basic extends Horde_Service_Twitter_Request -{ - /** - * - * @var Horde_Service_Twitter - */ - protected $_twitter; - - /** - * Const'r - * - * @param Horde_Service_Twitter $twitter - */ - public function __construct($twitter) - { - $this->_twitter = $twitter; - } - - /** - * Perform a GET request. - * - * @param string $url The URL for the request - * @param array $params - * - * @return mixed The response - */ - public function get($url, $params = array()) - { - $key = md5($url . 'get' . serialize($params) . $this->_twitter->auth->username); - $cache = $this->_twitter->responseCache; - if (!empty($cache) && $results = $cache->get($key, $this->_twitter->cacheLifetime)) { - return $results; - } - try { - $response = $this->_twitter->getHttpClient()->get($url, array('Authorization' => $this->_twitter->auth->buildAuthorizationHeader())); - } catch (Horde_Http_Exception $e) { - throw new Horde_Service_Twitter_Exception($e); - } - - $body = $response->getBody(); - if ($response->code >= 400 && $response->code <= 500) { - throw new Horde_Service_Twitter_Exception($body); - } - if (!empty($cache)) { - $cache->set($key, $body); - } - - return $body; - } - - /** - * Perform a POST request - * - * @see self::get - */ - public function post($url, $params = array()) - { - $client = new Horde_Http_Client(); - try { - $response = $this->_twitter->getHttpClient()->post($url, $params, array('Authorization' => $this->_twitter->auth->buildAuthorizationHeader())); - } 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($body); - } - return $response->getBody(); - } - -} diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php index 5845c5552..2dad95545 100644 --- a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php +++ b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php @@ -13,22 +13,6 @@ class Horde_Service_Twitter_Request_Oauth extends Horde_Service_Twitter_Request { /** - * - * @var Horde_Service_Twitter - */ - protected $_twitter; - - /** - * Const'r - * - * @param Horde_Service_Twitter $twitter - */ - public function __construct($twitter) - { - $this->_twitter = $twitter; - } - - /** * Perform a GET request with OAuth authorization. * * @param mixed (string | Horde_Url) $url The url to request. diff --git a/framework/Service_Twitter/package.xml b/framework/Service_Twitter/package.xml index 910be2f86..5c82be3c1 100644 --- a/framework/Service_Twitter/package.xml +++ b/framework/Service_Twitter/package.xml @@ -1,20 +1,17 @@ - + Service_Twitter pear.horde.org Horde Twitter client - This package provides client libraries for the Twitter REST API - + This package provides client libraries for the Twitter REST API Michael J. Rubinsky mrubinsk mrubinsk@horde.org yes - 2009-07-18 + 2010-07-01 + 0.1.0 0.1.0 @@ -28,25 +25,26 @@ http://pear.php.net/dtd/package-2.0.xsd"> * Initial release - + + + + - - + - + - - + @@ -69,16 +67,32 @@ http://pear.php.net/dtd/package-2.0.xsd"> - - - - - - - - - - + + + + + + + + + + + + + 0.1.0 + 0.1.0 + + + alpha + alpha + + 2010-07-01 + BSD + +* Initial release + + + -- 2.11.0