From: Michael J. Rubinsky Date: Sat, 18 Jul 2009 22:49:25 +0000 (-0400) Subject: Initial, very basic, code for Service_Twitter X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=32b0ff14b6e71cb833b628d50c8ce4f2e43ca18c;p=horde.git Initial, very basic, code for Service_Twitter The only functional part at this point it the OAuth authentication and setting status. See the example file in doc/ --- diff --git a/framework/Service_Twitter/doc/twitter.php.example b/framework/Service_Twitter/doc/twitter.php.example new file mode 100644 index 000000000..4fa2d7f4c --- /dev/null +++ b/framework/Service_Twitter/doc/twitter.php.example @@ -0,0 +1,86 @@ + $consumer_key, + 'secret' => $consumer_secret, + 'requestTokenUrl' => $token_url, + 'authorizeTokenUrl' => $auth_url, + 'accessTokenUrl' => $accessToken_url, + 'signatureMethod' => new Horde_Oauth_SignatureMethod_HmacSha1()); + +/* Create the Consumer */ +$oauth = new Horde_Oauth_Consumer($params); + +/* Create the Twitter client */ +$twitter = new Horde_Service_Twitter(array('oauth' => $oauth, + 'request' => new Horde_Controller_Request_Http())); +/* At this point we would check for an existing, valid authorization token */ +// $auth_token should be a Horde_Oauth_Token object +// $auth_token = getTokenFromStorage(); + +// Do we have a good auth token? Keep in mind this is example code, and in a true +// callback page we probably wouldn't be doing anything if we already have a token, +// but for testing purposes.... +if (!empty($auth_token)) { + /* Have a token, tell the Twitter client about it */ + $twitter->auth->setToken($auth_token); + + // Do something cool.... + // $twitter->statuses->update('Testing Horde/Twitter integration'); + +} elseif (!empty($_SESSION['twitter_request_secret'])) { + /* No existing auth token, maybe we are in the process of getting it? */ + $a_token = $twitter->auth->getAccessToken(new Horde_Controller_Request_Http(), + $_SESSION['twitter_request_secret']); + + // Clear the request secret from the session now that we're done with it, + // again, using _SESSION for simplicity for this example + $_SESSION['twitter_request_secret'] = ''; + + if ($a_token === false || empty($a_token)) { + // We had a request secret, but something went wrong. maybe navigated + // back here between requests? + echo 'error'; + die; + } else { + // We have a good token, save it to DB etc.... + var_dump($a_token); + die; + } +} + +// No auth token, not in the process of getting one...ask user to verify +$results = $twitter->auth->getRequestToken(); +$_SESSION['twitter_request_secret'] = $results->secret; + +// Redirect to auth url +header('Location:' . Horde::externalUrl($twitter->auth->getUserAuthorizationUrl($results), false)); diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter.php b/framework/Service_Twitter/lib/Horde/Service/Twitter.php new file mode 100644 index 000000000..0a86214a5 --- /dev/null +++ b/framework/Service_Twitter/lib/Horde/Service/Twitter.php @@ -0,0 +1,100 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Service_Twitter + */ +class Horde_Service_Twitter +{ + + /** + * Cache for the various objects we lazy load in __get() + * + * @var hash of Horde_Service_Twitter_* objects + */ + protected $_objCache = array(); + + protected $_config; + + /** + * Const'r + * + * @param array $config Configuration parameters: + *
+     *     'oauth'  - Horde_Oauth object
+     */
+    public function __construct($config)
+    {
+        // TODO: Check for req'd config
+        $this->_config = $config;
+
+    }
+
+    /**
+     * 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 'oauth':
+            return $this->_config['oauth'];
+
+        }
+
+        // If not, assume it's a method/action class...
+        $class = 'Horde_Service_Twitter_' . 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, $this->oauth);
+        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();
+    }
+
+}
diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Account.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Account.php
new file mode 100644
index 000000000..b3d9bbc7f
--- /dev/null
+++ b/framework/Service_Twitter/lib/Horde/Service/Twitter/Account.php
@@ -0,0 +1 @@
+
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package Horde_Service_Twitter
+ */
+class Horde_Service_Twitter_Auth {
+
+    /**
+     *
+     * @var Horde_Service_Twitter
+     */
+    protected $_twitter;
+
+    /**
+     *
+     */
+    protected $_token;
+
+    /**
+     * Const'r
+     *
+     * @return Horde_Service_Twitter_Auth
+     */
+    public function __construct($twitter, $oauth)
+    {
+        $this->_twitter = $twitter;
+    }
+
+    /**
+     * 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)
+    {
+        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->_twitter->oauth->getAccessToken($token);
+    }
+
+    public function getRequestToken($params = array())
+    {
+        return $this->_twitter->oauth->getRequestToken($params);
+    }
+
+}
diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Exception.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Exception.php
new file mode 100644
index 000000000..e9c2bd2eb
--- /dev/null
+++ b/framework/Service_Twitter/lib/Horde/Service/Twitter/Exception.php
@@ -0,0 +1,4 @@
+
diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Statuses.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Statuses.php
new file mode 100644
index 000000000..234b45aeb
--- /dev/null
+++ b/framework/Service_Twitter/lib/Horde/Service/Twitter/Statuses.php
@@ -0,0 +1,35 @@
+
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package Horde_Service_Twitter
+ */
+class Horde_Service_Twitter_Statuses
+{
+
+    public function __construct($twitter, $oauth)
+    {
+        $this->_twitter = $twitter;
+    }
+
+    /**
+     * Obtain the requested status
+     *
+     * @return unknown_type
+     */
+    public function show($id)
+    {
+
+    }
+
+    public function update($status)
+    {
+        $url = 'http://twitter.com/statuses/update.json';
+        return $this->_twitter->postRequest($url, array('status' => $status));
+    }
+}
diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Timeline.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Timeline.php
new file mode 100644
index 000000000..b3d9bbc7f
--- /dev/null
+++ b/framework/Service_Twitter/lib/Horde/Service/Twitter/Timeline.php
@@ -0,0 +1 @@
+
+
+ Service_Twitter
+ pear.horde.org
+ Horde Twitter client
+ This package provides client libraries for the Twitter REST API
+ 
+ 
+  Michael J. Rubinsky
+  mrubinsk
+  mrubinsk@horde.org
+  yes
+ 
+ 2009-07-18
+ 
+  0.1.0
+  0.1.0
+ 
+ 
+  alpha
+  alpha
+ 
+ BSD
+ 
+* Initial release
+ 
+ 
+  
+   
+    
+     
+      
+       
+       
+       
+       
+      
+      
+     
+    
+   
+ 
+ 
+  
+   
+    5.2.0
+   
+   
+    1.5.0
+   
+   
+    Http_Client
+    pear.horde.org
+   
+  
+ 
+ 
+  
+   
+   
+   
+   
+  
+ 
+