Add http basic authentication support.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Sun, 19 Jul 2009 18:09:18 +0000 (14:09 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Sun, 19 Jul 2009 18:09:18 +0000 (14:09 -0400)
If you instantiate the Horde_Service_Twitter object with a 'oauth'
parameter containing a Horde_Oauth object, it will use it...otherwise,
you should instantiate the object with username and password parameters
so it can use http basic

framework/Service_Twitter/lib/Horde/Service/Twitter.php
framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Basic.php [new file with mode: 0644]
framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php [new file with mode: 0644]
framework/Service_Twitter/lib/Horde/Service/Twitter/Statuses.php

index d3c224d..9584d44 100644 (file)
@@ -71,7 +71,8 @@ class Horde_Service_Twitter
         } elseif (!empty($config['username']) && !empty($config['password'])) {
             // Http_Basic
             $this->_authType = 'Basic';
-            $params = array();
+            $params = array('username' => $config['username'],
+                            'password' => $config['password']);
         }
 
         $aclass = 'Horde_Service_Twitter_Auth_' . $this->_authType;
diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Basic.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Basic.php
new file mode 100644 (file)
index 0000000..7413665
--- /dev/null
@@ -0,0 +1,28 @@
+<?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_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/Request/Basic.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php
new file mode 100644 (file)
index 0000000..792b0d0
--- /dev/null
@@ -0,0 +1,39 @@
+<?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_Basic extends Horde_Service_Twitter_Request
+{
+
+    protected $_twitter;
+
+    public function __construct($twitter)
+    {
+        $this->_twitter = $twitter;
+    }
+
+    public function get($url, $params = array())
+    {
+        $client = new Horde_Http_Client();
+        $response = $client->get($url, array('Authorization' => $this->_twitter->auth->buildAuthorizationHeader()));
+
+        return $response->getBody();
+    }
+
+    public function post($url, $params = array())
+    {
+        $client = new Horde_Http_Client();
+        $response = $client->post($url, $params, array('Authorization' => $this->_twitter->auth->buildAuthorizationHeader()));
+
+        return $response->getBody();
+    }
+
+}
index f48b4d7..ee7628c 100644 (file)
@@ -27,7 +27,7 @@ class Horde_Service_Twitter_Statuses
      */
     public function show($id)
     {
-        $url = $this->_endpoint . '/destroy.' . $this->_format;
+        $url = $this->_endpoint . 'show.' . $this->_format;
         return $this->_twitter->request->post($url, array('id' => $id));
     }
 
@@ -43,7 +43,7 @@ class Horde_Service_Twitter_Statuses
      */
     public function destroy($id)
     {
-        $url = $this->_endpoint . '/destroy.' . $this->_format;
+        $url = $this->_endpoint . 'destroy.' . $this->_format;
         return $this->_twitter->request->post($url, array('id' => $id));
     }
 
@@ -60,7 +60,7 @@ class Horde_Service_Twitter_Statuses
      */
     public function update($status, $replyTo = '')
     {
-        $url = $this->_endpoint . '/update.' . $this->_format;
+        $url = $this->_endpoint . 'update.' . $this->_format;
         $params = array('status' => $status);
         if (!empty($replyTo)) {
             $params['in_reply_to_status_id'] = $replyTo;