Twitter is removing the HTTP Basic Auth ability from it's API in August 2010.
All clients MUST use OAuth.
*/
class Horde_Service_Twitter
{
-
/* Constants */
const REQUEST_TOKEN_URL = 'http://twitter.com/oauth/request_token';
const USER_AUTHORIZE_URL = 'http://twitter.com/oauth/authorize';
*/
protected $_responseCache;
+ /**
+ * Default cache lifetime.
+ *
+ * @var integer
+ */
protected $_cacheLifetime = 300;
-
/**
+ * Optional logger.
*
* @var Horde_Log_Logger
*/
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
*/
* 'password' - if using Basic auth
* </pre>
*/
- 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;
}
/**
*/
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;
}
/**
+++ /dev/null
-<?php
-/**
- * Horde_Service_Twitter_Auth class to abstract all auth related tasks
- *
- * Basically implements Horde_Oauth_Client and passes the calls along to the
- * protected oauth object.
- *
- * Copyright 2009-2010 The Horde Project (http://www.horde.org)
- *
- * @author Michael J. Rubinsky <mrubinsk@horde.org>
- * @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;
- }
-
-}
*/
protected $_token;
+ public function __construct(Horde_OAuth_Consumer $oauth)
+ {
+ $this->_config['oauth'] = $oauth;
+ }
+
/**
* Obtain the URL used to get an authorization token.
*
*/
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());
}
+++ /dev/null
-<?php
-/**
- * Horde_Service_Twitter_Request_Basic class wraps sending requests to Twitter's
- * REST API using http basic authentication.
- *
- * Copyright 2009-2010 The Horde Project (http://www.horde.org)
- *
- * @author Michael J. Rubinsky <mrubinsk@horde.org>
- * @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();
- }
-
-}
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.
<?xml version="1.0" encoding="UTF-8"?>
-<package packagerversion="1.4.9" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0
-http://pear.php.net/dtd/tasks-1.0.xsd
-http://pear.php.net/dtd/package-2.0
-http://pear.php.net/dtd/package-2.0.xsd">
+<package packagerversion="1.9.0" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
<name>Service_Twitter</name>
<channel>pear.horde.org</channel>
<summary>Horde Twitter client</summary>
- <description>This package provides client libraries for the Twitter REST API
- </description>
+ <description>This package provides client libraries for the Twitter REST API</description>
<lead>
<name>Michael J. Rubinsky</name>
<user>mrubinsk</user>
<email>mrubinsk@horde.org</email>
<active>yes</active>
</lead>
- <date>2009-07-18</date>
+ <date>2010-07-01</date>
+ <time>12:46:10</time>
<version>
<release>0.1.0</release>
<api>0.1.0</api>
* Initial release
</notes>
<contents>
- <dir name="/">
+ <dir baseinstalldir="/" name="/">
+ <dir name="doc">
+ <file name="twitter.php.example" role="doc" />
+ </dir> <!-- /doc -->
<dir name="lib">
<dir name="Horde">
<dir name="Service">
<dir name="Twitter">
<dir name="Auth">
<file name="Oauth.php" role="php" />
- <file name="Basic.php" role="php" />
</dir> <!-- /lib/Horde/Service/Twitter/Auth -->
<dir name="Request">
<file name="Oauth.php" role="php" />
- <file name="Basic.php" role="php" />
</dir> <!-- /lib/Horde/Service/Twitter/Request -->
+ <file name="Account.php" role="php" />
<file name="Auth.php" role="php" />
- <file name="Request.php" role="php" />
<file name="Exception.php" role="php" />
+ <file name="Request.php" role="php" />
<file name="Statuses.php" role="php" />
- <file name="Account.php" role="php" />
- </dir> <!-- /lib/Horde/Service/Twitter-->
+ </dir> <!-- /lib/Horde/Service/Twitter -->
<file name="Twitter.php" role="php" />
</dir> <!-- /lib/Horde/Service -->
</dir> <!-- /lib/Horde -->
</dependencies>
<phprelease>
<filelist>
- <install name="lib/Horde/Service/Twitter/Auth.php" as="Horde/Service/Twitter/Auth.php" />
- <install name="lib/Horde/Service/Twitter/Auth/Oauth.php" as="Horde/Service/Twitter/Auth/Oauth.php" />
- <install name="lib/Horde/Service/Twitter/Auth/Basic.php" as="Horde/Service/Twitter/Auth/Basic.php" />
- <install name="lib/Horde/Service/Twitter/Exception.php" as="Horde/Service/Twitter/Exception.php" />
- <install name="lib/Horde/Service/Twitter/Statuses.php" as="Horde/Service/Twitter/Statuses.php" />
- <install name="lib/Horde/Service/Twitter/Account.php" as="Horde/Service/Twitter/Account.php" />
- <install name="lib/Horde/Service/Twitter/Request.php" as="Horde/Service/Twitter/Request.php" />
- <install name="lib/Horde/Service/Twitter/Request/Oauth.php" as="Horde/Service/Twitter/Request/Oauth.php" />
- <install name="lib/Horde/Service/Twitter/Request/Basic.php" as="Horde/Service/Twitter/Request/Basic.php" />
- <install name="lib/Horde/Service/Twitter.php" as="Horde/Service/Twitter.php" />
+ <install as="twitter.php.example" name="doc/twitter.php.example" />
+ <install as="Horde/Service/Twitter.php" name="lib/Horde/Service/Twitter.php" />
+ <install as="Horde/Service/Twitter/Account.php" name="lib/Horde/Service/Twitter/Account.php" />
+ <install as="Horde/Service/Twitter/Auth.php" name="lib/Horde/Service/Twitter/Auth.php" />
+ <install as="Horde/Service/Twitter/Exception.php" name="lib/Horde/Service/Twitter/Exception.php" />
+ <install as="Horde/Service/Twitter/Request.php" name="lib/Horde/Service/Twitter/Request.php" />
+ <install as="Horde/Service/Twitter/Statuses.php" name="lib/Horde/Service/Twitter/Statuses.php" />
+ <install as="Horde/Service/Twitter/Auth/Oauth.php" name="lib/Horde/Service/Twitter/Auth/Oauth.php" />
+ <install as="Horde/Service/Twitter/Request/Oauth.php" name="lib/Horde/Service/Twitter/Request/Oauth.php" />
</filelist>
</phprelease>
+ <changelog>
+ <release>
+ <version>
+ <release>0.1.0</release>
+ <api>0.1.0</api>
+ </version>
+ <stability>
+ <release>alpha</release>
+ <api>alpha</api>
+ </stability>
+ <date>2010-07-01</date>
+ <license uri="http://opensource.org/licenses/bsd-license.php">BSD</license>
+ <notes>
+* Initial release
+ </notes>
+ </release>
+ </changelog>
</package>