*/
protected $_objCache = array();
+ /**
+ * Configuration values
+ *
+ * @var array
+ */
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
+ *
+ * @var Horde_Service_Twitter_Auth
+ */
+ protected $_auth;
+
+ /**
+ *
+ * @var Horde_Service_Twitter_Request
+ */
+ protected $_request;
+
+ /**
* Const'r
*
* @param array $config Configuration parameters:
* <pre>
- * 'oauth' - Horde_Oauth object
+ * 'oauth' - Horde_Oauth object if using Oauth
+ * 'username' - if using Basic auth
+ * 'password' - if using Basic auth
+ * </pre>
*/
public function __construct($config)
{
// TODO: Check for req'd config
$this->_config = $config;
+ // 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();
+ }
+
+ $aclass = 'Horde_Service_Twitter_Auth_' . $this->_authType;
+ $rclass = 'Horde_Service_Twitter_Request_' . $this->_authType;
+
+ $this->_auth = new $aclass($this, $params);
+ $this->_request = new $rclass($this);
}
/**
{
// First, see if it's an allowed protected value.
switch ($value) {
- case 'oauth':
- return $this->_config['oauth'];
-
+ case 'auth':
+ return $this->_auth;
+ case 'request':
+ return $this->_request;
}
// If not, assume it's a method/action class...
}
- $this->_objCache[$class] = new $class($this, $this->oauth);
+ $this->_objCache[$class] = new $class($this);
return $this->_objCache[$class];
}
- /**
- * Send a request to the Twitter api
- *
- * @param $url
- * @param $params
- * @return unknown_type
- */
- public function getRequest($url, $params = array())
- {
- $request = new Horde_Oauth_Request($url, $params);
- $request->sign($this->oauth->signatureMethod, $this->oauth, $this->auth->getAccessToken());
-
- $client = new Horde_Http_Client();
- $response = $client->get($url, array('Authorization' => $request->buildAuthorizationHeader()));
-
- return $response->getBody();
- }
-
- public function postRequest($url, $params = array())
- {
- $request = new Horde_Oauth_Request($url, $params);
- $request->sign($this->oauth->signatureMethod, $this->oauth, $this->auth->getAccessToken());
-
- $client = new Horde_Http_Client();
- $response = $client->post($url, $params, array('Authorization' => $request->buildAuthorizationHeader()));
-
- return $response->getBody();
- }
-
}
<?php
+/**
+ * Horde_Service_Twitter_Account class for calling account methods
+ *
+ * Copyright 2009 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_Account
+{
+ protected $_endpoint = 'http://twitter.com/account/';
+ protected $_format = 'json';
+
+ public function __construct($twitter)
+ {
+ $this->_twitter = $twitter;
+ }
+
+ public function verifyCredentials()
+ {
+ $url = $this->_endpoint . 'verify_credentials.' . $this->_format;
+ return $this->_twitter->request->get($url);
+ }
+
+}
<?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.
+ * Horde_Service_Twitter_Auth_* classes to abstract all auth related tasks for
+ * various auth mechanisms.
*
* Copyright 2009 The Horde Project (http://www.horde.org)
*
* @category Horde
* @package Horde_Service_Twitter
*/
-class Horde_Service_Twitter_Auth {
-
+abstract class Horde_Service_Twitter_Auth
+{
/**
*
* @var Horde_Service_Twitter
protected $_twitter;
/**
+ * Configuration parameters
*
+ * @param array
*/
- protected $_token;
+ protected $_config;
/**
* Const'r
*
* @return Horde_Service_Twitter_Auth
*/
- public function __construct($twitter, $oauth)
+ public function __construct($twitter, $config)
{
$this->_twitter = $twitter;
+ $this->_config = $config;
}
- /**
- * 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->_twitter->oauth->getUserAuthorizationUrl($requestToken);
- }
-
- /**
- * Set the access token
- *
- * @param $token
- * @return unknown_type
- */
- public function setToken($token)
- {
- // @TODO: sanity check this
- $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 Horde_Oauth_Token $requestSecret The token secret returned by
- * Twitter after the user authorizes
- * the application.
- * @return Horde_Oauth_Token
- */
- public function getAccessToken($request = null, $requestSecret = null)
+ public function __get($value)
{
- if (!empty($this->_token)) {
- return $this->_token;
+ if (!empty($this->_config[$value])) {
+ return $this->_config[$value];
}
- //@TODO: Verify the existence of requestSecret...
-
- $params = $request->getGetParams();
- if (empty($params['oauth_token'])) {
- return false;
- }
- $token = new Horde_Oauth_Token($params['oauth_token'], $requestSecret);
-
- return $this->_twitter->oauth->getAccessToken($token);
- }
-
- public function getRequestToken($params = array())
- {
- return $this->_twitter->oauth->getRequestToken($params);
+ throw new Horde_Service_Twitter_Exception(sprintf("The property %s does not exist", $value));
}
}
--- /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 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_Oauth extends Horde_Service_Twitter_Auth
+{
+ /**
+ *
+ */
+ protected $_token;
+
+ /**
+ * 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 $token
+ * @return unknown_type
+ */
+ public function setToken($token)
+ {
+ // @TODO: sanity check this
+ $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 Horde_Oauth_Token $requestSecret The token secret returned by
+ * Twitter after the user authorizes
+ * the application.
+ * @return Horde_Oauth_Token
+ */
+ public function getAccessToken($request = null, $requestSecret = null)
+ {
+ if (!empty($this->_token)) {
+ return $this->_token;
+ }
+
+ //@TODO: Verify the existence of requestSecret...
+
+ $params = $request->getGetParams();
+ if (empty($params['oauth_token'])) {
+ return false;
+ }
+ $token = new Horde_Oauth_Token($params['oauth_token'], $requestSecret);
+
+ return $this->oauth->getAccessToken($token);
+ }
+
+ public function getRequestToken($params = array())
+ {
+ return $this->oauth->getRequestToken($params);
+ }
+
+}
--- /dev/null
+<?php
+/**
+ * Horde_Service_Twitter_Request_* classes wrap sending requests to Twitter's
+ * REST API using various authentication mechanisms.
+ *
+ * Copyright 2009 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
+ */
+abstract class Horde_Service_Twitter_Request
+{
+ protected $_twitter;
+
+ public function __construct($twitter)
+ {
+ $this->_twitter = $twitter;
+ }
+
+ abstract public function get($url, $params = array());
+ abstract public function post($url, $params = array());
+
+}
--- /dev/null
+<?php
+/**
+ * Horde_Service_Twitter_Request_Oauth class wraps sending requests to Twitter's
+ * REST API using OAuth authentication.
+ *
+ * Copyright 2009 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_Oauth extends Horde_Service_Twitter_Request
+{
+
+ protected $_twitter;
+
+ public function __construct($twitter)
+ {
+ $this->_twitter = $twitter;
+ }
+
+ public function get($url, $params = array())
+ {
+ $request = new Horde_Oauth_Request($url, $params);
+ $request->sign($this->_twitter->auth->oauth->signatureMethod,
+ $this->_twitter->auth->oauth,
+ $this->_twitter->auth->getAccessToken());
+ $client = new Horde_Http_Client();
+ $response = $client->get($url, array('Authorization' => $request->buildAuthorizationHeader('Twitter API')));
+
+ return $response->getBody();
+ }
+
+ public function post($url, $params = array())
+ {
+ $request = new Horde_Oauth_Request($url, $params);
+ $request->sign($this->_twitter->auth->oauth->signatureMethod,
+ $this->_twitter->auth->oauth,
+ $this->_twitter->auth->getAccessToken());
+
+ $client = new Horde_Http_Client();
+ $response = $client->post($url, $params, array('Authorization' => $request->buildAuthorizationHeader('Twitter API')));
+
+ return $response->getBody();
+ }
+
+}
class Horde_Service_Twitter_Statuses
{
- public function __construct($twitter, $oauth)
+ public function __construct($twitter)
{
$this->_twitter = $twitter;
}
public function update($status)
{
$url = 'http://twitter.com/statuses/update.json';
- return $this->_twitter->postRequest($url, array('status' => $status));
+ return $this->_twitter->request->post($url, array('status' => $status));
}
}
<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="Auth.php" role="php" />
+ <file name="Request.php" role="php" />
<file name="Exception.php" role="php" />
<file name="Statuses.php" role="php" />
+ <file name="Account.php" role="php" />
</dir> <!-- /lib/Horde/Service/Twitter-->
<file name="Twitter.php" role="php" />
</dir> <!-- /lib/Horde/Service -->
<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" />
</filelist>
</phprelease>