]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/twitterbasicauthclient.php
use home_timeline instead of friends_timeline
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitterbasicauthclient.php
index 7ee8d7d4c4d5210e668cf7de3eacb726c93520f4..2c18c94695619ef2ad37cbd648cc157962e7f796 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.
  *
@@ -62,18 +76,21 @@ class TwitterBasicAuthClient
     /**
      * Calls Twitter's /statuses/update API method
      *
-     * @param string $status                text of the status
-     * @param int    $in_reply_to_status_id optional id of the status it's
-     *                                      a reply to
+     * @param string $status  text of the status
+     * @param mixed  $params  optional other parameters to pass to Twitter,
+     *                        as defined. For back-compatibility, if an int
+     *                        is passed we'll consider it a reply-to ID.
      *
      * @return mixed the status
      */
     function statusesUpdate($status, $in_reply_to_status_id = null)
     {
         $url      = 'https://twitter.com/statuses/update.json';
-        $params   = array('status' => $status,
-                          'source' => common_config('integration', 'source'),
-                          'in_reply_to_status_id' => $in_reply_to_status_id);
+        if (is_numeric($params)) {
+            $params = array('in_reply_to_status_id' => intval($params));
+        }
+        $params['status'] = $status;
+        $params['source'] = common_config('integration', 'source');
         $response = $this->httpRequest($url, $params);
         $status   = json_decode($response);
         return $status;
@@ -169,12 +186,13 @@ 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)
     {
@@ -199,6 +217,12 @@ class TwitterBasicAuthClient
             $response = $request->get($url);
         }
 
+        $code = $response->getStatus();
+
+        if ($code < 200 || $code >= 400) {
+            throw new BasicAuthException($response->getBody(), $code);
+        }
+
         return $response->getBody();
     }