]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/oauthclient.php
Moved some stuff around. More comments and phpcs compliance.
[quix0rs-gnu-social.git] / lib / oauthclient.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
24  * @author    Zach Copley <zach@controlyourself.ca>
25  * @copyright 2008 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!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  Laconica
41  * @author   Zach Copley <zach@controlyourself.ca>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://laconi.ca/
44  *
45  */
46 class OAuthClientCurlException extends Exception
47 {
48 }
49
50 /**
51  * Base class for doing OAuth calls as a consumer
52  *
53  * @category Integration
54  * @package  Laconica
55  * @author   Zach Copley <zach@controlyourself.ca>
56  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
57  * @link     http://laconi.ca/
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      *
95      * @return OAuthToken $token the request token
96      */
97     function getRequestToken($url)
98     {
99         $response = $this->oAuthGet($url);
100         parse_str($response);
101         $token = new OAuthToken($oauth_token, $oauth_token_secret);
102         return $token;
103     }
104
105     /**
106      * Builds a link that can be redirected to in order to
107      * authorize a request token.
108      *
109      * @param string     $url            endpoint for authorizing request tokens
110      * @param OAuthToken $request_token  the request token to be authorized
111      * @param string     $oauth_callback optional callback url
112      *
113      * @return string $authorize_url the url to redirect to
114      */
115     function getAuthorizeLink($url, $request_token, $oauth_callback = null)
116     {
117         $authorize_url = $url . '?oauth_token=' .
118             $request_token->key;
119
120         if (isset($oauth_callback)) {
121             $authorize_url .= '&oauth_callback=' . urlencode($oauth_callback);
122         }
123
124         common_debug("$authorize_url");
125         
126         return $authorize_url;
127     }
128
129     /**
130      * Fetches an access token
131      *
132      * @param string $url OAuth endpoint for exchanging authorized request tokens
133      *                     for access tokens
134      *
135      * @return OAuthToken $token the access token
136      */
137     function getAccessToken($url)
138     {
139         $response = $this->oAuthPost($url);
140         parse_str($response);
141         $token = new OAuthToken($oauth_token, $oauth_token_secret);
142         return $token;
143     }
144
145     /**
146      * Use HTTP GET to make a signed OAuth request
147      *
148      * @param string $url OAuth endpoint
149      *
150      * @return mixed the request
151      */
152     function oAuthGet($url)
153     {
154         $request = OAuthRequest::from_consumer_and_token($this->consumer,
155             $this->token, 'GET', $url, null);
156         $request->sign_request($this->sha1_method,
157             $this->consumer, $this->token);
158
159         return $this->httpRequest($request->to_url());
160     }
161
162     /**
163      * Use HTTP POST to make a signed OAuth request
164      *
165      * @param string $url    OAuth endpoint
166      * @param array  $params additional post parameters
167      *
168      * @return mixed the request
169      */
170     function oAuthPost($url, $params = null)
171     {
172         $request = OAuthRequest::from_consumer_and_token($this->consumer,
173             $this->token, 'POST', $url, $params);
174         $request->sign_request($this->sha1_method,
175             $this->consumer, $this->token);
176
177         return $this->httpRequest($request->get_normalized_http_url(),
178             $request->to_postdata());
179     }
180
181     /**
182      * Make a HTTP request using cURL.
183      *
184      * @param string $url    Where to make the
185      * @param array  $params post parameters
186      *
187      * @return mixed the request
188      */
189     function httpRequest($url, $params = null)
190     {
191         $options = array(
192             CURLOPT_RETURNTRANSFER => true,
193             CURLOPT_FAILONERROR    => true,
194             CURLOPT_HEADER         => false,
195             CURLOPT_FOLLOWLOCATION => true,
196             CURLOPT_USERAGENT      => 'Laconica',
197             CURLOPT_CONNECTTIMEOUT => 120,
198             CURLOPT_TIMEOUT        => 120,
199             CURLOPT_HTTPAUTH       => CURLAUTH_ANY,
200             CURLOPT_SSL_VERIFYPEER => false,
201
202             // Twitter is strict about accepting invalid "Expect" headers
203
204             CURLOPT_HTTPHEADER => array('Expect:')
205         );
206
207         if (isset($params)) {
208             $options[CURLOPT_POST]       = true;
209             $options[CURLOPT_POSTFIELDS] = $params;
210         }
211
212         $ch = curl_init($url);
213         curl_setopt_array($ch, $options);
214         $response = curl_exec($ch);
215
216         if ($response === false) {
217             $msg  = curl_error($ch);
218             $code = curl_errno($ch);
219             throw new OAuthClientCurlException($msg, $code);
220         }
221
222         curl_close($ch);
223
224         return $response;
225     }
226
227 }