wpScoopITOAuth

The NextScripts: Social Networks Auto-Poster wpScoopITOAuth class.

Defined (1)

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

/inc-cl/apis/scOAuth.php  
  1. class wpScoopITOAuth{ 
  2. public $baseURL = 'http://www.scoop.it'; 
  3. public $request_token_path = '/oauth/request'; 
  4. public $access_token_path = '/oauth/access'; 
  5. public $http_code; 
  6. protected $version = '1.0'; 
  7. protected $sign_method; 
  8. protected $request_token; 
  9. protected $access_token; 
  10. protected $access_secret; 
  11. protected $consumer_key;  
  12. protected $consumer_secret;  
  13.  
  14. function __construct($consumer_key, $consumer_secret, $access_token = NULL, $access_secret = NULL) {  
  15. $this->sign_method = new nxssc_SigMethod_HMAC_SHA1();  
  16. $this->consumer_key = $consumer_key; $this->consumer_secret = $consumer_secret; 
  17. $this->access_token = $access_token; $this->access_secret = $access_secret; 
  18. // if (!empty($access_token) && !empty($access_secret)) $this->authorize($access_token, $access_secret); 
  19.  
  20. function get_normalized_parameters($params) { $items = array(); 
  21. foreach ($params as $key => $value) { 
  22. if ($key == 'oauth_signature') continue; 
  23. if (is_array($value)) $mtems = array_merge($value, $items); else $items[$key] = $value; 
  24. ksort($items); 
  25. $item_parts = array(); 
  26. foreach ($items as $key => $value) { 
  27. $item_parts[] = 
  28. sprintf("%s=%s", rawurlencode($key), rawurlencode($value)); 
  29. return implode('&', $item_parts); 
  30. function genRndString($length = 8) { $chars = '0123456789abcdefghijklmnopqrstuvwxyzz'; $string = ''; 
  31. for ($p = 0; $p < $length; $p++) $string .= $chars[mt_rand(0, strlen($chars)-1)]; 
  32. return $string; 
  33. function makeHTTPHeaders($ref, $post=false) { $hdrsArr = array();  
  34. $hdrsArr['X-Requested-With']='XMLHttpRequest'; $hdrsArr['Connection']='keep-alive'; $hdrsArr['Referer']=$ref; 
  35. $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'; 
  36. if($post) $hdrsArr['Content-Type']='application/x-www-form-urlencoded';  
  37. $hdrsArr['Accept']='application/json, text/javascript, */*; q=0.01';  
  38. $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; 
  39.  
  40. function oAuthRespToArr($str) { $arr = explode('&', $str); $out = array(); 
  41. foreach ($arr as $ar) { $strr = explode('=', $ar); $out[$strr[0]] = $strr[1];} return $out; 
  42.  
  43. function getReqToken($cbu) {  
  44. $args = array ( 
  45. 'oauth_consumer_key' => $this->consumer_key,  
  46. 'oauth_timestamp' => time(),  
  47. 'oauth_nonce' => $this->genRndString(),  
  48. 'oauth_version' => $this->version,  
  49. 'oauth_callback' => $cbu,  
  50. 'oauth_signature_method' => 'HMAC-SHA1' 
  51.  
  52. );  
  53. $req = array(); $req['method'] = 'GET'; $token = ''; 
  54. $req['normalized_url'] = $this->baseURL.$this->request_token_path;  
  55. $req['normalized_parameters'] = $this->get_normalized_parameters($args); 
  56. $args['oauth_signature'] = $this->sign_method->sign2($req, $this->consumer_secret, $token);  
  57. $cbu = nxssc_SigMethod_HMAC_SHA1::urlencode_rfc3986($cbu);  
  58. $url = $this->baseURL.$this->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'];  
  59. echo "<br/>REQ Token URL: ".$url."<br/>"; 
  60. $hdrsArr = $this->makeHTTPHeaders($url); $ckArr = '';  
  61. $response = wp_remote_get($url, array( 'method' => 'GET', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr, 'cookies' => $ckArr));  
  62. if ( is_wp_error($response) ) return print_r($response, true); 
  63. $this->http_code = $response['response']['code']; // prr($response); 
  64. if (stripos($response['body'], 'oauth_token_secret=')===false) echo 'Bad oAuth Login:'.$response['body']; else return $this->oAuthRespToArr($response['body']); 
  65. function getAccToken($verifier) { 
  66. $args = array ( 
  67. 'oauth_token' => $this->access_token,  
  68. 'oauth_token_secret' => $this->access_secret,  
  69. 'oauth_timestamp' => time(),  
  70. 'oauth_nonce' => $this->genRndString(),  
  71. 'oauth_version' => $this->version,  
  72. 'oauth_consumer_key' => $this->consumer_key,  
  73. 'oauth_verifier' => $verifier,  
  74. 'oauth_signature_method' => 'HMAC-SHA1'  
  75. );  
  76. $req = array(); $req['method'] = 'GET'; $req['normalized_url'] = $this->baseURL.$this->access_token_path; // echo "ARGS:"; prr($args);  
  77. $req['normalized_parameters'] = $this->get_normalized_parameters($args); 
  78. $args['oauth_signature'] = $this->sign_method->sign2($req, $this->consumer_secret, $this->access_secret);  
  79. $url = $this->baseURL.$this->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']; 
  80. echo "<br/>REQ Token URL: ".$url."<br/>"; 
  81. $hdrsArr = $this->makeHTTPHeaders($url); $ckArr = '';  
  82. $response = wp_remote_get($url, array( 'method' => 'GET', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr, 'cookies' => $ckArr));  
  83. if ( is_wp_error($response) ) return $response; 
  84. $this->http_code = $response['response']['code'];  
  85. if (stripos($response['body'], 'oauth_token_secret=')===false) echo 'Bad oAuth Login:'.$response['body']; else return $this->oAuthRespToArr($response['body']);  
  86.  
  87. public function to_header($params, $realm=null) { $first = true; prr($params); 
  88. if($realm) { $out = 'OAuth realm="' . nxssc_SigMethod_HMAC_SHA1::urlencode_rfc3986($realm) . '"'; $first = false; } else $out = 'OAuth'; $total = array(); 
  89. foreach ($params as $k => $v) { 
  90. if (substr($k, 0, 5) != "oauth") continue;  
  91. $out .= ($first) ? ' ' : ', '."\r"; $out .= nxssc_SigMethod_HMAC_SHA1::urlencode_rfc3986($k) . '="' . nxssc_SigMethod_HMAC_SHA1::urlencode_rfc3986($v) . '"'; 
  92. $first = false; 
  93. } prr($out); return $out; 
  94.  
  95. function makeReq($url, $params='', $type='GET') { 
  96. $args = array ( 
  97. 'oauth_token' => $this->access_token,  
  98. 'oauth_consumer_key' => $this->consumer_key,  
  99. 'oauth_timestamp' => time(),  
  100. 'oauth_nonce' => $this->genRndString(),  
  101. 'oauth_version' => $this->version,  
  102. 'oauth_signature_method' => 'HMAC-SHA1'  
  103. );  
  104. if (is_array($params)) { $argsTS = array_merge($args, $params);} else $argsTS = $args; 
  105. $req = array(); $req['method'] = $type; $req['normalized_url'] = $url;  
  106. // if (!empty($argsTS['photo'])) unset($argsTS['photo']); 
  107. $req['normalized_parameters'] = $this->get_normalized_parameters($argsTS); // prr($req); 
  108. $args['oauth_signature'] = $this->sign_method->sign2($req, $this->consumer_secret, $this->access_secret);  
  109. if (is_array($params)) { if (isset($params['tag']) && is_array($params['tag']) ) { $tags = $params['tag']; unset($params['tag']); $argsAddStr = ''; foreach ($tags as $tg) $argsAddStr .= '&tag='.urlencode($tg); } 
  110. $params = nxssc_SigMethod_HMAC_SHA1::urlencode_rfc3986($params); $args = array_merge($args, $params); 
  111. } $argsStr = ''; $argsT = array(); uksort($args, 'strcmp'); foreach ($args as $arN=>$arV) {$argsT[] = $arN.'='.$arV;} $argsStr = implode('&', $argsT);  
  112. if ( $type=='GET') { $url .= '?'.$argsStr; $hdrsArr = $this->makeHTTPHeaders($url); $ckArr = ''; // prr($url); 
  113. $response = wp_remote_get($url, array( 'method' => 'GET', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr)); //prr($response); 
  114. } else { $hdrsArr = $this->makeHTTPHeaders($url, true); if (!empty($argsAddStr)) $argsStr .= $argsAddStr; //prr($url); prr($hdrsArr); prr($argsStr); prr($argsT); 
  115. $response = wp_remote_post($url, array( 'timeout' => 45, 'redirection' => 0, 'body'=>$argsStr, 'headers' => $hdrsArr)); //prr($argsStr); prr($argsT); prr($response); 
  116. if ( is_wp_error($response) ) return $response; 
  117. $this->http_code = $response['response']['code']; $body = $response['body']; $body = maybe_unserialize($body); if (is_array($body)) return $body; else return json_decode($body, true);  
  118.  
  119. private function joinParameters($parameters) { $keys = array_keys($parameters); sort($keys, SORT_STRING); $keyValuePairs = array(); 
  120. foreach ($keys as $k) array_push($keyValuePairs, rawurlencode($k) . "=" . rawurlencode($parameters[$k])); return implode("&", $keyValuePairs); 
  121. private function sign($url, &$parameters) { $string = implode("&", array( 'POST', rawurlencode($url), rawurlencode($this->joinParameters($parameters))));  
  122. $parameters['oauth_signature'] = base64_encode(hash_hmac('sha1', $string, $this->consumer_secret.'&'.$this->access_secret, true)); 
  123. private function httpRequest($url, $parameters) { $curl = curl_init(); 
  124. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_TIMEOUT, '25'); curl_setopt($curl, CURLOPT_URL, $url); 
  125. curl_setopt($curl, CURLOPT_POST, TRUE); curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);  
  126. $response = curl_exec($curl); $headers = curl_getinfo($curl); $errmsg = curl_error($curl); curl_close($curl); 
  127. if (!empty($errmsg)) return $errmsg; else return $response; 
  128.  
  129. public function flUploadPhoto ($imgFile, $parameters) { $url = 'https://www.flickr.com/services/upload/'; $requestParams = ($parameters == NULL ? array() : $parameters); $phid = '';  
  130. $oauthParams = array (  
  131. 'oauth_nonce' => $this->genRndString(),  
  132. 'oauth_timestamp' => time(),  
  133. 'oauth_token' => $this->access_token,  
  134. 'oauth_consumer_key' => $this->consumer_key,  
  135. 'oauth_signature_method' => 'HMAC-SHA1',  
  136. 'oauth_version' => '1.0',  
  137. ); 
  138. $requestParams = array_merge($requestParams, $oauthParams); $this->sign($url, $requestParams); $requestParams['photo'] = $imgFile; // prr($requestParams);  
  139. $xml = $this->httpRequest($url, $requestParams); if (stripos($xml, '</photoid>')!==false && stripos($xml, 'stat="ok"')!==false) $phid = CutFromTo($xml, '<photoid>', '</photoid>'); 
  140. return empty($phid) ? "Problem: ".$xml."<pre>".print_r($requestParams, true)."</pre>" : $phid; 
  141.