]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/twitterbasicauthclient.php
Lowercased switch cases in UAP Plugin
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitterbasicauthclient.php
index e4cae73734e80ef5c57411c514f90d35af4d1ddd..fd26293f9e40acf54ff4e63402c61813866cbd99 100644 (file)
@@ -31,6 +31,20 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
     exit(1);
 }
 
+/**
+ * General Exception wrapper for HTTP basic auth errors
+ *
+ *  @category Integration
+ *  @package  StatusNet
+ *  @author   Zach Copley <zach@status.net>
+ *  @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ *  @link     http://status.net/
+ *
+ */
+class BasicAuthException extends Exception
+{
+}
+
 /**
  * Class for talking to the Twitter API with HTTP Basic Auth.
  *
@@ -169,35 +183,43 @@ class TwitterBasicAuthClient
     }
 
     /**
-     * Make a HTTP request using cURL.
+     * Make an HTTP request
      *
      * @param string $url    Where to make the request
      * @param array  $params post parameters
      *
      * @return mixed the request
+     * @throws BasicAuthException
      */
     function httpRequest($url, $params = null, $auth = true)
     {
-        $request = new HTTPClient($url, 'GET', array(
+        $request = HTTPClient::start();
+        $request->setConfig(array(
             'follow_redirects' => true,
             'connect_timeout' => 120,
             'timeout' => 120,
-            'ssl_verifypeer' => false,
+            'ssl_verify_peer' => false,
+            'ssl_verify_host' => false
         ));
 
-        // Twitter is strict about accepting invalid "Expect" headers
-        $request->setHeader('Expect', '');
+        if ($auth) {
+            $request->setAuth($this->screen_name, $this->password);
+        }
 
         if (isset($params)) {
-            $request->setMethod('POST');
-            $request->addPostParameter($params);
+            // Twitter is strict about accepting invalid "Expect" headers
+            $headers = array('Expect:');
+            $response = $request->post($url, $headers, $params);
+        } else {
+            $response = $request->get($url);
         }
 
-        if ($auth) {
-            $request->setAuth($this->screen_name, $this->password);
+        $code = $response->getStatus();
+
+        if ($code < 200 || $code >= 400) {
+            throw new BasicAuthException($response->getBody(), $code);
         }
 
-        $response = $request->send();
         return $response->getBody();
     }