]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/oauthclient.php
Merge branch 'testing'
[quix0rs-gnu-social.git] / lib / oauthclient.php
index b66a24be44d53c491e4ccda3c49208ca2719e365..b22fd789740c95b93fc0c5623202ceb920447b3e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Laconica, the distributed open-source microblogging tool
+ * StatusNet, the distributed open-source microblogging tool
  *
  * Base class for doing OAuth calls as a consumer
  *
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  * @category  Action
- * @package   Laconica
- * @author    Zach Copley <zach@controlyourself.ca>
- * @copyright 2008 Control Yourself, Inc.
+ * @package   StatusNet
+ * @author    Zach Copley <zach@status.net>
+ * @copyright 2009 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link      http://laconi.ca/
+ * @link      http://status.net/
  */
 
-if (!defined('LACONICA')) {
+if (!defined('STATUSNET') && !defined('LACONICA')) {
     exit(1);
 }
 
@@ -37,13 +37,13 @@ require_once 'OAuth.php';
  * Exception wrapper for cURL errors
  *
  * @category Integration
- * @package  Laconica
- * @author   Zach Copley <zach@controlyourself.ca>
+ * @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://laconi.ca/
+ * @link     http://status.net/
  *
  */
-class OAuthClientCurlException extends Exception
+class OAuthClientException extends Exception
 {
 }
 
@@ -51,10 +51,10 @@ class OAuthClientCurlException extends Exception
  * Base class for doing OAuth calls as a consumer
  *
  * @category Integration
- * @package  Laconica
- * @author   Zach Copley <zach@controlyourself.ca>
+ * @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://laconi.ca/
+ * @link     http://status.net/
  *
  */
 class OAuthClient
@@ -97,9 +97,14 @@ class OAuthClient
     function getRequestToken($url)
     {
         $response = $this->oAuthGet($url);
-        parse_str($response);
-        $token = new OAuthToken($oauth_token, $oauth_token_secret);
-        return $token;
+        $arr = array();
+        parse_str($response, $arr);
+        if (isset($arr['oauth_token']) && isset($arr['oauth_token_secret'])) {
+            $token = new OAuthToken($arr['oauth_token'], @$arr['oauth_token_secret']);
+            return $token;
+        } else {
+            throw new OAuthClientException();
+        }
     }
 
     /**
@@ -177,7 +182,7 @@ class OAuthClient
     }
 
     /**
-     * Make a HTTP request using cURL.
+     * Make a HTTP request.
      *
      * @param string $url    Where to make the
      * @param array  $params post parameters
@@ -186,40 +191,33 @@ class OAuthClient
      */
     function httpRequest($url, $params = null)
     {
-        $options = array(
-            CURLOPT_RETURNTRANSFER => true,
-            CURLOPT_FAILONERROR    => true,
-            CURLOPT_HEADER         => false,
-            CURLOPT_FOLLOWLOCATION => true,
-            CURLOPT_USERAGENT      => 'Laconica',
-            CURLOPT_CONNECTTIMEOUT => 120,
-            CURLOPT_TIMEOUT        => 120,
-            CURLOPT_HTTPAUTH       => CURLAUTH_ANY,
-            CURLOPT_SSL_VERIFYPEER => false,
-
-            // Twitter is strict about accepting invalid "Expect" headers
-
-            CURLOPT_HTTPHEADER => array('Expect:')
-        );
+        $request = new HTTPClient($url);
+        $request->setConfig(array(
+            'connect_timeout' => 120,
+            'timeout' => 120,
+            'follow_redirects' => true,
+            'ssl_verify_peer' => false,
+            'ssl_verify_host' => false
+        ));
+
+        // Twitter is strict about accepting invalid "Expect" headers
+        $request->setHeader('Expect', '');
 
         if (isset($params)) {
-            $options[CURLOPT_POST]       = true;
-            $options[CURLOPT_POSTFIELDS] = $params;
+            $request->setMethod(HTTP_Request2::METHOD_POST);
+            $request->setBody($params);
         }
 
-        $ch = curl_init($url);
-        curl_setopt_array($ch, $options);
-        $response = curl_exec($ch);
-
-        if ($response === false) {
-            $msg  = curl_error($ch);
-            $code = curl_errno($ch);
-            throw new OAuthClientCurlException($msg, $code);
+        try {
+            $response = $request->send();
+            $code = $response->getStatus();
+            if ($code < 200 || $code >= 400) {
+                throw new OAuthClientException($response->getBody(), $code);
+            }
+            return $response->getBody();
+        } catch (Exception $e) {
+            throw new OAuthClientException($e->getMessage(), $e->getCode());
         }
-
-        curl_close($ch);
-
-        return $response;
     }
 
 }