PageRenderTime 52ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress/wp-content/plugins/wp-connect/OAuth/tianya_OAuth.php

http://ownerpress.googlecode.com/
PHP | 230 lines | 125 code | 24 blank | 81 comment | 19 complexity | 79903d6e0fef2354a45ee684221ae229 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * ?????
  4. *
  5. * @package sae
  6. * @author Easy Chen
  7. * @version 1.0
  8. */
  9. class tianyaClient
  10. {
  11. // ????
  12. function __construct( $akey , $skey , $accecss_token , $accecss_token_secret )
  13. {
  14. $this->oauth = new tianyaOAuth( $akey , $skey , $accecss_token , $accecss_token_secret );
  15. $this->appkey = $akey;
  16. $this->secret = $accecss_token_secret;
  17. }
  18. // ????(?????)
  19. function update( $text, $value )
  20. {
  21. $param['word'] = $text;
  22. $param['appkey'] = $this->appkey;
  23. $param['oauth_token_secret'] = $this->secret;
  24. if ($value[0] == "image" && $value[1]) {
  25. $param['media'] = $value[1];
  26. return $this->oauth->post( 'http://open.tianya.cn/api/weibo/addimg.php' , $param , true );
  27. } else {
  28. return $this->oauth->post( 'http://open.tianya.cn/api/weibo/add.php' , $param );
  29. }
  30. }
  31. function get_user_info()
  32. {
  33. $param['appkey'] = $this->appkey;
  34. $param['oauth_token_secret'] = $this->secret;
  35. return $this->oauth->get( 'http://open.tianya.cn/api/user/info.php', $param );
  36. }
  37. }
  38. /**
  39. * ?? OAuth ???
  40. *
  41. * @package sae
  42. * @author Easy Chen
  43. * @version 1.0
  44. */
  45. class tianyaOAuth {
  46. // Contains the last HTTP status code returned.
  47. public $http_code;
  48. // Contains the last API call.
  49. public $url;
  50. // Set up the API root URL.
  51. public $host = "http://open.tianya.cn/";
  52. // Set timeout default.
  53. public $timeout = 30;
  54. // Set connect timeout.
  55. public $connecttimeout = 30;
  56. // Verify SSL Cert.
  57. public $ssl_verifypeer = FALSE;
  58. // Respons format.
  59. public $format = 'json';
  60. // Decode returned json data.
  61. public $decode_json = TRUE;
  62. // Contains the last HTTP headers returned.
  63. public $http_info;
  64. // Set the useragnet.
  65. public $useragent = 'Tianya OAuth v0.1.0';
  66. /* Immediately retry the API call if the response was not successful. */
  67. //public $retry = TRUE;
  68. /**
  69. *Set API URLS
  70. */
  71. function accessTokenURL() { return 'http://open.tianya.cn/oauth/access_token.php'; }
  72. function authorizeURL() { return 'http://open.tianya.cn/oauth/authorize.php'; }
  73. function requestTokenURL() { return 'http://open.tianya.cn/oauth/request_token.php'; }
  74. /**
  75. * Debug helpers
  76. */
  77. function lastStatusCode() { return $this->http_status; }
  78. function lastAPICall() { return $this->last_api_call; }
  79. /**
  80. * construct WeiboOAuth object
  81. */
  82. function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
  83. $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
  84. $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
  85. if (!empty($oauth_token) && !empty($oauth_token_secret)) {
  86. $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
  87. } else {
  88. $this->token = NULL;
  89. }
  90. }
  91. /**
  92. * Get a request_token from Weibo
  93. *
  94. * @return array a key/value array containing oauth_token and oauth_token_secret
  95. */
  96. function getRequestToken($oauth_callback = NULL) {
  97. $parameters = array();
  98. if (!empty($oauth_callback)) {
  99. $parameters['oauth_callback'] = $oauth_callback;
  100. }
  101. $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
  102. $token = OAuthUtil::parse_parameters($request);
  103. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  104. return $token;
  105. }
  106. /**
  107. * Get the authorize URL
  108. *
  109. * @return string
  110. */
  111. function getAuthorizeURL($token, $sign_in_with_Weibo = TRUE , $url) {
  112. if (is_array($token)) {
  113. $token = $token['oauth_token'];
  114. }
  115. return $this->authorizeURL() . "?oauth_token={$token}&oauth_callback=" . urlencode($url);
  116. }
  117. /**
  118. * Exchange the request token and secret for an access token and
  119. * secret, to sign API calls.
  120. *
  121. * @return array array("oauth_token" => the access token,
  122. * "oauth_token_secret" => the access secret)
  123. */
  124. function getAccessToken($oauth_verifier = FALSE, $oauth_token = false) {
  125. $parameters = array();
  126. if (!empty($oauth_verifier)) {
  127. $parameters['oauth_verifier'] = $oauth_verifier;
  128. }
  129. $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
  130. $token = OAuthUtil::parse_parameters($request);
  131. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  132. return $token;
  133. }
  134. /**
  135. * GET wrappwer for oAuthRequest.
  136. *
  137. * @return mixed
  138. */
  139. function get($url, $parameters = array()) {
  140. $response = $this->oAuthRequest($url, 'GET', $parameters);
  141. if ($this->format === 'json' && $this->decode_json) {
  142. return json_decode($response, true);
  143. }
  144. return $response;
  145. }
  146. /**
  147. * POST wreapper for oAuthRequest.
  148. *
  149. * @return mixed
  150. */
  151. function post($url, $parameters = array() , $multi = false) {
  152. $response = $this->oAuthRequest($url, 'POST', $parameters , $multi );
  153. if ($this->format === 'json' && $this->decode_json) {
  154. return json_decode($response, true);
  155. }
  156. return $response;
  157. }
  158. /**
  159. * DELTE wrapper for oAuthReqeust.
  160. *
  161. * @return mixed
  162. */
  163. function delete($url, $parameters = array()) {
  164. $response = $this->oAuthRequest($url, 'DELETE', $parameters);
  165. if ($this->format === 'json' && $this->decode_json) {
  166. return json_decode($response, true);
  167. }
  168. return $response;
  169. }
  170. /**
  171. * Format and sign an OAuth / API request
  172. *
  173. * @return string
  174. */
  175. function oAuthRequest($url, $method, $parameters , $multi = false) {
  176. if (strrpos($url, 'http://') !== 0 && strrpos($url, 'http://') !== 0) {
  177. $url = "{$this->host}{$url}.{$this->format}";
  178. }
  179. // echo $url ;
  180. $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
  181. $request->sign_request($this->sha1_method, $this->consumer, $this->token);
  182. switch ($method) {
  183. case 'GET':
  184. //echo $request->to_url();
  185. return $this->http($request->to_url(), 'GET');
  186. default:
  187. return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata($multi) , $multi );
  188. }
  189. }
  190. /**
  191. * Make an HTTP request
  192. *
  193. * @return string API results
  194. */
  195. function http($url, $method, $postfields = null , $multi = false) {
  196. $params = array(
  197. "method" => $method,
  198. "timeout" => $this -> timeout,
  199. "user-agent" => $this -> useragent,
  200. "sslverify" => $this -> ssl_verifypeer,
  201. "body" => $postfields,
  202. "headers" => ($multi) ? array("Content-Type" => "multipart/form-data; boundary=" . OAuthUtil :: $boundary , "Expect: ") : ''
  203. );
  204. return class_http($url, $params);
  205. }
  206. }