]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/oauthclient.php
Introduced common_location_shared() to check if location sharing is always,
[quix0rs-gnu-social.git] / lib / oauthclient.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Base class for doing OAuth calls as a consumer
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Action
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once 'OAuth.php';
35
36 /**
37  * Exception wrapper for cURL errors
38  *
39  * @category Integration
40  * @package  StatusNet
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  *
45  */
46 class OAuthClientException extends Exception
47 {
48 }
49
50 /**
51  * Base class for doing OAuth calls as a consumer
52  *
53  * @category Integration
54  * @package  StatusNet
55  * @author   Zach Copley <zach@status.net>
56  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
57  * @link     http://status.net/
58  *
59  */
60 class OAuthClient
61 {
62     var $consumer;
63     var $token;
64
65     /**
66      * Constructor
67      *
68      * Can be initialized with just consumer key and secret for requesting new
69      * tokens or with additional request token or access token
70      *
71      * @param string $consumer_key       consumer key
72      * @param string $consumer_secret    consumer secret
73      * @param string $oauth_token        user's token
74      * @param string $oauth_token_secret user's secret
75      *
76      * @return nothing
77      */
78     function __construct($consumer_key, $consumer_secret,
79                          $oauth_token = null, $oauth_token_secret = null)
80     {
81         $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
82         $this->consumer    = new OAuthConsumer($consumer_key, $consumer_secret);
83         $this->token       = null;
84
85         if (isset($oauth_token) && isset($oauth_token_secret)) {
86             $this->token = new OAuthToken($oauth_token, $oauth_token_secret);
87         }
88     }
89
90     /**
91      * Gets a request token from the given url
92      *
93      * @param string $url      OAuth endpoint for grabbing request tokens
94      * @param string $callback authorized request token callback
95      *
96      * @return OAuthToken $token the request token
97      */
98     function getRequestToken($url, $callback = null)
99     {
100         $params = null;
101
102         if (!is_null($callback)) {
103             $params['oauth_callback'] = $callback;
104         }
105
106         $response = $this->oAuthGet($url, $params);
107
108         $arr = array();
109         parse_str($response, $arr);
110
111         $token   = $arr['oauth_token'];
112         $secret  = $arr['oauth_token_secret'];
113         $confirm = $arr['oauth_callback_confirmed'];
114
115         if (isset($token) && isset($secret)) {
116
117             $token = new OAuthToken($token, $secret);
118
119             if (isset($confirm)) {
120                 if ($confirm == 'true') {
121                     return $token;
122                 } else {
123                     throw new OAuthClientException(
124                         'Callback was not confirmed by remote OAuth side.'
125                     );
126                 }
127             }
128             return $token;
129         } else {
130             throw new OAuthClientException(
131                 'Could not get a request token from remote OAuth side.'
132             );
133         }
134     }
135
136     /**
137      * Builds a link that can be redirected to in order to
138      * authorize a request token.
139      *
140      * @param string     $url            endpoint for authorizing request tokens
141      * @param OAuthToken $request_token  the request token to be authorized
142      *
143      * @return string $authorize_url the url to redirect to
144      */
145     function getAuthorizeLink($url, $request_token)
146     {
147         $authorize_url = $url . '?oauth_token=' .
148             $request_token->key;
149
150         return $authorize_url;
151     }
152
153     /**
154      * Fetches an access token
155      *
156      * @param string $url      OAuth endpoint for exchanging authorized request tokens
157      *                         for access tokens
158      * @param string $verifier 1.0a verifier
159      *
160      * @return OAuthToken $token the access token
161      */
162     function getAccessToken($url, $verifier = null)
163     {
164         $params = array();
165
166         if (!is_null($verifier)) {
167             $params['oauth_verifier'] = $verifier;
168         }
169
170         $response = $this->oAuthPost($url, $params);
171
172         $arr = array();
173         parse_str($response, $arr);
174
175         $token  = $arr['oauth_token'];
176         $secret = $arr['oauth_token_secret'];
177
178         if (isset($token) && isset($secret)) {
179             $token = new OAuthToken($token, $secret);
180             return $token;
181         } else {
182             throw new OAuthClientException(
183                 'Could not get a access token from remote OAuth side.'
184             );
185         }
186     }
187
188     /**
189      * Use HTTP GET to make a signed OAuth requesta
190      *
191      * @param string $url    OAuth request token endpoint
192      * @param array  $params additional parameters
193      *
194      * @return mixed the request
195      */
196     function oAuthGet($url, $params = null)
197     {
198         $request = OAuthRequest::from_consumer_and_token($this->consumer,
199             $this->token, 'GET', $url, $params);
200         $request->sign_request($this->sha1_method,
201             $this->consumer, $this->token);
202
203         return $this->httpRequest($request->to_url());
204     }
205
206     /**
207      * Use HTTP POST to make a signed OAuth request
208      *
209      * @param string $url    OAuth endpoint
210      * @param array  $params additional post parameters
211      *
212      * @return mixed the request
213      */
214     function oAuthPost($url, $params = null)
215     {
216         $request = OAuthRequest::from_consumer_and_token($this->consumer,
217             $this->token, 'POST', $url, $params);
218         $request->sign_request($this->sha1_method,
219             $this->consumer, $this->token);
220
221         return $this->httpRequest($request->get_normalized_http_url(),
222             $request->to_postdata());
223     }
224
225     /**
226      * Make a HTTP request.
227      *
228      * @param string $url    Where to make the
229      * @param array  $params post parameters
230      *
231      * @return mixed the request
232      */
233     function httpRequest($url, $params = null)
234     {
235         $request = new HTTPClient($url);
236         $request->setConfig(array(
237             'connect_timeout' => 120,
238             'timeout' => 120,
239             'follow_redirects' => true,
240             'ssl_verify_peer' => false,
241             'ssl_verify_host' => false
242         ));
243
244         // Twitter was strict about accepting invalid "Expect" headers
245         // between 2008ish and October 2012. Caused "417 Expectation failed"
246         //$request->setHeader('Expect', '');
247
248         if (isset($params)) {
249             $request->setMethod(HTTP_Request2::METHOD_POST);
250             $request->setBody($params);
251         }
252
253         try {
254             $response = $request->send();
255             $code = $response->getStatus();
256             if ($code < 200 || $code >= 400) {
257                 throw new OAuthClientException($response->getBody(), $code);
258             }
259             return $response->getBody();
260         } catch (Exception $e) {
261             throw new OAuthClientException($e->getMessage(), $e->getCode());
262         }
263     }
264
265 }