]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/twitteroauthclient.php
better handling of params in oauthget
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitteroauthclient.php
index ba45b533dc9b50fdf4a72075948dadeba14693cd..876e304259df7818afd5b67f708d80379e428845 100644 (file)
@@ -22,7 +22,7 @@
  * @category  Integration
  * @package   StatusNet
  * @author    Zach Copley <zach@status.net>
- * @copyright 2009 StatusNet, Inc.
+ * @copyright 2009-2010 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link      http://status.net/
  */
@@ -61,8 +61,23 @@ class TwitterOAuthClient extends OAuthClient
         $consumer_key    = common_config('twitter', 'consumer_key');
         $consumer_secret = common_config('twitter', 'consumer_secret');
 
-        parent::__construct($consumer_key, $consumer_secret,
-                            $oauth_token, $oauth_token_secret);
+        if (empty($consumer_key) && empty($consumer_secret)) {
+            $consumer_key = common_config(
+                'twitter',
+                'global_consumer_key'
+            );
+            $consumer_secret = common_config(
+                'twitter',
+                'global_consumer_secret'
+            );
+        }
+
+        parent::__construct(
+            $consumer_key,
+            $consumer_secret,
+            $oauth_token,
+            $oauth_token_secret
+        );
     }
 
     // XXX: the following two functions are to support the horrible hack
@@ -151,24 +166,29 @@ class TwitterOAuthClient extends OAuthClient
     /**
      * 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)
+    function statusesUpdate($status, $params=array())
     {
         $url      = 'https://twitter.com/statuses/update.json';
-        $params   = array('status' => $status,
-            '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;
+        // We don't have to pass 'source' as the oauth key is tied to an app.
+
         $response = $this->oAuthPost($url, $params);
         $status   = json_decode($response);
         return $status;
     }
 
     /**
-     * Calls Twitter's /statuses/friends_timeline API method
+     * Calls Twitter's /statuses/home_timeline API method
      *
      * @param int $since_id show statuses after this id
      * @param int $max_id   show statuses before this id
@@ -177,22 +197,28 @@ class TwitterOAuthClient extends OAuthClient
      *
      * @return mixed an array of statuses
      */
-    function statusesFriendsTimeline($since_id = null, $max_id = null,
-                                     $cnt = null, $page = null)
+    function statusesHomeTimeline($since_id = null, $max_id = null,
+                                  $cnt = null, $page = null)
     {
 
-        $url    = 'https://twitter.com/statuses/friends_timeline.json';
-        $params = array('since_id' => $since_id,
-                        'max_id' => $max_id,
-                        'count' => $cnt,
-                        'page' => $page);
-        $qry    = http_build_query($params);
+        $url    = 'https://twitter.com/statuses/home_timeline.json';
+
+        $params = array('include_entities' => 'true');
 
-        if (!empty($qry)) {
-            $url .= "?$qry";
+        if (!empty($since_id)) {
+            $params['since_id'] = $since_id;
+        }
+        if (!empty($max_id)) {
+            $params['max_id'] = $max_id;
+        }
+        if (!empty($cnt)) {
+            $params['count'] = $cnt;
+        }
+        if (!empty($page)) {
+            $params['page'] = $page;
         }
 
-        $response = $this->oAuthGet($url);
+        $response = $this->oAuthGet($url, $params);
         $statuses = json_decode($response);
         return $statuses;
     }
@@ -212,17 +238,25 @@ class TwitterOAuthClient extends OAuthClient
     {
         $url = "https://twitter.com/statuses/friends.json";
 
-        $params = array('id' => $id,
-                        'user_id' => $user_id,
-                        'screen_name' => $screen_name,
-                        'page' => $page);
-        $qry    = http_build_query($params);
+        $params = array();
+
+        if (!empty($id)) {
+            $params['id'] = $id;
+        }
+
+        if (!empty($user_id)) {
+            $params['user_id'] = $user_id;
+        }
+
+        if (!empty($screen_name)) {
+            $params['screen_name'] = $screen_name;
+        }
 
-        if (!empty($qry)) {
-            $url .= "?$qry";
+        if (!empty($page)) {
+            $params['page'] = $page;
         }
 
-        $response = $this->oAuthGet($url);
+        $response = $this->oAuthGet($url, $params);
         $friends  = json_decode($response);
         return $friends;
     }
@@ -242,19 +276,90 @@ class TwitterOAuthClient extends OAuthClient
     {
         $url = "https://twitter.com/friends/ids.json";
 
-        $params = array('id' => $id,
-                        'user_id' => $user_id,
-                        'screen_name' => $screen_name,
-                        'page' => $page);
-        $qry    = http_build_query($params);
+        $params = array();
+
+        if (!empty($id)) {
+            $params['id'] = $id;
+        }
+
+        if (!empty($user_id)) {
+            $params['user_id'] = $user_id;
+        }
+
+        if (!empty($screen_name)) {
+            $params['screen_name'] = $screen_name;
+        }
 
-        if (!empty($qry)) {
-            $url .= "?$qry";
+        if (!empty($page)) {
+            $params['page'] = $page;
         }
 
-        $response = $this->oAuthGet($url);
+        $response = $this->oAuthGet($url, $params);
         $ids      = json_decode($response);
         return $ids;
     }
 
+    /**
+     * Calls Twitter's /statuses/retweet/id.json API method
+     *
+     * @param int $id id of the notice to retweet
+     *
+     * @return retweeted status
+     */
+
+    function statusesRetweet($id)
+    {
+        $url = "http://api.twitter.com/1/statuses/retweet/$id.json";
+        $response = $this->oAuthPost($url);
+        $status = json_decode($response);
+        return $status;
+    }
+
+    /**
+     * Calls Twitter's /favorites/create API method
+     *
+     * @param int $id ID of the status to favorite
+     *
+     * @return object faved status
+     */
+
+    function favoritesCreate($id)
+    {
+        $url = "http://api.twitter.com/1/favorites/create/$id.json";
+        $response = $this->oAuthPost($url);
+        $status = json_decode($response);
+        return $status;
+    }
+
+    /**
+     * Calls Twitter's /favorites/destroy API method
+     *
+     * @param int $id ID of the status to unfavorite
+     *
+     * @return object unfaved status
+     */
+
+    function favoritesDestroy($id)
+    {
+        $url = "http://api.twitter.com/1/favorites/destroy/$id.json";
+        $response = $this->oAuthPost($url);
+        $status = json_decode($response);
+        return $status;
+    }
+
+    /**
+     * Calls Twitter's /statuses/destroy API method
+     *
+     * @param int $id ID of the status to destroy
+     *
+     * @return object destroyed
+     */
+
+    function statusesDestroy($id)
+    {
+        $url = "http://api.twitter.com/1/statuses/destroy/$id.json";
+        $response = $this->oAuthPost($url);
+        $status = json_decode($response);
+        return $status;
+    }
 }