PageRenderTime 1710ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/shop quần áo starloveshop.com/wp-content/plugins/social-networks-auto-poster-facebook-twitter-g/inc-cl/apis/plurkOAuth.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 204 lines | 179 code | 21 blank | 4 comment | 31 complexity | ceab09df3966546c39437ec051bb114b MD5 | raw file
  1. <?php
  2. define('PLURK_ACCESS_TOKEN_PATH', "/OAuth/access_token");
  3. define('PLURK_AUTHORIZE_PATH', "/OAuth/authorize");
  4. define('PLURK_REQUEST_TOKEN_PATH', "/OAuth/request_token");
  5. abstract class nxspk_SignatureMethod
  6. {
  7. abstract public function signing_base($request, $consumer, $token);
  8. abstract public function sign($request, $consumer, $token);
  9. public function check($request, $consumer, $token, $signature) {
  10. $built = $this->sign($request, $consumer, $token);
  11. return $built == $signature;
  12. }
  13. }
  14. class nxspk_SigMethod_HMAC_SHA1 extends nxspk_SignatureMethod {
  15. public $name = 'HMAC-SHA1';
  16. public function signing_base($request, $consumer_secret, $token) {
  17. $sig = array(rawurlencode($request['method']), rawurlencode($request['normalized_url']), rawurlencode($request['normalized_parameters']));
  18. $key = sprintf("%s&", rawurlencode($consumer_secret));
  19. if (isset($token)) $key .= rawurlencode($token->secret);
  20. $raw = implode('&', $sig);
  21. return array ($key, $raw);
  22. }
  23. public function sign($request, $consumer_secret, $token) {
  24. $key_raw = $this->signing_base($request, $consumer_secret, $token);// prr($key_raw);
  25. $basestring = base64_encode (hash_hmac('sha1', $key_raw[1], $key_raw[0], true)); //echo $basestring;
  26. return rawurlencode($basestring);
  27. }
  28. public static function urlencode_rfc3986($input) {
  29. if (is_array($input)) {
  30. return array_map(array('nxspk_SigMethod_HMAC_SHA1', 'urlencode_rfc3986'), $input);
  31. } else if (is_scalar($input)) {
  32. return str_replace(
  33. '+',
  34. ' ',
  35. str_replace('%7E', '~', rawurlencode($input))
  36. );
  37. } else {
  38. return '';
  39. }
  40. }
  41. public function get_normalized_http_url($url) {
  42. $parts = parse_url($url);
  43. $port = @$parts['port'];
  44. $scheme = $parts['scheme'];
  45. $host = $parts['host'];
  46. $path = @$parts['path'];
  47. $port or $port = ($scheme == 'https') ? '443' : '80';
  48. if (($scheme == 'https' && $port != '443')|| ($scheme == 'http' && $port != '80')) {
  49. $host = "$host:$port";
  50. }
  51. return "$scheme://$host$path";
  52. }
  53. public function get_signature_base_string($url, $params) {
  54. $parts = array( 'GET', $this->get_normalized_http_url($url), $params); //prr($parts);
  55. $parts = $this->urlencode_rfc3986($parts);
  56. return implode('&', $parts);
  57. }
  58. public function sign2($request, $consumer_secret, $token){
  59. $base_string = $this->get_signature_base_string($request['normalized_url'], $request['normalized_parameters']);
  60. //$request->base_string = $base_string;
  61. //$key_parts = array( $consumer_secret); if ($token) $key_parts[] = $token->secret;
  62. $key_parts = array( $consumer_secret, ($token) ? $token : "");
  63. //$key_parts = array( $consumer_secret, "");
  64. $key_parts = $this->urlencode_rfc3986($key_parts);
  65. $key = implode('&', $key_parts); //prr($key); prr($base_string);
  66. return rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));
  67. }
  68. public function check($request, $consumer_secret, $token, $signature) {
  69. $built = $this->sign($request, $consumer_secret, $token);
  70. return $built == $signature;
  71. }
  72. }
  73. class wpPlurkOAuth{
  74. public $baseURL = 'http://www.plurk.com';
  75. public $http_code;
  76. protected $version = '1.0';
  77. protected $sign_method;
  78. protected $request_token;
  79. protected $access_token;
  80. protected $access_secret;
  81. protected $consumer_key;
  82. protected $consumer_secret;
  83. function __construct($consumer_key, $consumer_secret, $access_token = NULL, $access_secret = NULL) {
  84. $this->sign_method = new nxspk_SigMethod_HMAC_SHA1();
  85. $this->consumer_key = $consumer_key; $this->consumer_secret = $consumer_secret;
  86. $this->access_token = $access_token; $this->access_secret = $access_secret;
  87. // if (!empty($access_token) && !empty($access_secret)) $this->authorize($access_token, $access_secret);
  88. }
  89. function get_normalized_parameters($params) { $items = array();
  90. foreach ($params as $key => $value) {
  91. if ($key == 'oauth_signature') continue;
  92. if (is_array($value)) $mtems = array_merge($value, $items); else $items[$key] = $value;
  93. }
  94. ksort($items);
  95. $item_parts = array();
  96. foreach ($items as $key => $value) {
  97. $item_parts[] =
  98. sprintf("%s=%s",rawurlencode($key),rawurlencode($value));
  99. }
  100. return implode('&', $item_parts);
  101. }
  102. function genRndString($length = 8) { $chars = '0123456789abcdefghijklmnopqrstuvwxyz'; $string = '';
  103. for ($p = 0; $p < $length; $p++) $string .= $chars[mt_rand(0, strlen($chars)-1)];
  104. return $string;
  105. }
  106. function makeHTTPHeaders($ref, $post=false){ $hdrsArr = array();
  107. $hdrsArr['X-Requested-With']='XMLHttpRequest'; $hdrsArr['Connection']='keep-alive'; $hdrsArr['Referer']=$ref;
  108. $hdrsArr['User-Agent']='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.22 Safari/537.11';
  109. if($post) $hdrsArr['Content-Type']='application/x-www-form-urlencoded';
  110. $hdrsArr['Accept']='application/json, text/javascript, */*; q=0.01';
  111. $hdrsArr['Accept-Encoding']='gzip,deflate,sdch'; $hdrsArr['Accept-Language']='en-US,en;q=0.8'; $hdrsArr['Accept-Charset']='ISO-8859-1,utf-8;q=0.7,*;q=0.3'; return $hdrsArr;
  112. }
  113. function oAuthRespToArr($str){ $arr = explode('&', $str); $out = array();
  114. foreach ($arr as $ar) { $strr = explode('=', $ar); $out[$strr[0]] = $strr[1];} return $out;
  115. }
  116. function getReqToken($cbu){
  117. $args = array (
  118. 'oauth_consumer_key' => $this->consumer_key,
  119. 'oauth_timestamp' => time(),
  120. 'oauth_nonce' => $this->genRndString(),
  121. 'oauth_version' => $this->version,
  122. 'oauth_callback' => $cbu,
  123. 'oauth_signature_method' => 'HMAC-SHA1'
  124. );
  125. $req = array(); $req['method'] = 'GET';
  126. $req['normalized_url'] = $this->baseURL.PLURK_REQUEST_TOKEN_PATH;
  127. $req['normalized_parameters'] = $this->get_normalized_parameters($args);
  128. $args['oauth_signature'] = $this->sign_method->sign2($req, $this->consumer_secret, $token);
  129. $cbu = nxspk_SigMethod_HMAC_SHA1::urlencode_rfc3986($cbu);
  130. $url = $this->baseURL.PLURK_REQUEST_TOKEN_PATH.'?oauth_nonce='.$args['oauth_nonce'].'&oauth_timestamp='.$args['oauth_timestamp'].'&oauth_consumer_key='.$this->consumer_key.'&oauth_signature_method='.$args['oauth_signature_method'].'&oauth_version='.$args['oauth_version'].'&oauth_callback='.$cbu.'&oauth_signature='.$args['oauth_signature'];
  131. echo "<br/>REQ Token URL: ".$url."<br/>";
  132. $hdrsArr = $this->makeHTTPHeaders($url); $ckArr = $nxs_vbCkArray;
  133. $response = wp_remote_get($url, array( 'method' => 'GET', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr, 'cookies' => $ckArr));
  134. if (is_nxs_error($response)){ $badOut = print_r($response, true)." - Connection ERROR"; return $badOut; }
  135. $this->http_code = $response['response']['code']; // prr($response);
  136. if (stripos($response['body'],'oauth_token_secret=')===false) echo 'Bad oAuth Login:'.$response['body']; else return $this->oAuthRespToArr($response['body']);
  137. }
  138. function getAccToken($verifier){
  139. $args = array (
  140. 'oauth_token' => $this->access_token,
  141. 'oauth_token_secret' => $this->access_secret,
  142. 'oauth_timestamp' => time(),
  143. 'oauth_nonce' => $this->genRndString(),
  144. 'oauth_version' => $this->version,
  145. 'oauth_consumer_key' => $this->consumer_key,
  146. 'oauth_verifier' => $verifier,
  147. 'oauth_signature_method' => 'HMAC-SHA1'
  148. );
  149. $req = array(); $req['method'] = 'GET'; $req['normalized_url'] = $this->baseURL.PLURK_ACCESS_TOKEN_PATH; // echo "ARGS:"; prr($args);
  150. $req['normalized_parameters'] = $this->get_normalized_parameters($args);
  151. $args['oauth_signature'] = $this->sign_method->sign2($req, $this->consumer_secret, $this->access_secret);
  152. $url = $this->baseURL.PLURK_ACCESS_TOKEN_PATH.'?oauth_nonce='.$args['oauth_nonce'].'&oauth_timestamp='.$args['oauth_timestamp'].'&oauth_token_secret='.$this->access_secret.'&oauth_signature_method='.$args['oauth_signature_method'].'&oauth_consumer_key='.$this->consumer_key.'&oauth_verifier='.$verifier.'&oauth_version='.$args['oauth_version'].'&oauth_token='.$this->access_token.'&oauth_signature='.$args['oauth_signature'];
  153. echo "<br/>REQ Token URL: ".$url."<br/>";
  154. $hdrsArr = $this->makeHTTPHeaders($url); $ckArr = $nxs_vbCkArray;
  155. $response = wp_remote_get($url, array( 'method' => 'GET', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr, 'cookies' => $ckArr));
  156. if ( is_wp_error($response) ) return $response;
  157. $this->http_code = $response['response']['code'];
  158. if (stripos($response['body'],'oauth_token_secret=')===false) echo 'Bad oAuth Login:'.$response['body']; else return $this->oAuthRespToArr($response['body']);
  159. }
  160. function makeReq($url, $params){
  161. $args = array (
  162. 'oauth_token' => $this->access_token,
  163. 'oauth_token_secret' => $this->access_secret,
  164. 'oauth_consumer_key' => $this->consumer_key,
  165. 'oauth_timestamp' => time(),
  166. 'oauth_nonce' => $this->genRndString(),
  167. 'oauth_version' => $this->version,
  168. 'oauth_signature_method' => 'HMAC-SHA1'
  169. );
  170. if (is_array($params)) { $argsTS = array_merge($args, $params);} else $argsTS = $args;
  171. $req = array(); $req['method'] = 'GET'; $req['normalized_url'] = $url;
  172. $req['normalized_parameters'] = $this->get_normalized_parameters($argsTS);
  173. $args['oauth_signature'] = $this->sign_method->sign2($req, $this->consumer_secret, $this->access_secret);
  174. if (is_array($params)) { $params = nxspk_SigMethod_HMAC_SHA1::urlencode_rfc3986($params); $args = array_merge($args, $params);} //prr($args);
  175. $argsStr = ''; $argsT = array(); foreach ($args as $arN=>$arV){$argsT[] = $arN.'='.$arV;} $argsStr = implode('&', $argsT); $url .= '?'.$argsStr;
  176. $hdrsArr = $this->makeHTTPHeaders($url); $ckArr = '';
  177. $response = wp_remote_get($url, array( 'method' => 'GET', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr, 'cookies' => $ckArr)); // prr($response);
  178. if ( is_wp_error($response) ) return $response;
  179. $this->http_code = $response['response']['code'];
  180. return json_decode($response['body'], true);
  181. }
  182. }
  183. ?>