nsx_trOAuthRequest

The NextScripts: Social Networks Auto-Poster nsx trOAuthRequest class.

Defined (1)

The class is defined in the following location(s).

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