3 * StatusNet, the distributed open-source microblogging tool
5 * Base class for doing OAuth calls as a consumer
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.
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.
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/>.
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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 require_once 'OAuth.php';
37 * Exception wrapper for cURL errors
39 * @category Integration
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/
46 class OAuthClientCurlException extends Exception
51 * Base class for doing OAuth calls as a consumer
53 * @category Integration
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/
68 * Can be initialized with just consumer key and secret for requesting new
69 * tokens or with additional request token or access token
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
78 function __construct($consumer_key, $consumer_secret,
79 $oauth_token = null, $oauth_token_secret = null)
81 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
82 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
85 if (isset($oauth_token) && isset($oauth_token_secret)) {
86 $this->token = new OAuthToken($oauth_token, $oauth_token_secret);
91 * Gets a request token from the given url
93 * @param string $url OAuth endpoint for grabbing request tokens
95 * @return OAuthToken $token the request token
97 function getRequestToken($url)
99 $response = $this->oAuthGet($url);
100 parse_str($response);
101 $token = new OAuthToken($oauth_token, $oauth_token_secret);
106 * Builds a link that can be redirected to in order to
107 * authorize a request token.
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
113 * @return string $authorize_url the url to redirect to
115 function getAuthorizeLink($url, $request_token, $oauth_callback = null)
117 $authorize_url = $url . '?oauth_token=' .
120 if (isset($oauth_callback)) {
121 $authorize_url .= '&oauth_callback=' . urlencode($oauth_callback);
124 return $authorize_url;
128 * Fetches an access token
130 * @param string $url OAuth endpoint for exchanging authorized request tokens
133 * @return OAuthToken $token the access token
135 function getAccessToken($url)
137 $response = $this->oAuthPost($url);
138 parse_str($response);
139 $token = new OAuthToken($oauth_token, $oauth_token_secret);
144 * Use HTTP GET to make a signed OAuth request
146 * @param string $url OAuth endpoint
148 * @return mixed the request
150 function oAuthGet($url)
152 $request = OAuthRequest::from_consumer_and_token($this->consumer,
153 $this->token, 'GET', $url, null);
154 $request->sign_request($this->sha1_method,
155 $this->consumer, $this->token);
157 return $this->httpRequest($request->to_url());
161 * Use HTTP POST to make a signed OAuth request
163 * @param string $url OAuth endpoint
164 * @param array $params additional post parameters
166 * @return mixed the request
168 function oAuthPost($url, $params = null)
170 $request = OAuthRequest::from_consumer_and_token($this->consumer,
171 $this->token, 'POST', $url, $params);
172 $request->sign_request($this->sha1_method,
173 $this->consumer, $this->token);
175 return $this->httpRequest($request->get_normalized_http_url(),
176 $request->to_postdata());
180 * Make a HTTP request using cURL.
182 * @param string $url Where to make the
183 * @param array $params post parameters
185 * @return mixed the request
187 function httpRequest($url, $params = null)
190 CURLOPT_RETURNTRANSFER => true,
191 CURLOPT_FAILONERROR => true,
192 CURLOPT_HEADER => false,
193 CURLOPT_FOLLOWLOCATION => true,
194 CURLOPT_USERAGENT => 'StatusNet',
195 CURLOPT_CONNECTTIMEOUT => 120,
196 CURLOPT_TIMEOUT => 120,
197 CURLOPT_HTTPAUTH => CURLAUTH_ANY,
198 CURLOPT_SSL_VERIFYPEER => false,
200 // Twitter is strict about accepting invalid "Expect" headers
202 CURLOPT_HTTPHEADER => array('Expect:')
205 if (isset($params)) {
206 $options[CURLOPT_POST] = true;
207 $options[CURLOPT_POSTFIELDS] = $params;
210 $ch = curl_init($url);
211 curl_setopt_array($ch, $options);
212 $response = curl_exec($ch);
214 if ($response === false) {
215 $msg = curl_error($ch);
216 $code = curl_errno($ch);
217 throw new OAuthClientCurlException($msg, $code);