PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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