2 namespace PayPal\Auth\Oauth;
5 protected $http_method;
9 public static $version = '1.0';
10 public static $POST_INPUT = 'php://input';
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;
22 * attempt to build up a request from what was passed to the server
24 public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) {
25 $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on")
28 $http_url = ($http_url) ? $http_url : $scheme .
29 '://' . $_SERVER['HTTP_HOST'] .
31 $_SERVER['SERVER_PORT'] .
32 $_SERVER['REQUEST_URI'];
33 $http_method = ($http_method) ? $http_method : $_SERVER['REQUEST_METHOD'];
35 // We weren't handed any parameters, so let's find the ones relevant to
37 // If you run XML-RPC or similar you should use this to provide your own
38 // parsed parameter-list
40 // Find request headers
41 $request_headers = OAuthUtil::get_headers();
43 // Parse the query-string to find GET parameters
44 $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
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')
53 $post_data = OAuthUtil::parse_parameters(
54 file_get_contents(self::$POST_INPUT)
56 $parameters = array_merge($parameters, $post_data);
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']
65 $parameters = array_merge($parameters, $header_parameters);
70 return new OAuthRequest($http_method, $http_url, $parameters);
74 * pretty much a helper function to set up the request
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(),
82 "oauth_consumer_key" => $consumer->key);
84 $defaults['oauth_token'] = $token->key;
86 $parameters = array_merge($defaults, $parameters);
88 return new OAuthRequest($http_method, $http_url, $parameters);
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]);
100 $this->parameters[$name][] = $value;
102 $this->parameters[$name] = $value;
106 public function get_parameter($name) {
107 return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
110 public function get_parameters() {
111 return $this->parameters;
114 public function unset_parameter($name) {
115 unset($this->parameters[$name]);
119 * The request parameters, sorted and concatenated into a normalized string.
122 public function get_signable_parameters() {
123 // Grab all parameters
124 $params = $this->parameters;
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']);
131 foreach($params as $key => $value)
133 $res[]=$key."=".$value;
136 return implode('&', $res);
137 //return OAuthUtil::build_http_query($params);
141 * Returns the base string of this request
143 * The base string defined as the method, the url
144 * and the parameters (normalized), each urlencoded
145 * and the concated with &.
147 public function get_signature_base_string() {
149 $this->get_normalized_http_method(),
150 $this->get_normalized_http_url(),
151 $this->get_signable_parameters()
154 $parts = OAuthUtil::urlencode_rfc3986($parts);
156 return implode('&', $parts);
160 * just uppercases the http method
162 public function get_normalized_http_method() {
163 return strtoupper($this->http_method);
167 * parses the url and rebuilds it to be
170 public function get_normalized_http_url() {
171 $parts = parse_url($this->http_url);
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'] : '';
178 if (($scheme == 'https' && $port != '443')
179 || ($scheme == 'http' && $port != '80')) {
180 $host = "$host:$port";
182 return "$scheme://$host$path";
186 * builds a url usable for a GET request
188 public function to_url() {
189 $post_data = $this->to_postdata();
190 $out = $this->get_normalized_http_url();
192 $out .= '?'.$post_data;
198 * builds the data one would send in a POST request
200 public function to_postdata() {
201 return OAuthUtil::build_http_query($this->parameters);
205 * builds the Authorization: header
207 public function to_header($realm=null) {
210 $out = 'Authorization: OAuth realm="' . OAuthUtil::urlencode_rfc3986($realm) . '"';
213 $out = 'Authorization: OAuth';
216 foreach ($this->parameters as $k => $v) {
217 if (substr($k, 0, 5) != "oauth") continue;
219 throw new OAuthException('Arrays not supported in headers');
221 $out .= ($first) ? ' ' : ',';
222 $out .= OAuthUtil::urlencode_rfc3986($k) .
224 OAuthUtil::urlencode_rfc3986($v) .
231 public function __toString() {
232 return $this->to_url();
236 public function sign_request($signature_method,$consumer, $token) {
240 if( $token->key==null){
241 $msg[] = 'Token key';
243 if($token->secret==null){
244 $msg[] = 'Token secret';
246 if($consumer->key == null){
248 $msg[] = 'Consumer key';
250 if($consumer->secret == null){
252 $msg[] = 'Consumer secret';
254 if($this->http_url == null){
258 if($this->http_method == null){
260 $msg[] ='HTTP method';
264 throw new OAuthException('Enter valid '. implode(',',$msg));
266 $this->set_parameter(
267 "oauth_signature_method",
268 $signature_method->get_name(),
271 $signature = $this->build_signature($signature_method, $consumer, $token);
272 $this->set_parameter("oauth_signature", $signature, false);
276 public function build_signature($signature_method, $consumer, $token) {
277 $signature = $signature_method->build_signature($this, $consumer, $token);
282 * util function: current timestamp
284 private static function generate_timestamp() {
289 * util function: current nonce
291 private static function generate_nonce() {
295 return md5($mt . $rand); // md5s look nicer than numbers