fe86dd8cf69838ccbde882ddc5ae863cc1534b78
[WP-Plugins/glm-member-db.git] /
1 <?php
2 namespace PayPal\Auth\Oauth;
3 class OAuthRequest {
4         public $parameters;
5         protected $http_method;
6         protected $http_url;
7         // for debug purposes
8         public $base_string;
9         public static $version = '1.0';
10         public static $POST_INPUT = 'php://input';
11
12         function __construct($http_method, $http_url, $parameters=NULL) {
13                 $parameters = ($parameters) ? $parameters : array();
14                 $parameters = array_merge( OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters);
15                 $this->parameters = $parameters;
16                 $this->http_method = $http_method;
17                 $this->http_url = $http_url;
18         }
19
20
21         /**
22          * attempt to build up a request from what was passed to the server
23          */
24         public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) {
25                 $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on")
26                 ? 'http'
27                 : 'https';
28                 $http_url = ($http_url) ? $http_url : $scheme .
29                               '://' . $_SERVER['HTTP_HOST'] .
30                               ':' .
31                 $_SERVER['SERVER_PORT'] .
32                 $_SERVER['REQUEST_URI'];
33                 $http_method = ($http_method) ? $http_method : $_SERVER['REQUEST_METHOD'];
34
35                 // We weren't handed any parameters, so let's find the ones relevant to
36                 // this request.
37                 // If you run XML-RPC or similar you should use this to provide your own
38                 // parsed parameter-list
39                 if (!$parameters) {
40                         // Find request headers
41                         $request_headers = OAuthUtil::get_headers();
42
43                         // Parse the query-string to find GET parameters
44                         $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
45
46                         // It's a POST request of the proper content-type, so parse POST
47                         // parameters and add those overriding any duplicates from GET
48                         if ($http_method == "POST"
49                         &&  isset($request_headers['Content-Type'])
50                         && strstr($request_headers['Content-Type'],
51                      'application/x-www-form-urlencoded')
52                         ) {
53                                 $post_data = OAuthUtil::parse_parameters(
54                                 file_get_contents(self::$POST_INPUT)
55                                 );
56                                 $parameters = array_merge($parameters, $post_data);
57                         }
58
59                         // We have a Authorization-header with OAuth data. Parse the header
60                         // and add those overriding any duplicates from GET or POST
61                         if (isset($request_headers['Authorization']) && substr($request_headers['Authorization'], 0, 6) == 'OAuth ') {
62                                 $header_parameters = OAuthUtil::split_header(
63                                 $request_headers['Authorization']
64                                 );
65                                 $parameters = array_merge($parameters, $header_parameters);
66                         }
67
68                 }
69
70                 return new OAuthRequest($http_method, $http_url, $parameters);
71         }
72
73         /**
74          * pretty much a helper function to set up the request
75          */
76         public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) {
77                 $parameters = ($parameters) ?  $parameters : array();
78                 $defaults = array("oauth_version" => OAuthRequest::$version,
79                 // "oauth_nonce" => OAuthRequest::generate_nonce(),
80                 "oauth_timestamp" => OAuthRequest::generate_timestamp(),
81
82                       "oauth_consumer_key" => $consumer->key);
83                 if ($token)
84                 $defaults['oauth_token'] = $token->key;
85
86                 $parameters = array_merge($defaults, $parameters);
87                 ksort($parameters);
88                 return new OAuthRequest($http_method, $http_url, $parameters);
89         }
90
91         public function set_parameter($name, $value, $allow_duplicates = true) {
92                 if ($allow_duplicates && isset($this->parameters[$name])) {
93                         // We have already added parameter(s) with this name, so add to the list
94                         if (is_scalar($this->parameters[$name])) {
95                                 // This is the first duplicate, so transform scalar (string)
96                                 // into an array so we can add the duplicates
97                                 $this->parameters[$name] = array($this->parameters[$name]);
98                         }
99
100                         $this->parameters[$name][] = $value;
101                 } else {
102                         $this->parameters[$name] = $value;
103                 }
104         }
105
106         public function get_parameter($name) {
107                 return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
108         }
109
110         public function get_parameters() {
111                 return $this->parameters;
112         }
113
114         public function unset_parameter($name) {
115                 unset($this->parameters[$name]);
116         }
117
118         /**
119          * The request parameters, sorted and concatenated into a normalized string.
120          * @return string
121          */
122         public function get_signable_parameters() {
123                 // Grab all parameters
124                 $params = $this->parameters;
125                 ksort($params);
126                 // Remove oauth_signature if present
127                 // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
128                 if (isset($params['oauth_signature'])) {
129                         unset($params['oauth_signature']);
130                 }
131                 foreach($params as $key => $value)
132                 {
133                         $res[]=$key."=".$value;
134                 }
135
136                 return implode('&', $res);
137                 //return OAuthUtil::build_http_query($params);
138         }
139
140         /**
141          * Returns the base string of this request
142          *
143          * The base string defined as the method, the url
144          * and the parameters (normalized), each urlencoded
145          * and the concated with &.
146          */
147         public function get_signature_base_string() {
148                 $parts = array(
149                 $this->get_normalized_http_method(),
150                 $this->get_normalized_http_url(),
151                 $this->get_signable_parameters()
152                 );
153
154                 $parts = OAuthUtil::urlencode_rfc3986($parts);
155
156                 return implode('&', $parts);
157         }
158
159         /**
160          * just uppercases the http method
161          */
162         public function get_normalized_http_method() {
163                 return strtoupper($this->http_method);
164         }
165
166         /**
167          * parses the url and rebuilds it to be
168          * scheme://host/path
169          */
170         public function get_normalized_http_url() {
171                 $parts = parse_url($this->http_url);
172
173                 $scheme = (isset($parts['scheme'])) ? $parts['scheme'] : 'http';
174                 $port = (isset($parts['port'])) ? $parts['port'] : (($scheme == 'https') ? '443' : '80');
175                 $host = (isset($parts['host'])) ? $parts['host'] : '';
176                 $path = (isset($parts['path'])) ? $parts['path'] : '';
177
178                 if (($scheme == 'https' && $port != '443')
179                 || ($scheme == 'http' && $port != '80')) {
180                         $host = "$host:$port";
181                 }
182                 return "$scheme://$host$path";
183         }
184
185         /**
186          * builds a url usable for a GET request
187          */
188         public function to_url() {
189                 $post_data = $this->to_postdata();
190                 $out = $this->get_normalized_http_url();
191                 if ($post_data) {
192                         $out .= '?'.$post_data;
193                 }
194                 return $out;
195         }
196
197         /**
198          * builds the data one would send in a POST request
199          */
200         public function to_postdata() {
201                 return OAuthUtil::build_http_query($this->parameters);
202         }
203
204         /**
205          * builds the Authorization: header
206          */
207         public function to_header($realm=null) {
208                 $first = true;
209                 if($realm) {
210                         $out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"';
211                         $first = false;
212                 } else
213                 $out = 'Authorization: OAuth';
214
215                 $total = array();
216                 foreach ($this->parameters as $k => $v) {
217                         if (substr($k, 0, 5) != "oauth") continue;
218                         if (is_array($v)) {
219                                 throw new OAuthException('Arrays not supported in headers');
220                         }
221                         $out .= ($first) ? ' ' : ',';
222                         $out .= OAuthUtil::urlencode_rfc3986($k) .
223               '="' .
224                         OAuthUtil::urlencode_rfc3986($v) .
225               '"';
226                         $first = false;
227                 }
228                 return $out;
229         }
230
231         public function __toString() {
232                 return $this->to_url();
233         }
234
235
236         public function sign_request($signature_method,$consumer, $token) {
237                         
238                 $empty=false;
239                 $msg=array();
240                 if( $token->key==null){
241                         $msg[] = 'Token key';
242                 }
243                 if($token->secret==null){
244                         $msg[] = 'Token secret';
245                 }
246                 if($consumer->key == null){
247
248                         $msg[] = 'Consumer key';
249                 }
250                 if($consumer->secret == null){
251
252                         $msg[] = 'Consumer secret';
253                 }
254                 if($this->http_url == null){
255
256                         $msg[] ='Endpoint';
257                 }
258                 if($this->http_method == null){
259
260                         $msg[] ='HTTP method';
261                 }
262                 if(count($msg))
263                 {
264                         throw new OAuthException('Enter valid '. implode(',',$msg));
265                 }
266                 $this->set_parameter(
267       "oauth_signature_method",
268                 $signature_method->get_name(),
269                 false );
270
271                 $signature = $this->build_signature($signature_method, $consumer, $token);
272                 $this->set_parameter("oauth_signature", $signature, false);
273
274         }
275
276         public function build_signature($signature_method, $consumer, $token) {
277                 $signature = $signature_method->build_signature($this, $consumer, $token);
278                 return $signature;
279         }
280
281         /**
282          * util function: current timestamp
283          */
284         private static function generate_timestamp() {
285                 return time();
286         }
287
288         /**
289          * util function: current nonce
290          */
291         private static function generate_nonce() {
292                 $mt = microtime();
293                 $rand = mt_rand();
294
295                 return md5($mt . $rand); // md5s look nicer than numbers
296         }
297 }