PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/PHP/semantria/authrequest.php

http://semantria.codeplex.com
PHP | 222 lines | 180 code | 39 blank | 3 comment | 16 complexity | d2374d069d74cc33eb1756f68fa9d79e MD5 | raw file
  1. <?php
  2. namespace Semantria;
  3. class AuthRequest
  4. {
  5. protected $OAuthVersion = "1.0";
  6. protected $OAuthParameterPrefix = "oauth_";
  7. protected $OAuthConsumerKeyKey = "oauth_consumer_key";
  8. protected $OAuthVersionKey = "oauth_version";
  9. protected $OAuthSignatureMethodKey = "oauth_signature_method";
  10. protected $OAuthSignatureKey = "oauth_signature";
  11. protected $OAuthTimestampKey = "oauth_timestamp";
  12. protected $OAuthNonceKey = "oauth_nonce";
  13. // the consumer key and secret
  14. protected $_consumerKey;
  15. protected $_consumerSecret;
  16. protected $_applicationName;
  17. protected $_http_info;
  18. protected $use_compression;
  19. function __construct($consumerKey = NULL, $consumerSecret = NULL, $applicationName = NULL, $use_compression = FALSE)
  20. {
  21. if (empty($consumerKey))
  22. throw new \Exception('Parameter is null or empty "'.$consumerKey.'"');
  23. if (empty($consumerSecret))
  24. throw new \Exception('Parameter is null or empty "'.$consumerSecret.'"');
  25. $this->_consumerKey = $consumerKey;
  26. $this->_consumerSecret = $consumerSecret;
  27. $this->_applicationName = $applicationName;
  28. $this->use_compression = $use_compression;
  29. }
  30. function authWebRequest($method, $url, $body)
  31. {
  32. $nonce = uniqid('');
  33. $timestamp = time();
  34. $query = $this->generateQuery($method, $url, $timestamp, $nonce);
  35. $auth_header = $this->generateAuthHeader($query, $timestamp, $nonce);
  36. $headers = array();
  37. $headers[] = 'Authorization: '.$auth_header;
  38. if ($method == "POST") {
  39. $headers[] = 'Content-type: application/x-www-form-urlencoded';
  40. }
  41. $headers[] = 'x-api-version: 3.8';
  42. $headers[] = 'x-app-name: ' . $this->_applicationName;
  43. $response = $this->httpRequest($query, $method, $headers, $body);
  44. return $response;
  45. }
  46. protected function generateQuery($method, $url, $timestamp, $nonce)
  47. {
  48. $ps = @parse_url($url);
  49. $np = $this->getNormalizedParameters('&', $timestamp, $nonce);
  50. if (!empty($ps['query'])) {
  51. $ps['query'] = $ps['query'].'&'.$np;
  52. }
  53. else{
  54. $ps['query'] = $np;
  55. }
  56. $query = $ps['scheme'].'://'.$ps['host'].(isset($ps['port']) && $ps['port'] != '' ? (':'.$ps['port']) : '').$ps['path'].'?'.$ps['query'];
  57. return $query;
  58. }
  59. protected function generateAuthHeader($query, $timestamp, $nonce)
  60. {
  61. $md5cs = md5($this->_consumerSecret);
  62. $escquery = $this->urlencode($query);
  63. $hash = $this->getSHA1($md5cs, $escquery);
  64. $headers = array();
  65. $headers['OAuth realm'] = "";
  66. $headers[$this->OAuthVersionKey] = $this->OAuthVersion;
  67. $headers[$this->OAuthTimestampKey] = $timestamp;
  68. $headers[$this->OAuthNonceKey] = $nonce;
  69. $headers[$this->OAuthSignatureMethodKey] = "HMAC-SHA1";
  70. $headers[$this->OAuthConsumerKeyKey] = $this->_consumerKey;
  71. $headers[$this->OAuthSignatureKey] = $hash;
  72. ksort($headers);
  73. $h = array();
  74. foreach ($headers as $name => $value)
  75. {
  76. $h[] = $name.'="'.$value.'"';
  77. }
  78. $hs = implode(',', $h);
  79. return $hs;
  80. }
  81. protected function getNormalizedParameters($glue = "", $timestamp, $nonce)
  82. {
  83. $headers = array();
  84. $headers[$this->OAuthVersionKey] = $this->OAuthVersion;
  85. $headers[$this->OAuthTimestampKey] = $timestamp;
  86. $headers[$this->OAuthNonceKey] = $nonce;
  87. $headers[$this->OAuthSignatureMethodKey] = "HMAC-SHA1";
  88. $headers[$this->OAuthConsumerKeyKey] = $this->_consumerKey;
  89. ksort($headers);
  90. $h = array();
  91. foreach ($headers as $name => $value)
  92. {
  93. $h[] = $name.'='.$value;
  94. }
  95. $hs = implode($glue, $h);
  96. return $hs;
  97. }
  98. protected function urlencode($s)
  99. {
  100. if ($s === false)
  101. {
  102. return $s;
  103. }
  104. else
  105. {
  106. return str_replace('%7E', '~', rawurlencode($s));
  107. }
  108. }
  109. protected function getMD5Hash($str)
  110. {
  111. $md5 = md5($str);
  112. $bin = '';
  113. for ($i = 0; $i < strlen($md5); $i += 2)
  114. {
  115. $bin .= chr(hexdec($md5{$i+1}) + hexdec($md5{$i}) * 16);
  116. }
  117. $md5sig = $this->urlencode(base64_encode($bin));
  118. return $md5sig;
  119. }
  120. protected function getSHA1($key, $query)
  121. {
  122. //$signature = base64_encode(hash_hmac("sha1", $query, $key, true));
  123. $blocksize = 64;
  124. $hashfunc = 'sha1';
  125. if (strlen($key) > $blocksize)
  126. {
  127. $key = pack('H*', $hashfunc($key));
  128. }
  129. $key = str_pad($key,$blocksize,chr(0x00));
  130. $ipad = str_repeat(chr(0x36),$blocksize);
  131. $opad = str_repeat(chr(0x5c),$blocksize);
  132. $hmac = pack(
  133. 'H*',$hashfunc(
  134. ($key^$opad).pack(
  135. 'H*',$hashfunc(
  136. ($key^$ipad).$query
  137. )
  138. )
  139. )
  140. );
  141. $signature = base64_encode($hmac);
  142. $sha1sig = $this->urlencode($signature);
  143. return $sha1sig;
  144. }
  145. protected function httpRequest($url, $method, $headers, $postfields=NULL) {
  146. $http_info = array();
  147. $ci = curl_init();
  148. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  149. curl_setopt($ci, CURLOPT_VERBOSE, FALSE);
  150. curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  151. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
  152. curl_setopt($ci, CURLOPT_TIMEOUT, 30);
  153. if ($this->use_compression) {
  154. curl_setopt($ci, CURLOPT_ENCODING, "gzip,deflate");
  155. }
  156. switch ($method) {
  157. case 'POST':
  158. curl_setopt($ci, CURLOPT_POST, TRUE);
  159. if (!empty($postfields)) {
  160. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  161. }
  162. break;
  163. case 'DELETE':
  164. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  165. if (!empty($postfields)) {
  166. //$url = "{$url}?{$postfields}";
  167. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  168. }
  169. }
  170. curl_setopt($ci, CURLOPT_URL, $url);
  171. $response = curl_exec($ci);
  172. $this->_http_info = array_merge($http_info, curl_getinfo($ci));
  173. $code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  174. $message = $response;
  175. if ($code == 0)
  176. {
  177. $message = curl_error($ci);
  178. }
  179. curl_close ($ci);
  180. $result = array("status"=>$code, "message"=>$message);
  181. return $result;
  182. }
  183. }